From 6c4abba93c18b50eddeccc0f90b8602c3b386717 Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Tue, 10 Jun 2014 22:06:37 +1000 Subject: [PATCH] Provide a way to send binary data over unix sockets --- src/libckpool.c | 39 +++++++++++++++++++++++++++++++++++++++ src/libckpool.h | 2 ++ 2 files changed, 41 insertions(+) diff --git a/src/libckpool.c b/src/libckpool.c index 7e070dde..308c5154 100644 --- a/src/libckpool.c +++ b/src/libckpool.c @@ -722,6 +722,45 @@ int write_length(int sockd, const void *buf, int len) return ofs; } +bool _send_unix_data(int sockd, void *buf, uint32_t len, const char *file, const char *func, const int line) +{ + bool retval = false; + uint32_t msglen; + int ret; + + if (unlikely(!buf)) { + LOGWARNING("Null message sent to send_unix_msg"); + goto out; + } + msglen = htole32(len); + ret = wait_write_select(sockd, 5); + if (unlikely(ret < 1)) { + LOGERR("Select1 failed in send_unix_msg"); + goto out; + } + ret = write_length(sockd, &msglen, 4); + if (unlikely(ret < 4)) { + LOGERR("Failed to write 4 byte length in send_unix_msg"); + goto out; + } + ret = wait_write_select(sockd, 5); + if (unlikely(ret < 1)) { + LOGERR("Select2 failed in send_unix_msg"); + goto out; + } + ret = write_length(sockd, buf, len); + if (unlikely(ret < 0)) { + LOGERR("Failed to write %d bytes in send_unix_msg", len); + goto out; + } + retval = true; +out: + if (unlikely(!retval)) + LOGERR("Failure in send_unix_msg from %s %s:%d", file, func, line); + return retval; +} + + bool _send_unix_msg(int sockd, const char *buf, const char *file, const char *func, const int line) { uint32_t msglen, len; diff --git a/src/libckpool.h b/src/libckpool.h index 52a91dd6..29bea99c 100644 --- a/src/libckpool.h +++ b/src/libckpool.h @@ -344,6 +344,8 @@ int wait_write_select(int sockd, int timeout); int write_length(int sockd, const void *buf, int len); bool _send_unix_msg(int sockd, const char *buf, const char *file, const char *func, const int line); #define send_unix_msg(sockd, buf) _send_unix_msg(sockd, buf, __FILE__, __func__, __LINE__) +bool _send_unix_data(int sockd, void *buf, uint32_t len, const char *file, const char *func, const int line); +#define send_unix_data(sockd, buf, len) _send_unix_data(sockd, buf, len, __FILE__, __func__, __LINE__) const char *__json_array_string(json_t *val, unsigned int entry); char *json_array_string(json_t *val, unsigned int entry);