Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for simple log outputs as alternative to email #147

Merged
merged 6 commits into from
Apr 27, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions anacron/global.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ struct job_rec1 {
char *ident;
char *command;
char *mailto;
int no_mail_output;

int tab_line;
int arg_num;
Expand Down
130 changes: 71 additions & 59 deletions anacron/runjob.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
JohnnyJayJay marked this conversation as resolved.
Show resolved Hide resolved
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))
Expand Down Expand Up @@ -299,62 +303,67 @@ 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;
jr->no_mail_output = getenv("NO_MAIL_OUTPUT") != NULL && *getenv("NO_MAIL_OUTPUT");
JohnnyJayJay marked this conversation as resolved.
Show resolved Hide resolved

/* 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)
Expand All @@ -379,7 +388,7 @@ 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;
if (!jr->no_mail_output && file_size(jr->output_fd) > jr->mail_header_size) mail_output = 1;
JohnnyJayJay marked this conversation as resolved.
Show resolved Hide resolved
else mail_output = 0;

m = mail_output ? " (produced output)" : "";
Expand All @@ -397,8 +406,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
Expand Down
4 changes: 4 additions & 0 deletions man/anacrontab.5
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ and
.I MAILTO
variables are expanded, so setting them as in the following example works as expected: [email protected] ($USER is replaced by the system user) )
.PP
If
.I NO_MAIL_OUTPUT
is defined (and non-empty), anacron's output includes the outputs of job processes and the mail functionality is disabled.
JohnnyJayJay marked this conversation as resolved.
Show resolved Hide resolved
.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
Expand Down