commit 874cd63: [Feature] Use own daemonization routine
Vsevolod Stakhov
vsevolod at highsecure.ru
Fri Nov 6 21:28:06 UTC 2020
Author: Vsevolod Stakhov
Date: 2020-11-06 21:23:45 +0000
URL: https://github.com/rspamd/rspamd/commit/874cd63ad2b66e3eb70db5c4fa35236f9d75e96e (HEAD -> master)
[Feature] Use own daemonization routine
---
src/rspamd.c | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++---
1 file 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 */
More information about the Commits
mailing list