Fix bug #9166 - Starting smbd or nmbd with stdin from /dev/null results in "EOF on...
authorJeremy Allison <jra@samba.org>
Mon, 10 Jun 2013 20:33:40 +0000 (13:33 -0700)
committerAndrew Bartlett <abartlet@samba.org>
Thu, 20 Jun 2013 11:41:01 +0000 (13:41 +0200)
Only install the stdin handler if it's a pipe or fifo.

Signed-off-by: Jeremy Allison <jra@samba.org>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
source3/nmbd/nmbd.c
source3/smbd/server.c
source3/winbindd/winbindd.c
source4/smbd/server.c

index 12afb00993970bb1dfa8c9480c2f2583f4a4668c..42e2b2f6ff918ebd89b00f8a44007aa325809ba6 100644 (file)
@@ -130,8 +130,20 @@ static bool nmbd_setup_stdin_handler(struct messaging_context *msg, bool foregro
                /* 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
+                  Only do this on a pipe or socket, no other device.
                */
-               tevent_add_fd(nmbd_event_context(), nmbd_event_context(), 0, TEVENT_FD_READ, nmbd_stdin_handler, msg);
+               struct stat st;
+               if (fstat(0, &st) != 0) {
+                       return false;
+               }
+               if (S_ISFIFO(st.st_mode) || S_ISSOCK(st.st_mode)) {
+                       tevent_add_fd(nmbd_event_context(),
+                               nmbd_event_context(),
+                               0,
+                               TEVENT_FD_READ,
+                               nmbd_stdin_handler,
+                               msg);
+               }
        }
 
        return true;
index f07bd28fb46193e4865a6f71ba1f3d51560078fc..d3cd33ec90bd1e3421a4b43351ba50b388c823a5 100644 (file)
@@ -1558,8 +1558,20 @@ extern void build_options(bool screen);
                /* 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
+                  Only do this on a pipe or socket, no other device.
                */
-               tevent_add_fd(ev_ctx, parent, 0, TEVENT_FD_READ, smbd_stdin_handler, NULL);
+               struct stat st;
+               if (fstat(0, &st) != 0) {
+                       return false;
+               }
+               if (S_ISFIFO(st.st_mode) || S_ISSOCK(st.st_mode)) {
+                       tevent_add_fd(ev_ctx,
+                                       parent,
+                                       0,
+                                       TEVENT_FD_READ,
+                                       smbd_stdin_handler,
+                                       NULL);
+               }
        }
 
        smbd_parent_loop(ev_ctx, parent);
index 7a0700dffa6569ae605136f3ebe34da2783ac1ef..141ca5c7c56d662b85819d61753b66695b47f5c2 100644 (file)
@@ -308,6 +308,8 @@ bool winbindd_setup_stdin_handler(bool parent, bool foreground)
        bool *is_parent;
 
        if (foreground) {
+               struct stat st;
+
                is_parent = talloc(winbind_event_context(), bool);
                if (!is_parent) {
                        return false;
@@ -318,8 +320,19 @@ bool winbindd_setup_stdin_handler(bool parent, bool 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
+                  Only do this on a pipe or socket, no other device.
                */
-               tevent_add_fd(winbind_event_context(), is_parent, 0, TEVENT_FD_READ, winbindd_stdin_handler, is_parent);
+               if (fstat(0, &st) != 0) {
+                       return false;
+               }
+               if (S_ISFIFO(st.st_mode) || S_ISSOCK(st.st_mode)) {
+                       tevent_add_fd(winbind_event_context(),
+                                       is_parent,
+                                       0,
+                                       TEVENT_FD_READ,
+                                       winbindd_stdin_handler,
+                                       is_parent);
+               }
        }
 
        return true;
index 5fb252e93d2df97e81ad734b0fedece78fe30f32..0ad3e6ba4157a649f679ce8a2f27eaecb520380b 100644 (file)
@@ -301,6 +301,7 @@ static int binary_smbd_main(const char *binary_name, int argc, const char *argv[
        NTSTATUS status;
        const char *model = "standard";
        int max_runtime = 0;
+       struct stat st;
        enum {
                OPT_DAEMON = 1000,
                OPT_INTERACTIVE,
@@ -439,9 +440,19 @@ static int binary_smbd_main(const char *binary_name, int argc, const char *argv[
 #ifdef SIGTTIN
        signal(SIGTTIN, SIG_IGN);
 #endif
-       tevent_add_fd(event_ctx, event_ctx, 0, stdin_event_flags,
-                     server_stdin_handler,
-                     discard_const(binary_name));
+
+       if (fstat(0, &st) != 0) {
+               exit(1);
+       }
+
+       if (S_ISFIFO(st.st_mode) || S_ISSOCK(st.st_mode)) {
+               tevent_add_fd(event_ctx,
+                               event_ctx,
+                               0,
+                               stdin_event_flags,
+                               server_stdin_handler,
+                               discard_const(binary_name));
+       }
 
        if (max_runtime) {
                DEBUG(0,("Called with maxruntime %d - current ts %llu\n",