From: Will Woods Date: Tue, 5 Jan 2010 15:43:49 +0000 (+0100) Subject: Output could be redirectered to syslog. X-Git-Tag: cronie1.4.4~7 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=0a73c96db4eed7b12972c474ebeef8b61c1a37c6;p=cronie Output could be redirectered to syslog. Signed-off-by: Marcela Mašláňová --- diff --git a/man/cron.8 b/man/cron.8 index ca29f0b..446bc69 100644 --- a/man/cron.8 +++ b/man/cron.8 @@ -19,12 +19,12 @@ .\" .\" $Id: cron.8,v 1.8 2004/01/23 19:03:32 vixie Exp $ .\" -.TH CRON "8" "July 2009" "Marcela MaÅ¡láňová" "Cronie Users' Manual" +.TH CRON "8" "December 2009" "Marcela MaÅ¡láňová" "Cronie Users' Manual" .SH NAME cron \- daemon to execute scheduled commands .SH SYNOPSIS .B cron -.RB [ -n " | " -p " | " -m \fP\fI\fP\fB ] +.RB [ -n " | " -p " | " -s " | " -m \fP\fI\fP\fB ] .br .B cron .B -x @@ -57,6 +57,9 @@ examines all stored crontabs, checking each command to see if it should be run in the current minute. When executing commands, any output is mailed to the owner of the crontab (or to the user named in the MAILTO environment variable in the crontab, if such exists). +Job output can also be sent to syslog by using the +.B "\-s" +option. .PP There are two ways, how the changes are checked in crontables. The first is checking the modtime of file and the other is using inotify support. @@ -114,6 +117,12 @@ crond loads the PAM environment from the pam_env module, but these can be overriden by settings in the appropriate crontab file. .SH "OPTIONS" .TP +.B "\-s" +This option will direct cron to send job output to the system log using +.IR syslog (3). +This is useful if your system has no +.BR sendmail (8). +.TP .B "\-m" This option allows you to specify a shell command string to use for sending cron mail output instead of diff --git a/src/cron.c b/src/cron.c index b79e61d..7b57932 100644 --- a/src/cron.c +++ b/src/cron.c @@ -123,7 +123,7 @@ static void handle_signals(cron_db * database) { static void usage(void) { const char **dflags; - fprintf(stderr, "usage: %s [-n] [-p] [-i] [-m ] [-x [", + fprintf(stderr, "usage: %s [-n] [-p] [-s] [-i] [-m ] [-x [", ProgramName); for (dflags = DebugFlagNames; *dflags; dflags++) fprintf(stderr, "%s%s", *dflags, dflags[1] ? "," : "]"); @@ -152,6 +152,7 @@ int main(int argc, char *argv[]) { setlinebuf(stderr); #endif + SyslogOutput = 0; NoFork = 0; parse_args(argc, argv); @@ -581,7 +582,7 @@ static void sigchld_reaper(void) { static void parse_args(int argc, char *argv[]) { int argch; - while (-1 != (argch = getopt(argc, argv, "npix:m:"))) { + while (-1 != (argch = getopt(argc, argv, "npsix:m:"))) { switch (argch) { default: usage(); @@ -595,6 +596,9 @@ static void parse_args(int argc, char *argv[]) { case 'p': PermitAnyCrontab = 1; break; + case 's': + SyslogOutput = 1; + break; case 'i': DisableInotify = 1; break; diff --git a/src/do_command.c b/src/do_command.c index 05736a5..fe41ec8 100644 --- a/src/do_command.c +++ b/src/do_command.c @@ -62,6 +62,7 @@ static void child_process(entry * e, user * u) { int children = 0; char **jobenv = 0L; pid_t pid = getpid(); + pid_t jobpid; /* Set up the Red Hat security context for both mail/minder and job processes: */ @@ -147,7 +148,7 @@ static void child_process(entry * e, user * u) { /* fork again, this time so we can exec the user's command. */ - switch (fork()) { + switch ((jobpid = fork())) { case -1: log_it("CRON", pid, "can't fork", "child_process", errno); cron_close_pam(); @@ -333,6 +334,19 @@ static void child_process(entry * e, user * u) { FILE *mail = NULL; int bytes = 1; int status = 0; +#if defined(SYSLOG) + char jobtag[64], logbuf[1024]; + int bufidx = 0; + /* according to the NOTES section of openlog(3), jobtag will be + * used (implicitly) by future calls to syslog(). That's why it + * was defined outside of the if block here. */ + if (SyslogOutput) { + snprintf(jobtag, sizeof(jobtag), "CROND[%d]", jobpid); + openlog(jobtag, 0, LOG_CRON); + if (ch != '\n') + logbuf[bufidx++] = ch; + } +#endif Debug(DPROC | DEXT, ("[%ld] got data (%x:%c) from grandchild\n", @@ -456,6 +470,19 @@ static void child_process(entry * e, user * u) { if (mail) putc(ch, mail); } +#if defined(SYSLOG) + if (SyslogOutput) { + logbuf[bufidx++] = ch; + if ((ch == '\n') || (bufidx == sizeof(logbuf)-1)) { + if (ch == '\n') + logbuf[bufidx-1] = '\0'; + else + logbuf[bufidx] = '\0'; + syslog(LOG_INFO, "%s", logbuf); + bufidx = 0; + } + } +#endif /* only close pipe if we opened it -- i.e., we're * mailing... @@ -471,6 +498,15 @@ static void child_process(entry * e, user * u) { */ status = cron_pclose(mail); } +#if defined(SYSLOG) + if (SyslogOutput) { + if (bufidx) { + logbuf[bufidx] = '\0'; + syslog(LOG_INFO, "%s", logbuf); + } + closelog(); + } +#endif /* if there was output and we could not mail it, * log the facts so the poor user can figure out diff --git a/src/globals.h b/src/globals.h index 82e6980..21798e2 100644 --- a/src/globals.h +++ b/src/globals.h @@ -61,6 +61,7 @@ XTRN const char *DowNames[] XTRN char *ProgramName; XTRN int LineNumber; +XTRN int SyslogOutput; XTRN time_t StartTime; XTRN int NoFork; XTRN int PermitAnyCrontab;