daemon: Use separate method to set default loggers
authorTobias Brunner <tobias@strongswan.org>
Mon, 23 Jan 2017 16:14:01 +0000 (17:14 +0100)
committerTobias Brunner <tobias@strongswan.org>
Wed, 25 Jan 2017 13:58:09 +0000 (14:58 +0100)
This way it is not necessary to pass the same values to reload the
loggers.

13 files changed:
src/charon-cmd/charon-cmd.c
src/charon-nm/charon-nm.c
src/charon-svc/charon-svc.c
src/charon-systemd/charon-systemd.c
src/charon-tkm/src/charon-tkm.c
src/charon-tkm/tests/tests.c
src/charon/charon.c
src/frontends/android/app/src/main/jni/libandroidbridge/charonservice.c
src/frontends/osx/charon-xpc/charon-xpc.c
src/libcharon/daemon.c
src/libcharon/daemon.h
src/libcharon/tests/libcharon_tests.c
src/libcharon/tests/utils/exchange_test_helper.c

index f350198..7934964 100644 (file)
 static level_t default_loglevel = LEVEL_CTRL;
 
 /**
- * Loglevel configuration
- */
-static level_t levels[DBG_MAX];
-
-/**
  * Connection to initiate
  */
 static cmd_connection_t *conn;
@@ -129,7 +124,7 @@ static int run()
                                         "configuration");
                                if (lib->settings->load_files(lib->settings, lib->conf, FALSE))
                                {
-                                       charon->load_loggers(charon, levels, TRUE);
+                                       charon->load_loggers(charon);
                                        lib->plugins->reload(lib->plugins, NULL);
                                }
                                else
@@ -311,6 +306,7 @@ int main(int argc, char *argv[])
 {
        struct sigaction action;
        struct utsname utsname;
+       level_t levels[DBG_MAX];
        int group;
 
        /* handle simple arguments */
@@ -338,7 +334,8 @@ int main(int argc, char *argv[])
        {
                levels[group] = default_loglevel;
        }
-       charon->load_loggers(charon, levels, TRUE);
+       charon->set_default_loggers(charon, levels, TRUE);
+       charon->load_loggers(charon);
 
        if (!lookup_uid_gid())
        {
index cbbed7a..ca12db8 100644 (file)
@@ -192,7 +192,7 @@ int main(int argc, char *argv[])
        lib->settings->set_int(lib->settings, "charon-nm.syslog.daemon.default",
                lib->settings->get_int(lib->settings,
                                                           "charon-nm.syslog.daemon.default", 1));
-       charon->load_loggers(charon, NULL, FALSE);
+       charon->load_loggers(charon);
 
        /* use random ports to avoid conflicts with regular charon */
        lib->settings->set_int(lib->settings, "charon-nm.port", 0);
index 823b366..7201fae 100644 (file)
@@ -220,7 +220,8 @@ static void init_and_run(DWORD dwArgc, LPTSTR *lpszArgv, int (*wait)())
                        update_status(SERVICE_START_PENDING);
                        if (libcharon_init())
                        {
-                               charon->load_loggers(charon, levels, TRUE);
+                               charon->set_default_loggers(charon, levels, TRUE);
+                               charon->load_loggers(charon);
                                print_version();
                                update_status(SERVICE_START_PENDING);
                                if (charon->initialize(charon, PLUGINS))
index fc93e21..60e509f 100644 (file)
@@ -269,7 +269,7 @@ static int run()
                                         "configuration");
                                if (lib->settings->load_files(lib->settings, lib->conf, FALSE))
                                {
-                                       charon->load_loggers(charon, NULL, FALSE);
+                                       charon->load_loggers(charon);
                                        lib->plugins->reload(lib->plugins, NULL);
                                }
                                else
@@ -390,7 +390,7 @@ int main(int argc, char *argv[])
                sd_notifyf(0, "STATUS=unknown uid/gid");
                return SS_RC_INITIALIZATION_FAILED;
        }
-       charon->load_loggers(charon, NULL, FALSE);
+       charon->load_loggers(charon);
 
        lib->plugins->add_static_features(lib->plugins, lib->ns, features,
                                                        countof(features), TRUE, journal_reload, &journal);
index 4a6d2ae..a4d4d0c 100644 (file)
@@ -275,7 +275,7 @@ int main(int argc, char *argv[])
        lib->settings->set_int(lib->settings, "%s.syslog.daemon.default",
                        lib->settings->get_int(lib->settings, "%s.syslog.daemon.default", 1,
                                                                   dmn_name), dmn_name);
-       charon->load_loggers(charon, NULL, FALSE);
+       charon->load_loggers(charon);
 
        DBG1(DBG_DMN, "Starting charon with TKM backend (strongSwan "VERSION")");
 
index e3cd2d9..3d57599 100644 (file)
@@ -52,7 +52,7 @@ static bool test_runner_init(bool init)
                libcharon_init();
                lib->settings->set_int(lib->settings,
                                                           "test-runner.filelog.stdout.default", 0);
-               charon->load_loggers(charon, NULL, FALSE);
+               charon->load_loggers(charon);
 
                /* Register TKM specific plugins */
                static plugin_feature_t features[] = {
index 116ce7e..520cb3c 100644 (file)
 static FILE *pidfile = NULL;
 
 /**
- * Log levels as defined via command line arguments
- */
-static level_t levels[DBG_MAX];
-
-/**
- * Whether to only use syslog when logging
- */
-static bool use_syslog = FALSE;
-
-/**
  * hook in library for debugging messages
  */
 extern void (*dbg) (debug_t group, level_t level, char *fmt, ...);
@@ -126,7 +116,7 @@ static void run()
                                         "configuration");
                                if (lib->settings->load_files(lib->settings, lib->conf, FALSE))
                                {
-                                       charon->load_loggers(charon, levels, !use_syslog);
+                                       charon->load_loggers(charon);
                                        lib->plugins->reload(lib->plugins, NULL);
                                }
                                else
@@ -289,6 +279,8 @@ int main(int argc, char *argv[])
        struct sigaction action;
        int group, status = SS_RC_INITIALIZATION_FAILED;
        struct utsname utsname;
+       level_t levels[DBG_MAX];
+       bool use_syslog = FALSE;
 
        /* logging for library during initialization, as we have no bus yet */
        dbg = dbg_stderr;
@@ -382,7 +374,8 @@ int main(int argc, char *argv[])
                goto deinit;
        }
 
