smbd: detect EOF on stdin in --foreground mode
authorAndrew Tridgell <tridge@samba.org>
Wed, 30 Nov 2011 03:08:28 +0000 (14:08 +1100)
committerAndrew Tridgell <tridge@samba.org>
Thu, 9 Feb 2012 04:41:49 +0000 (15:41 +1100)
if EOF is detected on stdin then exit

source3/smbd/server.c

index 10d59adbc6c51c25783d8cd5eef4ad95b25b0002..3ca6f425cec39211c01bf3ac469560bea2bb45e5 100644 (file)
@@ -856,6 +856,23 @@ static bool open_sockets_smbd(struct smbd_parent_context *parent,
        return true;
 }
 
+
+/*
+  handle stdin becoming readable when we are in --foreground mode
+ */
+static void smbd_stdin_handler(struct tevent_context *ev,
+                              struct tevent_fd *fde,
+                              uint16_t flags,
+                              void *private_data)
+{
+       char c;
+       if (read(0, &c, 1) != 1) {
+               /* we have reached EOF on stdin, which means the
+                  parent has exited. Shutdown the server */
+               exit_server_cleanly("EOF on stdin");
+       }
+}
+
 static void smbd_parent_loop(struct tevent_context *ev_ctx,
                             struct smbd_parent_context *parent)
 {
@@ -1410,6 +1427,14 @@ extern void build_options(bool screen);
        /* make sure we always have a valid stackframe */
        frame = talloc_stackframe();
 
+       if (!Fork) {
+               /* 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(ev_ctx, parent, 0, TEVENT_FD_READ, smbd_stdin_handler, NULL);
+       }
+
        smbd_parent_loop(ev_ctx, parent);
 
        exit_server_cleanly(NULL);