Browse Source

Pass unix message length with unix_recv

master
Con Kolivas 9 years ago
parent
commit
7ad63b09e2
  1. 4
      src/ckpool.c
  2. 1
      src/ckpool.h
  3. 28
      src/libckpool.c
  4. 3
      src/libckpool.h

4
src/ckpool.c

@ -223,6 +223,7 @@ static void *unix_receiver(void *arg)
while (42) {
unix_msg_t *umsg;
uint32_t msglen;
char *buf;
sockd = accept(rsockd, NULL, NULL);
@ -231,7 +232,7 @@ static void *unix_receiver(void *arg)
childsighandler(15);
break;
}
buf = recv_unix_msg(sockd);
buf = recv_unix(sockd, &msglen);
if (unlikely(!buf)) {
Close(sockd);
LOGWARNING("Failed to get message on %s socket", qname);
@ -240,6 +241,7 @@ static void *unix_receiver(void *arg)
umsg = ckalloc(sizeof(unix_msg_t));
umsg->sockd = sockd;
umsg->buf = buf;
umsg->msglen = msglen;
mutex_lock(&pi->rmsg_lock);
DL_APPEND(pi->unix_msgs, umsg);

1
src/ckpool.h

@ -39,6 +39,7 @@ struct unix_msg {
unix_msg_t *prev;
int sockd;
char *buf;
uint32_t msglen;
};
struct ckmsgq {

28
src/libckpool.c

@ -997,12 +997,13 @@ int read_length(int sockd, void *buf, int len)
/* Use a standard message across the unix sockets:
* 4 byte length of message as little endian encoded uint32_t followed by the
* string. Return NULL in case of failure. */
char *_recv_unix_msg(int sockd, int timeout1, int timeout2, const char *file, const char *func, const int line)
char *_recv_unix(int sockd, uint32_t *msglen, int timeout1, int timeout2, const char *file,
const char *func, const int line)
{
char *buf = NULL;
uint32_t msglen;
int ret, ern;
*msglen = 0;
ret = wait_read_select(sockd, timeout1);
if (unlikely(ret < 1)) {
ern = errno;
@ -1010,15 +1011,15 @@ char *_recv_unix_msg(int sockd, int timeout1, int timeout2, const char *file, co
goto out;
}
/* Get message length */
ret = read_length(sockd, &msglen, 4);
ret = read_length(sockd, msglen, 4);
if (unlikely(ret < 4)) {
ern = errno;
LOGERR("Failed to read 4 byte length in recv_unix_msg (%d?)", ern);
goto out;
}
msglen = le32toh(msglen);
if (unlikely(msglen < 1 || msglen > 0x80000000)) {
LOGWARNING("Invalid message length %u sent to recv_unix_msg", msglen);
*msglen = le32toh(*msglen);
if (unlikely(*msglen < 1 || *msglen > 0x80000000)) {
LOGWARNING("Invalid message length %u sent to recv_unix_msg", *msglen);
goto out;
}
ret = wait_read_select(sockd, timeout2);
@ -1027,11 +1028,11 @@ char *_recv_unix_msg(int sockd, int timeout1, int timeout2, const char *file, co
LOGERR("Select2 failed in recv_unix_msg (%d)", ern);
goto out;
}
buf = ckzalloc(msglen + 1);
ret = read_length(sockd, buf, msglen);
if (unlikely(ret < (int)msglen)) {
buf = ckzalloc(*msglen + 1);
ret = read_length(sockd, buf, *msglen);
if (unlikely(ret < (int)*msglen)) {
ern = errno;
LOGERR("Failed to read %u bytes in recv_unix_msg (%d?)", msglen, ern);
LOGERR("Failed to read %u bytes in recv_unix_msg (%d?)", *msglen, ern);
dealloc(buf);
}
out:
@ -1041,6 +1042,13 @@ out:
return buf;
}
char *_recv_unix_msg(int sockd, int timeout1, int timeout2, const char *file, const char *func, const int line)
{
uint32_t msglen;
return _recv_unix(sockd, &msglen, timeout1, timeout2, file, func, line);
}
/* Emulate a select write wait for high fds that select doesn't support */
int wait_write_select(int sockd, float timeout)
{

3
src/libckpool.h

@ -517,9 +517,12 @@ int _open_unix_client(const char *server_path, const char *file, const char *fun
int wait_close(int sockd, int timeout);
int wait_read_select(int sockd, float timeout);
int read_length(int sockd, void *buf, int len);
char *_recv_unix(int sockd, uint32_t *msglen, int timeout1, int timeout2, const char *file,
const char *func, const int line);
char *_recv_unix_msg(int sockd, int timeout1, int timeout2, const char *file, const char *func, const int line);
#define RECV_UNIX_TIMEOUT1 30
#define RECV_UNIX_TIMEOUT2 5
#define recv_unix(sockd, msglen) _recv_unix(sockd, msglen, UNIX_READ_TIMEOUT, UNIX_READ_TIMEOUT, __FILE__, __func__, __LINE__)
#define recv_unix_msg(sockd) _recv_unix_msg(sockd, UNIX_READ_TIMEOUT, UNIX_READ_TIMEOUT, __FILE__, __func__, __LINE__)
#define recv_unix_msg_tmo(sockd, tmo) _recv_unix_msg(sockd, tmo, UNIX_READ_TIMEOUT, __FILE__, __func__, __LINE__)
#define recv_unix_msg_tmo2(sockd, tmo1, tmo2) _recv_unix_msg(sockd, tmo1, tmo2, __FILE__, __func__, __LINE__)

Loading…
Cancel
Save