>From f136cd82146060e4f3a68e06ab821daaa8e7a557 Mon Sep 17 00:00:00 2001
From: Simo Sorce <simo@redhat.com>
Date: Sat, 9 Nov 2013 16:07:06 -0500
Subject: [PATCH 3/3] 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.
---
 src/providers/krb5/krb5_common.c |  2 +-
 src/providers/ldap/ldap_common.c |  2 +-
 src/util/server.c                | 36 ++++++++++--------------------------
 src/util/util.h                  |  2 +-
 4 files changed, 13 insertions(+), 29 deletions(-)

diff --git a/src/providers/krb5/krb5_common.c b/src/providers/krb5/krb5_common.c
index c40f0dd493a6df5091553044b99d1f8807de7fd6..d1d26ee711db992e30d0d107c8c2fbcc41327368 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 83f901c74f5916e67ec3216036c8cfa74b5eb99d..e29a5219768df154608aad9740dd9ed44c14e22e 100644
--- a/src/providers/ldap/ldap_common.c
+++ b/src/providers/ldap/ldap_common.c
@@ -1089,7 +1089,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 005bd5e0f82810a70e9c3094a4c42de3277147d0..ec3fc24bcb26fbc6c0873e57d04c34218569cc72 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 581ba6fec0ece4d0178b89f137358e106e27ecaa..4c0e75f125ed4574add3bd0b128e8a58069a33cc 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 <signal.h>
-- 
1.8.3.1

