s3:winbindd: handle SIG_TERM, SIGHUP, SIGCHLD and SIGUSR2 via tevent
authorStefan Metzmacher <metze@samba.org>
Thu, 22 Jan 2009 13:54:21 +0000 (14:54 +0100)
committerStefan Metzmacher <metze@samba.org>
Tue, 27 Jan 2009 14:28:08 +0000 (15:28 +0100)
metze

source3/winbindd/winbindd.c
source3/winbindd/winbindd_dual.c
source3/winbindd/winbindd_proto.h

index fad27ea224f4b4a7fc6329e74c303100f536cd0d..5720bfc5178c995f25d48a98e4164b1eb55edf77 100644 (file)
@@ -173,36 +173,161 @@ static void terminate(bool is_parent)
        exit(0);
 }
 
-static SIG_ATOMIC_T do_sigterm = 0;
+static void winbindd_sig_term_handler(struct tevent_context *ev,
+                                     struct tevent_signal *se,
+                                     int signum,
+                                     int count,
+                                     void *siginfo,
+                                     void *private_data)
+{
+       bool *is_parent = talloc_get_type_abort(private_data, bool);
+
+       DEBUG(0,("Got sig[%d] terminate (is_parent=%d)\n",
+                signum, (int)*is_parent));
+       terminate(*is_parent);
+}
 
-static void termination_handler(int signum)
+bool winbindd_setup_sig_term_handler(bool parent)
 {
-       do_sigterm = 1;
-       sys_select_signal(signum);
+       struct tevent_signal *se;
+       bool *is_parent;
+
+       is_parent = talloc(winbind_event_context(), bool);
+       if (!is_parent) {
+               return false;
+       }
+
+       *is_parent = parent;
+
+       se = tevent_add_signal(winbind_event_context(),
+                              is_parent,
+                              SIGTERM, 0,
+                              winbindd_sig_term_handler,
+                              is_parent);
+       if (!se) {
+               DEBUG(0,("failed to setup SIGTERM handler"));
+               talloc_free(is_parent);
+               return false;
+       }
+
+       se = tevent_add_signal(winbind_event_context(),
+                              is_parent,
+                              SIGINT, 0,
+                              winbindd_sig_term_handler,
+                              is_parent);
+       if (!se) {
+               DEBUG(0,("failed to setup SIGINT handler"));
+               talloc_free(is_parent);
+               return false;
+       }
+
+       se = tevent_add_signal(winbind_event_context(),
+                              is_parent,
+                              SIGQUIT, 0,
+                              winbindd_sig_term_handler,
+                              is_parent);
+       if (!se) {
+               DEBUG(0,("failed to setup SIGINT handler"));
+               talloc_free(is_parent);
+               return false;
+       }
+
+       return true;
 }
 
-static SIG_ATOMIC_T do_sigusr2 = 0;
+static void winbindd_sig_hup_handler(struct tevent_context *ev,
+                                    struct tevent_signal *se,
+                                    int signum,
+                                    int count,
+                                    void *siginfo,
+                                    void *private_data)
+{
+       const char *file = (const char *)private_data;
+
+       DEBUG(1,("Reloading services after SIGHUP\n"));
+       flush_caches();
+       reload_services_file(file);
+}
 
-static void sigusr2_handler(int signum)
+bool winbindd_setup_sig_hup_handler(const char *lfile)
 {
-       do_sigusr2 = 1;
-       sys_select_signal(SIGUSR2);
+       struct tevent_signal *se;
+       char *file = NULL;
+
+       if (lfile) {
+               file = talloc_strdup(winbind_event_context(),
+                                    lfile);
+               if (!file) {
+                       return false;
+               }
+       }
+
+       se = tevent_add_signal(winbind_event_context(),
+                              winbind_event_context(),
+                              SIGHUP, 0,
+                              winbindd_sig_hup_handler,
+                              file);
+       if (!se) {
+               return false;
+       }
+
+       return true;
 }
 
-static SIG_ATOMIC_T do_sighup = 0;
+static void winbindd_sig_chld_handler(struct tevent_context *ev,
+                                     struct tevent_signal *se,
+                                     int signum,
+                                     int count,
+                                     void *siginfo,
+                                     void *private_data)
+{
+       pid_t pid;
+
+       while ((pid = sys_waitpid(-1, NULL, WNOHANG)) > 0) {
+               winbind_child_died(pid);
+       }
+}
 
-static void sighup_handler(int signum)
+static bool winbindd_setup_sig_chld_handler(void)
 {
-       do_sighup = 1;
-       sys_select_signal(SIGHUP);
+       struct tevent_signal *se;
+
+       se = tevent_add_signal(winbind_event_context(),
+                              winbind_event_context(),
+                              SIGCHLD, 0,
+                              winbindd_sig_chld_handler,
+                              NULL);
+       if (!se) {
+               return false;
+       }
+
+       return true;
 }
 
-static SIG_ATOMIC_T do_sigchld = 0;
+static void winbindd_sig_usr2_handler(struct tevent_context *ev,
+                                     struct tevent_signal *se,
+                                     int signum,
+                                     int count,
+                                     void *siginfo,
+                                     void *private_data)
+{
+       print_winbindd_status();
+}
 
-static void sigchld_handler(int signum)
+static bool winbindd_setup_sig_usr2_handler(void)
 {
-       do_sigchld = 1;
-       sys_select_signal(SIGCHLD);
+       struct tevent_signal *se;
+
+       se = tevent_add_signal(winbind_event_context(),
+                              winbind_event_context(),
+                              SIGCHLD, 0,
+                              winbindd_sig_usr2_handler,
+                              NULL);
+       if (!se) {
+               return false;
+       }
+
+       return true;
 }
 
 /* React on 'smbcontrol winbindd reload-config' in the same way as on SIGHUP*/
