These patches are dependent on the patch provided in my earlier email
"Override config file debug_level with command-line"

Patch 0002: Create common sss_monitor_init()

This was implemented almost identically for both the responders
and the providers. It is easier to maintain as a single routine.

This patch also adds the ability to provide a private context to
attach to the sbus_connection for later use.



Patch 0003: 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.



For now, this can be tested by using ldbedit
on /var/lib/sss/db/confdb.ldb to change the debug_level values and then
send 'kill -HUP $(pidof sssd)'. In the future, I will write a
command-line tool to effect this change more easily.
From 869ed40203558710a51de87a275bdb4fa39f741b Mon Sep 17 00:00:00 2001
From: Stephen Gallagher <sgall...@redhat.com>
Date: Mon, 2 May 2011 10:04:44 -0400
Subject: [PATCH 2/3] Create common sss_monitor_init()

This was implemented almost identically for both the responders
and the providers. It is easier to maintain as a single routine.

This patch also adds the ability to provide a private context to
attach to the sbus_connection for later use.
---
 src/monitor/monitor_interfaces.h        |    7 +++++
 src/monitor/monitor_sbus.c              |   40 +++++++++++++++++++++++++++++++
 src/providers/data_provider_be.c        |   38 ++---------------------------
 src/responder/common/responder_common.c |   37 ++--------------------------
 4 files changed, 53 insertions(+), 69 deletions(-)

diff --git a/src/monitor/monitor_interfaces.h b/src/monitor/monitor_interfaces.h
index 4830f678f3709bf3ab023a57cf251b5245ce86fc..8ec6d89bdaa25a7ce312aafc6de80a4bad584378 100644
--- a/src/monitor/monitor_interfaces.h
+++ b/src/monitor/monitor_interfaces.h
@@ -56,3 +56,10 @@ int monitor_common_res_init(DBusMessage *message,
 int monitor_common_rotate_logs(DBusMessage *message,
                                struct sbus_connection *conn);
 
+errno_t sss_monitor_init(TALLOC_CTX *mem_ctx,
+                         struct tevent_context *ev,
+                         struct sbus_interface *intf,
+                         const char *svc_name,
+                         uint16_t svc_version,
+                         void *pvt,
+                         struct sbus_connection **mon_conn);
diff --git a/src/monitor/monitor_sbus.c b/src/monitor/monitor_sbus.c
index c2106e8624c539d11917623d3f1a85149a902d9d..ed83d339dd51f14dc64c05014397aa7cf700e5a3 100644
--- a/src/monitor/monitor_sbus.c
+++ b/src/monitor/monitor_sbus.c
@@ -27,6 +27,7 @@
 #include "util/util.h"
 #include "confdb/confdb.h"
 #include "sbus/sssd_dbus.h"
+#include "sbus/sbus_client.h"
 #include "monitor/monitor_interfaces.h"
 
 int monitor_get_sbus_address(TALLOC_CTX *mem_ctx, char **address)
@@ -191,3 +192,42 @@ int monitor_common_rotate_logs(DBusMessage *message,
 
     return monitor_common_pong(message, conn);
 }
+
+errno_t sss_monitor_init(TALLOC_CTX *mem_ctx,
+                         struct tevent_context *ev,
+                         struct sbus_interface *intf,
+                         const char *svc_name,
+                         uint16_t svc_version,
+                         void *pvt,
+                         struct sbus_connection **mon_conn)
+{
+    errno_t ret;
+    char *sbus_address;
+    struct sbus_connection *conn;
+
+    /* Set up SBUS connection to the monitor */
+    ret = monitor_get_sbus_address(NULL, &sbus_address);
+    if (ret != EOK) {
+        DEBUG(0, ("Could not locate monitor address.\n"));
+        return ret;
+    }
+
+    ret = sbus_client_init(mem_ctx, ev, sbus_address,
+                           intf, &conn,
+                           NULL, pvt);
+    if (ret != EOK) {
+        DEBUG(0, ("Failed to connect to monitor services.\n"));
+        talloc_free(sbus_address);
+        return ret;
+    }
+    talloc_free(sbus_address);
+
+    /* Identify ourselves to the monitor */
+    ret = monitor_common_send_id(conn, svc_name, svc_version);
+    if (ret != EOK) {
+        DEBUG(0, ("Failed to identify to the monitor!\n"));
+        return ret;
+    }
+
+    return EOK;
+}
diff --git a/src/providers/data_provider_be.c b/src/providers/data_provider_be.c
index 01d28558035f6c936dd541f8df34034e6710809f..0fbf809e50784c4601ff4d29d3db84713543a979 100644
--- a/src/providers/data_provider_be.c
+++ b/src/providers/data_provider_be.c
@@ -868,40 +868,6 @@ static int be_srv_init(struct be_ctx *ctx)
     return EOK;
 }
 
