X-Git-Url: http://git.madism.org/?a=blobdiff_plain;f=common%2Fcommon.c;h=1b8cd980b681040703db3717f756e676870d1b5b;hb=d622febc1fb702194f7ac3f95bfa6cba448dcc5b;hp=dc0e194694ff44490570c8dd6983b99351d31d73;hpb=0ad0ac6446aa4490800addafd8219f0f2a968c4d;p=apps%2Fpfixtools.git diff --git a/common/common.c b/common/common.c index dc0e194..1b8cd98 100644 --- a/common/common.c +++ b/common/common.c @@ -41,9 +41,10 @@ #include "common.h" -bool daemon_process = true; -int log_level = LOG_INFO; -bool log_syslog = false; +bool daemon_process = true; +int log_level = LOG_INFO; +bool log_syslog = false; +const char *log_state = ""; static FILE *pidfile = NULL; @@ -280,32 +281,41 @@ 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) { + log_state = "stopping "; if (daemon_process && log_syslog) { - info("stopping..."); + info(""); } 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; + } + log_state = "starting "; 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; }