daemon: further tweaks to keepalive handling
[yaz-moved-to-github.git] / src / daemon.c
index f0ccd3e..c72aa30 100644 (file)
@@ -1,5 +1,5 @@
 /* This file is part of the YAZ toolkit.
- * Copyright (C) 1995-2008 Index Data
+ * Copyright (C) 1995-2012 Index Data
  * See the file LICENSE for details.
  */
 
 #include <sys/types.h>
 #endif
 
-#if HAVE_SYS_STAT_H
-#include <sys/stat.h>
-#endif
-
 #include <fcntl.h>
 
 #if HAVE_PWD_H
 #include <pwd.h>
 #endif
 
+#if HAVE_SYS_PRCTL_H
+#include <sys/prctl.h>
+#endif
+
 #include <yaz/daemon.h>
 #include <yaz/log.h>
 #include <yaz/snprintf.h>
@@ -53,7 +53,7 @@ static void write_pidfile(int pid_fd)
             yaz_log(YLOG_FATAL|YLOG_ERRNO, "ftruncate");
             exit(1);
         }
-        if (write(pid_fd, buf, strlen(buf)) != strlen(buf))
+        if (write(pid_fd, buf, strlen(buf)) != (int) strlen(buf))
         {
             yaz_log(YLOG_FATAL|YLOG_ERRNO, "write");
             exit(1);
@@ -62,11 +62,31 @@ static void write_pidfile(int pid_fd)
     }
 }
 
+int child_got_signal_from_us = 0;
 pid_t child_pid = 0;
-static void kill_child_handler(int num)
+static void normal_stop_handler(int num)
+{
+    if (child_pid)
+    {
+        /* tell child to terminate . ensure keepalive stops running -
+           let wait(2) wait once - so we exit AFTER the child */
+        child_got_signal_from_us = 1;
+        kill(child_pid, num);
+    }
+}
+
+static void graceful_stop_handler(int num)
 {
     if (child_pid)
+    {
+        /* tell our child to stop listening and wait some in time
+           in the hope that it has stopped listening by then. Then
+           we exit - quite possibly the child is still running - serving
+           remaining requests */
         kill(child_pid, num);
+        sleep(2);
+        _exit(0);
+    }
 }
 
 static void keepalive(void (*work)(void *data), void *data)
@@ -75,13 +95,15 @@ static void keepalive(void (*work)(void *data), void *data)
     int cont = 1;
     void (*old_sighup)(int);
     void (*old_sigterm)(int);
+    void (*old_sigusr1)(int);
     
     /* keep signals in their original state and make sure that some signals
        to parent process also gets sent to the child.. 
     */
-    old_sighup = signal(SIGHUP, kill_child_handler);
-    old_sigterm = signal(SIGTERM, kill_child_handler);
-    while (cont)
+    old_sighup = signal(SIGHUP, normal_stop_handler);
+    old_sigterm = signal(SIGTERM, normal_stop_handler);
+    old_sigusr1 = signal(SIGUSR1, graceful_stop_handler);
+    while (cont && !child_got_signal_from_us)
     {
         pid_t p = fork();
         pid_t p1;
@@ -97,6 +119,7 @@ static void keepalive(void (*work)(void *data), void *data)
                 /* child */
             signal(SIGHUP, old_sighup);  /* restore */
             signal(SIGTERM, old_sigterm);/* restore */
+            signal(SIGUSR1, old_sigusr1);/* restore */
             
             work(data);
             exit(0);
@@ -203,6 +226,13 @@ int yaz_daemon(const char *progname,
             yaz_log(YLOG_FATAL|YLOG_ERRNO, "setuid");
             exit(1);
         }
+        /* Linux don't produce core dumps evern if the limit is right and
+           files are writable.. This fixes this. See prctl(2) */
+#if HAVE_SYS_PRCTL_H
+#ifdef PR_SET_DUMPABLE
+        prctl(PR_SET_DUMPABLE, 1, 0, 0);
+#endif
+#endif
     }
 
     if (flags & YAZ_DAEMON_FORK)
@@ -274,7 +304,9 @@ int yaz_daemon(const char *progname,
 /*
  * Local variables:
  * c-basic-offset: 4
+ * c-file-style: "Stroustrup"
  * indent-tabs-mode: nil
  * End:
  * vim: shiftwidth=4 tabstop=8 expandtab
  */
+