X-Git-Url: http://git.madism.org/?a=blobdiff_plain;f=common.h;h=9b86f09324271972f8c35290899fb8abd65b3f28;hb=36003695aa2db17ee573d951bf19717796271d38;hp=801457cfe4c1bb292c3419abf5fc5689cc34b2ac;hpb=59b31c88179af4a9a9c9074a7b428841ce8661f8;p=apps%2Fpfixtools.git diff --git a/common.h b/common.h index 801457c..9b86f09 100644 --- a/common.h +++ b/common.h @@ -31,19 +31,20 @@ /* * Copyright © 2007 Pierre Habouzit + * Copyright © 2008 Florent Bruneau */ -#ifndef PFIXTOOLS_H -#define PFIXTOOLS_H +#ifndef PFIXTOOLS_COMMON_H +#define PFIXTOOLS_COMMON_H #include #include #include #include -#include #include #include #include +#include #include #include #include @@ -54,10 +55,15 @@ #include #include +#include "mem.h" + #define UNIXERR(fun) \ syslog(LOG_ERR, "%s:%d:%s: %s: %m", \ __FILE__, __LINE__, __func__, fun) +#define __tostr(x) #x +#define STR(x) __tostr(x) + typedef int (*initcall_t)(void); typedef void (*exitcall_t)(void); @@ -67,17 +73,45 @@ 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; -/* common.c */ extern sig_atomic_t sigint; extern sig_atomic_t sighup; void common_sighandler(int sig); -/* daemon.c */ +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); int accept_nonblock(int fd); +int xwrite(int fd, const char *s, size_t l); int daemon_detach(void); int drop_privileges(const char *user, const char *group); +int pidfile_open(const char *name); +int pidfile_refresh(void); + +int common_setup(const char* pidfile, bool unsafe, const char* runas_user, + const char* runas_group, bool daemonize); + +#define DECLARE_MAIN \ + static int main_initialize(void) \ + { \ + 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); \ + syslog(LOG_INFO, "Starting..."); \ + return 0; \ + } \ + \ + static void main_shutdown(void) \ + { \ + closelog(); \ + } \ + \ + module_init(main_initialize); \ + module_exit(main_shutdown); + #endif