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

Annotation of src/usr.bin/calendar/calendar.c, Revision 1.17

1.17    ! millert     1: /*     $OpenBSD: calendar.c,v 1.16 2001/09/26 20:38:55 mickey Exp $    */
1.1       deraadt     2:
                      3: /*
                      4:  * Copyright (c) 1989, 1993, 1994
                      5:  *     The Regents of the University of California.  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. All advertising materials mentioning features or use of this software
                     16:  *    must display the following acknowledgement:
                     17:  *     This product includes software developed by the University of
                     18:  *     California, Berkeley and its contributors.
                     19:  * 4. Neither the name of the University nor the names of its contributors
                     20:  *    may be used to endorse or promote products derived from this software
                     21:  *    without specific prior written permission.
                     22:  *
                     23:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     24:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     25:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     26:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     27:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     28:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     29:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     30:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     31:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     32:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     33:  * SUCH DAMAGE.
                     34:  */
                     35:
                     36: #ifndef lint
1.5       millert    37: static const char copyright[] =
1.1       deraadt    38: "@(#) Copyright (c) 1989, 1993\n\
                     39:        The Regents of the University of California.  All rights reserved.\n";
                     40: #endif /* not lint */
                     41:
                     42: #ifndef lint
                     43: #if 0
1.5       millert    44: static const char sccsid[] = "@(#)calendar.c  8.3 (Berkeley) 3/25/94";
                     45: #else
1.17    ! millert    46: static char rcsid[] = "$OpenBSD: calendar.c,v 1.16 2001/09/26 20:38:55 mickey Exp $";
1.1       deraadt    47: #endif
                     48: #endif /* not lint */
                     49:
1.15      pjanzen    50: #include <sys/stat.h>
                     51: #include <sys/types.h>
                     52: #include <sys/wait.h>
1.1       deraadt    53: #include <err.h>
                     54: #include <errno.h>
1.5       millert    55: #include <locale.h>
1.15      pjanzen    56: #include <login_cap.h>
1.1       deraadt    57: #include <pwd.h>
1.15      pjanzen    58: #include <signal.h>
1.1       deraadt    59: #include <stdio.h>
                     60: #include <stdlib.h>
                     61: #include <string.h>
1.5       millert    62: #include <time.h>
1.12      pjanzen    63: #include <tzfile.h>
1.1       deraadt    64: #include <unistd.h>
                     65:
                     66: #include "pathnames.h"
1.5       millert    67: #include "calendar.h"
1.1       deraadt    68:
1.15      pjanzen    69: char *calendarFile = "calendar";  /* default calendar file */
                     70: char *calendarHome = ".calendar"; /* HOME */
                     71: char *calendarNoMail = "nomail";  /* don't sent mail if this file exists */
                     72:
1.1       deraadt    73: struct passwd *pw;
1.5       millert    74: int doall = 0;
                     75: time_t f_time = 0;
1.16      mickey     76: int bodun_always = 0;
1.1       deraadt    77:
1.5       millert    78: int f_dayAfter = 0; /* days after current date */
                     79: int f_dayBefore = 0; /* days before current date */
1.1       deraadt    80:
1.12      pjanzen    81: struct specialev spev[NUMEV];
                     82:
