X-Git-Url: http://git.madism.org/?a=blobdiff_plain;f=common%2Fcommon.h;h=af393e556c7b2454037ce414bf95246dbefcff04;hb=8429bd1157d8a6ccb8e15b8c02b84d1ad548902b;hp=30ce1d8ca9a4a78665eaa9111861efcfa2399960;hpb=f840e396bc900fe05ad159e1ab06e5df63679ea3;p=apps%2Fpfixtools.git diff --git a/common/common.h b/common/common.h index 30ce1d8..af393e5 100644 --- a/common/common.h +++ b/common/common.h @@ -57,27 +57,51 @@ #include "mem.h" - #define __tostr(x) #x #define STR(x) __tostr(x) typedef int (*initcall_t)(void); typedef void (*exitcall_t)(void); -#define __init __attribute__((__used__,__section__(".mad.init"))) -#define __exit __attribute__((__used__,__section__(".mad.exit"))) +void common_register_exit(exitcall_t exitcall); +void common_init(void); -#define module_init(fn) static __init initcall_t __init_##fn = fn; -#define module_exit(fn) static __exit exitcall_t __exit_##fn = fn; +#define module_init(fn) \ + __attribute__((constructor,used)) \ + static void __init_wrapper__ ## fn (void) { \ + common_init(); \ + if (fn() != 0) { \ + exit(-1); \ + } \ + } +#define module_exit(fn) \ + __attribute__((constructor,used)) \ + static void __exit_wrapper ## fn(void) { \ + common_init(); \ + common_register_exit(fn); \ + } -#define __log(Level, Fmt, ...) \ - if (log_level >= Level) { \ - if (log_syslog) { \ - syslog(Level, Fmt, ##__VA_ARGS__); \ - } else { \ - fprintf(stderr, "[%d] " Fmt "\n", \ - Level, ##__VA_ARGS__); \ - } \ +#define likely(expr) __builtin_expect((expr) != 0, 1) +#define unlikely(expr) __builtin_expect((expr) != 0, 0) + +#define __level_name(L) \ + ( (L) == LOG_DEBUG ? "debug " \ + : (L) == LOG_NOTICE ? "notice" \ + : (L) == LOG_INFO ? "info " \ + : (L) == LOG_WARNING ? "warn " \ + : (L) == LOG_ERR ? "error " \ + : (L) == LOG_CRIT ? "crit " \ + : (L) == LOG_ALERT ? "alert " \ + : "??? " ) + +#define __log(Level, Fmt, ...) \ + if (log_level >= Level) { \ + if (log_syslog) { \ + syslog(Level, "%s" Fmt, log_state, ##__VA_ARGS__); \ + } else { \ + fprintf(stderr, "[%s] %s" Fmt "\n", \ + __level_name(Level), log_state, ##__VA_ARGS__);\ + } \ } #define debug(Fmt, ...) __log(LOG_DEBUG, Fmt, ##__VA_ARGS__) @@ -92,13 +116,13 @@ typedef void (*exitcall_t)(void); #define UNIXERR(fun) err("%s:%d:%s %s: %m", \ __FILE__, __LINE__, __func__, fun) -extern sig_atomic_t sigint; -extern sig_atomic_t sighup; extern int log_level; extern bool log_syslog; +extern const char *log_state; void common_sighandler(int sig); +int setnonblock(int sock); int tcp_bind(const struct sockaddr *addr, socklen_t len); int tcp_listen(const struct sockaddr *addr, socklen_t len); int tcp_listen_nonblock(const struct sockaddr *addr, socklen_t len); @@ -117,11 +141,7 @@ int common_setup(const char* pidfile, bool unsafe, const char* runas_user, static inline void common_startup(void) { signal(SIGPIPE, SIG_IGN); - signal(SIGINT, &common_sighandler); - signal(SIGTERM, &common_sighandler); - signal(SIGHUP, &common_sighandler); signal(SIGSEGV, &common_sighandler); - info("starting"); }