diff --git a/configure.ac b/configure.ac index 33afd572..93ed7a61 100644 --- a/configure.ac +++ b/configure.ac @@ -37,7 +37,7 @@ AC_CHECK_LIB(jansson, json_loads, , AC_CHECK_HEADERS(stdio.h stdlib.h fcntl.h sys/time.h unistd.h) AC_CHECK_HEADERS(ctype.h errno.h byteswap.h string.h time.h) AC_CHECK_HEADERS(endian.h sys/endian.h arpa/inet.h syslog.h) -AC_CHECK_HEADERS(alloca.h pthread.h stdio.h math.h) +AC_CHECK_HEADERS(alloca.h pthread.h stdio.h math.h signal.h) AC_CHECK_HEADERS(sys/types.h sys/socket.h sys/stat.h linux/un.h netdb.h) AC_CHECK_HEADERS(stdint.h netinet/in.h netinet/tcp.h) AC_CHECK_HEADERS(jansson.h) diff --git a/src/Makefile.am b/src/Makefile.am index a9d3c79c..7c810063 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -5,5 +5,5 @@ libckpool_la_SOURCES = libckpool.c libckpool.h sha2.c sha2.h libckpool_la_LIBADD = @PTHREAD_LIBS@ @MATH_LIBS@ @RT_LIBS@ @JANSSON_LIBS@ bin_PROGRAMS = ckpool -ckpool_SOURCES = ckpool.c ckpool.h bitcoin.c bitcoin.h +ckpool_SOURCES = ckpool.c ckpool.h generator.c generator.h bitcoin.c bitcoin.h ckpool_LDADD = libckpool.la diff --git a/src/ckpool.c b/src/ckpool.c index 9922cd4f..fe65158d 100644 --- a/src/ckpool.c +++ b/src/ckpool.c @@ -7,55 +7,95 @@ * any later version. See COPYING for more details. */ +#include #include +#include +#include #include #include -#include #include +#include #include "ckpool.h" #include "libckpool.h" -#include "bitcoin.h" +#include "generator.h" -static char *socket_dir = "/tmp/ckpool/"; +static char *socket_dir = "/tmp/ckpool"; -static void create_pthread(pthread_t *thread, void *(*start_routine)(void *), void *arg) +static void *listener(void *arg) { - int ret = pthread_create(thread, NULL, start_routine, arg); + unixsock_t *us = (unixsock_t *)arg; + int sockd; - if (unlikely(ret)) - quit(1, "Failed to pthread_create"); +retry: + sockd = accept(us->sockd, NULL, NULL); + if (sockd < 0) { + if (interrupted()) + goto retry; + LOGERR("Failed to accept on socket in listener"); + goto out; + } + /* Insert parsing and repeat code here */ +out: + if (sockd >= 0) + close(sockd); + close_unix_socket(us->sockd, us->path); + return NULL; } -static void join_pthread(pthread_t thread) +/* Open the file in path, check if there is a pid in there that still exists + * and if not, write the pid into that file. */ +static bool write_pid(const char *path, pid_t pid) { - int ret = pthread_join(thread, NULL); + struct stat statbuf; + FILE *fp; + int ret; - if (unlikely(ret)) - quit(1, "Failed to pthread_join"); -} + if (!stat(path, &statbuf)) { + int oldpid; -static void *listener(void *arg) -{ - unixsock_t *us = (unixsock_t *)arg; + LOGWARNING("File %s exists", path); + fp = fopen(path, "r"); + if (!fp) { + LOGERR("Failed to open file %s", path); + return false; + } + ret = fscanf(fp, "%d", &oldpid); + fclose(fp); + if (ret == 1 && !(kill(oldpid, 0))) { + LOGWARNING("Process %s pid %d still exists", path, oldpid); + return false; + } + } + fp = fopen(path, "w"); + if (!fp) { + LOGERR("Failed to open file %s", path); + return false; + } + fprintf(fp, "%d", pid); + fclose(fp); - close_unix_socket(us->sockd, us->path); - return NULL; + return true; } int main(int argc, char **argv) { pthread_t pth_listener; unixsock_t uslistener; + pid_t mainpid; ckpool_t ckp; int c, ret; + char *s; memset(&ckp, 0, sizeof(ckp)); - while ((c = getopt(argc, argv, "c:n:")) != -1) { + while ((c = getopt(argc, argv, "c:gn:s:")) != -1) { switch (c) { case 'c': ckp.config = optarg; break; + case 'g': + /* Launch generator only */ + break; case 'n': ckp.name = optarg; break; @@ -69,6 +109,14 @@ int main(int argc, char **argv) if (ret && errno != EEXIST) quit(1, "Failed to make directory %s", socket_dir); + mainpid = getpid(); + + s = strdup(socket_dir); + realloc_strcat(&s, "main.pid"); + if (!write_pid(s, mainpid)) + quit(1, "Failed to write pid"); + dealloc(s); + uslistener.path = strdup(socket_dir); realloc_strcat(&uslistener.path, "listener"); LOGDEBUG("Opening %s", uslistener.path); diff --git a/src/ckpool.h b/src/ckpool.h index 5739c663..a96f7cd2 100644 --- a/src/ckpool.h +++ b/src/ckpool.h @@ -7,8 +7,6 @@ * any later version. See COPYING for more details. */ -/* This file should contain all exported functions of libckpool */ - #ifndef CKPOOL_H #define CKPOOL_H diff --git a/src/libckpool.c b/src/libckpool.c index ac14fc1b..ceadd0af 100644 --- a/src/libckpool.c +++ b/src/libckpool.c @@ -36,6 +36,23 @@ #define UNIX_PATH_MAX 108 #endif +void create_pthread(pthread_t *thread, void *(*start_routine)(void *), void *arg) +{ + int ret = pthread_create(thread, NULL, start_routine, arg); + + if (unlikely(ret)) + quit(1, "Failed to pthread_create"); +} + +void join_pthread(pthread_t thread) +{ + int ret = pthread_join(thread, NULL); + + if (unlikely(ret)) + quit(1, "Failed to pthread_join"); +} + + /* Place holders for when we add lock debugging */ #define GETLOCK(_lock, _file, _func, _line) #define GOTLOCK(_lock, _file, _func, _line) diff --git a/src/libckpool.h b/src/libckpool.h index e6a8f67c..1a37cd1c 100644 --- a/src/libckpool.h +++ b/src/libckpool.h @@ -122,6 +122,9 @@ struct unixsock { typedef struct unixsock unixsock_t; +void create_pthread(pthread_t *thread, void *(*start_routine)(void *), void *arg); +void join_pthread(pthread_t thread); + void _mutex_lock(pthread_mutex_t *lock, const char *file, const char *func, const int line); void _mutex_unlock_noyield(pthread_mutex_t *lock, const char *file, const char *func, const int line); void _mutex_unlock(pthread_mutex_t *lock, const char *file, const char *func, const int line);