]> granicus.if.org Git - cronie/commitdiff
Output could be redirectered to syslog.
authorWill Woods <wwoods@redhat.com>
Tue, 5 Jan 2010 15:43:49 +0000 (16:43 +0100)
committerMarcela Mašláňová <mmaslano@redhat.com>
Tue, 5 Jan 2010 15:43:49 +0000 (16:43 +0100)
Signed-off-by: Marcela Mašláňová <mmaslano@redhat.com>
man/cron.8
src/cron.c
src/do_command.c
src/globals.h

index ca29f0bf11a7046f0ed0660d57add5a7312fad9b..446bc69058fd270313028195ffc70db15922c559 100644 (file)
 .\"
 .\" $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<mail command>\fP\fB ]
+.RB [ -n " | " -p " | " -s " | " -m \fP\fI<mail command>\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
index b79e61d8c9907d1e060498c454a42391069a30f6..7b57932ad34aeca22e06535ec51dd54604ff0a20 100644 (file)
@@ -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 <mail command>] [-x [",
+       fprintf(stderr, "usage:  %s [-n] [-p] [-s] [-i] [-m <mail command>] [-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;
index 05736a525911c417432803c821ff8658de2641f4..fe41ec8665f4c3407ff284a954126566249dda4b 100644 (file)
@@ -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
index 82e69801bbe22f761cc918b955d4586a3018c336..21798e24581a69a6a03285ca529251be11638f42 100644 (file)
@@ -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;