X-Git-Url: http://git.madism.org/?a=blobdiff_plain;f=common%2Fcommon.c;h=9fdf961a45c1a1bbb5ff13509318ff1094267726;hb=f78d6baee60179e6ce41cc7058a8df5857116010;hp=c2abeb41feea8271d83b747f1bacb4f4ea62fc43;hpb=44d04c83a53af19faecb1620cf0f9ed53054da5c;p=apps%2Fpfixtools.git diff --git a/common/common.c b/common/common.c index c2abeb4..9fdf961 100644 --- a/common/common.c +++ b/common/common.c @@ -41,31 +41,22 @@ #include "common.h" -sig_atomic_t sigint = false; -sig_atomic_t sighup = false; - bool daemon_process = true; +int log_level = LOG_INFO; +bool log_syslog = false; static FILE *pidfile = NULL; void common_sighandler(int sig) { switch (sig) { - case SIGINT: - sigint = true; - return; - - case SIGHUP: - sighup = true; - return; - default: - syslog(LOG_ERR, "Killed (got signal %d)...", sig); + err("Killed (got signal %d)...", sig); exit(-1); } } -static int setnonblock(int sock) +int setnonblock(int sock) { int res = fcntl(sock, F_GETFL); @@ -197,11 +188,11 @@ int daemon_detach(void) pid = fork(); if (pid < 0) { return -1; - } + } if (pid) { - daemon_process = false; + daemon_process = false; exit(0); - } + } setsid(); return 0; @@ -234,20 +225,11 @@ int drop_privileges(const char *user, const char *group) int pidfile_open(const char *name) { - struct flock lock; - p_clear(&lock, 1); - lock.l_type = F_WRLCK; if (name) { pidfile = fopen(name, "w"); if (!pidfile) return -1; - if (fcntl(fileno(pidfile), F_SETLK, &lock) == -1) { - syslog(LOG_ERR, "program already started"); - fclose(pidfile); - pidfile = NULL; - return -1; - } - fprintf(pidfile, "%d\n", getpid()); + fprintf(pidfile, "%d\n", getpid()); return fflush(pidfile); } return 0; @@ -266,14 +248,12 @@ int pidfile_refresh(void) static void pidfile_close(void) { - struct flock lock; - p_clear(&lock, 1); - lock.l_type = F_UNLCK; if (pidfile) { - rewind(pidfile); - ftruncate(fileno(pidfile), 0); - fcntl(fileno(pidfile), F_SETLK, &lock); - fclose(pidfile); + if (daemon_process) { + rewind(pidfile); + ftruncate(fileno(pidfile), 0); + } + fclose(pidfile); pidfile = NULL; } } @@ -281,18 +261,18 @@ static void pidfile_close(void) int common_setup(const char* pidfilename, bool unsafe, const char* runas_user, const char* runas_group, bool daemonize) { - if (!unsafe && drop_privileges(runas_user, runas_group) < 0) { - syslog(LOG_CRIT, "unable to drop privileges"); + if (pidfile_open(pidfilename) < 0) { + crit("unable to write pidfile %s", pidfilename); return EXIT_FAILURE; } - if (daemonize && daemon_detach() < 0) { - syslog(LOG_CRIT, "unable to fork"); + if (!unsafe && drop_privileges(runas_user, runas_group) < 0) { + crit("unable to drop privileges"); return EXIT_FAILURE; } - if (pidfile_open(pidfilename) < 0) { - syslog(LOG_CRIT, "unable to write pidfile %s", pidfilename); + if (daemonize && daemon_detach() < 0) { + crit("unable to fork"); return EXIT_FAILURE; } @@ -300,32 +280,39 @@ int common_setup(const char* pidfilename, bool unsafe, const char* runas_user, return EXIT_SUCCESS; } -extern initcall_t __madinit[]; -extern exitcall_t __madexit[]; +#include "array.h" + +ARRAY(exitcall_t) + +static A(exitcall_t) __exit = ARRAY_INIT; + +void common_register_exit(exitcall_t _exit) +{ + array_add(__exit, _exit); +} static void common_shutdown(void) { - if (daemon_process) { - syslog(LOG_INFO, "Stopping..."); - } - pidfile_close(); - for (int i = -1; __madexit[i]; i--) { - (*__madexit[i])(); + if (daemon_process && log_syslog) { + info("stopping..."); } + pidfile_close(); + for (int i = array_len(__exit) - 1 ; i >= 0 ; --i) { + array_elt(__exit, i)(); + } + array_wipe(__exit); } -static void __attribute__((__constructor__,__used__)) -common_initialize(void) +void common_init(void) { + static bool __ran = false; + if (__ran) { + return; + } if (atexit(common_shutdown)) { fputs("Cannot hook my atexit function, quitting !\n", stderr); abort(); } - - for (int i = 0; __madinit[i]; i++) { - if ((*__madinit[i])()) { - exit(EXIT_FAILURE); - } - } + __ran = true; }