* Add a command-line option to enable debugging mode, which sends error

messages to stderr instead of syslog.

* Fix manpage with respect to diagnostics.

Reported by:	rfg@monkeys.com (Ronald F. Guilmette)
This commit is contained in:
Sheldon Hearn 1999-06-17 14:44:42 +00:00
parent f360e91d4d
commit 42a1247753
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=47980
2 changed files with 67 additions and 19 deletions

View File

@ -30,7 +30,7 @@
.\" SUCH DAMAGE. .\" SUCH DAMAGE.
.\" .\"
.\" From: @(#)vacation.1 8.1 (Berkeley) 6/16/93 .\" From: @(#)vacation.1 8.1 (Berkeley) 6/16/93
.\" $Id$ .\" $Id: vacation.1,v 1.6 1997/02/22 19:57:38 peter Exp $
.\" .\"
.Dd June 16, 1993 .Dd June 16, 1993
.Dt VACATION 1 .Dt VACATION 1
@ -40,11 +40,14 @@
.Nd return ``I am not here'' indication .Nd return ``I am not here'' indication
.Sh SYNOPSIS .Sh SYNOPSIS
.Nm vacation .Nm vacation
.Op Fl d
.Fl i .Fl i
.Op Fl r Ar interval .Op Fl r Ar interval
.Nm vacation .Nm vacation
.Op Fl d
.Fl l .Fl l
.Nm vacation .Nm vacation
.Op Fl d
.Op Fl a Ar alias .Op Fl a Ar alias
.Ar login .Ar login
.Sh DESCRIPTION .Sh DESCRIPTION
@ -71,6 +74,8 @@ Handle messages for
.Ar alias .Ar alias
in the same manner as those received for the user's in the same manner as those received for the user's
login name. login name.
.It Fl d
Enable debugging mode. See below.
.It Fl i .It Fl i
Initialize the vacation database files. It should be used Initialize the vacation database files. It should be used
before you modify your before you modify your
@ -160,7 +165,14 @@ Fatal errors, such as calling
with incorrect arguments, or with non-existent with incorrect arguments, or with non-existent
.Ar login Ns Ar s , .Ar login Ns Ar s ,
are logged in the system log file, using are logged in the system log file, using
.Xr syslog 3 . .Xr syslog 3
unless debugging mode is enabled with the
.Fl d
option, in which case they are written to the standard error output.
.Sh DIAGNOSTICS
The
.Nm
utility exits 0 on success, and >0 if an error occurs.
.Sh FILES .Sh FILES
.Bl -tag -width "vacation.dirxxx" -compact .Bl -tag -width "vacation.dirxxx" -compact
.It Pa ~/.vacation.db .It Pa ~/.vacation.db

View File

