[BACK]Return to newsyslog.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / newsyslog

Annotation of src/usr.bin/newsyslog/newsyslog.c, Revision 1.60

1.60    ! millert     1: /*     $OpenBSD: newsyslog.c,v 1.59 2002/11/27 22:56:16 millert Exp $  */
1.10      downsj      2:
                      3: /*
1.48      millert     4:  * Copyright (c) 1999, 2002 Todd C. Miller <Todd.Miller@courtesan.com>
1.30      millert     5:  * All rights reserved.
                      6:  *
                      7:  * Redistribution and use in source and binary forms, with or without
                      8:  * modification, are permitted provided that the following conditions
                      9:  * are met:
                     10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
                     12:  * 2. Redistributions in binary form must reproduce the above copyright
                     13:  *    notice, this list of conditions and the following disclaimer in the
                     14:  *    documentation and/or other materials provided with the distribution.
                     15:  * 3. The name of the author may not be used to endorse or promote products
                     16:  *    derived from this software without specific prior written permission.
                     17:  *
                     18:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
                     19:  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
                     20:  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
                     21:  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
                     22:  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
                     23:  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
                     24:  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
                     25:  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
                     26:  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
                     27:  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     28:  */
                     29:
                     30: /*
1.10      downsj     31:  * Copyright (c) 1997, Jason Downs.  All rights reserved.
                     32:  *
                     33:  * Redistribution and use in source and binary forms, with or without
                     34:  * modification, are permitted provided that the following conditions
                     35:  * are met:
                     36:  * 1. Redistributions of source code must retain the above copyright
                     37:  *    notice, this list of conditions and the following disclaimer.
                     38:  * 2. Redistributions in binary form must reproduce the above copyright
                     39:  *    notice, this list of conditions and the following disclaimer in the
                     40:  *    documentation and/or other materials provided with the distribution.
                     41:  * 3. All advertising materials mentioning features or use of this software
                     42:  *    must display the following acknowledgement:
                     43:  *      This product includes software developed by Jason Downs for the
                     44:  *      OpenBSD system.
                     45:  * 4. Neither the name(s) of the author(s) nor the name OpenBSD
                     46:  *    may be used to endorse or promote products derived from this software
                     47:  *    without specific prior written permission.
                     48:  *
                     49:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS
                     50:  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
                     51:  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
                     52:  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
                     53:  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
                     54:  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
                     55:  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
                     56:  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     57:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     58:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     59:  * SUCH DAMAGE.
                     60:  */
1.3       deraadt    61:
1.1       deraadt    62: /*
                     63:  * This file contains changes from the Open Software Foundation.
                     64:  */
                     65:
                     66: /*
1.50      millert    67:  * Copyright 1988, 1989 by the Massachusetts Institute of Technology
                     68:  *
                     69:  * Permission to use, copy, modify, and distribute this software
                     70:  * and its documentation for any purpose and without fee is
                     71:  * hereby granted, provided that the above copyright notice
                     72:  * appear in all copies and that both that copyright notice and
                     73:  * this permission notice appear in supporting documentation,
                     74:  * and that the names of M.I.T. and the M.I.T. S.I.P.B. not be
                     75:  * used in advertising or publicity pertaining to distribution
                     76:  * of the software without specific, written prior permission.
                     77:  * M.I.T. and the M.I.T. S.I.P.B. make no representations about
                     78:  * the suitability of this software for any purpose.  It is
                     79:  * provided "as is" without express or implied warranty.
                     80:  */
1.1       deraadt    81:
                     82: /*
                     83:  *      newsyslog - roll over selected logs at the appropriate time,
                     84:  *              keeping the a specified number of backup files around.
                     85:  *
                     86:  */
                     87:
                     88: #ifndef lint
1.60    ! millert    89: static const char rcsid[] = "$OpenBSD: newsyslog.c,v 1.59 2002/11/27 22:56:16 millert Exp $";
1.1       deraadt    90: #endif /* not lint */
                     91:
                     92: #ifndef CONF
1.47      millert    93: #define CONF "/etc/newsyslog.conf" /* Configuration file */
1.1       deraadt    94: #endif
                     95: #ifndef PIDFILE
                     96: #define PIDFILE "/etc/syslog.pid"
                     97: #endif
                     98: #ifndef COMPRESS
1.47      millert    99: #define COMPRESS "/usr/bin/compress" /* File compression program */
1.1       deraadt   100: #endif
                    101: #ifndef COMPRESS_POSTFIX
                    102: #define COMPRESS_POSTFIX ".Z"
                    103: #endif
1.10      downsj    104: #ifndef STATS_DIR
                    105: #define STATS_DIR "/etc"
                    106: #endif
                    107: #ifndef SENDMAIL
                    108: #define SENDMAIL "/usr/lib/sendmail"
                    109: #endif
1.1       deraadt   110:
1.48      millert   111: #include <sys/param.h>
                    112: #include <sys/stat.h>
1.9       downsj    113: #include <sys/time.h>
                    114: #include <sys/wait.h>
1.48      millert   115:
1.1       deraadt   116: #include <ctype.h>
1.48      millert   117: #include <err.h>
                    118: #include <errno.h>
1.9       downsj    119: #include <fcntl.h>
1.1       deraadt   120: #include <grp.h>
1.47      millert   121: #include <limits.h>
1.48      millert   122: #include <pwd.h>
                    123: #include <signal.h>
                    124: #include <stdio.h>
                    125: #include <stdlib.h>
                    126: #include <string.h>
1.9       downsj    127: #include <unistd.h>
1.1       deraadt   128:
1.26      millert   129: #define CE_ROTATED     0x01            /* Log file has been rotated */
                    130: #define CE_COMPACT     0x02            /* Compact the achived log files */
                    131: #define CE_BINARY      0x04            /* Logfile is in binary, don't add */
1.10      downsj    132:                                        /* status messages */
1.26      millert   133: #define CE_MONITOR     0x08            /* Monitory for changes */
1.45      wcobb     134: #define CE_FOLLOW      0x10            /* Follow symbolic links */
1.35      deraadt   135:
1.49      millert   136: #define        MIN_PID         4               /* Don't touch pids lower than this */
1.58      millert   137: #define        MIN_SIZE        256             /* Don't rotate if smaller (in bytes) */
1.49      millert   138:
1.50      millert   139: #define        DPRINTF(x)      do { if (verbose) printf x ; } while (0)
                    140:
