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