-       charon->load_loggers(charon, levels, !use_syslog);
+       charon->set_default_loggers(charon, levels, !use_syslog);
+       charon->load_loggers(charon);
 
        if (uname(&utsname) != 0)
        {
index fa97089..b9f6f1d 100644 (file)
@@ -624,7 +624,7 @@ JNI_METHOD(CharonVpnService, initializeCharon, jboolean,
                return FALSE;
        }
 
-       charon->load_loggers(charon, NULL, FALSE);
+       charon->load_loggers(charon);
 
        charonservice_init(env, this, builder, byod);
 
index 14ce2e2..e3063f2 100644 (file)
@@ -42,11 +42,6 @@ void dispatcher_cleanup()
 }
 
 /**
- * Loglevel configuration
- */
-static level_t levels[DBG_MAX];
-
-/**
  * hook in library for debugging messages
  */
 extern void (*dbg) (debug_t group, level_t level, char *fmt, ...);
@@ -154,6 +149,7 @@ int main(int argc, char *argv[])
 {
        struct sigaction action;
        struct utsname utsname;
+       level_t levels[DBG_MAX];
        int group;
 
        dbg = dbg_stderr;
@@ -178,7 +174,8 @@ int main(int argc, char *argv[])
        {
                levels[group] = LEVEL_CTRL;
        }
-       charon->load_loggers(charon, levels, TRUE);
+       charon->set_default_loggers(charon, levels, TRUE);
+       charon->load_loggers(charon);
 
        lib->settings->set_default_str(lib->settings, "charon-xpc.port", "0");
        lib->settings->set_default_str(lib->settings, "charon-xpc.port_nat_t", "0");
index 532d081..eadc10a 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2006-2016 Tobias Brunner
+ * Copyright (C) 2006-2017 Tobias Brunner
  * Copyright (C) 2005-2009 Martin Willi
  * Copyright (C) 2006 Daniel Roethlisberger
  * Copyright (C) 2005 Jan Hutter
@@ -87,6 +87,16 @@ struct private_daemon_t {
        linked_list_t *loggers;
 
        /**
+        * Cached log levels for default loggers
+        */
+       level_t *levels;
+
+       /**
+        * Whether to log to stdout/err by default
+        */
+       bool to_stderr;
+
+       /**
         * Identifier used for syslog (in the openlog call)
         */
        char *syslog_identifier;
@@ -532,7 +542,7 @@ static void load_custom_logger(private_daemon_t *this,
 }
 
 METHOD(daemon_t, load_loggers, void,
-       private_daemon_t *this, level_t levels[DBG_MAX], bool to_stderr)
+       private_daemon_t *this)
 {
        enumerator_t *enumerator;
        linked_list_t *current_loggers;
@@ -564,7 +574,7 @@ METHOD(daemon_t, load_loggers, void,
                load_custom_logger(this, &custom_loggers[i], current_loggers);
        }
 
-       if (!this->loggers->get_count(this->loggers) && levels)
+       if (!this->loggers->get_count(this->loggers) && this->levels)
        {       /* setup legacy style default loggers configured via command-line */
                file_logger_t *file_logger;
                sys_logger_t *sys_logger;
@@ -578,11 +588,11 @@ METHOD(daemon_t, load_loggers, void,
                {
                        if (sys_logger)
                        {
-                               sys_logger->set_level(sys_logger, group, levels[group]);
+                               sys_logger->set_level(sys_logger, group, this->levels[group]);
                        }
-                       if (to_stderr)
+                       if (this->to_stderr)
                        {
-                               file_logger->set_level(file_logger, group, levels[group]);
+                               file_logger->set_level(file_logger, group, this->levels[group]);
                        }
                }
                if (sys_logger)
@@ -604,13 +614,39 @@ METHOD(daemon_t, load_loggers, void,
        this->mutex->unlock(this->mutex);
 }
 
+METHOD(daemon_t, set_default_loggers, void,
+       private_daemon_t *this, level_t levels[DBG_MAX], bool to_stderr)
+{
+       debug_t group;
+
+       this->mutex->lock(this->mutex);
+       if (!levels)
+       {
+               free(this->levels);
+               this->levels = NULL;
+       }
+       else
+       {
+               if (!this->levels)
+               {
+                       this->levels = calloc(sizeof(level_t), DBG_MAX);
+               }
+               for (group = 0; group < DBG_MAX; group++)
+               {
+                       this->levels[group] = levels[group];
+               }
+               this->to_stderr = to_stderr;
+       }
+       this->mutex->unlock(this->mutex);
+}
+
 METHOD(daemon_t, set_level, void,
        private_daemon_t *this, debug_t group, level_t level)
 {
        enumerator_t *enumerator;
        logger_entry_t *entry;
 
-       /* we set the loglevel on ALL sys- and file-loggers */
+       /* we set the loglevel on ALL loggers */
        this->mutex->lock(this->mutex);
        enumerator = this->loggers->create_enumerator(this->loggers);
        while (enumerator->enumerate(enumerator, &entry))
@@ -694,6 +730,7 @@ static void destroy(private_daemon_t *this)
        DESTROY_IF(this->public.bus);
        this->loggers->destroy_function(this->loggers, (void*)logger_entry_destroy);
        this->mutex->destroy(this->mutex);
+       free(this->levels);
        free(this);
 }
 
@@ -879,6 +916,7 @@ private_daemon_t *daemon_create()
                        .initialize = _initialize,
                        .start = _start,
                        .load_loggers = _load_loggers,
+                       .set_default_loggers = _set_default_loggers,
                        .set_level = _set_level,
                        .bus = bus_create(),
                },
index 48b9c7e..3d69105 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2006-2012 Tobias Brunner
+ * Copyright (C) 2006-2017 Tobias Brunner
  * Copyright (C) 2005-2009 Martin Willi
  * Copyright (C) 2006 Daniel Roethlisberger
  * Copyright (C) 2005 Jan Hutter
@@ -338,17 +338,27 @@ struct daemon_t {
        /**
         * Load/Reload loggers defined in strongswan.conf
         *
-        * @param levels        optional debug levels used to create default loggers
-        *                                      if none are defined in strongswan.conf
+        * If none are defined in strongswan.conf default loggers configured via
+        * set_default_loggers() are loaded.
+        */
+       void (*load_loggers)(daemon_t *this);
+
+       /**
+        * Configure default loggers if none are defined in strongswan.conf
+        *
+        * @param levels        debug levels used to create default loggers if none are
+        *                                      defined in strongswan.conf (NULL to disable)
         * @param to_stderr     TRUE to log to stderr/stdout if no loggers are defined
-        *                                      in strongswan.conf
+        *                                      in strongswan.conf (logging to syslog is always enabled)
         */
-       void (*load_loggers)(daemon_t *this, level_t levels[DBG_MAX],
-                                                bool to_stderr);
+       void (*set_default_loggers)(daemon_t *this, level_t levels[DBG_MAX],
+                                                               bool to_stderr);
 
        /**
-        * Set the log level for the given log group for all configured file-,
-        * syslog and custom-loggers.
+        * Set the log level for the given log group for all loaded loggers.
+        *
+        * This change is not persistent and gets reset if loggers are reloaded
+        * via load_loggers().
         *
         * @param group         log group
         * @param level         log level
index 1ef13e9..3fe5b0e 100644 (file)
@@ -45,7 +45,7 @@ static void initialize_logging()
        lib->settings->set_int(lib->settings, "%s.filelog.stderr.default",
                        lib->settings->get_int(lib->settings, "%s.filelog.stderr.default",
                                                                   level, lib->ns), lib->ns);
-       charon->load_loggers(charon, NULL, TRUE);
+       charon->load_loggers(charon);
 }
 
 static bool test_runner_init(bool init)
index f32906d..fce0cce 100644 (file)
@@ -282,7 +282,7 @@ static void initialize_logging()
                                                                   level, lib->ns), lib->ns);
        lib->settings->set_bool(lib->settings, "%s.filelog.stderr.ike_name", TRUE,
                                                        lib->ns);
-       charon->load_loggers(charon, NULL, TRUE);
+       charon->load_loggers(charon);
 }
 
 /**