From 0bf0bb9b3448a19e081c05050eafd76a8c0d0bd7 Mon Sep 17 00:00:00 2001
From: Stephen Gallagher <sgallagh@redhat.com>
Date: Mon, 2 May 2011 13:46:27 -0400
Subject: [PATCH 2/2] Allow changing the log level without restart

We will now re-read the confdb debug_level value when processing
the monitor_common_logrotate() function, which occurs when the
monitor receives a SIGHUP.
---
 src/monitor/monitor.c                   |    4 +++-
 src/monitor/monitor_interfaces.h        |    6 ++++--
 src/monitor/monitor_sbus.c              |   25 +++++++++++++++++++++----
 src/providers/data_provider_be.c        |   25 ++++++++++++++++++++-----
 src/responder/common/responder.h        |    3 +++
 src/responder/common/responder_common.c |   12 ++++++++++++
 src/responder/nss/nsssrv.c              |    2 +-
 src/responder/pam/pamsrv.c              |    2 +-
 src/util/debug.c                        |    1 -
 src/util/server.c                       |   26 ++++++++++++++++++++++++--
 10 files changed, 89 insertions(+), 17 deletions(-)

diff --git a/src/monitor/monitor.c b/src/monitor/monitor.c
index 194e74c5d2119cdbf693363ebe4777d0f5c130f9..baa9994e4213a707b3596057fe8a9afb1fb5f066 100644
--- a/src/monitor/monitor.c
+++ b/src/monitor/monitor.c
@@ -122,6 +122,7 @@ struct mt_ctx {
     bool check_children;
     bool services_started;
     struct netlink_ctx *nlctx;
+    const char *conf_path;
 };
 
 static int start_service(struct mt_svc *mt_svc);
@@ -2372,7 +2373,8 @@ int main(int argc, const char *argv[])
     }
 
     /* set up things like debug , signals, daemonization, etc... */
-    ret = server_setup("sssd", flags, CONFDB_MONITOR_CONF_ENTRY, &main_ctx);
+    monitor->conf_path = CONFDB_MONITOR_CONF_ENTRY;
+    ret = server_setup("sssd", flags, monitor->conf_path, &main_ctx);
     if (ret != EOK) return 2;
 
     monitor->ev = main_ctx->event_ctx;
diff --git a/src/monitor/monitor_interfaces.h b/src/monitor/monitor_interfaces.h
index 8ec6d89bdaa25a7ce312aafc6de80a4bad584378..51ac254cd6d10da61deeebdbc4592a12e05a2f3c 100644
--- a/src/monitor/monitor_interfaces.h
+++ b/src/monitor/monitor_interfaces.h
@@ -19,6 +19,8 @@
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
+#include "sbus/sssd_dbus.h"
+
 /*** Monitor ***/
 
 #define MONITOR_VERSION 0x0001
