diff --git a/anacron/global.h b/anacron/global.h index 1856854..5a5934c 100644 --- a/anacron/global.h +++ b/anacron/global.h @@ -62,6 +62,7 @@ struct job_rec1 { char *ident; char *command; char *mailto; + int no_mail_output; int tab_line; int arg_num; diff --git a/anacron/runjob.c b/anacron/runjob.c index 04d6904..050ef20 100644 --- a/anacron/runjob.c +++ b/anacron/runjob.c @@ -166,12 +166,16 @@ static void run_job(const job_rec *jr) /* This is called to start the job, after the fork */ { - /* setup stdout and stderr */ - xclose(1); - xclose(2); - if (dup2(jr->output_fd, 1) != 1 || dup2(jr->output_fd, 2) != 2) - die_e("dup2() error"); /* dup2 also clears close-on-exec flag */ - in_background = 0; /* now, errors will be mailed to the user */ + /* If mail functionality is not disabled, pipe outputs to temp file */ + if (!jr->no_mail_output) { + /* setup stdout and stderr */ + xclose(1); + xclose(2); + if (dup2(jr->output_fd, 1) != 1 || dup2(jr->output_fd, 2) != 2) + die_e("dup2() error"); /* dup2 also clears close-on-exec flag */ + in_background = 0; /* now, errors will be mailed to the user */ + } + if (chdir("/")) die_e("Can't chdir to '/'"); if (sigprocmask(SIG_SETMASK, &old_sigmask, NULL)) @@ -291,6 +295,7 @@ launch_job(job_rec *jr) char *mailfrom; char mailto_expanded[MAX_EMAILSTR]; char mailfrom_expanded[MAX_EMAILSTR]; + char *no_mail_output; /* get hostname */ if (gethostname(hostname, 512)) { @@ -299,62 +304,68 @@ launch_job(job_rec *jr) setup_env(jr); - /* Get the destination email address if set, or current user otherwise */ - mailto = getenv("MAILTO"); - if (mailto == NULL) { - mailto = username(); - } - else { - if (expand_envvar(mailto, mailto_expanded, sizeof(mailto_expanded))) { - mailto = mailto_expanded; + no_mail_output = getenv("NO_MAIL_OUTPUT"); + jr->no_mail_output = no_mail_output != NULL && *no_mail_output; + + /* Set up email functionality if it isn't disabled */ + if (!jr->no_mail_output) { + /* Get the destination email address if set, or current user otherwise */ + mailto = getenv("MAILTO"); + if (mailto == NULL) { + mailto = username(); } else { - complain("The environment variable 'MAILTO' could not be expanded. The non-expanded value will be used."); - } - } + if (expand_envvar(mailto, mailto_expanded, sizeof(mailto_expanded))) { + mailto = mailto_expanded; + } + else { + complain("The environment variable 'MAILTO' could not be expanded. The non-expanded value will be used."); + } + } - /* Get the source email address if set, or current user otherwise */ - mailfrom = getenv("MAILFROM"); - if (mailfrom == NULL) { - mailfrom = username(); - } - else { - if (expand_envvar(mailfrom, mailfrom_expanded, sizeof(mailfrom_expanded))) { - mailfrom = mailfrom_expanded; + /* Get the source email address if set, or current user otherwise */ + mailfrom = getenv("MAILFROM"); + if (mailfrom == NULL) { + mailfrom = username(); } else { - complain("The environment variable 'MAILFROM' could not be expanded. The non-expanded value will be used."); - } - } + if (expand_envvar(mailfrom, mailfrom_expanded, sizeof(mailfrom_expanded))) { + mailfrom = mailfrom_expanded; + } + else { + complain("The environment variable 'MAILFROM' could not be expanded. The non-expanded value will be used."); + } + } - /* create temporary file for stdout and stderr of the job */ - temp_file(jr); fd = jr->output_fd; - /* write mail header */ - xwrite(fd, "From: "); - xwrite(fd, "Anacron <"); - xwrite(fd, mailfrom); - xwrite(fd, ">\n"); - xwrite(fd, "To: "); - xwrite(fd, mailto); - xwrite(fd, "\n"); - xwrite(fd, "MIME-Version: 1.0\n"); - xwrite(fd, "Content-Type: text/plain; charset=\""); - xwrite(fd, nl_langinfo(CODESET)); - xwrite(fd, "\"\n"); - xwrite(fd, "Content-Transfer-Encoding: 8bit\n"); - xwrite(fd, "Subject: Anacron job '"); - xwrite(fd, jr->ident); - xwrite(fd, "' on "); - xwrite(fd, hostname); - xwrite(fd, "\n\n"); - - if (*mailto == '\0') - jr->mailto = NULL; - else - /* ugly but works without strdup() */ - jr->mailto = mailto; - - jr->mail_header_size = file_size(fd); + /* create temporary file for stdout and stderr of the job */ + temp_file(jr); fd = jr->output_fd; + /* write mail header */ + xwrite(fd, "From: "); + xwrite(fd, "Anacron <"); + xwrite(fd, mailfrom); + xwrite(fd, ">\n"); + xwrite(fd, "To: "); + xwrite(fd, mailto); + xwrite(fd, "\n"); + xwrite(fd, "MIME-Version: 1.0\n"); + xwrite(fd, "Content-Type: text/plain; charset=\""); + xwrite(fd, nl_langinfo(CODESET)); + xwrite(fd, "\"\n"); + xwrite(fd, "Content-Transfer-Encoding: 8bit\n"); + xwrite(fd, "Subject: Anacron job '"); + xwrite(fd, jr->ident); + xwrite(fd, "' on "); + xwrite(fd, hostname); + xwrite(fd, "\n\n"); + + if (*mailto == '\0') + jr->mailto = NULL; + else + /* ugly but works without strdup() */ + jr->mailto = mailto; + + jr->mail_header_size = file_size(fd); + } pid = xfork(); if (pid == 0) @@ -379,8 +390,8 @@ tend_job(job_rec *jr, int status) update_timestamp(jr); unlock(jr); - if (file_size(jr->output_fd) > jr->mail_header_size) mail_output = 1; - else mail_output = 0; + + mail_output = !jr->no_mail_output && file_size(jr->output_fd) > jr->mail_header_size; m = mail_output ? " (produced output)" : ""; if (WIFEXITED(status) && WEXITSTATUS(status) == 0) @@ -397,8 +408,11 @@ tend_job(job_rec *jr, int status) jr->job_pid = 0; running_jobs--; if (mail_output) launch_mailer(jr); - xclose(jr->output_fd); - xclose(jr->input_fd); + /* output_fd and input_fd are only set if mail functionality is enabled */ + if (!jr->no_mail_output) { + xclose(jr->output_fd); + xclose(jr->input_fd); + } } void diff --git a/man/anacrontab.5 b/man/anacrontab.5 index 507cfd8..f7ca5c0 100644 --- a/man/anacrontab.5 +++ b/man/anacrontab.5 @@ -81,6 +81,10 @@ and .I MAILTO variables are expanded, so setting them as in the following example works as expected: MAILFROM=cron-$USER@cron.com ($USER is replaced by the system user) ) .PP +If +.I NO_MAIL_OUTPUT +is defined (and non-empty), the standard output and error descriptors of job processes are not redirected and e-mailed. +.PP .PP Empty lines are either blank lines, line containing white spaces only, or lines with white spaces followed by a '#' followed by an arbitrary