X-Git-Url: http://git.madism.org/?a=blobdiff_plain;f=common%2Fcommon.c;h=63f0d9ab4663e6102b0fcc5564fa8d2baa305bfa;hb=1f20b76400d58d143d433236a26b43e2c25da1f5;hp=aa9e5ea2c4568cc243d34a44c968d220b0f524f3;hpb=d25cf39d7cb4d2df2d7e2fa307c1c8f14bb018cc;p=apps%2Fpfixtools.git diff --git a/common/common.c b/common/common.c index aa9e5ea..63f0d9a 100644 --- a/common/common.c +++ b/common/common.c @@ -41,9 +41,6 @@ #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; @@ -53,22 +50,13 @@ static FILE *pidfile = NULL; void common_sighandler(int sig) { switch (sig) { - case SIGTERM: - case SIGINT: - sigint = true; - return; - - case SIGHUP: - sighup = true; - return; - default: err("Killed (got signal %d)...", sig); exit(-1); } } -static int setnonblock(int sock) +int setnonblock(int sock) { int res = fcntl(sock, F_GETFL); @@ -292,8 +280,16 @@ 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 exitcall) +{ + array_add(__exit, exitcall); +} static void common_shutdown(void) { @@ -301,23 +297,22 @@ static void common_shutdown(void) info("stopping..."); } pidfile_close(); - for (int i = -1; __madexit[i]; i--) { - (*__madexit[i])(); + 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; }