From 7ad63b09e2e5e1e0662a4e7fbc24c9fabe64ae46 Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Tue, 26 Jan 2016 16:25:35 +1100 Subject: [PATCH] Pass unix message length with unix_recv --- src/ckpool.c | 4 +++- src/ckpool.h | 1 + src/libckpool.c | 28 ++++++++++++++++++---------- src/libckpool.h | 3 +++ 4 files changed, 25 insertions(+), 11 deletions(-) diff --git a/src/ckpool.c b/src/ckpool.c index 0102755b..7eb3e2da 100644 --- a/src/ckpool.c +++ b/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); diff --git a/src/ckpool.h b/src/ckpool.h index 12b35a83..39c32227 100644 --- a/src/ckpool.h +++ b/src/ckpool.h @@ -39,6 +39,7 @@ struct unix_msg { unix_msg_t *prev; int sockd; char *buf; + uint32_t msglen; }; struct ckmsgq { diff --git a/src/libckpool.c b/src/libckpool.c index 79939eb9..07e1f0dd 100644 --- a/src/libckpool.c +++ b/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) { diff --git a/src/libckpool.h b/src/libckpool.h index 097b2751..58a1b5b7 100644 --- a/src/libckpool.h +++ b/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__)