|
|
|
@ -7,55 +7,95 @@
|
|
|
|
|
* any later version. See COPYING for more details. |
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
#include <sys/socket.h> |
|
|
|
|
#include <sys/stat.h> |
|
|
|
|
#include <sys/types.h> |
|
|
|
|
#include <signal.h> |
|
|
|
|
#include <stdio.h> |
|
|
|
|
#include <stdlib.h> |
|
|
|
|
#include <unistd.h> |
|
|
|
|
#include <string.h> |
|
|
|
|
#include <unistd.h> |
|
|
|
|
|
|
|
|
|
#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); |
|
|
|
|