xref: /csrg-svn/sbin/shutdown/shutdown.c (revision 69011)
121179Sdist /*
261547Sbostic  * Copyright (c) 1988, 1990, 1993
361547Sbostic  *	The Regents of the University of California.  All rights reserved.
435528Sbostic  *
542712Sbostic  * %sccs.include.redist.c%
621179Sdist  */
721179Sdist 
812682Ssam #ifndef lint
961547Sbostic static char copyright[] =
1061547Sbostic "@(#) Copyright (c) 1988, 1990, 1993\n\
1161547Sbostic 	The Regents of the University of California.  All rights reserved.\n";
1235528Sbostic #endif /* not lint */
132373Swnj 
1421179Sdist #ifndef lint
15*69011Sbostic static char sccsid[] = "@(#)shutdown.c	8.4 (Berkeley) 04/28/95";
1635528Sbostic #endif /* not lint */
1721179Sdist 
1835528Sbostic #include <sys/param.h>
1935528Sbostic #include <sys/time.h>
2035528Sbostic #include <sys/resource.h>
2135528Sbostic #include <sys/syslog.h>
2266102Sbostic 
2366102Sbostic #include <ctype.h>
2466102Sbostic #include <fcntl.h>
2566102Sbostic #include <pwd.h>
2615920Skarels #include <setjmp.h>
2766102Sbostic #include <signal.h>
2866102Sbostic #include <stdio.h>
2966102Sbostic #include <stdlib.h>
3066102Sbostic #include <string.h>
3135528Sbostic #include <tzfile.h>
3266102Sbostic #include <unistd.h>
3366102Sbostic 
3437300Sbostic #include "pathnames.h"
3528024Seric 
3635528Sbostic #ifdef DEBUG
3737300Sbostic #undef _PATH_NOLOGIN
3837300Sbostic #define	_PATH_NOLOGIN	"./nologin"
3937300Sbostic #undef _PATH_FASTBOOT
4037300Sbostic #define	_PATH_FASTBOOT	"./fastboot"
412373Swnj #endif
428841Smckusick 
4335528Sbostic #define	H		*60*60
4435528Sbostic #define	M		*60
4535528Sbostic #define	S		*1
4635528Sbostic #define	NOLOG_TIME	5*60
472373Swnj struct interval {
4835528Sbostic 	int timeleft, timetowait;
4935528Sbostic } tlist[] = {
5035528Sbostic 	10 H,  5 H,	 5 H,  3 H,	 2 H,  1 H,	1 H, 30 M,
5135528Sbostic 	30 M, 10 M,	20 M, 10 M,	10 M,  5 M,	5 M,  3 M,
5235528Sbostic 	 2 M,  1 M,	 1 M, 30 S,	30 S, 30 S,
5335528Sbostic 	 0, 0,
5466102Sbostic };
5535528Sbostic #undef H
5635528Sbostic #undef M
5735528Sbostic #undef S
588841Smckusick 
5935528Sbostic static time_t offset, shuttime;
6035528Sbostic static int dofast, dohalt, doreboot, killflg, mbuflen;
6135528Sbostic static char *nosync, *whom, mbuf[BUFSIZ];
628841Smckusick 
6366102Sbostic void badtime __P((void));
6466102Sbostic void die_you_gravy_sucking_pig_dog __P((void));
6566102Sbostic void doitfast __P((void));
6666102Sbostic void finish __P((int));
6766102Sbostic void getoffset __P((char *));
6866102Sbostic void loop __P((void));
6966102Sbostic void nolog __P((void));
7066102Sbostic void timeout __P((int));
7166102Sbostic void timewarn __P((int));
7266102Sbostic void usage __P((void));
7366102Sbostic 
7466102Sbostic int
main(argc,argv)7535528Sbostic main(argc, argv)
762373Swnj 	int argc;
7766102Sbostic 	char *argv[];
782373Swnj {
7935528Sbostic 	extern int optind;
8036433Sbostic 	register char *p, *endp;
8166102Sbostic 	struct passwd *pw;
8236433Sbostic 	int arglen, ch, len, readstdin;
832373Swnj 
8435528Sbostic #ifndef DEBUG
8535528Sbostic 	if (geteuid()) {
8643465Sbostic 		(void)fprintf(stderr, "shutdown: NOT super-user\n");
8735528Sbostic 		exit(1);
8835528Sbostic 	}
8935528Sbostic #endif
9036433Sbostic 	nosync = NULL;
9136433Sbostic 	readstdin = 0;
9236433Sbostic 	while ((ch = getopt(argc, argv, "-fhknr")) != EOF)
9340224Skarels 		switch (ch) {
9436433Sbostic 		case '-':
9536433Sbostic 			readstdin = 1;
9636433Sbostic 			break;
9735528Sbostic 		case 'f':
9835528Sbostic 			dofast = 1;
9935528Sbostic 			break;
10035528Sbostic 		case 'h':
10135528Sbostic 			dohalt = 1;
10235528Sbostic 			break;
1032373Swnj 		case 'k':
10435528Sbostic 			killflg = 1;
10535528Sbostic 			break;
10625366Sbloom 		case 'n':
10735528Sbostic 			nosync = "-n";
10835528Sbostic 			break;
1092373Swnj 		case 'r':
11025408Sbloom 			doreboot = 1;
11135528Sbostic 			break;
11235528Sbostic 		case '?':
1132373Swnj 		default:
11435528Sbostic 			usage();
1152373Swnj 		}
11635528Sbostic 	argc -= optind;
11735528Sbostic 	argv += optind;
11835528Sbostic 
11935528Sbostic 	if (argc < 1)
12035528Sbostic 		usage();
12135528Sbostic 
12236433Sbostic 	if (dofast && nosync) {
12343465Sbostic 		(void)fprintf(stderr,
12435528Sbostic 		    "shutdown: incompatible switches -f and -n.\n");
12535528Sbostic 		usage();
1262373Swnj 	}
12735528Sbostic 	if (doreboot && dohalt) {
12843465Sbostic 		(void)fprintf(stderr,
12935528Sbostic 		    "shutdown: incompatible switches -h and -r.\n");
13035528Sbostic 		usage();
1312373Swnj 	}
13235528Sbostic 	getoffset(*argv++);
13336433Sbostic 
13435528Sbostic 	if (*argv) {
13536433Sbostic 		for (p = mbuf, len = sizeof(mbuf); *argv; ++argv) {
13636433Sbostic 			arglen = strlen(*argv);
13736433Sbostic 			if ((len -= arglen) <= 2)
13836433Sbostic 				break;
13936433Sbostic 			if (p != mbuf)
14036433Sbostic 				*p++ = ' ';
141*69011Sbostic 			memmove(p, *argv, arglen);
14236433Sbostic 			p += arglen;
14336433Sbostic 		}
14436433Sbostic 		*p = '\n';
14536433Sbostic 		*++p = '\0';
14625366Sbloom 	}
14736433Sbostic 
14836433Sbostic 	if (readstdin) {
14936433Sbostic 		p = mbuf;
15036433Sbostic 		endp = mbuf + sizeof(mbuf) - 2;
15136433Sbostic 		for (;;) {
15236433Sbostic 			if (!fgets(p, endp - p + 1, stdin))
15336433Sbostic 				break;
15436433Sbostic 			for (; *p &&  p < endp; ++p);
15536433Sbostic 			if (p == endp) {
15636433Sbostic 				*p = '\n';
15736433Sbostic 				*++p = '\0';
15836433Sbostic 				break;
15936433Sbostic 			}
16036433Sbostic 		}
1613648Stoy 	}
16235528Sbostic 	mbuflen = strlen(mbuf);
16335528Sbostic 
16435528Sbostic 	if (offset)
16543465Sbostic 		(void)printf("Shutdown at %.24s.\n", ctime(&shuttime));
16635528Sbostic 	else
16743465Sbostic 		(void)printf("Shutdown NOW!\n");
16835528Sbostic 
16935528Sbostic 	if (!(whom = getlogin()))
17035528Sbostic 		whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???";
17135528Sbostic 
17235528Sbostic #ifdef DEBUG
17335528Sbostic 	(void)putc('\n', stdout);
17435528Sbostic #else
17535528Sbostic 	(void)setpriority(PRIO_PROCESS, 0, PRIO_MIN);
17635528Sbostic 	{
17735528Sbostic 		int forkpid;
17835528Sbostic 
17935528Sbostic 		forkpid = fork();
18035528Sbostic 		if (forkpid == -1) {
18135528Sbostic 			perror("shutdown: fork");
18235528Sbostic 			exit(1);
18335528Sbostic 		}
18435528Sbostic 		if (forkpid) {
18543465Sbostic 			(void)printf("shutdown: [pid %d]\n", forkpid);
18635528Sbostic 			exit(0);
18735528Sbostic 		}
18828024Seric 	}
1892373Swnj #endif
19035528Sbostic 	openlog("shutdown", LOG_CONS, LOG_AUTH);
19135528Sbostic 	loop();
19266102Sbostic 	/* NOTREACHED */
19335528Sbostic }
19435528Sbostic 
19566102Sbostic void
loop()19635528Sbostic loop()
19735528Sbostic {
19866102Sbostic 	struct interval *tp;
19935528Sbostic 	u_int sltime;
20035528Sbostic 	int logged;
20135528Sbostic 
20235528Sbostic 	if (offset <= NOLOG_TIME) {
20335528Sbostic 		logged = 1;
20435528Sbostic 		nolog();
2052373Swnj 	}
20635528Sbostic 	else
20735528Sbostic 		logged = 0;
20835528Sbostic 	tp = tlist;
20935528Sbostic 	if (tp->timeleft < offset)
21035528Sbostic 		(void)sleep((u_int)(offset - tp->timeleft));
21135528Sbostic 	else {
21235528Sbostic 		while (offset < tp->timeleft)
21335528Sbostic 			++tp;
21435528Sbostic 		/*
21566102Sbostic 		 * Warn now, if going to sleep more than a fifth of
21635528Sbostic 		 * the next wait time.
21735528Sbostic 		 */
21835528Sbostic 		if (sltime = offset - tp->timeleft) {
21935528Sbostic 			if (sltime > tp->timetowait / 5)
22066102Sbostic 				timewarn(offset);
22135528Sbostic 			(void)sleep(sltime);
22235528Sbostic 		}
2238841Smckusick 	}
22435528Sbostic 	for (;; ++tp) {
22566102Sbostic 		timewarn(tp->timeleft);
22635528Sbostic 		if (!logged && tp->timeleft <= NOLOG_TIME) {
22735528Sbostic 			logged = 1;
22835528Sbostic 			nolog();
2292373Swnj 		}
23035528Sbostic 		(void)sleep((u_int)tp->timetowait);
23135528Sbostic 		if (!tp->timeleft)
23235528Sbostic 			break;
23335528Sbostic 	}
23435528Sbostic 	die_you_gravy_sucking_pig_dog();
23535528Sbostic }
23625366Sbloom 
23735528Sbostic static jmp_buf alarmbuf;
23835528Sbostic 
23966102Sbostic void
timewarn(timeleft)24066102Sbostic timewarn(timeleft)
24166102Sbostic 	int timeleft;
24235528Sbostic {
24335528Sbostic 	static int first;
24440224Skarels 	static char hostname[MAXHOSTNAMELEN + 1];
24566102Sbostic 	FILE *pf;
24640224Skarels 	char wcmd[MAXPATHLEN + 4];
24735528Sbostic 
24839421Sbostic 	if (!first++)
24940224Skarels 		(void)gethostname(hostname, sizeof(hostname));
25035528Sbostic 
25140224Skarels 	/* undoc -n option to wall suppresses normal wall banner */
25266102Sbostic 	(void)snprintf(wcmd, sizeof(wcmd), "%s -n", _PATH_WALL);
25340224Skarels 	if (!(pf = popen(wcmd, "w"))) {
25437300Sbostic 		syslog(LOG_ERR, "shutdown: can't find %s: %m", _PATH_WALL);
25535528Sbostic 		return;
25635528Sbostic 	}
25735528Sbostic 
25843465Sbostic 	(void)fprintf(pf,
25943465Sbostic 	    "\007*** %sSystem shutdown message from %s@%s ***\007\n",
26066102Sbostic 	    timeleft ? "": "FINAL ", whom, hostname);
26135528Sbostic 
26266102Sbostic 	if (timeleft > 10*60)
26343465Sbostic 		(void)fprintf(pf, "System going down at %5.5s\n\n",
26435528Sbostic 		    ctime(&shuttime) + 11);
26566102Sbostic 	else if (timeleft > 59)
26643465Sbostic 		(void)fprintf(pf, "System going down in %d minute%s\n\n",
26766102Sbostic 		    timeleft / 60, (timeleft > 60) ? "s" : "");
26866102Sbostic 	else if (timeleft)
26943465Sbostic 		(void)fprintf(pf, "System going down in 30 seconds\n\n");
27035528Sbostic 	else
27143465Sbostic 		(void)fprintf(pf, "System going down IMMEDIATELY\n\n");
27235528Sbostic 
27336433Sbostic 	if (mbuflen)
27436433Sbostic 		(void)fwrite(mbuf, sizeof(*mbuf), mbuflen, pf);
27535528Sbostic 
27635528Sbostic 	/*
27735528Sbostic 	 * play some games, just in case wall doesn't come back
27835528Sbostic 	 * probably unecessary, given that wall is careful.
27935528Sbostic 	 */
28035528Sbostic 	if (!setjmp(alarmbuf)) {
28140224Skarels 		(void)signal(SIGALRM, timeout);
28235528Sbostic 		(void)alarm((u_int)30);
28335528Sbostic 		(void)pclose(pf);
28435528Sbostic 		(void)alarm((u_int)0);
28540224Skarels 		(void)signal(SIGALRM, SIG_DFL);
28635528Sbostic 	}
2872373Swnj }
2882373Swnj 
28943465Sbostic void
timeout(signo)29066102Sbostic timeout(signo)
29166102Sbostic 	int signo;
2922373Swnj {
29335528Sbostic 	longjmp(alarmbuf, 1);
29435528Sbostic }
2952373Swnj 
29666102Sbostic void
die_you_gravy_sucking_pig_dog()29735528Sbostic die_you_gravy_sucking_pig_dog()
29835528Sbostic {
29943465Sbostic 
30035528Sbostic 	syslog(LOG_NOTICE, "%s by %s: %s",
30135528Sbostic 	    doreboot ? "reboot" : dohalt ? "halt" : "shutdown", whom, mbuf);
30235528Sbostic 	(void)sleep(2);
30335528Sbostic 
30443465Sbostic 	(void)printf("\r\nSystem shutdown time has arrived\007\007\r\n");
30535528Sbostic 	if (killflg) {
30643465Sbostic 		(void)printf("\rbut you'll have to do it yourself\r\n");
30766102Sbostic 		finish(0);
3082373Swnj 	}
30935528Sbostic 	if (dofast)
31035528Sbostic 		doitfast();
31135528Sbostic #ifdef DEBUG
31235528Sbostic 	if (doreboot)
31343465Sbostic 		(void)printf("reboot");
31436433Sbostic 	else if (dohalt)
31543465Sbostic 		(void)printf("halt");
31636433Sbostic 	if (nosync)
31743465Sbostic 		(void)printf(" no sync");
31835528Sbostic 	if (dofast)
31943465Sbostic 		(void)printf(" no fsck");
32043465Sbostic 	(void)printf("\nkill -HUP 1\n");
32135528Sbostic #else
32235528Sbostic 	if (doreboot) {
32337300Sbostic 		execle(_PATH_REBOOT, "reboot", "-l", nosync, 0);
32437300Sbostic 		syslog(LOG_ERR, "shutdown: can't exec %s: %m.", _PATH_REBOOT);
32535528Sbostic 		perror("shutdown");
32635528Sbostic 	}
32735528Sbostic 	else if (dohalt) {
32837300Sbostic 		execle(_PATH_HALT, "halt", "-l", nosync, 0);
32937300Sbostic 		syslog(LOG_ERR, "shutdown: can't exec %s: %m.", _PATH_HALT);
33035528Sbostic 		perror("shutdown");
33135528Sbostic 	}
33235528Sbostic 	(void)kill(1, SIGTERM);		/* to single user */
33335528Sbostic #endif
33466102Sbostic 	finish(0);
3352373Swnj }
3362373Swnj 
33735528Sbostic #define	ATOI2(p)	(p[0] - '0') * 10 + (p[1] - '0'); p += 2;
33835528Sbostic 
33966102Sbostic void
getoffset(timearg)34035528Sbostic getoffset(timearg)
34135528Sbostic 	register char *timearg;
3422373Swnj {
34335528Sbostic 	register struct tm *lt;
34435528Sbostic 	register char *p;
34566102Sbostic 	time_t now;
3462373Swnj 
34735528Sbostic 	if (!strcasecmp(timearg, "now")) {		/* now */
34835528Sbostic 		offset = 0;
34935528Sbostic 		return;
35035528Sbostic 	}
3513880Swnj 
35235528Sbostic 	(void)time(&now);
35335528Sbostic 	if (*timearg == '+') {				/* +minutes */
35435528Sbostic 		if (!isdigit(*++timearg))
35543465Sbostic 			badtime();
35643465Sbostic 		offset = atoi(timearg) * 60;
35735528Sbostic 		shuttime = now + offset;
35835528Sbostic 		return;
35935528Sbostic 	}
3608841Smckusick 
36135528Sbostic 	/* handle hh:mm by getting rid of the colon */
36235528Sbostic 	for (p = timearg; *p; ++p)
36335528Sbostic 		if (!isascii(*p) || !isdigit(*p))
36435528Sbostic 			if (*p == ':' && strlen(p) == 3) {
36535528Sbostic 				p[0] = p[1];
36635528Sbostic 				p[1] = p[2];
36735528Sbostic 				p[2] = '\0';
36835528Sbostic 			}
36935528Sbostic 			else
37043465Sbostic 				badtime();
37135528Sbostic 
37235528Sbostic 	unsetenv("TZ");					/* OUR timezone */
37343465Sbostic 	lt = localtime(&now);				/* current time val */
37435528Sbostic 
37535528Sbostic 	switch(strlen(timearg)) {
37635528Sbostic 	case 10:
37743465Sbostic 		lt->tm_year = ATOI2(timearg);
37835528Sbostic 		/* FALLTHROUGH */
37935528Sbostic 	case 8:
38043465Sbostic 		lt->tm_mon = ATOI2(timearg);
38143465Sbostic 		if (--lt->tm_mon < 0 || lt->tm_mon > 11)
38243465Sbostic 			badtime();
38335528Sbostic 		/* FALLTHROUGH */
38435528Sbostic 	case 6:
38543465Sbostic 		lt->tm_mday = ATOI2(timearg);
38643465Sbostic 		if (lt->tm_mday < 1 || lt->tm_mday > 31)
38743465Sbostic 			badtime();
38835528Sbostic 		/* FALLTHROUGH */
38935528Sbostic 	case 4:
39043465Sbostic 		lt->tm_hour = ATOI2(timearg);
39143465Sbostic 		if (lt->tm_hour < 0 || lt->tm_hour > 23)
39243465Sbostic 			badtime();
39343465Sbostic 		lt->tm_min = ATOI2(timearg);
39443465Sbostic 		if (lt->tm_min < 0 || lt->tm_min > 59)
39543465Sbostic 			badtime();
39643465Sbostic 		lt->tm_sec = 0;
39743465Sbostic 		if ((shuttime = mktime(lt)) == -1)
39843465Sbostic 			badtime();
39943465Sbostic 		if ((offset = shuttime - now) < 0) {
40043465Sbostic 			(void)fprintf(stderr,
40143465Sbostic 			    "shutdown: that time is already past.\n");
40243465Sbostic 			exit(1);
40343465Sbostic 		}
40443465Sbostic 		break;
40535528Sbostic 	default:
40643465Sbostic 		badtime();
40735528Sbostic 	}
4082373Swnj }
4092373Swnj 
41035528Sbostic #define	FSMSG	"fastboot file for fsck\n"
41166102Sbostic void
doitfast()41225366Sbloom doitfast()
41325366Sbloom {
41435528Sbostic 	int fastfd;
41525366Sbloom 
41637300Sbostic 	if ((fastfd = open(_PATH_FASTBOOT, O_WRONLY|O_CREAT|O_TRUNC,
41737300Sbostic 	    0664)) >= 0) {
41835528Sbostic 		(void)write(fastfd, FSMSG, sizeof(FSMSG) - 1);
41935528Sbostic 		(void)close(fastfd);
42025366Sbloom 	}
42125366Sbloom }
42225366Sbloom 
42335528Sbostic #define	NOMSG	"\n\nNO LOGINS: System going down at "
42466102Sbostic void
nolog()42535528Sbostic nolog()
4262373Swnj {
42743465Sbostic 	int logfd;
42866102Sbostic 	char *ct;
4292373Swnj 
43037300Sbostic 	(void)unlink(_PATH_NOLOGIN);	/* in case linked to another file */
43135528Sbostic 	(void)signal(SIGINT, finish);
43235528Sbostic 	(void)signal(SIGHUP, finish);
43335528Sbostic 	(void)signal(SIGQUIT, finish);
43435528Sbostic 	(void)signal(SIGTERM, finish);
43537300Sbostic 	if ((logfd = open(_PATH_NOLOGIN, O_WRONLY|O_CREAT|O_TRUNC,
43637300Sbostic 	    0664)) >= 0) {
43735528Sbostic 		(void)write(logfd, NOMSG, sizeof(NOMSG) - 1);
43835528Sbostic 		ct = ctime(&shuttime);
43935528Sbostic 		(void)write(logfd, ct + 11, 5);
44035528Sbostic 		(void)write(logfd, "\n\n", 2);
44135528Sbostic 		(void)write(logfd, mbuf, strlen(mbuf));
44235528Sbostic 		(void)close(logfd);
4432373Swnj 	}
4442373Swnj }
4452373Swnj 
44643465Sbostic void
finish(signo)44766102Sbostic finish(signo)
44866102Sbostic 	int signo;
4492373Swnj {
45068956Sbostic 	if (!killflg)
45168956Sbostic 		(void)unlink(_PATH_NOLOGIN);
4522373Swnj 	exit(0);
4532373Swnj }
4542373Swnj 
45566102Sbostic void
badtime()45643465Sbostic badtime()
45743465Sbostic {
45843465Sbostic 	(void)fprintf(stderr, "shutdown: bad time format.\n");
45943465Sbostic 	exit(1);
46043465Sbostic }
46143465Sbostic 
46266102Sbostic void
usage()46335528Sbostic usage()
4642373Swnj {
46535528Sbostic 	fprintf(stderr, "usage: shutdown [-fhknr] shutdowntime [ message ]\n");
46635528Sbostic 	exit(1);
4672373Swnj }
468