Browse Source

Make the main listener socket always return a response and create a message input/sending loop in ckpmsg

master
Con Kolivas 11 years ago
parent
commit
d66fde2185
  1. 45
      src/ckpmsg.c
  2. 16
      src/ckpool.c

45
src/ckpmsg.c

@ -9,6 +9,7 @@
#include "config.h"
#include <stdio.h>
#include <string.h>
#include <unistd.h>
@ -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;
}

16
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;

Loading…
Cancel
Save