@@ -224,7 +349,9 @@ static void msg_shutdown(struct messaging_context *msg,
                         struct server_id server_id,
                         DATA_BLOB *data)
 {
-       do_sigterm = 1;
+       /* only the parent waits for this message */
+       DEBUG(0,("Got shutdown message\n"));
+       terminate(true);
 }
 
 
@@ -797,27 +924,6 @@ static bool remove_idle_client(void)
        return False;
 }
 
-/* check if HUP has been received and reload files */
-void winbind_check_sighup(const char *lfile)
-{
-       if (do_sighup) {
-
-               DEBUG(3, ("got SIGHUP\n"));
-
-               flush_caches();
-               reload_services_file(lfile);
-
-               do_sighup = 0;
-       }
-}
-
-/* check if TERM has been received */
-void winbind_check_sigterm(bool is_parent)
-{
-       if (do_sigterm)
-               terminate(is_parent);
-}
-
 /* Process incoming clients on listen_sock.  We use a tricky non-blocking,
    non-forking, non-threaded model which allows us to handle many
    simultaneous connections while remaining impervious to many denial of
@@ -979,26 +1085,6 @@ static void process_loop(void)
 #if 0
        winbindd_check_cache_size(time(NULL));
 #endif
-
-       /* Check signal handling things */
-
-       winbind_check_sigterm(true);
-       winbind_check_sighup(NULL);
-
-       if (do_sigusr2) {
-               print_winbindd_status();
-               do_sigusr2 = 0;
-       }
-
-       if (do_sigchld) {
-               pid_t pid;
-
-               do_sigchld = 0;
-
-               while ((pid = sys_waitpid(-1, NULL, WNOHANG)) > 0) {
-                       winbind_child_died(pid);
-               }
-       }
 }
 
 /* Main function */
@@ -1168,18 +1254,6 @@ int main(int argc, char **argv, char **envp)
        BlockSignals(False, SIGHUP);
        BlockSignals(False, SIGCHLD);
 
-       /* Setup signal handlers */
-
-       CatchSignal(SIGINT, termination_handler);      /* Exit on these sigs */
-       CatchSignal(SIGQUIT, termination_handler);
-       CatchSignal(SIGTERM, termination_handler);
-       CatchSignal(SIGCHLD, sigchld_handler);
-
-       CatchSignal(SIGPIPE, SIG_IGN);                 /* Ignore sigpipe */
-
-       CatchSignal(SIGUSR2, sigusr2_handler);         /* Debugging sigs */
-       CatchSignal(SIGHUP, sighup_handler);
-
        if (!interactive)
                become_daemon(Fork, no_process_group);
 
@@ -1207,6 +1281,19 @@ int main(int argc, char **argv, char **envp)
                exit(1);
        }
 
+       /* Setup signal handlers */
+
+       if (!winbindd_setup_sig_term_handler(true))
+               exit(1);
+       if (!winbindd_setup_sig_hup_handler(NULL))
+               exit(1);
+       if (!winbindd_setup_sig_chld_handler())
+               exit(1);
+       if (!winbindd_setup_sig_usr2_handler())
+               exit(1);
+
+       CatchSignal(SIGPIPE, SIG_IGN);                 /* Ignore sigpipe */
+
        /*
         * Ensure all cache and idmap caches are consistent
         * and initialized before we startup.
index c320e967469bee1b437fdc3ae408a0b3c78bfdd5..1385c76bae497a09ff94de217018d836c5daeff9 100644 (file)
@@ -1160,6 +1160,12 @@ bool winbindd_reinit_after_fork(const char *logfilename)
                reopen_logs();
        }
 
+       if (!winbindd_setup_sig_term_handler(false))
+               return false;
+       if (!winbindd_setup_sig_hup_handler(override_logfile ? NULL :
+                                           logfilename))
+               return false;
+
        /* Don't handle the same messages as our parent. */
        messaging_deregister(winbind_messaging_context(),
                             MSG_SMB_CONF_UPDATED, NULL);
@@ -1379,11 +1385,6 @@ static bool fork_domain_child(struct winbindd_child *child)
                struct timeval now;
                TALLOC_CTX *frame = talloc_stackframe();
 
-               /* check for signals */
-               winbind_check_sigterm(false);
-               winbind_check_sighup(override_logfile ? NULL :
-                               child->logfilename);
-
                if (run_events(winbind_event_context(), 0, NULL, NULL)) {
                        TALLOC_FREE(frame);
                        continue;
index 594f8be9423e34d5cc66b7965b19a84d8fafc85e..d72351d02139523204fccf673cddab6d5307f28b 100644 (file)
@@ -63,8 +63,8 @@ void setup_async_write(struct winbindd_fd_event *event, void *data, size_t lengt
                       void *private_data);
 void request_error(struct winbindd_cli_state *state);
 void request_ok(struct winbindd_cli_state *state);
-void winbind_check_sighup(const char *lfile);
-void winbind_check_sigterm(bool in_parent);
+bool winbindd_setup_sig_term_handler(bool parent);
+bool winbindd_setup_sig_hup_handler(const char *logfile);
 int main(int argc, char **argv, char **envp);
 
 /* The following definitions come from winbindd/winbindd_reqtrans.c  */