From 3242dd4a49c3869c9b066a63afb81cebf1a35b7d Mon Sep 17 00:00:00 2001 From: Simo Sorce Date: Nov 12 2013 10:24:58 +0000 Subject: Signals: Refactor termination of processes sig_term() was never used as a real signal handler, but only called by tevent signal handlers in the kerberos and ldap children. Also the same code was duplicated with separate local guard variables in other functions. Unify orderly termination handling, between all these functions. --- diff --git a/src/providers/krb5/krb5_common.c b/src/providers/krb5/krb5_common.c index c40f0dd..d1d26ee 100644 --- a/src/providers/krb5/krb5_common.c +++ b/src/providers/krb5/krb5_common.c @@ -887,7 +887,7 @@ void krb5_finalize(struct tevent_context *ev, DEBUG(1, ("remove_krb5_info_files failed.\n")); } - sig_term(signum); + orderly_shutdown(0); } errno_t krb5_install_offline_callback(struct be_ctx *be_ctx, diff --git a/src/providers/ldap/ldap_common.c b/src/providers/ldap/ldap_common.c index cd38ac3..07fb6ae 100644 --- a/src/providers/ldap/ldap_common.c +++ b/src/providers/ldap/ldap_common.c @@ -1090,7 +1090,7 @@ static void sdap_finalize(struct tevent_context *ev, DEBUG(1, ("remove_krb5_info_files failed.\n")); } - sig_term(signum); + orderly_shutdown(0); } errno_t sdap_install_sigterm_handler(TALLOC_CTX *mem_ctx, diff --git a/src/util/server.c b/src/util/server.c index 11569b9..b2fad11 100644 --- a/src/util/server.c +++ b/src/util/server.c @@ -237,18 +237,18 @@ int pidfile(const char *path, const char *name) return 0; } -void sig_term(int sig) +void orderly_shutdown(int status) { #if HAVE_GETPGRP - static int done_sigterm; - if (done_sigterm == 0 && getpgrp() == getpid()) { + static int sent_sigterm; + if (sent_sigterm == 0 && getpgrp() == getpid()) { DEBUG(SSSDBG_FATAL_FAILURE, ("SIGTERM: killing children\n")); - done_sigterm = 1; + sent_sigterm = 1; kill(-getpgrp(), SIGTERM); } #endif - sss_log(SSS_LOG_INFO, "Shutting down"); - exit(0); + if (status == 0) sss_log(SSS_LOG_INFO, "Shutting down"); + exit(status); } static void default_quit(struct tevent_context *ev, @@ -258,31 +258,15 @@ static void default_quit(struct tevent_context *ev, void *siginfo, void *private_data) { -#if HAVE_GETPGRP - static int done_sigterm; - if (done_sigterm == 0 && getpgrp() == getpid()) { - DEBUG(SSSDBG_FATAL_FAILURE, ("SIGTERM: killing children\n")); - done_sigterm = 1; - kill(-getpgrp(), SIGTERM); - } -#endif - sss_log(SSS_LOG_INFO, "Shutting down"); - exit(0); + orderly_shutdown(0); } #ifndef HAVE_PRCTL static void sig_segv_abrt(int sig) { -#if HAVE_GETPGRP - static int done; - if (done == 0 && getpgrp() == getpid()) { - DEBUG(SSSDBG_FATAL_FAILURE, ("%s: killing children\n", - strsignal(sig))); - done = 1; - kill(-getpgrp(), SIGTERM); - } -#endif /* HAVE_GETPGRP */ - exit(1); + DEBUG(SSSDBG_FATAL_FAILURE, + ("Received signal %s, shutting down\n", strsignal(sig))); + orderly_shutdown(1); } #endif /* HAVE_PRCTL */ diff --git a/src/util/util.h b/src/util/util.h index 581ba6f..4c0e75f 100644 --- a/src/util/util.h +++ b/src/util/util.h @@ -298,7 +298,7 @@ int server_setup(const char *name, int flags, const char *conf_entry, struct main_context **main_ctx); void server_loop(struct main_context *main_ctx); -void sig_term(int sig); +void orderly_shutdown(int status); /* from signal.c */ #include