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