diff --git a/daemon/call.c b/daemon/call.c index 8f10fdf1e..e92214d7b 100644 --- a/daemon/call.c +++ b/daemon/call.c @@ -337,7 +337,7 @@ retry: for (i = 0; i < 100; i++) close(i); - if (!ilog_stderr) { + if (!rtpe_config.common.log_stderr) { openlog("rtpengine/child", LOG_PID | LOG_NDELAY, LOG_DAEMON); } ilog(LOG_INFO, "Initiating XMLRPC call for tag "STR_FORMAT"", STR_FMT(tag)); diff --git a/daemon/cli.c b/daemon/cli.c index 2678add3a..1af68c888 100644 --- a/daemon/cli.c +++ b/daemon/cli.c @@ -811,7 +811,7 @@ fail: } static void cli_incoming_list_loglevel(str *instr, struct streambuf *replybuffer) { - streambuf_printf(replybuffer, "%i\n", g_atomic_int_get(&log_level)); + streambuf_printf(replybuffer, "%i\n", get_log_level()); } static void cli_incoming_set_loglevel(str *instr, struct streambuf *replybuffer) { int nl; @@ -828,6 +828,6 @@ static void cli_incoming_set_loglevel(str *instr, struct streambuf *replybuffer) return; } - g_atomic_int_set(&log_level, nl); + g_atomic_int_set(&rtpe_config.common.log_level, nl); streambuf_printf(replybuffer, "Success setting loglevel to %i\n", nl); } diff --git a/daemon/main.c b/daemon/main.c index b45404f4b..dc164b881 100644 --- a/daemon/main.c +++ b/daemon/main.c @@ -92,14 +92,14 @@ static void sighandler(gpointer x) { rtpe_shutdown = 1; else if (ret == SIGUSR1) { if (get_log_level() > 0) { - g_atomic_int_add(&log_level, -1); + g_atomic_int_add(&rtpe_config.common.log_level, -1); ilog(get_log_level(), "Set log level to %d\n", get_log_level()); } } else if (ret == SIGUSR2) { if (get_log_level() < 7) { - g_atomic_int_add(&log_level, 1); + g_atomic_int_add(&rtpe_config.common.log_level, 1); ilog(get_log_level(), "Set log level to %d\n", get_log_level()); } @@ -289,7 +289,7 @@ static void options(int *argc, char ***argv) { }; config_load(argc, argv, e, " - next-generation media proxy", - "/etc/rtpengine/rtpengine.conf", "rtpengine"); + "/etc/rtpengine/rtpengine.conf", "rtpengine", &rtpe_config.common); if (!if_a) die("Missing option --interface"); @@ -384,9 +384,6 @@ static void options(int *argc, char ***argv) { if (rtpe_config.fmt > 1) die("Invalid XMLRPC format"); - if ((log_level < LOG_EMERG) || (log_level > LOG_DEBUG)) - die("Invalid log level (--log_level)"); - if (log_facility_cdr_s) { if (!parse_log_facility(log_facility_cdr_s, &_log_facility_cdr)) { print_available_log_facilities(); @@ -479,8 +476,6 @@ static void init_everything() { if (call_interfaces_init()) abort(); statistics_init(); - if (call_init()) - abort(); } @@ -510,6 +505,9 @@ no_kernel: dtls_timer(rtpe_poller); + if (call_init()) + abort(); + rwlock_init(&rtpe_config.config_lock); if (rtpe_config.max_sessions < -1) { rtpe_config.max_sessions = -1; diff --git a/daemon/main.h b/daemon/main.h index b939e9dc0..9cfd07f73 100644 --- a/daemon/main.h +++ b/daemon/main.h @@ -5,6 +5,7 @@ #include "aux.h" #include #include "socket.h" +#include "auxlib.h" enum xmlrpc_format { XF_SEMS = 0, @@ -14,6 +15,9 @@ enum xmlrpc_format { struct rtpengine_config { /* everything below protected by config_lock */ rwlock_t config_lock; + + struct rtpengine_common_config common; + int kernel_table; int max_sessions; int timeout; diff --git a/daemon/redis.c b/daemon/redis.c index 5eeecb5e7..8e2dd14bb 100644 --- a/daemon/redis.c +++ b/daemon/redis.c @@ -1594,7 +1594,7 @@ int redis_restore(struct redis *r) { if (!r) return 0; - log_level |= LOG_FLAG_RESTORE; + rtpe_config.common.log_level |= LOG_FLAG_RESTORE; rlog(LOG_DEBUG, "Restoring calls from Redis..."); @@ -1637,7 +1637,7 @@ int redis_restore(struct redis *r) { freeReplyObject(calls); err: - log_level &= ~LOG_FLAG_RESTORE; + rtpe_config.common.log_level &= ~LOG_FLAG_RESTORE; return ret; } diff --git a/lib/auxlib.c b/lib/auxlib.c index 8f24fb0fb..9c724176b 100644 --- a/lib/auxlib.c +++ b/lib/auxlib.c @@ -7,18 +7,15 @@ #include #include #include "log.h" +#include "loglib.h" -static const char *config_file; -static const char *config_section; -static const char *pid_file; -static const char *log_facility; -static int foreground; static int version; +struct rtpengine_common_config *rtpe_common_config_ptr; void daemonize(void) { - if (foreground) + if (rtpe_common_config_ptr->foreground) return; if (fork()) _exit(0); @@ -32,10 +29,10 @@ void daemonize(void) { void wpidfile() { FILE *fp; - if (!pid_file) + if (!rtpe_common_config_ptr->pidfile) return; - fp = fopen(pid_file, "w"); + fp = fopen(rtpe_common_config_ptr->pidfile, "w"); if (!fp) { ilog(LOG_CRIT, "Failed to create PID file (%s), aborting startup", strerror(errno)); exit(-1); @@ -54,22 +51,10 @@ static unsigned int options_length(const GOptionEntry *arr) { } -static const GOptionEntry shared_options[] = { - { "version", 'v', 0, G_OPTION_ARG_NONE, &version, "Print build time and exit", NULL }, - { "config-file", 0, 0, G_OPTION_ARG_STRING, &config_file, "Load config from this file", "FILE" }, - { "config-section", 0, 0, G_OPTION_ARG_STRING, &config_section,"Config file section to use", "STRING" }, - { "log-facility", 0, 0, G_OPTION_ARG_STRING, &log_facility, "Syslog facility to use for logging", "daemon|local0|...|local7"}, - { "log-level", 'L', 0, G_OPTION_ARG_INT, (void *)&log_level,"Mask log priorities above this level","INT" }, - { "log-stderr", 'E', 0, G_OPTION_ARG_NONE, &ilog_stderr, "Log on stderr instead of syslog", NULL }, - { "pidfile", 'p', 0, G_OPTION_ARG_FILENAME, &pid_file, "Write PID to file", "FILE" }, - { "foreground", 'f', 0, G_OPTION_ARG_NONE, &foreground, "Don't fork to background", NULL }, - { NULL, } -}; - #define CONF_OPTION_GLUE(get_func, data_type, ...) \ { \ data_type *varptr = e->arg_data; \ - data_type var = g_key_file_get_ ## get_func(kf, config_section, e->long_name, \ + data_type var = g_key_file_get_ ## get_func(kf, rtpe_common_config_ptr->config_section, e->long_name, \ ##__VA_ARGS__, &er); \ if (er && g_error_matches(er, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_KEY_NOT_FOUND)) { \ g_error_free(er); \ @@ -83,7 +68,8 @@ static const GOptionEntry shared_options[] = { } void config_load(int *argc, char ***argv, GOptionEntry *app_entries, const char *description, - const char *default_config, const char *default_section) + char *default_config, char *default_section, + struct rtpengine_common_config *cconfig) { GOptionContext *c; GError *er = NULL; @@ -92,6 +78,28 @@ void config_load(int *argc, char ***argv, GOptionEntry *app_entries, const char int saved_argc = *argc; char **saved_argv = g_strdupv(*argv); + rtpe_common_config_ptr = cconfig; + + // defaults +#ifndef __DEBUG + rtpe_common_config_ptr->log_level = LOG_INFO; +#else + rtpe_common_config_ptr->log_level = LOG_DEBUG; +#endif + + GOptionEntry shared_options[] = { + { "version", 'v', 0, G_OPTION_ARG_NONE, &version, "Print build time and exit", NULL }, + { "config-file", 0, 0, G_OPTION_ARG_STRING, &rtpe_common_config_ptr->config_file, "Load config from this file", "FILE" }, + { "config-section", 0, 0, G_OPTION_ARG_STRING, &rtpe_common_config_ptr->config_section,"Config file section to use", "STRING" }, + { "log-facility", 0, 0, G_OPTION_ARG_STRING, &rtpe_common_config_ptr->log_facility, "Syslog facility to use for logging", "daemon|local0|...|local7"}, + { "log-level", 'L', 0, G_OPTION_ARG_INT, (void *)&rtpe_common_config_ptr->log_level,"Mask log priorities above this level","INT" }, + { "log-stderr", 'E', 0, G_OPTION_ARG_NONE, &rtpe_common_config_ptr->log_stderr, "Log on stderr instead of syslog", NULL }, + { "pidfile", 'p', 0, G_OPTION_ARG_FILENAME, &rtpe_common_config_ptr->pidfile, "Write PID to file", "FILE" }, + { "foreground", 'f', 0, G_OPTION_ARG_NONE, &rtpe_common_config_ptr->foreground, "Don't fork to background", NULL }, + { NULL, } + }; + + // prepend shared CLI options unsigned int shared_len = options_length(shared_options); unsigned int app_len = options_length(app_entries); @@ -99,8 +107,8 @@ void config_load(int *argc, char ***argv, GOptionEntry *app_entries, const char memcpy(entries, shared_options, sizeof(*entries) * shared_len); memcpy(&entries[shared_len], app_entries, sizeof(*entries) * (app_len + 1)); - if (!config_section) - config_section = default_section; + if (!rtpe_common_config_ptr->config_section) + rtpe_common_config_ptr->config_section = default_section; c = g_option_context_new(description); g_option_context_add_main_entries(c, entries, NULL); @@ -109,8 +117,8 @@ void config_load(int *argc, char ***argv, GOptionEntry *app_entries, const char // is there a config file to load? use_config = default_config; - if (config_file) { - use_config = config_file; + if (rtpe_common_config_ptr->config_file) { + use_config = rtpe_common_config_ptr->config_file; fatal = 1; } @@ -153,14 +161,17 @@ out: } - if (log_facility) { - if (!parse_log_facility(log_facility, &ilog_facility)) { + if (rtpe_common_config_ptr->log_facility) { + if (!parse_log_facility(rtpe_common_config_ptr->log_facility, &ilog_facility)) { print_available_log_facilities(); - die ("Invalid log facility '%s' (--log-facility)", log_facility); + die ("Invalid log facility '%s' (--log-facility)", rtpe_common_config_ptr->log_facility); } } - if (ilog_stderr) { + if ((rtpe_common_config_ptr->log_level < LOG_EMERG) || (rtpe_common_config_ptr->log_level > LOG_DEBUG)) + die("Invalid log level (--log_level)"); + + if (rtpe_common_config_ptr->log_stderr) { write_log = log_to_stderr; max_log_line_length = 0; } diff --git a/lib/auxlib.h b/lib/auxlib.h index 3e29c7223..eae9ef72f 100644 --- a/lib/auxlib.h +++ b/lib/auxlib.h @@ -3,10 +3,23 @@ #include +struct rtpengine_common_config { + char *config_file; + char *config_section; + char *log_facility; + volatile int log_level; + int log_stderr; + char *pidfile; + int foreground; +}; + +extern struct rtpengine_common_config *rtpe_common_config_ptr; + void daemonize(void); void wpidfile(void); void config_load(int *argc, char ***argv, GOptionEntry *entries, const char *description, - const char *default_config, const char *default_section); + char *default_config, char *default_section, + struct rtpengine_common_config *); #endif diff --git a/lib/loglib.c b/lib/loglib.c index 2f80e4f41..2837567e9 100644 --- a/lib/loglib.c +++ b/lib/loglib.c @@ -8,6 +8,7 @@ #include #include #include +#include "auxlib.h" struct log_limiter_entry { @@ -22,14 +23,6 @@ typedef struct _fac_code { -#ifndef __DEBUG -volatile gint log_level = LOG_INFO; -#else -volatile gint log_level = LOG_DEBUG; -#endif - - - static write_log_t log_both; unsigned int max_log_line_length = 500; @@ -73,7 +66,6 @@ static const char* const prio_str[] = { "DEBUG" }; -gboolean ilog_stderr = 0; int ilog_facility = LOG_DAEMON; @@ -230,7 +222,7 @@ void log_init(const char *handle) { __log_limiter = g_hash_table_new(log_limiter_entry_hash, log_limiter_entry_equal); __log_limiter_strings = g_string_chunk_new(1024); - if (!ilog_stderr) + if (!rtpe_common_config_ptr->log_stderr) openlog(handle, LOG_PID | LOG_NDELAY, ilog_facility); } diff --git a/lib/loglib.h b/lib/loglib.h index 72aeffd67..04ebbea7b 100644 --- a/lib/loglib.h +++ b/lib/loglib.h @@ -6,13 +6,11 @@ #include #include #include "compat.h" +#include "auxlib.h" -extern gboolean ilog_stderr; extern int ilog_facility; - -extern volatile gint log_level; extern unsigned int max_log_line_length; @@ -46,7 +44,7 @@ void __ilog_np(int prio, const char *format, ...) __attribute__ ((format (printf INLINE int get_log_level(void) { - return g_atomic_int_get(&log_level); + return g_atomic_int_get(&rtpe_common_config_ptr->log_level); } diff --git a/recording-daemon/main.c b/recording-daemon/main.c index 4a18a1f26..55bfd610d 100644 --- a/recording-daemon/main.c +++ b/recording-daemon/main.c @@ -45,6 +45,9 @@ static GQueue threads = G_QUEUE_INIT; // only accessed from main thread volatile int shutdown_flag; +struct rtpengine_common_config rtpe_common_config; + + static void signals(void) { sigset_t ss; @@ -186,7 +189,7 @@ static void options(int *argc, char ***argv) { }; config_load(argc, argv, e, " - rtpengine recording daemon", - "/etc/rtpengine/rtpengine-recording.conf", "rtpengine-recording"); + "/etc/rtpengine/rtpengine-recording.conf", "rtpengine-recording", &rtpe_common_config); if (!strcmp(output_format, "none")) { output_enabled = 0; diff --git a/recording-daemon/main.h b/recording-daemon/main.h index bf33b8309..f11b39105 100644 --- a/recording-daemon/main.h +++ b/recording-daemon/main.h @@ -2,6 +2,9 @@ #define _MAIN_H_ +#include "auxlib.h" + + extern int ktable; extern int num_threads; extern const char *spool_dir; @@ -19,4 +22,7 @@ extern const char *forward_to; extern volatile int shutdown_flag; +extern struct rtpengine_common_config rtpe_common_config; + + #endif