1.1       deraadt   141: struct conf_entry {
1.35      deraadt   142:        char    *log;           /* Name of the log */
1.51      millert   143:        char    *logbase;       /* Basename of the log */
                    144:        char    *backdir;       /* Directory in which to store backups */
1.35      deraadt   145:        uid_t   uid;            /* Owner of log */
                    146:        gid_t   gid;            /* Group of log */
                    147:        int     numlogs;        /* Number of logs to keep */
1.58      millert   148:        off_t   size;           /* Size cutoff to trigger trimming the log */
1.35      deraadt   149:        int     hours;          /* Hours between log trimming */
                    150:        int     permissions;    /* File permissions on the log */
1.30      millert   151:        int     signal;         /* Signal to send (defaults to SIGHUP) */
1.35      deraadt   152:        int     flags;          /* Flags (CE_COMPACT & CE_BINARY)  */
1.10      downsj    153:        char    *whom;          /* Whom to notify if logfile changes */
1.30      millert   154:        char    *pidfile;       /* Path to file containg pid to signal */
                    155:        char    *runcmd;        /* Command to run instead of sending a signal */
1.35      deraadt   156:        struct conf_entry *next; /* Linked list pointer */
1.1       deraadt   157: };
                    158:
1.30      millert   159: struct pidinfo {
                    160:        char    *file;
                    161:        int     signal;
                    162: };
                    163:
1.52      millert   164: int    verbose = 0;            /* Print out what's going on */
                    165: int    needroot = 1;           /* Root privs are necessary */
                    166: int    noaction = 0;           /* Don't do anything, just show it */
1.33      millert   167: int    monitormode = 0;        /* Don't do monitoring by default */
1.52      millert   168: int    force = 0;              /* Force the logs to be rotated */
                    169: char   *conf = CONF;           /* Configuration file to use */
                    170: time_t timenow;
                    171: char   hostname[MAXHOSTNAMELEN]; /* hostname */
                    172: char   *daytime;               /* timenow in human readable form */
                    173: char   *arcdir;                /* dir to put archives in (if it exists) */
1.1       deraadt   174:
1.43      millert   175: void do_entry(struct conf_entry *);
1.48      millert   176: void parse_args(int, char **);
1.43      millert   177: void usage(void);
                    178: struct conf_entry *parse_file(int *);
1.60    ! millert   179: char *missing_field(char *, char *, int);
1.51      millert   180: void dotrim(struct conf_entry *);
1.43      millert   181: int log_trim(char *);
1.51      millert   182: void compress_log(struct conf_entry *);
1.58      millert   183: off_t sizefile(char *);
1.51      millert   184: int age_old_log(struct conf_entry *);
1.43      millert   185: char *sob(char *);
                    186: char *son(char *);
                    187: int isnumberstr(char *);
                    188: void domonitor(char *, char *);
                    189: FILE *openmail(void);
                    190: void child_killer(int);
                    191: void run_command(char *);
                    192: void send_signal(char *, int);
1.57      millert   193: char *lstat_log(char *, size_t, int);
                    194: int stat_suffix(char *, size_t, char *, struct stat *, int (*)());
1.1       deraadt   195:
1.25      millert   196: int
1.48      millert   197: main(int argc, char **argv)
1.1       deraadt   198: {
1.53      millert   199:        struct conf_entry *p, *q, *x, *y;
1.30      millert   200:        struct pidinfo *pidlist, *pl;
1.53      millert   201:        char **av;
1.28      millert   202:        int status, listlen;
1.35      deraadt   203:
1.48      millert   204:        parse_args(argc, argv);
1.53      millert   205:        argc -= optind;
                    206:        argv += optind;
                    207:
1.35      deraadt   208:        if (needroot && getuid() && geteuid())
1.11      downsj    209:                errx(1, "You must be root.");
1.53      millert   210:
                    211:        p = parse_file(&listlen);
                    212:        if (argc > 0) {
                    213:                /* Only rotate specified files. */
                    214:                x = y = NULL;
                    215:                listlen = 0;
                    216:                for (av = argv; *av; av++) {
                    217:                        for (q = p; q; q = q->next)
                    218:                                if (strcmp(*av, q->log) == 0) {
                    219:                                        if (x == NULL)
                    220:                                                x = y = q;
                    221:                                        else {
                    222:                                                y->next = q;
                    223:                                                y = q;
                    224:                                        }
                    225:                                        listlen++;
                    226:                                        break;
                    227:                                }
                    228:                        if (q == NULL)
1.60    ! millert   229:                                warnx("%s: %s not found", conf, *av);
1.53      millert   230:                }
                    231:                if (x == NULL)
1.60    ! millert   232:                        errx(1, "%s: no specified log files", conf);
1.53      millert   233:                y->next = NULL;
                    234:                p = x;
                    235:        }
1.26      millert   236:
1.46      aaron     237:        pidlist = (struct pidinfo *)calloc(listlen + 1, sizeof(struct pidinfo));
1.30      millert   238:        if (pidlist == NULL)
1.28      millert   239:                err(1, "calloc");
                    240:
1.53      millert   241:        signal(SIGCHLD, child_killer);
                    242:
1.26      millert   243:        /* Step 1, rotate all log files */
1.53      millert   244:        for (q = p; q; q = q->next)
1.35      deraadt   245:                do_entry(q);
1.26      millert   246:
1.28      millert   247:        /* Step 2, make a list of unique pid files */
1.30      millert   248:        for (q = p, pl = pidlist; q; ) {
1.28      millert   249:                if (q->flags & CE_ROTATED) {
1.30      millert   250:                        struct pidinfo *pltmp;
1.28      millert   251:
1.30      millert   252:                        for (pltmp = pidlist; pltmp < pl; pltmp++) {
1.55      millert   253:                                if ((q->pidfile &&
                    254:                                    strcmp(pltmp->file, q->pidfile) == 0 &&
                    255:                                    pltmp->signal == q->signal) ||
                    256:                                    (q->runcmd &&
1.30      millert   257:                                    strcmp(q->runcmd, pltmp->file) == 0))
1.28      millert   258:                                        break;
1.30      millert   259:                        }
                    260:                        if (pltmp == pl) {      /* unique entry */
                    261:                                if (q->runcmd) {
                    262:                                        pl->file = q->runcmd;
                    263:                                        pl->signal = -1;
                    264:                                } else {
                    265:                                        pl->file = q->pidfile;
                    266:                                        pl->signal = q->signal;
                    267:                                }
1.31      millert   268:                                pl++;
1.30      millert   269:                        }
1.28      millert   270:                }
1.35      deraadt   271:                q = q->next;
                    272:        }
1.26      millert   273:
1.30      millert   274:        /* Step 3, send a signal or run a command */
                    275:        for (pl = pidlist; pl->file; pl++) {
1.56      millert   276:                if (pl->file != NULL) {
                    277:                        if (pl->signal == -1)
                    278:                                run_command(pl->file);
                    279:                        else
                    280:                                send_signal(pl->file, pl->signal);
                    281:                }
1.30      millert   282:        }
1.28      millert   283:        if (!noaction)
                    284:                sleep(5);
                    285:
                    286:        /* Step 4, compress the log.0 file if configured to do so and free */
1.35      deraadt   287:        while (p) {
1.26      millert   288:                if ((p->flags & CE_COMPACT) && (p->flags & CE_ROTATED))
1.51      millert   289:                        compress_log(p);
1.26      millert   290:                q = p;
1.35      deraadt   291:                p = p->next;
                    292:                free(q);
                    293:        }
1.16      millert   294:
                    295:        /* Wait for children to finish, then exit */
                    296:        while (waitpid(-1, &status, 0) != -1)
                    297:                ;
1.35      deraadt   298:        exit(0);
1.1       deraadt   299: }
                    300:
