X-Git-Url: http://git.madism.org/?a=blobdiff_plain;f=common%2Fcommon.h;h=e2f2b3ba3339f016259013ab99f19bfa86af9215;hb=0ad0ac6446aa4490800addafd8219f0f2a968c4d;hp=03d2fbb6cf20cba49da0fe8d77c78d129bc383c7;hpb=520b2f3bb198bfbb88b90b058ef610f0a9b980c6;p=apps%2Fpfixtools.git diff --git a/common/common.h b/common/common.h index 03d2fbb..e2f2b3b 100644 --- a/common/common.h +++ b/common/common.h @@ -57,7 +57,6 @@ #include "mem.h" - #define __tostr(x) #x #define STR(x) __tostr(x) @@ -70,9 +69,27 @@ typedef void (*exitcall_t)(void); #define module_init(fn) static __init initcall_t __init_##fn = fn; #define module_exit(fn) static __exit exitcall_t __exit_##fn = fn; +#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) { \ - syslog(Level, Fmt, ##__VA_ARGS__); \ + if (log_syslog) { \ + syslog(Level, Fmt, ##__VA_ARGS__); \ + } else { \ + fprintf(stderr, "[%s] " Fmt "\n", \ + __level_name(Level), ##__VA_ARGS__); \ + } \ } #define debug(Fmt, ...) __log(LOG_DEBUG, Fmt, ##__VA_ARGS__) @@ -87,12 +104,12 @@ 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; 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); @@ -108,16 +125,19 @@ int pidfile_refresh(void); int common_setup(const char* pidfile, bool unsafe, const char* runas_user, const char* runas_group, bool daemonize); +static inline void common_startup(void) +{ + signal(SIGPIPE, SIG_IGN); + signal(SIGSEGV, &common_sighandler); +} + + #define DECLARE_MAIN \ static int main_initialize(void) \ { \ + log_syslog = true; \ openlog(DAEMON_NAME, LOG_PID, LOG_MAIL); \ - signal(SIGPIPE, SIG_IGN); \ - signal(SIGINT, &common_sighandler); \ - signal(SIGTERM, &common_sighandler); \ - signal(SIGHUP, &common_sighandler); \ - signal(SIGSEGV, &common_sighandler); \ - info("starting..."); \ + common_startup(); \ return 0; \ } \ \