xref: /netbsd-src/sbin/reboot/reboot.c (revision 85f97b7a69dd2602478c01e06a44cac87df7e462)
1*85f97b7aSroy /*	$NetBSD: reboot.c,v 1.41 2019/08/08 21:14:12 roy Exp $	*/
20114e805Scgd 
361f28255Scgd /*
4ce5ebe99Scgd  * Copyright (c) 1980, 1986, 1993
5ce5ebe99Scgd  *	The Regents of the University of California.  All rights reserved.
661f28255Scgd  *
761f28255Scgd  * Redistribution and use in source and binary forms, with or without
861f28255Scgd  * modification, are permitted provided that the following conditions
961f28255Scgd  * are met:
1061f28255Scgd  * 1. Redistributions of source code must retain the above copyright
1161f28255Scgd  *    notice, this list of conditions and the following disclaimer.
1261f28255Scgd  * 2. Redistributions in binary form must reproduce the above copyright
1361f28255Scgd  *    notice, this list of conditions and the following disclaimer in the
1461f28255Scgd  *    documentation and/or other materials provided with the distribution.
15276d62f6Sagc  * 3. Neither the name of the University nor the names of its contributors
1661f28255Scgd  *    may be used to endorse or promote products derived from this software
1761f28255Scgd  *    without specific prior written permission.
1861f28255Scgd  *
1961f28255Scgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2061f28255Scgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2161f28255Scgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2261f28255Scgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2361f28255Scgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2461f28255Scgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2561f28255Scgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2661f28255Scgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2761f28255Scgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2861f28255Scgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2961f28255Scgd  * SUCH DAMAGE.
3061f28255Scgd  */
3161f28255Scgd 
326b1fa5abSperry #include <sys/cdefs.h>
336b1fa5abSperry 
3461f28255Scgd #ifndef lint
356543a91fSlukem __COPYRIGHT("@(#) Copyright (c) 1980, 1986, 1993\
366543a91fSlukem  The Regents of the University of California.  All rights reserved.");
3761f28255Scgd #endif /* not lint */
3861f28255Scgd 
3961f28255Scgd #ifndef lint
400114e805Scgd #if 0
41ce5ebe99Scgd static char sccsid[] = "@(#)reboot.c	8.1 (Berkeley) 6/5/93";
420114e805Scgd #else
43*85f97b7aSroy __RCSID("$NetBSD: reboot.c,v 1.41 2019/08/08 21:14:12 roy Exp $");
440114e805Scgd #endif
4561f28255Scgd #endif /* not lint */
4661f28255Scgd 
4761f28255Scgd #include <sys/reboot.h>
48689db8fcSmycroft 
496b1fa5abSperry #include <err.h>
50ce5ebe99Scgd #include <errno.h>
51689db8fcSmycroft #include <pwd.h>
52689db8fcSmycroft #include <signal.h>
53ce5ebe99Scgd #include <stdio.h>
54ce5ebe99Scgd #include <stdlib.h>
55ce5ebe99Scgd #include <string.h>
56689db8fcSmycroft #include <syslog.h>
57689db8fcSmycroft #include <unistd.h>
586b1fa5abSperry #include <util.h>
596a9b095cSchristos #ifdef SUPPORT_UTMPX
606a9b095cSchristos #include <utmpx.h>
616a9b095cSchristos #endif
6261f28255Scgd 
63eccb2789Sjoerg __dead static void usage(void);
64ce5ebe99Scgd 
65eccb2789Sjoerg static int dohalt;
66eccb2789Sjoerg static int dopoweroff;
67c9117f53Scgd 
68c9117f53Scgd int
main(int argc,char * argv[])69d9deaad6Swiz main(int argc, char *argv[])
7061f28255Scgd {
718a986b2eScgd 	const char *progname;
726b1fa5abSperry 	int i;
7361f28255Scgd 	struct passwd *pw;
7474693079Smrg 	int ch, howto, lflag, nflag, qflag, sverrno, len;
75b93acf4aSmycroft 	const char *user;
76b93acf4aSmycroft 	char *bootstr, **av;
7761f28255Scgd 
788a986b2eScgd 	progname = getprogname();
79885f3f0aSchristos 	if (progname[0] == '-')
80885f3f0aSchristos 		progname++;
81885f3f0aSchristos 	if (strcmp(progname, "halt") == 0) {
82ce5ebe99Scgd 		dohalt = 1;
83ce5ebe99Scgd 		howto = RB_HALT;
84885f3f0aSchristos 	} else if (strcmp(progname, "poweroff") == 0) {
85416a0bcaShubertf 		dopoweroff = 1;
86416a0bcaShubertf 		howto = RB_HALT | RB_POWERDOWN;
87ce5ebe99Scgd 	} else
8861f28255Scgd 		howto = 0;
89ce5ebe99Scgd 	lflag = nflag = qflag = 0;
9000ef49e9Sdyoung 	while ((ch = getopt(argc, argv, "dlnpqvxz")) != -1)
91ce5ebe99Scgd 		switch(ch) {
92bc0a5a65Smycroft 		case 'd':
93bc0a5a65Smycroft 			howto |= RB_DUMP;
94bc0a5a65Smycroft 			break;
95bc0a5a65Smycroft 		case 'l':
96ce5ebe99Scgd 			lflag = 1;
97ce5ebe99Scgd 			break;
98ce5ebe99Scgd 		case 'n':
99ce5ebe99Scgd 			nflag = 1;
10061f28255Scgd 			howto |= RB_NOSYNC;
101ce5ebe99Scgd 			break;
1025f9840e9Sthorpej 		case 'p':
1035f9840e9Sthorpej 			if (dohalt == 0)
1045f9840e9Sthorpej 				usage();
1055f9840e9Sthorpej 			howto |= RB_POWERDOWN;
1065f9840e9Sthorpej 			break;
107ce5ebe99Scgd 		case 'q':
108ce5ebe99Scgd 			qflag = 1;
109ce5ebe99Scgd 			break;
11000ef49e9Sdyoung 		case 'v':
11100ef49e9Sdyoung 			howto |= AB_VERBOSE;
11200ef49e9Sdyoung 			break;
11300ef49e9Sdyoung 		case 'x':
11400ef49e9Sdyoung 			howto |= AB_DEBUG;
11500ef49e9Sdyoung 			break;
11600ef49e9Sdyoung 		case 'z':
11700ef49e9Sdyoung 			howto |= AB_SILENT;
11800ef49e9Sdyoung 			break;
119ce5ebe99Scgd 		case '?':
120ce5ebe99Scgd 		default:
121ce5ebe99Scgd 			usage();
12261f28255Scgd 		}
123ce5ebe99Scgd 	argc -= optind;
124ce5ebe99Scgd 	argv += optind;
125ce5ebe99Scgd 
12674693079Smrg 	if (argc) {
12774693079Smrg 		for (av = argv, len = 0; *av; av++)
128970a5291Smrg 			len += strlen(*av) + 1;
12974693079Smrg 		bootstr = malloc(len + 1);
130970a5291Smrg 		*bootstr = '\0';		/* for first strcat */
131970a5291Smrg 		for (av = argv; *av; av++) {
132970a5291Smrg 			strcat(bootstr, *av);
133970a5291Smrg 			strcat(bootstr, " ");
134970a5291Smrg 		}
1354b541a44Stsutsui 		bootstr[len - 1] = '\0';	/* to kill last space */
136a9f27985Smrg 		howto |= RB_STRING;
13774693079Smrg 	} else
13874693079Smrg 		bootstr = NULL;
13974693079Smrg 
140ce5ebe99Scgd 	if (geteuid())
1416b1fa5abSperry 		errx(1, "%s", strerror(EPERM));
142ce5ebe99Scgd 
143ce5ebe99Scgd 	if (qflag) {
14474693079Smrg 		reboot(howto, bootstr);
1456b1fa5abSperry 		err(1, "reboot");
14661f28255Scgd 	}
14761f28255Scgd 
148ce5ebe99Scgd 	/* Log the reboot. */
149ce5ebe99Scgd 	if (!lflag)  {
150ce5ebe99Scgd 		if ((user = getlogin()) == NULL)
151ce5ebe99Scgd 			user = (pw = getpwuid(getuid())) ?
152ce5ebe99Scgd 			    pw->pw_name : "???";
153ce5ebe99Scgd 		if (dohalt) {
154f022cce9Slukem 			openlog("halt", LOG_CONS, LOG_AUTH);
155ce5ebe99Scgd 			syslog(LOG_CRIT, "halted by %s", user);
156416a0bcaShubertf 		} else if (dopoweroff) {
157f022cce9Slukem 			openlog("poweroff", LOG_CONS, LOG_AUTH);
158416a0bcaShubertf 			syslog(LOG_CRIT, "powered off by %s", user);
159ce5ebe99Scgd 		} else {
160f022cce9Slukem 			openlog("reboot", LOG_CONS, LOG_AUTH);
161ed46fe3aSmrg 			if (bootstr)
162ed46fe3aSmrg 				syslog(LOG_CRIT, "rebooted by %s: %s", user,
163ed46fe3aSmrg 				    bootstr);
164ed46fe3aSmrg 			else
165ed46fe3aSmrg 				syslog(LOG_CRIT, "rebooted by %s", user);
16661f28255Scgd 		}
16761f28255Scgd 	}
16842ec3bd1Swiz #ifdef SUPPORT_UTMP
169ce5ebe99Scgd 	logwtmp("~", "shutdown", "");
17042ec3bd1Swiz #endif
17142ec3bd1Swiz #ifdef SUPPORT_UTMPX
172*85f97b7aSroy 	logwtmpx("~", "shutdown", "", 0, INIT_PROCESS);
17342ec3bd1Swiz #endif
17461f28255Scgd 
175ce5ebe99Scgd 	/*
176ce5ebe99Scgd 	 * Do a sync early on, so disks start transfers while we're off
177ce5ebe99Scgd 	 * killing processes.  Don't worry about writes done before the
178ce5ebe99Scgd 	 * processes die, the reboot system call syncs the disks.
179ce5ebe99Scgd 	 */
180ce5ebe99Scgd 	if (!nflag)
181ce5ebe99Scgd 		sync();
182ce5ebe99Scgd 
18391b9544eSaugustss 	/*
18491b9544eSaugustss 	 * Ignore signals that we can get as a result of killing
18591b9544eSaugustss 	 * parents, group leaders, etc.
18691b9544eSaugustss 	 */
187ce5ebe99Scgd 	(void)signal(SIGHUP,  SIG_IGN);
18891b9544eSaugustss 	(void)signal(SIGINT,  SIG_IGN);
18991b9544eSaugustss 	(void)signal(SIGQUIT, SIG_IGN);
19091b9544eSaugustss 	(void)signal(SIGTERM, SIG_IGN);
191cf423b39Sperry 	(void)signal(SIGTSTP, SIG_IGN);
192ce5ebe99Scgd 
193cf423b39Sperry 	/*
194cf423b39Sperry 	 * If we're running in a pipeline, we don't want to die
195cf423b39Sperry 	 * after killing whatever we're writing to.
196cf423b39Sperry 	 */
197a519c154Sjdolecek 	(void)signal(SIGPIPE, SIG_IGN);
198a519c154Sjdolecek 
199cf423b39Sperry 	/* Just stop init -- if we fail, we'll restart it. */
200cf423b39Sperry 	if (kill(1, SIGTSTP) == -1)
201cf423b39Sperry 		err(1, "SIGTSTP init");
202cf423b39Sperry 
203ce5ebe99Scgd 	/* Send a SIGTERM first, a chance to save the buffers. */
20428753bf1Sperry 	if (kill(-1, SIGTERM) == -1) {
20528753bf1Sperry 		/*
20628753bf1Sperry 		 * If ESRCH, everything's OK: we're the only non-system
20728753bf1Sperry 		 * process!  That can happen e.g. via 'exec reboot' in
20828753bf1Sperry 		 * single-user mode.
20928753bf1Sperry 		 */
21028753bf1Sperry 		if (errno != ESRCH) {
211317c5696Sdholland 			warn("SIGTERM all processes");
21228753bf1Sperry 			goto restart;
21328753bf1Sperry 		}
21428753bf1Sperry 	}
215ce5ebe99Scgd 
216ce5ebe99Scgd 	/*
217ce5ebe99Scgd 	 * After the processes receive the signal, start the rest of the
2183523dc9cSis 	 * buffers on their way.  Wait 5 seconds between the SIGTERM and
2193523dc9cSis 	 * the SIGKILL to pretend to give everybody a chance.
220ce5ebe99Scgd 	 */
221ce5ebe99Scgd 	sleep(2);
222ce5ebe99Scgd 	if (!nflag)
223ce5ebe99Scgd 		sync();
2243523dc9cSis 	sleep(3);
225ce5ebe99Scgd 
226ce5ebe99Scgd 	for (i = 1;; ++i) {
22761f28255Scgd 		if (kill(-1, SIGKILL) == -1) {
22861f28255Scgd 			if (errno == ESRCH)
22961f28255Scgd 				break;
230317c5696Sdholland 			warn("SIGKILL all processes");
231ce5ebe99Scgd 			goto restart;
23261f28255Scgd 		}
23361f28255Scgd 		if (i > 5) {
234689db8fcSmycroft 			warnx("WARNING: some process(es) wouldn't die");
23561f28255Scgd 			break;
23661f28255Scgd 		}
237ce5ebe99Scgd 		(void)sleep(2 * i);
23861f28255Scgd 	}
23961f28255Scgd 
24074693079Smrg 	reboot(howto, bootstr);
241317c5696Sdholland 	warn("reboot()");
242ce5ebe99Scgd 	/* FALLTHROUGH */
243ce5ebe99Scgd 
244ce5ebe99Scgd restart:
245ce5ebe99Scgd 	sverrno = errno;
2466b1fa5abSperry 	errx(1, "%s%s", kill(1, SIGHUP) == -1 ? "(can't restart init): " : "",
247ce5ebe99Scgd 	    strerror(sverrno));
248ce5ebe99Scgd 	/* NOTREACHED */
24961f28255Scgd }
250ce5ebe99Scgd 
251eccb2789Sjoerg static void
usage(void)252d9deaad6Swiz usage(void)
253ce5ebe99Scgd {
2545f9840e9Sthorpej 	const char *pflag = dohalt ? "p" : "";
2555f9840e9Sthorpej 
25632cebeb7Swiz 	(void)fprintf(stderr, "usage: %s [-dln%sqvxz] [-- <boot string>]\n",
2578a986b2eScgd 	    getprogname(), pflag);
25861f28255Scgd 	exit(1);
25961f28255Scgd }
260