@ -42,7 +42,7 @@ static const char copyright[] =
static char sccsid[] = "@(#)from: vacation.c 8.2 (Berkeley) 1/26/94"; static char sccsid[] = "@(#)from: vacation.c 8.2 (Berkeley) 1/26/94";
#endif #endif
static const char rcsid[] = static const char rcsid[] =
"$Id: vacation.c,v 1.12 1997/11/03 07:51:05 charnier Exp $"; "$Id: vacation.c,v 1.13 1998/10/13 14:52:32 des Exp $";
#endif /* not lint */ #endif /* not lint */
/* /*
@ -62,8 +62,10 @@ static const char rcsid[] =
#include <unistd.h> #include <unistd.h>
#include <stdio.h> #include <stdio.h>
#include <ctype.h> #include <ctype.h>
#include <stdarg.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sysexits.h>
#include <paths.h> #include <paths.h>
/* /*
@ -88,6 +90,7 @@ ALIAS *names;
DB *db; DB *db;
char from[MAXLINE]; char from[MAXLINE];
void (*msglog)(int, const char *, ...) = &syslog;
static int isdelim __P((int)); static int isdelim __P((int));
static int junkmail __P((void)); static int junkmail __P((void));
@ -99,6 +102,7 @@ static void sendmessage __P((char *));
static void setinterval __P((time_t)); static void setinterval __P((time_t));
static void setreply __P((void)); static void setreply __P((void));
static void usage __P((void)); static void usage __P((void));
static void debuglog __P((int, const char *, ...));
int int
main(argc, argv) main(argc, argv)
@ -110,19 +114,24 @@ main(argc, argv)
struct passwd *pw; struct passwd *pw;
ALIAS *cur; ALIAS *cur;
time_t interval; time_t interval;
int ch, iflag, lflag; int ch, iflag, lflag, mfail;
opterr = iflag = lflag = 0; opterr = iflag = lflag = mfail = 0;
interval = -1; interval = -1;
while ((ch = getopt(argc, argv, "a:Iilr:")) != -1) while ((ch = getopt(argc, argv, "a:dIilr:")) != -1) {
switch((char)ch) { switch((char)ch) {
case 'a': /* alias */ case 'a': /* alias */
if (!(cur = (ALIAS *)malloc((u_int)sizeof(ALIAS)))) if (!(cur = (ALIAS *)malloc((u_int)sizeof(ALIAS)))) {
mfail++;
break; break;
}
cur->name = optarg; cur->name = optarg;
cur->next = names; cur->next = names;
names = cur; names = cur;
break; break;
case 'd': /* debug mode */
msglog = &debuglog;
break;
case 'I': /* backward compatible */ case 'I': /* backward compatible */
case 'i': /* init the database */ case 'i': /* init the database */
iflag = 1; iflag = 1;
@ -143,6 +152,16 @@ main(argc, argv)
default: default:
usage(); usage();
} }
}
/* Only die on the above malloc failure here so that the
* correct logging medium is used.
*/
if (mfail) {
msglog(LOG_ERR, "vacation: malloc failed\n");
exit(EX_TEMPFAIL);
}
argc -= optind; argc -= optind;
argv += optind; argv += optind;
@ -150,17 +169,17 @@ main(argc, argv)
if (!iflag && !lflag) if (!iflag && !lflag)
usage(); usage();
if (!(pw = getpwuid(getuid()))) { if (!(pw = getpwuid(getuid()))) {
syslog(LOG_ERR, msglog(LOG_ERR,
"vacation: no such user uid %u.\n", getuid()); "vacation: no such user uid %u.\n", getuid());
exit(1); exit(1);
} }
} }
else if (!(pw = getpwnam(*argv))) { else if (!(pw = getpwnam(*argv))) {
syslog(LOG_ERR, "vacation: no such user %s.\n", *argv); msglog(LOG_ERR, "vacation: no such user %s.\n", *argv);
exit(1); exit(1);
} }
if (chdir(pw->pw_dir)) { if (chdir(pw->pw_dir)) {
syslog(LOG_NOTICE, msglog(LOG_NOTICE,
"vacation: no such directory %s.\n", pw->pw_dir); "vacation: no such directory %s.\n", pw->pw_dir);
exit(1); exit(1);
} }
@ -168,7 +187,7 @@ main(argc, argv)
db = dbopen(VDB, O_CREAT|O_RDWR | (iflag ? O_TRUNC : 0), db = dbopen(VDB, O_CREAT|O_RDWR | (iflag ? O_TRUNC : 0),
S_IRUSR|S_IWUSR, DB_HASH, NULL); S_IRUSR|S_IWUSR, DB_HASH, NULL);
if (!db) { if (!db) {
syslog(LOG_NOTICE, "vacation: %s: %s\n", VDB, strerror(errno)); msglog(LOG_NOTICE, "vacation: %s: %s\n", VDB, strerror(errno));
exit(1); exit(1);
} }
@ -182,8 +201,10 @@ main(argc, argv)
exit(0); exit(0);
} }
if (!(cur = malloc((u_int)sizeof(ALIAS)))) if (!(cur = malloc((u_int)sizeof(ALIAS)))) {
exit(1); msglog(LOG_ERR, "vacation: malloc failed\n");
exit(EX_TEMPFAIL);
}
cur->name = pw->pw_name; cur->name = pw->pw_name;
cur->next = names; cur->next = names;
names = cur; names = cur;
@ -263,7 +284,7 @@ findme: for (cur = names; !tome && cur; cur = cur->next)
if (!tome) if (!tome)
exit(0); exit(0);
if (!*from) { if (!*from) {
syslog(LOG_NOTICE, "vacation: no initial \"From\" line.\n"); msglog(LOG_NOTICE, "vacation: no initial \"From\" line.\n");
exit(1); exit(1);
} }
} }
@ -410,16 +431,16 @@ sendmessage(myname)
mfp = fopen(VMSG, "r"); mfp = fopen(VMSG, "r");
if (mfp == NULL) { if (mfp == NULL) {
syslog(LOG_NOTICE, "vacation: no ~%s/%s file.\n", myname, VMSG); msglog(LOG_NOTICE, "vacation: no ~%s/%s file.\n", myname, VMSG);
exit(1); exit(1);
} }
if (pipe(pvect) < 0) { if (pipe(pvect) < 0) {
syslog(LOG_ERR, "vacation: pipe: %s", strerror(errno)); msglog(LOG_ERR, "vacation: pipe: %s", strerror(errno));
exit(1); exit(1);
} }
i = fork(); i = fork();
if (i < 0) { if (i < 0) {
syslog(LOG_ERR, "vacation: fork: %s", strerror(errno)); msglog(LOG_ERR, "vacation: fork: %s", strerror(errno));
exit(1); exit(1);
} }
if (i == 0) { if (i == 0) {
@ -428,7 +449,7 @@ sendmessage(myname)
close(pvect[1]); close(pvect[1]);
close(fileno(mfp)); close(fileno(mfp));
execl(_PATH_SENDMAIL, "sendmail", "-f", myname, "--", from, NULL); execl(_PATH_SENDMAIL, "sendmail", "-f", myname, "--", from, NULL);
syslog(LOG_ERR, "vacation: can't exec %s: %s", msglog(LOG_ERR, "vacation: can't exec %s: %s",
_PATH_SENDMAIL, strerror(errno)); _PATH_SENDMAIL, strerror(errno));
_exit(1); _exit(1);
} }
@ -444,7 +465,7 @@ sendmessage(myname)
static void static void
usage() usage()
{ {
syslog(LOG_NOTICE, "uid %u: usage: vacation [-i [-rinterval]] [-l] [-a alias] login\n", msglog(LOG_NOTICE, "uid %u: usage: vacation [-d] [-i [-rinterval]] [-l] [-a alias] login\n",
getuid()); getuid());
exit(1); exit(1);
} }
@ -486,3 +507,18 @@ isdelim(c)
return(0); return(0);
return(1); return(1);
} }
/*
* Append a message to the standard error for the convenience of end-users
* debugging without access to the syslog messages.
*/
static void
debuglog(int i, const char *fmt, ...)
{
va_list ap;
i = 0; /* Printing syslog priority not implemented */
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
}