xref: /dragonfly/sbin/reboot/reboot.c (revision 2cd2d2b5)
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  * $DragonFly: src/sbin/reboot/reboot.c,v 1.4 2004/08/30 19:27:21 eirikn Exp $
37  */
38 
39 #include <sys/reboot.h>
40 #include <sys/types.h>
41 #include <sys/sysctl.h>
42 #include <signal.h>
43 #include <err.h>
44 #include <errno.h>
45 #include <fcntl.h>
46 #include <libutil.h>
47 #include <pwd.h>
48 #include <syslog.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <unistd.h>
53 
54 void usage(void);
55 u_int get_pageins(void);
56 
57 int dohalt;
58 
59 int
60 main(int argc, char *argv[])
61 {
62 	struct passwd *pw;
63 	int ch, howto, i, fd, kflag, lflag, nflag, qflag, pflag, sverrno;
64 	u_int pageins;
65 	char *kernel, *p;
66 	const char *user;
67 
68 	if (strstr((p = strrchr(*argv, '/')) ? p + 1 : *argv, "halt")) {
69 		dohalt = 1;
70 		howto = RB_HALT;
71 	} else
72 		howto = 0;
73 	kflag = lflag = nflag = qflag = 0;
74 	while ((ch = getopt(argc, argv, "dk:lnpq")) != -1)
75 		switch(ch) {
76 		case 'd':
77 			howto |= RB_DUMP;
78 			break;
79 		case 'k':
80 			kflag = 1;
81 			kernel = optarg;
82 			break;
83 		case 'l':
84 			lflag = 1;
85 			break;
86 		case 'n':
87 			nflag = 1;
88 			howto |= RB_NOSYNC;
89 			break;
90 		case 'p':
91 			pflag = 1;
92 			howto |= (RB_POWEROFF | RB_HALT);
93 			break;
94 		case 'q':
95 			qflag = 1;
96 			break;
97 		case '?':
98 		default:
99 			usage();
100 		}
101 	argc -= optind;
102 	argv += optind;
103 
104 	if ((howto & (RB_DUMP | RB_HALT)) == (RB_DUMP | RB_HALT))
105 		errx(1, "cannot dump (-d) when halting; must reboot instead");
106 	if (geteuid()) {
107 		errno = EPERM;
108 		err(1, NULL);
109 	}
110 
111 	if (qflag) {
112 		reboot(howto);
113 		err(1, NULL);
114 	}
115 
116 	if (kflag) {
117 		fd = open("/boot/nextboot.conf", O_WRONLY | O_CREAT, 0444);
118 		if (fd > -1) {
119 			(void)write(fd, "kernel=\"", 8L);
120 			(void)write(fd, kernel, strlen(kernel));
121 			(void)write(fd, "\"\n", 2);
122 			close(fd);
123 		}
124 	}
125 
126 	/* Log the reboot. */
127 	if (!lflag)  {
128 		if ((user = getlogin()) == NULL)
129 			user = (pw = getpwuid(getuid())) ?
130 			    pw->pw_name : "???";
131 		if (dohalt) {
132 			openlog("halt", 0, LOG_AUTH | LOG_CONS);
133 			syslog(LOG_CRIT, "halted by %s", user);
134 		} else {
135 			openlog("reboot", 0, LOG_AUTH | LOG_CONS);
136 			syslog(LOG_CRIT, "rebooted by %s", user);
137 		}
138 	}
139 	logwtmp("~", "shutdown", "");
140 
141 	/*
142 	 * Do a sync early on, so disks start transfers while we're off
143 	 * killing processes.  Don't worry about writes done before the
144 	 * processes die, the reboot system call syncs the disks.
145 	 */
146 	if (!nflag)
147 		sync();
148 
149 	/* Just stop init -- if we fail, we'll restart it. */
150 	if (kill(1, SIGTSTP) == -1)
151 		err(1, "SIGTSTP init");
152 
153 	/* Ignore the SIGHUP we get when our parent shell dies. */
154 	(void)signal(SIGHUP, SIG_IGN);
155 
156 	/* Send a SIGTERM first, a chance to save the buffers. */
157 	if (kill(-1, SIGTERM) == -1)
158 		err(1, "SIGTERM processes");
159 
160 	/*
161 	 * After the processes receive the signal, start the rest of the
162 	 * buffers on their way.  Wait 5 seconds between the SIGTERM and
163 	 * the SIGKILL to give everybody a chance. If there is a lot of
164 	 * paging activity then wait longer, up to a maximum of approx
165 	 * 60 seconds.
166 	 */
167 	sleep(2);
168 	for (i = 0; i < 20; i++) {
169 		pageins = get_pageins();
170 		if (!nflag)
171 			sync();
172 		sleep(3);
173 		if (get_pageins() == pageins)
174 			break;
175 	}
176 
177 	for (i = 1;; ++i) {
178 		if (kill(-1, SIGKILL) == -1) {
179 			if (errno == ESRCH)
180 				break;
181 			goto restart;
182 		}
183 		if (i > 5) {
184 			(void)fprintf(stderr,
185 			    "WARNING: some process(es) wouldn't die\n");
186 			break;
187 		}
188 		(void)sleep(2 * i);
189 	}
190 
191 	reboot(howto);
192 	/* FALLTHROUGH */
193 
194 restart:
195 	sverrno = errno;
196 	errx(1, "%s%s", kill(1, SIGHUP) == -1 ? "(can't restart init): " : "",
197 	    strerror(sverrno));
198 	/* NOTREACHED */
199 }
200 
201 void
202 usage(void)
203 {
204 	(void)fprintf(stderr, "usage: %s [-dnpq] [-k kernel]\n",
205 	    dohalt ? "halt" : "reboot");
206 	exit(1);
207 }
208 
209 u_int
210 get_pageins(void)
211 {
212 	u_int pageins;
213 	size_t len;
214 
215 	len = sizeof(pageins);
216 	if (sysctlbyname("vm.stats.vm.v_swappgsin", &pageins, &len, NULL, 0)
217 	    != 0) {
218 		warnx("v_swappgsin");
219 		return (0);
220 	}
221 	return pageins;
222 }
223