xref: /netbsd/sbin/reboot/reboot.c (revision c4a72b64)
1 /*	$NetBSD: reboot.c,v 1.31 2002/08/02 15:05:58 wiz 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.31 2002/08/02 15:05:58 wiz 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 	/* Just stop init -- if we fail, we'll restart it. */
176 	if (kill(1, SIGTSTP) == -1)
177 		err(1, "SIGTSTP init");
178 
179 	/*
180 	 * Ignore signals that we can get as a result of killing
181 	 * parents, group leaders, etc.
182 	 */
183 	(void)signal(SIGHUP,  SIG_IGN);
184 	(void)signal(SIGINT,  SIG_IGN);
185 	(void)signal(SIGQUIT, SIG_IGN);
186 	(void)signal(SIGTERM, SIG_IGN);
187 
188 	/* If we're running in a pipeline, we don't want to die
189 	 * after killing whatever we're writing to. */
190 	(void)signal(SIGPIPE, SIG_IGN);
191 
192 	/* Send a SIGTERM first, a chance to save the buffers. */
193 	if (kill(-1, SIGTERM) == -1) {
194 		/*
195 		 * If ESRCH, everything's OK: we're the only non-system
196 		 * process!  That can happen e.g. via 'exec reboot' in
197 		 * single-user mode.
198 		 */
199 		if (errno != ESRCH) {
200 			warn("SIGTERM processes");
201 			goto restart;
202 		}
203 	}
204 
205 	/*
206 	 * After the processes receive the signal, start the rest of the
207 	 * buffers on their way.  Wait 5 seconds between the SIGTERM and
208 	 * the SIGKILL to pretend to give everybody a chance.
209 	 */
210 	sleep(2);
211 	if (!nflag)
212 		sync();
213 	sleep(3);
214 
215 	for (i = 1;; ++i) {
216 		if (kill(-1, SIGKILL) == -1) {
217 			if (errno == ESRCH)
218 				break;
219 			goto restart;
220 		}
221 		if (i > 5) {
222 			warnx("WARNING: some process(es) wouldn't die");
223 			break;
224 		}
225 		(void)sleep(2 * i);
226 	}
227 
228 	reboot(howto, bootstr);
229 	/* FALLTHROUGH */
230 
231 restart:
232 	sverrno = errno;
233 	errx(1, "%s%s", kill(1, SIGHUP) == -1 ? "(can't restart init): " : "",
234 	    strerror(sverrno));
235 	/* NOTREACHED */
236 }
237 
238 void
239 usage(void)
240 {
241 	const char *pflag = dohalt ? "p" : "";
242 
243 	(void)fprintf(stderr, "usage: %s [-dln%sq] [-- <boot string>]\n",
244 	    getprogname(), pflag);
245 	exit(1);
246 }
247