From 9563877fbb5fbee44af23b638be30a7a9397da4d Mon Sep 17 00:00:00 2001 From: Christian Kruse Date: Thu, 16 Jan 2014 15:22:34 +0100 Subject: [PATCH] new config option, stdout/stdin closed MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Now stdin and stdout get closed. Additionally stderr gets closed and reopened to the new config option „logfile“ if specified --- config.c | 2 ++ config.h | 3 ++- log.c | 10 +++++++++- log.h | 2 +- repmgr.c | 2 +- repmgr.conf.sample | 5 +++++ repmgrd.c | 2 +- 7 files changed, 21 insertions(+), 5 deletions(-) diff --git a/config.c b/config.c index 6f8f0362..16f5b23f 100644 --- a/config.c +++ b/config.c @@ -120,6 +120,8 @@ parse_config(const char *config_file, t_configuration_options *options) strncpy (options->pg_bindir, value, MAXLEN); else if (strcmp(name, "pg_ctl_options") == 0) strncpy (options->pgctl_options, value, MAXLEN); + else if (strcmp(name, "logfile") == 0) + strncpy(options->logfile, value, MAXLEN); else log_warning(_("%s/%s: Unknown name/value pair!\n"), name, value); } diff --git a/config.h b/config.h index 8c8bbea1..611672ed 100644 --- a/config.h +++ b/config.h @@ -42,9 +42,10 @@ typedef struct int reconnect_intvl; char pg_bindir[MAXLEN]; char pgctl_options[MAXLEN]; + char logfile[MAXLEN]; } t_configuration_options; -#define T_CONFIGURATION_OPTIONS_INITIALIZER { "", -1, "", MANUAL_FAILOVER, -1, "", "", "", "", "", "", "", -1, -1, -1, "", "" } +#define T_CONFIGURATION_OPTIONS_INITIALIZER { "", -1, "", MANUAL_FAILOVER, -1, "", "", "", "", "", "", "", -1, -1, -1, "", "", "" } void parse_config(const char *config_file, t_configuration_options *options); void parse_line(char *buff, char *name, char *value); diff --git a/log.c b/log.c index 41e18f2c..aa953318 100644 --- a/log.c +++ b/log.c @@ -68,7 +68,7 @@ static int detect_log_facility(const char* facility); int log_type = REPMGR_STDERR; int log_level = LOG_NOTICE; -bool logger_init(const char* ident, const char* level, const char* facility) +bool logger_init(t_configuration_options *opts, const char* ident, const char* level, const char* facility) { int l; @@ -140,6 +140,14 @@ bool logger_init(const char* ident, const char* level, const char* facility) #endif + fclose(stdin); + fclose(stdout); + + if (*opts->logfile) + { + freopen(opts->logfile, "a", stderr); + } + return true; } diff --git a/log.h b/log.h index 643b3f69..6dcb0b8d 100644 --- a/log.h +++ b/log.h @@ -114,7 +114,7 @@ void stderr_log_with_level(const char *level_name, int level, const char *fmt, . /* Logger initialisation and shutdown */ bool logger_shutdown(void); -bool logger_init(const char* ident, const char* level, const char* facility); +bool logger_init(t_configuration_options *opts, const char* ident, const char* level, const char* facility); void logger_min_verbose(int minimum); extern int log_type; diff --git a/repmgr.c b/repmgr.c index eee477fa..68dd4e49 100644 --- a/repmgr.c +++ b/repmgr.c @@ -322,7 +322,7 @@ main(int argc, char **argv) * at, but it often requires detailed logging to troubleshoot * problems. */ - logger_init(progname, options.loglevel, options.logfacility); + logger_init(&options, progname, options.loglevel, options.logfacility); if (runtime_options.verbose) logger_min_verbose(LOG_INFO); diff --git a/repmgr.conf.sample b/repmgr.conf.sample index c9465cd1..8aa8d63b 100644 --- a/repmgr.conf.sample +++ b/repmgr.conf.sample @@ -42,3 +42,8 @@ pg_bindir=/usr/bin/ # you may add command line arguments for pg_ctl # # pg_ctl_options='-s' + +# +# redirect stderr to a logfile +# +# logfile='/var/log/repmgr.log' diff --git a/repmgrd.c b/repmgrd.c index 6147162f..e5191d48 100644 --- a/repmgrd.c +++ b/repmgrd.c @@ -272,7 +272,7 @@ main(int argc, char **argv) exit(ERR_BAD_CONFIG); } - logger_init(progname, local_options.loglevel, local_options.logfacility); + logger_init(&local_options, progname, local_options.loglevel, local_options.logfacility); if (verbose) logger_min_verbose(LOG_INFO);