xref: /freebsd/sbin/shutdown/shutdown.c (revision d349bd35330d3ec7ce1d3e7d6c2d6fc1f6a95704)
18a16b7a1SPedro F. Giffuni /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
48fae3551SRodney W. Grimes  * Copyright (c) 1988, 1990, 1993
58fae3551SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
68fae3551SRodney W. Grimes  *
78fae3551SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
88fae3551SRodney W. Grimes  * modification, are permitted provided that the following conditions
98fae3551SRodney W. Grimes  * are met:
108fae3551SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
118fae3551SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
128fae3551SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
138fae3551SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
148fae3551SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
168fae3551SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
178fae3551SRodney W. Grimes  *    without specific prior written permission.
188fae3551SRodney W. Grimes  *
198fae3551SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
208fae3551SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
218fae3551SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
228fae3551SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
238fae3551SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
248fae3551SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
258fae3551SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
268fae3551SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
278fae3551SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
288fae3551SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
298fae3551SRodney W. Grimes  * SUCH DAMAGE.
308fae3551SRodney W. Grimes  */
318fae3551SRodney W. Grimes 
328fae3551SRodney W. Grimes #include <sys/param.h>
337b0a665dSMitchell Horne #include <sys/boottrace.h>
348fae3551SRodney W. Grimes #include <sys/resource.h>
358fae3551SRodney W. Grimes #include <sys/syslog.h>
367b0a665dSMitchell Horne #include <sys/time.h>
378fae3551SRodney W. Grimes 
388fae3551SRodney W. Grimes #include <ctype.h>
39d3714863SPhilippe Charnier #include <err.h>
40c7d73a4dSGleb Kurtsou #include <errno.h>
418fae3551SRodney W. Grimes #include <fcntl.h>
428d5c19ffSDavid E. O'Brien #include <paths.h>
438fae3551SRodney W. Grimes #include <pwd.h>
448fae3551SRodney W. Grimes #include <setjmp.h>
458fae3551SRodney W. Grimes #include <signal.h>
466c7ec630SKyle Evans #include <stdbool.h>
478fae3551SRodney W. Grimes #include <stdio.h>
488fae3551SRodney W. Grimes #include <stdlib.h>
498fae3551SRodney W. Grimes #include <string.h>
508fae3551SRodney W. Grimes #include <unistd.h>
518fae3551SRodney W. Grimes 
528fae3551SRodney W. Grimes #ifdef DEBUG
538fae3551SRodney W. Grimes #undef _PATH_NOLOGIN
548fae3551SRodney W. Grimes #define	_PATH_NOLOGIN	"./nologin"
558fae3551SRodney W. Grimes #endif
568fae3551SRodney W. Grimes 
578fae3551SRodney W. Grimes #define	H		*60*60
588fae3551SRodney W. Grimes #define	M		*60
598fae3551SRodney W. Grimes #define	S		*1
608fae3551SRodney W. Grimes #define	NOLOG_TIME	5*60
611efe3c6bSEd Schouten static struct interval {
628fae3551SRodney W. Grimes 	int timeleft, timetowait;
638fae3551SRodney W. Grimes } tlist[] = {
64f6faa785SAlexander Langer 	{ 10 H,  5 H },
65f6faa785SAlexander Langer 	{  5 H,  3 H },
66f6faa785SAlexander Langer 	{  2 H,  1 H },
67f6faa785SAlexander Langer 	{  1 H, 30 M },
68f6faa785SAlexander Langer 	{ 30 M, 10 M },
69f6faa785SAlexander Langer 	{ 20 M, 10 M },
70f6faa785SAlexander Langer 	{ 10 M,  5 M },
71f6faa785SAlexander Langer 	{  5 M,  3 M },
72f6faa785SAlexander Langer 	{  2 M,  1 M },
73f6faa785SAlexander Langer 	{  1 M, 30 S },
74f6faa785SAlexander Langer 	{ 30 S, 30 S },
75f6faa785SAlexander Langer 	{  0  ,  0   }
768fae3551SRodney W. Grimes };
778fae3551SRodney W. Grimes #undef H
788fae3551SRodney W. Grimes #undef M
798fae3551SRodney W. Grimes #undef S
808fae3551SRodney W. Grimes 
818fae3551SRodney W. Grimes static time_t offset, shuttime;
82e60baa72SWarner Losh static int docycle, dohalt, dopower, doreboot, killflg, mbuflen, oflag;
8304983c4fSDima Dorfman static char mbuf[BUFSIZ];
8404983c4fSDima Dorfman static const char *nosync, *whom;
858fae3551SRodney W. Grimes 
869534110fSXin LI static void badtime(void);
8739faed57SDag-Erling Smørgrav static void die_you_gravy_sucking_pig_dog(void);
889534110fSXin LI static void finish(int);
899534110fSXin LI static void getoffset(char *);
906c7ec630SKyle Evans static void loop(bool);
919534110fSXin LI static void nolog(void);
929534110fSXin LI static void timeout(int);
939534110fSXin LI static void timewarn(int);
949534110fSXin LI static void usage(const char *);
958fae3551SRodney W. Grimes 
96f906f843SXin LI extern const char **environ;
97f906f843SXin LI 
988fae3551SRodney W. Grimes int
main(int argc,char ** argv)99f906f843SXin LI main(int argc, char **argv)
1008fae3551SRodney W. Grimes {
10104983c4fSDima Dorfman 	char *p, *endp;
1028fae3551SRodney W. Grimes 	struct passwd *pw;
1038fae3551SRodney W. Grimes 	int arglen, ch, len, readstdin;
1046c7ec630SKyle Evans 	bool dowarn;
1058fae3551SRodney W. Grimes 
1068fae3551SRodney W. Grimes #ifndef DEBUG
107a2bfcdfdSPhilippe Charnier 	if (geteuid())
108a2bfcdfdSPhilippe Charnier 		errx(1, "NOT super-user");
1098fae3551SRodney W. Grimes #endif
1106868734cSPawel Jakub Dawidek 
1116c7ec630SKyle Evans 	dowarn = true;
1128fae3551SRodney W. Grimes 	nosync = NULL;
1138fae3551SRodney W. Grimes 	readstdin = 0;
1146868734cSPawel Jakub Dawidek 
1156868734cSPawel Jakub Dawidek 	/*
1166868734cSPawel Jakub Dawidek 	 * Test for the special case where the utility is called as
1176868734cSPawel Jakub Dawidek 	 * "poweroff", for which it runs 'shutdown -p now'.
1186868734cSPawel Jakub Dawidek 	 */
119b3608ae1SEd Schouten 	if ((p = strrchr(argv[0], '/')) == NULL)
1206868734cSPawel Jakub Dawidek 		p = argv[0];
1216868734cSPawel Jakub Dawidek 	else
1226868734cSPawel Jakub Dawidek 		++p;
1236868734cSPawel Jakub Dawidek 	if (strcmp(p, "poweroff") == 0) {
1246868734cSPawel Jakub Dawidek 		if (getopt(argc, argv, "") != -1)
1256868734cSPawel Jakub Dawidek 			usage((char *)NULL);
1266868734cSPawel Jakub Dawidek 		argc -= optind;
1276868734cSPawel Jakub Dawidek 		argv += optind;
1286868734cSPawel Jakub Dawidek 		if (argc != 0)
1296868734cSPawel Jakub Dawidek 			usage((char *)NULL);
1306868734cSPawel Jakub Dawidek 		dopower = 1;
1316868734cSPawel Jakub Dawidek 		offset = 0;
1326868734cSPawel Jakub Dawidek 		(void)time(&shuttime);
1336868734cSPawel Jakub Dawidek 		goto poweroff;
1346868734cSPawel Jakub Dawidek 	}
1356868734cSPawel Jakub Dawidek 
136*d349bd35SKyle Evans 	while ((ch = getopt(argc, argv, "-chknopqr")) != -1)
1378fae3551SRodney W. Grimes 		switch (ch) {
1388fae3551SRodney W. Grimes 		case '-':
1398fae3551SRodney W. Grimes 			readstdin = 1;
1408fae3551SRodney W. Grimes 			break;
141e60baa72SWarner Losh 		case 'c':
142e60baa72SWarner Losh 			docycle = 1;
143e60baa72SWarner Losh 			break;
1448fae3551SRodney W. Grimes 		case 'h':
1458fae3551SRodney W. Grimes 			dohalt = 1;
1468fae3551SRodney W. Grimes 			break;
1478fae3551SRodney W. Grimes 		case 'k':
1488fae3551SRodney W. Grimes 			killflg = 1;
1498fae3551SRodney W. Grimes 			break;
1508fae3551SRodney W. Grimes 		case 'n':
1518fae3551SRodney W. Grimes 			nosync = "-n";
1528fae3551SRodney W. Grimes 			break;
15335cd460fSRuslan Ermilov 		case 'o':
15435cd460fSRuslan Ermilov 			oflag = 1;
15535cd460fSRuslan Ermilov 			break;
1567e2d04d7SMike Smith 		case 'p':
1577e2d04d7SMike Smith 			dopower = 1;
1587e2d04d7SMike Smith 			break;
1596c7ec630SKyle Evans 		case 'q':
1606c7ec630SKyle Evans 			dowarn = false;
1616c7ec630SKyle Evans 			break;
162*d349bd35SKyle Evans 		case 'r':
163*d349bd35SKyle Evans 			doreboot = 1;
164*d349bd35SKyle Evans 			break;
1658fae3551SRodney W. Grimes 		case '?':
1668fae3551SRodney W. Grimes 		default:
16735cd460fSRuslan Ermilov 			usage((char *)NULL);
1688fae3551SRodney W. Grimes 		}
1698fae3551SRodney W. Grimes 	argc -= optind;
1708fae3551SRodney W. Grimes 	argv += optind;
1718fae3551SRodney W. Grimes 
1728fae3551SRodney W. Grimes 	if (argc < 1)
17335cd460fSRuslan Ermilov 		usage((char *)NULL);
1748fae3551SRodney W. Grimes 
175e60baa72SWarner Losh 	if (killflg + doreboot + dohalt + dopower + docycle > 1)
176e60baa72SWarner Losh 		usage("incompatible switches -c, -h, -k, -p and -r");
1771d4f4c0dSJoseph Koshy 
178e60baa72SWarner Losh 	if (oflag && !(dohalt || dopower || doreboot || docycle))
179e60baa72SWarner Losh 		usage("-o requires -c, -h, -p or -r");
18035cd460fSRuslan Ermilov 
18135cd460fSRuslan Ermilov 	if (nosync != NULL && !oflag)
18235cd460fSRuslan Ermilov 		usage("-n requires -o");
1831d4f4c0dSJoseph Koshy 
1848fae3551SRodney W. Grimes 	getoffset(*argv++);
1858fae3551SRodney W. Grimes 
1866868734cSPawel Jakub Dawidek poweroff:
1876c7ec630SKyle Evans 	if (!dowarn && *argv != NULL)
1886c7ec630SKyle Evans 		usage("warning-message supplied but suppressed with -q");
1898fae3551SRodney W. Grimes 	if (*argv) {
1908fae3551SRodney W. Grimes 		for (p = mbuf, len = sizeof(mbuf); *argv; ++argv) {
1918fae3551SRodney W. Grimes 			arglen = strlen(*argv);
1928fae3551SRodney W. Grimes 			if ((len -= arglen) <= 2)
1938fae3551SRodney W. Grimes 				break;
1948fae3551SRodney W. Grimes 			if (p != mbuf)
1958fae3551SRodney W. Grimes 				*p++ = ' ';
19639b831afSBruce Evans 			memmove(p, *argv, arglen);
1978fae3551SRodney W. Grimes 			p += arglen;
1988fae3551SRodney W. Grimes 		}
1998fae3551SRodney W. Grimes 		*p = '\n';
2008fae3551SRodney W. Grimes 		*++p = '\0';
2018fae3551SRodney W. Grimes 	}
2028fae3551SRodney W. Grimes 
2038fae3551SRodney W. Grimes 	if (readstdin) {
2048fae3551SRodney W. Grimes 		p = mbuf;
2058fae3551SRodney W. Grimes 		endp = mbuf + sizeof(mbuf) - 2;
2068fae3551SRodney W. Grimes 		for (;;) {
2078fae3551SRodney W. Grimes 			if (!fgets(p, endp - p + 1, stdin))
2088fae3551SRodney W. Grimes 				break;
2098fae3551SRodney W. Grimes 			for (; *p &&  p < endp; ++p);
2108fae3551SRodney W. Grimes 			if (p == endp) {
2118fae3551SRodney W. Grimes 				*p = '\n';
2128fae3551SRodney W. Grimes 				*++p = '\0';
2138fae3551SRodney W. Grimes 				break;
2148fae3551SRodney W. Grimes 			}
2158fae3551SRodney W. Grimes 		}
2168fae3551SRodney W. Grimes 	}
2178fae3551SRodney W. Grimes 	mbuflen = strlen(mbuf);
2188fae3551SRodney W. Grimes 
2197b0a665dSMitchell Horne 	if (offset) {
2207b0a665dSMitchell Horne 		BOOTTRACE("Shutdown at %s", ctime(&shuttime));
2218fae3551SRodney W. Grimes 		(void)printf("Shutdown at %.24s.\n", ctime(&shuttime));
2227b0a665dSMitchell Horne 	} else {
2237b0a665dSMitchell Horne 		BOOTTRACE("Shutdown NOW!");
2248fae3551SRodney W. Grimes 		(void)printf("Shutdown NOW!\n");
2257b0a665dSMitchell Horne 	}
2268fae3551SRodney W. Grimes 
2278fae3551SRodney W. Grimes 	if (!(whom = getlogin()))
2288fae3551SRodney W. Grimes 		whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???";
2298fae3551SRodney W. Grimes 
2308fae3551SRodney W. Grimes #ifdef DEBUG
2318fae3551SRodney W. Grimes 	(void)putc('\n', stdout);
2328fae3551SRodney W. Grimes #else
2338fae3551SRodney W. Grimes 	(void)setpriority(PRIO_PROCESS, 0, PRIO_MIN);
2348fae3551SRodney W. Grimes 	{
2358fae3551SRodney W. Grimes 		int forkpid;
2368fae3551SRodney W. Grimes 
2378fae3551SRodney W. Grimes 		forkpid = fork();
238a2bfcdfdSPhilippe Charnier 		if (forkpid == -1)
239a2bfcdfdSPhilippe Charnier 			err(1, "fork");
240a2bfcdfdSPhilippe Charnier 		if (forkpid)
241a2bfcdfdSPhilippe Charnier 			errx(0, "[pid %d]", forkpid);
2428fae3551SRodney W. Grimes 	}
24332c9dffbSJoerg Wunsch 	setsid();
2448fae3551SRodney W. Grimes #endif
2458fae3551SRodney W. Grimes 	openlog("shutdown", LOG_CONS, LOG_AUTH);
2466c7ec630SKyle Evans 	loop(dowarn);
247d3714863SPhilippe Charnier 	return(0);
2488fae3551SRodney W. Grimes }
2498fae3551SRodney W. Grimes 
2509534110fSXin LI static void
loop(bool dowarn)2516c7ec630SKyle Evans loop(bool dowarn)
2528fae3551SRodney W. Grimes {
2538fae3551SRodney W. Grimes 	struct interval *tp;
2548fae3551SRodney W. Grimes 	u_int sltime;
2558fae3551SRodney W. Grimes 	int logged;
2568fae3551SRodney W. Grimes 
2578fae3551SRodney W. Grimes 	if (offset <= NOLOG_TIME) {
2588fae3551SRodney W. Grimes 		logged = 1;
2598fae3551SRodney W. Grimes 		nolog();
2608fae3551SRodney W. Grimes 	}
2618fae3551SRodney W. Grimes 	else
2628fae3551SRodney W. Grimes 		logged = 0;
2638fae3551SRodney W. Grimes 	tp = tlist;
2648fae3551SRodney W. Grimes 	if (tp->timeleft < offset)
2658fae3551SRodney W. Grimes 		(void)sleep((u_int)(offset - tp->timeleft));
2668fae3551SRodney W. Grimes 	else {
267966ea00aSRuslan Ermilov 		while (tp->timeleft && offset < tp->timeleft)
2688fae3551SRodney W. Grimes 			++tp;
2698fae3551SRodney W. Grimes 		/*
2708fae3551SRodney W. Grimes 		 * Warn now, if going to sleep more than a fifth of
2718fae3551SRodney W. Grimes 		 * the next wait time.
2728fae3551SRodney W. Grimes 		 */
273f6faa785SAlexander Langer 		if ((sltime = offset - tp->timeleft)) {
2746c7ec630SKyle Evans 			if (dowarn && sltime > (u_int)(tp->timetowait / 5))
2758fae3551SRodney W. Grimes 				timewarn(offset);
2768fae3551SRodney W. Grimes 			(void)sleep(sltime);
2778fae3551SRodney W. Grimes 		}
2788fae3551SRodney W. Grimes 	}
2798fae3551SRodney W. Grimes 	for (;; ++tp) {
2806c7ec630SKyle Evans 		if (dowarn)
2818fae3551SRodney W. Grimes 			timewarn(tp->timeleft);
2828fae3551SRodney W. Grimes 		if (!logged && tp->timeleft <= NOLOG_TIME) {
2838fae3551SRodney W. Grimes 			logged = 1;
2848fae3551SRodney W. Grimes 			nolog();
2858fae3551SRodney W. Grimes 		}
2868fae3551SRodney W. Grimes 		(void)sleep((u_int)tp->timetowait);
2878fae3551SRodney W. Grimes 		if (!tp->timeleft)
2888fae3551SRodney W. Grimes 			break;
2898fae3551SRodney W. Grimes 	}
29039faed57SDag-Erling Smørgrav 	die_you_gravy_sucking_pig_dog();
2918fae3551SRodney W. Grimes }
2928fae3551SRodney W. Grimes 
2938fae3551SRodney W. Grimes static jmp_buf alarmbuf;
2948fae3551SRodney W. Grimes 
29504983c4fSDima Dorfman static const char *restricted_environ[] = {
296f6faa785SAlexander Langer 	"PATH=" _PATH_STDPATH,
297f6faa785SAlexander Langer 	NULL
298f6faa785SAlexander Langer };
299f6faa785SAlexander Langer 
3009534110fSXin LI static void
timewarn(int timeleft)301f74b6bc2SXin LI timewarn(int timeleft)
3028fae3551SRodney W. Grimes {
3038fae3551SRodney W. Grimes 	static int first;
3048fae3551SRodney W. Grimes 	static char hostname[MAXHOSTNAMELEN + 1];
3058fae3551SRodney W. Grimes 	FILE *pf;
3068fae3551SRodney W. Grimes 	char wcmd[MAXPATHLEN + 4];
3078fae3551SRodney W. Grimes 
3088fae3551SRodney W. Grimes 	if (!first++)
3098fae3551SRodney W. Grimes 		(void)gethostname(hostname, sizeof(hostname));
3108fae3551SRodney W. Grimes 
3118fae3551SRodney W. Grimes 	/* undoc -n option to wall suppresses normal wall banner */
3128fae3551SRodney W. Grimes 	(void)snprintf(wcmd, sizeof(wcmd), "%s -n", _PATH_WALL);
313f6faa785SAlexander Langer 	environ = restricted_environ;
3148fae3551SRodney W. Grimes 	if (!(pf = popen(wcmd, "w"))) {
3158fae3551SRodney W. Grimes 		syslog(LOG_ERR, "shutdown: can't find %s: %m", _PATH_WALL);
3168fae3551SRodney W. Grimes 		return;
3178fae3551SRodney W. Grimes 	}
3188fae3551SRodney W. Grimes 
3198fae3551SRodney W. Grimes 	(void)fprintf(pf,
3208fae3551SRodney W. Grimes 	    "\007*** %sSystem shutdown message from %s@%s ***\007\n",
3218fae3551SRodney W. Grimes 	    timeleft ? "": "FINAL ", whom, hostname);
3228fae3551SRodney W. Grimes 
3238fae3551SRodney W. Grimes 	if (timeleft > 10*60)
3248fae3551SRodney W. Grimes 		(void)fprintf(pf, "System going down at %5.5s\n\n",
3258fae3551SRodney W. Grimes 		    ctime(&shuttime) + 11);
3268fae3551SRodney W. Grimes 	else if (timeleft > 59)
3278fae3551SRodney W. Grimes 		(void)fprintf(pf, "System going down in %d minute%s\n\n",
3288fae3551SRodney W. Grimes 		    timeleft / 60, (timeleft > 60) ? "s" : "");
3298fae3551SRodney W. Grimes 	else if (timeleft)
330c7d73a4dSGleb Kurtsou 		(void)fprintf(pf, "System going down in %s30 seconds\n\n",
331c7d73a4dSGleb Kurtsou 		    (offset > 0 && offset < 30 ? "less than " : ""));
3328fae3551SRodney W. Grimes 	else
3338fae3551SRodney W. Grimes 		(void)fprintf(pf, "System going down IMMEDIATELY\n\n");
3348fae3551SRodney W. Grimes 
3358fae3551SRodney W. Grimes 	if (mbuflen)
3368fae3551SRodney W. Grimes 		(void)fwrite(mbuf, sizeof(*mbuf), mbuflen, pf);
3378fae3551SRodney W. Grimes 
3388fae3551SRodney W. Grimes 	/*
3398fae3551SRodney W. Grimes 	 * play some games, just in case wall doesn't come back
340d3714863SPhilippe Charnier 	 * probably unnecessary, given that wall is careful.
3418fae3551SRodney W. Grimes 	 */
3428fae3551SRodney W. Grimes 	if (!setjmp(alarmbuf)) {
3438fae3551SRodney W. Grimes 		(void)signal(SIGALRM, timeout);
3448fae3551SRodney W. Grimes 		(void)alarm((u_int)30);
3458fae3551SRodney W. Grimes 		(void)pclose(pf);
3468fae3551SRodney W. Grimes 		(void)alarm((u_int)0);
3478fae3551SRodney W. Grimes 		(void)signal(SIGALRM, SIG_DFL);
3488fae3551SRodney W. Grimes 	}
3498fae3551SRodney W. Grimes }
3508fae3551SRodney W. Grimes 
3519534110fSXin LI static void
timeout(int signo __unused)352f74b6bc2SXin LI timeout(int signo __unused)
3538fae3551SRodney W. Grimes {
3548fae3551SRodney W. Grimes 	longjmp(alarmbuf, 1);
3558fae3551SRodney W. Grimes }
3568fae3551SRodney W. Grimes 
3579534110fSXin LI static void
die_you_gravy_sucking_pig_dog(void)35839faed57SDag-Erling Smørgrav die_you_gravy_sucking_pig_dog(void)
3598fae3551SRodney W. Grimes {
360f6faa785SAlexander Langer 	char *empty_environ[] = { NULL };
3618fae3551SRodney W. Grimes 
3627b0a665dSMitchell Horne 	BOOTTRACE("%s by %s",
3637b0a665dSMitchell Horne 	    doreboot ? "reboot" : dohalt ? "halt" : dopower ? "power-down" :
3647b0a665dSMitchell Horne 	    docycle ? "power-cycle" : "shutdown", whom);
3658fae3551SRodney W. Grimes 	syslog(LOG_NOTICE, "%s by %s: %s",
3667e2d04d7SMike Smith 	    doreboot ? "reboot" : dohalt ? "halt" : dopower ? "power-down" :
367e60baa72SWarner Losh 	    docycle ? "power-cycle" : "shutdown", whom, mbuf);
3688fae3551SRodney W. Grimes 
3698fae3551SRodney W. Grimes 	(void)printf("\r\nSystem shutdown time has arrived\007\007\r\n");
3708fae3551SRodney W. Grimes 	if (killflg) {
3717b0a665dSMitchell Horne 		BOOTTRACE("fake shutdown...");
3728fae3551SRodney W. Grimes 		(void)printf("\rbut you'll have to do it yourself\r\n");
3737dd4667fSAndreas Schulz 		exit(0);
3748fae3551SRodney W. Grimes 	}
3758fae3551SRodney W. Grimes #ifdef DEBUG
3768fae3551SRodney W. Grimes 	if (doreboot)
3778fae3551SRodney W. Grimes 		(void)printf("reboot");
378e60baa72SWarner Losh 	else if (docycle)
379e60baa72SWarner Losh 		(void)printf("power-cycle");
3808fae3551SRodney W. Grimes 	else if (dohalt)
3818fae3551SRodney W. Grimes 		(void)printf("halt");
3827e2d04d7SMike Smith 	else if (dopower)
3837e2d04d7SMike Smith 		(void)printf("power-down");
38435cd460fSRuslan Ermilov 	if (nosync != NULL)
3858fae3551SRodney W. Grimes 		(void)printf(" no sync");
3868fae3551SRodney W. Grimes 	(void)printf("\nkill -HUP 1\n");
3878fae3551SRodney W. Grimes #else
38835cd460fSRuslan Ermilov 	if (!oflag) {
3897b0a665dSMitchell Horne 		BOOTTRACE("signal to init(8)...");
39035cd460fSRuslan Ermilov 		(void)kill(1, doreboot ? SIGINT :	/* reboot */
39135cd460fSRuslan Ermilov 			      dohalt ? SIGUSR1 :	/* halt */
39235cd460fSRuslan Ermilov 			      dopower ? SIGUSR2 :	/* power-down */
393e60baa72SWarner Losh 			      docycle ? SIGWINCH :	/* power-cycle */
39435cd460fSRuslan Ermilov 			      SIGTERM);			/* single-user */
39535cd460fSRuslan Ermilov 	} else {
3968fae3551SRodney W. Grimes 		if (doreboot) {
3977b0a665dSMitchell Horne 			BOOTTRACE("exec reboot(8) -l...");
398f6faa785SAlexander Langer 			execle(_PATH_REBOOT, "reboot", "-l", nosync,
399f6faa785SAlexander Langer 				(char *)NULL, empty_environ);
40035cd460fSRuslan Ermilov 			syslog(LOG_ERR, "shutdown: can't exec %s: %m.",
40135cd460fSRuslan Ermilov 				_PATH_REBOOT);
402f6faa785SAlexander Langer 			warn(_PATH_REBOOT);
4038fae3551SRodney W. Grimes 		}
4048fae3551SRodney W. Grimes 		else if (dohalt) {
4057b0a665dSMitchell Horne 			BOOTTRACE("exec halt(8) -l...");
406f6faa785SAlexander Langer 			execle(_PATH_HALT, "halt", "-l", nosync,
407f6faa785SAlexander Langer 				(char *)NULL, empty_environ);
40835cd460fSRuslan Ermilov 			syslog(LOG_ERR, "shutdown: can't exec %s: %m.",
40935cd460fSRuslan Ermilov 				_PATH_HALT);
410f6faa785SAlexander Langer 			warn(_PATH_HALT);
4118fae3551SRodney W. Grimes 		}
4127e2d04d7SMike Smith 		else if (dopower) {
4137b0a665dSMitchell Horne 			BOOTTRACE("exec halt(8) -l -p...");
4147e2d04d7SMike Smith 			execle(_PATH_HALT, "halt", "-l", "-p", nosync,
4157e2d04d7SMike Smith 				(char *)NULL, empty_environ);
41635cd460fSRuslan Ermilov 			syslog(LOG_ERR, "shutdown: can't exec %s: %m.",
41735cd460fSRuslan Ermilov 				_PATH_HALT);
4187e2d04d7SMike Smith 			warn(_PATH_HALT);
4197e2d04d7SMike Smith 		}
420e60baa72SWarner Losh 		else if (docycle) {
421e60baa72SWarner Losh 			execle(_PATH_HALT, "halt", "-l", "-c", nosync,
422e60baa72SWarner Losh 				(char *)NULL, empty_environ);
423e60baa72SWarner Losh 			syslog(LOG_ERR, "shutdown: can't exec %s: %m.",
424e60baa72SWarner Losh 				_PATH_HALT);
425e60baa72SWarner Losh 			warn(_PATH_HALT);
426e60baa72SWarner Losh 		}
4277b0a665dSMitchell Horne 		BOOTTRACE("SIGTERM to init(8)...");
42835cd460fSRuslan Ermilov 		(void)kill(1, SIGTERM);		/* to single-user */
42935cd460fSRuslan Ermilov 	}
4308fae3551SRodney W. Grimes #endif
4318fae3551SRodney W. Grimes 	finish(0);
4328fae3551SRodney W. Grimes }
4338fae3551SRodney W. Grimes 
4348fae3551SRodney W. Grimes #define	ATOI2(p)	(p[0] - '0') * 10 + (p[1] - '0'); p += 2;
4358fae3551SRodney W. Grimes 
4369534110fSXin LI static void
getoffset(char * timearg)437f74b6bc2SXin LI getoffset(char *timearg)
4388fae3551SRodney W. Grimes {
43904983c4fSDima Dorfman 	struct tm *lt;
44004983c4fSDima Dorfman 	char *p;
4418fae3551SRodney W. Grimes 	time_t now;
4427d3df190SEitan Adler 	int maybe_today, this_year;
443c7d73a4dSGleb Kurtsou 	char *timeunit;
4448fae3551SRodney W. Grimes 
4451d4f4c0dSJoseph Koshy 	(void)time(&now);
4461d4f4c0dSJoseph Koshy 
4478fae3551SRodney W. Grimes 	if (!strcasecmp(timearg, "now")) {		/* now */
4488fae3551SRodney W. Grimes 		offset = 0;
4491d4f4c0dSJoseph Koshy 		shuttime = now;
4508fae3551SRodney W. Grimes 		return;
4518fae3551SRodney W. Grimes 	}
4528fae3551SRodney W. Grimes 
4538fae3551SRodney W. Grimes 	if (*timearg == '+') {				/* +minutes */
4548fae3551SRodney W. Grimes 		if (!isdigit(*++timearg))
4558fae3551SRodney W. Grimes 			badtime();
456c7d73a4dSGleb Kurtsou 		errno = 0;
457c7d73a4dSGleb Kurtsou 		offset = strtol(timearg, &timeunit, 10);
458c7d73a4dSGleb Kurtsou 		if (offset < 0 || offset == LONG_MAX || errno != 0)
459966ea00aSRuslan Ermilov 			badtime();
460c7d73a4dSGleb Kurtsou 		if (timeunit[0] == '\0' || strcasecmp(timeunit, "m") == 0 ||
461c7d73a4dSGleb Kurtsou 		    strcasecmp(timeunit, "min") == 0 ||
462c7d73a4dSGleb Kurtsou 		    strcasecmp(timeunit, "mins") == 0) {
463c7d73a4dSGleb Kurtsou 			offset *= 60;
464c7d73a4dSGleb Kurtsou 		} else if (strcasecmp(timeunit, "h") == 0 ||
465c7d73a4dSGleb Kurtsou 		    strcasecmp(timeunit, "hour") == 0 ||
466c7d73a4dSGleb Kurtsou 		    strcasecmp(timeunit, "hours") == 0) {
467c7d73a4dSGleb Kurtsou 			offset *= 60 * 60;
468c7d73a4dSGleb Kurtsou 		} else if (strcasecmp(timeunit, "s") == 0 ||
469c7d73a4dSGleb Kurtsou 		    strcasecmp(timeunit, "sec") == 0 ||
470c7d73a4dSGleb Kurtsou 		    strcasecmp(timeunit, "secs") == 0) {
471c7d73a4dSGleb Kurtsou 			offset *= 1;
472c7d73a4dSGleb Kurtsou 		} else {
473c7d73a4dSGleb Kurtsou 			badtime();
474c7d73a4dSGleb Kurtsou 		}
4758fae3551SRodney W. Grimes 		shuttime = now + offset;
4768fae3551SRodney W. Grimes 		return;
4778fae3551SRodney W. Grimes 	}
4788fae3551SRodney W. Grimes 
4798fae3551SRodney W. Grimes 	/* handle hh:mm by getting rid of the colon */
4808fae3551SRodney W. Grimes 	for (p = timearg; *p; ++p)
481c1160fe4SBill Fumerola 		if (!isascii(*p) || !isdigit(*p)) {
4828fae3551SRodney W. Grimes 			if (*p == ':' && strlen(p) == 3) {
4838fae3551SRodney W. Grimes 				p[0] = p[1];
4848fae3551SRodney W. Grimes 				p[1] = p[2];
4858fae3551SRodney W. Grimes 				p[2] = '\0';
4868fae3551SRodney W. Grimes 			}
4878fae3551SRodney W. Grimes 			else
4888fae3551SRodney W. Grimes 				badtime();
489c1160fe4SBill Fumerola 		}
4908fae3551SRodney W. Grimes 
4918fae3551SRodney W. Grimes 	unsetenv("TZ");					/* OUR timezone */
4928fae3551SRodney W. Grimes 	lt = localtime(&now);				/* current time val */
4937d3df190SEitan Adler 	maybe_today = 1;
4948fae3551SRodney W. Grimes 
4958fae3551SRodney W. Grimes 	switch(strlen(timearg)) {
4968fae3551SRodney W. Grimes 	case 10:
4972dc34227SAlexander Langer 		this_year = lt->tm_year;
4988fae3551SRodney W. Grimes 		lt->tm_year = ATOI2(timearg);
4992dc34227SAlexander Langer 		/*
5002dc34227SAlexander Langer 		 * check if the specified year is in the next century.
5012dc34227SAlexander Langer 		 * allow for one year of user error as many people will
5022dc34227SAlexander Langer 		 * enter n - 1 at the start of year n.
5032dc34227SAlexander Langer 		 */
5042dc34227SAlexander Langer 		if (lt->tm_year < (this_year % 100) - 1)
5052dc34227SAlexander Langer 			lt->tm_year += 100;
50674179785SAlexander Langer 		/* adjust for the year 2000 and beyond */
5072dc34227SAlexander Langer 		lt->tm_year += (this_year - (this_year % 100));
5088fae3551SRodney W. Grimes 		/* FALLTHROUGH */
5098fae3551SRodney W. Grimes 	case 8:
5108fae3551SRodney W. Grimes 		lt->tm_mon = ATOI2(timearg);
5118fae3551SRodney W. Grimes 		if (--lt->tm_mon < 0 || lt->tm_mon > 11)
5128fae3551SRodney W. Grimes 			badtime();
5138fae3551SRodney W. Grimes 		/* FALLTHROUGH */
5148fae3551SRodney W. Grimes 	case 6:
515cb1101afSEitan Adler 		maybe_today = 0;
5168fae3551SRodney W. Grimes 		lt->tm_mday = ATOI2(timearg);
5178fae3551SRodney W. Grimes 		if (lt->tm_mday < 1 || lt->tm_mday > 31)
5188fae3551SRodney W. Grimes 			badtime();
5198fae3551SRodney W. Grimes 		/* FALLTHROUGH */
5208fae3551SRodney W. Grimes 	case 4:
5218fae3551SRodney W. Grimes 		lt->tm_hour = ATOI2(timearg);
5228fae3551SRodney W. Grimes 		if (lt->tm_hour < 0 || lt->tm_hour > 23)
5238fae3551SRodney W. Grimes 			badtime();
5248fae3551SRodney W. Grimes 		lt->tm_min = ATOI2(timearg);
5258fae3551SRodney W. Grimes 		if (lt->tm_min < 0 || lt->tm_min > 59)
5268fae3551SRodney W. Grimes 			badtime();
5278fae3551SRodney W. Grimes 		lt->tm_sec = 0;
5288fae3551SRodney W. Grimes 		if ((shuttime = mktime(lt)) == -1)
5298fae3551SRodney W. Grimes 			badtime();
530cb1101afSEitan Adler 
531cb1101afSEitan Adler 		if ((offset = shuttime - now) < 0) {
532cb1101afSEitan Adler 			if (!maybe_today)
533a2bfcdfdSPhilippe Charnier 				errx(1, "that time is already past.");
534cb1101afSEitan Adler 
535cb1101afSEitan Adler 			/*
536cb1101afSEitan Adler 			 * If the user only gave a time, assume that
537cb1101afSEitan Adler 			 * any time earlier than the current time
538cb1101afSEitan Adler 			 * was intended to be that time tomorrow.
539cb1101afSEitan Adler 			 */
540cb1101afSEitan Adler 			lt->tm_mday++;
541cb1101afSEitan Adler 			if ((shuttime = mktime(lt)) == -1)
542cb1101afSEitan Adler 				badtime();
543cb1101afSEitan Adler 			if ((offset = shuttime - now) < 0) {
544cb1101afSEitan Adler 				errx(1, "tomorrow is before today?");
545cb1101afSEitan Adler 			}
546cb1101afSEitan Adler 		}
5478fae3551SRodney W. Grimes 		break;
5488fae3551SRodney W. Grimes 	default:
5498fae3551SRodney W. Grimes 		badtime();
5508fae3551SRodney W. Grimes 	}
5518fae3551SRodney W. Grimes }
5528fae3551SRodney W. Grimes 
5538fae3551SRodney W. Grimes #define	NOMSG	"\n\nNO LOGINS: System going down at "
5549534110fSXin LI static void
nolog(void)5559534110fSXin LI nolog(void)
5568fae3551SRodney W. Grimes {
5578fae3551SRodney W. Grimes 	int logfd;
5588fae3551SRodney W. Grimes 	char *ct;
5598fae3551SRodney W. Grimes 
5608fae3551SRodney W. Grimes 	(void)unlink(_PATH_NOLOGIN);	/* in case linked to another file */
5618fae3551SRodney W. Grimes 	(void)signal(SIGINT, finish);
5628fae3551SRodney W. Grimes 	(void)signal(SIGHUP, finish);
5638fae3551SRodney W. Grimes 	(void)signal(SIGQUIT, finish);
5648fae3551SRodney W. Grimes 	(void)signal(SIGTERM, finish);
5658fae3551SRodney W. Grimes 	if ((logfd = open(_PATH_NOLOGIN, O_WRONLY|O_CREAT|O_TRUNC,
5668fae3551SRodney W. Grimes 	    0664)) >= 0) {
5678fae3551SRodney W. Grimes 		(void)write(logfd, NOMSG, sizeof(NOMSG) - 1);
5688fae3551SRodney W. Grimes 		ct = ctime(&shuttime);
5698fae3551SRodney W. Grimes 		(void)write(logfd, ct + 11, 5);
5708fae3551SRodney W. Grimes 		(void)write(logfd, "\n\n", 2);
5718fae3551SRodney W. Grimes 		(void)write(logfd, mbuf, strlen(mbuf));
5728fae3551SRodney W. Grimes 		(void)close(logfd);
5738fae3551SRodney W. Grimes 	}
5748fae3551SRodney W. Grimes }
5758fae3551SRodney W. Grimes 
5769534110fSXin LI static void
finish(int signo __unused)577f74b6bc2SXin LI finish(int signo __unused)
5788fae3551SRodney W. Grimes {
57939b831afSBruce Evans 	if (!killflg)
5808fae3551SRodney W. Grimes 		(void)unlink(_PATH_NOLOGIN);
5818fae3551SRodney W. Grimes 	exit(0);
5828fae3551SRodney W. Grimes }
5838fae3551SRodney W. Grimes 
5849534110fSXin LI static void
badtime(void)58527d434afSEd Schouten badtime(void)
5868fae3551SRodney W. Grimes {
587d3714863SPhilippe Charnier 	errx(1, "bad time format");
5888fae3551SRodney W. Grimes }
5898fae3551SRodney W. Grimes 
5909534110fSXin LI static void
usage(const char * cp)591f74b6bc2SXin LI usage(const char *cp)
5928fae3551SRodney W. Grimes {
59335cd460fSRuslan Ermilov 	if (cp != NULL)
59435cd460fSRuslan Ermilov 		warnx("%s", cp);
59535cd460fSRuslan Ermilov 	(void)fprintf(stderr,
5966c7ec630SKyle Evans 	    "usage: shutdown [-] [-c | -h | -p | -r | -k] [-o [-n]] [-q] time [warning-message ...]\n"
5976868734cSPawel Jakub Dawidek 	    "       poweroff\n");
5988fae3551SRodney W. Grimes 	exit(1);
5998fae3551SRodney W. Grimes }
600