1.17    ! millert    83: void childsig(int);
1.15      pjanzen    84:
1.1       deraadt    85: int
                     86: main(argc, argv)
                     87:        int argc;
                     88:        char *argv[];
                     89: {
1.15      pjanzen    90:        int ch;
1.1       deraadt    91:        char *caldir;
                     92:
1.12      pjanzen    93:        (void)setlocale(LC_ALL, "");
1.5       millert    94:
1.16      mickey     95:        while ((ch = getopt(argc, argv, "-abf:t:A:B:")) != -1)
1.1       deraadt    96:                switch (ch) {
                     97:                case '-':               /* backward contemptible */
                     98:                case 'a':
1.5       millert    99:                        if (getuid())
1.13      millert   100:                                errx(1, "%s", strerror(EPERM));
1.1       deraadt   101:                        doall = 1;
                    102:                        break;
1.5       millert   103:
1.16      mickey    104:                case 'b':
                    105:                        bodun_always++;
                    106:                        break;
                    107:
1.5       millert   108:                case 'f': /* other calendar file */
                    109:                        calendarFile = optarg;
                    110:                        break;
                    111:
                    112:                case 't': /* other date, undocumented, for tests */
1.12      pjanzen   113:                        if ((f_time = Mktime(optarg)) <= 0)
1.11      pjanzen   114:                                errx(1, "specified date is outside allowed range");
1.5       millert   115:                        break;
                    116:
                    117:                case 'A': /* days after current date */
                    118:                        f_dayAfter = atoi(optarg);
                    119:                        break;
                    120:
                    121:                case 'B': /* days before current date */
                    122:                        f_dayBefore = atoi(optarg);
                    123:                        break;
                    124:
1.1       deraadt   125:                default:
                    126:                        usage();
                    127:                }
                    128:        argc -= optind;
                    129:        argv += optind;
                    130:
                    131:        if (argc)
                    132:                usage();
                    133:
1.5       millert   134:        /* use current time */
                    135:        if (f_time <= 0)
                    136:            (void)time(&f_time);
                    137:
1.12      pjanzen   138:        if (f_dayBefore) {
                    139:                /* Move back in time and only look forwards */
                    140:                f_dayAfter += f_dayBefore;
                    141:                f_time -= SECSPERDAY * f_dayBefore;
                    142:                f_dayBefore = 0;
                    143:        }
                    144:        settime(&f_time);
1.5       millert   145:
1.10      millert   146:        if (doall) {
1.15      pjanzen   147:                pid_t kid, deadkid;
                    148:                int kidstat, kidreaped, runningkids;
                    149:                int acstat;
                    150:                struct stat sbuf;
                    151:                time_t t;
                    152:                unsigned int sleeptime;
                    153:
                    154:                signal(SIGCHLD, childsig);
                    155:                runningkids = 0;
                    156:                t = time(NULL);
1.1       deraadt   157:                while ((pw = getpwent()) != NULL) {
1.15      pjanzen   158:                        acstat = 0;
                    159:                        /* Avoid unnecessary forks.  The calendar file is only
                    160:                         * opened as the user later; if it can't be opened,
                    161:                         * it's no big deal.  Also, get to correct directory.
                    162:                         * Note that in an NFS environment root may get EACCES
                    163:                         * on a chdir(), in which case we have to fork.  As long as
                    164:                         * we can chdir() we can stat(), unless the user is
                    165:                         * modifying permissions while this is running.
                    166:                         */
                    167:                        if (chdir(pw->pw_dir)) {
                    168:                                if (errno == EACCES)
                    169:                                        acstat = 1;
                    170:                                else
                    171:                                        continue;
                    172:                        }
                    173:                        if (stat(calendarFile, &sbuf) != 0) {
                    174:                                if (chdir(calendarHome)) {
                    175:                                        if (errno == EACCES)
                    176:                                                acstat = 1;
                    177:                                        else
                    178:                                                continue;
                    179:                                }
                    180:                                if (stat(calendarNoMail, &sbuf) == 0 ||
                    181:                                    stat(calendarFile, &sbuf) != 0)
                    182:                                        continue;
                    183:                        }
                    184:                        sleeptime = USERTIMEOUT;
                    185:                        switch ((kid = fork())) {
                    186:                        case -1:        /* error */
                    187:                                warn("fork");
                    188:                                continue;
                    189:                        case 0: /* child */
                    190:                                (void)setlocale(LC_ALL, "");
                    191:                                if (setusercontext(NULL, pw, pw->pw_uid,
                    192:                                    LOGIN_SETALL ^ LOGIN_SETLOGIN))
                    193:                                        err(1, "unable to set user context (uid %d)",
                    194:                                            (int)pw->pw_uid);
                    195:                                if (acstat) {
                    196:                                        if (chdir(pw->pw_dir) ||
                    197:                                            stat(calendarFile, &sbuf) != 0 ||
                    198:                                            chdir(calendarHome) ||
                    199:                                            stat(calendarNoMail, &sbuf) == 0 ||
                    200:                                            stat(calendarFile, &sbuf) != 0)
                    201:                                                exit(0);
                    202:                                }
1.1       deraadt   203:                                cal();
1.15      pjanzen   204:                                exit(0);
                    205:                        }
                    206:                        /* parent: wait a reasonable time, then kill child if
                    207:                         * necessary.
                    208:                         */
                    209:                        runningkids++;
                    210:                        kidreaped = 0;
                    211:                        do {
                    212:                                sleeptime = sleep(sleeptime);
                    213:                                /* Note that there is the possibility, if the sleep
                    214:                                 * stops early due to some other signal, of the child
                    215:                                 * terminating and not getting detected during the next
                    216:                                 * sleep.  In that unlikely worst case, we just sleep
                    217:                                 * too long for that user.
                    218:                                 */
                    219:                                for (;;) {
                    220:                                        deadkid = waitpid(-1, &kidstat, WNOHANG);
                    221:                                        if (deadkid <= 0)
                    222:                                                break;
                    223:                                        runningkids--;
                    224:                                        if (deadkid == kid) {
                    225:                                                kidreaped = 1;
                    226:                                                sleeptime = 0;
                    227:                                        }
                    228:                                }
                    229:                        } while (sleeptime);
                    230:
                    231:                        if (!kidreaped) {
                    232:                                /* It doesn't _really_ matter if the kill fails, e.g.
                    233:                                 * if there's only a zombie now.
                    234:                                 */
                    235:                                (void)kill(kid, SIGTERM);
                    236:                                warnx("uid %d did not finish in time", (int)pw->pw_uid);
1.12      pjanzen   237:                        }
1.15      pjanzen   238:                        if (time(NULL) - t >= SECSPERDAY)
                    239:                                errx(2, "'calendar -a' took more than a day; stopped at uid %d",
                    240:                                    (int)pw->pw_uid);
                    241:                }
                    242:                for (;;) {
                    243:                        deadkid = waitpid(-1, &kidstat, WNOHANG);
                    244:                        if (deadkid <= 0)
                    245:                                break;
                    246:                        runningkids--;
1.1       deraadt   247:                }
1.15      pjanzen   248:                if (runningkids)
                    249:                        warnx(
                    250: "%d child processes still running when 'calendar -a' finished", runningkids);
1.10      millert   251:        }
1.1       deraadt   252:        else if ((caldir = getenv("CALENDAR_DIR")) != NULL) {
1.5       millert   253:                if(!chdir(caldir))
                    254:                        cal();
1.1       deraadt   255:        } else
                    256:                cal();
1.5       millert   257:
1.1       deraadt   258:        exit(0);
                    259: }
                    260:
                    261:
                    262: void
                    263: usage()
                    264: {
1.5       millert   265:        (void)fprintf(stderr,
1.16      mickey    266:            "usage: calendar [-a] [-A num] [-b] [-B num] [-t [[[cc]yy][mm]]dd] "
1.14      aaron     267:            "[-f calendarfile]\n");
1.1       deraadt   268:        exit(1);
1.15      pjanzen   269: }
                    270:
                    271:
                    272: void
                    273: childsig(sig)
                    274:        int sig;
                    275: {
1.1       deraadt   276: }