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