xref: /freebsd/sbin/reboot/reboot.c (revision 3f5ac575ea1288891300312b2fb9590c4c1483c4)
18fae3551SRodney W. Grimes /*
28fae3551SRodney W. Grimes  * Copyright (c) 1980, 1986, 1993
38fae3551SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
48fae3551SRodney W. Grimes  *
58fae3551SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
68fae3551SRodney W. Grimes  * modification, are permitted provided that the following conditions
78fae3551SRodney W. Grimes  * are met:
88fae3551SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
98fae3551SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
108fae3551SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
118fae3551SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
128fae3551SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
138fae3551SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
148fae3551SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
158fae3551SRodney W. Grimes  *    without specific prior written permission.
168fae3551SRodney W. Grimes  *
178fae3551SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
188fae3551SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
198fae3551SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
208fae3551SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
218fae3551SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
228fae3551SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
238fae3551SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
248fae3551SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
258fae3551SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
268fae3551SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
278fae3551SRodney W. Grimes  * SUCH DAMAGE.
288fae3551SRodney W. Grimes  */
298fae3551SRodney W. Grimes 
30c69284caSDavid E. O'Brien #if 0
318fae3551SRodney W. Grimes #ifndef lint
329448def9SPhilippe Charnier static const char copyright[] =
338fae3551SRodney W. Grimes "@(#) Copyright (c) 1980, 1986, 1993\n\
348fae3551SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
358fae3551SRodney W. Grimes #endif /* not lint */
368fae3551SRodney W. Grimes 
378fae3551SRodney W. Grimes #ifndef lint
388fae3551SRodney W. Grimes static char sccsid[] = "@(#)reboot.c	8.1 (Berkeley) 6/5/93";
398fae3551SRodney W. Grimes #endif /* not lint */
40c69284caSDavid E. O'Brien #endif
41c69284caSDavid E. O'Brien #include <sys/cdefs.h>
42c69284caSDavid E. O'Brien __FBSDID("$FreeBSD$");
438fae3551SRodney W. Grimes 
448fae3551SRodney W. Grimes #include <sys/reboot.h>
4514c69f21SEd Schouten #include <sys/time.h>
469448def9SPhilippe Charnier #include <sys/types.h>
4785ae580cSIan Dowse #include <sys/sysctl.h>
488fae3551SRodney W. Grimes #include <signal.h>
49098166edSPhilippe Charnier #include <err.h>
509448def9SPhilippe Charnier #include <errno.h>
511de372dcSWes Peters #include <fcntl.h>
529448def9SPhilippe Charnier #include <pwd.h>
539448def9SPhilippe Charnier #include <syslog.h>
549448def9SPhilippe Charnier #include <stdio.h>
55cafefe8cSDima Dorfman #include <stdlib.h>
569448def9SPhilippe Charnier #include <string.h>
579448def9SPhilippe Charnier #include <unistd.h>
58bf8959b0SEd Schouten #include <utmpx.h>
598fae3551SRodney W. Grimes 
6036737a0bSXin LI static void usage(void);
6136737a0bSXin LI static u_int get_pageins(void);
628fae3551SRodney W. Grimes 
63e802a0b3SEd Schouten static int dohalt;
648fae3551SRodney W. Grimes 
658fae3551SRodney W. Grimes int
6685ae580cSIan Dowse main(int argc, char *argv[])
678fae3551SRodney W. Grimes {
6814c69f21SEd Schouten 	struct utmpx utx;
6936737a0bSXin LI 	const struct passwd *pw;
70c09a6b1aSBjoern A. Zeeb 	int ch, howto, i, fd, lflag, nflag, qflag, sverrno;
7185ae580cSIan Dowse 	u_int pageins;
72e802a0b3SEd Schouten 	const char *user, *kernel = NULL;
738fae3551SRodney W. Grimes 
74e802a0b3SEd Schouten 	if (strcmp(getprogname(), "halt") == 0) {
758fae3551SRodney W. Grimes 		dohalt = 1;
768fae3551SRodney W. Grimes 		howto = RB_HALT;
778fae3551SRodney W. Grimes 	} else
788fae3551SRodney W. Grimes 		howto = 0;
79faa0ecddSXin LI 	lflag = nflag = qflag = 0;
80*3f5ac575SEdward Tomasz Napierala 	while ((ch = getopt(argc, argv, "dk:lnpqr")) != -1)
818fae3551SRodney W. Grimes 		switch(ch) {
8285c981ccSJohn Polstra 		case 'd':
8385c981ccSJohn Polstra 			howto |= RB_DUMP;
8485c981ccSJohn Polstra 			break;
851de372dcSWes Peters 		case 'k':
861de372dcSWes Peters 			kernel = optarg;
871de372dcSWes Peters 			break;
886714dac4SNik Clayton 		case 'l':
898fae3551SRodney W. Grimes 			lflag = 1;
908fae3551SRodney W. Grimes 			break;
918fae3551SRodney W. Grimes 		case 'n':
928fae3551SRodney W. Grimes 			nflag = 1;
938fae3551SRodney W. Grimes 			howto |= RB_NOSYNC;
948fae3551SRodney W. Grimes 			break;
9547be7466SJulian Elischer 		case 'p':
96cfde77fbSThomas Quinot 			howto |= RB_POWEROFF;
9747be7466SJulian Elischer 			break;
988fae3551SRodney W. Grimes 		case 'q':
998fae3551SRodney W. Grimes 			qflag = 1;
1008fae3551SRodney W. Grimes 			break;
101*3f5ac575SEdward Tomasz Napierala 		case 'r':
102*3f5ac575SEdward Tomasz Napierala 			howto |= RB_REROOT;
103*3f5ac575SEdward Tomasz Napierala 			break;
1048fae3551SRodney W. Grimes 		case '?':
1058fae3551SRodney W. Grimes 		default:
1068fae3551SRodney W. Grimes 			usage();
1078fae3551SRodney W. Grimes 		}
1088fae3551SRodney W. Grimes 	argc -= optind;
1098fae3551SRodney W. Grimes 	argv += optind;
1108fae3551SRodney W. Grimes 
11185c981ccSJohn Polstra 	if ((howto & (RB_DUMP | RB_HALT)) == (RB_DUMP | RB_HALT))
11285c981ccSJohn Polstra 		errx(1, "cannot dump (-d) when halting; must reboot instead");
113*3f5ac575SEdward Tomasz Napierala 	if ((howto & RB_REROOT) != 0 && howto != RB_REROOT)
114*3f5ac575SEdward Tomasz Napierala 		errx(1, "-r cannot be used with -d, -n, or -p");
115098166edSPhilippe Charnier 	if (geteuid()) {
116098166edSPhilippe Charnier 		errno = EPERM;
117098166edSPhilippe Charnier 		err(1, NULL);
118098166edSPhilippe Charnier 	}
1198fae3551SRodney W. Grimes 
1208fae3551SRodney W. Grimes 	if (qflag) {
1218fae3551SRodney W. Grimes 		reboot(howto);
122098166edSPhilippe Charnier 		err(1, NULL);
1238fae3551SRodney W. Grimes 	}
1248fae3551SRodney W. Grimes 
125faa0ecddSXin LI 	if (kernel != NULL) {
1263edf7a78SPawel Jakub Dawidek 		fd = open("/boot/nextboot.conf", O_WRONLY | O_CREAT | O_TRUNC,
1273edf7a78SPawel Jakub Dawidek 		    0444);
1281de372dcSWes Peters 		if (fd > -1) {
129a72b2ac6SGordon Tetlow 			(void)write(fd, "nextboot_enable=\"YES\"\n", 22);
1301de372dcSWes Peters 			(void)write(fd, "kernel=\"", 8L);
1311de372dcSWes Peters 			(void)write(fd, kernel, strlen(kernel));
1321de372dcSWes Peters 			(void)write(fd, "\"\n", 2);
1331de372dcSWes Peters 			close(fd);
1341de372dcSWes Peters 		}
1351de372dcSWes Peters 	}
1361de372dcSWes Peters 
1378fae3551SRodney W. Grimes 	/* Log the reboot. */
1388fae3551SRodney W. Grimes 	if (!lflag)  {
1398fae3551SRodney W. Grimes 		if ((user = getlogin()) == NULL)
1408fae3551SRodney W. Grimes 			user = (pw = getpwuid(getuid())) ?
1418fae3551SRodney W. Grimes 			    pw->pw_name : "???";
1428fae3551SRodney W. Grimes 		if (dohalt) {
1438fae3551SRodney W. Grimes 			openlog("halt", 0, LOG_AUTH | LOG_CONS);
1448fae3551SRodney W. Grimes 			syslog(LOG_CRIT, "halted by %s", user);
145*3f5ac575SEdward Tomasz Napierala 		} else if (howto & RB_REROOT) {
146*3f5ac575SEdward Tomasz Napierala 			openlog("reroot", 0, LOG_AUTH | LOG_CONS);
147*3f5ac575SEdward Tomasz Napierala 			syslog(LOG_CRIT, "rerooted by %s", user);
1488fae3551SRodney W. Grimes 		} else {
1498fae3551SRodney W. Grimes 			openlog("reboot", 0, LOG_AUTH | LOG_CONS);
1508fae3551SRodney W. Grimes 			syslog(LOG_CRIT, "rebooted by %s", user);
1518fae3551SRodney W. Grimes 		}
1528fae3551SRodney W. Grimes 	}
15314c69f21SEd Schouten 	utx.ut_type = SHUTDOWN_TIME;
15414c69f21SEd Schouten 	gettimeofday(&utx.ut_tv, NULL);
15514c69f21SEd Schouten 	pututxline(&utx);
1568fae3551SRodney W. Grimes 
1578fae3551SRodney W. Grimes 	/*
1588fae3551SRodney W. Grimes 	 * Do a sync early on, so disks start transfers while we're off
1598fae3551SRodney W. Grimes 	 * killing processes.  Don't worry about writes done before the
1608fae3551SRodney W. Grimes 	 * processes die, the reboot system call syncs the disks.
1618fae3551SRodney W. Grimes 	 */
1628fae3551SRodney W. Grimes 	if (!nflag)
1638fae3551SRodney W. Grimes 		sync();
1648fae3551SRodney W. Grimes 
165d8f70938SBruce M Simpson 	/*
166d8f70938SBruce M Simpson 	 * Ignore signals that we can get as a result of killing
167d8f70938SBruce M Simpson 	 * parents, group leaders, etc.
168d8f70938SBruce M Simpson 	 */
169b08d1553SBruce M Simpson 	(void)signal(SIGHUP,  SIG_IGN);
170d8f70938SBruce M Simpson 	(void)signal(SIGINT,  SIG_IGN);
171d8f70938SBruce M Simpson 	(void)signal(SIGQUIT, SIG_IGN);
172d8f70938SBruce M Simpson 	(void)signal(SIGTERM, SIG_IGN);
173d8f70938SBruce M Simpson 	(void)signal(SIGTSTP, SIG_IGN);
174d8f70938SBruce M Simpson 
175d8f70938SBruce M Simpson 	/*
176d8f70938SBruce M Simpson 	 * If we're running in a pipeline, we don't want to die
177d8f70938SBruce M Simpson 	 * after killing whatever we're writing to.
178d8f70938SBruce M Simpson 	 */
179d8f70938SBruce M Simpson 	(void)signal(SIGPIPE, SIG_IGN);
180b08d1553SBruce M Simpson 
181*3f5ac575SEdward Tomasz Napierala 	/*
182*3f5ac575SEdward Tomasz Napierala 	 * Only init(8) can perform rerooting.
183*3f5ac575SEdward Tomasz Napierala 	 */
184*3f5ac575SEdward Tomasz Napierala 	if (howto & RB_REROOT) {
185*3f5ac575SEdward Tomasz Napierala 		if (kill(1, SIGEMT) == -1)
186*3f5ac575SEdward Tomasz Napierala 			err(1, "SIGEMT init");
187*3f5ac575SEdward Tomasz Napierala 
188*3f5ac575SEdward Tomasz Napierala 		return (0);
189*3f5ac575SEdward Tomasz Napierala 	}
190*3f5ac575SEdward Tomasz Napierala 
1918fae3551SRodney W. Grimes 	/* Just stop init -- if we fail, we'll restart it. */
1928fae3551SRodney W. Grimes 	if (kill(1, SIGTSTP) == -1)
193098166edSPhilippe Charnier 		err(1, "SIGTSTP init");
1948fae3551SRodney W. Grimes 
1958fae3551SRodney W. Grimes 	/* Send a SIGTERM first, a chance to save the buffers. */
1966ab0d6c2SRobert Watson 	if (kill(-1, SIGTERM) == -1 && errno != ESRCH)
197098166edSPhilippe Charnier 		err(1, "SIGTERM processes");
1988fae3551SRodney W. Grimes 
1998fae3551SRodney W. Grimes 	/*
2008fae3551SRodney W. Grimes 	 * After the processes receive the signal, start the rest of the
2018fae3551SRodney W. Grimes 	 * buffers on their way.  Wait 5 seconds between the SIGTERM and
20285ae580cSIan Dowse 	 * the SIGKILL to give everybody a chance. If there is a lot of
20385ae580cSIan Dowse 	 * paging activity then wait longer, up to a maximum of approx
20485ae580cSIan Dowse 	 * 60 seconds.
2058fae3551SRodney W. Grimes 	 */
2068fae3551SRodney W. Grimes 	sleep(2);
20785ae580cSIan Dowse 	for (i = 0; i < 20; i++) {
20885ae580cSIan Dowse 		pageins = get_pageins();
2098fae3551SRodney W. Grimes 		if (!nflag)
2108fae3551SRodney W. Grimes 			sync();
2118fae3551SRodney W. Grimes 		sleep(3);
21285ae580cSIan Dowse 		if (get_pageins() == pageins)
21385ae580cSIan Dowse 			break;
21485ae580cSIan Dowse 	}
2158fae3551SRodney W. Grimes 
2168fae3551SRodney W. Grimes 	for (i = 1;; ++i) {
2178fae3551SRodney W. Grimes 		if (kill(-1, SIGKILL) == -1) {
2188fae3551SRodney W. Grimes 			if (errno == ESRCH)
2198fae3551SRodney W. Grimes 				break;
2208fae3551SRodney W. Grimes 			goto restart;
2218fae3551SRodney W. Grimes 		}
2228fae3551SRodney W. Grimes 		if (i > 5) {
2238fae3551SRodney W. Grimes 			(void)fprintf(stderr,
2248fae3551SRodney W. Grimes 			    "WARNING: some process(es) wouldn't die\n");
2258fae3551SRodney W. Grimes 			break;
2268fae3551SRodney W. Grimes 		}
2278fae3551SRodney W. Grimes 		(void)sleep(2 * i);
2288fae3551SRodney W. Grimes 	}
2298fae3551SRodney W. Grimes 
2308fae3551SRodney W. Grimes 	reboot(howto);
2318fae3551SRodney W. Grimes 	/* FALLTHROUGH */
2328fae3551SRodney W. Grimes 
2338fae3551SRodney W. Grimes restart:
2348fae3551SRodney W. Grimes 	sverrno = errno;
235098166edSPhilippe Charnier 	errx(1, "%s%s", kill(1, SIGHUP) == -1 ? "(can't restart init): " : "",
2368fae3551SRodney W. Grimes 	    strerror(sverrno));
2378fae3551SRodney W. Grimes 	/* NOTREACHED */
2388fae3551SRodney W. Grimes }
2398fae3551SRodney W. Grimes 
24036737a0bSXin LI static void
241c574558fSDag-Erling Smørgrav usage(void)
2428fae3551SRodney W. Grimes {
243c574558fSDag-Erling Smørgrav 
244c574558fSDag-Erling Smørgrav 	(void)fprintf(stderr, dohalt ?
245c574558fSDag-Erling Smørgrav 	    "usage: halt [-lnpq] [-k kernel]\n" :
246c574558fSDag-Erling Smørgrav 	    "usage: reboot [-dlnpq] [-k kernel]\n");
2478fae3551SRodney W. Grimes 	exit(1);
2488fae3551SRodney W. Grimes }
24985ae580cSIan Dowse 
25036737a0bSXin LI static u_int
25188a8f792SEd Schouten get_pageins(void)
25285ae580cSIan Dowse {
25385ae580cSIan Dowse 	u_int pageins;
25485ae580cSIan Dowse 	size_t len;
25585ae580cSIan Dowse 
25685ae580cSIan Dowse 	len = sizeof(pageins);
25785ae580cSIan Dowse 	if (sysctlbyname("vm.stats.vm.v_swappgsin", &pageins, &len, NULL, 0)
25885ae580cSIan Dowse 	    != 0) {
25985ae580cSIan Dowse 		warnx("v_swappgsin");
26085ae580cSIan Dowse 		return (0);
26185ae580cSIan Dowse 	}
26285ae580cSIan Dowse 	return pageins;
26385ae580cSIan Dowse }
264