From: Pierre Habouzit Date: Thu, 29 Nov 2007 10:13:13 +0000 (+0100) Subject: common module updates. X-Git-Url: http://git.madism.org/?a=commitdiff_plain;h=dbc6818c72dfe618b164db970773490b19b808c5;p=apps%2Fpfixtools.git common module updates. --- diff --git a/common.c b/common.c index 0d7671d..80746ae 100644 --- a/common.c +++ b/common.c @@ -204,22 +204,25 @@ int drop_privileges(const char *user, const char *group) return 0; } +extern initcall_t __madinit[], __madexit[]; + void common_initialize(void) { - extern initcall_t __madinit_start, __madinit_end; + if (atexit(common_shutdown)) { + fputs("Cannot hook my atexit function, quitting !\n", stderr); + abort(); + } - initcall_t *call_p = &__madinit_start; - while (call_p < &__madinit_end) { - (*call_p++)(); + for (int i = 0; __madinit[i]; i++) { + if ((*__madinit[i])()) { + exit(EXIT_FAILURE); + } } } void common_shutdown(void) { - extern exitcall_t __madexit_start, __madexit_end; - - exitcall_t *call_p = &__madexit_end; - while (call_p > &__madexit_start) { - (*--call_p)(); + for (int i = -1; __madexit[i]; i--) { + (*__madexit[i])(); } } diff --git a/common.h b/common.h index 6e76557..449ead5 100644 --- a/common.h +++ b/common.h @@ -37,17 +37,21 @@ #define PFIXTOOLS_H #include +#include #include +#include #include #include #include #include -#include #include +#include #include #include #include +#include #include +#include #include #define UNIXERR(fun) \ @@ -60,8 +64,8 @@ typedef void (*exitcall_t)(void); #define __init __attribute__((__used__,__section__(".mad.init"))) #define __exit __attribute__((__used__,__section__(".mad.exit"))) -#define module_init(fn) static initcall_t __init_##fn __init = fn; -#define module_exit(fn) static exitcall_t __exit_##fn __exit = fn; +#define module_init(fn) static __init initcall_t __init_##fn = fn; +#define module_exit(fn) static __exit exitcall_t __exit_##fn = fn; /* common.c */ extern sig_atomic_t sigint; diff --git a/common.ld b/common.ld index 3ef8a1b..6f7485f 100644 --- a/common.ld +++ b/common.ld @@ -1,11 +1,10 @@ SECTIONS { - .data : { - __madinit_start = . ; + .rodata : { + . = ALIGN(8); + __madinit = . ; *( .mad.init ) - __madinit_end = . ; - - __madexit_start = . ; + QUAD(0) *( .mad.exit ) - __madexit_end = . ; + __madexit = . ; } }