1.25      millert   301: void
1.48      millert   302: do_entry(struct conf_entry *ent)
1.1       deraadt   303: {
1.58      millert   304:        int modtime;
                    305:        off_t size;
1.48      millert   306:        struct stat sb;
1.45      wcobb     307:
1.51      millert   308:        if (lstat(ent->log, &sb) != 0)
1.50      millert   309:                return;
                    310:        if (!S_ISREG(sb.st_mode) &&
                    311:            (!S_ISLNK(sb.st_mode) || !(ent->flags & CE_FOLLOW))) {
                    312:                DPRINTF(("--> not a regular file, skipping\n"));
                    313:                return;
1.45      wcobb     314:        }
1.14      millert   315:
1.50      millert   316:        DPRINTF(("%s <%d%s%s%s>: ", ent->log, ent->numlogs,
1.51      millert   317:            (ent->flags & CE_COMPACT) ? "Z" : "",
                    318:            (ent->flags & CE_BINARY) ? "B" : "",
                    319:            (ent->flags & CE_FOLLOW) ? "F" : ""));
1.50      millert   320:
1.35      deraadt   321:        size = sizefile(ent->log);
1.51      millert   322:        modtime = age_old_log(ent);
1.35      deraadt   323:        if (size < 0) {
1.50      millert   324:                DPRINTF(("does not exist.\n"));
1.35      deraadt   325:        } else {
1.50      millert   326:                if (ent->size > 0)
1.58      millert   327:                        DPRINTF(("size (KB): %.2f [%d] ", size / 1024.0,
                    328:                            (int)(ent->size / 1024)));
1.50      millert   329:                if (ent->hours > 0)
                    330:                        DPRINTF(("age (hr): %d [%d] ", modtime, ent->hours));
1.33      millert   331:                if (monitormode && ent->flags & CE_MONITOR)
1.10      downsj    332:                        domonitor(ent->log, ent->whom);
1.52      millert   333:                if (!monitormode && (force ||
                    334:                    (ent->size > 0 && size >= ent->size) ||
1.49      millert   335:                    (ent->hours > 0 && (modtime >= ent->hours || modtime < 0)
                    336:                    && ((ent->flags & CE_BINARY) || size >= MIN_SIZE)))) {
1.50      millert   337:                        DPRINTF(("--> trimming log....\n"));
1.19      kstailey  338:                        if (noaction && !verbose)
1.50      millert   339:                                printf("%s <%d%s%s%s>\n", ent->log,
                    340:                                    ent->numlogs,
                    341:                                    (ent->flags & CE_COMPACT) ? "Z" : "",
                    342:                                    (ent->flags & CE_BINARY) ? "B" : "",
                    343:                                    (ent->flags & CE_FOLLOW) ? "F" : "");
1.51      millert   344:                        dotrim(ent);
1.26      millert   345:                        ent->flags |= CE_ROTATED;
1.50      millert   346:                } else
                    347:                        DPRINTF(("--> skipping\n"));
1.35      deraadt   348:        }
1.1       deraadt   349: }
                    350:
1.30      millert   351: /* Run the specified command */
                    352: void
1.48      millert   353: run_command(char *cmd)
1.30      millert   354: {
                    355:        if (noaction)
1.55      millert   356:                (void)printf("run %s\n", cmd);
1.30      millert   357:        else
                    358:                system(cmd);
                    359: }
                    360:
                    361: /* Send a signal to the pid specified by pidfile */
1.26      millert   362: void
1.48      millert   363: send_signal(char *pidfile, int signal)
1.26      millert   364: {
1.48      millert   365:        pid_t pid;
                    366:        FILE *f;
                    367:        char line[BUFSIZ], *ep, *err;
1.40      deraadt   368:        unsigned long ulval;
1.26      millert   369:
1.35      deraadt   370:        if ((f = fopen(pidfile, "r")) == NULL) {
1.26      millert   371:                warn("can't open %s", pidfile);
                    372:                return;
                    373:        }
                    374:
1.48      millert   375:        pid = 0;
1.42      millert   376:        errno = 0;
                    377:        err = NULL;
1.40      deraadt   378:        if (fgets(line, sizeof(line), f)) {
                    379:                ulval = strtoul(line, &ep, 10);
1.41      marc      380:                if (line[0] == '\0' || (*ep != '\0' && *ep != '\n'))
1.40      deraadt   381:                        err = "invalid number in";
                    382:                else if (errno == ERANGE && ulval == ULONG_MAX)
                    383:                        err = "out of range number in";
                    384:                else if (ulval == 0)
                    385:                        err = "no number in";
                    386:                else if (ulval < MIN_PID)
                    387:                        err = "preposterous process number in";
                    388:                else
                    389:                        pid = ulval;
1.42      millert   390:        } else {
                    391:                if (errno == 0)
                    392:                        err = "empty";
                    393:                else
                    394:                        err = "error reading";
1.40      deraadt   395:        }
1.26      millert   396:        (void)fclose(f);
                    397:
1.40      deraadt   398:        if (err)
                    399:                warnx("%s pid file: %s", err, pidfile);
                    400:        else if (noaction)
1.44      mpech     401:                (void)printf("kill -%s %ld\n", sys_signame[signal], (long)pid);
1.30      millert   402:        else if (kill(pid, signal))
                    403:                warnx("warning - could not send SIG%s to daemon",
                    404:                    sys_signame[signal]);
1.26      millert   405: }
                    406:
1.25      millert   407: void
1.48      millert   408: parse_args(int argc, char **argv)
1.1       deraadt   409: {
1.48      millert   410:        int ch;
                    411:        char *p;
1.1       deraadt   412:
1.35      deraadt   413:        timenow = time(NULL);
                    414:        daytime = ctime(&timenow) + 4;
                    415:        daytime[15] = '\0';
1.1       deraadt   416:
1.35      deraadt   417:        /* Let's get our hostname */
                    418:        (void)gethostname(hostname, sizeof(hostname));
1.1       deraadt   419:
                    420:        /* Truncate domain */
1.48      millert   421:        if ((p = strchr(hostname, '.')) != NULL)
1.1       deraadt   422:                *p = '\0';
                    423:
1.52      millert   424:        while ((ch = getopt(argc, argv, "Fmnrva:f:")) != -1) {
1.48      millert   425:                switch (ch) {
1.51      millert   426:                case 'a':
                    427:                        arcdir = optarg;
                    428:                        break;
1.35      deraadt   429:                case 'n':
                    430:                        noaction++; /* This implies needroot as off */
                    431:                        /* fall through */
                    432:                case 'r':
                    433:                        needroot = 0;
                    434:                        break;
                    435:                case 'v':
                    436:                        verbose++;
                    437:                        break;
                    438:                case 'f':
                    439:                        conf = optarg;
                    440:                        break;
1.10      downsj    441:                case 'm':
1.33      millert   442:                        monitormode++;
1.10      downsj    443:                        break;
1.52      millert   444:                case 'F':
                    445:                        force++;
                    446:                        break;
1.35      deraadt   447:                default:
                    448:                        usage();
                    449:                }
                    450:        }
1.52      millert   451:        if (monitormode && force)
                    452:                errx(1, "cannot specify both -m and -F flags");
1.9       downsj    453: }
1.1       deraadt   454:
1.25      millert   455: void
1.48      millert   456: usage(void)
1.1       deraadt   457: {
1.25      millert   458:        extern const char *__progname;
                    459:
1.52      millert   460:        (void)fprintf(stderr, "usage: %s [-Fmnrv] [-a directory] "
1.53      millert   461:            "[-f config_file] [log ...]\n", __progname);
1.14      millert   462:        exit(1);
1.1       deraadt   463: }
                    464:
1.52      millert   465: /*
                    466:  * Parse a configuration file and return a linked list of all the logs
1.1       deraadt   467:  * to process
                    468:  */
