Home | History | Annotate | Line # | Download | only in shutdown
shutdown.c revision 1.52
      1  1.52       riz /*	$NetBSD: shutdown.c,v 1.52 2010/06/09 04:51:53 riz Exp $	*/
      2   1.9       cgd 
      3   1.1       cgd /*
      4   1.7   mycroft  * Copyright (c) 1988, 1990, 1993
      5   1.7   mycroft  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1       cgd  * modification, are permitted provided that the following conditions
      9   1.1       cgd  * are met:
     10   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15  1.40       agc  * 3. Neither the name of the University nor the names of its contributors
     16   1.1       cgd  *    may be used to endorse or promote products derived from this software
     17   1.1       cgd  *    without specific prior written permission.
     18   1.1       cgd  *
     19   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.1       cgd  * SUCH DAMAGE.
     30   1.1       cgd  */
     31   1.1       cgd 
     32  1.14     lukem #include <sys/cdefs.h>
     33   1.1       cgd #ifndef lint
     34  1.51     lukem __COPYRIGHT("@(#) Copyright (c) 1988, 1990, 1993\
     35  1.51     lukem  The Regents of the University of California.  All rights reserved.");
     36   1.1       cgd #endif /* not lint */
     37   1.1       cgd 
     38   1.1       cgd #ifndef lint
     39   1.9       cgd #if 0
     40  1.15     lukem static char sccsid[] = "@(#)shutdown.c	8.4 (Berkeley) 4/28/95";
     41   1.9       cgd #else
     42  1.52       riz __RCSID("$NetBSD: shutdown.c,v 1.52 2010/06/09 04:51:53 riz Exp $");
     43   1.9       cgd #endif
     44   1.1       cgd #endif /* not lint */
     45   1.1       cgd 
     46   1.1       cgd #include <sys/param.h>
     47   1.1       cgd #include <sys/time.h>
     48   1.1       cgd #include <sys/resource.h>
     49   1.1       cgd #include <sys/syslog.h>
     50   1.7   mycroft 
     51   1.7   mycroft #include <ctype.h>
     52  1.19   mycroft #include <err.h>
     53   1.7   mycroft #include <fcntl.h>
     54   1.7   mycroft #include <pwd.h>
     55   1.1       cgd #include <setjmp.h>
     56   1.7   mycroft #include <signal.h>
     57   1.7   mycroft #include <stdio.h>
     58   1.7   mycroft #include <stdlib.h>
     59   1.7   mycroft #include <string.h>
     60  1.26    kleink #include <time.h>
     61   1.1       cgd #include <tzfile.h>
     62   1.7   mycroft #include <unistd.h>
     63  1.47  christos #include <errno.h>
     64   1.7   mycroft 
     65   1.1       cgd #include "pathnames.h"
     66   1.1       cgd 
     67   1.1       cgd #ifdef DEBUG
     68   1.1       cgd #undef _PATH_NOLOGIN
     69   1.1       cgd #define	_PATH_NOLOGIN	"./nologin"
     70   1.1       cgd #undef _PATH_FASTBOOT
     71   1.1       cgd #define	_PATH_FASTBOOT	"./fastboot"
     72   1.1       cgd #endif
     73   1.1       cgd 
     74   1.1       cgd #define	H		*60*60
     75   1.1       cgd #define	M		*60
     76   1.1       cgd #define	S		*1
     77   1.1       cgd #define	NOLOG_TIME	5*60
     78  1.47  christos static const struct interval {
     79  1.47  christos 	time_t timeleft, timetowait;
     80   1.1       cgd } tlist[] = {
     81  1.13     mikel 	{ 10 H,  5 H },	{  5 H,  3 H },	{  2 H,  1 H },	{ 1 H, 30 M },
     82  1.13     mikel 	{ 30 M, 10 M },	{ 20 M, 10 M },	{ 10 M,  5 M },	{ 5 M,  3 M },
     83  1.13     mikel 	{  2 M,  1 M },	{  1 M, 30 S },	{ 30 S, 30 S },
     84  1.13     mikel 	{  0, 0 }
     85   1.7   mycroft };
     86   1.1       cgd #undef H
     87   1.1       cgd #undef M
     88   1.1       cgd #undef S
     89   1.1       cgd 
     90   1.1       cgd static time_t offset, shuttime;
     91  1.47  christos static int dofast, dohalt, doreboot, killflg, nofork, nosync, dodump;
     92  1.47  christos static size_t mbuflen;
     93  1.27   thorpej static int dopowerdown;
     94  1.29   mycroft static const char *whom;
     95  1.29   mycroft static char mbuf[BUFSIZ];
     96  1.47  christos static char *bootstr;
     97   1.1       cgd 
     98  1.49     perry static void badtime(void) __dead;
     99  1.49     perry static void die_you_gravy_sucking_pig_dog(void) __dead;
    100  1.47  christos static void doitfast(void);
    101  1.43   xtraeme void dorcshutdown(void);
    102  1.49     perry static void finish(int) __dead;
    103  1.47  christos static void getoffset(char *);
    104  1.47  christos static void loop(void);
    105  1.47  christos static void nolog(void);
    106  1.47  christos static void timeout(int);
    107  1.47  christos static void timewarn(time_t);
    108  1.49     perry static void usage(void) __dead;
    109   1.5       cgd 
    110   1.7   mycroft int
    111  1.43   xtraeme main(int argc, char *argv[])
    112   1.1       cgd {
    113  1.14     lukem 	char *p, *endp;
    114   1.7   mycroft 	struct passwd *pw;
    115  1.47  christos 	size_t arglen, len;
    116  1.47  christos 	int ch;
    117   1.1       cgd 
    118  1.47  christos 	(void)setprogname(argv[0]);
    119   1.1       cgd #ifndef DEBUG
    120  1.19   mycroft 	if (geteuid())
    121  1.50  dholland 		errx(1, "%s: Not super-user", strerror(EPERM));
    122   1.1       cgd #endif
    123  1.39    atatat 	while ((ch = getopt(argc, argv, "b:Ddfhknpr")) != -1)
    124   1.1       cgd 		switch (ch) {
    125  1.39    atatat 		case 'b':
    126  1.39    atatat 			bootstr = optarg;
    127  1.39    atatat 			break;
    128  1.19   mycroft 		case 'd':
    129  1.19   mycroft 			dodump = 1;
    130   1.1       cgd 			break;
    131  1.25      fair 		case 'D':
    132  1.25      fair 			nofork = 1;
    133  1.25      fair 			break;
    134   1.1       cgd 		case 'f':
    135   1.1       cgd 			dofast = 1;
    136   1.1       cgd 			break;
    137  1.27   thorpej 		case 'p':
    138  1.27   thorpej 			dopowerdown = 1;
    139  1.27   thorpej 			/* FALLTHROUGH */
    140   1.1       cgd 		case 'h':
    141   1.1       cgd 			dohalt = 1;
    142   1.1       cgd 			break;
    143   1.1       cgd 		case 'k':
    144   1.1       cgd 			killflg = 1;
    145   1.1       cgd 			break;
    146   1.1       cgd 		case 'n':
    147  1.11     mikel 			nosync = 1;
    148   1.1       cgd 			break;
    149   1.1       cgd 		case 'r':
    150   1.1       cgd 			doreboot = 1;
    151   1.1       cgd 			break;
    152   1.1       cgd 		case '?':
    153   1.1       cgd 		default:
    154   1.1       cgd 			usage();
    155   1.1       cgd 		}
    156   1.1       cgd 	argc -= optind;
    157   1.1       cgd 	argv += optind;
    158   1.1       cgd 
    159   1.1       cgd 	if (argc < 1)
    160   1.1       cgd 		usage();
    161   1.1       cgd 
    162  1.20   mycroft 	if (dodump && !dohalt && !doreboot)
    163  1.20   mycroft 		doreboot = 1;
    164  1.20   mycroft 
    165   1.1       cgd 	if (dofast && nosync) {
    166  1.50  dholland 		warnx("Incompatible options -f and -n");
    167   1.1       cgd 		usage();
    168   1.1       cgd 	}
    169  1.20   mycroft 	if (dohalt && doreboot) {
    170  1.27   thorpej 		const char *which_flag = dopowerdown ? "p" : "h";
    171  1.27   thorpej 
    172  1.50  dholland 		warnx("Incompatible options -%s and -r", which_flag);
    173  1.12       jtk 		usage();
    174  1.12       jtk 	}
    175  1.19   mycroft 
    176   1.1       cgd 	getoffset(*argv++);
    177   1.1       cgd 
    178  1.30      ross 	if (argv[0]) {
    179  1.21   mycroft 		if (strcmp(argv[0], "-") || argv[1]) {
    180  1.19   mycroft 			for (p = mbuf, len = sizeof(mbuf); *argv; ++argv) {
    181  1.19   mycroft 				arglen = strlen(*argv);
    182  1.19   mycroft 				if ((len -= arglen) <= 2)
    183  1.19   mycroft 					break;
    184  1.19   mycroft 				if (p != mbuf)
    185  1.19   mycroft 					*p++ = ' ';
    186  1.47  christos 				(void)memmove(p, *argv, arglen);
    187  1.19   mycroft 				p += arglen;
    188  1.19   mycroft 			}
    189  1.19   mycroft 			*p = '\n';
    190  1.19   mycroft 			*++p = '\0';
    191  1.19   mycroft 		} else {
    192  1.19   mycroft 			p = mbuf;
    193  1.19   mycroft 			endp = mbuf + sizeof(mbuf) - 2;
    194  1.19   mycroft 			for (;;) {
    195  1.19   mycroft 				if (!fgets(p, endp - p + 1, stdin))
    196  1.19   mycroft 					break;
    197  1.19   mycroft 				for (; *p &&  p < endp; ++p);
    198  1.19   mycroft 				if (p == endp) {
    199  1.19   mycroft 					*p = '\n';
    200  1.19   mycroft 					*++p = '\0';
    201  1.19   mycroft 					break;
    202  1.19   mycroft 				}
    203   1.1       cgd 			}
    204   1.1       cgd 		}
    205  1.30      ross 	}
    206   1.1       cgd 	mbuflen = strlen(mbuf);
    207   1.1       cgd 
    208   1.1       cgd 	if (offset)
    209   1.1       cgd 		(void)printf("Shutdown at %.24s.\n", ctime(&shuttime));
    210   1.1       cgd 	else
    211   1.1       cgd 		(void)printf("Shutdown NOW!\n");
    212   1.1       cgd 
    213   1.1       cgd 	if (!(whom = getlogin()))
    214   1.1       cgd 		whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???";
    215   1.1       cgd 
    216   1.1       cgd #ifdef DEBUG
    217   1.1       cgd 	(void)putc('\n', stdout);
    218   1.1       cgd #else
    219   1.1       cgd 	(void)setpriority(PRIO_PROCESS, 0, PRIO_MIN);
    220  1.25      fair 	if (nofork == 0) {
    221   1.1       cgd 		int forkpid;
    222   1.1       cgd 
    223   1.1       cgd 		forkpid = fork();
    224   1.1       cgd 		if (forkpid == -1) {
    225   1.1       cgd 			perror("shutdown: fork");
    226   1.1       cgd 			exit(1);
    227   1.1       cgd 		}
    228   1.1       cgd 		if (forkpid) {
    229   1.1       cgd 			(void)printf("shutdown: [pid %d]\n", forkpid);
    230   1.1       cgd 			exit(0);
    231   1.1       cgd 		}
    232  1.46   jnemeth 		(void)setsid();
    233   1.1       cgd 	}
    234   1.1       cgd #endif
    235   1.1       cgd 	openlog("shutdown", LOG_CONS, LOG_AUTH);
    236   1.1       cgd 	loop();
    237   1.7   mycroft 	/* NOTREACHED */
    238  1.13     mikel #ifdef __GNUC__
    239  1.13     mikel 	return 1;
    240  1.13     mikel #endif
    241   1.1       cgd }
    242   1.1       cgd 
    243   1.7   mycroft void
    244  1.43   xtraeme loop(void)
    245   1.1       cgd {
    246  1.47  christos 	const struct interval *tp;
    247   1.1       cgd 	u_int sltime;
    248   1.1       cgd 	int logged;
    249   1.1       cgd 
    250   1.1       cgd 	if (offset <= NOLOG_TIME) {
    251   1.1       cgd 		logged = 1;
    252   1.1       cgd 		nolog();
    253   1.1       cgd 	}
    254   1.1       cgd 	else
    255   1.1       cgd 		logged = 0;
    256   1.1       cgd 	tp = tlist;
    257   1.1       cgd 	if (tp->timeleft < offset)
    258   1.1       cgd 		(void)sleep((u_int)(offset - tp->timeleft));
    259   1.1       cgd 	else {
    260   1.1       cgd 		while (offset < tp->timeleft)
    261   1.1       cgd 			++tp;
    262   1.1       cgd 		/*
    263   1.7   mycroft 		 * Warn now, if going to sleep more than a fifth of
    264   1.1       cgd 		 * the next wait time.
    265   1.1       cgd 		 */
    266  1.13     mikel 		if ((sltime = offset - tp->timeleft) != 0) {
    267   1.1       cgd 			if (sltime > tp->timetowait / 5)
    268   1.7   mycroft 				timewarn(offset);
    269   1.1       cgd 			(void)sleep(sltime);
    270   1.1       cgd 		}
    271   1.1       cgd 	}
    272   1.1       cgd 	for (;; ++tp) {
    273   1.7   mycroft 		timewarn(tp->timeleft);
    274   1.1       cgd 		if (!logged && tp->timeleft <= NOLOG_TIME) {
    275   1.1       cgd 			logged = 1;
    276   1.1       cgd 			nolog();
    277   1.1       cgd 		}
    278   1.1       cgd 		(void)sleep((u_int)tp->timetowait);
    279   1.1       cgd 		if (!tp->timeleft)
    280   1.1       cgd 			break;
    281   1.1       cgd 	}
    282   1.1       cgd 	die_you_gravy_sucking_pig_dog();
    283   1.1       cgd }
    284   1.1       cgd 
    285   1.1       cgd static jmp_buf alarmbuf;
    286   1.1       cgd 
    287   1.7   mycroft void
    288  1.47  christos timewarn(time_t timeleft)
    289   1.1       cgd {
    290   1.1       cgd 	static int first;
    291   1.1       cgd 	static char hostname[MAXHOSTNAMELEN + 1];
    292   1.7   mycroft 	FILE *pf;
    293   1.1       cgd 	char wcmd[MAXPATHLEN + 4];
    294   1.1       cgd 
    295  1.28       mrg 	if (!first++) {
    296   1.1       cgd 		(void)gethostname(hostname, sizeof(hostname));
    297  1.28       mrg 		hostname[sizeof(hostname) - 1] = '\0';
    298  1.28       mrg 	}
    299   1.1       cgd 
    300   1.1       cgd 	/* undoc -n option to wall suppresses normal wall banner */
    301  1.28       mrg 	(void)snprintf(wcmd, sizeof wcmd, "%s -n", _PATH_WALL);
    302  1.47  christos 	if ((pf = popen(wcmd, "w")) == NULL) {
    303  1.47  christos 		syslog(LOG_ERR, "%s: Can't find `%s' (%m)", getprogname(),
    304  1.47  christos 		    _PATH_WALL);
    305   1.1       cgd 		return;
    306   1.1       cgd 	}
    307   1.1       cgd 
    308   1.1       cgd 	(void)fprintf(pf,
    309   1.1       cgd 	    "\007*** %sSystem shutdown message from %s@%s ***\007\n",
    310   1.5       cgd 	    timeleft ? "": "FINAL ", whom, hostname);
    311   1.1       cgd 
    312   1.5       cgd 	if (timeleft > 10*60)
    313   1.1       cgd 		(void)fprintf(pf, "System going down at %5.5s\n\n",
    314   1.1       cgd 		    ctime(&shuttime) + 11);
    315   1.5       cgd 	else if (timeleft > 59)
    316  1.47  christos 		(void)fprintf(pf, "System going down in %ld minute%s\n\n",
    317  1.47  christos 		    (long)timeleft / 60, (timeleft > 60) ? "s" : "");
    318   1.5       cgd 	else if (timeleft)
    319   1.1       cgd 		(void)fprintf(pf, "System going down in 30 seconds\n\n");
    320   1.1       cgd 	else
    321   1.1       cgd 		(void)fprintf(pf, "System going down IMMEDIATELY\n\n");
    322   1.1       cgd 
    323   1.1       cgd 	if (mbuflen)
    324   1.1       cgd 		(void)fwrite(mbuf, sizeof(*mbuf), mbuflen, pf);
    325   1.1       cgd 
    326   1.1       cgd 	/*
    327   1.1       cgd 	 * play some games, just in case wall doesn't come back
    328  1.41   dsainty 	 * probably unnecessary, given that wall is careful.
    329   1.1       cgd 	 */
    330   1.1       cgd 	if (!setjmp(alarmbuf)) {
    331   1.1       cgd 		(void)signal(SIGALRM, timeout);
    332   1.1       cgd 		(void)alarm((u_int)30);
    333   1.1       cgd 		(void)pclose(pf);
    334   1.1       cgd 		(void)alarm((u_int)0);
    335   1.1       cgd 		(void)signal(SIGALRM, SIG_DFL);
    336   1.1       cgd 	}
    337   1.1       cgd }
    338   1.1       cgd 
    339  1.47  christos static void
    340  1.47  christos /*ARGSUSED*/
    341  1.43   xtraeme timeout(int signo)
    342   1.1       cgd {
    343   1.1       cgd 	longjmp(alarmbuf, 1);
    344   1.1       cgd }
    345   1.1       cgd 
    346  1.47  christos static void
    347  1.44   xtraeme die_you_gravy_sucking_pig_dog(void)
    348   1.1       cgd {
    349  1.50  dholland 	const char *what;
    350   1.1       cgd 
    351  1.50  dholland 	if (doreboot) {
    352  1.50  dholland 		what = "reboot";
    353  1.50  dholland 	} else if (dohalt && dopowerdown) {
    354  1.50  dholland 		what = "poweroff";
    355  1.50  dholland 	} else if (dohalt) {
    356  1.50  dholland 		what = "halt";
    357  1.50  dholland 	} else {
    358  1.50  dholland 		what = "shutdown";
    359  1.50  dholland 	}
    360  1.50  dholland 
    361  1.50  dholland 	syslog(LOG_NOTICE, "%s by %s: %s", what, whom, mbuf);
    362   1.1       cgd 	(void)sleep(2);
    363   1.1       cgd 
    364   1.1       cgd 	(void)printf("\r\nSystem shutdown time has arrived\007\007\r\n");
    365   1.1       cgd 	if (killflg) {
    366   1.1       cgd 		(void)printf("\rbut you'll have to do it yourself\r\n");
    367   1.7   mycroft 		finish(0);
    368   1.1       cgd 	}
    369   1.1       cgd 	if (dofast)
    370   1.1       cgd 		doitfast();
    371  1.33       bad 	dorcshutdown();
    372  1.20   mycroft 	if (doreboot || dohalt) {
    373  1.45  christos 		const char *args[16];
    374  1.45  christos 		const char **arg, *path;
    375  1.48   jnemeth #ifndef DEBUG
    376  1.47  christos 		int serrno;
    377  1.48   jnemeth #endif
    378  1.20   mycroft 
    379  1.20   mycroft 		arg = &args[0];
    380  1.20   mycroft 		if (doreboot) {
    381  1.20   mycroft 			path = _PATH_REBOOT;
    382  1.20   mycroft 			*arg++ = "reboot";
    383  1.20   mycroft 		} else {
    384  1.20   mycroft 			path = _PATH_HALT;
    385  1.20   mycroft 			*arg++ = "halt";
    386  1.20   mycroft 		}
    387  1.20   mycroft 		if (dodump)
    388  1.20   mycroft 			*arg++ = "-d";
    389  1.20   mycroft 		if (nosync)
    390  1.20   mycroft 			*arg++ = "-n";
    391  1.27   thorpej 		if (dopowerdown)
    392  1.27   thorpej 			*arg++ = "-p";
    393  1.20   mycroft 		*arg++ = "-l";
    394  1.39    atatat 		if (bootstr)
    395  1.39    atatat 			*arg++ = bootstr;
    396  1.20   mycroft 		*arg++ = 0;
    397  1.20   mycroft #ifndef DEBUG
    398  1.52       riz 		(void)unlink(_PATH_NOLOGIN);
    399  1.47  christos 		(void)execve(path, __UNCONST(args), NULL);
    400  1.47  christos 		serrno = errno;
    401  1.50  dholland 		syslog(LOG_ERR, "Can't exec `%s' (%m)", path);
    402  1.47  christos 		errno = serrno;
    403  1.47  christos 		warn("Can't exec `%s'", path);
    404  1.20   mycroft #else
    405  1.20   mycroft 		printf("%s", path);
    406  1.20   mycroft 		for (arg = &args[0]; *arg; arg++)
    407  1.20   mycroft 			printf(" %s", *arg);
    408  1.20   mycroft 		printf("\n");
    409  1.20   mycroft #endif
    410  1.20   mycroft 	} else {
    411  1.20   mycroft #ifndef DEBUG
    412  1.20   mycroft 		(void)kill(1, SIGTERM);		/* to single user */
    413   1.1       cgd #else
    414  1.20   mycroft 		printf("kill 1\n");
    415  1.20   mycroft #endif
    416   1.1       cgd 	}
    417   1.7   mycroft 	finish(0);
    418   1.1       cgd }
    419   1.1       cgd 
    420  1.23   mycroft #define	ATOI2(s)	((s) += 2, ((s)[-2] - '0') * 10 + ((s)[-1] - '0'))
    421   1.1       cgd 
    422   1.7   mycroft void
    423  1.43   xtraeme getoffset(char *timearg)
    424   1.1       cgd {
    425  1.14     lukem 	struct tm *lt;
    426  1.14     lukem 	char *p;
    427   1.7   mycroft 	time_t now;
    428  1.18   mycroft 	int yearset;
    429   1.1       cgd 
    430  1.32     enami 	(void)time(&now);
    431   1.1       cgd 	if (!strcasecmp(timearg, "now")) {		/* now */
    432   1.1       cgd 		offset = 0;
    433  1.32     enami 		shuttime = now;
    434   1.1       cgd 		return;
    435   1.1       cgd 	}
    436   1.1       cgd 
    437   1.1       cgd 	if (*timearg == '+') {				/* +minutes */
    438  1.42       dsl 		if (!isdigit((unsigned char)*++timearg))
    439   1.1       cgd 			badtime();
    440   1.1       cgd 		offset = atoi(timearg) * 60;
    441   1.1       cgd 		shuttime = now + offset;
    442   1.1       cgd 		return;
    443   1.1       cgd 	}
    444   1.1       cgd 
    445   1.1       cgd 	/* handle hh:mm by getting rid of the colon */
    446   1.1       cgd 	for (p = timearg; *p; ++p)
    447  1.42       dsl 		if (!isascii(*p) || !isdigit((unsigned char)*p)) {
    448   1.1       cgd 			if (*p == ':' && strlen(p) == 3) {
    449   1.1       cgd 				p[0] = p[1];
    450   1.1       cgd 				p[1] = p[2];
    451   1.1       cgd 				p[2] = '\0';
    452   1.1       cgd 			}
    453   1.1       cgd 			else
    454   1.1       cgd 				badtime();
    455  1.30      ross 		}
    456   1.1       cgd 
    457  1.47  christos 	(void)unsetenv("TZ");				/* OUR timezone */
    458   1.1       cgd 	lt = localtime(&now);				/* current time val */
    459   1.1       cgd 
    460  1.18   mycroft 	lt->tm_sec = 0;
    461  1.18   mycroft 
    462  1.18   mycroft 	yearset = 0;
    463  1.18   mycroft 	switch (strlen(timearg)) {
    464  1.18   mycroft 	case 12:
    465  1.23   mycroft 		lt->tm_year = ATOI2(timearg) * 100 - TM_YEAR_BASE;
    466  1.18   mycroft 		yearset = 1;
    467  1.18   mycroft 		/* FALLTHROUGH */
    468   1.1       cgd 	case 10:
    469  1.18   mycroft 		if (yearset) {
    470  1.23   mycroft 			lt->tm_year += ATOI2(timearg);
    471  1.18   mycroft 		} else {
    472  1.18   mycroft 			yearset = ATOI2(timearg);
    473  1.18   mycroft 			if (yearset < 69)
    474  1.23   mycroft 				lt->tm_year = yearset + 2000 - TM_YEAR_BASE;
    475  1.18   mycroft 			else
    476  1.23   mycroft 				lt->tm_year = yearset + 1900 - TM_YEAR_BASE;
    477  1.18   mycroft 		}
    478   1.1       cgd 		/* FALLTHROUGH */
    479   1.1       cgd 	case 8:
    480   1.1       cgd 		lt->tm_mon = ATOI2(timearg);
    481  1.18   mycroft 		--lt->tm_mon;
    482   1.1       cgd 		/* FALLTHROUGH */
    483   1.1       cgd 	case 6:
    484   1.1       cgd 		lt->tm_mday = ATOI2(timearg);
    485   1.1       cgd 		/* FALLTHROUGH */
    486   1.1       cgd 	case 4:
    487   1.1       cgd 		lt->tm_hour = ATOI2(timearg);
    488  1.18   mycroft 		/* FALLTHROUGH */
    489  1.18   mycroft 	case 2:
    490   1.1       cgd 		lt->tm_min = ATOI2(timearg);
    491   1.1       cgd 		break;
    492   1.1       cgd 	default:
    493   1.1       cgd 		badtime();
    494  1.18   mycroft 	}
    495  1.18   mycroft 
    496  1.18   mycroft 	if ((shuttime = mktime(lt)) == -1)
    497  1.18   mycroft 		badtime();
    498  1.19   mycroft 	if ((offset = shuttime - now) < 0)
    499  1.19   mycroft 		errx(1, "time is already past");
    500  1.33       bad }
    501  1.33       bad 
    502  1.33       bad void
    503  1.43   xtraeme dorcshutdown(void)
    504  1.33       bad {
    505  1.33       bad 	(void)printf("\r\nAbout to run shutdown hooks...\r\n");
    506  1.47  christos #ifndef DEBUG
    507  1.47  christos 	(void)setuid(0);
    508  1.38       cgd 	(void)system(". " _PATH_RCSHUTDOWN);
    509  1.47  christos #endif
    510  1.37  jdolecek 	(void)sleep(5);		/* Give operator a chance to abort this. */
    511  1.36  jdolecek 	(void)printf("\r\nDone running shutdown hooks.\r\n");
    512   1.1       cgd }
    513   1.1       cgd 
    514   1.1       cgd #define	FSMSG	"fastboot file for fsck\n"
    515   1.7   mycroft void
    516  1.43   xtraeme doitfast(void)
    517   1.1       cgd {
    518   1.1       cgd 	int fastfd;
    519   1.1       cgd 
    520   1.1       cgd 	if ((fastfd = open(_PATH_FASTBOOT, O_WRONLY|O_CREAT|O_TRUNC,
    521   1.1       cgd 	    0664)) >= 0) {
    522   1.1       cgd 		(void)write(fastfd, FSMSG, sizeof(FSMSG) - 1);
    523   1.1       cgd 		(void)close(fastfd);
    524   1.1       cgd 	}
    525   1.1       cgd }
    526   1.1       cgd 
    527   1.1       cgd #define	NOMSG	"\n\nNO LOGINS: System going down at "
    528   1.7   mycroft void
    529  1.43   xtraeme nolog(void)
    530   1.1       cgd {
    531   1.1       cgd 	int logfd;
    532   1.7   mycroft 	char *ct;
    533   1.1       cgd 
    534   1.1       cgd 	(void)unlink(_PATH_NOLOGIN);	/* in case linked to another file */
    535   1.1       cgd 	(void)signal(SIGINT, finish);
    536   1.1       cgd 	(void)signal(SIGHUP, finish);
    537   1.1       cgd 	(void)signal(SIGQUIT, finish);
    538   1.1       cgd 	(void)signal(SIGTERM, finish);
    539   1.1       cgd 	if ((logfd = open(_PATH_NOLOGIN, O_WRONLY|O_CREAT|O_TRUNC,
    540   1.1       cgd 	    0664)) >= 0) {
    541   1.1       cgd 		(void)write(logfd, NOMSG, sizeof(NOMSG) - 1);
    542   1.1       cgd 		ct = ctime(&shuttime);
    543   1.1       cgd 		(void)write(logfd, ct + 11, 5);
    544   1.1       cgd 		(void)write(logfd, "\n\n", 2);
    545   1.1       cgd 		(void)write(logfd, mbuf, strlen(mbuf));
    546   1.1       cgd 		(void)close(logfd);
    547   1.1       cgd 	}
    548   1.1       cgd }
    549   1.1       cgd 
    550  1.47  christos static void
    551  1.47  christos /*ARGSUSED*/
    552  1.43   xtraeme finish(int signo)
    553   1.1       cgd {
    554  1.28       mrg 
    555   1.6   mycroft 	if (!killflg)
    556   1.6   mycroft 		(void)unlink(_PATH_NOLOGIN);
    557   1.1       cgd 	exit(0);
    558   1.1       cgd }
    559   1.1       cgd 
    560  1.47  christos static void
    561  1.43   xtraeme badtime(void)
    562   1.1       cgd {
    563  1.28       mrg 
    564  1.19   mycroft 	warnx("illegal time format");
    565  1.19   mycroft 	usage();
    566   1.1       cgd }
    567   1.1       cgd 
    568  1.47  christos static void
    569  1.43   xtraeme usage(void)
    570   1.1       cgd {
    571  1.28       mrg 
    572  1.19   mycroft 	(void)fprintf(stderr,
    573  1.47  christos 	    "Usage: %s [-Ddfhknpr] time [message ... | -]\n",
    574  1.47  christos 	    getprogname());
    575   1.1       cgd 	exit(1);
    576   1.1       cgd }
    577