From 881732b600190bc02b9dde47980b14382851c266 Mon Sep 17 00:00:00 2001 From: Florent Bruneau Date: Mon, 8 Sep 2008 14:44:10 +0200 Subject: [PATCH] Factorize code. Signed-off-by: Florent Bruneau --- common.c | 24 ++++++++++++++++++++++++ common.h | 28 ++++++++++++++++++++++++++++ main-postlicyd.c | 45 ++++++--------------------------------------- main-srsd.c | 43 ++++--------------------------------------- server.c | 4 ++-- 5 files changed, 64 insertions(+), 80 deletions(-) diff --git a/common.c b/common.c index 59ac027..3688c9d 100644 --- a/common.c +++ b/common.c @@ -31,6 +31,7 @@ /* * Copyright © 2007 Pierre Habouzit + * Copyright © 2008 Florent Bruneau */ #include @@ -259,11 +260,34 @@ static void pidfile_close(void) } } +int common_setup(const char* pidfilename, bool unsafe, const char* runas_user, + const char* runas_group, bool daemonize) +{ + if (pidfile_open(pidfilename) < 0) { + syslog(LOG_CRIT, "unable to write pidfile %s", pidfilename); + return EXIT_FAILURE; + } + + if (!unsafe && drop_privileges(runas_user, runas_group) < 0) { + syslog(LOG_CRIT, "unable to drop privileges"); + return EXIT_FAILURE; + } + + if (daemonize && daemon_detach() < 0) { + syslog(LOG_CRIT, "unable to fork"); + return EXIT_FAILURE; + } + + pidfile_refresh(); + return EXIT_SUCCESS; +} + extern initcall_t __madinit[]; extern exitcall_t __madexit[]; static void common_shutdown(void) { + syslog(LOG_INFO, "Stopping..."); pidfile_close(); for (int i = -1; __madexit[i]; i--) { diff --git a/common.h b/common.h index fb70b75..9b86f09 100644 --- a/common.h +++ b/common.h @@ -31,6 +31,7 @@ /* * Copyright © 2007 Pierre Habouzit + * Copyright © 2008 Florent Bruneau */ #ifndef PFIXTOOLS_COMMON_H @@ -60,6 +61,9 @@ 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); @@ -86,4 +90,28 @@ 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 diff --git a/main-postlicyd.c b/main-postlicyd.c index bc04e85..febc1e2 100644 --- a/main-postlicyd.c +++ b/main-postlicyd.c @@ -47,6 +47,8 @@ #define RUNAS_USER "nobody" #define RUNAS_GROUP "nogroup" +DECLARE_MAIN + enum smtp_state { SMTP_UNKNOWN, SMTP_CONNECT, @@ -257,26 +259,6 @@ int start_listener(int port) /* administrivia {{{ */ -static int main_initialize(void) -{ - openlog("postlicyd", 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); - void usage(void) { fputs("usage: "DAEMON_NAME" [options] config\n" @@ -318,29 +300,14 @@ int main(int argc, char *argv[]) return EXIT_FAILURE; } - if (pidfile_open(pidfile) < 0) { - syslog(LOG_CRIT, "unable to write pidfile %s", pidfile); + if (common_setup(pidfile, false, RUNAS_USER, RUNAS_GROUP, daemonize) + != EXIT_SUCCESS) { return EXIT_FAILURE; } - if (drop_privileges(RUNAS_USER, RUNAS_GROUP) < 0) { - syslog(LOG_CRIT, "unable to drop privileges"); - return EXIT_FAILURE; - } - - if (daemonize && daemon_detach() < 0) { - syslog(LOG_CRIT, "unable to fork"); - return EXIT_FAILURE; - } - - pidfile_refresh(); - if (start_listener(port) < 0) return EXIT_FAILURE; - (void)server_loop(query_starter, (delete_client_t)query_delete, - policy_run, NULL); - - syslog(LOG_INFO, "Stopping..."); - return EXIT_SUCCESS; + return server_loop(query_starter, (delete_client_t)query_delete, + policy_run, NULL); } diff --git a/main-srsd.c b/main-srsd.c index 3634647..89e21de 100644 --- a/main-srsd.c +++ b/main-srsd.c @@ -49,8 +49,7 @@ #define RUNAS_USER "nobody" #define RUNAS_GROUP "nogroup" -#define __tostr(x) #x -#define STR(x) __tostr(x) +DECLARE_MAIN typedef struct srs_config_t { srs_t* srs; @@ -175,25 +174,6 @@ int start_listener(int port, bool decoder) /* }}} */ /* administrivia {{{ */ -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); void usage(void) { @@ -261,7 +241,6 @@ int main(int argc, char *argv[]) int port_dec = DEFAULT_DECODER_PORT; const char *pidfile = NULL; - int res; srs_t *srs; for (int c = 0; (c = getopt(argc, argv, "hfu" "e:d:p:")) >= 0; ) { @@ -297,22 +276,10 @@ int main(int argc, char *argv[]) return EXIT_FAILURE; } - if (pidfile_open(pidfile) < 0) { - syslog(LOG_CRIT, "unable to write pidfile %s", pidfile); + if (common_setup(pidfile, unsafe, RUNAS_USER, RUNAS_GROUP, daemonize) + != EXIT_SUCCESS) { return EXIT_FAILURE; } - - if (!unsafe && drop_privileges(RUNAS_USER, RUNAS_GROUP) < 0) { - syslog(LOG_CRIT, "unable to drop privileges"); - return EXIT_FAILURE; - } - - if (daemonize && daemon_detach() < 0) { - syslog(LOG_CRIT, "unable to fork"); - return EXIT_FAILURE; - } - - pidfile_refresh(); { srs_config_t config = { .srs = srs, @@ -324,8 +291,6 @@ int main(int argc, char *argv[]) if (start_listener(port_dec, true) < 0) return EXIT_FAILURE; - res = server_loop(srsd_stater, NULL, process_srs, &config); + return server_loop(srsd_stater, NULL, process_srs, &config); } - syslog(LOG_INFO, "Stopping..."); - return res; } diff --git a/server.c b/server.c index 203808c..0d6443c 100644 --- a/server.c +++ b/server.c @@ -130,7 +130,7 @@ int server_loop(start_client_t starter, delete_client_t deleter, if (n < 0) { if (errno != EAGAIN && errno != EINTR) { UNIXERR("epoll_wait"); - return -1; + return EXIT_FAILURE; } continue; } @@ -161,5 +161,5 @@ int server_loop(start_client_t starter, delete_client_t deleter, } } } - return 0; + return EXIT_SUCCESS; } -- 2.20.1