-/* mon_cli_init
- * sbus channel to the monitor daemon */
-static int mon_cli_init(struct be_ctx *ctx)
-{
-    char *sbus_address;
-    int ret;
-
-    /* Set up SBUS connection to the monitor */
-    ret = monitor_get_sbus_address(ctx, &sbus_address);
-    if (ret != EOK) {
-        DEBUG(0, ("Could not locate monitor address.\n"));
-        return ret;
-    }
-
-    ret = sbus_client_init(ctx, ctx->ev, sbus_address,
-                           &monitor_be_interface, &ctx->mon_conn,
-                           NULL, ctx);
-    if (ret != EOK) {
-        DEBUG(0, ("Failed to connect to monitor services.\n"));
-        return ret;
-    }
-
-    /* Identify ourselves to the monitor */
-    ret = monitor_common_send_id(ctx->mon_conn,
-                                 ctx->identity,
-                                 DATA_PROVIDER_VERSION);
-    if (ret != EOK) {
-        DEBUG(0, ("Failed to identify to the monitor!\n"));
-        return ret;
-    }
-
-    return EOK;
-}
-
 static void be_target_access_permit(struct be_req *be_req)
 {
     struct pam_data *pd = talloc_get_type(be_req->req_data, struct pam_data);
@@ -1138,7 +1104,9 @@ int be_process_init(TALLOC_CTX *mem_ctx,
         return ret;
     }
 
-    ret = mon_cli_init(ctx);
+    ret = sss_monitor_init(ctx, ctx->ev, &monitor_be_interface,
+                           ctx->identity, DATA_PROVIDER_VERSION,
+                           ctx, &ctx->mon_conn);
     if (ret != EOK) {
         DEBUG(0, ("fatal error setting up monitor bus\n"));
         return ret;
diff --git a/src/responder/common/responder_common.c b/src/responder/common/responder_common.c
index 2a4a5d20c171ff7c7a8be3dfafaef17844c96c08..4ddb549c16ab96717d0771a8d37b4fb0d83b66ce 100644
--- a/src/responder/common/responder_common.c
+++ b/src/responder/common/responder_common.c
@@ -362,39 +362,6 @@ static void accept_fd_handler(struct tevent_context *ev,
     return;
 }
 
-static int sss_monitor_init(struct resp_ctx *rctx,
-                            struct sbus_interface *intf,
-                            const char *svc_name,
-                            uint16_t svc_version)
-{
-    char *sbus_address;
-    int ret;
-
-    /* Set up SBUS connection to the monitor */
-    ret = monitor_get_sbus_address(rctx, &sbus_address);
-    if (ret != EOK) {
-        DEBUG(0, ("Could not locate monitor address.\n"));
-        return ret;
-    }
-
-    ret = sbus_client_init(rctx, rctx->ev, sbus_address,
-                           intf, &rctx->mon_conn,
-                           NULL, NULL);
-    if (ret != EOK) {
-        DEBUG(0, ("Failed to connect to monitor services.\n"));
-        return ret;
-    }
-
-    /* Identify ourselves to the monitor */
-    ret = monitor_common_send_id(rctx->mon_conn, svc_name, svc_version);
-    if (ret != EOK) {
-        DEBUG(0, ("Failed to identify to the monitor!\n"));
-        return ret;
-    }
-
-    return EOK;
-}
-
 static int sss_dp_init(struct resp_ctx *rctx,
                        struct sbus_interface *intf,
                        const char *cli_name,
@@ -620,7 +587,9 @@ int sss_process_init(TALLOC_CTX *mem_ctx,
         return ret;
     }
 
-    ret = sss_monitor_init(rctx, monitor_intf, svc_name, svc_version);
+    ret = sss_monitor_init(rctx, rctx->ev, monitor_intf,
+                           svc_name, svc_version, rctx,
+                           &rctx->mon_conn);
     if (ret != EOK) {
         DEBUG(0, ("fatal error setting up message bus\n"));
         return ret;
-- 
1.7.5

From f74ba9f84ddd6dedaa0fe8a28f89e6247c3923dc Mon Sep 17 00:00:00 2001
From: Stephen Gallagher <sgall...@redhat.com>
Date: Mon, 2 May 2011 13:46:27 -0400
Subject: [PATCH 3/3] 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 ed83d339dd51f14dc64c05014397aa7cf700e5a3..32aef413f6ba0d10a09a63787003a1b97d1999e5 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

Attachment: signature.asc
Description: This is a digitally signed message part

_______________________________________________
sssd-devel mailing list
sssd-devel@lists.fedorahosted.org
https://fedorahosted.org/mailman/listinfo/sssd-devel

Reply via email to