1 /* $NetBSD: reboot.c,v 1.35 2008/02/09 04:27:06 dholland Exp $ */
4 * Copyright (c) 1980, 1986, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
32 #include <sys/cdefs.h>
35 __COPYRIGHT("@(#) Copyright (c) 1980, 1986, 1993\
36 The Regents of the University of California. All rights reserved.");
41 static char sccsid
[] = "@(#)reboot.c 8.1 (Berkeley) 6/5/93";
43 __RCSID("$NetBSD: reboot.c,v 1.35 2008/02/09 04:27:06 dholland Exp $");
47 #include <sys/reboot.h>
60 int main(int, char *[]);
67 main(int argc
, char *argv
[])
72 int ch
, howto
, lflag
, nflag
, qflag
, sverrno
, len
;
76 progname
= getprogname();
77 if (progname
[0] == '-')
79 if (strcmp(progname
, "halt") == 0) {
82 } else if (strcmp(progname
, "poweroff") == 0) {
84 howto
= RB_HALT
| RB_POWERDOWN
;
87 lflag
= nflag
= qflag
= 0;
88 while ((ch
= getopt(argc
, argv
, "dlnpq")) != -1)
103 howto
|= RB_POWERDOWN
;
116 for (av
= argv
, len
= 0; *av
; av
++)
117 len
+= strlen(*av
) + 1;
118 bootstr
= malloc(len
+ 1);
119 *bootstr
= '\0'; /* for first strcat */
120 for (av
= argv
; *av
; av
++) {
121 strcat(bootstr
, *av
);
122 strcat(bootstr
, " ");
124 bootstr
[len
- 1] = '\0'; /* to kill last space */
130 errx(1, "%s", strerror(EPERM
));
133 reboot(howto
, bootstr
);
137 /* Log the reboot. */
139 if ((user
= getlogin()) == NULL
)
140 user
= (pw
= getpwuid(getuid())) ?
143 openlog("halt", LOG_CONS
, LOG_AUTH
);
144 syslog(LOG_CRIT
, "halted by %s", user
);
145 } else if (dopoweroff
) {
146 openlog("poweroff", LOG_CONS
, LOG_AUTH
);
147 syslog(LOG_CRIT
, "powered off by %s", user
);
149 openlog("reboot", LOG_CONS
, LOG_AUTH
);
151 syslog(LOG_CRIT
, "rebooted by %s: %s", user
,
154 syslog(LOG_CRIT
, "rebooted by %s", user
);
158 logwtmp("~", "shutdown", "");
161 logwtmpx("~", "shutdown", "", INIT_PROCESS
, 0);
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.
173 * Ignore signals that we can get as a result of killing
174 * parents, group leaders, etc.
176 (void)signal(SIGHUP
, SIG_IGN
);
177 (void)signal(SIGINT
, SIG_IGN
);
178 (void)signal(SIGQUIT
, SIG_IGN
);
179 (void)signal(SIGTERM
, SIG_IGN
);
180 (void)signal(SIGTSTP
, SIG_IGN
);
183 * If we're running in a pipeline, we don't want to die
184 * after killing whatever we're writing to.
186 (void)signal(SIGPIPE
, SIG_IGN
);
188 /* Just stop init -- if we fail, we'll restart it. */
189 if (kill(1, SIGTSTP
) == -1)
190 err(1, "SIGTSTP init");
192 /* Send a SIGTERM first, a chance to save the buffers. */
193 if (kill(-1, SIGTERM
) == -1) {
195 * If ESRCH, everything's OK: we're the only non-system
196 * process! That can happen e.g. via 'exec reboot' in
199 if (errno
!= ESRCH
) {
200 warn("SIGTERM all processes");
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.
216 if (kill(-1, SIGKILL
) == -1) {
219 warn("SIGKILL all processes");
223 warnx("WARNING: some process(es) wouldn't die");
229 reboot(howto
, bootstr
);
235 errx(1, "%s%s", kill(1, SIGHUP
) == -1 ? "(can't restart init): " : "",
243 const char *pflag
= dohalt
? "p" : "";
245 (void)fprintf(stderr
, "usage: %s [-dln%sq] [-- <boot string>]\n",
246 getprogname(), pflag
);