From 24bed47794fab3af54f9fc7966e957f285423140 Mon Sep 17 00:00:00 2001 From: Johan Wikman Date: Tue, 10 Nov 2015 10:17:42 +0200 Subject: [PATCH] Syslog ident must be provided explicitly. The syslog ident must be provided explicitly when calling skygw_logmanager_init (and not provided via the argv array). It can be NULL, in which case it automatically will be the program name. The openlog() call is now always made, irrespective of what the value of the global syslog flag is. That way it will be possible to turn syslog logging on or off after the fact. --- log_manager/log_manager.cc | 70 ++++++------------- log_manager/log_manager.h | 5 +- log_manager/test/testlog.c | 40 +++++------ log_manager/test/testorder.c | 2 +- server/core/gateway.c | 2 +- server/core/maxkeys.c | 2 +- server/core/maxpasswd.c | 2 +- server/include/test_utils.h | 2 +- server/modules/filter/test/harness_common.c | 4 +- .../modules/routing/binlog/maxbinlogcheck.c | 2 +- .../modules/routing/binlog/test/testbinlog.c | 2 +- 11 files changed, 56 insertions(+), 77 deletions(-) diff --git a/log_manager/log_manager.cc b/log_manager/log_manager.cc index 72af88911..79f954074 100644 --- a/log_manager/log_manager.cc +++ b/log_manager/log_manager.cc @@ -91,8 +91,7 @@ ssize_t log_ses_count[LOGFILE_LAST] = {0}; const char* shm_pathname_prefix = "/dev/shm/"; /** Errors are written to syslog too by default */ -char* syslog_id_str = strdup("LOGFILE_ERROR"); -char* syslog_ident_str = NULL; +char* syslog_id_str = strdup("LOGFILE_ERROR"); /** Forward declarations */ @@ -273,7 +272,10 @@ static void* thr_filewriter_fun(void* data); static logfile_t* logmanager_get_logfile(logmanager_t* lm); static bool logmanager_register(bool writep); static void logmanager_unregister(void); -static bool logmanager_init_nomutex(const char* logdir, log_target_t target, int argc, char* argv[]); +static bool logmanager_init_nomutex(const char* ident, + const char* logdir, + log_target_t target, + int argc, char* argv[]); static void logmanager_done_nomutex(void); static bool logmanager_is_valid_id(logfile_id_t id); @@ -304,7 +306,10 @@ static int find_last_seqno(strpart_t* parts, int seqno, int seqnoidx); void flushall_logfiles(bool flush); bool thr_flushall_check(); -static bool logmanager_init_nomutex(const char* logdir, log_target_t target, int argc, char* argv[]) +static bool logmanager_init_nomutex(const char* ident, + const char* logdir, + log_target_t target, + int argc, char* argv[]) { fnames_conf_t* fn; filewriter_t* fw; @@ -345,11 +350,9 @@ static bool logmanager_init_nomutex(const char* logdir, log_target_t target, int fn->fn_state = UNINIT; fw->fwr_state = UNINIT; - if (!do_syslog) - { - free(syslog_id_str); - syslog_id_str = NULL; - } + // The openlog call is always made, but a connection to the system logger will + // not be made until a call to syslog is made. + openlog(ident, LOG_PID | LOG_ODELAY, LOG_USER); /** Initialize configuration including log file naming info */ if (!fnames_conf_init(fn, logdir, argc, argv)) @@ -414,6 +417,7 @@ return_succp: /** * Initializes log managing routines in MariaDB Corporation MaxScale. * + * @param ident The syslog ident. If NULL, then the program name is used. * @param logdir The directory for the log file. If NULL logging will be made to stdout. * @param target Whether the log should be written to filesystem or shared memory. * Meaningless if logdir is NULL. @@ -423,7 +427,10 @@ return_succp: * @return true if succeed, otherwise false * */ -bool skygw_logmanager_init(const char* logdir, log_target_t target, int argc, char* argv[]) +bool skygw_logmanager_init(const char* ident, + const char* logdir, + log_target_t target, + int argc, char* argv[]) { bool succp = false; @@ -435,7 +442,7 @@ bool skygw_logmanager_init(const char* logdir, log_target_t target, int argc, ch goto return_succp; } - succp = logmanager_init_nomutex(logdir, target, argc, argv); + succp = logmanager_init_nomutex(ident, logdir, target, argc, argv); return_succp: release_lock(&lmlock); @@ -474,10 +481,8 @@ static void logmanager_done_nomutex(void) /** Release logfile memory */ logfile_done(lf); - if (syslog_id_str) - { - closelog(); - } + closelog(); + /** Release messages and finally logmanager memory */ fnames_conf_done(&lm->lm_fnames_conf); skygw_message_done(lm->lm_clientmes); @@ -1623,7 +1628,7 @@ static bool logmanager_register(bool writep) // If someone is logging before the log manager has been inited, // or after the log manager has been finished, the messages are // written to stdout. - succp = logmanager_init_nomutex(NULL, LOG_TARGET_DEFAULT, 0, NULL); + succp = logmanager_init_nomutex(NULL, NULL, LOG_TARGET_DEFAULT, 0, NULL); } } /** if logmanager existed or was succesfully restarted, increase link */ @@ -1689,8 +1694,7 @@ static bool fnames_conf_init(fnames_conf_t* fn, bool succp = false; const char* argstr = "-h - help\n" - "-l .......(no default)\n" - "-m ............(argv[0])\n"; + "-l .......(no default)\n"; /** * When init_started is set, clean must be done for it. @@ -1702,7 +1706,7 @@ static bool fnames_conf_init(fnames_conf_t* fn, #endif optind = 1; /**fn_logpath = strdup("/tmp"); // Not used. } - /** Set identity string for syslog if it is not set in config.*/ - if (do_syslog) - { - syslog_ident_str = - (syslog_ident_str == NULL ? - (argv == NULL ? strdup(program_invocation_short_name) : strdup(*argv)) : - syslog_ident_str); - } - /* ss_dfprintf(stderr, "\n\n\tCommand line : "); - for (i = 0; i < argc; i++) - { - ss_dfprintf(stderr, "%s ", argv[i]); - } - ss_dfprintf(stderr, "\n");*/ #if defined(NOT_USED) fprintf(stderr, "Log :\t%s/%s1%s\n\n", @@ -1804,12 +1786,6 @@ static bool logfiles_init(logmanager_t* lm) bool store_shmem = (lm->lm_target == LOG_TARGET_SHMEM); bool write_syslog; - /** Open syslog immediately. Print pid of loggind process. */ - if (syslog_id_str != NULL) - { - openlog(syslog_ident_str, LOG_PID | LOG_NDELAY, LOG_USER); - } - /** * Check if file is also written to syslog. */ diff --git a/log_manager/log_manager.h b/log_manager/log_manager.h index ec75eb81c..2318f7ae8 100644 --- a/log_manager/log_manager.h +++ b/log_manager/log_manager.h @@ -151,7 +151,10 @@ int mxs_log_rotate(); int mxs_log_enable_priority(int priority); int mxs_log_disable_priority(int priority); -bool skygw_logmanager_init(const char* logdir, log_target_t target, int argc, char* argv[]); +bool skygw_logmanager_init(const char* ident, + const char* logdir, + log_target_t target, + int argc, char* argv[]); void skygw_logmanager_done(void); void skygw_logmanager_exit(void); diff --git a/log_manager/test/testlog.c b/log_manager/test/testlog.c index 496e25aa2..152d3b082 100644 --- a/log_manager/test/testlog.c +++ b/log_manager/test/testlog.c @@ -122,7 +122,7 @@ int main(int argc, char* argv[]) fprintf(stderr, "Couldn't register exit function.\n"); } - succp = skygw_logmanager_init("/tmp", LOG_TARGET_FS, log_argc, log_argv); + succp = skygw_logmanager_init(NULL, "/tmp", LOG_TARGET_FS, log_argc, log_argv); if (!succp) { @@ -141,7 +141,7 @@ int main(int argc, char* argv[]) tm.tm_min, tm.tm_sec); - skygw_logmanager_init("/tmp", LOG_TARGET_FS, log_argc, log_argv); + skygw_logmanager_init(NULL, "/tmp", LOG_TARGET_FS, log_argc, log_argv); logstr = ("First write with flush."); err = skygw_log_write_flush(LOGFILE_ERROR, logstr); @@ -189,7 +189,7 @@ int main(int argc, char* argv[]) logstr = "Ph%dlip."; err = skygw_log_write(LOGFILE_TRACE, logstr, 1); - skygw_logmanager_init("/tmp", LOG_TARGET_FS, log_argc, log_argv); + skygw_logmanager_init(NULL, "/tmp", LOG_TARGET_FS, log_argc, log_argv); logstr = ("A terrible error has occurred!"); err = skygw_log_write_flush(LOGFILE_ERROR, logstr); @@ -335,7 +335,7 @@ int main(int argc, char* argv[]) #if !defined(SS_DEBUG) skygw_log_enable(LOGFILE_TRACE); #endif - succp = skygw_logmanager_init("/tmp", LOG_TARGET_FS, log_argc, log_argv); + succp = skygw_logmanager_init(NULL, "/tmp", LOG_TARGET_FS, log_argc, log_argv); ss_dassert(succp); logstr = ("\tTEST 3 - test enabling and disabling logs."); @@ -400,7 +400,7 @@ int main(int argc, char* argv[]) #endif /* TEST 3 */ #if defined(TEST4) - succp = skygw_logmanager_init("/tmp", LOG_TARGET_FS, log_argc, log_argv); + succp = skygw_logmanager_init(NULL, "/tmp", LOG_TARGET_FS, log_argc, log_argv); ss_dassert(succp); #if !defined(SS_DEBUG) skygw_log_enable(LOGFILE_TRACE); @@ -436,7 +436,7 @@ int main(int argc, char* argv[]) skygw_logmanager_done(); - succp = skygw_logmanager_init("/tmp", LOG_TARGET_FS, log_argc, log_argv); + succp = skygw_logmanager_init(NULL, "/tmp", LOG_TARGET_FS, log_argc, log_argv); ss_dassert(succp); #if !defined(SS_DEBUG) skygw_log_enable(LOGFILE_TRACE); @@ -509,7 +509,7 @@ static void* thr_run(void* data) char* logstr; int err; - skygw_logmanager_init("/tmp", LOG_TARGET_FS, 0, NULL); + skygw_logmanager_init(NULL, "/tmp", LOG_TARGET_FS, 0, NULL); skygw_log_flush(LOGFILE_MESSAGE); logstr = ("Hi, how are you?"); err = skygw_log_write(LOGFILE_MESSAGE, logstr); @@ -533,7 +533,7 @@ static void* thr_run(void* data) } ss_dassert(err == 0); err = skygw_log_write(LOGFILE_MESSAGE, logstr); - skygw_logmanager_init("/tmp", LOG_TARGET_FS, 0, NULL); + skygw_logmanager_init(NULL, "/tmp", LOG_TARGET_FS, 0, NULL); logstr = ("Testing. One, two, three\n"); err = skygw_log_write(LOGFILE_ERROR, logstr); if (err != 0) @@ -541,8 +541,8 @@ static void* thr_run(void* data) TEST_ERROR("Error, log write failed."); } ss_dassert(err == 0); - skygw_logmanager_init("/tmp", LOG_TARGET_FS, 0, NULL); - skygw_logmanager_init("/tmp", LOG_TARGET_FS, 0, NULL); + skygw_logmanager_init(NULL, "/tmp", LOG_TARGET_FS, 0, NULL); + skygw_logmanager_init(NULL, "/tmp", LOG_TARGET_FS, 0, NULL); skygw_log_flush(LOGFILE_ERROR); logstr = ("For automatic and register variables, it is done each time the function or block is entered."); @@ -556,7 +556,7 @@ static void* thr_run(void* data) } ss_dassert(err == 0); skygw_logmanager_done(); - skygw_logmanager_init("/tmp", LOG_TARGET_FS, 0, NULL); + skygw_logmanager_init(NULL, "/tmp", LOG_TARGET_FS, 0, NULL); logstr = ("Rather more surprising, at least at first sight, is the fact that a reference " "to a[i] can also be written as *(a+i). In evaluating a[i], C converts it to *(a+i) " "immediately; the two forms are equivalent. Applying the operatos & to both parts " @@ -568,11 +568,11 @@ static void* thr_run(void* data) TEST_ERROR("Error, log write failed."); } ss_dassert(err == 0); - skygw_logmanager_init("/tmp", LOG_TARGET_FS, 0, NULL); + skygw_logmanager_init(NULL, "/tmp", LOG_TARGET_FS, 0, NULL); skygw_logmanager_done(); skygw_log_flush(LOGFILE_ERROR); skygw_logmanager_done(); - skygw_logmanager_init("/tmp", LOG_TARGET_FS, 0, NULL); + skygw_logmanager_init(NULL, "/tmp", LOG_TARGET_FS, 0, NULL); logstr = ("..and you?"); err = skygw_log_write(LOGFILE_MESSAGE, logstr); if (err != 0) @@ -581,7 +581,7 @@ static void* thr_run(void* data) } ss_dassert(err == 0); skygw_logmanager_done(); - skygw_logmanager_init("/tmp", LOG_TARGET_FS, 0, NULL); + skygw_logmanager_init(NULL, "/tmp", LOG_TARGET_FS, 0, NULL); logstr = ("For automatic and register variables, it is done each time the function or block is entered."); #if !defined(SS_DEBUG) skygw_log_enable(LOGFILE_TRACE); @@ -592,7 +592,7 @@ static void* thr_run(void* data) TEST_ERROR("Error, log write failed."); } ss_dassert(err == 0); - skygw_logmanager_init("/tmp", LOG_TARGET_FS, 0, NULL); + skygw_logmanager_init(NULL, "/tmp", LOG_TARGET_FS, 0, NULL); logstr = ("Rather more surprising, at least at first sight, is the fact that a reference to " "a[i] can also be written as *(a+i). In evaluating a[i], C converts it to *(a+i) " "immediately; the two forms are equivalent. Applying the operatos & to both parts " @@ -604,7 +604,7 @@ static void* thr_run(void* data) TEST_ERROR("Error, log write failed."); } ss_dassert(err == 0); - skygw_logmanager_init("/tmp", LOG_TARGET_FS, 0, NULL); + skygw_logmanager_init(NULL, "/tmp", LOG_TARGET_FS, 0, NULL); logstr = ("..... and you too?"); err = skygw_log_write(LOGFILE_MESSAGE, logstr); if (err != 0) @@ -613,7 +613,7 @@ static void* thr_run(void* data) } ss_dassert(err == 0); skygw_logmanager_done(); - skygw_logmanager_init("/tmp", LOG_TARGET_FS, 0, NULL); + skygw_logmanager_init(NULL, "/tmp", LOG_TARGET_FS, 0, NULL); #if !defined(SS_DEBUG) skygw_log_enable(LOGFILE_TRACE); #endif @@ -629,7 +629,7 @@ static void* thr_run(void* data) } ss_dassert(err == 0); skygw_logmanager_done(); - skygw_logmanager_init("/tmp", LOG_TARGET_FS, 0, NULL); + skygw_logmanager_init(NULL, "/tmp", LOG_TARGET_FS, 0, NULL); logstr = ("Testing. One, two, three, four\n"); err = skygw_log_write(LOGFILE_ERROR, logstr); if (err != 0) @@ -638,7 +638,7 @@ static void* thr_run(void* data) } ss_dassert(err == 0); skygw_logmanager_done(); - skygw_logmanager_init("/tmp", LOG_TARGET_FS, 0, NULL); + skygw_logmanager_init(NULL, "/tmp", LOG_TARGET_FS, 0, NULL); logstr = ("Testing. One, two, three, .. where was I?\n"); err = skygw_log_write(LOGFILE_ERROR, logstr); if (err != 0) @@ -647,7 +647,7 @@ static void* thr_run(void* data) } ss_dassert(err == 0); skygw_logmanager_done(); - skygw_logmanager_init("/tmp", LOG_TARGET_FS, 0, NULL); + skygw_logmanager_init(NULL, "/tmp", LOG_TARGET_FS, 0, NULL); skygw_logmanager_done(); simple_mutex_lock(td->mtx, true); *td->nactive -= 1; diff --git a/log_manager/test/testorder.c b/log_manager/test/testorder.c index 91885f862..3b149ee7b 100644 --- a/log_manager/test/testorder.c +++ b/log_manager/test/testorder.c @@ -71,7 +71,7 @@ int main(int argc, char** argv) iterations = atoi(argv[1]); interval = atoi(argv[2]); - succp = skygw_logmanager_init(tmp, LOG_TARGET_FS, 1, optstr); + succp = skygw_logmanager_init(NULL, tmp, LOG_TARGET_FS, 1, optstr); if (!succp) { diff --git a/server/core/gateway.c b/server/core/gateway.c index 49e8cf72b..0ae0e5989 100644 --- a/server/core/gateway.c +++ b/server/core/gateway.c @@ -1730,7 +1730,7 @@ int main(int argc, char **argv) char* log_argv[] = { "MaxScale", "-l", "LOGFILE_MESSAGE,LOGFILE_ERROR", NULL }; int log_argc = sizeof(log_argv) / sizeof(log_argv[0]) - 1; - succp = skygw_logmanager_init(get_logdir(), log_target, log_argc, log_argv); + succp = skygw_logmanager_init(NULL, get_logdir(), log_target, log_argc, log_argv); if (!succp) { diff --git a/server/core/maxkeys.c b/server/core/maxkeys.c index 14850a332..543a5f205 100644 --- a/server/core/maxkeys.c +++ b/server/core/maxkeys.c @@ -60,7 +60,7 @@ int main(int argc, char **argv) arg_vector[0] = "logmanager"; arg_vector[1] = NULL; - skygw_logmanager_init(NULL, LOG_TARGET_DEFAULT, arg_count, arg_vector); + skygw_logmanager_init(NULL, NULL, LOG_TARGET_DEFAULT, arg_count, arg_vector); free(arg_vector); if (secrets_writeKeys(keyfile)) diff --git a/server/core/maxpasswd.c b/server/core/maxpasswd.c index f24ecd21c..813a9b446 100644 --- a/server/core/maxpasswd.c +++ b/server/core/maxpasswd.c @@ -63,7 +63,7 @@ main(int argc, char **argv) arg_vector[0] = "logmanager"; arg_vector[1] = NULL; - skygw_logmanager_init(NULL, LOG_TARGET_DEFAULT, arg_count, arg_vector); + skygw_logmanager_init(NULL, NULL, LOG_TARGET_DEFAULT, arg_count, arg_vector); free(arg_vector); pw = calloc(81, sizeof(char)); diff --git a/server/include/test_utils.h b/server/include/test_utils.h index bd3daa777..199959e5a 100644 --- a/server/include/test_utils.h +++ b/server/include/test_utils.h @@ -20,7 +20,7 @@ void init_test_env(char *path) NULL }; - skygw_logmanager_init(logdir, LOG_TARGET_DEFAULT, argc, argv); + skygw_logmanager_init(NULL, logdir, LOG_TARGET_DEFAULT, argc, argv); poll_init(); hkinit(); } diff --git a/server/modules/filter/test/harness_common.c b/server/modules/filter/test/harness_common.c index 6ec0f12a2..0e020b571 100644 --- a/server/modules/filter/test/harness_common.c +++ b/server/modules/filter/test/harness_common.c @@ -18,7 +18,7 @@ int harness_init(int argc, char** argv, HARNESS_INSTANCE** inst){ char** optstr; if(!(argc == 2 && strcmp(argv[1],"-h") == 0)){ - skygw_logmanager_init(NULL,LOG_TARGET_DEFAULT,0,NULL); + skygw_logmanager_init(NULL,NULL,LOG_TARGET_DEFAULT,0,NULL); } if(!(instance.head = calloc(1,sizeof(FILTERCHAIN)))) @@ -55,7 +55,7 @@ int harness_init(int argc, char** argv, HARNESS_INSTANCE** inst){ optstr = (char**)malloc(sizeof(char*)*2); optstr[0] = strdup("log_manager"); optstr[1] = NULL; - skygw_logmanager_init(tmp, LOG_TARGET_DEFAULT, 1, optstr); + skygw_logmanager_init(NULL, tmp, LOG_TARGET_DEFAULT, 1, optstr); free(optstr); rval = process_opts(argc,argv); diff --git a/server/modules/routing/binlog/maxbinlogcheck.c b/server/modules/routing/binlog/maxbinlogcheck.c index 2c8966226..a1fcc8ec6 100644 --- a/server/modules/routing/binlog/maxbinlogcheck.c +++ b/server/modules/routing/binlog/maxbinlogcheck.c @@ -136,7 +136,7 @@ int main(int argc, char **argv) { arg_vector[0] = "logmanager"; arg_vector[1] = NULL; - skygw_logmanager_init(NULL, LOG_TARGET_DEFAULT, arg_count, arg_vector); + skygw_logmanager_init(NULL, NULL, LOG_TARGET_DEFAULT, arg_count, arg_vector); skygw_log_set_augmentation(0); diff --git a/server/modules/routing/binlog/test/testbinlog.c b/server/modules/routing/binlog/test/testbinlog.c index 0ee73df77..b21c48253 100644 --- a/server/modules/routing/binlog/test/testbinlog.c +++ b/server/modules/routing/binlog/test/testbinlog.c @@ -101,7 +101,7 @@ int main(int argc, char **argv) { arg_vector[0] = "logmanager"; arg_vector[1] = NULL; - skygw_logmanager_init(NULL, LOG_TARGET_DEFAULT, arg_count,arg_vector); + skygw_logmanager_init(NULL, NULL, LOG_TARGET_DEFAULT, arg_count,arg_vector); free(arg_vector); skygw_log_disable(LOGFILE_DEBUG);