xref: /freebsd/sbin/reboot/reboot.c (revision 9207b4cff7b8d483f4dd3c62266c2b58819eb7f9)
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 
34 #ifndef lint
35 static const char copyright[] =
36 "@(#) Copyright (c) 1980, 1986, 1993\n\
37 	The Regents of the University of California.  All rights reserved.\n";
38 #endif /* not lint */
39 
40 #ifndef lint
41 #if 0
42 static char sccsid[] = "@(#)reboot.c	8.1 (Berkeley) 6/5/93";
43 #endif
44 static const char rcsid[] =
45   "$FreeBSD$";
46 #endif /* not lint */
47 
48 #include <sys/reboot.h>
49 #include <sys/types.h>
50 #include <sys/sysctl.h>
51 #include <signal.h>
52 #include <err.h>
53 #include <errno.h>
54 #include <libutil.h>
55 #include <pwd.h>
56 #include <syslog.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include <unistd.h>
61 
62 void usage(void);
63 u_int get_pageins(void);
64 
65 int dohalt;
66 
67 int
68 main(int argc, char *argv[])
69 {
70 	struct passwd *pw;
71 	int ch, howto, i, lflag, nflag, qflag, pflag, sverrno;
72 	u_int pageins;
73 	char *p;
74 	const char *user;
75 
76 	if (strstr((p = rindex(*argv, '/')) ? p + 1 : *argv, "halt")) {
77 		dohalt = 1;
78 		howto = RB_HALT;
79 	} else
80 		howto = 0;
81 	lflag = nflag = qflag = 0;
82 	while ((ch = getopt(argc, argv, "dlnpq")) != -1)
83 		switch(ch) {
84 		case 'd':
85 			howto |= RB_DUMP;
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 | RB_HALT);
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 	/* Log the reboot. */
121 	if (!lflag)  {
122 		if ((user = getlogin()) == NULL)
123 			user = (pw = getpwuid(getuid())) ?
124 			    pw->pw_name : "???";
125 		if (dohalt) {
126 			openlog("halt", 0, LOG_AUTH | LOG_CONS);
127 			syslog(LOG_CRIT, "halted by %s", user);
128 		} else {
129 			openlog("reboot", 0, LOG_AUTH | LOG_CONS);
130 			syslog(LOG_CRIT, "rebooted by %s", user);
131 		}
132 	}
133 	logwtmp("~", "shutdown", "");
134 
135 	/*
136 	 * Do a sync early on, so disks start transfers while we're off
137 	 * killing processes.  Don't worry about writes done before the
138 	 * processes die, the reboot system call syncs the disks.
139 	 */
140 	if (!nflag)
141 		sync();
142 
143 	/* Just stop init -- if we fail, we'll restart it. */
144 	if (kill(1, SIGTSTP) == -1)
145 		err(1, "SIGTSTP init");
146 
147 	/* Ignore the SIGHUP we get when our parent shell dies. */
148 	(void)signal(SIGHUP, SIG_IGN);
149 
150 	/* Send a SIGTERM first, a chance to save the buffers. */
151 	if (kill(-1, SIGTERM) == -1)
152 		err(1, "SIGTERM processes");
153 
154 	/*
155 	 * After the processes receive the signal, start the rest of the
156 	 * buffers on their way.  Wait 5 seconds between the SIGTERM and
157 	 * the SIGKILL to give everybody a chance. If there is a lot of
158 	 * paging activity then wait longer, up to a maximum of approx
159 	 * 60 seconds.
160 	 */
161 	sleep(2);
162 	for (i = 0; i < 20; i++) {
163 		pageins = get_pageins();
164 		if (!nflag)
165 			sync();
166 		sleep(3);
167 		if (get_pageins() == pageins)
168 			break;
169 	}
170 
171 	for (i = 1;; ++i) {
172 		if (kill(-1, SIGKILL) == -1) {
173 			if (errno == ESRCH)
174 				break;
175 			goto restart;
176 		}
177 		if (i > 5) {
178 			(void)fprintf(stderr,
179 			    "WARNING: some process(es) wouldn't die\n");
180 			break;
181 		}
182 		(void)sleep(2 * i);
183 	}
184 
185 	reboot(howto);
186 	/* FALLTHROUGH */
187 
188 restart:
189 	sverrno = errno;
190 	errx(1, "%s%s", kill(1, SIGHUP) == -1 ? "(can't restart init): " : "",
191 	    strerror(sverrno));
192 	/* NOTREACHED */
193 }
194 
195 void
196 usage()
197 {
198 	(void)fprintf(stderr, "usage: %s [-dnpq]\n",
199 	    dohalt ? "halt" : "reboot");
200 	exit(1);
201 }
202 
203 u_int
204 get_pageins()
205 {
206 	u_int pageins;
207 	size_t len;
208 
209 	len = sizeof(pageins);
210 	if (sysctlbyname("vm.stats.vm.v_swappgsin", &pageins, &len, NULL, 0)
211 	    != 0) {
212 		warnx("v_swappgsin");
213 		return (0);
214 	}
215 	return pageins;
216 }
217