xref: /netbsd/sbin/reboot/reboot.c (revision bf9ec67e)
1 /*	$NetBSD: reboot.c,v 1.28 2001/02/19 22:56:22 cgd 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.28 2001/02/19 22:56:22 cgd 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 __P((int, char *[]));
65 void usage __P((void));
66 
67 int dohalt;
68 int dopoweroff;
69 
70 int
71 main(argc, argv)
72 	int argc;
73 	char *argv[];
74 {
75 	const char *progname;
76 	int i;
77 	struct passwd *pw;
78 	int ch, howto, lflag, nflag, qflag, sverrno, len;
79 	const char *user;
80 	char *bootstr, **av;
81 
82 	progname = getprogname();
83 	if (!strcmp(progname, "halt") || !strcmp(progname, "-halt")) {
84 		dohalt = 1;
85 		howto = RB_HALT;
86 	} else if (!strcmp(progname, "poweroff")
87 		   || !strcmp(progname, "-poweroff")) {
88 		dopoweroff = 1;
89 		howto = RB_HALT | RB_POWERDOWN;
90 	} else
91 		howto = 0;
92 	lflag = nflag = qflag = 0;
93 	while ((ch = getopt(argc, argv, "dlnpq")) != -1)
94 		switch(ch) {
95 		case 'd':
96 			howto |= RB_DUMP;
97 			break;
98 		case 'l':
99 			lflag = 1;
100 			break;
101 		case 'n':
102 			nflag = 1;
103 			howto |= RB_NOSYNC;
104 			break;
105 		case 'p':
106 			if (dohalt == 0)
107 				usage();
108 			howto |= RB_POWERDOWN;
109 			break;
110 		case 'q':
111 			qflag = 1;
112 			break;
113 		case '?':
114 		default:
115 			usage();
116 		}
117 	argc -= optind;
118 	argv += optind;
119 
120 	if (argc) {
121 		for (av = argv, len = 0; *av; av++)
122 			len += strlen(*av) + 1;
123 		bootstr = malloc(len + 1);
124 		*bootstr = '\0';		/* for first strcat */
125 		for (av = argv; *av; av++) {
126 			strcat(bootstr, *av);
127 			strcat(bootstr, " ");
128 		}
129 		bootstr[len] = '\0';		/* to kill last space */
130 		howto |= RB_STRING;
131 	} else
132 		bootstr = NULL;
133 
134 	if (geteuid())
135 		errx(1, "%s", strerror(EPERM));
136 
137 	if (qflag) {
138 		reboot(howto, bootstr);
139 		err(1, "reboot");
140 	}
141 
142 	/* Log the reboot. */
143 	if (!lflag)  {
144 		if ((user = getlogin()) == NULL)
145 			user = (pw = getpwuid(getuid())) ?
146 			    pw->pw_name : "???";
147 		if (dohalt) {
148 			openlog("halt", LOG_CONS, LOG_AUTH);
149 			syslog(LOG_CRIT, "halted by %s", user);
150 		} else if (dopoweroff) {
151 			openlog("poweroff", LOG_CONS, LOG_AUTH);
152 			syslog(LOG_CRIT, "powered off by %s", user);
153 		} else {
154 			openlog("reboot", LOG_CONS, LOG_AUTH);
155 			if (bootstr)
156 				syslog(LOG_CRIT, "rebooted by %s: %s", user,
157 				    bootstr);
158 			else
159 				syslog(LOG_CRIT, "rebooted by %s", user);
160 		}
161 	}
162 	logwtmp("~", "shutdown", "");
163 
164 	/*
165 	 * Do a sync early on, so disks start transfers while we're off
166 	 * killing processes.  Don't worry about writes done before the
167 	 * processes die, the reboot system call syncs the disks.
168 	 */
169 	if (!nflag)
170 		sync();
171 
172 	/* Just stop init -- if we fail, we'll restart it. */
173 	if (kill(1, SIGTSTP) == -1)
174 		err(1, "SIGTSTP init");
175 
176 	/*
177 	 * Ignore signals that we can get as a result of killing
178 	 * parents, group leaders, etc.
179 	 */
180 	(void)signal(SIGHUP,  SIG_IGN);
181 	(void)signal(SIGINT,  SIG_IGN);
182 	(void)signal(SIGQUIT, SIG_IGN);
183 	(void)signal(SIGTERM, SIG_IGN);
184 
185 	/* If we're running in a pipeline, we don't want to die
186 	 * after killing whatever we're writing to. */
187 	(void)signal(SIGPIPE, SIG_IGN);
188 
189 	/* Send a SIGTERM first, a chance to save the buffers. */
190 	if (kill(-1, SIGTERM) == -1) {
191 		/*
192 		 * If ESRCH, everything's OK: we're the only non-system
193 		 * process!  That can happen e.g. via 'exec reboot' in
194 		 * single-user mode.
195 		 */
196 		if (errno != ESRCH) {
197 			warn("SIGTERM processes");
198 			goto restart;
199 		}
200 	}
201 
202 	/*
203 	 * After the processes receive the signal, start the rest of the
204 	 * buffers on their way.  Wait 5 seconds between the SIGTERM and
205 	 * the SIGKILL to pretend to give everybody a chance.
206 	 */
207 	sleep(2);
208 	if (!nflag)
209 		sync();
210 	sleep(3);
211 
212 	for (i = 1;; ++i) {
213 		if (kill(-1, SIGKILL) == -1) {
214 			if (errno == ESRCH)
215 				break;
216 			goto restart;
217 		}
218 		if (i > 5) {
219 			warnx("WARNING: some process(es) wouldn't die");
220 			break;
221 		}
222 		(void)sleep(2 * i);
223 	}
224 
225 	reboot(howto, bootstr);
226 	/* FALLTHROUGH */
227 
228 restart:
229 	sverrno = errno;
230 	errx(1, "%s%s", kill(1, SIGHUP) == -1 ? "(can't restart init): " : "",
231 	    strerror(sverrno));
232 	/* NOTREACHED */
233 }
234 
235 void
236 usage()
237 {
238 	const char *pflag = dohalt ? "p" : "";
239 
240 	(void)fprintf(stderr, "usage: %s [-dln%sq] [-- <boot string>]\n",
241 	    getprogname(), pflag);
242 	exit(1);
243 }
244