reboot.c revision 140794
1/*
2 * Copyright (c) 1980, 1986, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 *    may be used to endorse or promote products derived from this software
15 *    without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#if 0
31#ifndef lint
32static const char copyright[] =
33"@(#) Copyright (c) 1980, 1986, 1993\n\
34	The Regents of the University of California.  All rights reserved.\n";
35#endif /* not lint */
36
37#ifndef lint
38static char sccsid[] = "@(#)reboot.c	8.1 (Berkeley) 6/5/93";
39#endif /* not lint */
40#endif
41#include <sys/cdefs.h>
42__FBSDID("$FreeBSD: head/sbin/reboot/reboot.c 140794 2005-01-25 08:14:00Z delphij $");
43
44#include <sys/reboot.h>
45#include <sys/types.h>
46#include <sys/sysctl.h>
47#include <signal.h>
48#include <err.h>
49#include <errno.h>
50#include <fcntl.h>
51#include <libutil.h>
52#include <pwd.h>
53#include <syslog.h>
54#include <stdio.h>
55#include <stdlib.h>
56#include <string.h>
57#include <unistd.h>
58
59void usage(void);
60u_int get_pageins(void);
61
62int dohalt;
63
64int
65main(int argc, char *argv[])
66{
67	struct passwd *pw;
68	int ch, howto, i, fd, lflag, nflag, qflag, pflag, sverrno;
69	u_int pageins;
70	char *kernel = NULL, *p;
71	const char *user;
72
73	if (strstr((p = rindex(*argv, '/')) ? p + 1 : *argv, "halt")) {
74		dohalt = 1;
75		howto = RB_HALT;
76	} else
77		howto = 0;
78	lflag = nflag = qflag = 0;
79	while ((ch = getopt(argc, argv, "dk:lnpq")) != -1)
80		switch(ch) {
81		case 'd':
82			howto |= RB_DUMP;
83			break;
84		case 'k':
85			kernel = optarg;
86			break;
87		case 'l':
88			lflag = 1;
89			break;
90		case 'n':
91			nflag = 1;
92			howto |= RB_NOSYNC;
93			break;
94		case 'p':
95			pflag = 1;
96			howto |= RB_POWEROFF;
97			break;
98		case 'q':
99			qflag = 1;
100			break;
101		case '?':
102		default:
103			usage();
104		}
105	argc -= optind;
106	argv += optind;
107
108	if ((howto & (RB_DUMP | RB_HALT)) == (RB_DUMP | RB_HALT))
109		errx(1, "cannot dump (-d) when halting; must reboot instead");
110	if (geteuid()) {
111		errno = EPERM;
112		err(1, NULL);
113	}
114
115	if (qflag) {
116		reboot(howto);
117		err(1, NULL);
118	}
119
120	if (kernel != NULL) {
121		fd = open("/boot/nextboot.conf", O_WRONLY | O_CREAT, 0444);
122		if (fd > -1) {
123			(void)write(fd, "nextboot_enable=\"YES\"\n", 22);
124			(void)write(fd, "kernel=\"", 8L);
125			(void)write(fd, kernel, strlen(kernel));
126			(void)write(fd, "\"\n", 2);
127			close(fd);
128		}
129	}
130
131	/* Log the reboot. */
132	if (!lflag)  {
133		if ((user = getlogin()) == NULL)
134			user = (pw = getpwuid(getuid())) ?
135			    pw->pw_name : "???";
136		if (dohalt) {
137			openlog("halt", 0, LOG_AUTH | LOG_CONS);
138			syslog(LOG_CRIT, "halted by %s", user);
139		} else {
140			openlog("reboot", 0, LOG_AUTH | LOG_CONS);
141			syslog(LOG_CRIT, "rebooted by %s", user);
142		}
143	}
144	logwtmp("~", "shutdown", "");
145
146	/*
147	 * Do a sync early on, so disks start transfers while we're off
148	 * killing processes.  Don't worry about writes done before the
149	 * processes die, the reboot system call syncs the disks.
150	 */
151	if (!nflag)
152		sync();
153
154	/* Just stop init -- if we fail, we'll restart it. */
155	if (kill(1, SIGTSTP) == -1)
156		err(1, "SIGTSTP init");
157
158	/* Ignore the SIGHUP we get when our parent shell dies. */
159	(void)signal(SIGHUP, SIG_IGN);
160
161	/* Send a SIGTERM first, a chance to save the buffers. */
162	if (kill(-1, SIGTERM) == -1 && errno != ESRCH)
163		err(1, "SIGTERM processes");
164
165	/*
166	 * After the processes receive the signal, start the rest of the
167	 * buffers on their way.  Wait 5 seconds between the SIGTERM and
168	 * the SIGKILL to give everybody a chance. If there is a lot of
169	 * paging activity then wait longer, up to a maximum of approx
170	 * 60 seconds.
171	 */
172	sleep(2);
173	for (i = 0; i < 20; i++) {
174		pageins = get_pageins();
175		if (!nflag)
176			sync();
177		sleep(3);
178		if (get_pageins() == pageins)
179			break;
180	}
181
182	for (i = 1;; ++i) {
183		if (kill(-1, SIGKILL) == -1) {
184			if (errno == ESRCH)
185				break;
186			goto restart;
187		}
188		if (i > 5) {
189			(void)fprintf(stderr,
190			    "WARNING: some process(es) wouldn't die\n");
191			break;
192		}
193		(void)sleep(2 * i);
194	}
195
196	reboot(howto);
197	/* FALLTHROUGH */
198
199restart:
200	sverrno = errno;
201	errx(1, "%s%s", kill(1, SIGHUP) == -1 ? "(can't restart init): " : "",
202	    strerror(sverrno));
203	/* NOTREACHED */
204}
205
206void
207usage()
208{
209	(void)fprintf(stderr, "usage: %s [-dnpq] [-k kernel]\n",
210	    dohalt ? "halt" : "reboot");
211	exit(1);
212}
213
214u_int
215get_pageins()
216{
217	u_int pageins;
218	size_t len;
219
220	len = sizeof(pageins);
221	if (sysctlbyname("vm.stats.vm.v_swappgsin", &pageins, &len, NULL, 0)
222	    != 0) {
223		warnx("v_swappgsin");
224		return (0);
225	}
226	return pageins;
227}
228