Add -m option to cron(8), overriding default mail recipient for cron mails,

unless explicitly provided by MAILTO= line in crontab.  This feature can be
useful in massive hosting environment, where most users do not care about
autogenerated mails.

Setting recipient to null string disables default mails at all.

Approved by:	yar
MFC after:	4 weeks
This commit is contained in:
Dmitry Morozovsky 2008-06-29 16:56:18 +00:00
parent 6db9940f5f
commit b75634d238
4 changed files with 36 additions and 17 deletions

View File

@ -17,7 +17,7 @@
.\"
.\" $FreeBSD$
.\"
.Dd June 17, 2007
.Dd June 29, 2008
.Dt CRON 8
.Os
.Sh NAME
@ -27,6 +27,7 @@
.Nm
.Op Fl j Ar jitter
.Op Fl J Ar rootjitter
.Op Fl m Ar mailto
.Op Fl s
.Op Fl o
.Op Fl x Ar debugflag Ns Op , Ns Ar ...
@ -114,6 +115,23 @@ Enable time jitter for superuser jobs.
The same as
.Fl j
except that it will affect jobs run by the superuser only.
.It Fl m Ar mailto
Overrides the default recipient for
.Nm
mail.
Each
.Xr crontab 5
without
.Ev MAILTO
explicitly set will send mail to the
.Ar mailto
mailbox.
Sending mail will be disabled by default if
.Ar mailto
set to a null string, usually specified in a shell as
.Li ''
or
.Li \*q\*q .
.It Fl s
Enable special handling of situations when the GMT offset of the local
timezone changes, such as the switches between the standard time and

View File

@ -53,7 +53,7 @@ usage() {
char **dflags;
fprintf(stderr, "usage: cron [-j jitter] [-J rootjitter] "
"[-s] [-o] [-x debugflag[,...]]\n");
"[-m mailto] [-s] [-o] [-x debugflag[,...]]\n");
fprintf(stderr, "\ndebugflags: ");
for(dflags = DebugFlagNames; *dflags; dflags++) {
@ -443,7 +443,7 @@ parse_args(argc, argv)
int argch;
char *endp;
while ((argch = getopt(argc, argv, "j:J:osx:")) != -1) {
while ((argch = getopt(argc, argv, "j:J:m:osx:")) != -1) {
switch (argch) {
case 'j':
Jitter = strtoul(optarg, &endp, 10);
@ -457,6 +457,9 @@ parse_args(argc, argv)
errx(ERROR_EXIT,
"bad value for root jitter: %s", optarg);
break;
case 'm':
defmailto = optarg;
break;
case 'o':
dst_enabled = 0;
break;

View File

@ -268,7 +268,8 @@ char *DowNames[] = {
NULL
};
char *ProgramName;
char *ProgramName,
*defmailto;
int LineNumber;
unsigned Jitter,
RootJitter;
@ -285,7 +286,8 @@ char *DebugFlagNames[] = { /* sync with #defines */
extern char *copyright[],
*MonthNames[],
*DowNames[],
*ProgramName;
*ProgramName,
*defmailto;
extern int LineNumber;
extern unsigned Jitter,
RootJitter;

View File

@ -459,18 +459,14 @@ child_process(e, u)
/* get name of recipient. this is MAILTO if set to a
* valid local username; USER otherwise.
*/
if (mailto) {
/* MAILTO was present in the environment
if (mailto == NULL) {
/* MAILTO not present, set to USER,
* unless globally overriden.
*/
if (!*mailto) {
/* ... but it's empty. set to NULL
*/
mailto = NULL;
}
} else {
/* MAILTO not present, set to USER.
*/
mailto = usernm;
if (defmailto)
mailto = defmailto;
else
mailto = usernm;
}
/* if we are supposed to be mailing, MAILTO will
@ -478,7 +474,7 @@ child_process(e, u)
* up the mail command and subjects and stuff...
*/
if (mailto) {
if (mailto && *mailto != '\0') {
register char **env;
auto char mailcmd[MAX_COMMAND];
auto char hostname[MAXHOSTNAMELEN];