s4: process_standard: Add a simplified SIGTERM handler based on code from source4...
authorJeremy Allison <jra@samba.org>
Fri, 7 Apr 2017 22:45:41 +0000 (15:45 -0700)
committerRalph Boehme <slow@samba.org>
Sat, 8 Apr 2017 14:21:57 +0000 (16:21 +0200)
Allows us to be independent of parent SIGTERM signal handling.

Signed-off-by: Jeremy Allison <jra@samba.org>
Reviewed-by: Ralph Boehme <slow@samba.org>
Autobuild-User(master): Ralph Böhme <slow@samba.org>
Autobuild-Date(master): Sat Apr  8 16:21:57 CEST 2017 on sn-devel-144

source4/smbd/process_standard.c

index 776e497e987297f4924c60917de29a040b5fec46..92f07add42c40666805828ce9d99ba35c3e1cca5 100644 (file)
@@ -67,6 +67,26 @@ static void sighup_signal_handler(struct tevent_context *ev,
        debug_schedule_reopen_logs();
 }
 
+static void sigterm_signal_handler(struct tevent_context *ev,
+                               struct tevent_signal *se,
+                               int signum, int count, void *siginfo,
+                               void *private_data)
+{
+#if HAVE_GETPGRP
+       if (getpgrp() == getpid()) {
+               /*
+                * We're the process group leader, send
+                * SIGTERM to our process group.
+                */
+               DEBUG(0,("SIGTERM: killing children\n"));
+               kill(-getpgrp(), SIGTERM);
+       }
+#endif
+       DEBUG(0,("Exiting pid %u on SIGTERM\n", (unsigned int)getpid()));
+       talloc_free(ev);
+       exit(127);
+}
+
 /*
   handle EOF on the parent-to-all-children pipe in the child
 */
@@ -310,6 +330,16 @@ static void standard_accept_connection(struct tevent_context *ev,
                smb_panic("Failed to add SIGHUP handler after fork");
        }
 
+       se = tevent_add_signal(ev,
+                               ev,
+                               SIGTERM,
+                               0,
+                               sigterm_signal_handler,
+                               NULL);
+       if (se == NULL) {
+               smb_panic("Failed to add SIGTERM handler after fork");
+       }
+
        /* setup the process title */
        c = socket_get_peer_addr(sock2, ev);
        s = socket_get_my_addr(sock2, ev);
@@ -408,6 +438,16 @@ static void standard_new_task(struct tevent_context *ev,
                smb_panic("Failed to add SIGHUP handler after fork");
        }
 
+       se = tevent_add_signal(ev,
+                               ev,
+                               SIGTERM,
+                               0,
+                               sigterm_signal_handler,
+                               NULL);
+       if (se == NULL) {
+               smb_panic("Failed to add SIGTERM handler after fork");
+       }
+
        setproctitle("task %s server_id[%d]", service_name, (int)pid);
 
        /* setup this new task.  Cluster ID is PID based for this process model */