From f0411ee6a9589d8781db0fc8e0576099436fc3e7 Mon Sep 17 00:00:00 2001
From: Jakub Hrozek <jhrozek@redhat.com>
Date: Wed, 24 Feb 2010 18:58:15 +0100
Subject: [PATCH 1/2] Reopen logs when SIGHUP is caught

Upon receiving SIGHUP, the monitor signals all services to reopen their
debug logs. It is also possible to signal individual services to reopen
their particular files.

Fixes: #332
---
 src/monitor/monitor.c            |   13 ++++++++++++-
 src/monitor/monitor_interfaces.h |    3 +++
 src/monitor/monitor_sbus.c       |   13 +++++++++++++
 src/providers/data_provider_be.c |    2 ++
 src/responder/nss/nsssrv.c       |    1 +
 src/responder/pam/pamsrv.c       |    1 +
 src/util/debug.c                 |   13 +++++++++++++
 src/util/server.c                |   19 +++++++++++++++++++
 src/util/util.h                  |    1 +
 9 files changed, 65 insertions(+), 1 deletions(-)

diff --git a/src/monitor/monitor.c b/src/monitor/monitor.c
index ddbfb6a..8244cce 100644
--- a/src/monitor/monitor.c
+++ b/src/monitor/monitor.c
@@ -862,6 +862,10 @@ static int service_signal_offline(struct mt_svc *svc)
 {
     return service_signal(svc, MON_CLI_METHOD_OFFLINE);
 }
+static int service_signal_rotate(struct mt_svc *svc)
+{
+    return service_signal(svc, MON_CLI_METHOD_ROTATE);
+}
 
 static int check_domain_ranges(struct sss_domain_info *domains)
 {
@@ -1453,9 +1457,16 @@ static void monitor_hup(struct tevent_context *ev,
                         void *private_data)
 {
     struct mt_ctx *ctx = talloc_get_type(private_data, struct mt_ctx);
+    struct mt_svc *cur_svc;
 
-    DEBUG(1, ("Received SIGHUP. Rereading configuration.\n"));
+    DEBUG(1, ("Received SIGHUP. Rereading configuration and "
+              "signaling children to rotate logs.\n"));
     update_monitor_config(ctx);
+
+    /* Signal all services to rotate debug files */
+    for(cur_svc = ctx->svc_list; cur_svc; cur_svc = cur_svc->next) {
+        service_signal_rotate(cur_svc);
+    }
 }
 
 static int monitor_cleanup(void)
diff --git a/src/monitor/monitor_interfaces.h b/src/monitor/monitor_interfaces.h
index c6361fa..6e043d8 100644
--- a/src/monitor/monitor_interfaces.h
+++ b/src/monitor/monitor_interfaces.h
@@ -42,6 +42,7 @@
 #define MON_CLI_METHOD_SHUTDOWN "shutDown"
 #define MON_CLI_METHOD_RES_INIT "resInit"
 #define MON_CLI_METHOD_OFFLINE "goOffline" /* Applicable only to providers */
+#define MON_CLI_METHOD_ROTATE "rotateLogs"
 
 #define SSSD_SERVICE_PIPE "private/sbus-monitor"
 
@@ -52,4 +53,6 @@ int monitor_common_pong(DBusMessage *message,
                         struct sbus_connection *conn);
 int monitor_common_res_init(DBusMessage *message,
                             struct sbus_connection *conn);
+int monitor_common_rotate_logs(DBusMessage *message,
+                               struct sbus_connection *conn);
 
diff --git a/src/monitor/monitor_sbus.c b/src/monitor/monitor_sbus.c
index 3f73e84..d60a087 100644
--- a/src/monitor/monitor_sbus.c
+++ b/src/monitor/monitor_sbus.c
@@ -193,3 +193,16 @@ int monitor_common_res_init(DBusMessage *message,
     return monitor_common_pong(message, conn);
 }
 
+int monitor_common_rotate_logs(DBusMessage *message,
+                               struct sbus_connection *conn)
+{
+    int ret;
+
+    ret = rotate_debug_files();
+    if (ret) {
+        DEBUG(1, ("Could not rotate debug files!\n"));
+        return ret;
+    }
+
+    return monitor_common_pong(message, conn);
+}
diff --git a/src/providers/data_provider_be.c b/src/providers/data_provider_be.c
index c384d01..977e866 100644
--- a/src/providers/data_provider_be.c
+++ b/src/providers/data_provider_be.c
@@ -60,6 +60,7 @@ struct sbus_method monitor_be_methods[] = {
     { MON_CLI_METHOD_PING, monitor_common_pong },
     { MON_CLI_METHOD_RES_INIT, data_provider_res_init },
     { MON_CLI_METHOD_OFFLINE, data_provider_go_offline },
+    { MON_CLI_METHOD_ROTATE, monitor_common_rotate_logs },
     { NULL, NULL }
 };
 
@@ -1231,3 +1232,4 @@ static int data_provider_go_offline(DBusMessage *message,
     be_mark_offline(be_ctx);
     return monitor_common_pong(message, conn);
 }
+
diff --git a/src/responder/nss/nsssrv.c b/src/responder/nss/nsssrv.c
index 1b55baf..ed6d1bf 100644
--- a/src/responder/nss/nsssrv.c
+++ b/src/responder/nss/nsssrv.c
@@ -53,6 +53,7 @@ struct sbus_method monitor_nss_methods[] = {
     { MON_CLI_METHOD_PING, monitor_common_pong },
     { MON_CLI_METHOD_RELOAD, service_reload },
     { MON_CLI_METHOD_RES_INIT, monitor_common_res_init },
+    { MON_CLI_METHOD_ROTATE, monitor_common_rotate_logs },
     { NULL, NULL }
 };
 
diff --git a/src/responder/pam/pamsrv.c b/src/responder/pam/pamsrv.c
index 9abd213..21aa702 100644
--- a/src/responder/pam/pamsrv.c
+++ b/src/responder/pam/pamsrv.c
@@ -52,6 +52,7 @@ struct sbus_method monitor_pam_methods[] = {
     { MON_CLI_METHOD_PING, monitor_common_pong },
     { MON_CLI_METHOD_RELOAD, service_reload },
     { MON_CLI_METHOD_RES_INIT, monitor_common_res_init },
+    { MON_CLI_METHOD_ROTATE, monitor_common_rotate_logs },
     { NULL, NULL }
 };
 
diff --git a/src/util/debug.c b/src/util/debug.c
index d26d31c..5b6fccd 100644
--- a/src/util/debug.c
+++ b/src/util/debug.c
@@ -152,3 +152,16 @@ int open_debug_file(void)
 {
     return open_debug_file_ex(NULL, NULL);
 }
+
+int rotate_debug_files(void)
+{
+    int ret;
+
+    if (!debug_to_file) return EOK;
+
+    ret = fclose(debug_file);
+    if (ret) return ret;
+    debug_file = NULL;
+
+    return open_debug_file();
+}
diff --git a/src/util/server.c b/src/util/server.c
index fd6c465..34eb4cc 100644
--- a/src/util/server.c
+++ b/src/util/server.c
@@ -284,6 +284,17 @@ int die_if_parent_died(void)
     return EOK;
 }
 