1.25      millert   469: struct conf_entry *
1.48      millert   470: parse_file(int *nentries)
1.1       deraadt   471: {
1.48      millert   472:        FILE *f;
                    473:        char line[BUFSIZ], *parse, *q, *errline, *group, *tmp;
1.60    ! millert   474:        int lineno;
1.35      deraadt   475:        struct conf_entry *first = NULL;
                    476:        struct conf_entry *working = NULL;
1.48      millert   477:        struct passwd *pwd;
1.35      deraadt   478:        struct group *grp;
1.51      millert   479:        struct stat sb;
1.1       deraadt   480:
1.35      deraadt   481:        if (strcmp(conf, "-") == 0)
                    482:                f = stdin;
1.48      millert   483:        else if ((f = fopen(conf, "r")) == NULL)
                    484:                err(1, "can't open %s", conf);
1.11      downsj    485:
1.28      millert   486:        *nentries = 0;
1.60    ! millert   487:        for (lineno = 0; fgets(line, sizeof(line), f); lineno++) {
1.48      millert   488:                tmp = sob(line);
1.54      millert   489:                if (*tmp == '\0' || *tmp == '#')
1.35      deraadt   490:                        continue;
1.48      millert   491:                errline = strdup(tmp);
1.11      downsj    492:                if (errline == NULL)
                    493:                        err(1, "strdup");
1.28      millert   494:                (*nentries)++;
1.35      deraadt   495:                if (!first) {
                    496:                        working = (struct conf_entry *) malloc(sizeof(struct conf_entry));
1.11      downsj    497:                        if (working == NULL)
                    498:                                err(1, "malloc");
1.35      deraadt   499:                        first = working;
                    500:                } else {
                    501:                        working->next = (struct conf_entry *) malloc(sizeof(struct conf_entry));
1.11      downsj    502:                        if (working->next == NULL)
                    503:                                err(1, "malloc");
1.35      deraadt   504:                        working = working->next;
                    505:                }
1.1       deraadt   506:
1.60    ! millert   507:                q = parse = missing_field(sob(line), errline, lineno);
1.35      deraadt   508:                *(parse = son(line)) = '\0';
                    509:                working->log = strdup(q);
1.11      downsj    510:                if (working->log == NULL)
                    511:                        err(1, "strdup");
1.1       deraadt   512:
1.51      millert   513:                if ((working->logbase = strrchr(working->log, '/')) != NULL)
                    514:                        working->logbase++;
                    515:
1.60    ! millert   516:                q = parse = missing_field(sob(++parse), errline, lineno);
1.35      deraadt   517:                *(parse = son(parse)) = '\0';
1.25      millert   518:                if ((group = strchr(q, '.')) != NULL) {
                    519:                        *group++ = '\0';
                    520:                        if (*q) {
                    521:                                if (!(isnumberstr(q))) {
1.48      millert   522:                                        if ((pwd = getpwnam(q)) == NULL)
1.60    ! millert   523:                                                errx(1, "%s:%d: unknown user: %s",
        !           524:                                                    conf, lineno, q);
1.48      millert   525:                                        working->uid = pwd->pw_uid;
1.25      millert   526:                                } else
                    527:                                        working->uid = atoi(q);
                    528:                        } else
1.50      millert   529:                                working->uid = (uid_t)-1;
1.25      millert   530:
                    531:                        q = group;
                    532:                        if (*q) {
                    533:                                if (!(isnumberstr(q))) {
                    534:                                        if ((grp = getgrnam(q)) == NULL)
1.60    ! millert   535:
        !           536:                                                errx(1, "%s:%d: unknown group: %s",
        !           537:                                                    conf, lineno, q);
1.25      millert   538:                                        working->gid = grp->gr_gid;
                    539:                                } else
                    540:                                        working->gid = atoi(q);
                    541:                        } else
1.50      millert   542:                                working->gid = (gid_t)-1;
1.25      millert   543:
1.60    ! millert   544:                        q = parse = missing_field(sob(++parse), errline, lineno);
1.25      millert   545:                        *(parse = son(parse)) = '\0';
1.50      millert   546:                } else {
                    547:                        working->uid = (uid_t)-1;
                    548:                        working->gid = (gid_t)-1;
                    549:                }
1.1       deraadt   550:
1.35      deraadt   551:                if (!sscanf(q, "%o", &working->permissions))
1.60    ! millert   552:                        errx(1, "%s:%d: bad permissions: %s", conf, lineno, q);
1.1       deraadt   553:
1.60    ! millert   554:                q = parse = missing_field(sob(++parse), errline, lineno);
1.35      deraadt   555:                *(parse = son(parse)) = '\0';
                    556:                if (!sscanf(q, "%d", &working->numlogs) || working->numlogs < 0)
1.60    ! millert   557:                        errx(1, "%s:%d: bad number: %s", conf, lineno, q);
1.1       deraadt   558:
1.60    ! millert   559:                q = parse = missing_field(sob(++parse), errline, lineno);
1.35      deraadt   560:                *(parse = son(parse)) = '\0';
                    561:                if (isdigit(*q))
1.58      millert   562:                        working->size = atoi(q) * 1024;
1.35      deraadt   563:                else
                    564:                        working->size = -1;
                    565:
1.60    ! millert   566:                q = parse = missing_field(sob(++parse), errline, lineno);
1.35      deraadt   567:                *(parse = son(parse)) = '\0';
                    568:                if (isdigit(*q))
                    569:                        working->hours = atoi(q);
                    570:                else
                    571:                        working->hours = -1;
1.1       deraadt   572:
1.35      deraadt   573:                working->flags = 0;
                    574:                q = sob(++parse);       /* Optional field */
1.32      millert   575:                if (*q == 'Z' || *q == 'z' || *q == 'B' || *q == 'b' ||
                    576:                    *q == 'M' || *q == 'm') {
                    577:                        *(parse = son(q)) = '\0';
                    578:                        while (*q) {
                    579:                                switch (*q) {
                    580:                                case 'Z':
                    581:                                case 'z':
                    582:                                        working->flags |= CE_COMPACT;
                    583:                                        break;
                    584:                                case 'B':
                    585:                                case 'b':
                    586:                                        working->flags |= CE_BINARY;
                    587:                                        break;
                    588:                                case 'M':
                    589:                                case 'm':
                    590:                                        working->flags |= CE_MONITOR;
1.45      wcobb     591:                                        break;
                    592:                                case 'F':
                    593:                                case 'f':
                    594:                                        working->flags |= CE_FOLLOW;
1.32      millert   595:                                        break;
                    596:                                default:
1.60    ! millert   597:                                        errx(1, "%s:%d: illegal flag: `%c'",
        !           598:                                            conf, lineno, *q);
1.32      millert   599:                                        break;
                    600:                                }
                    601:                                q++;
                    602:                        }
                    603:                } else
1.40      deraadt   604:                        parse--;        /* no flags so undo */
1.10      downsj    605:
                    606:                working->whom = NULL;
                    607:                if (working->flags & CE_MONITOR) {      /* Optional field */
                    608:                        q = parse = sob(++parse);
                    609:                        *(parse = son(parse)) = '\0';
                    610:
                    611:                        working->whom = strdup(q);
1.11      downsj    612:                        if (working->log == NULL)
                    613:                                err(1, "strdup");
1.10      downsj    614:                }
1.14      millert   615:
                    616:                working->pidfile = PIDFILE;
1.30      millert   617:                working->signal = SIGHUP;
                    618:                working->runcmd = NULL;
                    619:                for (;;) {
                    620:                        q = parse = sob(++parse);       /* Optional field */
                    621:                        if (q == NULL || *q == '\0')
                    622:                                break;
                    623:                        if (*q == '/') {
                    624:                                *(parse = son(parse)) = '\0';
                    625:                                if (strlen(q) >= MAXPATHLEN)
1.60    ! millert   626:                                        errx(1, "%s:%d: pathname too long: %s",
        !           627:                                            conf, lineno, q);
1.30      millert   628:                                working->pidfile = strdup(q);
                    629:                                if (working->pidfile == NULL)
                    630:                                        err(1, "strdup");
                    631:                        } else if (*q == '"' && (tmp = strchr(q + 1, '"'))) {
                    632:                                *(parse = tmp) = '\0';
1.56      millert   633:                                if (*++q != '\0') {
                    634:                                        working->runcmd = strdup(q);
                    635:                                        if (working->runcmd == NULL)
                    636:                                                err(1, "strdup");
                    637:                                }
1.55      millert   638:                                working->pidfile = NULL;
                    639:                                working->signal = -1;
1.30      millert   640:                        } else if (strncmp(q, "SIG", 3) == 0) {
                    641:                                int i;
                    642:
                    643:                                *(parse = son(parse)) = '\0';
                    644:                                for (i = 1; i < NSIG; i++) {
                    645:                                        if (!strcmp(sys_signame[i], q + 3)) {
                    646:                                                working->signal = i;
                    647:                                                break;
                    648:                                        }
                    649:                                }
                    650:                                if (i == NSIG)
1.60    ! millert   651:                                        errx(1, "%s:%d: unknown signal: %s",
        !           652:                                            conf, lineno, q);
1.30      millert   653:                        } else
1.60    ! millert   654:                                errx(1, "%s:%d: unrecognized field: %s",
        !           655:                                    conf, lineno, q);
1.14      millert   656:                }
1.51      millert   657:                free(errline);
                    658:
                    659:                /* If there is an arcdir, set working->backdir. */
                    660:                if (arcdir != NULL && working->logbase != NULL) {
                    661:                        if (*arcdir == '/') {
                    662:                                /* Fully qualified arcdir */
                    663:                                working->backdir = arcdir;
                    664:                        } else {
                    665:                                /* arcdir is relative to log's parent dir */
                    666:                                *(working->logbase - 1) = '\0';
                    667:                                if ((asprintf(&working->backdir, "%s/%s",
                    668:                                    working->log, arcdir)) == -1)
                    669:                                        err(1, "malloc");
                    670:                                *(working->logbase - 1) = '/';
                    671:                        }
                    672:                        /* Ignore arcdir if it doesn't exist. */
                    673:                        if (stat(working->backdir, &sb) != 0 ||
                    674:                            !S_ISDIR(sb.st_mode)) {
                    675:                                if (working->backdir != arcdir)
                    676:                                        free(working->backdir);
                    677:                                working->backdir = NULL;
                    678:                        }
                    679:                } else
                    680:                        working->backdir = NULL;
1.25      millert   681:
                    682:                /* Make sure we can't oflow MAXPATHLEN */
1.51      millert   683:                if (working->backdir != NULL) {
                    684:                        if (snprintf(line, sizeof(line), "%s/%s.%d%s",
                    685:                            working->backdir, working->logbase,
                    686:                            working->numlogs, COMPRESS_POSTFIX) >= MAXPATHLEN)
1.60    ! millert   687:                                errx(1, "%s:%d: pathname too long: %s",
        !           688:                                    conf, lineno, q);
1.51      millert   689:                } else {
                    690:                        if (snprintf(line, sizeof(line), "%s.%d%s",
                    691:                            working->log, working->numlogs, COMPRESS_POSTFIX)
                    692:                            >= MAXPATHLEN)
1.60    ! millert   693:                                errx(1, "%s:%d: pathname too long: %s",
        !           694:                                    conf, lineno, working->log);
1.51      millert   695:                }
1.35      deraadt   696:        }
                    697:        if (working)
                    698:                working->next = NULL;
                    699:        (void)fclose(f);
1.48      millert   700:        return (first);
1.1       deraadt   701: }
                    702:
