Browse Source

Write the pid to a temporary file

master
Con Kolivas 11 years ago
parent
commit
8a23b0bb28
  1. 2
      configure.ac
  2. 2
      src/Makefile.am
  3. 84
      src/ckpool.c
  4. 2
      src/ckpool.h
  5. 17
      src/libckpool.c
  6. 3
      src/libckpool.h

2
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)

2
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

84
src/ckpool.c

@ -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);

2
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

17
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)

3
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);

Loading…
Cancel
Save