+static void te_server_hup(struct tevent_context *ev,
+                          struct tevent_signal *se,
+                          int signum,
+                          int count,
+                          void *siginfo,
+                          void *private_data)
+{
+    DEBUG(1, ("Received SIGHUP. Rotating logfiles.\n"));
+    rotate_debug_files();
+}
+
 int server_setup(const char *name, int flags,
                  const char *conf_entry,
                  struct main_context **main_ctx)
@@ -295,6 +306,7 @@ int server_setup(const char *name, int flags,
     int ret = EOK;
     bool dt;
     bool dl;
+    struct tevent_signal *tes;
 
     debug_prg_name = strdup(name);
     if (!debug_prg_name) {
@@ -390,6 +402,13 @@ int server_setup(const char *name, int flags,
     }
     if (dl) debug_to_file = 1;
 
+    /* before opening the log file set up log rotation */
+    tes = tevent_add_signal(ctx->event_ctx, ctx, SIGHUP, 0,
+                            te_server_hup, NULL);
+    if (tes == NULL) {
+        return EIO;
+    }
+
     /* open log file if told so */
     if (debug_to_file) {
         ret = open_debug_file();
diff --git a/src/util/util.h b/src/util/util.h
index 5d2dff2..8e0a571 100644
--- a/src/util/util.h
+++ b/src/util/util.h
@@ -192,6 +192,7 @@ void ldb_debug_messages(void *context, enum ldb_debug_level level,
                         const char *fmt, va_list ap);
 int open_debug_file_ex(const char *filename, FILE **filep);
 int open_debug_file(void);
+int rotate_debug_files(void);
 
 /* from server.c */
 struct main_context {
-- 
1.6.6