1.25      millert   703: char *
1.60    ! millert   704: missing_field(char *p, char *errline, int lineno)
1.1       deraadt   705: {
1.60    ! millert   706:        if (p == NULL || *p == '\0') {
        !           707:                warnx("%s:%d: missing field", conf, lineno);
1.35      deraadt   708:                fputs(errline, stderr);
                    709:                exit(1);
                    710:        }
1.48      millert   711:        return (p);
1.1       deraadt   712: }
                    713:
1.25      millert   714: void
1.51      millert   715: dotrim(struct conf_entry *ent)
1.35      deraadt   716: {
                    717:        char    file1[MAXPATHLEN], file2[MAXPATHLEN];
1.57      millert   718:        char    oldlog[MAXPATHLEN], *suffix;
1.35      deraadt   719:        int     fd;
1.51      millert   720:        int     numdays = ent->numlogs;
                    721:
                    722:        /* Is there a separate backup dir? */
                    723:        if (ent->backdir != NULL)
                    724:                snprintf(oldlog, sizeof(oldlog), "%s/%s", ent->backdir,
                    725:                    ent->logbase);
                    726:        else
                    727:                strlcpy(oldlog, ent->log, sizeof(oldlog));
1.1       deraadt   728:
1.35      deraadt   729:        /* Remove oldest log (may not exist) */
1.51      millert   730:        (void)snprintf(file1, sizeof(file1), "%s.%d", oldlog, numdays);
1.57      millert   731:        (void)snprintf(file2, sizeof(file2), "%s.%d%s", oldlog, numdays,
1.35      deraadt   732:            COMPRESS_POSTFIX);
                    733:
                    734:        if (noaction) {
1.57      millert   735:                printf("\trm -f %s %s\n", file1, file2);
1.35      deraadt   736:        } else {
                    737:                (void)unlink(file1);
1.57      millert   738:                (void)unlink(file2);
1.35      deraadt   739:        }
                    740:
                    741:        /* Move down log files */
                    742:        while (numdays--) {
1.57      millert   743:                /*
                    744:                 * If both the compressed archive or the non-compressed archive
                    745:                 * exist, we one or the other based on the CE_COMPACT flag.
                    746:                 */
1.51      millert   747:                (void)snprintf(file1, sizeof(file1), "%s.%d", oldlog, numdays);
1.57      millert   748:                suffix = lstat_log(file1, sizeof(file1), ent->flags);
                    749:                if (suffix == NULL)
                    750:                        continue;
                    751:                (void)snprintf(file2, sizeof(file2), "%s.%d%s", oldlog,
                    752:                    numdays + 1, suffix);
                    753:
1.35      deraadt   754:                if (noaction) {
1.57      millert   755:                        printf("\tmv %s %s\n", file1, file2);
                    756:                        printf("\tchmod %o %s\n", ent->permissions, file2);
1.51      millert   757:                        if (ent->uid != (uid_t)-1 || ent->gid != (gid_t)-1)
1.50      millert   758:                                printf("\tchown %u:%u %s\n",
1.57      millert   759:                                    ent->uid, ent->gid, file2);
1.35      deraadt   760:                } else {
1.57      millert   761:                        if (rename(file1, file2))
                    762:                                warn("can't mv %s to %s", file1, file2);
                    763:                        if (chmod(file2, ent->permissions))
                    764:                                warn("can't chmod %s", file2);
1.51      millert   765:                        if (ent->uid != (uid_t)-1 || ent->gid != (gid_t)-1)
1.57      millert   766:                                if (chown(file2, ent->uid, ent->gid))
                    767:                                        warn("can't chown %s", file2);
1.35      deraadt   768:                }
                    769:        }
1.51      millert   770:        if (!noaction && !(ent->flags & CE_BINARY))
                    771:                (void)log_trim(ent->log);  /* Report the trimming to the old log */
1.1       deraadt   772:
1.51      millert   773:        (void)snprintf(file2, sizeof(file2), "%s.XXXXXXXXXX", ent->log);
1.35      deraadt   774:        if (noaction)  {
1.50      millert   775:                printf("\tmktemp %s\n", file2);
1.35      deraadt   776:        } else {
                    777:                if ((fd = mkstemp(file2)) < 0)
1.26      millert   778:                        err(1, "can't start '%s' log", file2);
1.51      millert   779:                if (ent->uid != (uid_t)-1 || ent->gid != (gid_t)-1)
                    780:                        if (fchown(fd, ent->uid, ent->gid))
                    781:                            err(1, "can't chown '%s' log file", file2);
                    782:                if (fchmod(fd, ent->permissions))
1.26      millert   783:                        err(1, "can't chmod '%s' log file", file2);
1.35      deraadt   784:                (void)close(fd);
1.26      millert   785:                /* Add status message */
1.51      millert   786:                if (!(ent->flags & CE_BINARY) && log_trim(file2))
1.26      millert   787:                        err(1, "can't add status message to log '%s'", file2);
1.35      deraadt   788:        }
1.26      millert   789:
1.51      millert   790:        if (ent->numlogs == 0) {
1.5       deraadt   791:                if (noaction)
1.51      millert   792:                        printf("\trm %s\n", ent->log);
                    793:                else if (unlink(ent->log))
                    794:                        warn("can't rm %s", ent->log);
1.5       deraadt   795:        } else {
1.57      millert   796:                (void)snprintf(file1, sizeof(file1), "%s.0", oldlog);
1.51      millert   797:                if (noaction)
                    798:                        printf("\tmv %s to %s\n", ent->log, file1);
                    799:                else if (rename(ent->log, file1))
1.59      millert   800:                        warn("can't mv %s to %s", ent->log, file1);
1.5       deraadt   801:        }
                    802:
1.26      millert   803:        /* Now move the new log file into place */
                    804:        if (noaction)
1.51      millert   805:                printf("\tmv %s to %s\n", file2, ent->log);
                    806:        else if (rename(file2, ent->log))
1.59      millert   807:                warn("can't mv %s to %s", file2, ent->log);
1.1       deraadt   808: }
                    809:
                    810: /* Log the fact that the logs were turned over */
