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