Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 15 Jan 1996 08:50:56 +0100
From:      "Philippe Charnier" <charnier@lirmm.fr>
To:        bugs@freebsd.org
Subject:   newsyslog
Message-ID:  <199601150750.IAA05190@lirmm.lirmm.fr>

next in thread | raw e-mail | index | archive | help


Hi,


You will find a set of patches/cosmetics/stylistics changes for
newsyslog:
1) correct MAX_PID according to system definition.
2) replace fprintf(stderr,...)/perror/exit by 4.4 equivalence.
3) make strings enough larger so they can't overflow: MAXHOSTNAMELEN for
   host and MAXPATHLEN for filenames.
4) call execl with correct parameters (compress -> gzip).



cvs diff: Diffing .
Index: Makefile
===================================================================
RCS file: /home2h/FreeBSD.cvsroot/src/usr.sbin/newsyslog/Makefile,v
retrieving revision 1.1.1.1
diff -u -r1.1.1.1 Makefile
--- Makefile	1996/01/05 09:28:11	1.1.1.1
+++ Makefile	1996/01/13 23:28:14
@@ -5,7 +5,8 @@
 CFLAGS+= -DOSF
 CFLAGS+= -DCONF=\"/etc/newsyslog.conf\"
 CFLAGS+= -DPIDFILE=\"/var/run/syslog.pid\"
-CFLAGS+= -DCOMPRESS=\"/usr/bin/gzip\"
+CFLAGS+= -DCOMPRESS_PATH=\"/usr/bin/gzip\"
+CFLAGS+= -DCOMPRESS_PROG=\"gzip\"
 CFLAGS+= -DCOMPRESS_POSTFIX=\".gz\"
 
 BINOWN=	root
Index: newsyslog.c
===================================================================
RCS file: /home2h/FreeBSD.cvsroot/src/usr.sbin/newsyslog/newsyslog.c,v
retrieving revision 1.2
diff -u -r1.2 newsyslog.c
--- newsyslog.c	1996/01/09 08:40:08	1.2
+++ newsyslog.c	1996/01/13 23:38:36
@@ -38,8 +38,11 @@
 #ifndef PIDFILE
 #define PIDFILE "/etc/syslog.pid"
 #endif
-#ifndef COMPRESS
-#define COMPRESS "/usr/ucb/compress" /* File compression program */
+#ifndef COMPRESS_PATH
+#define COMPRESS_PATH "/usr/ucb/compress" /* File compression program */
+#endif
+#ifndef COMPRESS_PROG
+#define COMPRESS_PROG "compress"
 #endif
 #ifndef COMPRESS_POSTFIX
 #define COMPRESS_POSTFIX ".Z"
@@ -65,7 +68,7 @@
 #endif
 
 #define CE_COMPACT 1            /* Compact the achived log files */
-#define CE_BINARY 2             /* Logfile is in binary, don't add */
+#define CE_BINARY  2            /* Logfile is in binary, don't add */
                                 /* status messages */
 #define NONE -1
         
@@ -95,8 +98,8 @@
 time_t  timenow;
 int     syslog_pid;             /* read in from /etc/syslog.pid */
 #define MIN_PID		3
-#define MAX_PID		65534
-char    hostname[64];           /* hostname */
+#define MAX_PID		30000   /* was 65534, see /usr/include/sys/proc.h */
+char    hostname[MAXHOSTNAMELEN+1]; /* hostname */
 char    *daytime;               /* timenow in human readable form */
 
 
@@ -242,11 +245,8 @@
                 f = fopen(conf,"r");
         else
                 f = stdin;
