From 464bab19acf0aaeb8c5512c2092042162266de2d Mon Sep 17 00:00:00 2001 From: Matthias Andree Date: Sun, 23 Sep 2012 14:44:25 +0200 Subject: [PATCH] Clean up logfile vs. syslog handling. In case logfile overrides syslog, send a message to the latter stating where logging goes. Also revise manual page. --- NEWS | 2 ++ fetchmail.c | 45 ++++++++++++++++++++++++++++++++++++--------- fetchmail.man | 15 ++++++++++----- 3 files changed, 48 insertions(+), 14 deletions(-) diff --git a/NEWS b/NEWS index a0705cd8..2012440c 100644 --- a/NEWS +++ b/NEWS @@ -72,6 +72,8 @@ after 6.3.22 not available on their own in a newer 6.3.X release. # BUG FIXES * Fix combination of --plugin and -f -. Patch by Alexander Zangerl, to fix Debian Bug#671294. +* Clean up logfile vs. syslog handling, and in case logfile overrides + syslog, send a message to the latter stating where logging goes. fetchmail-6.3.22 (released 2012-08-29, 26077 LoC): diff --git a/fetchmail.c b/fetchmail.c index 4327c6e4..c02e4432 100644 --- a/fetchmail.c +++ b/fetchmail.c @@ -307,9 +307,31 @@ int main(int argc, char **argv) if (!quitonly) implicitmode = load_params(argc, argv, optind); - /* precedence: logfile (if effective) overrides syslog. */ - if (run.logfile && run.poll_interval && !nodetach) { - run.use_syslog = 0; + if (run.logfile) { + /* nodetach -> turn off logfile option */ + if (nodetach) { + if (outlevel >= O_DEBUG) { fprintf(stderr, GT_("The nodetach option is in effect, ignoring logfile option.\n")); } + xfree(run.logfile); + } + + /* not in daemon mode -> turn off logfile option */ + if (0 == run.poll_interval) { + if (outlevel >= O_DEBUG) { fprintf(stderr, GT_("Not running in daemon mode, ignoring logfile option.\n")); } + xfree(run.logfile); + } + + /* log file not writable -> turn off logfile option */ + if (run.logfile && 0 != access(run.logfile, F_OK)) { + if (outlevel >= O_DEBUG) { fprintf(stderr, GT_("Logfile \"%s\" does not exist, ignoring logfile option.\n"), run.logfile); } + xfree(run.logfile); + } + + /* log file not writable -> turn off logfile option */ + if (run.logfile && 0 != access(run.logfile, W_OK)) { + if (outlevel >= O_DEBUG) { fprintf(stderr, GT_("Logfile \"%s\" is not writable, aborting.\n"), run.logfile); } + xfree(run.logfile); + exit(PS_UNDEFINED); + } } #if defined(HAVE_SYSLOG) @@ -322,11 +344,18 @@ int main(int argc, char **argv) /* Assume BSD4.2 openlog with two arguments */ openlog(program_name, LOG_PID); #endif - report_init(-1); + /* precedence: logfile (if effective) overrides syslog. */ + if (run.logfile) { + syslog(LOG_ERR, GT_("syslog and logfile options are both set, ignoring syslog, and logging to %s"), run.logfile); + run.use_syslog = 0; + report_init((run.poll_interval == 0 || nodetach) && !run.logfile); /* when changing this, change copy below, too */ + } else { + report_init(-1); + } } else #endif - report_init((run.poll_interval == 0 || nodetach) && !run.logfile); + report_init((run.poll_interval == 0 || nodetach) && !run.logfile); /* when changing this, change copy above, too */ #ifdef POP3_ENABLE /* initialize UID handling */ @@ -601,14 +630,12 @@ int main(int argc, char **argv) else { /* not in daemon mode */ - if (run.logfile && !nodetach && access(run.logfile, F_OK) == 0) - { + if (run.logfile) + { if (!freopen(run.logfile, "a", stdout)) report(stderr, GT_("could not open %s to append logs to\n"), run.logfile); if (!freopen(run.logfile, "a", stderr)) report(stdout, GT_("could not open %s to append logs to\n"), run.logfile); - if (run.use_syslog) - report(stdout, GT_("fetchmail: Warning: syslog and logfile are set. Check both for logs!\n")); } } diff --git a/fetchmail.man b/fetchmail.man index 56026d0b..0a95f033 100644 --- a/fetchmail.man +++ b/fetchmail.man @@ -1366,6 +1366,8 @@ The option turns off use of .BR syslog (3), assuming it's turned on in the \fI~/.fetchmailrc\fP file. +This option is overridden, in certain situations, by \fB\-\-logfile\fP (which +see). .PP The .B \-N @@ -1377,8 +1379,7 @@ fetchmail runs as the child of a supervisor process such as .BR init (8) or Gerrit Pape's .BR runit (8). -Note that this also causes the logfile option to be ignored (though -perhaps it shouldn't). +Note that this also causes the logfile option to be ignored. .PP Note that while running in daemon mode polling a POP2 or IMAP2bis server, transient errors (such as DNS failures or sendmail delivery refusals) @@ -1702,13 +1703,16 @@ Keep permanently undeliverable mail as though a temporary error had occurred (default). T} set logfile \-L \& T{ -Name of a file to append error and status messages to. +Name of a file to append error and status messages to. Only effective +in daemon mode and if fetchmail detaches. If effective, overrides \fBset +syslog\fP. T} set idfile \-i \& T{ Name of the file to store UID lists in. T} set syslog \& \& T{ -Do error logging through syslog(3). +Do error logging through syslog(3). May be overriden by \fBset +logfile\fP. T} set no syslog \& \& T{ Turn off error logging through syslog(3). (default) @@ -2226,7 +2230,8 @@ authentication. These defaults may be overridden by later options. There are some global option statements: 'set logfile' followed by a string sets the same global specified by \-\-logfile. A command-line \-\-logfile option will override this. Note that \-\-logfile is -only effective if fetchmail detaches itself from the terminal and the +only effective if fetchmail detaches itself from the terminal, is in +daemon mode, and if the logfile already exists before fetchmail is run, and it overrides \-\-syslog in this case. Also, \&'set daemon' sets the poll interval as \-\-daemon does. This can be -- 2.43.2