Skip to content
Browse files

awaken-fetchmail: add logging & coptions to control it

  • Loading branch information...
1 parent f2eba73 commit ed19050b0485fa1af4868396b6f53356387e1453 @marschap committed May 20, 2011
Showing with 126 additions and 6 deletions.
  1. +17 −1 awaken-fetchmail.1.in
  2. +109 −5 awaken-fetchmail.c
View
18 awaken-fetchmail.1.in
@@ -15,7 +15,7 @@
.\" with this program; if not, write to the Free Software Foundation, Inc.,
.\" 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
.\"
-.TH AWAKEN-FETCHMAIL 1 "26 July 2009" "" "Dovecot Plugins"
+.TH AWAKEN-FETCHMAIL 1 "20 May 2011" "" "Dovecot Plugins"
.SH NAME
awaken-fetchmail \- setuid helper to trigger fetchmail wakeups
@@ -37,7 +37,23 @@ to the user the system-wide fetchmail daemon is running with.
.SH CONFIGURATION
For security reasons, awaken-fetchmail can be configured at compile time only.
+.SH OPTIONS
+Normal invocation of awaken-fetchmail does not require any options.
+Logging behavior, however, can be fine-tuned by specifying one or more
+of the following flags:
+.TP
+.B \-\-quiet\fR, \fB\-q
+Be quiet: suppress any log messages to syslog.
+.TP
+.B \-\-verbose\fR, \fB\-v
+Be verbose: log informational messages to syslog.
+.TP
+.B \-\-help\fR, \fB\-h
+Display usage information and exit.
+
.SH ERRORS
+Any potential error or informational messages of awaken-fetchmail are reported
+via syslog's LOG_MAIL facility.
After having successfully sent the signal to fetchmail, awaken-fetchmail
returns \fBEXIT_SUCCESS\fP, on error it returns \fBEXIT_FAILURE\fP.
View
114 awaken-fetchmail.c
@@ -1,4 +1,4 @@
-/*
+/*
* awaken-fetchmail: send SIGUSR1 to a fetchmail daemon in order to wake it
*
* Copyright (C) 2009-2011 Peter Marschall <peter@adpm.de>
@@ -15,23 +15,75 @@
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- *
+ *
*/
-
+#
#include <stdlib.h>
#include <stdio.h>
+#include <unistd.h> /* for isatty() */
#include <errno.h>
#include <sys/types.h>
#include <signal.h>
+#include <getopt.h> /* for getopt_long() */
+#include <syslog.h> /* for openlog() */
+#include <stdarg.h> /* for vsyslog() */
+#include <libgen.h> /* for basename() */
+
#if !defined(FETCHMAIL_PIDFILE)
# define FETCHMAIL_PIDFILE "/var/run/fetchmail/fetchmail.pid"
#endif
+
+/* declare functions */
+void logger(int priority, const char *format, ...);
+void usage(int status);
+
+
+/* declare global variables */
+static char *progname = NULL;
+static int quiet = 0;
+static int verbose = 0;
+
+
int main(int argc, char *argv[])
{
- FILE *file = fopen(FETCHMAIL_PIDFILE, "r");
+ const char shortopts[] = "qhv";
+ const struct option longopts[] = {
+ { "quiet", no_argument, NULL, 'q' },
+ { "verbose", no_argument, NULL, 'v' },
+ { "help", no_argument, NULL, 'h' },
+ { NULL, 0, NULL, 0 }
+ };
+
+ int optc;
+ FILE *file;
+
+ progname = basename(argv[0]);
+
+ /* parse options */
+ while ((optc = getopt_long(argc, argv, shortopts, longopts, NULL)) != EOF) {
+ switch (optc) {
+ case 'h':
+ usage(EXIT_SUCCESS);
+ break;
+ case 'q':
+ quiet++;
+ break;
+ case 'v':
+ verbose++;
+ break;
+ default:
+ usage(EXIT_FAILURE);
+ }
+ }
+
+ /* do not expect any non-option arguments */
+ if (optind != argc)
+ usage(EXIT_FAILURE);
+
+ file = fopen(FETCHMAIL_PIDFILE, "r");
if (file != NULL) {
char buffer[256];
@@ -45,16 +97,68 @@ int main(int argc, char *argv[])
/* convert buffer to pid (with error checking) */
errno = 0;
pid = strtol(buffer, NULL, 10);
-
+
if ((pid > 0) && (errno == 0)) {
if (kill(pid, SIGUSR1) == 0) {
+ if (verbose)
+ logger(LOG_INFO, "sending signal SIGUSR1 to PID %ld", pid);
fclose(file);
exit(EXIT_SUCCESS);
}
+ else
+ logger(LOG_ERR, "unable to send signal SIGUSR1 to PID %ld", pid);
}
+ else
+ logger(LOG_ERR, "no numeric PID found in file '%s'", FETCHMAIL_PIDFILE);
}
+ else
+ logger(LOG_ERR, "unable to read from file '%s'", FETCHMAIL_PIDFILE);
+
fclose(file);
}
+ else
+ logger(LOG_ERR, "unable to open file '%s'", FETCHMAIL_PIDFILE);
+
exit(EXIT_FAILURE);
}
+
+/* log to syslog */
+void logger(int priority, const char *format, ...)
+{
+ static int opened = 0;
+
+ if (!opened) {
+ openlog(progname, 0, LOG_MAIL);
+ opened++;
+ }
+
+ if (!quiet) {
+ va_list ap;
+
+ va_start(ap, format);
+ syslog(priority | LOG_MAIL, format, ap);
+ va_end(ap);
+ }
+}
+
+/* display usage message */
+void usage(int status)
+{
+ if (isatty(fileno(stderr))) {
+ fprintf(stderr, "Usage: %s [<options>]\n", progname);
+
+ if (status == 0) {
+ fprintf(stderr,
+ " where <options> are:\n"
+ " -h --help show this help page\n"
+ " -q --quiet be quiet: suppress logging error messages\n"
+ " -v --verbose be verbose: log informational messages\n");
+ }
+ else
+ fprintf(stderr, "For help, type: %s -h\n", progname);
+
+ }
+
+ exit(status);
+}

0 comments on commit ed19050

Please sign in to comment.
Something went wrong with that request. Please try again.