reboot.c revision 95485
11558Srgrimes/*
21558Srgrimes * Copyright (c) 1980, 1986, 1993
31558Srgrimes *	The Regents of the University of California.  All rights reserved.
41558Srgrimes *
51558Srgrimes * Redistribution and use in source and binary forms, with or without
61558Srgrimes * modification, are permitted provided that the following conditions
71558Srgrimes * are met:
81558Srgrimes * 1. Redistributions of source code must retain the above copyright
91558Srgrimes *    notice, this list of conditions and the following disclaimer.
101558Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111558Srgrimes *    notice, this list of conditions and the following disclaimer in the
121558Srgrimes *    documentation and/or other materials provided with the distribution.
131558Srgrimes * 3. All advertising materials mentioning features or use of this software
141558Srgrimes *    must display the following acknowledgement:
151558Srgrimes *	This product includes software developed by the University of
161558Srgrimes *	California, Berkeley and its contributors.
171558Srgrimes * 4. Neither the name of the University nor the names of its contributors
181558Srgrimes *    may be used to endorse or promote products derived from this software
191558Srgrimes *    without specific prior written permission.
201558Srgrimes *
211558Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221558Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231558Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241558Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251558Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261558Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271558Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281558Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291558Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301558Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311558Srgrimes * SUCH DAMAGE.
321558Srgrimes */
331558Srgrimes
341558Srgrimes#ifndef lint
3537674Scharnierstatic const char copyright[] =
361558Srgrimes"@(#) Copyright (c) 1980, 1986, 1993\n\
371558Srgrimes	The Regents of the University of California.  All rights reserved.\n";
381558Srgrimes#endif /* not lint */
391558Srgrimes
401558Srgrimes#ifndef lint
4137674Scharnier#if 0
421558Srgrimesstatic char sccsid[] = "@(#)reboot.c	8.1 (Berkeley) 6/5/93";
4337674Scharnier#endif
4437674Scharnierstatic const char rcsid[] =
4550476Speter  "$FreeBSD: head/sbin/reboot/reboot.c 95485 2002-04-26 07:31:04Z wes $";
461558Srgrimes#endif /* not lint */
471558Srgrimes
481558Srgrimes#include <sys/reboot.h>
4937674Scharnier#include <sys/types.h>
5074525Siedowse#include <sys/sysctl.h>
511558Srgrimes#include <signal.h>
5237674Scharnier#include <err.h>
5337674Scharnier#include <errno.h>
5495485Swes#include <fcntl.h>
5537674Scharnier#include <libutil.h>
561558Srgrimes#include <pwd.h>
571558Srgrimes#include <syslog.h>
581558Srgrimes#include <stdio.h>
5978732Sdd#include <stdlib.h>
601558Srgrimes#include <string.h>
6137674Scharnier#include <unistd.h>
621558Srgrimes
6374525Siedowsevoid usage(void);
6474525Siedowseu_int get_pageins(void);
651558Srgrimes
661558Srgrimesint dohalt;
671558Srgrimes
681558Srgrimesint
6974525Siedowsemain(int argc, char *argv[])
701558Srgrimes{
711558Srgrimes	struct passwd *pw;
7295485Swes	int ch, howto, i, fd, kflag, lflag, nflag, qflag, pflag, sverrno;
7374525Siedowse	u_int pageins;
7495485Swes	char *kernel, *p;
7579748Sdd	const char *user;
761558Srgrimes
772171Sdg	if (strstr((p = rindex(*argv, '/')) ? p + 1 : *argv, "halt")) {
781558Srgrimes		dohalt = 1;
791558Srgrimes		howto = RB_HALT;
801558Srgrimes	} else
811558Srgrimes		howto = 0;
8295485Swes	kflag = lflag = nflag = qflag = 0;
8395485Swes	while ((ch = getopt(argc, argv, "dk:lnpq")) != -1)
841558Srgrimes		switch(ch) {
8553523Sjdp		case 'd':
8653523Sjdp			howto |= RB_DUMP;
8753523Sjdp			break;
8895485Swes		case 'k':
8995485Swes			kflag = 1;
9095485Swes			kernel = optarg;
9195485Swes			break;
9277122Snik		case 'l':
931558Srgrimes			lflag = 1;
941558Srgrimes			break;
951558Srgrimes		case 'n':
961558Srgrimes			nflag = 1;
971558Srgrimes			howto |= RB_NOSYNC;
981558Srgrimes			break;
9917801Sjulian		case 'p':
10017801Sjulian			pflag = 1;
10117801Sjulian			howto |= (RB_POWEROFF | RB_HALT);
10217801Sjulian			break;
1031558Srgrimes		case 'q':
1041558Srgrimes			qflag = 1;
1051558Srgrimes			break;
1061558Srgrimes		case '?':
1071558Srgrimes		default:
1081558Srgrimes			usage();
1091558Srgrimes		}
1101558Srgrimes	argc -= optind;
1111558Srgrimes	argv += optind;
1121558Srgrimes
11353523Sjdp	if ((howto & (RB_DUMP | RB_HALT)) == (RB_DUMP | RB_HALT))
11453523Sjdp		errx(1, "cannot dump (-d) when halting; must reboot instead");
11526676Scharnier	if (geteuid()) {
11626676Scharnier		errno = EPERM;
11726676Scharnier		err(1, NULL);
11826676Scharnier	}
1191558Srgrimes
1201558Srgrimes	if (qflag) {
1211558Srgrimes		reboot(howto);
12226676Scharnier		err(1, NULL);
1231558Srgrimes	}
1241558Srgrimes
12595485Swes	if (kflag) {
12695485Swes		fd = open("/boot/nextkernel", O_WRONLY | O_CREAT, 0444);
12795485Swes		if (fd > -1) {
12895485Swes			(void)write(fd, "kernel=\"", 8L);
12995485Swes			(void)write(fd, kernel, strlen(kernel));
13095485Swes			(void)write(fd, "\"\n", 2);
13195485Swes			close(fd);
13295485Swes		}
13395485Swes	}
13495485Swes
1351558Srgrimes	/* Log the reboot. */
1361558Srgrimes	if (!lflag)  {
1371558Srgrimes		if ((user = getlogin()) == NULL)
1381558Srgrimes			user = (pw = getpwuid(getuid())) ?
1391558Srgrimes			    pw->pw_name : "???";
1401558Srgrimes		if (dohalt) {
1411558Srgrimes			openlog("halt", 0, LOG_AUTH | LOG_CONS);
1421558Srgrimes			syslog(LOG_CRIT, "halted by %s", user);
1431558Srgrimes		} else {
1441558Srgrimes			openlog("reboot", 0, LOG_AUTH | LOG_CONS);
1451558Srgrimes			syslog(LOG_CRIT, "rebooted by %s", user);
1461558Srgrimes		}
1471558Srgrimes	}
1481558Srgrimes	logwtmp("~", "shutdown", "");
1491558Srgrimes
1501558Srgrimes	/*
1511558Srgrimes	 * Do a sync early on, so disks start transfers while we're off
1521558Srgrimes	 * killing processes.  Don't worry about writes done before the
1531558Srgrimes	 * processes die, the reboot system call syncs the disks.
1541558Srgrimes	 */
1551558Srgrimes	if (!nflag)
1561558Srgrimes		sync();
1571558Srgrimes
1581558Srgrimes	/* Just stop init -- if we fail, we'll restart it. */
1591558Srgrimes	if (kill(1, SIGTSTP) == -1)
16026676Scharnier		err(1, "SIGTSTP init");
1611558Srgrimes
1621558Srgrimes	/* Ignore the SIGHUP we get when our parent shell dies. */
1631558Srgrimes	(void)signal(SIGHUP, SIG_IGN);
1641558Srgrimes
1651558Srgrimes	/* Send a SIGTERM first, a chance to save the buffers. */
1661558Srgrimes	if (kill(-1, SIGTERM) == -1)
16726676Scharnier		err(1, "SIGTERM processes");
1681558Srgrimes
1691558Srgrimes	/*
1701558Srgrimes	 * After the processes receive the signal, start the rest of the
1711558Srgrimes	 * buffers on their way.  Wait 5 seconds between the SIGTERM and
17274525Siedowse	 * the SIGKILL to give everybody a chance. If there is a lot of
17374525Siedowse	 * paging activity then wait longer, up to a maximum of approx
17474525Siedowse	 * 60 seconds.
1751558Srgrimes	 */
1761558Srgrimes	sleep(2);
17774525Siedowse	for (i = 0; i < 20; i++) {
17874525Siedowse		pageins = get_pageins();
17974525Siedowse		if (!nflag)
18074525Siedowse			sync();
18174525Siedowse		sleep(3);
18274525Siedowse		if (get_pageins() == pageins)
18374525Siedowse			break;
18474525Siedowse	}
1851558Srgrimes
1861558Srgrimes	for (i = 1;; ++i) {
1871558Srgrimes		if (kill(-1, SIGKILL) == -1) {
1881558Srgrimes			if (errno == ESRCH)
1891558Srgrimes				break;
1901558Srgrimes			goto restart;
1911558Srgrimes		}
1921558Srgrimes		if (i > 5) {
1931558Srgrimes			(void)fprintf(stderr,
1941558Srgrimes			    "WARNING: some process(es) wouldn't die\n");
1951558Srgrimes			break;
1961558Srgrimes		}
1971558Srgrimes		(void)sleep(2 * i);
1981558Srgrimes	}
1991558Srgrimes
2001558Srgrimes	reboot(howto);
2011558Srgrimes	/* FALLTHROUGH */
2021558Srgrimes
2031558Srgrimesrestart:
2041558Srgrimes	sverrno = errno;
20526676Scharnier	errx(1, "%s%s", kill(1, SIGHUP) == -1 ? "(can't restart init): " : "",
2061558Srgrimes	    strerror(sverrno));
2071558Srgrimes	/* NOTREACHED */
2081558Srgrimes}
2091558Srgrimes
2101558Srgrimesvoid
2111558Srgrimesusage()
2121558Srgrimes{
21395485Swes	(void)fprintf(stderr, "usage: %s [-dnpq] [-k kernel]\n",
21453523Sjdp	    dohalt ? "halt" : "reboot");
2151558Srgrimes	exit(1);
2161558Srgrimes}
21774525Siedowse
21874525Siedowseu_int
21974525Siedowseget_pageins()
22074525Siedowse{
22174525Siedowse	u_int pageins;
22274525Siedowse	size_t len;
22374525Siedowse
22474525Siedowse	len = sizeof(pageins);
22574525Siedowse	if (sysctlbyname("vm.stats.vm.v_swappgsin", &pageins, &len, NULL, 0)
22674525Siedowse	    != 0) {
22774525Siedowse		warnx("v_swappgsin");
22874525Siedowse		return (0);
22974525Siedowse	}
23074525Siedowse	return pageins;
23174525Siedowse}
232