xref: /openbsd-src/sbin/shutdown/shutdown.c (revision b205d946bc4d92a63918093e4dac726834be3d03)
1*b205d946Sflorian /*	$OpenBSD: shutdown.c,v 1.56 2024/04/28 16:43:42 florian Exp $	*/
2df930be7Sderaadt /*	$NetBSD: shutdown.c,v 1.9 1995/03/18 15:01:09 cgd Exp $	*/
3df930be7Sderaadt 
4df930be7Sderaadt /*
5df930be7Sderaadt  * Copyright (c) 1988, 1990, 1993
6df930be7Sderaadt  *	The Regents of the University of California.  All rights reserved.
7df930be7Sderaadt  *
8df930be7Sderaadt  * Redistribution and use in source and binary forms, with or without
9df930be7Sderaadt  * modification, are permitted provided that the following conditions
10df930be7Sderaadt  * are met:
11df930be7Sderaadt  * 1. Redistributions of source code must retain the above copyright
12df930be7Sderaadt  *    notice, this list of conditions and the following disclaimer.
13df930be7Sderaadt  * 2. Redistributions in binary form must reproduce the above copyright
14df930be7Sderaadt  *    notice, this list of conditions and the following disclaimer in the
15df930be7Sderaadt  *    documentation and/or other materials provided with the distribution.
161ef0d710Smillert  * 3. Neither the name of the University nor the names of its contributors
17df930be7Sderaadt  *    may be used to endorse or promote products derived from this software
18df930be7Sderaadt  *    without specific prior written permission.
19df930be7Sderaadt  *
20df930be7Sderaadt  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21df930be7Sderaadt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22df930be7Sderaadt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23df930be7Sderaadt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24df930be7Sderaadt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25df930be7Sderaadt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26df930be7Sderaadt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27df930be7Sderaadt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28df930be7Sderaadt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29df930be7Sderaadt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30df930be7Sderaadt  * SUCH DAMAGE.
31df930be7Sderaadt  */
32df930be7Sderaadt 
33b9fc9a72Sderaadt #include <sys/types.h>
34df930be7Sderaadt #include <sys/resource.h>
35df930be7Sderaadt #include <sys/syslog.h>
3626f85218Sericj #include <sys/wait.h>
37df930be7Sderaadt 
38df930be7Sderaadt #include <ctype.h>
39df930be7Sderaadt #include <fcntl.h>
4030b72076Sderaadt #include <sys/termios.h>
41df930be7Sderaadt #include <pwd.h>
42df930be7Sderaadt #include <signal.h>
43df930be7Sderaadt #include <stdio.h>
44df930be7Sderaadt #include <stdlib.h>
45df930be7Sderaadt #include <string.h>
4626f85218Sericj #include <time.h>
47df930be7Sderaadt #include <unistd.h>
48b9fc9a72Sderaadt #include <limits.h>
499ea0cd24Smickey #include <errno.h>
509ea0cd24Smickey #include <err.h>
51df930be7Sderaadt 
52df930be7Sderaadt #include "pathnames.h"
53df930be7Sderaadt 
54df930be7Sderaadt #ifdef DEBUG
55df930be7Sderaadt #undef _PATH_NOLOGIN
56df930be7Sderaadt #define	_PATH_NOLOGIN	"./nologin"
57df930be7Sderaadt #undef _PATH_FASTBOOT
58df930be7Sderaadt #define	_PATH_FASTBOOT	"./fastboot"
59df930be7Sderaadt #endif
60df930be7Sderaadt 
61c766b913Scheloha #define	H		*60*60LL
62c766b913Scheloha #define	M		*60LL
63c766b913Scheloha #define	S		*1LL
64c766b913Scheloha #define	TEN_HOURS	(10*60*60)
65c766b913Scheloha #define	NOLOG_TIME	(5*60)
66df930be7Sderaadt struct interval {
67c766b913Scheloha 	time_t timeleft;
68c766b913Scheloha 	time_t timetowait;
69df930be7Sderaadt } tlist[] = {
70c766b913Scheloha 	{    0,    0 },
71d9c11eccSmickey 	{ 10 H,  5 H },
72d9c11eccSmickey 	{  5 H,  3 H },
73d9c11eccSmickey 	{  2 H,  1 H },
74d9c11eccSmickey 	{  1 H, 30 M },
75d9c11eccSmickey 	{ 30 M, 10 M },
76d9c11eccSmickey 	{ 20 M, 10 M },
77d9c11eccSmickey 	{ 10 M,  5 M },
78d9c11eccSmickey 	{  5 M,  3 M },
79d9c11eccSmickey 	{  2 M,  1 M },
80d9c11eccSmickey 	{  1 M, 30 S },
81d9c11eccSmickey 	{ 30 S, 30 S },
82d9c11eccSmickey 	{    0,    0 }
83df930be7Sderaadt };
84c766b913Scheloha const int tlistlen = sizeof(tlist) / sizeof(tlist[0]);
85df930be7Sderaadt #undef H
86df930be7Sderaadt #undef M
87df930be7Sderaadt #undef S
88df930be7Sderaadt 
89df930be7Sderaadt static time_t offset, shuttime;
90ca707a5cSderaadt static int dofast, dohalt, doreboot, dopower, dodump, mbuflen, nosync;
9111ec551fScheloha static volatile sig_atomic_t killflg, timed_out;
928963e31cSdownsj static char *whom, mbuf[BUFSIZ];
93df930be7Sderaadt 
94c72b5b24Smillert void badtime(void);
956054a60aScloder void __dead die_you_gravy_sucking_pig_dog(void);
96c72b5b24Smillert void doitfast(void);
976054a60aScloder void __dead finish(int);
98c72b5b24Smillert void getoffset(char *);
996054a60aScloder void __dead loop(void);
1007e337579Scheloha void nolog(time_t);
101c72b5b24Smillert void timeout(int);
102c766b913Scheloha void timewarn(time_t);
103c72b5b24Smillert void usage(void);
104df930be7Sderaadt 
105df930be7Sderaadt int
main(int argc,char * argv[])106bc52e260Sderaadt main(int argc, char *argv[])
107df930be7Sderaadt {
1087e337579Scheloha 	char when[64];
10912b8e25fSderaadt 	char *p, *endp;
1107e337579Scheloha 	struct passwd *pw;
1117e337579Scheloha 	struct tm *lt;
1127e337579Scheloha 	int arglen, ch, len, readstdin = 0;
11312b8e25fSderaadt 	pid_t forkpid;
114df930be7Sderaadt 
115df930be7Sderaadt #ifndef DEBUG
1167e9edf67Smickey 	if (geteuid())
1177e9edf67Smickey 		errx(1, "NOT super-user");
118df930be7Sderaadt #endif
11902e888b3Smillert 	while ((ch = getopt(argc, argv, "dfhknpr-")) != -1)
120df930be7Sderaadt 		switch (ch) {
121df930be7Sderaadt 		case '-':
122df930be7Sderaadt 			readstdin = 1;
123df930be7Sderaadt 			break;
124891a04fbSmickey 		case 'd':
125891a04fbSmickey 			dodump = 1;
126891a04fbSmickey 			break;
127df930be7Sderaadt 		case 'f':
128df930be7Sderaadt 			dofast = 1;
129df930be7Sderaadt 			break;
130df930be7Sderaadt 		case 'h':
131df930be7Sderaadt 			dohalt = 1;
132df930be7Sderaadt 			break;
133df930be7Sderaadt 		case 'k':
134df930be7Sderaadt 			killflg = 1;
135df930be7Sderaadt 			break;
136df930be7Sderaadt 		case 'n':
1378963e31cSdownsj 			nosync = 1;
138df930be7Sderaadt 			break;
139dffb5420Sdownsj 		case 'p':
140dffb5420Sdownsj 			dopower = 1;
141dffb5420Sdownsj 			break;
142df930be7Sderaadt 		case 'r':
143df930be7Sderaadt 			doreboot = 1;
144df930be7Sderaadt 			break;
145df930be7Sderaadt 		default:
146df930be7Sderaadt 			usage();
147df930be7Sderaadt 		}
148df930be7Sderaadt 	argc -= optind;
149df930be7Sderaadt 	argv += optind;
150df930be7Sderaadt 
151df930be7Sderaadt 	if (argc < 1)
152df930be7Sderaadt 		usage();
153df930be7Sderaadt 
154df930be7Sderaadt 	if (dofast && nosync) {
15559eaf1c4Scheloha 		warnx("incompatible switches -f and -n.");
156df930be7Sderaadt 		usage();
157df930be7Sderaadt 	}
158df930be7Sderaadt 	if (doreboot && dohalt) {
15959eaf1c4Scheloha 		warnx("incompatible switches -h and -r.");
160df930be7Sderaadt 		usage();
161df930be7Sderaadt 	}
162d6bda39eSnaddy 	if (doreboot && dopower) {
16359eaf1c4Scheloha 		warnx("incompatible switches -p and -r.");
164dffb5420Sdownsj 		usage();
165dffb5420Sdownsj 	}
166a5617f5eSderaadt 
167a5617f5eSderaadt 	if (unveil(_PATH_CONSOLE, "rw") == -1)
168bc5a8259Sbeck 		err(1, "unveil %s", _PATH_CONSOLE);
169a5617f5eSderaadt 	if (unveil(_PATH_RC, "r") == -1)
170bc5a8259Sbeck 		err(1, "unveil %s", _PATH_RC);
171a5617f5eSderaadt 	if (unveil(_PATH_WALL, "x") == -1)
172bc5a8259Sbeck 		err(1, "unveil %s", _PATH_WALL);
173a5617f5eSderaadt 	if (unveil(_PATH_FASTBOOT, "wc") == -1)
174bc5a8259Sbeck 		err(1, "unveil %s", _PATH_FASTBOOT);
175a5617f5eSderaadt 	if (unveil(_PATH_NOLOGIN, "wc") == -1)
176bc5a8259Sbeck 		err(1, "unveil %s", _PATH_NOLOGIN);
177a5617f5eSderaadt 	if (dohalt || dopower) {
178a5617f5eSderaadt 		if (unveil(_PATH_HALT, "x") == -1)
179bc5a8259Sbeck 			err(1, "unveil %s", _PATH_HALT);
180a5617f5eSderaadt 	} else if (doreboot) {
181a5617f5eSderaadt 		if (unveil(_PATH_REBOOT, "x") == -1)
182bc5a8259Sbeck 			err(1, "unveil %s", _PATH_REBOOT);
183a5617f5eSderaadt 	} else {
184a5617f5eSderaadt 		if (unveil(_PATH_BSHELL, "x") == -1)
185bc5a8259Sbeck 			err(1, "unveil %s", _PATH_BSHELL);
186a5617f5eSderaadt 	}
187a5617f5eSderaadt 	if (pledge("stdio rpath wpath cpath getpw tty id proc exec", NULL) == -1)
188a5617f5eSderaadt 		err(1, "pledge");
189a5617f5eSderaadt 
190df930be7Sderaadt 	getoffset(*argv++);
191df930be7Sderaadt 
192df930be7Sderaadt 	if (*argv) {
193df930be7Sderaadt 		for (p = mbuf, len = sizeof(mbuf); *argv; ++argv) {
194df930be7Sderaadt 			arglen = strlen(*argv);
195df930be7Sderaadt 			if ((len -= arglen) <= 2)
196df930be7Sderaadt 				break;
197df930be7Sderaadt 			if (p != mbuf)
198df930be7Sderaadt 				*p++ = ' ';
199df930be7Sderaadt 			memcpy(p, *argv, arglen);
200df930be7Sderaadt 			p += arglen;
201df930be7Sderaadt 		}
202df930be7Sderaadt 		*p = '\n';
203df930be7Sderaadt 		*++p = '\0';
204df930be7Sderaadt 	}
205df930be7Sderaadt 
206df930be7Sderaadt 	if (readstdin) {
207df930be7Sderaadt 		p = mbuf;
208df930be7Sderaadt 		endp = mbuf + sizeof(mbuf) - 2;
209df930be7Sderaadt 		for (;;) {
210df930be7Sderaadt 			if (!fgets(p, endp - p + 1, stdin))
211df930be7Sderaadt 				break;
21212b8e25fSderaadt 			for (; *p &&  p < endp; ++p)
21312b8e25fSderaadt 				;
214df930be7Sderaadt 			if (p == endp) {
215df930be7Sderaadt 				*p = '\n';
216df930be7Sderaadt 				*++p = '\0';
217df930be7Sderaadt 				break;
218df930be7Sderaadt 			}
219df930be7Sderaadt 		}
220df930be7Sderaadt 	}
221df930be7Sderaadt 	mbuflen = strlen(mbuf);
222df930be7Sderaadt 
2237e337579Scheloha 	if (offset > 0) {
2247e337579Scheloha 		shuttime = time(NULL) + offset;
2257e337579Scheloha 		lt = localtime(&shuttime);
2267e337579Scheloha 		if (lt != NULL) {
2277e337579Scheloha 			strftime(when, sizeof(when), "%a %b %e %T %Z %Y", lt);
2287e337579Scheloha 			printf("Shutdown at %s.\n", when);
2297e337579Scheloha 		} else
230a91c3ab5Sderaadt 			printf("Shutdown soon.\n");
231a91c3ab5Sderaadt 	} else
2327e337579Scheloha 		printf("Shutdown NOW!\n");
233df930be7Sderaadt 
234df930be7Sderaadt 	if (!(whom = getlogin()))
235df930be7Sderaadt 		whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???";
236df930be7Sderaadt 
237df930be7Sderaadt #ifdef DEBUG
238df930be7Sderaadt 	(void)putc('\n', stdout);
239df930be7Sderaadt #else
240df930be7Sderaadt 	(void)setpriority(PRIO_PROCESS, 0, PRIO_MIN);
241df930be7Sderaadt 
242df930be7Sderaadt 	forkpid = fork();
2439ea0cd24Smickey 	if (forkpid == -1)
2449ea0cd24Smickey 		err(1, "fork");
245df930be7Sderaadt 	if (forkpid) {
24613ee8a54Sderaadt 		(void)printf("shutdown: [pid %ld]\n", (long)forkpid);
247df930be7Sderaadt 		exit(0);
248df930be7Sderaadt 	}
2492465766eSderaadt 	setsid();
250df930be7Sderaadt #endif
251df930be7Sderaadt 	openlog("shutdown", LOG_CONS, LOG_AUTH);
252df930be7Sderaadt 	loop();
253df930be7Sderaadt 	/* NOTREACHED */
254df930be7Sderaadt }
255df930be7Sderaadt 
256df930be7Sderaadt void
loop(void)257bc52e260Sderaadt loop(void)
258df930be7Sderaadt {
259c766b913Scheloha 	struct timespec timeout;
260c766b913Scheloha 	int broadcast, i, logged;
261df930be7Sderaadt 
262c766b913Scheloha 	broadcast = 1;
263c766b913Scheloha 
264c766b913Scheloha 	for (i = 0; i < tlistlen - 1; i++) {
265c766b913Scheloha 		if (offset > tlist[i + 1].timeleft) {
266c766b913Scheloha 			tlist[i].timeleft = offset;
267c766b913Scheloha 			tlist[i].timetowait = offset - tlist[i + 1].timeleft;
268df930be7Sderaadt 			break;
269df930be7Sderaadt 		}
270c766b913Scheloha 	}
271c766b913Scheloha 
272c766b913Scheloha 	/*
273c766b913Scheloha 	 * Don't spam the users: skip our offset's warning broadcast if
274c766b913Scheloha 	 * there's a broadcast scheduled after ours and it's relatively
275c766b913Scheloha 	 * imminent.
276c766b913Scheloha 	 */
277c766b913Scheloha 	if (offset > TEN_HOURS ||
278c766b913Scheloha 	    (offset > 0 && tlist[i].timetowait < tlist[i+1].timetowait / 5))
279c766b913Scheloha 		broadcast = 0;
280c766b913Scheloha 
281c766b913Scheloha 	for (logged = 0; i < tlistlen; i++) {
282c766b913Scheloha 		if (broadcast)
283c766b913Scheloha 			timewarn(tlist[i].timeleft);
284c766b913Scheloha 		broadcast = 1;
285c766b913Scheloha 		if (!logged && tlist[i].timeleft <= NOLOG_TIME) {
286c766b913Scheloha 			logged = 1;
2877e337579Scheloha 			nolog(tlist[i].timeleft);
288c766b913Scheloha 		}
289c766b913Scheloha 		timeout.tv_sec = tlist[i].timetowait;
290c766b913Scheloha 		timeout.tv_nsec = 0;
291c766b913Scheloha 		nanosleep(&timeout, NULL);
292c766b913Scheloha 	}
293df930be7Sderaadt 	die_you_gravy_sucking_pig_dog();
294df930be7Sderaadt }
295df930be7Sderaadt 
296362e25abSderaadt static char *restricted_environ[] = {
297362e25abSderaadt 	"PATH=" _PATH_STDPATH,
298362e25abSderaadt 	NULL
299362e25abSderaadt };
300362e25abSderaadt 
301df930be7Sderaadt void
timewarn(time_t timeleft)302c766b913Scheloha timewarn(time_t timeleft)
303df930be7Sderaadt {
304b9fc9a72Sderaadt 	static char hostname[HOST_NAME_MAX+1];
3057e337579Scheloha 	char when[64];
3067e337579Scheloha 	struct tm *lt;
30712b8e25fSderaadt 	static int first;
308fde1d200Scheloha 	int fd[2];
309fde1d200Scheloha 	pid_t pid, wpid;
310df930be7Sderaadt 
311df930be7Sderaadt 	if (!first++)
312df930be7Sderaadt 		(void)gethostname(hostname, sizeof(hostname));
313df930be7Sderaadt 
314fde1d200Scheloha 	if (pipe(fd) == -1) {
315fde1d200Scheloha 		syslog(LOG_ERR, "pipe: %m");
316df930be7Sderaadt 		return;
317df930be7Sderaadt 	}
318fde1d200Scheloha 	switch (pid = fork()) {
319fde1d200Scheloha 	case -1:
320fde1d200Scheloha 		syslog(LOG_ERR, "fork: %m");
321fde1d200Scheloha 		close(fd[0]);
322fde1d200Scheloha 		close(fd[1]);
323fde1d200Scheloha 		return;
324fde1d200Scheloha 	case 0:
325fde1d200Scheloha 		if (dup2(fd[0], STDIN_FILENO) == -1) {
326fde1d200Scheloha 			syslog(LOG_ERR, "dup2: %m");
327fde1d200Scheloha 			_exit(1);
328fde1d200Scheloha 		}
329fde1d200Scheloha 		if (fd[0] != STDIN_FILENO)
330fde1d200Scheloha 			close(fd[0]);
331fde1d200Scheloha 		close(fd[1]);
332fde1d200Scheloha 		/* wall(1)'s undocumented '-n' flag suppresses its banner. */
333fde1d200Scheloha 		execle(_PATH_WALL, _PATH_WALL, "-n", (char *)NULL,
334fde1d200Scheloha 		    restricted_environ);
335fde1d200Scheloha 		syslog(LOG_ERR, "%s: %m", _PATH_WALL);
336fde1d200Scheloha 		_exit(1);
337fde1d200Scheloha 	default:
338fde1d200Scheloha 		close(fd[0]);
339fde1d200Scheloha 	}
340df930be7Sderaadt 
341fde1d200Scheloha 	dprintf(fd[1],
342df930be7Sderaadt 	    "\007*** %sSystem shutdown message from %s@%s ***\007\n",
343df930be7Sderaadt 	    timeleft ? "": "FINAL ", whom, hostname);
344df930be7Sderaadt 
345a91c3ab5Sderaadt 	if (timeleft > 10 * 60) {
3467e337579Scheloha 		shuttime = time(NULL) + timeleft;
3477e337579Scheloha 		lt = localtime(&shuttime);
348*b205d946Sflorian 		if (lt != NULL) {
3497e337579Scheloha 			strftime(when, sizeof(when), "%H:%M %Z", lt);
3507e337579Scheloha 			dprintf(fd[1], "System going down at %s\n\n", when);
351*b205d946Sflorian 		} else
352*b205d946Sflorian 			dprintf(fd[1], "System going down in %lld minute%s\n\n",
353*b205d946Sflorian 			    (long long)(timeleft / 60),
354*b205d946Sflorian 			    (timeleft > 60) ? "s" : "");
355c766b913Scheloha 	} else if (timeleft > 59) {
356c766b913Scheloha 		dprintf(fd[1], "System going down in %lld minute%s\n\n",
357c766b913Scheloha 		    (long long)(timeleft / 60), (timeleft > 60) ? "s" : "");
358c766b913Scheloha 	} else if (timeleft)
359fde1d200Scheloha 		dprintf(fd[1], "System going down in 30 seconds\n\n");
360df930be7Sderaadt 	else
361fde1d200Scheloha 		dprintf(fd[1], "System going down IMMEDIATELY\n\n");
362df930be7Sderaadt 
363df930be7Sderaadt 	if (mbuflen)
364fde1d200Scheloha 		(void)write(fd[1], mbuf, mbuflen);
365fde1d200Scheloha 	close(fd[1]);
366df930be7Sderaadt 
367df930be7Sderaadt 	/*
368fde1d200Scheloha 	 * If we wait longer than 30 seconds for wall(1) to exit we'll
369fde1d200Scheloha 	 * throw off our schedule.
370df930be7Sderaadt 	 */
371fde1d200Scheloha 	signal(SIGALRM, timeout);
372fde1d200Scheloha 	siginterrupt(SIGALRM, 1);
373fde1d200Scheloha 	alarm(30);
374fde1d200Scheloha 	while ((wpid = wait(NULL)) != pid && !timed_out)
375fde1d200Scheloha 		continue;
376fde1d200Scheloha 	alarm(0);
377fde1d200Scheloha 	signal(SIGALRM, SIG_DFL);
378fde1d200Scheloha 	if (timed_out) {
379fde1d200Scheloha 		syslog(LOG_NOTICE,
380fde1d200Scheloha 		    "wall[%ld] is taking too long to exit; continuing",
381fde1d200Scheloha 		    (long)pid);
382fde1d200Scheloha 		timed_out = 0;
383df930be7Sderaadt 	}
384df930be7Sderaadt }
385df930be7Sderaadt 
386df930be7Sderaadt void
timeout(int signo)387bc52e260Sderaadt timeout(int signo)
388df930be7Sderaadt {
389fde1d200Scheloha 	timed_out = 1;
390df930be7Sderaadt }
391df930be7Sderaadt 
392df930be7Sderaadt void
die_you_gravy_sucking_pig_dog(void)393bc52e260Sderaadt die_you_gravy_sucking_pig_dog(void)
394df930be7Sderaadt {
395df930be7Sderaadt 
396df930be7Sderaadt 	syslog(LOG_NOTICE, "%s by %s: %s",
397d6bda39eSnaddy 	    doreboot ? "reboot" : dopower ? "power-down" : dohalt ? "halt" :
398d6bda39eSnaddy 	    "shutdown", whom, mbuf);
399df930be7Sderaadt 	(void)sleep(2);
400df930be7Sderaadt 
401df930be7Sderaadt 	(void)printf("\r\nSystem shutdown time has arrived\007\007\r\n");
402df930be7Sderaadt 	if (killflg) {
403df930be7Sderaadt 		(void)printf("\rbut you'll have to do it yourself\r\n");
404df930be7Sderaadt 		finish(0);
405df930be7Sderaadt 	}
406df930be7Sderaadt 	if (dofast)
407df930be7Sderaadt 		doitfast();
4088aa0c8fbSderaadt 
4098aa0c8fbSderaadt 	if (pledge("stdio rpath wpath cpath tty id proc exec", NULL) == -1)
4108aa0c8fbSderaadt 		err(1, "pledge");
4118aa0c8fbSderaadt 
412df930be7Sderaadt #ifdef DEBUG
413df930be7Sderaadt 	if (doreboot)
414df930be7Sderaadt 		(void)printf("reboot");
415d6bda39eSnaddy 	else if (dopower)
416d6bda39eSnaddy 		(void)printf("power-down");
417df930be7Sderaadt 	else if (dohalt)
418df930be7Sderaadt 		(void)printf("halt");
419df930be7Sderaadt 	if (nosync)
420df930be7Sderaadt 		(void)printf(" no sync");
421df930be7Sderaadt 	if (dofast)
422df930be7Sderaadt 		(void)printf(" no fsck");
423891a04fbSmickey 	if (dodump)
424891a04fbSmickey 		(void)printf(" with dump");
425df930be7Sderaadt 	(void)printf("\nkill -HUP 1\n");
426df930be7Sderaadt #else
427ae9c7063Snaddy 	if (dohalt || dopower || doreboot) {
428ae9c7063Snaddy 		char *args[10];
429ae9c7063Snaddy 		char **arg, *path;
430ae9c7063Snaddy 
4318aa0c8fbSderaadt 		if (pledge("stdio exec", NULL) == -1)
4328aa0c8fbSderaadt 			err(1, "pledge");
4338aa0c8fbSderaadt 
434ae9c7063Snaddy 		arg = &args[0];
435df930be7Sderaadt 		if (doreboot) {
436ae9c7063Snaddy 			path = _PATH_REBOOT;
437ae9c7063Snaddy 			*arg++ = "reboot";
438ae9c7063Snaddy 		} else {
439ae9c7063Snaddy 			path = _PATH_HALT;
440ae9c7063Snaddy 			*arg++ = "halt";
441df930be7Sderaadt 		}
442ae9c7063Snaddy 		*arg++ = "-l";
443ae9c7063Snaddy 		if (dopower)
444ae9c7063Snaddy 			*arg++ = "-p";
445ae9c7063Snaddy 		if (nosync)
446ae9c7063Snaddy 			*arg++ = "-n";
447ae9c7063Snaddy 		if (dodump)
448ae9c7063Snaddy 			*arg++ = "-d";
449ae9c7063Snaddy 		*arg++ = NULL;
450ae9c7063Snaddy 		execve(path, args, NULL);
451ae9c7063Snaddy 		syslog(LOG_ERR, "shutdown: can't exec %s: %m.", path);
452a2f55687Sfcambus 		warn("%s", path);
453df930be7Sderaadt 	}
45475a54d2eSderaadt 	if (access(_PATH_RC, R_OK) != -1) {
4552dc1793dSmillert 		pid_t pid;
45630b72076Sderaadt 		struct termios t;
45730b72076Sderaadt 		int fd;
4582dc1793dSmillert 
4592dc1793dSmillert 		switch ((pid = fork())) {
4602dc1793dSmillert 		case -1:
4612dc1793dSmillert 			break;
4622dc1793dSmillert 		case 0:
46330b72076Sderaadt 			if (revoke(_PATH_CONSOLE) == -1)
46430b72076Sderaadt 				perror("revoke");
46530b72076Sderaadt 			if (setsid() == -1)
46630b72076Sderaadt 				perror("setsid");
46730b72076Sderaadt 			fd = open(_PATH_CONSOLE, O_RDWR);
46830b72076Sderaadt 			if (fd == -1)
46930b72076Sderaadt 				perror("open");
47030b72076Sderaadt 			dup2(fd, 0);
47130b72076Sderaadt 			dup2(fd, 1);
47230b72076Sderaadt 			dup2(fd, 2);
47330b72076Sderaadt 			if (fd > 2)
47430b72076Sderaadt 				close(fd);
47530b72076Sderaadt 
47630b72076Sderaadt 			/* At a minimum... */
47730b72076Sderaadt 			tcgetattr(0, &t);
47830b72076Sderaadt 			t.c_oflag |= (ONLCR | OPOST);
47930b72076Sderaadt 			tcsetattr(0, TCSANOW, &t);
48030b72076Sderaadt 
481c96f6a27Sderaadt 			execl(_PATH_BSHELL, "sh", _PATH_RC, "shutdown", (char *)NULL);
48230b72076Sderaadt 			_exit(1);
4832dc1793dSmillert 		default:
4842dc1793dSmillert 			waitpid(pid, NULL, 0);
4852dc1793dSmillert 		}
4862dc1793dSmillert 	}
487df930be7Sderaadt 	(void)kill(1, SIGTERM);		/* to single user */
488df930be7Sderaadt #endif
489df930be7Sderaadt 	finish(0);
490df930be7Sderaadt }
491df930be7Sderaadt 
492df930be7Sderaadt #define	ATOI2(p)	(p[0] - '0') * 10 + (p[1] - '0'); p += 2;
493df930be7Sderaadt 
494df930be7Sderaadt void
getoffset(char * timearg)495bc52e260Sderaadt getoffset(char *timearg)
496df930be7Sderaadt {
4977e337579Scheloha 	char when[64];
49844680ff5Scheloha 	const char *errstr;
499e073c79dSmpech 	struct tm *lt;
50002baf7cdSalex 	int this_year;
50144680ff5Scheloha 	time_t minutes, now;
50212b8e25fSderaadt 	char *p;
503df930be7Sderaadt 
504df930be7Sderaadt 	if (!strcasecmp(timearg, "now")) {		/* now */
505df930be7Sderaadt 		offset = 0;
506df930be7Sderaadt 		return;
507df930be7Sderaadt 	}
508df930be7Sderaadt 
50944680ff5Scheloha 	if (timearg[0] == '+') {			/* +minutes */
51044680ff5Scheloha 		minutes = strtonum(timearg, 0, INT_MAX, &errstr);
511089371e7Sderaadt 		if (errstr)
51244680ff5Scheloha 			errx(1, "relative offset is %s: %s", errstr, timearg);
51344680ff5Scheloha 		offset = minutes * 60;
514df930be7Sderaadt 		return;
515df930be7Sderaadt 	}
516df930be7Sderaadt 
517df930be7Sderaadt 	/* handle hh:mm by getting rid of the colon */
518a4df0321Sderaadt 	for (p = timearg; *p; ++p) {
519025f5691Sderaadt 		if (!isascii((unsigned char)*p) || !isdigit((unsigned char)*p)) {
520df930be7Sderaadt 			if (*p == ':' && strlen(p) == 3) {
521df930be7Sderaadt 				p[0] = p[1];
522df930be7Sderaadt 				p[1] = p[2];
523df930be7Sderaadt 				p[2] = '\0';
524a4df0321Sderaadt 			} else
525df930be7Sderaadt 				badtime();
526a4df0321Sderaadt 		}
527a4df0321Sderaadt 	}
528df930be7Sderaadt 
529df930be7Sderaadt 	unsetenv("TZ");					/* OUR timezone */
5307e337579Scheloha 	time(&now);
531df930be7Sderaadt 	lt = localtime(&now);				/* current time val */
532df930be7Sderaadt 
533*b205d946Sflorian 	if (lt == NULL)
534*b205d946Sflorian 		badtime();
535*b205d946Sflorian 
536df930be7Sderaadt 	switch (strlen(timearg)) {
537df930be7Sderaadt 	case 10:
53802baf7cdSalex 		this_year = lt->tm_year;
539df930be7Sderaadt 		lt->tm_year = ATOI2(timearg);
54002baf7cdSalex 		/*
54102baf7cdSalex 		 * check if the specified year is in the next century.
54202baf7cdSalex 		 * allow for one year of user error as many people will
54302baf7cdSalex 		 * enter n - 1 at the start of year n.
54402baf7cdSalex 		 */
54502baf7cdSalex 		if (lt->tm_year < (this_year % 100) - 1)
54602baf7cdSalex 			lt->tm_year += 100;
54702baf7cdSalex 		/* adjust for the year 2000 and beyond */
54802baf7cdSalex 		lt->tm_year += (this_year - (this_year % 100));
549df930be7Sderaadt 		/* FALLTHROUGH */
550df930be7Sderaadt 	case 8:
551df930be7Sderaadt 		lt->tm_mon = ATOI2(timearg);
552df930be7Sderaadt 		if (--lt->tm_mon < 0 || lt->tm_mon > 11)
553df930be7Sderaadt 			badtime();
554df930be7Sderaadt 		/* FALLTHROUGH */
555df930be7Sderaadt 	case 6:
556df930be7Sderaadt 		lt->tm_mday = ATOI2(timearg);
557df930be7Sderaadt 		if (lt->tm_mday < 1 || lt->tm_mday > 31)
558df930be7Sderaadt 			badtime();
559df930be7Sderaadt 		/* FALLTHROUGH */
560df930be7Sderaadt 	case 4:
561df930be7Sderaadt 		lt->tm_hour = ATOI2(timearg);
562df930be7Sderaadt 		if (lt->tm_hour < 0 || lt->tm_hour > 23)
563df930be7Sderaadt 			badtime();
564df930be7Sderaadt 		lt->tm_min = ATOI2(timearg);
565df930be7Sderaadt 		if (lt->tm_min < 0 || lt->tm_min > 59)
566df930be7Sderaadt 			badtime();
567df930be7Sderaadt 		lt->tm_sec = 0;
568df930be7Sderaadt 		if ((shuttime = mktime(lt)) == -1)
569df930be7Sderaadt 			badtime();
5707e337579Scheloha 		if ((offset = shuttime - now) < 0) {
5717e337579Scheloha 			strftime(when, sizeof(when), "%a %b %e %T %Z %Y", lt);
5727e337579Scheloha 			errx(1, "time is already past: %s", when);
5737e337579Scheloha 		}
574df930be7Sderaadt 		break;
575df930be7Sderaadt 	default:
576df930be7Sderaadt 		badtime();
577df930be7Sderaadt 	}
578df930be7Sderaadt }
579df930be7Sderaadt 
580df930be7Sderaadt void
doitfast(void)581bc52e260Sderaadt doitfast(void)
582df930be7Sderaadt {
583df930be7Sderaadt 	int fastfd;
584df930be7Sderaadt 
585df930be7Sderaadt 	if ((fastfd = open(_PATH_FASTBOOT, O_WRONLY|O_CREAT|O_TRUNC,
586df930be7Sderaadt 	    0664)) >= 0) {
587a91c3ab5Sderaadt 		dprintf(fastfd, "fastboot file for fsck\n");
588a91c3ab5Sderaadt 		close(fastfd);
589df930be7Sderaadt 	}
590df930be7Sderaadt }
591df930be7Sderaadt 
592df930be7Sderaadt void
nolog(time_t timeleft)5937e337579Scheloha nolog(time_t timeleft)
594df930be7Sderaadt {
5957e337579Scheloha 	char when[64];
596a91c3ab5Sderaadt 	struct tm *tm;
5977e337579Scheloha 	int logfd;
598df930be7Sderaadt 
599df930be7Sderaadt 	(void)unlink(_PATH_NOLOGIN);	/* in case linked to another file */
600df930be7Sderaadt 	(void)signal(SIGINT, finish);
601df930be7Sderaadt 	(void)signal(SIGHUP, finish);
602df930be7Sderaadt 	(void)signal(SIGQUIT, finish);
603df930be7Sderaadt 	(void)signal(SIGTERM, finish);
6047e337579Scheloha 	shuttime = time(NULL) + timeleft;
605a91c3ab5Sderaadt 	tm = localtime(&shuttime);
606*b205d946Sflorian 	if ((logfd = open(_PATH_NOLOGIN, O_WRONLY|O_CREAT|O_TRUNC,
607df930be7Sderaadt 	    0664)) >= 0) {
608*b205d946Sflorian 		if (tm) {
6097e337579Scheloha 			strftime(when, sizeof(when), "at %H:%M %Z", tm);
610*b205d946Sflorian 			dprintf(logfd,
611*b205d946Sflorian 			    "\n\nNO LOGINS: System going down %s\n\n", when);
612*b205d946Sflorian 		} else
613*b205d946Sflorian 			dprintf(logfd,
614*b205d946Sflorian 			    "\n\nNO LOGINS: System going in %lld minute%s\n\n",
615*b205d946Sflorian 			    (long long)(timeleft / 60),
616*b205d946Sflorian 			    (timeleft > 60) ? "s" : "");
617a91c3ab5Sderaadt 		close(logfd);
618df930be7Sderaadt 	}
619df930be7Sderaadt }
620df930be7Sderaadt 
621df930be7Sderaadt void
finish(int signo)622bc52e260Sderaadt finish(int signo)
623df930be7Sderaadt {
624df930be7Sderaadt 	if (!killflg)
625df930be7Sderaadt 		(void)unlink(_PATH_NOLOGIN);
626ff323b83Sderaadt 	if (signo == 0)
627df930be7Sderaadt 		exit(0);
628ff323b83Sderaadt 	else
629ff323b83Sderaadt 		_exit(0);
630df930be7Sderaadt }
631df930be7Sderaadt 
632df930be7Sderaadt void
badtime(void)633bc52e260Sderaadt badtime(void)
634df930be7Sderaadt {
6359ea0cd24Smickey 	errx(1, "bad time format.");
636df930be7Sderaadt }
637df930be7Sderaadt 
638df930be7Sderaadt void
usage(void)639bc52e260Sderaadt usage(void)
640df930be7Sderaadt {
641d6bda39eSnaddy 	fprintf(stderr,
642d6bda39eSnaddy 	    "usage: shutdown [-] [-dfhknpr] time [warning-message ...]\n");
643df930be7Sderaadt 	exit(1);
644df930be7Sderaadt }
645