s3-winbindd: Add stdin handler for winbind
authorAndrew Bartlett <abartlet@samba.org>
Fri, 2 Mar 2012 07:22:10 +0000 (18:22 +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/winbindd/winbindd.c
source3/winbindd/winbindd_proto.h

index 5e23859..3806eb8 100644 (file)
@@ -203,6 +203,26 @@ static void winbindd_sig_term_handler(struct tevent_context *ev,
        terminate(*is_parent);
 }
 
+/*
+  handle stdin becoming readable when we are in --foreground mode
+ */
+static void winbindd_stdin_handler(struct tevent_context *ev,
+                              struct tevent_fd *fde,
+                              uint16_t flags,
+                              void *private_data)
+{
+       char c;
+       if (read(0, &c, 1) != 1) {
+               bool *is_parent = talloc_get_type_abort(private_data, bool);
+               
+               /* we have reached EOF on stdin, which means the
+                  parent has exited. Shutdown the server */
+               DEBUG(0,("EOF on stdin (is_parent=%d)\n",
+                        (int)*is_parent));
+               terminate(*is_parent);
+       }
+}
+
 bool winbindd_setup_sig_term_handler(bool parent)
 {
        struct tevent_signal *se;
@@ -251,6 +271,28 @@ bool winbindd_setup_sig_term_handler(bool parent)
        return true;
 }
 
+bool winbindd_setup_stdin_handler(bool parent, bool foreground)
+{
+       bool *is_parent;
+
+       if (foreground) {
+               is_parent = talloc(winbind_event_context(), bool);
+               if (!is_parent) {
+                       return false;
+               }
+               
+               *is_parent = parent;
+
+               /* 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(winbind_event_context(), is_parent, 0, TEVENT_FD_READ, winbindd_stdin_handler, is_parent);
+       }
+       
+       return true;
+}
+
 static void winbindd_sig_hup_handler(struct tevent_context *ev,
                                     struct tevent_signal *se,
                                     int signum,
@@ -1028,12 +1070,14 @@ bool winbindd_use_cache(void)
        return !opt_nocache;
 }
 
-void winbindd_register_handlers(void)
+void winbindd_register_handlers(bool foreground)
 {
        /* Setup signal handlers */
 
        if (!winbindd_setup_sig_term_handler(true))
                exit(1);
+       if (!winbindd_setup_stdin_handler(true, foreground))
+               exit(1);
        if (!winbindd_setup_sig_hup_handler(NULL))
                exit(1);
        if (!winbindd_setup_sig_chld_handler())
@@ -1413,7 +1457,7 @@ int main(int argc, char **argv, char **envp)
                exit(1);
        }
 
-       winbindd_register_handlers();
+       winbindd_register_handlers(!Fork);
 
        status = init_system_info();
        if (!NT_STATUS_IS_OK(status)) {
index b965fda..3746fe0 100644 (file)
@@ -28,10 +28,11 @@ struct messaging_context *winbind_messaging_context(void);
 void request_error(struct winbindd_cli_state *state);
 void request_ok(struct winbindd_cli_state *state);
 bool winbindd_setup_sig_term_handler(bool parent);
+bool winbindd_setup_stdin_handler(bool parent, bool foreground);
 bool winbindd_setup_sig_hup_handler(const char *lfile);
 bool winbindd_use_idmap_cache(void);
 bool winbindd_use_cache(void);
-void winbindd_register_handlers(void);
+void winbindd_register_handlers(bool foreground);
 const char *get_winbind_pipe_dir(void);
 char *get_winbind_priv_pipe_dir(void);
 int main(int argc, char **argv, char **envp);