Home | History | Annotate | Line # | Download | only in newsyslog
newsyslog.c revision 1.20
      1  1.20        ad /*	$NetBSD: newsyslog.c,v 1.20 1999/10/06 13:26:28 ad Exp $	*/
      2  1.12   thorpej 
      3   1.1       cgd /*
      4   1.1       cgd  * This file contains changes from the Open Software Foundation.
      5   1.1       cgd  */
      6   1.1       cgd 
      7   1.1       cgd /*
      8   1.1       cgd 
      9   1.2       cgd Copyright 1988, 1989 by the Massachusetts Institute of Technology
     10   1.1       cgd 
     11   1.2       cgd Permission to use, copy, modify, and distribute this software
     12   1.2       cgd and its documentation for any purpose and without fee is
     13   1.2       cgd hereby granted, provided that the above copyright notice
     14   1.2       cgd appear in all copies and that both that copyright notice and
     15   1.2       cgd this permission notice appear in supporting documentation,
     16   1.2       cgd and that the names of M.I.T. and the M.I.T. S.I.P.B. not be
     17   1.2       cgd used in advertising or publicity pertaining to distribution
     18   1.2       cgd of the software without specific, written prior permission.
     19   1.2       cgd M.I.T. and the M.I.T. S.I.P.B. make no representations about
     20   1.2       cgd the suitability of this software for any purpose.  It is
     21   1.2       cgd provided "as is" without express or implied warranty.
     22   1.1       cgd 
     23   1.1       cgd */
     24   1.1       cgd 
     25   1.1       cgd /*
     26   1.1       cgd  *      newsyslog - roll over selected logs at the appropriate time,
     27   1.1       cgd  *              keeping the a specified number of backup files around.
     28   1.1       cgd  */
     29   1.1       cgd 
     30  1.13     lukem #include <sys/cdefs.h>
     31   1.5   mycroft #ifndef lint
     32  1.20        ad __RCSID("$NetBSD: newsyslog.c,v 1.20 1999/10/06 13:26:28 ad Exp $");
     33   1.5   mycroft #endif /* not lint */
     34   1.1       cgd 
     35   1.1       cgd #ifndef CONF
     36   1.1       cgd #define CONF "/etc/athena/newsyslog.conf" /* Configuration file */
     37   1.1       cgd #endif
     38   1.1       cgd #ifndef PIDFILE
     39   1.1       cgd #define PIDFILE "/etc/syslog.pid"
     40   1.1       cgd #endif
     41   1.1       cgd #ifndef COMPRESS
     42   1.1       cgd #define COMPRESS "/usr/ucb/compress" /* File compression program */
     43   1.1       cgd #endif
     44   1.3       cgd #ifndef COMPRESS_POSTFIX
     45   1.3       cgd #define COMPRESS_POSTFIX ".Z"
     46   1.3       cgd #endif
     47   1.1       cgd 
     48   1.1       cgd #include <sys/types.h>
     49   1.1       cgd #include <sys/time.h>
     50   1.1       cgd #include <sys/stat.h>
     51   1.1       cgd #include <sys/param.h>
     52   1.1       cgd #include <sys/wait.h>
     53   1.1       cgd 
     54  1.13     lukem #include <ctype.h>
     55  1.13     lukem #include <fcntl.h>
     56  1.13     lukem #include <grp.h>
     57  1.13     lukem #include <pwd.h>
     58  1.13     lukem #include <signal.h>
     59  1.13     lukem #include <stdio.h>
     60  1.13     lukem #include <stdlib.h>
     61  1.13     lukem #include <string.h>
     62  1.14    kleink #include <time.h>
     63  1.13     lukem #include <unistd.h>
     64  1.13     lukem 
     65   1.1       cgd #define kbytes(size)  (((size) + 1023) >> 10)
     66   1.1       cgd #ifdef _IBMR2
     67   1.1       cgd /* Calculates (db * DEV_BSIZE) */
     68   1.1       cgd #define dbtob(db)  ((unsigned)(db) << UBSHIFT)
     69   1.1       cgd #endif
     70   1.1       cgd 
     71   1.1       cgd #define CE_COMPACT 1            /* Compact the achived log files */
     72   1.1       cgd #define CE_BINARY 2             /* Logfile is in binary, don't add */
     73   1.1       cgd                                 /* status messages */
     74   1.1       cgd #define NONE -1
     75   1.1       cgd 
     76   1.1       cgd struct conf_entry {
     77   1.1       cgd         char    *log;           /* Name of the log */
     78   1.1       cgd         int     uid;            /* Owner of log */
     79   1.1       cgd         int     gid;            /* Group of log */
     80   1.1       cgd         int     numlogs;        /* Number of logs to keep */
     81   1.1       cgd         int     size;           /* Size cutoff to trigger trimming the log */
     82   1.1       cgd         int     hours;          /* Hours between log trimming */
     83   1.1       cgd         int     permissions;    /* File permissions on the log */
     84  1.20        ad         int     flags;          /* Flags (CE_COMPACT & CE_BINARY) */
     85  1.20        ad         char	*pidfile;	/* Name of file containing PID to signal */
     86   1.1       cgd         struct conf_entry       *next; /* Linked list pointer */
     87   1.1       cgd };
     88   1.1       cgd 
     89   1.1       cgd char    *progname;              /* contains argv[0] */
     90   1.1       cgd int     verbose = 0;            /* Print out what's going on */
     91   1.1       cgd int     needroot = 1;           /* Root privs are necessary */
     92   1.1       cgd int     noaction = 0;           /* Don't do anything, just show it */
     93   1.1       cgd char    *conf = CONF;           /* Configuration file to use */
     94   1.1       cgd time_t  timenow;
     95   1.1       cgd int     syslog_pid;             /* read in from /etc/syslog.pid */
     96   1.7        pk #define MIN_PID		3
     97   1.7        pk #define MAX_PID		65534
     98  1.15       mrg char    hostname[MAXHOSTNAMELEN + 1];           /* hostname */
     99   1.1       cgd char    *daytime;               /* timenow in human readable form */
    100   1.1       cgd 
    101   1.1       cgd 
    102  1.13     lukem void	PRS __P((int, char **));
    103  1.13     lukem int	age_old_log __P((char *));
    104  1.13     lukem void	compress_log __P((char *));
    105  1.20        ad void	dotrim __P((char *, int, int, int, int, int, char *));
    106  1.13     lukem void	do_entry __P((struct conf_entry *));
    107  1.13     lukem int	isnumber __P((char *));
    108  1.13     lukem int	log_trim __P((char *));
    109  1.13     lukem int	main __P((int, char **));
    110  1.13     lukem char   *missing_field __P((char *, char *));
    111  1.13     lukem struct conf_entry *parse_file __P((void));
    112  1.13     lukem int	sizefile __P((char *));
    113  1.13     lukem char   *sob __P((char *));
    114  1.13     lukem char   *son __P((char *));
    115  1.13     lukem void	usage __P((void));
    116   1.1       cgd 
    117  1.13     lukem int
    118   1.1       cgd main(argc,argv)
    119   1.1       cgd         int argc;
    120   1.1       cgd         char **argv;
    121   1.1       cgd {
    122   1.1       cgd         struct conf_entry *p, *q;
    123   1.1       cgd 
    124   1.1       cgd         PRS(argc,argv);
    125   1.1       cgd         if (needroot && getuid() && geteuid()) {
    126   1.1       cgd                 fprintf(stderr,"%s: must have root privs\n",progname);
    127   1.1       cgd                 exit(1);
    128   1.1       cgd         }
    129   1.1       cgd         p = q = parse_file();
    130   1.1       cgd         while (p) {
    131   1.1       cgd                 do_entry(p);
    132   1.1       cgd                 p=p->next;
    133   1.1       cgd                 free((char *) q);
    134   1.1       cgd                 q=p;
    135   1.1       cgd         }
    136   1.1       cgd         exit(0);
    137   1.1       cgd }
    138   1.1       cgd 
    139  1.13     lukem void
    140   1.1       cgd do_entry(ent)
    141   1.1       cgd         struct conf_entry       *ent;
    142   1.1       cgd {
    143   1.1       cgd         int     size, modtime;
    144   1.1       cgd 
    145   1.1       cgd         if (verbose) {
    146   1.1       cgd                 if (ent->flags & CE_COMPACT)
    147   1.1       cgd                         printf("%s <%dZ>: ",ent->log,ent->numlogs);
    148   1.1       cgd                 else
    149   1.1       cgd                         printf("%s <%d>: ",ent->log,ent->numlogs);
    150   1.1       cgd         }
    151   1.1       cgd         size = sizefile(ent->log);
    152   1.1       cgd         modtime = age_old_log(ent->log);
    153   1.1       cgd         if (size < 0) {
    154   1.1       cgd                 if (verbose)
    155   1.1       cgd                         printf("does not exist.\n");
    156   1.1       cgd         } else {
    157   1.1       cgd                 if (verbose && (ent->size > 0))
    158   1.1       cgd                         printf("size (Kb): %d [%d] ", size, ent->size);
    159   1.1       cgd                 if (verbose && (ent->hours > 0))
    160   1.1       cgd                         printf(" age (hr): %d [%d] ", modtime, ent->hours);
    161   1.1       cgd                 if (((ent->size > 0) && (size >= ent->size)) ||
    162   1.1       cgd                     ((ent->hours > 0) && ((modtime >= ent->hours)
    163   1.1       cgd                                         || (modtime < 0)))) {
    164   1.1       cgd                         if (verbose)
    165   1.1       cgd                                 printf("--> trimming log....\n");
    166   1.1       cgd                         if (noaction && !verbose) {
    167   1.1       cgd                                 if (ent->flags & CE_COMPACT)
    168   1.1       cgd                                         printf("%s <%dZ>: trimming",
    169   1.1       cgd                                                ent->log,ent->numlogs);
    170   1.1       cgd                                 else
    171   1.1       cgd                                         printf("%s <%d>: trimming",
    172   1.1       cgd                                                ent->log,ent->numlogs);
    173   1.1       cgd                         }
    174   1.1       cgd                         dotrim(ent->log, ent->numlogs, ent->flags,
    175  1.20        ad                                ent->permissions, ent->uid, ent->gid,
    176  1.20        ad                                ent->pidfile);
    177   1.1       cgd                 } else {
    178   1.1       cgd                         if (verbose)
    179   1.1       cgd                                 printf("--> skipping\n");
    180   1.1       cgd                 }
    181   1.1       cgd         }
    182   1.1       cgd }
    183   1.1       cgd 
    184  1.13     lukem void
    185   1.1       cgd PRS(argc,argv)
    186   1.1       cgd         int argc;
    187   1.1       cgd         char **argv;
    188   1.1       cgd {
    189   1.1       cgd         int     c;
    190   1.1       cgd         FILE    *f;
    191   1.1       cgd         char    line[BUFSIZ];
    192   1.8       jtc 	char	*p;
    193   1.1       cgd 
    194   1.1       cgd         progname = argv[0];
    195   1.1       cgd         timenow = time((time_t *) 0);
    196   1.8       jtc         daytime = ctime(&timenow) + 4;
    197  1.10        pk         daytime[15] = '\0';
    198   1.1       cgd 
    199   1.1       cgd         /* Let's find the pid of syslogd */
    200   1.1       cgd         syslog_pid = 0;
    201   1.1       cgd         f = fopen(PIDFILE,"r");
    202   1.1       cgd         if (f && fgets(line,BUFSIZ,f))
    203   1.1       cgd                 syslog_pid = atoi(line);
    204   1.7        pk 	if (f)
    205   1.7        pk 		(void)fclose(f);
    206   1.1       cgd 
    207   1.1       cgd         /* Let's get our hostname */
    208  1.15       mrg         (void)gethostname(hostname, sizeof(hostname));
    209  1.15       mrg 	hostname[sizeof(hostname) - 1] = '\0';
    210   1.8       jtc 
    211   1.8       jtc 	/* Truncate domain */
    212  1.13     lukem 	if ((p = strchr(hostname, '.')) != NULL) {
    213   1.8       jtc 		*p = '\0';
    214   1.8       jtc 	}
    215   1.1       cgd 
    216   1.1       cgd         optind = 1;             /* Start options parsing */
    217  1.13     lukem         while ((c=getopt(argc,argv,"nrvf:t:")) != -1)
    218   1.1       cgd                 switch (c) {
    219   1.1       cgd                 case 'n':
    220   1.1       cgd                         noaction++; /* This implies needroot as off */
    221   1.1       cgd                         /* fall through */
    222   1.1       cgd                 case 'r':
    223   1.1       cgd                         needroot = 0;
    224   1.1       cgd                         break;
    225   1.1       cgd                 case 'v':
    226   1.1       cgd                         verbose++;
    227   1.1       cgd                         break;
    228   1.1       cgd                 case 'f':
    229   1.1       cgd                         conf = optarg;
    230   1.1       cgd                         break;
    231   1.1       cgd                 default:
    232   1.1       cgd                         usage();
    233   1.1       cgd                 }
    234  1.13     lukem }
    235   1.1       cgd 
    236  1.13     lukem void
    237   1.1       cgd usage()
    238   1.1       cgd {
    239   1.1       cgd         fprintf(stderr,
    240   1.9       jtc                 "Usage: %s <-nrv> <-f config-file>\n", progname);
    241   1.1       cgd         exit(1);
    242   1.1       cgd }
    243   1.1       cgd 
    244   1.1       cgd /* Parse a configuration file and return a linked list of all the logs
    245   1.1       cgd  * to process
    246   1.1       cgd  */
    247  1.13     lukem struct conf_entry *
    248  1.13     lukem parse_file()
    249   1.1       cgd {
    250   1.1       cgd         FILE    *f;
    251   1.1       cgd         char    line[BUFSIZ], *parse, *q;
    252   1.1       cgd         char    *errline, *group;
    253   1.1       cgd         struct conf_entry *first = NULL;
    254   1.1       cgd         struct conf_entry *working;
    255   1.1       cgd         struct passwd *pass;
    256   1.1       cgd         struct group *grp;
    257   1.1       cgd 
    258  1.13     lukem 	working = NULL;
    259   1.1       cgd         if (strcmp(conf,"-"))
    260   1.1       cgd                 f = fopen(conf,"r");
    261   1.1       cgd         else
    262   1.1       cgd                 f = stdin;
    263   1.1       cgd         if (!f) {
    264   1.1       cgd                 (void) fprintf(stderr,"%s: ",progname);
    265   1.1       cgd                 perror(conf);
    266   1.1       cgd                 exit(1);
    267   1.1       cgd         }
    268   1.1       cgd         while (fgets(line,BUFSIZ,f)) {
    269   1.1       cgd                 if ((line[0]== '\n') || (line[0] == '#'))
    270   1.1       cgd                         continue;
    271   1.1       cgd                 errline = strdup(line);
    272   1.1       cgd                 if (!first) {
    273   1.1       cgd                         working = (struct conf_entry *) malloc(sizeof(struct conf_entry));
    274   1.1       cgd                         first = working;
    275   1.1       cgd                 } else {
    276   1.1       cgd                         working->next = (struct conf_entry *) malloc(sizeof(struct conf_entry));
    277   1.1       cgd                         working = working->next;
    278   1.1       cgd                 }
    279   1.1       cgd 
    280   1.1       cgd                 q = parse = missing_field(sob(line),errline);
    281   1.1       cgd                 *(parse = son(line)) = '\0';
    282   1.1       cgd                 working->log = strdup(q);
    283   1.1       cgd 
    284   1.1       cgd                 q = parse = missing_field(sob(++parse),errline);
    285   1.1       cgd                 *(parse = son(parse)) = '\0';
    286  1.19        ad                 if ((group = strchr(q, ':')) != NULL || (group = strchr(q, '.')) != NULL) {
    287   1.1       cgd                     *group++ = '\0';
    288   1.1       cgd                     if (*q) {
    289   1.1       cgd                         if (!(isnumber(q))) {
    290   1.1       cgd                             if ((pass = getpwnam(q)) == NULL) {
    291   1.1       cgd                                 fprintf(stderr,
    292   1.1       cgd                                     "Error in config file; unknown user:\n");
    293   1.1       cgd                                 fputs(errline,stderr);
    294   1.1       cgd                                 exit(1);
    295   1.1       cgd                             }
    296   1.1       cgd                             working->uid = pass->pw_uid;
    297   1.1       cgd                         } else
    298   1.1       cgd                             working->uid = atoi(q);
    299   1.1       cgd                     } else
    300   1.1       cgd                         working->uid = NONE;
    301   1.1       cgd 
    302   1.1       cgd                     q = group;
    303   1.1       cgd                     if (*q) {
    304   1.1       cgd                         if (!(isnumber(q))) {
    305   1.1       cgd                             if ((grp = getgrnam(q)) == NULL) {
    306   1.1       cgd                                 fprintf(stderr,
    307   1.1       cgd                                     "Error in config file; unknown group:\n");
    308   1.1       cgd                                 fputs(errline,stderr);
    309   1.1       cgd                                 exit(1);
    310   1.1       cgd                             }
    311   1.1       cgd                             working->gid = grp->gr_gid;
    312   1.1       cgd                         } else
    313   1.1       cgd                             working->gid = atoi(q);
    314   1.1       cgd                     } else
    315   1.1       cgd                         working->gid = NONE;
    316   1.1       cgd 
    317   1.1       cgd                     q = parse = missing_field(sob(++parse),errline);
    318   1.1       cgd                     *(parse = son(parse)) = '\0';
    319   1.1       cgd                 }
    320   1.1       cgd                 else
    321   1.1       cgd                     working->uid = working->gid = NONE;
    322   1.1       cgd 
    323   1.1       cgd                 if (!sscanf(q,"%o",&working->permissions)) {
    324   1.1       cgd                         fprintf(stderr,
    325   1.1       cgd                                 "Error in config file; bad permissions:\n");
    326   1.1       cgd                         fputs(errline,stderr);
    327   1.1       cgd                         exit(1);
    328   1.1       cgd                 }
    329   1.1       cgd 
    330   1.1       cgd                 q = parse = missing_field(sob(++parse),errline);
    331   1.1       cgd                 *(parse = son(parse)) = '\0';
    332   1.1       cgd                 if (!sscanf(q,"%d",&working->numlogs)) {
    333   1.1       cgd                         fprintf(stderr,
    334   1.1       cgd                                 "Error in config file; bad number:\n");
    335   1.1       cgd                         fputs(errline,stderr);
    336   1.1       cgd                         exit(1);
    337   1.1       cgd                 }
    338   1.1       cgd 
    339   1.1       cgd                 q = parse = missing_field(sob(++parse),errline);
    340   1.1       cgd                 *(parse = son(parse)) = '\0';
    341  1.17  christos                 if (isdigit((unsigned char)*q))
    342   1.1       cgd                         working->size = atoi(q);
    343   1.1       cgd                 else
    344   1.1       cgd                         working->size = -1;
    345   1.1       cgd 
    346   1.1       cgd                 q = parse = missing_field(sob(++parse),errline);
    347   1.1       cgd                 *(parse = son(parse)) = '\0';
    348  1.17  christos                 if (isdigit((unsigned char)*q))
    349   1.1       cgd                         working->hours = atoi(q);
    350   1.1       cgd                 else
    351   1.1       cgd                         working->hours = -1;
    352   1.1       cgd 
    353   1.1       cgd                 q = parse = sob(++parse); /* Optional field */
    354   1.1       cgd                 *(parse = son(parse)) = '\0';
    355   1.1       cgd                 working->flags = 0;
    356  1.17  christos                 while (q && *q && !isspace((unsigned char)*q)) {
    357   1.1       cgd                         if ((*q == 'Z') || (*q == 'z'))
    358   1.1       cgd                                 working->flags |= CE_COMPACT;
    359   1.1       cgd                         else if ((*q == 'B') || (*q == 'b'))
    360   1.1       cgd                                 working->flags |= CE_BINARY;
    361   1.1       cgd                         else {
    362   1.1       cgd                                 fprintf(stderr,
    363   1.1       cgd                                         "Illegal flag in config file -- %c\n",
    364   1.1       cgd                                         *q);
    365   1.1       cgd                                 exit(1);
    366   1.1       cgd                         }
    367   1.1       cgd                         q++;
    368   1.1       cgd                 }
    369  1.20        ad 
    370  1.20        ad                 if ((q = parse = sob(++parse)) != NULL && q[0] != '\0') {
    371  1.20        ad                 	*(parse = son(parse)) = '\0';
    372  1.20        ad                		working->pidfile = strdup(q);
    373  1.20        ad                	} else
    374  1.20        ad                		working->pidfile = NULL;
    375   1.1       cgd 
    376   1.1       cgd                 free(errline);
    377   1.1       cgd         }
    378   1.1       cgd         if (working)
    379   1.1       cgd                 working->next = (struct conf_entry *) NULL;
    380   1.1       cgd         (void) fclose(f);
    381   1.1       cgd         return(first);
    382   1.1       cgd }
    383   1.1       cgd 
    384  1.13     lukem char *
    385  1.13     lukem missing_field(p,errline)
    386   1.1       cgd         char    *p,*errline;
    387   1.1       cgd {
    388   1.1       cgd         if (!p || !*p) {
    389   1.1       cgd                 fprintf(stderr,"Missing field in config file:\n");
    390   1.1       cgd                 fputs(errline,stderr);
    391   1.1       cgd                 exit(1);
    392   1.1       cgd         }
    393   1.1       cgd         return(p);
    394   1.1       cgd }
    395   1.1       cgd 
    396  1.13     lukem void
    397  1.20        ad dotrim(log,numdays,flags,perm,owner_uid,group_gid,pidfile)
    398   1.1       cgd         char    *log;
    399   1.1       cgd         int     numdays;
    400   1.1       cgd         int     flags;
    401   1.1       cgd         int     perm;
    402   1.1       cgd         int     owner_uid;
    403   1.1       cgd         int     group_gid;
    404  1.20        ad         char	*pidfile;
    405   1.1       cgd {
    406   1.1       cgd         char    file1[128], file2[128];
    407   1.1       cgd         char    zfile1[128], zfile2[128];
    408  1.20        ad 	char    line[BUFSIZ];
    409   1.1       cgd         int     fd;
    410   1.1       cgd         struct  stat st;
    411  1.11   thorpej         int     ngen = numdays;
    412  1.20        ad         FILE    *f;
    413  1.20        ad         pid_t   pid;
    414   1.1       cgd 
    415   1.1       cgd #ifdef _IBMR2
    416   1.1       cgd /* AIX 3.1 has a broken fchown- if the owner_uid is -1, it will actually */
    417   1.1       cgd /* change it to be owned by uid -1, instead of leaving it as is, as it is */
    418   1.1       cgd /* supposed to. */
    419   1.1       cgd                 if (owner_uid == -1)
    420   1.1       cgd                   owner_uid = geteuid();
    421   1.1       cgd #endif
    422   1.1       cgd 
    423  1.20        ad         if (pidfile != NULL && pidfile[0] != '\0') {
    424  1.20        ad        		if ((f = fopen(pidfile,"r")) == NULL) {
    425  1.20        ad        			(void) fprintf(stderr,"%s: ",progname);
    426  1.20        ad                 	perror(conf);
    427  1.20        ad                 	return;
    428  1.20        ad                 }
    429  1.20        ad 
    430  1.20        ad        		if (fgets(line,BUFSIZ,f))
    431  1.20        ad        	        	pid = atoi(line);
    432  1.20        ad 		if (f)
    433  1.20        ad 			(void)fclose(f);
    434  1.20        ad 	} else
    435  1.20        ad 		pid = syslog_pid;
    436  1.20        ad 
    437   1.1       cgd         /* Remove oldest log */
    438   1.1       cgd         (void) sprintf(file1,"%s.%d",log,numdays);
    439   1.1       cgd         (void) strcpy(zfile1, file1);
    440   1.3       cgd         (void) strcat(zfile1, COMPRESS_POSTFIX);
    441   1.1       cgd 
    442   1.1       cgd         if (noaction) {
    443   1.1       cgd                 printf("rm -f %s\n", file1);
    444   1.1       cgd                 printf("rm -f %s\n", zfile1);
    445   1.1       cgd         } else {
    446   1.1       cgd                 (void) unlink(file1);
    447   1.1       cgd                 (void) unlink(zfile1);
    448   1.1       cgd         }
    449   1.1       cgd 
    450   1.1       cgd         /* Move down log files */
    451   1.1       cgd         while (numdays--) {
    452   1.1       cgd                 (void) strcpy(file2,file1);
    453   1.1       cgd                 (void) sprintf(file1,"%s.%d",log,numdays);
    454   1.1       cgd                 (void) strcpy(zfile1, file1);
    455   1.1       cgd                 (void) strcpy(zfile2, file2);
    456   1.1       cgd                 if (lstat(file1, &st)) {
    457   1.3       cgd                         (void) strcat(zfile1, COMPRESS_POSTFIX);
    458   1.3       cgd                         (void) strcat(zfile2, COMPRESS_POSTFIX);
    459   1.1       cgd                         if (lstat(zfile1, &st)) continue;
    460   1.1       cgd                 }
    461   1.1       cgd                 if (noaction) {
    462   1.1       cgd                         printf("mv %s %s\n",zfile1,zfile2);
    463   1.1       cgd                         printf("chmod %o %s\n", perm, zfile2);
    464   1.1       cgd                         printf("chown %d.%d %s\n",
    465   1.1       cgd                                owner_uid, group_gid, zfile2);
    466   1.1       cgd                 } else {
    467   1.1       cgd                         (void) rename(zfile1, zfile2);
    468   1.1       cgd                         (void) chmod(zfile2, perm);
    469   1.1       cgd                         (void) chown(zfile2, owner_uid, group_gid);
    470   1.1       cgd                 }
    471   1.1       cgd         }
    472   1.1       cgd         if (!noaction && !(flags & CE_BINARY))
    473   1.1       cgd                 (void) log_trim(log);  /* Report the trimming to the old log */
    474   1.1       cgd 
    475  1.17  christos 	if (ngen == 0) {
    476  1.11   thorpej 		if (noaction)
    477  1.11   thorpej 			printf("rm %s\n",log);
    478  1.11   thorpej 		else
    479  1.11   thorpej 			(void) unlink(log);
    480  1.17  christos 	} else
    481  1.11   thorpej 		if (noaction)
    482  1.11   thorpej 			printf("mv %s to %s\n",log,file1);
    483  1.11   thorpej 		else
    484  1.11   thorpej 			(void) rename(log,file1);
    485  1.11   thorpej 
    486  1.11   thorpej         if (noaction)
    487   1.1       cgd                 printf("Start new log...");
    488   1.1       cgd         else {
    489   1.1       cgd                 fd = creat(log,perm);
    490   1.1       cgd                 if (fd < 0) {
    491   1.1       cgd                         perror("can't start new log");
    492   1.1       cgd                         exit(1);
    493   1.1       cgd                 }
    494   1.1       cgd                 if (fchown(fd, owner_uid, group_gid)) {
    495   1.1       cgd                         perror("can't chmod new log file");
    496   1.1       cgd                         exit(1);
    497   1.1       cgd                 }
    498   1.1       cgd                 (void) close(fd);
    499   1.1       cgd                 if (!(flags & CE_BINARY))
    500   1.1       cgd                         if (log_trim(log)) {    /* Add status message */
    501   1.1       cgd                                 perror("can't add status message to log");
    502   1.1       cgd                                 exit(1);
    503   1.1       cgd                         }
    504   1.1       cgd         }
    505   1.1       cgd         if (noaction)
    506   1.1       cgd                 printf("chmod %o %s...",perm,log);
    507   1.1       cgd         else
    508   1.1       cgd                 (void) chmod(log,perm);
    509   1.1       cgd         if (noaction)
    510  1.20        ad                 printf("kill -HUP %d\n",pid);
    511   1.1       cgd         else
    512  1.20        ad 	if (pid < MIN_PID || pid > MAX_PID) {
    513   1.7        pk 		fprintf(stderr,"%s: preposterous process number: %d\n",
    514  1.20        ad 				progname, pid);
    515  1.20        ad         } else if (kill(pid,SIGHUP)) {
    516   1.1       cgd                         fprintf(stderr,"%s: ",progname);
    517  1.20        ad                         perror("warning - could not restart daemon");
    518   1.1       cgd                 }
    519   1.1       cgd         if (flags & CE_COMPACT) {
    520   1.1       cgd                 if (noaction)
    521   1.1       cgd                         printf("Compress %s.0\n",log);
    522   1.1       cgd                 else
    523   1.1       cgd                         compress_log(log);
    524   1.1       cgd         }
    525   1.1       cgd }
    526   1.1       cgd 
    527   1.1       cgd /* Log the fact that the logs were turned over */
    528  1.13     lukem int
    529   1.1       cgd log_trim(log)
    530   1.1       cgd         char    *log;
    531   1.1       cgd {
    532   1.1       cgd         FILE    *f;
    533   1.1       cgd         if ((f = fopen(log,"a")) == NULL)
    534   1.1       cgd                 return(-1);
    535  1.17  christos         fprintf(f,"%s %s newsyslog[%ld]: logfile turned over\n",
    536  1.17  christos                 daytime, hostname, (u_long)getpid());
    537   1.1       cgd         if (fclose(f) == EOF) {
    538  1.18     soren                 perror("log_trim: fclose");
    539   1.1       cgd                 exit(1);
    540   1.1       cgd         }
    541   1.1       cgd         return(0);
    542   1.1       cgd }
    543   1.1       cgd 
    544   1.1       cgd /* Fork of /usr/ucb/compress to compress the old log file */
    545  1.13     lukem void
    546   1.1       cgd compress_log(log)
    547   1.1       cgd         char    *log;
    548   1.1       cgd {
    549   1.1       cgd         int     pid;
    550   1.1       cgd         char    tmp[128];
    551   1.1       cgd 
    552   1.1       cgd         pid = fork();
    553   1.1       cgd         (void) sprintf(tmp,"%s.0",log);
    554   1.1       cgd         if (pid < 0) {
    555   1.1       cgd                 fprintf(stderr,"%s: ",progname);
    556   1.1       cgd                 perror("fork");
    557   1.1       cgd                 exit(1);
    558   1.1       cgd         } else if (!pid) {
    559   1.1       cgd                 (void) execl(COMPRESS,"compress","-f",tmp,0);
    560   1.1       cgd                 fprintf(stderr,"%s: ",progname);
    561   1.1       cgd                 perror(COMPRESS);
    562   1.1       cgd                 exit(1);
    563   1.1       cgd         }
    564   1.1       cgd }
    565   1.1       cgd 
    566   1.1       cgd /* Return size in kilobytes of a file */
    567  1.13     lukem int
    568  1.13     lukem sizefile(file)
    569   1.1       cgd         char    *file;
    570   1.1       cgd {
    571   1.1       cgd         struct stat sb;
    572   1.1       cgd 
    573   1.1       cgd         if (stat(file,&sb) < 0)
    574   1.1       cgd                 return(-1);
    575   1.1       cgd         return(kbytes(dbtob(sb.st_blocks)));
    576   1.1       cgd }
    577   1.1       cgd 
    578   1.1       cgd /* Return the age of old log file (file.0) */
    579  1.13     lukem int
    580  1.13     lukem age_old_log(file)
    581   1.1       cgd         char    *file;
    582   1.1       cgd {
    583   1.1       cgd         struct stat sb;
    584   1.7        pk         char tmp[MAXPATHLEN+3];
    585   1.1       cgd 
    586   1.1       cgd         (void) strcpy(tmp,file);
    587   1.1       cgd         if (stat(strcat(tmp,".0"),&sb) < 0)
    588   1.3       cgd             if (stat(strcat(tmp,COMPRESS_POSTFIX), &sb) < 0)
    589   1.1       cgd                 return(-1);
    590   1.1       cgd         return( (int) (timenow - sb.st_mtime + 1800) / 3600);
    591   1.1       cgd }
    592   1.1       cgd 
    593   1.1       cgd /* Skip Over Blanks */
    594   1.1       cgd char *sob(p)
    595  1.13     lukem         char   *p;
    596   1.1       cgd {
    597  1.17  christos         while (p && *p && isspace((unsigned char)*p))
    598   1.1       cgd                 p++;
    599   1.1       cgd         return(p);
    600   1.1       cgd }
    601   1.1       cgd 
    602   1.1       cgd /* Skip Over Non-Blanks */
    603  1.13     lukem char *
    604  1.13     lukem son(p)
    605  1.13     lukem         char   *p;
    606   1.1       cgd {
    607  1.17  christos         while (p && *p && !isspace((unsigned char)*p))
    608   1.1       cgd                 p++;
    609   1.1       cgd         return(p);
    610   1.1       cgd }
    611   1.1       cgd 
    612   1.1       cgd 
    613   1.1       cgd /* Check if string is actually a number */
    614   1.1       cgd 
    615  1.13     lukem int
    616   1.1       cgd isnumber(string)
    617  1.13     lukem 	char *string;
    618   1.1       cgd {
    619   1.1       cgd         while (*string != '\0') {
    620   1.1       cgd             if (*string < '0' || *string > '9') return(0);
    621   1.1       cgd             string++;
    622   1.1       cgd         }
    623   1.1       cgd         return(1);
    624   1.1       cgd }
    625