1.25      millert   811: int
1.48      millert   812: log_trim(char *log)
1.1       deraadt   813: {
1.35      deraadt   814:        FILE    *f;
1.25      millert   815:
1.35      deraadt   816:        if ((f = fopen(log, "a")) == NULL)
1.48      millert   817:                return (-1);
1.44      mpech     818:        (void)fprintf(f, "%s %s newsyslog[%ld]: logfile turned over\n",
                    819:            daytime, hostname, (long)getpid());
1.35      deraadt   820:        if (fclose(f) == EOF)
                    821:                err(1, "log_trim: fclose");
1.48      millert   822:        return (0);
1.1       deraadt   823: }
                    824:
1.16      millert   825: /* Fork off compress or gzip to compress the old log file */
1.25      millert   826: void
1.51      millert   827: compress_log(struct conf_entry *ent)
1.1       deraadt   828: {
1.48      millert   829:        pid_t pid;
                    830:        char *base, tmp[MAXPATHLEN];
1.51      millert   831:
                    832:        if (ent->backdir != NULL)
                    833:                snprintf(tmp, sizeof(tmp), "%s/%s.0", ent->backdir,
                    834:                    ent->logbase);
                    835:        else
                    836:                snprintf(tmp, sizeof(tmp), "%s.0", ent->log);
                    837:
1.27      millert   838:        if ((base = strrchr(COMPRESS, '/')) == NULL)
                    839:                base = COMPRESS;
                    840:        else
                    841:                base++;
                    842:        if (noaction) {
1.51      millert   843:                printf("%s %s\n", base, tmp);
1.27      millert   844:                return;
                    845:        }
1.35      deraadt   846:        pid = fork();
                    847:        if (pid < 0) {
1.11      downsj    848:                err(1, "fork");
1.50      millert   849:        } else if (pid == 0) {
1.37      deraadt   850:                (void)execl(COMPRESS, base, "-f", tmp, (char *)NULL);
1.16      millert   851:                warn(COMPRESS);
                    852:                _exit(1);
1.35      deraadt   853:        }
1.1       deraadt   854: }
                    855:
                    856: /* Return size in kilobytes of a file */
1.58      millert   857: off_t
1.48      millert   858: sizefile(char *file)
1.1       deraadt   859: {
1.35      deraadt   860:        struct stat sb;
1.1       deraadt   861:
1.35      deraadt   862:        if (stat(file, &sb) < 0)
1.48      millert   863:                return (-1);
1.58      millert   864:
                    865:        /* For sparse files, return the size based on number of blocks used. */
                    866:        if (sb.st_size / DEV_BSIZE > sb.st_blocks)
                    867:                return (sb.st_blocks * DEV_BSIZE);
                    868:        else
                    869:                return (sb.st_size);
1.1       deraadt   870: }
                    871:
1.25      millert   872: /* Return the age (in hours) of old log file (file.0), or -1 if none */
                    873: int
1.51      millert   874: age_old_log(struct conf_entry *ent)
1.1       deraadt   875: {
1.35      deraadt   876:        struct stat sb;
1.57      millert   877:        char file[MAXPATHLEN];
1.1       deraadt   878:
1.51      millert   879:        if (ent->backdir != NULL)
1.57      millert   880:                (void)snprintf(file, sizeof(file), "%s/%s.0", ent->backdir,
                    881:                    ent->logbase);
                    882:        else
                    883:                (void)snprintf(file, sizeof(file), "%s.0", ent->log);
                    884:        if (ent->flags & CE_COMPACT) {
                    885:                if (stat_suffix(file, sizeof(file), COMPRESS_POSTFIX, &sb,
                    886:                    stat) < 0 && stat(file, &sb) < 0)
                    887:                        return (-1);
                    888:        } else {
                    889:                if (stat(file, &sb) < 0 && stat_suffix(file, sizeof(file),
                    890:                    COMPRESS_POSTFIX, &sb, stat) < 0)
                    891:                        return (-1);
                    892:        }
1.48      millert   893:        return ((int)(timenow - sb.st_mtime + 1800) / 3600);
1.1       deraadt   894: }
                    895:
                    896: /* Skip Over Blanks */
1.25      millert   897: char *
1.48      millert   898: sob(char *p)
1.1       deraadt   899: {
1.35      deraadt   900:        while (p && *p && isspace(*p))
                    901:                p++;
1.48      millert   902:        return (p);
1.1       deraadt   903: }
                    904:
                    905: /* Skip Over Non-Blanks */
1.25      millert   906: char *
1.48      millert   907: son(char *p)
1.1       deraadt   908: {
1.35      deraadt   909:        while (p && *p && !isspace(*p))
                    910:                p++;
1.48      millert   911:        return (p);
1.1       deraadt   912: }
                    913:
                    914: /* Check if string is actually a number */
1.25      millert   915: int
1.48      millert   916: isnumberstr(char *string)
1.1       deraadt   917: {
1.35      deraadt   918:        while (*string) {
                    919:                if (!isdigit(*string++))
1.48      millert   920:                        return (0);
1.35      deraadt   921:        }
1.48      millert   922:        return (1);
1.10      downsj    923: }
                    924:
