summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJason Downs <downsj@cvs.openbsd.org>1997-07-08 04:26:01 +0000
committerJason Downs <downsj@cvs.openbsd.org>1997-07-08 04:26:01 +0000
commit7472333cce6cbbb33e17b00bd44c882f19b2cf15 (patch)
tree239645fbc352efef657ca9e617c743c36023fcb5
parent8a05d80dd77510a6c111902a080b319b4a9b0e8c (diff)
err()/warn()
-rw-r--r--usr.bin/newsyslog/newsyslog.c191
1 files changed, 62 insertions, 129 deletions
diff --git a/usr.bin/newsyslog/newsyslog.c b/usr.bin/newsyslog/newsyslog.c
index 650d044f93e..e4e9d61c8bf 100644
--- a/usr.bin/newsyslog/newsyslog.c
+++ b/usr.bin/newsyslog/newsyslog.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: newsyslog.c,v 1.10 1997/07/07 22:50:56 downsj Exp $ */
+/* $OpenBSD: newsyslog.c,v 1.11 1997/07/08 04:26:00 downsj Exp $ */
/*
* Copyright (c) 1997, Jason Downs. All rights reserved.
@@ -61,7 +61,7 @@ provided "as is" without express or implied warranty.
*/
#ifndef lint
-static char rcsid[] = "$OpenBSD: newsyslog.c,v 1.10 1997/07/07 22:50:56 downsj Exp $";
+static char rcsid[] = "$OpenBSD: newsyslog.c,v 1.11 1997/07/08 04:26:00 downsj Exp $";
#endif /* not lint */
#ifndef CONF
@@ -97,6 +97,7 @@ static char rcsid[] = "$OpenBSD: newsyslog.c,v 1.10 1997/07/07 22:50:56 downsj E
#include <pwd.h>
#include <grp.h>
#include <unistd.h>
+#include <err.h>
#define kbytes(size) (((size) + 1023) >> 10)
@@ -119,7 +120,8 @@ struct conf_entry {
struct conf_entry *next; /* Linked list pointer */
};
-char *progname; /* contains argv[0] */
+extern const char *__progname;
+
int verbose = 0; /* Print out what's going on */
int needroot = 1; /* Root privs are necessary */
int noaction = 0; /* Don't do anything, just show it */
@@ -157,15 +159,13 @@ int main(argc, argv)
struct conf_entry *p, *q;
PRS(argc,argv);
- if (needroot && getuid() && geteuid()) {
- fprintf(stderr,"%s: must have root privs\n",progname);
- exit(1);
- }
+ if (needroot && getuid() && geteuid())
+ errx(1, "You must be root.");
p = q = parse_file();
while (p) {
do_entry(p);
p=p->next;
- free((char *) q);
+ free(q);
q=p;
}
exit(0);
@@ -226,7 +226,6 @@ void PRS(argc, argv)
char line[BUFSIZ];
char *p;
- progname = argv[0];
timenow = time((time_t *) 0);
daytime = ctime(&timenow) + 4;
daytime[15] = '\0';
@@ -273,9 +272,7 @@ void PRS(argc, argv)
void usage()
{
- fprintf(stderr,
- "Usage: %s <-nrv> <-f config-file>\n", progname);
- exit(1);
+ errx(1, "usage: %s <-nrvm> <-f config-file>", __progname);
}
/* Parse a configuration file and return a linked list of all the logs
@@ -295,46 +292,32 @@ struct conf_entry *parse_file()
f = fopen(conf,"r");
else
f = stdin;
- if (!f) {
- (void) fprintf(stderr,"%s: ",progname);
- perror(conf);
- exit(1);
- }
+ if (!f)
+ err(1, conf);
+
while (fgets(line,BUFSIZ,f)) {
if ((line[0]== '\n') || (line[0] == '#'))
continue;
errline = strdup(line);
- if (errline == NULL) {
- (void) fprintf(stderr,"%s: ",progname);
- perror("strdup()");
- exit(1);
- }
+ if (errline == NULL)
+ err(1, "strdup");
if (!first) {
working = (struct conf_entry *) malloc(sizeof(struct conf_entry));
- if (working == NULL) {
- (void) fprintf(stderr,"%s: ",progname);
- perror("malloc()");
- exit(1);
- }
+ if (working == NULL)
+ err(1, "malloc");
first = working;
} else {
working->next = (struct conf_entry *) malloc(sizeof(struct conf_entry));
- if (working->next == NULL) {
- (void) fprintf(stderr,"%s: ",progname);
- perror("malloc()");
- exit(1);
- }
+ if (working->next == NULL)
+ err(1, "malloc");
working = working->next;
}
q = parse = missing_field(sob(line),errline);
*(parse = son(line)) = '\0';
working->log = strdup(q);
- if (working->log == NULL) {
- (void) fprintf(stderr,"%s: ",progname);
- perror("strdup()");
- exit(1);
- }
+ if (working->log == NULL)
+ err(1, "strdup");
q = parse = missing_field(sob(++parse),errline);
*(parse = son(parse)) = '\0';
@@ -342,12 +325,8 @@ struct conf_entry *parse_file()
*group++ = '\0';
if (*q) {
if (!(isnumberstr(q))) {
- if ((pass = getpwnam(q)) == NULL) {
- fprintf(stderr,
- "Error in config file; unknown user:\n");
- fputs(errline,stderr);
- exit(1);
- }
+ if ((pass = getpwnam(q)) == NULL)
+ errx(1, "Error in config file; unknown user: %s", q);
working->uid = pass->pw_uid;
} else
working->uid = atoi(q);
@@ -357,12 +336,8 @@ struct conf_entry *parse_file()
q = group;
if (*q) {
if (!(isnumberstr(q))) {
- if ((grp = getgrnam(q)) == NULL) {
- fprintf(stderr,
- "Error in config file; unknown group:\n");
- fputs(errline,stderr);
- exit(1);
- }
+ if ((grp = getgrnam(q)) == NULL)
+ errx(1, "Error in config file; unknown group: %s", q);
working->gid = grp->gr_gid;
} else
working->gid = atoi(q);
@@ -371,25 +346,16 @@ struct conf_entry *parse_file()
q = parse = missing_field(sob(++parse),errline);
*(parse = son(parse)) = '\0';
- }
- else
+ } else
working->uid = working->gid = NONE;
- if (!sscanf(q,"%o",&working->permissions)) {
- fprintf(stderr,
- "Error in config file; bad permissions:\n");
- fputs(errline,stderr);
- exit(1);
- }
+ if (!sscanf(q,"%o",&working->permissions))
+ errx(1, "Error in config file; bad permissions: %s", q);
q = parse = missing_field(sob(++parse),errline);
*(parse = son(parse)) = '\0';
- if (!sscanf(q,"%d",&working->numlogs)) {
- fprintf(stderr,
- "Error in config file; bad number:\n");
- fputs(errline,stderr);
- exit(1);
- }
+ if (!sscanf(q,"%d",&working->numlogs))
+ errx(1, "Error in config file; bad number: %s", q);
q = parse = missing_field(sob(++parse),errline);
*(parse = son(parse)) = '\0';
@@ -415,12 +381,8 @@ struct conf_entry *parse_file()
working->flags |= CE_BINARY;
else if ((*q == 'M') || (*q == 'm'))
working->flags |= CE_MONITOR;
- else {
- fprintf(stderr,
- "Illegal flag in config file -- %c\n",
- *q);
- exit(1);
- }
+ else
+ errx(1, "Illegal flag in config file: %c", *q);
q++;
}
@@ -430,11 +392,8 @@ struct conf_entry *parse_file()
*(parse = son(parse)) = '\0';
working->whom = strdup(q);
- if (working->log == NULL) {
- (void) fprintf(stderr,"%s: ",progname);
- perror("strdup()");
- exit(1);
- }
+ if (working->log == NULL)
+ err(1, "strdup");
}
free(errline);
@@ -449,8 +408,9 @@ char *missing_field(p, errline)
char *p,*errline;
{
if (!p || !*p) {
- fprintf(stderr,"Missing field in config file:\n");
- fputs(errline,stderr);
+ fprintf(stderr, "%s: Missing field in config file line:\n",
+ __progname);
+ fputs(errline, stderr);
exit(1);
}
return(p);
@@ -524,20 +484,14 @@ void dotrim(log, numdays, flags, perm, owner_uid, group_gid)
printf("Start new log...");
else {
fd = creat(log,perm);
- if (fd < 0) {
- perror("can't start new log");
- exit(1);
- }
- if (fchown(fd, owner_uid, group_gid)) {
- perror("can't chmod new log file");
- exit(1);
- }
+ if (fd < 0)
+ err(1, "can't start new log");
+ if (fchown(fd, owner_uid, group_gid))
+ err(1, "can't chmod new log file");
(void) close(fd);
if (!(flags & CE_BINARY))
- if (log_trim(log)) { /* Add status message */
- perror("can't add status message to log");
- exit(1);
- }
+ if (log_trim(log)) /* Add status message */
+ err(1, "can't add status message to log");
}
if (noaction)
printf("chmod %o %s...",perm,log);
@@ -545,14 +499,10 @@ void dotrim(log, numdays, flags, perm, owner_uid, group_gid)
(void) chmod(log,perm);
if (noaction)
printf("kill -HUP %d (syslogd)\n",syslog_pid);
- else
- if (syslog_pid < MIN_PID || syslog_pid > MAX_PID) {
- fprintf(stderr,"%s: preposterous process number: %d\n",
- progname, syslog_pid);
- } else if (kill(syslog_pid,SIGHUP)) {
- fprintf(stderr,"%s: ",progname);
- perror("warning - could not restart syslogd");
- }
+ else if (syslog_pid < MIN_PID || syslog_pid > MAX_PID)
+ warnx("preposterous process number: %d", syslog_pid);
+ else if (kill(syslog_pid,SIGHUP))
+ warnx("warning - could not restart syslogd");
if (flags & CE_COMPACT) {
if (noaction)
printf("Compress %s.0\n",log);
@@ -571,8 +521,7 @@ int log_trim(log)
fprintf(f,"%s %s newsyslog[%d]: logfile turned over\n",
daytime, hostname, getpid());
if (fclose(f) == EOF) {
- perror("log_trim: fclose:");
- exit(1);
+ err(1, "log_trim: fclose");
}
return(0);
}
@@ -587,14 +536,10 @@ void compress_log(log)
pid = fork();
(void) sprintf(tmp,"%s.0",log);
if (pid < 0) {
- fprintf(stderr,"%s: ",progname);
- perror("fork");
- exit(1);
+ err(1, "fork");
} else if (!pid) {
(void) execl(COMPRESS,"compress","-f",tmp,0);
- fprintf(stderr,"%s: ",progname);
- perror(COMPRESS);
- exit(1);
+ err(1, COMPRESS);
}
}
@@ -667,21 +612,17 @@ void domonitor(log, whom)
return;
flog = strdup(log);
- if (flog == NULL) {
- (void) fprintf(stderr,"%s: ",progname);
- perror("strdup()");
- exit(1);
- }
+ if (flog == NULL)
+ err(1, "strdup");
+
for (p = flog; *p != '\0'; p++) {
if (*p == '/')
*p = '_';
}
fname = (char *) malloc(strlen(STATS_DIR) + strlen(flog) + 17);
- if (fname == NULL) {
- (void) fprintf(stderr,"%s: ",progname);
- perror("malloc()");
- exit(1);
- }
+ if (fname == NULL)
+ err(1, "malloc");
+
sprintf(fname, "%s/newsyslog.%s.size", STATS_DIR, flog);
/* ..if it doesn't exist, simply record the current size. */
@@ -690,8 +631,7 @@ void domonitor(log, whom)
fp = fopen(fname, "r");
if (fp == NULL) {
- (void) fprintf(stderr,"%s: ",progname);
- perror(fname);
+ warn(fname);
goto cleanup;
}
#ifdef QUAD_OFF_T
@@ -712,24 +652,19 @@ void domonitor(log, whom)
/* Now see if current size is larger. */
if (sb.st_size > osize) {
rb = (char *) malloc(sb.st_size - osize);
- if (rb == NULL) {
- (void) fprintf(stderr,"%s: ",progname);
- perror("malloc()");
- exit(1);
- }
+ if (rb == NULL)
+ err(1, "malloc");
/* Open logfile, seek. */
fp = fopen(log, "r");
if (fp == NULL) {
- (void) fprintf(stderr,"%s: ",progname);
- perror(log);
+ warn(log);
goto cleanup;
}
fseek(fp, osize, SEEK_SET);
rd = fread(rb, 1, sb.st_size - osize, fp);
if (rd < 1) {
- (void) fprintf(stderr,"%s: ",progname);
- perror("fread()");
+ warn("fread");
fclose(fp);
goto cleanup;
}
@@ -739,8 +674,7 @@ void domonitor(log, whom)
fp = openmail();
if (fp == NULL) {
- (void) fprintf(stderr,"%s: ",progname);
- perror("openmail()");
+ warn("openmail");
goto cleanup;
}
fprintf(fp, "To: %s\nSubject: LOGFILE NOTIFICATION: %s\n\n\n",
@@ -754,8 +688,7 @@ update:
/* Reopen for writing and update file. */
fp = fopen(fname, "w");
if (fp == NULL) {
- (void) fprintf(stderr,"%s: ",progname);
- perror(fname);
+ warn(fname);
goto cleanup;
}
#ifdef QUAD_OFF_T