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