@@ -53,8 +55,8 @@ 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);
+int monitor_common_rotate_logs(struct confdb_ctx *confdb,
+                               const char *conf_entry);
 
 errno_t sss_monitor_init(TALLOC_CTX *mem_ctx,
                          struct tevent_context *ev,
diff --git a/src/monitor/monitor_sbus.c b/src/monitor/monitor_sbus.c
index 3d0d9d310210a423fda4b6841b42476e08e36fad..632de496f5da06da9218b5b03006b3255a1a7f88 100644
--- a/src/monitor/monitor_sbus.c
+++ b/src/monitor/monitor_sbus.c
@@ -178,10 +178,11 @@ int monitor_common_res_init(DBusMessage *message,
     return monitor_common_pong(message, conn);
 }
 
-int monitor_common_rotate_logs(DBusMessage *message,
-                               struct sbus_connection *conn)
+errno_t monitor_common_rotate_logs(struct confdb_ctx *confdb,
+                                   const char *conf_path)
 {
-    int ret;
+    errno_t ret;
+    int old_debug_level = debug_level;
 
     ret = rotate_debug_files();
     if (ret) {
@@ -190,7 +191,23 @@ int monitor_common_rotate_logs(DBusMessage *message,
         return ret;
     }
 
-    return monitor_common_pong(message, conn);
+    /* Get new debug level from the confdb */
+    ret = confdb_get_int(confdb, NULL, conf_path,
+                         CONFDB_SERVICE_DEBUG_LEVEL,
+                         old_debug_level,
+                         &debug_level);
+    if (ret != EOK) {
+        DEBUG(0, ("Error reading from confdb (%d) [%s]\n",
+                  ret, strerror(ret)));
+        /* Try to proceed with the old value */
+        debug_level = old_debug_level;
+    }
+
+    if (debug_level != old_debug_level) {
+        DEBUG(0, ("Debug level changed to %d\n", debug_level));
+    }
+
+    return EOK;
 }
 
 errno_t sss_monitor_init(TALLOC_CTX *mem_ctx,
diff --git a/src/providers/data_provider_be.c b/src/providers/data_provider_be.c
index 0fbf809e50784c4601ff4d29d3db84713543a979..380e6cdb1df6d6f253615d62f7faa8996d144872 100644
--- a/src/providers/data_provider_be.c
+++ b/src/providers/data_provider_be.c
@@ -57,13 +57,15 @@ static int data_provider_go_offline(DBusMessage *message,
                                     struct sbus_connection *conn);
 static int data_provider_reset_offline(DBusMessage *message,
                                        struct sbus_connection *conn);
+static int data_provider_logrotate(DBusMessage *message,
+                                struct sbus_connection *conn);
 
 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_RESET_OFFLINE, data_provider_reset_offline },
-    { MON_CLI_METHOD_ROTATE, monitor_common_rotate_logs },
+    { MON_CLI_METHOD_ROTATE, data_provider_logrotate },
     { NULL, NULL }
 };
 
@@ -1183,8 +1185,8 @@ int main(int argc, const char *argv[])
     poptContext pc;
     char *be_domain = NULL;
     char *srv_name = NULL;
-    char *conf_entry = NULL;
     struct main_context *main_ctx;
+    char *confdb_path;
     int ret;
 
     struct poptOption long_options[] = {
@@ -1222,10 +1224,10 @@ int main(int argc, const char *argv[])
     srv_name = talloc_asprintf(NULL, "sssd[be[%s]]", be_domain);
     if (!srv_name) return 2;
 
-    conf_entry = talloc_asprintf(NULL, CONFDB_DOMAIN_PATH_TMPL, be_domain);
-    if (!conf_entry) return 2;
+    confdb_path = talloc_asprintf(NULL, CONFDB_DOMAIN_PATH_TMPL, be_domain);
+    if (!confdb_path) return 2;
 
-    ret = server_setup(srv_name, 0, conf_entry, &main_ctx);
+    ret = server_setup(srv_name, 0, confdb_path, &main_ctx);
     if (ret != EOK) {
         DEBUG(0, ("Could not set up mainloop [%d]\n", ret));
         return 2;
@@ -1283,3 +1285,16 @@ static int data_provider_reset_offline(DBusMessage *message,
     check_if_online(be_ctx);
     return monitor_common_pong(message, conn);
 }
+
+static int data_provider_logrotate(DBusMessage *message,
+                                struct sbus_connection *conn)
+{
+    errno_t ret;
+    struct be_ctx *be_ctx = talloc_get_type(sbus_conn_get_private_data(conn),
+                                            struct be_ctx);
+
+    ret = monitor_common_rotate_logs(be_ctx->cdb, be_ctx->conf_path);
+    if (ret != EOK) return ret;
+
+    return monitor_common_pong(message, conn);
+}
diff --git a/src/responder/common/responder.h b/src/responder/common/responder.h
index 6b81aadacc2f4c47afb8363523e6cf00dbe4f6b6..321cedda8f495e38e19fe07b60271d54915316b7 100644
--- a/src/responder/common/responder.h
+++ b/src/responder/common/responder.h
@@ -171,4 +171,7 @@ int sss_dp_send_acct_req(struct resp_ctx *rctx, TALLOC_CTX *callback_memctx,
                          bool fast_reply, int type,
                          const char *opt_name, uint32_t opt_id);
 
+int responder_logrotate(DBusMessage *message,
+                        struct sbus_connection *conn);
+
 #endif /* __SSS_RESPONDER_H__ */
diff --git a/src/responder/common/responder_common.c b/src/responder/common/responder_common.c
index 4ddb549c16ab96717d0771a8d37b4fb0d83b66ce..37761ae9a9f556980abc97dd05527de2da880795 100644
--- a/src/responder/common/responder_common.c
+++ b/src/responder/common/responder_common.c
@@ -652,3 +652,15 @@ int sss_dp_get_domain_conn(struct resp_ctx *rctx, const char *domain,
     return EOK;
 }
 
+int responder_logrotate(DBusMessage *message,
+                        struct sbus_connection *conn)
+{
+    errno_t ret;
+    struct resp_ctx *rctx = talloc_get_type(sbus_conn_get_private_data(conn),
+                                            struct resp_ctx);
+
+    ret = monitor_common_rotate_logs(rctx->cdb, rctx->confdb_service_path);
+    if (ret != EOK) return ret;
+
+    return monitor_common_pong(message, conn);
+}
diff --git a/src/responder/nss/nsssrv.c b/src/responder/nss/nsssrv.c
index dfb0312e849248edef86a628282ec5617ebf613c..a8527ddd96d5692499f50dd7b306fbb9be013f16 100644
--- a/src/responder/nss/nsssrv.c
+++ b/src/responder/nss/nsssrv.c
@@ -51,7 +51,7 @@
 struct sbus_method monitor_nss_methods[] = {
     { MON_CLI_METHOD_PING, monitor_common_pong },
     { MON_CLI_METHOD_RES_INIT, monitor_common_res_init },
-    { MON_CLI_METHOD_ROTATE, monitor_common_rotate_logs },
+    { MON_CLI_METHOD_ROTATE, responder_logrotate },
     { NULL, NULL }
 };
 
diff --git a/src/responder/pam/pamsrv.c b/src/responder/pam/pamsrv.c
index 91ee4a899aebbf9646a796f1e31a16797360de86..86283eaeca169af600814b1c52e7312c5f06558b 100644
--- a/src/responder/pam/pamsrv.c
+++ b/src/responder/pam/pamsrv.c
@@ -50,7 +50,7 @@
 struct sbus_method monitor_pam_methods[] = {
     { MON_CLI_METHOD_PING, monitor_common_pong },
     { MON_CLI_METHOD_RES_INIT, monitor_common_res_init },
-    { MON_CLI_METHOD_ROTATE, monitor_common_rotate_logs },
+    { MON_CLI_METHOD_ROTATE, responder_logrotate },
     { NULL, NULL }
 };
 
diff --git a/src/util/debug.c b/src/util/debug.c
index 129b9d9b1c7f588998060ee76f9b05c51e6446cf..dbd54c1e993fffb77fff311a275e351ac84898d8 100644
--- a/src/util/debug.c
+++ b/src/util/debug.c
@@ -39,7 +39,6 @@ int debug_to_file = 0;
 const char *debug_log_file = "sssd";
 FILE *debug_file = NULL;
 
-
 errno_t set_debug_file_from_fd(const int fd)
 {
     FILE *dummy;
diff --git a/src/util/server.c b/src/util/server.c
index 977c751179f487733ef61ea0592cbc6c483279e3..e12623738504b0df53a7f0ccc87379e0d395f7b4 100644
--- a/src/util/server.c
+++ b/src/util/server.c
@@ -31,6 +31,7 @@
 #include "util/util.h"
 #include "ldb.h"
 #include "confdb/confdb.h"
+#include "monitor/monitor_interfaces.h"
 
 #ifdef HAVE_PRCTL
 #include <sys/prctl.h>
@@ -346,6 +347,11 @@ int die_if_parent_died(void)
     return EOK;
 }
 
+struct logrotate_ctx {
+    struct confdb_ctx *confdb;
+    const char *confdb_path;
+};
+
 static void te_server_hup(struct tevent_context *ev,
                           struct tevent_signal *se,
                           int signum,
@@ -353,8 +359,17 @@ static void te_server_hup(struct tevent_context *ev,
                           void *siginfo,
                           void *private_data)
 {
+    errno_t ret;
+    struct logrotate_ctx *lctx =
+            talloc_get_type(private_data, struct logrotate_ctx);
+
     DEBUG(1, ("Received SIGHUP. Rotating logfiles.\n"));
-    rotate_debug_files();
+
+    ret = monitor_common_rotate_logs(lctx->confdb, lctx->confdb_path);
+    if (ret != EOK) {
+        DEBUG(0, ("Could not reopen log file [%s]\n",
+                  strerror(ret)));
+    }
 }
 
 int server_setup(const char *name, int flags,
@@ -369,6 +384,7 @@ int server_setup(const char *name, int flags,
     bool dt;
     bool dl;
     struct tevent_signal *tes;
+    struct logrotate_ctx *lctx;
 
     debug_prg_name = strdup(name);
     if (!debug_prg_name) {
@@ -483,8 +499,14 @@ int server_setup(const char *name, int flags,
     if (dl) debug_to_file = 1;
 
     /* before opening the log file set up log rotation */
+    lctx = talloc_zero(ctx, struct logrotate_ctx);
+    if (!lctx) return ENOMEM;
+
+    lctx->confdb = ctx->confdb_ctx;
+    lctx->confdb_path = conf_entry;
+
     tes = tevent_add_signal(ctx->event_ctx, ctx, SIGHUP, 0,
-                            te_server_hup, NULL);
+                            te_server_hup, lctx);
     if (tes == NULL) {
         return EIO;
     }
-- 
1.7.5

