=================================================================== RCS file: /cvsrepo/anoncvs/cvs/src/usr.bin/newsyslog/newsyslog.c,v retrieving revision 1.10 retrieving revision 1.11 diff -u -r1.10 -r1.11 --- src/usr.bin/newsyslog/newsyslog.c 1997/07/07 22:50:56 1.10 +++ src/usr.bin/newsyslog/newsyslog.c 1997/07/08 04:26:00 1.11 @@ -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 @@ */ #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 @@ #include #include #include +#include #define kbytes(size) (((size) + 1023) >> 10) @@ -119,7 +120,8 @@ 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 @@ 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 @@ 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 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 @@ 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 @@ *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 @@ 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 @@ 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 @@ 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 @@ *(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 *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 @@ 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) 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ /* 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 @@ 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 @@ /* 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