/*
* Copyright © 2007 Pierre Habouzit
+ * Copyright © 2008 Florent Bruneau
*/
#include <fcntl.h>
}
}
+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--) {
/*
* Copyright © 2007 Pierre Habouzit
+ * Copyright © 2008 Florent Bruneau
*/
#ifndef PFIXTOOLS_COMMON_H
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);
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
#define RUNAS_USER "nobody"
#define RUNAS_GROUP "nogroup"
+DECLARE_MAIN
+
enum smtp_state {
SMTP_UNKNOWN,
SMTP_CONNECT,
/* 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"
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);
}
#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;
/* }}} */
/* 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)
{
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; ) {
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,
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;
}
if (n < 0) {
if (errno != EAGAIN && errno != EINTR) {
UNIXERR("epoll_wait");
- return -1;
+ return EXIT_FAILURE;
}
continue;
}
}
}
}
- return 0;
+ return EXIT_SUCCESS;
}