From 33dfa92dd4e2b68dda0237766d9e28304063e8aa Mon Sep 17 00:00:00 2001
From: Stephen Gallagher <sgallagh@redhat.com>
Date: Wed, 27 Apr 2011 15:28:07 -0400
Subject: [PATCH] Override config file debug_level with command-line

This patch also makes the following changes:
1) The [sssd] debug_level setting no longer acts as a default for
   all other sections.
2) We will now skip passing the debug argument to the child
   processes from the master unless the SSSD was run with a
   command-line argument for the debug level.

https://fedorahosted.org/sssd/ticket/764
---
 src/monitor/monitor.c |   63 ++++++++++++++++++++++++++++++++++++++----------
 src/util/debug.c      |    3 +-
 src/util/server.c     |   19 ++++++++------
 src/util/util.h       |    3 ++
 4 files changed, 66 insertions(+), 22 deletions(-)

diff --git a/src/monitor/monitor.c b/src/monitor/monitor.c
index e46125baff46bb790b4505a5b53f86da4169b98c..194e74c5d2119cdbf693363ebe4777d0f5c130f9 100644
--- a/src/monitor/monitor.c
+++ b/src/monitor/monitor.c
@@ -57,6 +57,8 @@
  * monitor will get crazy hammering children with messages */
 #define MONITOR_DEF_PING_TIME 10
 
+int cmdline_debug_level;
+
 struct svc_spy;
 
 struct mt_svc {
@@ -942,13 +944,30 @@ static int get_service_config(struct mt_ctx *ctx, const char *name,
     }
 
     if (!svc->command) {
-        svc->command = talloc_asprintf(svc, "%s/sssd_%s -d %d%s%s",
-                                       SSSD_LIBEXEC_PATH,
-                                       svc->name, debug_level,
-                                       (debug_timestamps?
-                                              "": " --debug-timestamps=0"),
-                                       (debug_to_file ?
-                                              " --debug-to-files":""));
+        if (cmdline_debug_level == SSS_UNRESOLVED_DEBUG_LEVEL) {
+            svc->command = talloc_asprintf(svc, "%s/sssd_%s %s%s",
+                                           SSSD_LIBEXEC_PATH,
+                                           svc->name,
+                                           (debug_timestamps?
+                                                  "": " --debug-timestamps=0"),
+                                           (debug_to_file ?
+                                                  " --debug-to-files":""));
+        } else {
+            /* If the debug level was specified at the command-line,
+             * make sure to pass it into the children, overriding the
+             * config file.
+             */
+            svc->command = talloc_asprintf(svc, "%s/sssd_%s -d %d%s%s",
+                                           SSSD_LIBEXEC_PATH,
+                                           svc->name,
+                                           cmdline_debug_level,
+                                           debug_timestamps ?
+                                               "" :
+                                               " --debug-timestamps=0",
+                                           debug_to_file ?
+                                               " --debug-to-files" :
+                                               "");
+        }
         if (!svc->command) {
             talloc_free(svc);
             return ENOMEM;
@@ -1071,12 +1090,25 @@ static int get_provider_config(struct mt_ctx *ctx, const char *name,
 
     /* if there are no custom commands, build a default one */
     if (!svc->command) {
-        svc->command = talloc_asprintf(svc,
-                            "%s/sssd_be -d %d%s%s --domain %s",
-                            SSSD_LIBEXEC_PATH, debug_level,
-                            (debug_timestamps?"": " --debug-timestamps=0"),
-                            (debug_to_file?" --debug-to-files":""),
-                            svc->name);
+        if (cmdline_debug_level == SSS_UNRESOLVED_DEBUG_LEVEL) {
+            svc->command = talloc_asprintf(svc,
+                                "%s/sssd_be --domain %s%s%s",
+                                SSSD_LIBEXEC_PATH,
+                                svc->name,
+                                debug_timestamps ? ""
+                                                 : " --debug-timestamps=0",
+                                debug_to_file ? " --debug-to-files" : "");
+        } else {
+            svc->command = talloc_asprintf(svc,
+                                "%s/sssd_be --domain %s -d %d%s%s ",
+                                SSSD_LIBEXEC_PATH,
+                                svc->name,
+                                cmdline_debug_level,
+                                debug_timestamps ? ""
+                                                 : " --debug-timestamps=0",
+                                debug_to_file ? " --debug-to-files" : "");
+        }
+
         if (!svc->command) {
             talloc_free(svc);
             return ENOMEM;
@@ -2261,6 +2293,11 @@ int main(int argc, const char *argv[])
         }
     }
 
+    /* If the level was passed at the command-line, we want
+     * to save it and pass it to the children later.
+     */
+    cmdline_debug_level = debug_level;
+
     if (opt_daemon && opt_interactive) {
         fprintf(stderr, "Option -i|--interactive is not allowed together with -D|--daemon\n");
         poptPrintUsage(pc, stderr, 0);
diff --git a/src/util/debug.c b/src/util/debug.c
index 1b78ebaeff002fadb8b1e85f912452a20891e2ce..129b9d9b1c7f588998060ee76f9b05c51e6446cf 100644
--- a/src/util/debug.c
+++ b/src/util/debug.c
@@ -31,7 +31,8 @@
 #include "util/util.h"
 
 const char *debug_prg_name = "sssd";
-int debug_level = 0;
+
+int debug_level = SSS_UNRESOLVED_DEBUG_LEVEL;
 int debug_timestamps = 1;
 
 int debug_to_file = 0;
diff --git a/src/util/server.c b/src/util/server.c
index 1e8b148d74c36ff8b66400d43ddd58e3aa71b2a5..977c751179f487733ef61ea0592cbc6c483279e3 100644
--- a/src/util/server.c
+++ b/src/util/server.c
@@ -444,14 +444,17 @@ int server_setup(const char *name, int flags,
         return ret;
     }
 
-    /* set debug level if any in conf_entry */
-    ret = confdb_get_int(ctx->confdb_ctx, ctx, conf_entry,
-                         CONFDB_SERVICE_DEBUG_LEVEL,
-                         debug_level, &debug_level);
-    if (ret != EOK) {
-        DEBUG(0, ("Error reading from confdb (%d) [%s]\n",
-                  ret, strerror(ret)));
-        return ret;
+    if (debug_level == SSS_UNRESOLVED_DEBUG_LEVEL) {
+        /* set debug level if any in conf_entry */
+        ret = confdb_get_int(ctx->confdb_ctx, ctx, conf_entry,
+                             CONFDB_SERVICE_DEBUG_LEVEL,
+                             SSS_DEFAULT_DEBUG_LEVEL,
+                             &debug_level);
+        if (ret != EOK) {
+            DEBUG(0, ("Error reading from confdb (%d) [%s]\n",
+                      ret, strerror(ret)));
+            return ret;
+        }
     }
 
     /* same for debug timestamps */
diff --git a/src/util/util.h b/src/util/util.h
index 61fe7f6c24c4c1673baf4789f4d76ae76eb25970..ac6704ae38e80bcc08f5d173ad0cdcf4bfb9e882 100644
--- a/src/util/util.h
+++ b/src/util/util.h
@@ -60,6 +60,9 @@ extern const char *debug_log_file;
 void debug_fn(const char *format, ...);
 errno_t set_debug_file_from_fd(const int fd);
 
+#define SSS_DEFAULT_DEBUG_LEVEL 0
+#define SSS_UNRESOLVED_DEBUG_LEVEL -1
+
 #define SSSD_DEBUG_OPTS \
         {"debug-level", 'd', POPT_ARG_INT, &debug_level, 0, \
          _("Debug level"), NULL}, \
-- 
1.7.4.4

