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