diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2020-11-06 21:23:45 +0000 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2020-11-06 21:23:45 +0000 |
commit | 874cd63ad2b66e3eb70db5c4fa35236f9d75e96e (patch) | |
tree | 3496f6d8cc95f4841f3213f031a5d1d043613e38 /src/rspamd.c | |
parent | e1aee3fcb56ea4dbca57903ae44254ea969cd81f (diff) | |
download | rspamd-874cd63ad2b66e3eb70db5c4fa35236f9d75e96e.tar.gz rspamd-874cd63ad2b66e3eb70db5c4fa35236f9d75e96e.zip |
[Feature] Use own daemonization routine
Diffstat (limited to 'src/rspamd.c')
-rw-r--r-- | src/rspamd.c | 64 |
1 files changed, 61 insertions, 3 deletions
diff --git a/src/rspamd.c b/src/rspamd.c index 5e87103eb..5a1898c29 100644 --- a/src/rspamd.c +++ b/src/rspamd.c @@ -1260,6 +1260,65 @@ version (void) #endif } +static gboolean +rspamd_main_daemon (struct rspamd_main *rspamd_main) +{ + int fd; + pid_t old_pid = getpid (); + + switch (fork ()) { + case -1: + msg_err_main ("fork() failed: %s", strerror (errno)); + return FALSE; + + case 0: + break; + + default: + /* Old process */ + exit (0); + } + + rspamd_log_on_fork (g_quark_from_static_string ("main"), + rspamd_main->cfg, + rspamd_main->logger); + + if (setsid () == -1) { + msg_err_main ("setsid () failed: %s", strerror (errno)); + return FALSE; + } + + umask (0); + + fd = open ("/dev/null", O_RDWR); + if (fd == -1) { + msg_err_main ("open(\"/dev/null\") failed: %s", strerror (errno)); + return FALSE; + } + + if (dup2 (fd, STDIN_FILENO) == -1) { + msg_err_main ("dup2(STDIN) failed: %s", strerror (errno)); + return FALSE; + } + + if (dup2 (fd, STDOUT_FILENO) == -1) { + msg_err_main ("dup2(STDOUT) failed: %s", strerror (errno)); + return FALSE; + } + + if (fd > STDERR_FILENO) { + if (close(fd) == -1) { + msg_err_main ("close() failed: %s", strerror (errno)); + return FALSE; + } + } + + msg_info_main ("daemonized successfully; old pid %P, new pid %P", + old_pid, getpid ()); + + return TRUE; +} + gint main (gint argc, gchar **argv, gchar **env) { @@ -1395,9 +1454,8 @@ main (gint argc, gchar **argv, gchar **env) /* Daemonize */ if (!no_fork) { - if (daemon (0, 0) == -1) { - msg_err_main ("cannot daemonize: %s", strerror (errno)); - exit (-errno); + if (!rspamd_main_daemon (rspamd_main)) { + exit (EXIT_FAILURE); } /* Close emergency logger */ |