xref: /freebsd/sbin/reboot/reboot.c (revision 6ab0d6c22064438f76e1a7aba89bdaed6ec848cc)
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  * 3. All advertising materials mentioning features or use of this software
148fae3551SRodney W. Grimes  *    must display the following acknowledgement:
158fae3551SRodney W. Grimes  *	This product includes software developed by the University of
168fae3551SRodney W. Grimes  *	California, Berkeley and its contributors.
178fae3551SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
188fae3551SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
198fae3551SRodney W. Grimes  *    without specific prior written permission.
208fae3551SRodney W. Grimes  *
218fae3551SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
228fae3551SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
238fae3551SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
248fae3551SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
258fae3551SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
268fae3551SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
278fae3551SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
288fae3551SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
298fae3551SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
308fae3551SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
318fae3551SRodney W. Grimes  * SUCH DAMAGE.
328fae3551SRodney W. Grimes  */
338fae3551SRodney W. Grimes 
348fae3551SRodney W. Grimes #ifndef lint
359448def9SPhilippe Charnier static const char copyright[] =
368fae3551SRodney W. Grimes "@(#) Copyright (c) 1980, 1986, 1993\n\
378fae3551SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
388fae3551SRodney W. Grimes #endif /* not lint */
398fae3551SRodney W. Grimes 
408fae3551SRodney W. Grimes #ifndef lint
419448def9SPhilippe Charnier #if 0
428fae3551SRodney W. Grimes static char sccsid[] = "@(#)reboot.c	8.1 (Berkeley) 6/5/93";
439448def9SPhilippe Charnier #endif
449448def9SPhilippe Charnier static const char rcsid[] =
457f3dea24SPeter Wemm   "$FreeBSD$";
468fae3551SRodney W. Grimes #endif /* not lint */
478fae3551SRodney W. Grimes 
488fae3551SRodney W. Grimes #include <sys/reboot.h>
499448def9SPhilippe Charnier #include <sys/types.h>
5085ae580cSIan Dowse #include <sys/sysctl.h>
518fae3551SRodney W. Grimes #include <signal.h>
52098166edSPhilippe Charnier #include <err.h>
539448def9SPhilippe Charnier #include <errno.h>
541de372dcSWes Peters #include <fcntl.h>
559448def9SPhilippe Charnier #include <libutil.h>
569448def9SPhilippe Charnier #include <pwd.h>
579448def9SPhilippe Charnier #include <syslog.h>
589448def9SPhilippe Charnier #include <stdio.h>
59cafefe8cSDima Dorfman #include <stdlib.h>
609448def9SPhilippe Charnier #include <string.h>
619448def9SPhilippe Charnier #include <unistd.h>
628fae3551SRodney W. Grimes 
6385ae580cSIan Dowse void usage(void);
6485ae580cSIan Dowse u_int get_pageins(void);
658fae3551SRodney W. Grimes 
668fae3551SRodney W. Grimes int dohalt;
678fae3551SRodney W. Grimes 
688fae3551SRodney W. Grimes int
6985ae580cSIan Dowse main(int argc, char *argv[])
708fae3551SRodney W. Grimes {
718fae3551SRodney W. Grimes 	struct passwd *pw;
721de372dcSWes Peters 	int ch, howto, i, fd, kflag, lflag, nflag, qflag, pflag, sverrno;
7385ae580cSIan Dowse 	u_int pageins;
741de372dcSWes Peters 	char *kernel, *p;
758b032500SDima Dorfman 	const char *user;
768fae3551SRodney W. Grimes 
777f4b19d6SDavid Greenman 	if (strstr((p = rindex(*argv, '/')) ? p + 1 : *argv, "halt")) {
788fae3551SRodney W. Grimes 		dohalt = 1;
798fae3551SRodney W. Grimes 		howto = RB_HALT;
808fae3551SRodney W. Grimes 	} else
818fae3551SRodney W. Grimes 		howto = 0;
821de372dcSWes Peters 	kflag = lflag = nflag = qflag = 0;
831de372dcSWes Peters 	while ((ch = getopt(argc, argv, "dk:lnpq")) != -1)
848fae3551SRodney W. Grimes 		switch(ch) {
8585c981ccSJohn Polstra 		case 'd':
8685c981ccSJohn Polstra 			howto |= RB_DUMP;
8785c981ccSJohn Polstra 			break;
881de372dcSWes Peters 		case 'k':
891de372dcSWes Peters 			kflag = 1;
901de372dcSWes Peters 			kernel = optarg;
911de372dcSWes Peters 			break;
926714dac4SNik Clayton 		case 'l':
938fae3551SRodney W. Grimes 			lflag = 1;
948fae3551SRodney W. Grimes 			break;
958fae3551SRodney W. Grimes 		case 'n':
968fae3551SRodney W. Grimes 			nflag = 1;
978fae3551SRodney W. Grimes 			howto |= RB_NOSYNC;
988fae3551SRodney W. Grimes 			break;
9947be7466SJulian Elischer 		case 'p':
10047be7466SJulian Elischer 			pflag = 1;
101cfde77fbSThomas Quinot 			howto |= RB_POWEROFF;
10247be7466SJulian Elischer 			break;
1038fae3551SRodney W. Grimes 		case 'q':
1048fae3551SRodney W. Grimes 			qflag = 1;
1058fae3551SRodney W. Grimes 			break;
1068fae3551SRodney W. Grimes 		case '?':
1078fae3551SRodney W. Grimes 		default:
1088fae3551SRodney W. Grimes 			usage();
1098fae3551SRodney W. Grimes 		}
1108fae3551SRodney W. Grimes 	argc -= optind;
1118fae3551SRodney W. Grimes 	argv += optind;
1128fae3551SRodney W. Grimes 
11385c981ccSJohn Polstra 	if ((howto & (RB_DUMP | RB_HALT)) == (RB_DUMP | RB_HALT))
11485c981ccSJohn Polstra 		errx(1, "cannot dump (-d) when halting; must reboot instead");
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 
1251de372dcSWes Peters 	if (kflag) {
12683739172SWes Peters 		fd = open("/boot/nextboot.conf", O_WRONLY | O_CREAT, 0444);
1271de372dcSWes Peters 		if (fd > -1) {
128a72b2ac6SGordon Tetlow 			(void)write(fd, "nextboot_enable=\"YES\"\n", 22);
1291de372dcSWes Peters 			(void)write(fd, "kernel=\"", 8L);
1301de372dcSWes Peters 			(void)write(fd, kernel, strlen(kernel));
1311de372dcSWes Peters 			(void)write(fd, "\"\n", 2);
1321de372dcSWes Peters 			close(fd);
1331de372dcSWes Peters 		}
1341de372dcSWes Peters 	}
1351de372dcSWes Peters 
1368fae3551SRodney W. Grimes 	/* Log the reboot. */
1378fae3551SRodney W. Grimes 	if (!lflag)  {
1388fae3551SRodney W. Grimes 		if ((user = getlogin()) == NULL)
1398fae3551SRodney W. Grimes 			user = (pw = getpwuid(getuid())) ?
1408fae3551SRodney W. Grimes 			    pw->pw_name : "???";
1418fae3551SRodney W. Grimes 		if (dohalt) {
1428fae3551SRodney W. Grimes 			openlog("halt", 0, LOG_AUTH | LOG_CONS);
1438fae3551SRodney W. Grimes 			syslog(LOG_CRIT, "halted by %s", user);
1448fae3551SRodney W. Grimes 		} else {
1458fae3551SRodney W. Grimes 			openlog("reboot", 0, LOG_AUTH | LOG_CONS);
1468fae3551SRodney W. Grimes 			syslog(LOG_CRIT, "rebooted by %s", user);
1478fae3551SRodney W. Grimes 		}
1488fae3551SRodney W. Grimes 	}
1498fae3551SRodney W. Grimes 	logwtmp("~", "shutdown", "");
1508fae3551SRodney W. Grimes 
1518fae3551SRodney W. Grimes 	/*
1528fae3551SRodney W. Grimes 	 * Do a sync early on, so disks start transfers while we're off
1538fae3551SRodney W. Grimes 	 * killing processes.  Don't worry about writes done before the
1548fae3551SRodney W. Grimes 	 * processes die, the reboot system call syncs the disks.
1558fae3551SRodney W. Grimes 	 */
1568fae3551SRodney W. Grimes 	if (!nflag)
1578fae3551SRodney W. Grimes 		sync();
1588fae3551SRodney W. Grimes 
1598fae3551SRodney W. Grimes 	/* Just stop init -- if we fail, we'll restart it. */
1608fae3551SRodney W. Grimes 	if (kill(1, SIGTSTP) == -1)
161098166edSPhilippe Charnier 		err(1, "SIGTSTP init");
1628fae3551SRodney W. Grimes 
1638fae3551SRodney W. Grimes 	/* Ignore the SIGHUP we get when our parent shell dies. */
1648fae3551SRodney W. Grimes 	(void)signal(SIGHUP, SIG_IGN);
1658fae3551SRodney W. Grimes 
1668fae3551SRodney W. Grimes 	/* Send a SIGTERM first, a chance to save the buffers. */
1676ab0d6c2SRobert Watson 	if (kill(-1, SIGTERM) == -1 && errno != ESRCH)
168098166edSPhilippe Charnier 		err(1, "SIGTERM processes");
1698fae3551SRodney W. Grimes 
1708fae3551SRodney W. Grimes 	/*
1718fae3551SRodney W. Grimes 	 * After the processes receive the signal, start the rest of the
1728fae3551SRodney W. Grimes 	 * buffers on their way.  Wait 5 seconds between the SIGTERM and
17385ae580cSIan Dowse 	 * the SIGKILL to give everybody a chance. If there is a lot of
17485ae580cSIan Dowse 	 * paging activity then wait longer, up to a maximum of approx
17585ae580cSIan Dowse 	 * 60 seconds.
1768fae3551SRodney W. Grimes 	 */
1778fae3551SRodney W. Grimes 	sleep(2);
17885ae580cSIan Dowse 	for (i = 0; i < 20; i++) {
17985ae580cSIan Dowse 		pageins = get_pageins();
1808fae3551SRodney W. Grimes 		if (!nflag)
1818fae3551SRodney W. Grimes 			sync();
1828fae3551SRodney W. Grimes 		sleep(3);
18385ae580cSIan Dowse 		if (get_pageins() == pageins)
18485ae580cSIan Dowse 			break;
18585ae580cSIan Dowse 	}
1868fae3551SRodney W. Grimes 
1878fae3551SRodney W. Grimes 	for (i = 1;; ++i) {
1888fae3551SRodney W. Grimes 		if (kill(-1, SIGKILL) == -1) {
1898fae3551SRodney W. Grimes 			if (errno == ESRCH)
1908fae3551SRodney W. Grimes 				break;
1918fae3551SRodney W. Grimes 			goto restart;
1928fae3551SRodney W. Grimes 		}
1938fae3551SRodney W. Grimes 		if (i > 5) {
1948fae3551SRodney W. Grimes 			(void)fprintf(stderr,
1958fae3551SRodney W. Grimes 			    "WARNING: some process(es) wouldn't die\n");
1968fae3551SRodney W. Grimes 			break;
1978fae3551SRodney W. Grimes 		}
1988fae3551SRodney W. Grimes 		(void)sleep(2 * i);
1998fae3551SRodney W. Grimes 	}
2008fae3551SRodney W. Grimes 
2018fae3551SRodney W. Grimes 	reboot(howto);
2028fae3551SRodney W. Grimes 	/* FALLTHROUGH */
2038fae3551SRodney W. Grimes 
2048fae3551SRodney W. Grimes restart:
2058fae3551SRodney W. Grimes 	sverrno = errno;
206098166edSPhilippe Charnier 	errx(1, "%s%s", kill(1, SIGHUP) == -1 ? "(can't restart init): " : "",
2078fae3551SRodney W. Grimes 	    strerror(sverrno));
2088fae3551SRodney W. Grimes 	/* NOTREACHED */
2098fae3551SRodney W. Grimes }
2108fae3551SRodney W. Grimes 
2118fae3551SRodney W. Grimes void
2128fae3551SRodney W. Grimes usage()
2138fae3551SRodney W. Grimes {
2141de372dcSWes Peters 	(void)fprintf(stderr, "usage: %s [-dnpq] [-k kernel]\n",
21585c981ccSJohn Polstra 	    dohalt ? "halt" : "reboot");
2168fae3551SRodney W. Grimes 	exit(1);
2178fae3551SRodney W. Grimes }
21885ae580cSIan Dowse 
21985ae580cSIan Dowse u_int
22085ae580cSIan Dowse get_pageins()
22185ae580cSIan Dowse {
22285ae580cSIan Dowse 	u_int pageins;
22385ae580cSIan Dowse 	size_t len;
22485ae580cSIan Dowse 
22585ae580cSIan Dowse 	len = sizeof(pageins);
22685ae580cSIan Dowse 	if (sysctlbyname("vm.stats.vm.v_swappgsin", &pageins, &len, NULL, 0)
22785ae580cSIan Dowse 	    != 0) {
22885ae580cSIan Dowse 		warnx("v_swappgsin");
22985ae580cSIan Dowse 		return (0);
23085ae580cSIan Dowse 	}
23185ae580cSIan Dowse 	return pageins;
23285ae580cSIan Dowse }
233