1.25      millert   925: void
1.48      millert   926: domonitor(char *log, char *whom)
1.10      downsj    927: {
                    928:        struct stat sb, tsb;
1.35      deraadt   929:        char fname[MAXPATHLEN], *flog, *p, *rb = NULL;
1.10      downsj    930:        FILE *fp;
                    931:        off_t osize;
                    932:        int rd;
                    933:
                    934:        if (stat(log, &sb) < 0)
                    935:                return;
                    936:
                    937:        flog = strdup(log);
1.11      downsj    938:        if (flog == NULL)
                    939:                err(1, "strdup");
                    940:
1.10      downsj    941:        for (p = flog; *p != '\0'; p++) {
                    942:                if (*p == '/')
                    943:                        *p = '_';
                    944:        }
1.48      millert   945:        snprintf(fname, sizeof(fname), "%s/newsyslog.%s.size",
1.35      deraadt   946:            STATS_DIR, flog);
1.10      downsj    947:
                    948:        /* ..if it doesn't exist, simply record the current size. */
                    949:        if ((sb.st_size == 0) || stat(fname, &tsb) < 0)
                    950:                goto update;
                    951:
                    952:        fp = fopen(fname, "r");
                    953:        if (fp == NULL) {
1.34      millert   954:                warn("%s", fname);
1.10      downsj    955:                goto cleanup;
                    956:        }
                    957: #ifdef QUAD_OFF_T
                    958:        if (fscanf(fp, "%qd\n", &osize) != 1) {
                    959: #else
                    960:        if (fscanf(fp, "%ld\n", &osize) != 1) {
                    961: #endif /* QUAD_OFF_T */
                    962:                fclose(fp);
                    963:                goto update;
                    964:        }
                    965:
                    966:        fclose(fp);
                    967:
                    968:        /* If the file is smaller, mark the entire thing as changed. */
                    969:        if (sb.st_size < osize)
                    970:                osize = 0;
                    971:
                    972:        /* Now see if current size is larger. */
                    973:        if (sb.st_size > osize) {
                    974:                rb = (char *) malloc(sb.st_size - osize);
1.11      downsj    975:                if (rb == NULL)
                    976:                        err(1, "malloc");
1.10      downsj    977:
                    978:                /* Open logfile, seek. */
                    979:                fp = fopen(log, "r");
                    980:                if (fp == NULL) {
1.34      millert   981:                        warn("%s", log);
1.10      downsj    982:                        goto cleanup;
                    983:                }
                    984:                fseek(fp, osize, SEEK_SET);
                    985:                rd = fread(rb, 1, sb.st_size - osize, fp);
                    986:                if (rd < 1) {
1.11      downsj    987:                        warn("fread");
1.10      downsj    988:                        fclose(fp);
                    989:                        goto cleanup;
                    990:                }
                    991:
                    992:                /* Send message. */
                    993:                fclose(fp);
                    994:
                    995:                fp = openmail();
                    996:                if (fp == NULL) {
1.11      downsj    997:                        warn("openmail");
1.10      downsj    998:                        goto cleanup;
                    999:                }
                   1000:                fprintf(fp, "To: %s\nSubject: LOGFILE NOTIFICATION: %s\n\n\n",
                   1001:                    whom, log);
                   1002:                fwrite(rb, 1, rd, fp);
                   1003:                fputs("\n\n", fp);
                   1004:
1.48      millert  1005:                pclose(fp);
1.10      downsj   1006:        }
                   1007: update:
                   1008:        /* Reopen for writing and update file. */
                   1009:        fp = fopen(fname, "w");
                   1010:        if (fp == NULL) {
1.34      millert  1011:                warn("%s", fname);
1.10      downsj   1012:                goto cleanup;
                   1013:        }
                   1014: #ifdef QUAD_OFF_T
                   1015:        fprintf(fp, "%qd\n", sb.st_size);
                   1016: #else
                   1017:        fprintf(fp, "%ld\n", sb.st_size);
                   1018: #endif /* QUAD_OFF_T */
                   1019:        fclose(fp);
                   1020:
                   1021: cleanup:
                   1022:        free(flog);
                   1023:        if (rb != NULL)
                   1024:                free(rb);
                   1025: }
                   1026:
1.25      millert  1027: FILE *
1.48      millert  1028: openmail(void)
1.10      downsj   1029: {
1.48      millert  1030:        FILE *ret;
1.35      deraadt  1031:        char *cmdbuf = NULL;
1.10      downsj   1032:
1.35      deraadt  1033:        asprintf(&cmdbuf, "%s -t", SENDMAIL);
                   1034:        if (cmdbuf) {
                   1035:                ret = popen(cmdbuf, "w");
                   1036:                free(cmdbuf);
                   1037:                return (ret);
                   1038:        }
                   1039:        return (NULL);
1.10      downsj   1040: }
                   1041:
1.25      millert  1042: void
1.48      millert  1043: child_killer(int signo)
1.16      millert  1044: {
1.38      deraadt  1045:        int save_errno = errno;
1.16      millert  1046:        int status;
                   1047:
                   1048:        while (waitpid(-1, &status, WNOHANG) > 0)
                   1049:                ;
1.38      deraadt  1050:        errno = save_errno;
1.57      millert  1051: }
                   1052:
                   1053: int
                   1054: stat_suffix(char *file, size_t size, char *suffix, struct stat *sp, int (*func)())
                   1055: {
                   1056:        size_t n;
                   1057:
                   1058:        n = strlcat(file, suffix, size);
                   1059:        if (n < size && func(file, sp) == 0)
                   1060:                return (0);
                   1061:        file[n - strlen(suffix)] = '\0';
                   1062:        return (-1);
                   1063: }
                   1064:
                   1065: /*
                   1066:  * lstat() a log, possibily appending a suffix; order is based on flags.
                   1067:  * Returns the suffix appended (may be empty string) or NULL if no file.
                   1068:  */
                   1069: char *
                   1070: lstat_log(char *file, size_t size, int flags)
                   1071: {
                   1072:        struct stat sb;
                   1073:
                   1074:        if (flags & CE_COMPACT) {
                   1075:                if (stat_suffix(file, size, COMPRESS_POSTFIX, &sb, lstat) == 0)
                   1076:                        return (COMPRESS_POSTFIX);
                   1077:                if (lstat(file, &sb) == 0)
                   1078:                        return ("");
                   1079:        } else {
                   1080:                if (lstat(file, &sb) == 0)
                   1081:                        return ("");
                   1082:                if (stat_suffix(file, size, COMPRESS_POSTFIX, &sb, lstat) == 0)
                   1083:                        return (COMPRESS_POSTFIX);
                   1084:
                   1085:        }
                   1086:        return (NULL);
1.1       deraadt  1087: }