-        if (!f) {
-                (void) fprintf(stderr,"%s: ",progname);
-                perror(conf);
-                exit(1);
-        }
+        if (!f)
+                err(1, "%s", conf);
         while (fgets(line,BUFSIZ,f)) {
                 if ((line[0]== '\n') || (line[0] == '#'))
                         continue;
@@ -269,12 +269,10 @@
                     *group++ = '\0';
                     if (*q) {
                         if (!(isnumber(*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:\n%s",
+                                  errline);
                             working->uid = pass->pw_uid;
                         } else
                             working->uid = atoi(q);
@@ -284,12 +282,10 @@
                     q = group;
                     if (*q) {
                         if (!(isnumber(*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:\n%s",
+                                  errline);
                             working->gid = grp->gr_gid;
                         } else
                             working->gid = atoi(q);
@@ -302,21 +298,15 @@
                 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:\n%s",
+                          errline);
 
                 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:\n%s",
+                          errline);
 
                 q = parse = missing_field(sob(++parse),errline);
                 *(parse = son(parse)) = '\0';
@@ -340,12 +330,8 @@
                                 working->flags |= CE_COMPACT;
                         else if ((*q == 'B') || (*q == 'b'))
                                 working->flags |= CE_BINARY;
-                        else {
-                                fprintf(stderr,
-                                        "Illegal flag in config file -- %c\n",
-                                        *q);
-                                exit(1);
-                        }
+                        else
+                           errx(1, "Illegal flag in config file -- %c", *q);
                         q++;
                 }
                 
@@ -360,11 +346,8 @@
 char *missing_field(p,errline)
         char    *p,*errline;
 {
-        if (!p || !*p) {
-                fprintf(stderr,"Missing field in config file:\n");
-                fputs(errline,stderr);
-                exit(1);
-        }
+        if (!p || !*p)
+            errx(1, "Missing field in config file:\n%s", errline);
         return(p);
 }
 
@@ -376,8 +359,8 @@
         int     owner_uid;
         int     group_gid;
 {
-        char    file1[128], file2[128];
-        char    zfile1[128], zfile2[128];
+        char    file1 [MAXPATHLEN+1], file2 [MAXPATHLEN+1];
+        char    zfile1[MAXPATHLEN+1], zfile2[MAXPATHLEN+1];
         int     fd;
         struct  stat st;
 
@@ -435,20 +418,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);
@@ -458,12 +435,9 @@
                 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");
-                }
+		warnx("preposterous process number: %d", syslog_pid);
+        } else if (kill(syslog_pid,SIGHUP))
+                warn("could not restart syslogd");
         if (flags & CE_COMPACT) {
                 if (noaction)
                         printf("Compress %s.0\n",log);
@@ -481,10 +455,8 @@
                 return(-1);
         fprintf(f,"%s %s newsyslog[%d]: logfile turned over\n",
                 daytime, hostname, getpid());
-        if (fclose(f) == EOF) {
-                perror("log_trim: fclose:");
-                exit(1);
-        }
+        if (fclose(f) == EOF)
+                err(1, "log_trim: fclose:");
         return(0);
 }
 
@@ -497,15 +469,11 @@
         
         pid = fork();
         (void) sprintf(tmp,"%s.0",log);
-        if (pid < 0) {
-                fprintf(stderr,"%s: ",progname);
-                perror("fork");
-                exit(1);
-        } else if (!pid) {
-                (void) execl(COMPRESS,"compress","-f",tmp,0);
-                fprintf(stderr,"%s: ",progname);
-                perror(COMPRESS);
-                exit(1);
+        if (pid < 0)
+                err(1, "fork");
+        else if (!pid) {
+                (void) execl(COMPRESS_PATH,COMPRESS_PROG,"-f",tmp,0);
+                err(1, COMPRESS_PATH);
         }
 }
 
@@ -525,7 +493,7 @@
         char    *file;
 {
         struct stat sb;
-        char tmp[MAXPATHLEN+3];
+        char tmp[MAXPATHLEN+sizeof(".0")+sizeof(COMPRESS_POSTFIX)+1];
 
         (void) strcpy(tmp,file);
         if (stat(strcat(tmp,".0"),&sb) < 0)

--------                                                        --------
Philippe Charnier                                      charnier@lirmm.fr
                               

         LIRMM, 161 rue Ada, 34392 Montpellier cedex 5 -- France
------------------------------------------------------------------------




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199601150750.IAA05190>