xref: /minix/sbin/reboot/reboot.c (revision a06e2ab3)
1 /*	$NetBSD: reboot.c,v 1.39 2011/08/27 18:46:19 joerg Exp $	*/
2 
3 /*
4  * Copyright (c) 1980, 1986, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 
34 #ifndef lint
35 __COPYRIGHT("@(#) Copyright (c) 1980, 1986, 1993\
36  The Regents of the University of California.  All rights reserved.");
37 #endif /* not lint */
38 
39 #ifndef lint
40 #if 0
41 static char sccsid[] = "@(#)reboot.c	8.1 (Berkeley) 6/5/93";
42 #else
43 __RCSID("$NetBSD: reboot.c,v 1.39 2011/08/27 18:46:19 joerg Exp $");
44 #endif
45 #endif /* not lint */
46 
47 #include <sys/reboot.h>
48 
49 #include <err.h>
50 #include <errno.h>
51 #include <pwd.h>
52 #include <signal.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <syslog.h>
57 #include <unistd.h>
58 #include <util.h>
59 
60 __dead static void usage(void);
61 
62 static int dohalt;
63 static int dopoweroff;
64 
65 int
66 main(int argc, char *argv[])
67 {
68 	const char *progname;
69 	/* int i; */
70 	struct passwd *pw;
71 	int ch, howto, lflag, nflag, qflag, sverrno, len;
72 	const char *user;
73 	char *bootstr, **av;
74 
75 	progname = getprogname();
76 	if (progname[0] == '-')
77 		progname++;
78 	if (strcmp(progname, "halt") == 0) {
79 		dohalt = 1;
80 		howto = RB_HALT;
81 	} else if (strcmp(progname, "poweroff") == 0) {
82 		dopoweroff = 1;
83 		howto = RB_HALT | RB_POWERDOWN;
84 	} else
85 		howto = 0;
86 	lflag = nflag = qflag = 0;
87 	while ((ch = getopt(argc, argv, "dlnpqvxz")) != -1)
88 		switch(ch) {
89 		case 'd':
90 			howto |= RB_DUMP;
91 			break;
92 		case 'l':
93 			lflag = 1;
94 			break;
95 		case 'n':
96 			nflag = 1;
97 			howto |= RB_NOSYNC;
98 			break;
99 		case 'p':
100 			if (dohalt == 0)
101 				usage();
102 			howto |= RB_POWERDOWN;
103 			break;
104 		case 'q':
105 			qflag = 1;
106 			break;
107 		case 'v':
108 			howto |= AB_VERBOSE;
109 			break;
110 		case 'x':
111 			howto |= AB_DEBUG;
112 			break;
113 		case 'z':
114 			howto |= AB_SILENT;
115 			break;
116 		case '?':
117 		default:
118 			usage();
119 		}
120 	argc -= optind;
121 	argv += optind;
122 
123 	if (argc) {
124 		for (av = argv, len = 0; *av; av++)
125 			len += strlen(*av) + 1;
126 		bootstr = malloc(len + 1);
127 		*bootstr = '\0';		/* for first strcat */
128 		for (av = argv; *av; av++) {
129 			strcat(bootstr, *av);
130 			strcat(bootstr, " ");
131 		}
132 		bootstr[len - 1] = '\0';	/* to kill last space */
133 		howto |= RB_STRING;
134 	} else
135 		bootstr = NULL;
136 
137 	if (geteuid())
138 		errx(1, "%s", strerror(EPERM));
139 
140 	if (qflag) {
141 		reboot(howto, bootstr);
142 		err(1, "reboot");
143 	}
144 
145 	/* Log the reboot. */
146 	if (!lflag)  {
147 		if ((user = getlogin()) == NULL)
148 			user = (pw = getpwuid(getuid())) ?
149 			    pw->pw_name : "???";
150 		if (dohalt) {
151 			openlog("halt", LOG_CONS, LOG_AUTH);
152 			syslog(LOG_CRIT, "halted by %s", user);
153 		} else if (dopoweroff) {
154 			openlog("poweroff", LOG_CONS, LOG_AUTH);
155 			syslog(LOG_CRIT, "powered off by %s", user);
156 		} else {
157 			openlog("reboot", LOG_CONS, LOG_AUTH);
158 			if (bootstr)
159 				syslog(LOG_CRIT, "rebooted by %s: %s", user,
160 				    bootstr);
161 			else
162 				syslog(LOG_CRIT, "rebooted by %s", user);
163 		}
164 	}
165 #ifdef SUPPORT_UTMP
166 	logwtmp("~", "shutdown", "");
167 #endif
168 #ifdef SUPPORT_UTMPX
169 	logwtmpx("~", "shutdown", "", INIT_PROCESS, 0);
170 #endif
171 
172 	/*
173 	 * Do a sync early on, so disks start transfers while we're off
174 	 * killing processes.  Don't worry about writes done before the
175 	 * processes die, the reboot system call syncs the disks.
176 	 */
177 	if (!nflag)
178 		sync();
179 
180 	/*
181 	 * Ignore signals that we can get as a result of killing
182 	 * parents, group leaders, etc.
183 	 */
184 	(void)signal(SIGHUP,  SIG_IGN);
185 	(void)signal(SIGINT,  SIG_IGN);
186 	(void)signal(SIGQUIT, SIG_IGN);
187 	(void)signal(SIGTERM, SIG_IGN);
188 	(void)signal(SIGTSTP, SIG_IGN);
189 
190 	/*
191 	 * If we're running in a pipeline, we don't want to die
192 	 * after killing whatever we're writing to.
193 	 */
194 	(void)signal(SIGPIPE, SIG_IGN);
195 
196 
197 	/* Just stop init -- if we fail, we'll restart it. */
198 #if 0 && defined(__minix)
199 	if (kill(1, SIGTERM) == -1)
200 		err(1, "SIGTERM init");
201 #else
202 	if (kill(1, SIGTSTP) == -1)
203 		err(1, "SIGTSTP init");
204 #endif
205 
206 	/* Send a SIGTERM first, a chance to save the buffers. */
207 	if (kill(-1, SIGTERM) == -1) {
208 		/*
209 		 * If ESRCH, everything's OK: we're the only non-system
210 		 * process!  That can happen e.g. via 'exec reboot' in
211 		 * single-user mode.
212 		 */
213 		if (errno != ESRCH) {
214 			warn("SIGTERM all processes");
215 			goto restart;
216 		}
217 	}
218 
219 	/*
220 	 * After the processes receive the signal, start the rest of the
221 	 * buffers on their way.  Wait 5 seconds between the SIGTERM and
222 	 * the SIGKILL to pretend to give everybody a chance.
223 	 */
224 	sleep(2);
225 	if (!nflag)
226 		sync();
227 	sleep(3);
228 
229 #ifndef __minix
230 	for (i = 1;; ++i) {
231 		if (kill(-1, SIGKILL) == -1) {
232 			if (errno == ESRCH)
233 				break;
234 			warn("SIGKILL all processes");
235 			goto restart;
236 		}
237 		if (i > 5) {
238 			warnx("WARNING: some process(es) wouldn't die");
239 			break;
240 		}
241 		(void)sleep(2 * i);
242 	}
243 #endif
244 
245 	reboot(howto, bootstr);
246 	warn("reboot()");
247 	/* FALLTHROUGH */
248 
249 restart:
250 	sverrno = errno;
251 	errx(1, "%s%s", kill(1, SIGHUP) == -1 ? "(can't restart init): " : "",
252 	    strerror(sverrno));
253 	/* NOTREACHED */
254 }
255 
256 static void
257 usage(void)
258 {
259 	const char *pflag = dohalt ? "p" : "";
260 
261 	(void)fprintf(stderr, "usage: %s [-dln%sqvxz] [-- <boot string>]\n",
262 	    getprogname(), pflag);
263 	exit(1);
264 }
265