xref: /freebsd/sbin/reboot/reboot.c (revision 2830819497fb2deae3dd71574592ace55f2fbdba)
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
32 static 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
38 static char sccsid[] = "@(#)reboot.c	8.1 (Berkeley) 6/5/93";
39 #endif /* not lint */
40 #endif
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
43 
44 #include <sys/reboot.h>
45 #include <sys/time.h>
46 #include <sys/types.h>
47 #include <sys/sysctl.h>
48 #include <signal.h>
49 #include <err.h>
50 #include <errno.h>
51 #include <fcntl.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 #include <utmpx.h>
59 
60 static void usage(void);
61 static u_int get_pageins(void);
62 
63 static int dohalt;
64 
65 int
66 main(int argc, char *argv[])
67 {
68 	struct utmpx utx;
69 	const struct passwd *pw;
70 	int ch, howto, i, fd, lflag, nflag, qflag, sverrno;
71 	u_int pageins;
72 	const char *user, *kernel = NULL;
73 
74 	if (strcmp(getprogname(), "halt") == 0) {
75 		dohalt = 1;
76 		howto = RB_HALT;
77 	} else
78 		howto = 0;
79 	lflag = nflag = qflag = 0;
80 	while ((ch = getopt(argc, argv, "dk:lnpqr")) != -1)
81 		switch(ch) {
82 		case 'd':
83 			howto |= RB_DUMP;
84 			break;
85 		case 'k':
86 			kernel = optarg;
87 			break;
88 		case 'l':
89 			lflag = 1;
90 			break;
91 		case 'n':
92 			nflag = 1;
93 			howto |= RB_NOSYNC;
94 			break;
95 		case 'p':
96 			howto |= RB_POWEROFF;
97 			break;
98 		case 'q':
99 			qflag = 1;
100 			break;
101 		case 'r':
102 			howto |= RB_REROOT;
103 			break;
104 		case '?':
105 		default:
106 			usage();
107 		}
108 	argc -= optind;
109 	argv += optind;
110 
111 	if ((howto & (RB_DUMP | RB_HALT)) == (RB_DUMP | RB_HALT))
112 		errx(1, "cannot dump (-d) when halting; must reboot instead");
113 	if ((howto & RB_REROOT) != 0 && howto != RB_REROOT)
114 		errx(1, "-r cannot be used with -d, -n, or -p");
115 	if (geteuid()) {
116 		errno = EPERM;
117 		err(1, NULL);
118 	}
119 
120 	if (qflag) {
121 		reboot(howto);
122 		err(1, NULL);
123 	}
124 
125 	if (kernel != NULL) {
126 		fd = open("/boot/nextboot.conf", O_WRONLY | O_CREAT | O_TRUNC,
127 		    0444);
128 		if (fd > -1) {
129 			(void)write(fd, "nextboot_enable=\"YES\"\n", 22);
130 			(void)write(fd, "kernel=\"", 8L);
131 			(void)write(fd, kernel, strlen(kernel));
132 			(void)write(fd, "\"\n", 2);
133 			close(fd);
134 		}
135 	}
136 
137 	/* Log the reboot. */
138 	if (!lflag)  {
139 		if ((user = getlogin()) == NULL)
140 			user = (pw = getpwuid(getuid())) ?
141 			    pw->pw_name : "???";
142 		if (dohalt) {
143 			openlog("halt", 0, LOG_AUTH | LOG_CONS);
144 			syslog(LOG_CRIT, "halted by %s", user);
145 		} else if (howto & RB_REROOT) {
146 			openlog("reroot", 0, LOG_AUTH | LOG_CONS);
147 			syslog(LOG_CRIT, "rerooted by %s", user);
148 		} else {
149 			openlog("reboot", 0, LOG_AUTH | LOG_CONS);
150 			syslog(LOG_CRIT, "rebooted by %s", user);
151 		}
152 	}
153 	utx.ut_type = SHUTDOWN_TIME;
154 	gettimeofday(&utx.ut_tv, NULL);
155 	pututxline(&utx);
156 
157 	/*
158 	 * Do a sync early on, so disks start transfers while we're off
159 	 * killing processes.  Don't worry about writes done before the
160 	 * processes die, the reboot system call syncs the disks.
161 	 */
162 	if (!nflag)
163 		sync();
164 
165 	/*
166 	 * Ignore signals that we can get as a result of killing
167 	 * parents, group leaders, etc.
168 	 */
169 	(void)signal(SIGHUP,  SIG_IGN);
170 	(void)signal(SIGINT,  SIG_IGN);
171 	(void)signal(SIGQUIT, SIG_IGN);
172 	(void)signal(SIGTERM, SIG_IGN);
173 	(void)signal(SIGTSTP, SIG_IGN);
174 
175 	/*
176 	 * If we're running in a pipeline, we don't want to die
177 	 * after killing whatever we're writing to.
178 	 */
179 	(void)signal(SIGPIPE, SIG_IGN);
180 
181 	/*
182 	 * Only init(8) can perform rerooting.
183 	 */
184 	if (howto & RB_REROOT) {
185 		if (kill(1, SIGEMT) == -1)
186 			err(1, "SIGEMT init");
187 
188 		return (0);
189 	}
190 
191 	/* Just stop init -- if we fail, we'll restart it. */
192 	if (kill(1, SIGTSTP) == -1)
193 		err(1, "SIGTSTP init");
194 
195 	/* Send a SIGTERM first, a chance to save the buffers. */
196 	if (kill(-1, SIGTERM) == -1 && errno != ESRCH)
197 		err(1, "SIGTERM processes");
198 
199 	/*
200 	 * After the processes receive the signal, start the rest of the
201 	 * buffers on their way.  Wait 5 seconds between the SIGTERM and
202 	 * the SIGKILL to give everybody a chance. If there is a lot of
203 	 * paging activity then wait longer, up to a maximum of approx
204 	 * 60 seconds.
205 	 */
206 	sleep(2);
207 	for (i = 0; i < 20; i++) {
208 		pageins = get_pageins();
209 		if (!nflag)
210 			sync();
211 		sleep(3);
212 		if (get_pageins() == pageins)
213 			break;
214 	}
215 
216 	for (i = 1;; ++i) {
217 		if (kill(-1, SIGKILL) == -1) {
218 			if (errno == ESRCH)
219 				break;
220 			goto restart;
221 		}
222 		if (i > 5) {
223 			(void)fprintf(stderr,
224 			    "WARNING: some process(es) wouldn't die\n");
225 			break;
226 		}
227 		(void)sleep(2 * i);
228 	}
229 
230 	reboot(howto);
231 	/* FALLTHROUGH */
232 
233 restart:
234 	sverrno = errno;
235 	errx(1, "%s%s", kill(1, SIGHUP) == -1 ? "(can't restart init): " : "",
236 	    strerror(sverrno));
237 	/* NOTREACHED */
238 }
239 
240 static void
241 usage(void)
242 {
243 
244 	(void)fprintf(stderr, dohalt ?
245 	    "usage: halt [-lnpq] [-k kernel]\n" :
246 	    "usage: reboot [-dlnpq] [-k kernel]\n");
247 	exit(1);
248 }
249 
250 static u_int
251 get_pageins(void)
252 {
253 	u_int pageins;
254 	size_t len;
255 
256 	len = sizeof(pageins);
257 	if (sysctlbyname("vm.stats.vm.v_swappgsin", &pageins, &len, NULL, 0)
258 	    != 0) {
259 		warnx("v_swappgsin");
260 		return (0);
261 	}
262 	return pageins;
263 }
264