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