#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)
{
/* 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 */