From adad2fc62f159c7e43c36af0ad4f9bf9f6b3efc7 Mon Sep 17 00:00:00 2001 From: Matthias Andree Date: Sun, 23 Sep 2012 14:58:06 +0200 Subject: [PATCH] Merge logfile/syslog cleanup from legacy_63 branch. --- NEWS | 2 ++ fetchmail.c | 45 ++++++++++++++++++++++++++++++++++++--------- fetchmail.man | 15 ++++++++++----- 3 files changed, 48 insertions(+), 14 deletions(-) diff --git a/NEWS b/NEWS index 23984363..6c05420c 100644 --- a/NEWS +++ b/NEWS @@ -160,6 +160,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 e30486f5..2498e471 100644 --- a/fetchmail.c +++ b/fetchmail.c @@ -574,19 +574,48 @@ 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); + } } /* logging should be set up early in case we were restarted from exec */ if (run.use_syslog) { openlog(program_name, LOG_PID, LOG_MAIL); - 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 - 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 */ @@ -865,14 +894,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 45f2cb98..8f80db38 100644 --- a/fetchmail.man +++ b/fetchmail.man @@ -1336,6 +1336,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 @@ -1347,8 +1349,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 IMAP2bis server, transient errors (such as DNS failures or sendmail delivery refusals) @@ -1667,13 +1668,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) @@ -2188,7 +2192,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