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