From 6f679e1cf40bbfd7222f59fcfc60ffd0baee54f1 Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Mon, 21 Apr 2014 13:46:18 +1000 Subject: [PATCH] Create the main read parse loop for the generator and send it a test message --- src/ckpool.c | 18 +++++++++++++++++- src/generator.c | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/src/ckpool.c b/src/ckpool.c index 1512cf84..963247c2 100644 --- a/src/ckpool.c +++ b/src/ckpool.c @@ -124,6 +124,7 @@ static void launch_process(proc_instance_t *pi) { pid_t pid; + create_process_unixsock(pi); pid = fork(); if (pid < 0) quit(1, "Failed to fork %s in launch_process", pi->processname); @@ -132,7 +133,6 @@ static void launch_process(proc_instance_t *pi) rename_proc(pi->processname); write_namepid(pi); - create_process_unixsock(pi); ret = pi->process(pi); close_unix_socket(pi->us.sockd, pi->us.path); rm_namepid(pi); @@ -194,6 +194,20 @@ static void parse_config(ckpool_t *ckp) json_decref(json_conf); } +static void test_functions(ckpool_t *ckp) +{ + char *path = ckp->generator.us.path; + int genfd; + + genfd = open_unix_client(ckp->generator.us.path); + if (genfd < 0) { + LOGWARNING("Failed to open generator socket %s", path); + return; + } + send_unix_msg(genfd, "shutdown"); + close(genfd); +} + int main(int argc, char **argv) { struct sigaction handler; @@ -261,6 +275,8 @@ int main(int argc, char **argv) sigaction(SIGTERM, &handler, NULL); sigaction(SIGINT, &handler, NULL); + test_functions(&ckp); + /* Shutdown from here */ join_pthread(pth_listener); diff --git a/src/generator.c b/src/generator.c index ff75400b..2026770e 100644 --- a/src/generator.c +++ b/src/generator.c @@ -9,13 +9,50 @@ #include "config.h" +#include #include +#include #include "ckpool.h" #include "libckpool.h" #include "generator.h" #include "bitcoin.h" +static int gen_loop(proc_instance_t *pi) +{ + unixsock_t *us = &pi->us; + int sockd, ret = 0; + gbtbase_t gbt; + char *buf; + + memset(&gbt, 0, sizeof(gbt)); +retry: + sockd = accept(us->sockd, NULL, NULL); + if (sockd < 0) { + if (interrupted()) + goto retry; + LOGERR("Failed to accept on generator socket"); + ret = 1; + goto out; + } + + buf = recv_unix_msg(sockd); + if (!buf) { + LOGWARNING("Failed to get message in gen_loop"); + close(sockd); + goto retry; + } + LOGDEBUG("Generator received request: %s", buf); + if (!strncmp(buf, "shutdown", 8)) + goto out; + dealloc(buf); + close(sockd); + goto retry; + +out: + return ret; +} + int generator(proc_instance_t *pi) { ckpool_t *ckp = pi->ckp; @@ -64,6 +101,8 @@ int generator(proc_instance_t *pi) goto out; } clear_gbtbase(&gbt); + + ret = gen_loop(pi); out: /* Clean up here */ dealloc(cs.url);