xref: /dragonfly/sbin/reboot/reboot.c (revision 092c2dd1)
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  * 3. 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  * @(#) Copyright (c) 1980, 1986, 1993 The Regents of the University of California.  All rights reserved.
30  * @(#)reboot.c	8.1 (Berkeley) 6/5/93
31  * $FreeBSD: src/sbin/reboot/reboot.c,v 1.9.2.4 2002/04/28 22:50:00 wes Exp $
32  */
33 
34 #include <sys/reboot.h>
35 #include <sys/types.h>
36 #include <sys/sysctl.h>
37 #include <signal.h>
38 #include <err.h>
39 #include <errno.h>
40 #include <fcntl.h>
41 #include <libutil.h>
42 #include <pwd.h>
43 #include <syslog.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <unistd.h>
48 #ifdef SUPPORT_UTMPX
49 #include <utmpx.h>
50 #endif
51 
52 void usage(void) __dead2;
53 u_int get_pageins(void);
54 
55 int dohalt;
56 
57 int
58 main(int argc, char *argv[])
59 {
60 	struct passwd *pw;
61 	int ch, howto, i, fd, kflag, lflag, nflag, qflag, sverrno;
62 	u_int pageins;
63 	char *kernel = NULL, *p;
64 	const char *user;
65 
66 	if (strstr((p = strrchr(*argv, '/')) ? p + 1 : *argv, "halt")) {
67 		dohalt = 1;
68 		howto = RB_HALT;
69 	} else
70 		howto = 0;
71 	kflag = lflag = nflag = qflag = 0;
72 	while ((ch = getopt(argc, argv, "dk:lnpq")) != -1)
73 		switch(ch) {
74 		case 'd':
75 			howto |= RB_DUMP;
76 			break;
77 		case 'k':
78 			kflag = 1;
79 			kernel = optarg;
80 			break;
81 		case 'l':
82 			lflag = 1;
83 			break;
84 		case 'n':
85 			nflag = 1;
86 			howto |= RB_NOSYNC;
87 			break;
88 		case 'p':
89 			howto |= (RB_POWEROFF | RB_HALT);
90 			break;
91 		case 'q':
92 			qflag = 1;
93 			break;
94 		case '?':
95 		default:
96 			usage();
97 		}
98 	argc -= optind;
99 	argv += optind;
100 
101 	if ((howto & (RB_DUMP | RB_HALT)) == (RB_DUMP | RB_HALT))
102 		errx(1, "cannot dump (-d) when halting; must reboot instead");
103 	if (geteuid()) {
104 		errno = EPERM;
105 		err(1, NULL);
106 	}
107 
108 	if (qflag) {
109 		reboot(howto);
110 		err(1, NULL);
111 	}
112 
113 	if (kflag) {
114 		fd = open("/boot/nextboot.conf",
115 			  O_WRONLY | O_CREAT | O_TRUNC, 0444);
116 		if (fd != -1) {
117 			write(fd, "kernel=\"", 8L);
118 			write(fd, kernel, strlen(kernel));
119 			write(fd, "\"\n", 2);
120 			close(fd);
121 		}
122 	}
123 
124 	/* Log the reboot. */
125 	if (!lflag)  {
126 		if ((user = getlogin()) == NULL)
127 			user = (pw = getpwuid(getuid())) ?
128 			    pw->pw_name : "???";
129 		if (dohalt) {
130 			openlog("halt", 0, LOG_AUTH | LOG_CONS);
131 			syslog(LOG_CRIT, "halted by %s", user);
132 		} else {
133 			openlog("reboot", 0, LOG_AUTH | LOG_CONS);
134 			syslog(LOG_CRIT, "rebooted by %s", user);
135 		}
136 	}
137 #ifdef SUPPORT_UTMP
138 	logwtmp("~", "shutdown", "");
139 #endif
140 #ifdef SUPPORT_UTMPX
141 	logwtmpx("~", "shutdown", "", 0, INIT_PROCESS);
142 #endif
143 
144 	/*
145 	 * Do a sync early on, so disks start transfers while we're off
146 	 * killing processes.  Don't worry about writes done before the
147 	 * processes die, the reboot system call syncs the disks.
148 	 */
149 	if (!nflag)
150 		sync();
151 
152 	/* Just stop init -- if we fail, we'll restart it. */
153 	if (kill(1, SIGTSTP) == -1)
154 		err(1, "SIGTSTP init");
155 
156 	/* Ignore the SIGHUP we get when our parent shell dies. */
157 	signal(SIGHUP, SIG_IGN);
158 	/* parent shell might also send a SIGTERM?  Best to ignore as well */
159 	signal(SIGTERM, SIG_IGN);
160 
161 	/* Send a SIGTERM first, a chance to save the buffers. */
162 	if (kill(-1, SIGTERM) == -1)
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 			fprintf(stderr,
190 			    "WARNING: some process(es) wouldn't die\n");
191 			break;
192 		}
193 		sleep(2 * i);
194 	}
195 
196 	reboot(howto);
197 	/* FALLTHROUGH */
198 
199 restart:
200 	sverrno = errno;
201 	errx(1, "%s%s", kill(1, SIGHUP) == -1 ? "(can't restart init): " : "",
202 	    strerror(sverrno));
203 	/* NOTREACHED */
204 }
205 
206 void
207 usage(void)
208 {
209 	fprintf(stderr, "usage: %s [-dnpq] [-k kernel]\n",
210 	    dohalt ? "halt" : "reboot");
211 	exit(1);
212 }
213 
214 u_int
215 get_pageins(void)
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