From d66fde21858fa9e42f1f559581edd9a4433af02a Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Tue, 3 Jun 2014 23:27:02 +1000 Subject: [PATCH] Make the main listener socket always return a response and create a message input/sending loop in ckpmsg --- src/ckpmsg.c | 45 ++++++++++++++++++++++++++++++++++++++++++--- src/ckpool.c | 16 +++++++--------- 2 files changed, 49 insertions(+), 12 deletions(-) diff --git a/src/ckpmsg.c b/src/ckpmsg.c index 6799da94..0227dca1 100644 --- a/src/ckpmsg.c +++ b/src/ckpmsg.c @@ -9,6 +9,7 @@ #include "config.h" +#include #include #include @@ -16,7 +17,7 @@ int main(int argc, char **argv) { - char *name = NULL, *socket_dir = NULL; + char *name = NULL, *socket_dir = NULL, *buf = NULL; bool proxy = false; int c; @@ -43,10 +44,48 @@ int main(int argc, char **argv) name = strdup("ckpool"); } realloc_strcat(&socket_dir, name); - free(name); + dealloc(name); trail_slash(&socket_dir); realloc_strcat(&socket_dir, "listener"); - free(socket_dir); + while (42) { + int sockd, len; + size_t n; + + dealloc(buf); + len = getline(&buf, &n, stdin); + if (len == -1) { + LOGERR("Failed to get a valid line"); + break; + } + len = strlen(buf); + if (len < 2) { + LOGERR("No message"); + continue; + } + buf[len - 1] = '\0'; // Strip /n + LOGDEBUG("Got message: %s", buf); + + sockd = open_unix_client(socket_dir); + if (sockd < 0) { + LOGERR("Failed to open socket: %s", socket_dir); + break; + } + if (!send_unix_msg(sockd, buf)) { + LOGERR("Failed to end unix msg: %s", buf); + break; + } + dealloc(buf); + buf = recv_unix_msg(sockd); + close(sockd); + if (!buf) { + LOGERR("Received empty message"); + continue; + } + LOGNOTICE("Received response: %s", buf); + } + + dealloc(buf); + dealloc(socket_dir); return 0; } diff --git a/src/ckpool.c b/src/ckpool.c index 0ed05683..64014054 100644 --- a/src/ckpool.c +++ b/src/ckpool.c @@ -74,6 +74,7 @@ void logmsg(int loglevel, const char *fmt, ...) { } } +/* Listen for incoming global requests. Always returns a response if possible */ static void *listener(void *arg) { proc_instance_t *pi = (proc_instance_t *)arg; @@ -86,8 +87,6 @@ retry: dealloc(buf); sockd = accept(us->sockd, NULL, NULL); if (sockd < 0) { - if (interrupted()) - goto retry; LOGERR("Failed to accept on socket in listener"); goto out; } @@ -95,17 +94,16 @@ retry: buf = recv_unix_msg(sockd); if (!buf) { LOGWARNING("Failed to get message in listener"); - close(sockd); - goto retry; - } - if (!strncasecmp(buf, "shutdown", 8)) { + } else if (!strncasecmp(buf, "shutdown", 8)) { LOGWARNING("Listener received shutdown message, terminating ckpool"); - close(sockd); + send_unix_msg(sockd, "exiting"); goto out; - } - if (!strncasecmp(buf, "ping", 4)) { + } else if (!strncasecmp(buf, "ping", 4)) { LOGDEBUG("Listener received ping request"); send_unix_msg(sockd, "pong"); + } else { + LOGINFO("Listener received unhandled message: %s", buf); + send_unix_msg(sockd, "unknown"); } close(sockd); goto retry;