Home | History | Annotate | Line # | Download | only in shutdown
shutdown.c revision 1.12
      1 /*	$NetBSD: shutdown.c,v 1.12 1997/07/09 02:39:38 jtk Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1988, 1990, 1993
      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
     37 static char copyright[] =
     38 "@(#) Copyright (c) 1988, 1990, 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
     44 static char sccsid[] = "@(#)shutdown.c	8.2 (Berkeley) 2/16/94";
     45 #else
     46 static char rcsid[] = "$NetBSD: shutdown.c,v 1.12 1997/07/09 02:39:38 jtk Exp $";
     47 #endif
     48 #endif /* not lint */
     49 
     50 #include <sys/param.h>
     51 #include <sys/time.h>
     52 #include <sys/resource.h>
     53 #include <sys/syslog.h>
     54 
     55 #include <ctype.h>
     56 #include <fcntl.h>
     57 #include <pwd.h>
     58 #include <setjmp.h>
     59 #include <signal.h>
     60 #include <stdio.h>
     61 #include <stdlib.h>
     62 #include <string.h>
     63 #include <tzfile.h>
     64 #include <unistd.h>
     65 
     66 #include "pathnames.h"
     67 
     68 #ifdef DEBUG
     69 #undef _PATH_NOLOGIN
     70 #define	_PATH_NOLOGIN	"./nologin"
     71 #undef _PATH_FASTBOOT
     72 #define	_PATH_FASTBOOT	"./fastboot"
     73 #endif
     74 
     75 #define	H		*60*60
     76 #define	M		*60
     77 #define	S		*1
     78 #define	NOLOG_TIME	5*60
     79 struct interval {
     80 	int timeleft, timetowait;
     81 } tlist[] = {
     82 	10 H,  5 H,	 5 H,  3 H,	 2 H,  1 H,	1 H, 30 M,
     83 	30 M, 10 M,	20 M, 10 M,	10 M,  5 M,	5 M,  3 M,
     84 	 2 M,  1 M,	 1 M, 30 S,	30 S, 30 S,
     85 	 0, 0,
     86 };
     87 #undef H
     88 #undef M
     89 #undef S
     90 
     91 static time_t offset, shuttime;
     92 static int dofast, dohalt, doreboot, killflg, mbuflen, nosync, dodump;
     93 static char *whom, mbuf[BUFSIZ];
     94 
     95 void badtime __P((void));
     96 void die_you_gravy_sucking_pig_dog __P((void));
     97 void doitfast __P((void));
     98 void finish __P((int));
     99 void getoffset __P((char *));
    100 void loop __P((void));
    101 void nolog __P((void));
    102 void timeout __P((int));
    103 void timewarn __P((int));
    104 void usage __P((void));
    105 
    106 int
    107 main(argc, argv)
    108 	int argc;
    109 	char *argv[];
    110 {
    111 	extern int optind;
    112 	register char *p, *endp;
    113 	struct passwd *pw;
    114 	int arglen, ch, len, readstdin;
    115 
    116 #ifndef DEBUG
    117 	if (geteuid()) {
    118 		(void)fprintf(stderr, "shutdown: NOT super-user\n");
    119 		exit(1);
    120 	}
    121 #endif
    122 	readstdin = 0;
    123 	while ((ch = getopt(argc, argv, "-fhknrd")) != EOF)
    124 		switch (ch) {
    125 		case '-':
    126 			readstdin = 1;
    127 			break;
    128 		case 'f':
    129 			dofast = 1;
    130 			break;
    131 		case 'h':
    132 			dohalt = 1;
    133 			break;
    134 		case 'k':
    135 			killflg = 1;
    136 			break;
    137 		case 'n':
    138 			nosync = 1;
    139 			break;
    140 		case 'r':
    141 			doreboot = 1;
    142 			break;
    143 		case 'd':
    144 			dodump = 1;
    145 			break;
    146 		case '?':
    147 		default:
    148 			usage();
    149 		}
    150 	argc -= optind;
    151 	argv += optind;
    152 
    153 	if (argc < 1)
    154 		usage();
    155 
    156 	if (dofast && nosync) {
    157 		(void)fprintf(stderr,
    158 		    "shutdown: incompatible switches -f and -n.\n");
    159 		usage();
    160 	}
    161 	if (doreboot && dohalt) {
    162 		(void)fprintf(stderr,
    163 		    "shutdown: incompatible switches -h and -r.\n");
    164 		usage();
    165 	}
    166 	if (dodump && dohalt) {
    167 		(void)fprintf(stderr,
    168 		    "shutdown: incompatible switches -h and -d.\n");
    169 		usage();
    170 	}
    171 	getoffset(*argv++);
    172 
    173 	if (*argv) {
    174 		for (p = mbuf, len = sizeof(mbuf); *argv; ++argv) {
    175 			arglen = strlen(*argv);
    176 			if ((len -= arglen) <= 2)
    177 				break;
    178 			if (p != mbuf)
    179 				*p++ = ' ';
    180 			memcpy(p, *argv, arglen);
    181 			p += arglen;
    182 		}
    183 		*p = '\n';
    184 		*++p = '\0';
    185 	}
    186 
    187 	if (readstdin) {
    188 		p = mbuf;
    189 		endp = mbuf + sizeof(mbuf) - 2;
    190 		for (;;) {
    191 			if (!fgets(p, endp - p + 1, stdin))
    192 				break;
    193 			for (; *p &&  p < endp; ++p);
    194 			if (p == endp) {
    195 				*p = '\n';
    196 				*++p = '\0';
    197 				break;
    198 			}
    199 		}
    200 	}
    201 	mbuflen = strlen(mbuf);
    202 
    203 	if (offset)
    204 		(void)printf("Shutdown at %.24s.\n", ctime(&shuttime));
    205 	else
    206 		(void)printf("Shutdown NOW!\n");
    207 
    208 	if (!(whom = getlogin()))
    209 		whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???";
    210 
    211 #ifdef DEBUG
    212 	(void)putc('\n', stdout);
    213 #else
    214 	(void)setpriority(PRIO_PROCESS, 0, PRIO_MIN);
    215 	{
    216 		int forkpid;
    217 
    218 		forkpid = fork();
    219 		if (forkpid == -1) {
    220 			perror("shutdown: fork");
    221 			exit(1);
    222 		}
    223 		if (forkpid) {
    224 			(void)printf("shutdown: [pid %d]\n", forkpid);
    225 			exit(0);
    226 		}
    227 	}
    228 #endif
    229 	openlog("shutdown", LOG_CONS, LOG_AUTH);
    230 	loop();
    231 	/* NOTREACHED */
    232 }
    233 
    234 void
    235 loop()
    236 {
    237 	struct interval *tp;
    238 	u_int sltime;
    239 	int logged;
    240 
    241 	if (offset <= NOLOG_TIME) {
    242 		logged = 1;
    243 		nolog();
    244 	}
    245 	else
    246 		logged = 0;
    247 	tp = tlist;
    248 	if (tp->timeleft < offset)
    249 		(void)sleep((u_int)(offset - tp->timeleft));
    250 	else {
    251 		while (offset < tp->timeleft)
    252 			++tp;
    253 		/*
    254 		 * Warn now, if going to sleep more than a fifth of
    255 		 * the next wait time.
    256 		 */
    257 		if (sltime = offset - tp->timeleft) {
    258 			if (sltime > tp->timetowait / 5)
    259 				timewarn(offset);
    260 			(void)sleep(sltime);
    261 		}
    262 	}
    263 	for (;; ++tp) {
    264 		timewarn(tp->timeleft);
    265 		if (!logged && tp->timeleft <= NOLOG_TIME) {
    266 			logged = 1;
    267 			nolog();
    268 		}
    269 		(void)sleep((u_int)tp->timetowait);
    270 		if (!tp->timeleft)
    271 			break;
    272 	}
    273 	die_you_gravy_sucking_pig_dog();
    274 }
    275 
    276 static jmp_buf alarmbuf;
    277 
    278 void
    279 timewarn(timeleft)
    280 	int timeleft;
    281 {
    282 	static int first;
    283 	static char hostname[MAXHOSTNAMELEN + 1];
    284 	FILE *pf;
    285 	char wcmd[MAXPATHLEN + 4];
    286 
    287 	if (!first++)
    288 		(void)gethostname(hostname, sizeof(hostname));
    289 
    290 	/* undoc -n option to wall suppresses normal wall banner */
    291 	(void)snprintf(wcmd, sizeof(wcmd), "%s -n", _PATH_WALL);
    292 	if (!(pf = popen(wcmd, "w"))) {
    293 		syslog(LOG_ERR, "shutdown: can't find %s: %m", _PATH_WALL);
    294 		return;
    295 	}
    296 
    297 	(void)fprintf(pf,
    298 	    "\007*** %sSystem shutdown message from %s@%s ***\007\n",
    299 	    timeleft ? "": "FINAL ", whom, hostname);
    300 
    301 	if (timeleft > 10*60)
    302 		(void)fprintf(pf, "System going down at %5.5s\n\n",
    303 		    ctime(&shuttime) + 11);
    304 	else if (timeleft > 59)
    305 		(void)fprintf(pf, "System going down in %d minute%s\n\n",
    306 		    timeleft / 60, (timeleft > 60) ? "s" : "");
    307 	else if (timeleft)
    308 		(void)fprintf(pf, "System going down in 30 seconds\n\n");
    309 	else
    310 		(void)fprintf(pf, "System going down IMMEDIATELY\n\n");
    311 
    312 	if (mbuflen)
    313 		(void)fwrite(mbuf, sizeof(*mbuf), mbuflen, pf);
    314 
    315 	/*
    316 	 * play some games, just in case wall doesn't come back
    317 	 * probably unecessary, given that wall is careful.
    318 	 */
    319 	if (!setjmp(alarmbuf)) {
    320 		(void)signal(SIGALRM, timeout);
    321 		(void)alarm((u_int)30);
    322 		(void)pclose(pf);
    323 		(void)alarm((u_int)0);
    324 		(void)signal(SIGALRM, SIG_DFL);
    325 	}
    326 }
    327 
    328 void
    329 timeout(signo)
    330 	int signo;
    331 {
    332 	longjmp(alarmbuf, 1);
    333 }
    334 
    335 void
    336 die_you_gravy_sucking_pig_dog()
    337 {
    338 
    339 	syslog(LOG_NOTICE, "%s by %s: %s",
    340 	    doreboot || dodump ? "reboot" : dohalt ? "halt" : "shutdown", whom, mbuf);
    341 	(void)sleep(2);
    342 
    343 	(void)printf("\r\nSystem shutdown time has arrived\007\007\r\n");
    344 	if (killflg) {
    345 		(void)printf("\rbut you'll have to do it yourself\r\n");
    346 		finish(0);
    347 	}
    348 	if (dofast)
    349 		doitfast();
    350 #ifdef DEBUG
    351 	if (doreboot || dodump)
    352 		(void)printf("reboot%s", dodump ? " -d" : "");
    353 	else if (dohalt)
    354 		(void)printf("halt");
    355 	if (nosync)
    356 		(void)printf(" no sync");
    357 	if (dofast)
    358 		(void)printf(" no fsck");
    359 	(void)printf("\nkill -HUP 1\n");
    360 #else
    361 	if (doreboot || dodump) {
    362 		execle(_PATH_REBOOT, "reboot",
    363 		       dodump ? (nosync ? "-ldn" : "-ld") :
    364 		       (nosync ? "-ln" : "-l"), 0, (char **)0);
    365 		syslog(LOG_ERR, "shutdown: can't exec %s: %m.", _PATH_REBOOT);
    366 		perror("shutdown");
    367 	}
    368 	else if (dohalt) {
    369 		execle(_PATH_HALT, "halt", nosync ? "-ln" : "-l",
    370 		    (char *)0, (char **)0);
    371 		syslog(LOG_ERR, "shutdown: can't exec %s: %m.", _PATH_HALT);
    372 		perror("shutdown");
    373 	}
    374 	(void)kill(1, SIGTERM);		/* to single user */
    375 #endif
    376 	finish(0);
    377 }
    378 
    379 #define	ATOI2(p)	(p[0] - '0') * 10 + (p[1] - '0'); p += 2;
    380 
    381 void
    382 getoffset(timearg)
    383 	register char *timearg;
    384 {
    385 	register struct tm *lt;
    386 	register char *p;
    387 	time_t now;
    388 
    389 	if (!strcasecmp(timearg, "now")) {		/* now */
    390 		offset = 0;
    391 		return;
    392 	}
    393 
    394 	(void)time(&now);
    395 	if (*timearg == '+') {				/* +minutes */
    396 		if (!isdigit(*++timearg))
    397 			badtime();
    398 		offset = atoi(timearg) * 60;
    399 		shuttime = now + offset;
    400 		return;
    401 	}
    402 
    403 	/* handle hh:mm by getting rid of the colon */
    404 	for (p = timearg; *p; ++p)
    405 		if (!isascii(*p) || !isdigit(*p))
    406 			if (*p == ':' && strlen(p) == 3) {
    407 				p[0] = p[1];
    408 				p[1] = p[2];
    409 				p[2] = '\0';
    410 			}
    411 			else
    412 				badtime();
    413 
    414 	unsetenv("TZ");					/* OUR timezone */
    415 	lt = localtime(&now);				/* current time val */
    416 
    417 	switch(strlen(timearg)) {
    418 	case 10:
    419 		lt->tm_year = ATOI2(timearg);
    420 		/* FALLTHROUGH */
    421 	case 8:
    422 		lt->tm_mon = ATOI2(timearg);
    423 		if (--lt->tm_mon < 0 || lt->tm_mon > 11)
    424 			badtime();
    425 		/* FALLTHROUGH */
    426 	case 6:
    427 		lt->tm_mday = ATOI2(timearg);
    428 		if (lt->tm_mday < 1 || lt->tm_mday > 31)
    429 			badtime();
    430 		/* FALLTHROUGH */
    431 	case 4:
    432 		lt->tm_hour = ATOI2(timearg);
    433 		if (lt->tm_hour < 0 || lt->tm_hour > 23)
    434 			badtime();
    435 		lt->tm_min = ATOI2(timearg);
    436 		if (lt->tm_min < 0 || lt->tm_min > 59)
    437 			badtime();
    438 		lt->tm_sec = 0;
    439 		if ((shuttime = mktime(lt)) == -1)
    440 			badtime();
    441 		if ((offset = shuttime - now) < 0) {
    442 			(void)fprintf(stderr,
    443 			    "shutdown: that time is already past.\n");
    444 			exit(1);
    445 		}
    446 		break;
    447 	default:
    448 		badtime();
    449 	}
    450 }
    451 
    452 #define	FSMSG	"fastboot file for fsck\n"
    453 void
    454 doitfast()
    455 {
    456 	int fastfd;
    457 
    458 	if ((fastfd = open(_PATH_FASTBOOT, O_WRONLY|O_CREAT|O_TRUNC,
    459 	    0664)) >= 0) {
    460 		(void)write(fastfd, FSMSG, sizeof(FSMSG) - 1);
    461 		(void)close(fastfd);
    462 	}
    463 }
    464 
    465 #define	NOMSG	"\n\nNO LOGINS: System going down at "
    466 void
    467 nolog()
    468 {
    469 	int logfd;
    470 	char *ct;
    471 
    472 	(void)unlink(_PATH_NOLOGIN);	/* in case linked to another file */
    473 	(void)signal(SIGINT, finish);
    474 	(void)signal(SIGHUP, finish);
    475 	(void)signal(SIGQUIT, finish);
    476 	(void)signal(SIGTERM, finish);
    477 	if ((logfd = open(_PATH_NOLOGIN, O_WRONLY|O_CREAT|O_TRUNC,
    478 	    0664)) >= 0) {
    479 		(void)write(logfd, NOMSG, sizeof(NOMSG) - 1);
    480 		ct = ctime(&shuttime);
    481 		(void)write(logfd, ct + 11, 5);
    482 		(void)write(logfd, "\n\n", 2);
    483 		(void)write(logfd, mbuf, strlen(mbuf));
    484 		(void)close(logfd);
    485 	}
    486 }
    487 
    488 void
    489 finish(signo)
    490 	int signo;
    491 {
    492 	if (!killflg)
    493 		(void)unlink(_PATH_NOLOGIN);
    494 	exit(0);
    495 }
    496 
    497 void
    498 badtime()
    499 {
    500 	(void)fprintf(stderr, "shutdown: bad time format.\n");
    501 	exit(1);
    502 }
    503 
    504 void
    505 usage()
    506 {
    507 	fprintf(stderr, "usage: shutdown [-fhknrd] shutdowntime [ message ]\n");
    508 	exit(1);
    509 }
    510