s3-nmbd: Add stdin handler for nmbd
authorAndrew Bartlett <abartlet@samba.org>
Fri, 2 Mar 2012 07:21:09 +0000 (18:21 +1100)
committerAndrew Bartlett <abartlet@samba.org>
Sun, 4 Mar 2012 09:14:34 +0000 (10:14 +0100)
This will help avoid runaway processes in the test env, particularly
when the whole selftest.pl is killed.

Andrew Bartlett

source3/nmbd/nmbd.c

index 35a9a31f8ac621656a06d2e37699dca569343712..7bd4facbb18f3dfead8c794e10f4d07c2d5a5d7f 100644 (file)
@@ -88,6 +88,24 @@ static void nmbd_sig_term_handler(struct tevent_context *ev,
        terminate(msg);
 }
 
+/*
+  handle stdin becoming readable when we are in --foreground mode
+ */
+static void nmbd_stdin_handler(struct tevent_context *ev,
+                              struct tevent_fd *fde,
+                              uint16_t flags,
+                              void *private_data)
+{
+       char c;
+       if (read(0, &c, 1) != 1) {
+               struct messaging_context *msg = talloc_get_type_abort(
+                       private_data, struct messaging_context);
+               
+               DEBUG(0,("EOF on stdin\n"));
+               terminate(msg);
+       }
+}
+
 static bool nmbd_setup_sig_term_handler(struct messaging_context *msg)
 {
        struct tevent_signal *se;
@@ -105,6 +123,19 @@ static bool nmbd_setup_sig_term_handler(struct messaging_context *msg)
        return true;
 }
 
+static bool nmbd_setup_stdin_handler(struct messaging_context *msg, bool foreground)
+{
+       if (foreground) {
+               /* if we are running in the foreground then look for
+                  EOF on stdin, and exit if it happens. This allows
+                  us to die if the parent process dies
+               */
+               tevent_add_fd(nmbd_event_context(), nmbd_event_context(), 0, TEVENT_FD_READ, nmbd_stdin_handler, msg);
+       }
+
+       return true;
+}
+
 static void msg_reload_nmbd_services(struct messaging_context *msg,
                                     void *private_data,
                                     uint32_t msg_type,
@@ -918,6 +949,8 @@ static bool open_sockets(bool isdaemon, int port)
 
        if (!nmbd_setup_sig_term_handler(msg))
                exit(1);
+       if (!nmbd_setup_stdin_handler(msg, !Fork))
+               exit(1);
        if (!nmbd_setup_sig_hup_handler(msg))
                exit(1);