From: Stefan Metzmacher Date: Thu, 22 Jan 2009 13:54:21 +0000 (+0100) Subject: s3:winbindd: handle SIG_TERM, SIGHUP, SIGCHLD and SIGUSR2 via tevent X-Git-Url: http://git.samba.org/?p=metze%2Fsamba%2Fwip.git;a=commitdiff_plain;h=e6612c99fe6c2cb87f09dbc76d077bed789a592d s3:winbindd: handle SIG_TERM, SIGHUP, SIGCHLD and SIGUSR2 via tevent metze --- diff --git a/source3/winbindd/winbindd.c b/source3/winbindd/winbindd.c index fad27ea224f4..5720bfc5178c 100644 --- a/source3/winbindd/winbindd.c +++ b/source3/winbindd/winbindd.c @@ -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. diff --git a/source3/winbindd/winbindd_dual.c b/source3/winbindd/winbindd_dual.c index c320e967469b..1385c76bae49 100644 --- a/source3/winbindd/winbindd_dual.c +++ b/source3/winbindd/winbindd_dual.c @@ -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; diff --git a/source3/winbindd/winbindd_proto.h b/source3/winbindd/winbindd_proto.h index 594f8be9423e..d72351d02139 100644 --- a/source3/winbindd/winbindd_proto.h +++ b/source3/winbindd/winbindd_proto.h @@ -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 */