Home | History | Annotate | Line # | Download | only in shutdown
shutdown.c revision 1.1.1.3
      1      1.1      cgd /*
      2  1.1.1.2  mycroft  * Copyright (c) 1988, 1990, 1993
      3  1.1.1.2  mycroft  *	The Regents of the University of California.  All rights reserved.
      4      1.1      cgd  *
      5      1.1      cgd  * Redistribution and use in source and binary forms, with or without
      6      1.1      cgd  * modification, are permitted provided that the following conditions
      7      1.1      cgd  * are met:
      8      1.1      cgd  * 1. Redistributions of source code must retain the above copyright
      9      1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     10      1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     11      1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     12      1.1      cgd  *    documentation and/or other materials provided with the distribution.
     13      1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     14      1.1      cgd  *    must display the following acknowledgement:
     15      1.1      cgd  *	This product includes software developed by the University of
     16      1.1      cgd  *	California, Berkeley and its contributors.
     17      1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     18      1.1      cgd  *    may be used to endorse or promote products derived from this software
     19      1.1      cgd  *    without specific prior written permission.
     20      1.1      cgd  *
     21      1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22      1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23      1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24      1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25      1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26      1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27      1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28      1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29      1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30      1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31      1.1      cgd  * SUCH DAMAGE.
     32      1.1      cgd  */
     33      1.1      cgd 
     34      1.1      cgd #ifndef lint
     35  1.1.1.2  mycroft static char copyright[] =
     36  1.1.1.2  mycroft "@(#) Copyright (c) 1988, 1990, 1993\n\
     37  1.1.1.2  mycroft 	The Regents of the University of California.  All rights reserved.\n";
     38      1.1      cgd #endif /* not lint */
     39      1.1      cgd 
     40      1.1      cgd #ifndef lint
     41  1.1.1.3    lukem static char sccsid[] = "@(#)shutdown.c	8.4 (Berkeley) 4/28/95";
     42      1.1      cgd #endif /* not lint */
     43      1.1      cgd 
     44      1.1      cgd #include <sys/param.h>
     45      1.1      cgd #include <sys/time.h>
     46      1.1      cgd #include <sys/resource.h>
     47      1.1      cgd #include <sys/syslog.h>
     48  1.1.1.2  mycroft 
     49  1.1.1.2  mycroft #include <ctype.h>
     50  1.1.1.2  mycroft #include <fcntl.h>
     51      1.1      cgd #include <pwd.h>
     52  1.1.1.2  mycroft #include <setjmp.h>
     53  1.1.1.2  mycroft #include <signal.h>
     54      1.1      cgd #include <stdio.h>
     55  1.1.1.2  mycroft #include <stdlib.h>
     56  1.1.1.2  mycroft #include <string.h>
     57  1.1.1.2  mycroft #include <tzfile.h>
     58  1.1.1.2  mycroft #include <unistd.h>
     59  1.1.1.2  mycroft 
     60      1.1      cgd #include "pathnames.h"
     61      1.1      cgd 
     62      1.1      cgd #ifdef DEBUG
     63      1.1      cgd #undef _PATH_NOLOGIN
     64      1.1      cgd #define	_PATH_NOLOGIN	"./nologin"
     65      1.1      cgd #undef _PATH_FASTBOOT
     66      1.1      cgd #define	_PATH_FASTBOOT	"./fastboot"
     67      1.1      cgd #endif
     68      1.1      cgd 
     69      1.1      cgd #define	H		*60*60
     70      1.1      cgd #define	M		*60
     71      1.1      cgd #define	S		*1
     72      1.1      cgd #define	NOLOG_TIME	5*60
     73      1.1      cgd struct interval {
     74      1.1      cgd 	int timeleft, timetowait;
     75      1.1      cgd } tlist[] = {
     76      1.1      cgd 	10 H,  5 H,	 5 H,  3 H,	 2 H,  1 H,	1 H, 30 M,
     77      1.1      cgd 	30 M, 10 M,	20 M, 10 M,	10 M,  5 M,	5 M,  3 M,
     78      1.1      cgd 	 2 M,  1 M,	 1 M, 30 S,	30 S, 30 S,
     79      1.1      cgd 	 0, 0,
     80  1.1.1.2  mycroft };
     81      1.1      cgd #undef H
     82      1.1      cgd #undef M
     83      1.1      cgd #undef S
     84      1.1      cgd 
     85      1.1      cgd static time_t offset, shuttime;
     86      1.1      cgd static int dofast, dohalt, doreboot, killflg, mbuflen;
     87      1.1      cgd static char *nosync, *whom, mbuf[BUFSIZ];
     88      1.1      cgd 
     89  1.1.1.2  mycroft void badtime __P((void));
     90  1.1.1.2  mycroft void die_you_gravy_sucking_pig_dog __P((void));
     91  1.1.1.2  mycroft void doitfast __P((void));
     92  1.1.1.2  mycroft void finish __P((int));
     93  1.1.1.2  mycroft void getoffset __P((char *));
     94  1.1.1.2  mycroft void loop __P((void));
     95  1.1.1.2  mycroft void nolog __P((void));
     96  1.1.1.2  mycroft void timeout __P((int));
     97  1.1.1.2  mycroft void timewarn __P((int));
     98  1.1.1.2  mycroft void usage __P((void));
     99  1.1.1.2  mycroft 
    100  1.1.1.2  mycroft int
    101      1.1      cgd main(argc, argv)
    102      1.1      cgd 	int argc;
    103  1.1.1.2  mycroft 	char *argv[];
    104      1.1      cgd {
    105      1.1      cgd 	extern int optind;
    106      1.1      cgd 	register char *p, *endp;
    107      1.1      cgd 	struct passwd *pw;
    108  1.1.1.2  mycroft 	int arglen, ch, len, readstdin;
    109      1.1      cgd 
    110      1.1      cgd #ifndef DEBUG
    111      1.1      cgd 	if (geteuid()) {
    112      1.1      cgd 		(void)fprintf(stderr, "shutdown: NOT super-user\n");
    113      1.1      cgd 		exit(1);
    114      1.1      cgd 	}
    115      1.1      cgd #endif
    116      1.1      cgd 	nosync = NULL;
    117      1.1      cgd 	readstdin = 0;
    118      1.1      cgd 	while ((ch = getopt(argc, argv, "-fhknr")) != EOF)
    119      1.1      cgd 		switch (ch) {
    120      1.1      cgd 		case '-':
    121      1.1      cgd 			readstdin = 1;
    122      1.1      cgd 			break;
    123      1.1      cgd 		case 'f':
    124      1.1      cgd 			dofast = 1;
    125      1.1      cgd 			break;
    126      1.1      cgd 		case 'h':
    127      1.1      cgd 			dohalt = 1;
    128      1.1      cgd 			break;
    129      1.1      cgd 		case 'k':
    130      1.1      cgd 			killflg = 1;
    131      1.1      cgd 			break;
    132      1.1      cgd 		case 'n':
    133      1.1      cgd 			nosync = "-n";
    134      1.1      cgd 			break;
    135      1.1      cgd 		case 'r':
    136      1.1      cgd 			doreboot = 1;
    137      1.1      cgd 			break;
    138      1.1      cgd 		case '?':
    139      1.1      cgd 		default:
    140      1.1      cgd 			usage();
    141      1.1      cgd 		}
    142      1.1      cgd 	argc -= optind;
    143      1.1      cgd 	argv += optind;
    144      1.1      cgd 
    145      1.1      cgd 	if (argc < 1)
    146      1.1      cgd 		usage();
    147      1.1      cgd 
    148      1.1      cgd 	if (dofast && nosync) {
    149      1.1      cgd 		(void)fprintf(stderr,
    150      1.1      cgd 		    "shutdown: incompatible switches -f and -n.\n");
    151      1.1      cgd 		usage();
    152      1.1      cgd 	}
    153      1.1      cgd 	if (doreboot && dohalt) {
    154      1.1      cgd 		(void)fprintf(stderr,
    155      1.1      cgd 		    "shutdown: incompatible switches -h and -r.\n");
    156      1.1      cgd 		usage();
    157      1.1      cgd 	}
    158      1.1      cgd 	getoffset(*argv++);
    159      1.1      cgd 
    160      1.1      cgd 	if (*argv) {
    161      1.1      cgd 		for (p = mbuf, len = sizeof(mbuf); *argv; ++argv) {
    162      1.1      cgd 			arglen = strlen(*argv);
    163      1.1      cgd 			if ((len -= arglen) <= 2)
    164      1.1      cgd 				break;
    165      1.1      cgd 			if (p != mbuf)
    166      1.1      cgd 				*p++ = ' ';
    167  1.1.1.3    lukem 			memmove(p, *argv, arglen);
    168      1.1      cgd 			p += arglen;
    169      1.1      cgd 		}
    170      1.1      cgd 		*p = '\n';
    171      1.1      cgd 		*++p = '\0';
    172      1.1      cgd 	}
    173      1.1      cgd 
    174      1.1      cgd 	if (readstdin) {
    175      1.1      cgd 		p = mbuf;
    176      1.1      cgd 		endp = mbuf + sizeof(mbuf) - 2;
    177      1.1      cgd 		for (;;) {
    178      1.1      cgd 			if (!fgets(p, endp - p + 1, stdin))
    179      1.1      cgd 				break;
    180      1.1      cgd 			for (; *p &&  p < endp; ++p);
    181      1.1      cgd 			if (p == endp) {
    182      1.1      cgd 				*p = '\n';
    183      1.1      cgd 				*++p = '\0';
    184      1.1      cgd 				break;
    185      1.1      cgd 			}
    186      1.1      cgd 		}
    187      1.1      cgd 	}
    188      1.1      cgd 	mbuflen = strlen(mbuf);
    189      1.1      cgd 
    190      1.1      cgd 	if (offset)
    191      1.1      cgd 		(void)printf("Shutdown at %.24s.\n", ctime(&shuttime));
    192      1.1      cgd 	else
    193      1.1      cgd 		(void)printf("Shutdown NOW!\n");
    194      1.1      cgd 
    195      1.1      cgd 	if (!(whom = getlogin()))
    196      1.1      cgd 		whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???";
    197      1.1      cgd 
    198      1.1      cgd #ifdef DEBUG
    199      1.1      cgd 	(void)putc('\n', stdout);
    200      1.1      cgd #else
    201      1.1      cgd 	(void)setpriority(PRIO_PROCESS, 0, PRIO_MIN);
    202      1.1      cgd 	{
    203      1.1      cgd 		int forkpid;
    204      1.1      cgd 
    205      1.1      cgd 		forkpid = fork();
    206      1.1      cgd 		if (forkpid == -1) {
    207      1.1      cgd 			perror("shutdown: fork");
    208      1.1      cgd 			exit(1);
    209      1.1      cgd 		}
    210      1.1      cgd 		if (forkpid) {
    211      1.1      cgd 			(void)printf("shutdown: [pid %d]\n", forkpid);
    212      1.1      cgd 			exit(0);
    213      1.1      cgd 		}
    214      1.1      cgd 	}
    215      1.1      cgd #endif
    216      1.1      cgd 	openlog("shutdown", LOG_CONS, LOG_AUTH);
    217      1.1      cgd 	loop();
    218  1.1.1.2  mycroft 	/* NOTREACHED */
    219      1.1      cgd }
    220      1.1      cgd 
    221  1.1.1.2  mycroft void
    222      1.1      cgd loop()
    223      1.1      cgd {
    224  1.1.1.2  mycroft 	struct interval *tp;
    225      1.1      cgd 	u_int sltime;
    226      1.1      cgd 	int logged;
    227      1.1      cgd 
    228      1.1      cgd 	if (offset <= NOLOG_TIME) {
    229      1.1      cgd 		logged = 1;
    230      1.1      cgd 		nolog();
    231      1.1      cgd 	}
    232      1.1      cgd 	else
    233      1.1      cgd 		logged = 0;
    234      1.1      cgd 	tp = tlist;
    235      1.1      cgd 	if (tp->timeleft < offset)
    236      1.1      cgd 		(void)sleep((u_int)(offset - tp->timeleft));
    237      1.1      cgd 	else {
    238      1.1      cgd 		while (offset < tp->timeleft)
    239      1.1      cgd 			++tp;
    240      1.1      cgd 		/*
    241  1.1.1.2  mycroft 		 * Warn now, if going to sleep more than a fifth of
    242      1.1      cgd 		 * the next wait time.
    243      1.1      cgd 		 */
    244      1.1      cgd 		if (sltime = offset - tp->timeleft) {
    245      1.1      cgd 			if (sltime > tp->timetowait / 5)
    246  1.1.1.2  mycroft 				timewarn(offset);
    247      1.1      cgd 			(void)sleep(sltime);
    248      1.1      cgd 		}
    249      1.1      cgd 	}
    250      1.1      cgd 	for (;; ++tp) {
    251  1.1.1.2  mycroft 		timewarn(tp->timeleft);
    252      1.1      cgd 		if (!logged && tp->timeleft <= NOLOG_TIME) {
    253      1.1      cgd 			logged = 1;
    254      1.1      cgd 			nolog();
    255      1.1      cgd 		}
    256      1.1      cgd 		(void)sleep((u_int)tp->timetowait);
    257      1.1      cgd 		if (!tp->timeleft)
    258      1.1      cgd 			break;
    259      1.1      cgd 	}
    260      1.1      cgd 	die_you_gravy_sucking_pig_dog();
    261      1.1      cgd }
    262      1.1      cgd 
    263      1.1      cgd static jmp_buf alarmbuf;
    264      1.1      cgd 
    265  1.1.1.2  mycroft void
    266  1.1.1.2  mycroft timewarn(timeleft)
    267  1.1.1.2  mycroft 	int timeleft;
    268      1.1      cgd {
    269      1.1      cgd 	static int first;
    270      1.1      cgd 	static char hostname[MAXHOSTNAMELEN + 1];
    271      1.1      cgd 	FILE *pf;
    272  1.1.1.2  mycroft 	char wcmd[MAXPATHLEN + 4];
    273      1.1      cgd 
    274      1.1      cgd 	if (!first++)
    275      1.1      cgd 		(void)gethostname(hostname, sizeof(hostname));
    276      1.1      cgd 
    277      1.1      cgd 	/* undoc -n option to wall suppresses normal wall banner */
    278  1.1.1.2  mycroft 	(void)snprintf(wcmd, sizeof(wcmd), "%s -n", _PATH_WALL);
    279      1.1      cgd 	if (!(pf = popen(wcmd, "w"))) {
    280      1.1      cgd 		syslog(LOG_ERR, "shutdown: can't find %s: %m", _PATH_WALL);
    281      1.1      cgd 		return;
    282      1.1      cgd 	}
    283      1.1      cgd 
    284      1.1      cgd 	(void)fprintf(pf,
    285      1.1      cgd 	    "\007*** %sSystem shutdown message from %s@%s ***\007\n",
    286  1.1.1.2  mycroft 	    timeleft ? "": "FINAL ", whom, hostname);
    287      1.1      cgd 
    288  1.1.1.2  mycroft 	if (timeleft > 10*60)
    289      1.1      cgd 		(void)fprintf(pf, "System going down at %5.5s\n\n",
    290      1.1      cgd 		    ctime(&shuttime) + 11);
    291  1.1.1.2  mycroft 	else if (timeleft > 59)
    292      1.1      cgd 		(void)fprintf(pf, "System going down in %d minute%s\n\n",
    293  1.1.1.2  mycroft 		    timeleft / 60, (timeleft > 60) ? "s" : "");
    294  1.1.1.2  mycroft 	else if (timeleft)
    295      1.1      cgd 		(void)fprintf(pf, "System going down in 30 seconds\n\n");
    296      1.1      cgd 	else
    297      1.1      cgd 		(void)fprintf(pf, "System going down IMMEDIATELY\n\n");
    298      1.1      cgd 
    299      1.1      cgd 	if (mbuflen)
    300      1.1      cgd 		(void)fwrite(mbuf, sizeof(*mbuf), mbuflen, pf);
    301      1.1      cgd 
    302      1.1      cgd 	/*
    303      1.1      cgd 	 * play some games, just in case wall doesn't come back
    304      1.1      cgd 	 * probably unecessary, given that wall is careful.
    305      1.1      cgd 	 */
    306      1.1      cgd 	if (!setjmp(alarmbuf)) {
    307      1.1      cgd 		(void)signal(SIGALRM, timeout);
    308      1.1      cgd 		(void)alarm((u_int)30);
    309      1.1      cgd 		(void)pclose(pf);
    310      1.1      cgd 		(void)alarm((u_int)0);
    311      1.1      cgd 		(void)signal(SIGALRM, SIG_DFL);
    312      1.1      cgd 	}
    313      1.1      cgd }
    314      1.1      cgd 
    315      1.1      cgd void
    316  1.1.1.2  mycroft timeout(signo)
    317  1.1.1.2  mycroft 	int signo;
    318      1.1      cgd {
    319      1.1      cgd 	longjmp(alarmbuf, 1);
    320      1.1      cgd }
    321      1.1      cgd 
    322  1.1.1.2  mycroft void
    323      1.1      cgd die_you_gravy_sucking_pig_dog()
    324      1.1      cgd {
    325      1.1      cgd 
    326      1.1      cgd 	syslog(LOG_NOTICE, "%s by %s: %s",
    327      1.1      cgd 	    doreboot ? "reboot" : dohalt ? "halt" : "shutdown", whom, mbuf);
    328      1.1      cgd 	(void)sleep(2);
    329      1.1      cgd 
    330      1.1      cgd 	(void)printf("\r\nSystem shutdown time has arrived\007\007\r\n");
    331      1.1      cgd 	if (killflg) {
    332      1.1      cgd 		(void)printf("\rbut you'll have to do it yourself\r\n");
    333  1.1.1.2  mycroft 		finish(0);
    334      1.1      cgd 	}
    335      1.1      cgd 	if (dofast)
    336      1.1      cgd 		doitfast();
    337      1.1      cgd #ifdef DEBUG
    338      1.1      cgd 	if (doreboot)
    339      1.1      cgd 		(void)printf("reboot");
    340      1.1      cgd 	else if (dohalt)
    341      1.1      cgd 		(void)printf("halt");
    342      1.1      cgd 	if (nosync)
    343      1.1      cgd 		(void)printf(" no sync");
    344      1.1      cgd 	if (dofast)
    345      1.1      cgd 		(void)printf(" no fsck");
    346      1.1      cgd 	(void)printf("\nkill -HUP 1\n");
    347      1.1      cgd #else
    348      1.1      cgd 	if (doreboot) {
    349      1.1      cgd 		execle(_PATH_REBOOT, "reboot", "-l", nosync, 0);
    350      1.1      cgd 		syslog(LOG_ERR, "shutdown: can't exec %s: %m.", _PATH_REBOOT);
    351      1.1      cgd 		perror("shutdown");
    352      1.1      cgd 	}
    353      1.1      cgd 	else if (dohalt) {
    354      1.1      cgd 		execle(_PATH_HALT, "halt", "-l", nosync, 0);
    355      1.1      cgd 		syslog(LOG_ERR, "shutdown: can't exec %s: %m.", _PATH_HALT);
    356      1.1      cgd 		perror("shutdown");
    357      1.1      cgd 	}
    358      1.1      cgd 	(void)kill(1, SIGTERM);		/* to single user */
    359      1.1      cgd #endif
    360  1.1.1.2  mycroft 	finish(0);
    361      1.1      cgd }
    362      1.1      cgd 
    363      1.1      cgd #define	ATOI2(p)	(p[0] - '0') * 10 + (p[1] - '0'); p += 2;
    364      1.1      cgd 
    365  1.1.1.2  mycroft void
    366      1.1      cgd getoffset(timearg)
    367      1.1      cgd 	register char *timearg;
    368      1.1      cgd {
    369      1.1      cgd 	register struct tm *lt;
    370      1.1      cgd 	register char *p;
    371  1.1.1.2  mycroft 	time_t now;
    372      1.1      cgd 
    373      1.1      cgd 	if (!strcasecmp(timearg, "now")) {		/* now */
    374      1.1      cgd 		offset = 0;
    375      1.1      cgd 		return;
    376      1.1      cgd 	}
    377      1.1      cgd 
    378      1.1      cgd 	(void)time(&now);
    379      1.1      cgd 	if (*timearg == '+') {				/* +minutes */
    380      1.1      cgd 		if (!isdigit(*++timearg))
    381      1.1      cgd 			badtime();
    382      1.1      cgd 		offset = atoi(timearg) * 60;
    383      1.1      cgd 		shuttime = now + offset;
    384      1.1      cgd 		return;
    385      1.1      cgd 	}
    386      1.1      cgd 
    387      1.1      cgd 	/* handle hh:mm by getting rid of the colon */
    388      1.1      cgd 	for (p = timearg; *p; ++p)
    389      1.1      cgd 		if (!isascii(*p) || !isdigit(*p))
    390      1.1      cgd 			if (*p == ':' && strlen(p) == 3) {
    391      1.1      cgd 				p[0] = p[1];
    392      1.1      cgd 				p[1] = p[2];
    393      1.1      cgd 				p[2] = '\0';
    394      1.1      cgd 			}
    395      1.1      cgd 			else
    396      1.1      cgd 				badtime();
    397      1.1      cgd 
    398      1.1      cgd 	unsetenv("TZ");					/* OUR timezone */
    399      1.1      cgd 	lt = localtime(&now);				/* current time val */
    400      1.1      cgd 
    401      1.1      cgd 	switch(strlen(timearg)) {
    402      1.1      cgd 	case 10:
    403      1.1      cgd 		lt->tm_year = ATOI2(timearg);
    404      1.1      cgd 		/* FALLTHROUGH */
    405      1.1      cgd 	case 8:
    406      1.1      cgd 		lt->tm_mon = ATOI2(timearg);
    407      1.1      cgd 		if (--lt->tm_mon < 0 || lt->tm_mon > 11)
    408      1.1      cgd 			badtime();
    409      1.1      cgd 		/* FALLTHROUGH */
    410      1.1      cgd 	case 6:
    411      1.1      cgd 		lt->tm_mday = ATOI2(timearg);
    412      1.1      cgd 		if (lt->tm_mday < 1 || lt->tm_mday > 31)
    413      1.1      cgd 			badtime();
    414      1.1      cgd 		/* FALLTHROUGH */
    415      1.1      cgd 	case 4:
    416      1.1      cgd 		lt->tm_hour = ATOI2(timearg);
    417      1.1      cgd 		if (lt->tm_hour < 0 || lt->tm_hour > 23)
    418      1.1      cgd 			badtime();
    419      1.1      cgd 		lt->tm_min = ATOI2(timearg);
    420      1.1      cgd 		if (lt->tm_min < 0 || lt->tm_min > 59)
    421      1.1      cgd 			badtime();
    422      1.1      cgd 		lt->tm_sec = 0;
    423      1.1      cgd 		if ((shuttime = mktime(lt)) == -1)
    424      1.1      cgd 			badtime();
    425      1.1      cgd 		if ((offset = shuttime - now) < 0) {
    426      1.1      cgd 			(void)fprintf(stderr,
    427      1.1      cgd 			    "shutdown: that time is already past.\n");
    428      1.1      cgd 			exit(1);
    429      1.1      cgd 		}
    430      1.1      cgd 		break;
    431      1.1      cgd 	default:
    432      1.1      cgd 		badtime();
    433      1.1      cgd 	}
    434      1.1      cgd }
    435      1.1      cgd 
    436      1.1      cgd #define	FSMSG	"fastboot file for fsck\n"
    437  1.1.1.2  mycroft void
    438      1.1      cgd doitfast()
    439      1.1      cgd {
    440      1.1      cgd 	int fastfd;
    441      1.1      cgd 
    442      1.1      cgd 	if ((fastfd = open(_PATH_FASTBOOT, O_WRONLY|O_CREAT|O_TRUNC,
    443      1.1      cgd 	    0664)) >= 0) {
    444      1.1      cgd 		(void)write(fastfd, FSMSG, sizeof(FSMSG) - 1);
    445      1.1      cgd 		(void)close(fastfd);
    446      1.1      cgd 	}
    447      1.1      cgd }
    448      1.1      cgd 
    449      1.1      cgd #define	NOMSG	"\n\nNO LOGINS: System going down at "
    450  1.1.1.2  mycroft void
    451      1.1      cgd nolog()
    452      1.1      cgd {
    453      1.1      cgd 	int logfd;
    454  1.1.1.2  mycroft 	char *ct;
    455      1.1      cgd 
    456      1.1      cgd 	(void)unlink(_PATH_NOLOGIN);	/* in case linked to another file */
    457      1.1      cgd 	(void)signal(SIGINT, finish);
    458      1.1      cgd 	(void)signal(SIGHUP, finish);
    459      1.1      cgd 	(void)signal(SIGQUIT, finish);
    460      1.1      cgd 	(void)signal(SIGTERM, finish);
    461      1.1      cgd 	if ((logfd = open(_PATH_NOLOGIN, O_WRONLY|O_CREAT|O_TRUNC,
    462      1.1      cgd 	    0664)) >= 0) {
    463      1.1      cgd 		(void)write(logfd, NOMSG, sizeof(NOMSG) - 1);
    464      1.1      cgd 		ct = ctime(&shuttime);
    465      1.1      cgd 		(void)write(logfd, ct + 11, 5);
    466      1.1      cgd 		(void)write(logfd, "\n\n", 2);
    467      1.1      cgd 		(void)write(logfd, mbuf, strlen(mbuf));
    468      1.1      cgd 		(void)close(logfd);
    469      1.1      cgd 	}
    470      1.1      cgd }
    471      1.1      cgd 
    472      1.1      cgd void
    473  1.1.1.2  mycroft finish(signo)
    474  1.1.1.2  mycroft 	int signo;
    475      1.1      cgd {
    476  1.1.1.3    lukem 	if (!killflg)
    477  1.1.1.3    lukem 		(void)unlink(_PATH_NOLOGIN);
    478      1.1      cgd 	exit(0);
    479      1.1      cgd }
    480      1.1      cgd 
    481  1.1.1.2  mycroft void
    482      1.1      cgd badtime()
    483      1.1      cgd {
    484      1.1      cgd 	(void)fprintf(stderr, "shutdown: bad time format.\n");
    485      1.1      cgd 	exit(1);
    486      1.1      cgd }
    487      1.1      cgd 
    488  1.1.1.2  mycroft void
    489      1.1      cgd usage()
    490      1.1      cgd {
    491      1.1      cgd 	fprintf(stderr, "usage: shutdown [-fhknr] shutdowntime [ message ]\n");
    492      1.1      cgd 	exit(1);
    493      1.1      cgd }
    494