reboot.c revision 1.16 1 /* $NetBSD: reboot.c,v 1.16 1997/12/09 05:49:14 mrg 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.16 1997/12/09 05:49:14 mrg Exp $");
48 #endif
49 #endif /* not lint */
50
51 #include <sys/reboot.h>
52 #include <signal.h>
53 #include <pwd.h>
54 #include <err.h>
55 #include <errno.h>
56 #include <syslog.h>
57 #include <unistd.h>
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <string.h>
61 #include <util.h>
62
63 int main __P((int, char *[]));
64 void usage __P((void));
65
66 extern char *__progname;
67
68 int dohalt;
69
70 int
71 main(argc, argv)
72 int argc;
73 char *argv[];
74 {
75 int i;
76 struct passwd *pw;
77 int ch, howto, lflag, nflag, qflag, sverrno, len;
78 char *user, *bootstr, **av;
79
80 if (!strcmp(__progname, "halt") || !strcmp(__progname, "-halt")) {
81 dohalt = 1;
82 howto = RB_HALT;
83 } else
84 howto = 0;
85 lflag = nflag = qflag = 0;
86 while ((ch = getopt(argc, argv, "lnqd")) != -1)
87 switch(ch) {
88 case 'l': /* Undocumented; used by shutdown. */
89 lflag = 1;
90 break;
91 case 'n':
92 nflag = 1;
93 howto |= RB_NOSYNC;
94 break;
95 case 'q':
96 qflag = 1;
97 break;
98 case 'd':
99 howto |= RB_DUMP;
100 break;
101 case '?':
102 default:
103 usage();
104 }
105 argc -= optind;
106 argv += optind;
107
108 if (argc) {
109 for (av = argv, len = 0; *av; av++)
110 len += strlen(*av) + 1;
111 bootstr = malloc(len + 1);
112 *bootstr = '\0'; /* for first strcat */
113 for (av = argv; *av; av++) {
114 strcat(bootstr, *av);
115 strcat(bootstr, " ");
116 }
117 bootstr[len] = '\0'; /* to kill last space */
118 howto |= RB_STRING;
119 } else
120 bootstr = NULL;
121
122 if (geteuid())
123 errx(1, "%s", strerror(EPERM));
124
125 if (qflag) {
126 reboot(howto, bootstr);
127 err(1, "reboot");
128 }
129
130 /* Log the reboot. */
131 if (!lflag) {
132 if ((user = getlogin()) == NULL)
133 user = (pw = getpwuid(getuid())) ?
134 pw->pw_name : "???";
135 if (dohalt) {
136 openlog("halt", 0, LOG_AUTH | LOG_CONS);
137 syslog(LOG_CRIT, "halted by %s", user);
138 } else {
139 openlog("reboot", 0, LOG_AUTH | LOG_CONS);
140 if (bootstr)
141 syslog(LOG_CRIT, "rebooted by %s: %s", user,
142 bootstr);
143 else
144 syslog(LOG_CRIT, "rebooted by %s", user);
145 }
146 }
147 logwtmp("~", "shutdown", "");
148
149 /*
150 * Do a sync early on, so disks start transfers while we're off
151 * killing processes. Don't worry about writes done before the
152 * processes die, the reboot system call syncs the disks.
153 */
154 if (!nflag)
155 sync();
156
157 /* Just stop init -- if we fail, we'll restart it. */
158 if (kill(1, SIGTSTP) == -1)
159 err(1, "SIGTSTP init");
160
161 /* Ignore the SIGHUP we get when our parent shell dies. */
162 (void)signal(SIGHUP, SIG_IGN);
163
164 /* Send a SIGTERM first, a chance to save the buffers. */
165 if (kill(-1, SIGTERM) == -1) {
166 /*
167 * If ESRCH, everything's OK: we're the only non-system
168 * process! That can happen e.g. via 'exec reboot' in
169 * single-user mode.
170 */
171 if (errno != ESRCH) {
172 (void)fprintf(stderr, "%s: SIGTERM processes: %s",
173 dohalt ? "halt" : "reboot", strerror(errno));
174 goto restart;
175 }
176 }
177
178 /*
179 * After the processes receive the signal, start the rest of the
180 * buffers on their way. Wait 5 seconds between the SIGTERM and
181 * the SIGKILL to give everybody a chance.
182 */
183 sleep(2);
184 if (!nflag)
185 sync();
186 sleep(3);
187
188 for (i = 1;; ++i) {
189 if (kill(-1, SIGKILL) == -1) {
190 if (errno == ESRCH)
191 break;
192 goto restart;
193 }
194 if (i > 5) {
195 (void)fprintf(stderr,
196 "WARNING: some process(es) wouldn't die\n");
197 break;
198 }
199 (void)sleep(2 * i);
200 }
201
202 reboot(howto, bootstr);
203 /* FALLTHROUGH */
204
205 restart:
206 sverrno = errno;
207 errx(1, "%s%s", kill(1, SIGHUP) == -1 ? "(can't restart init): " : "",
208 strerror(sverrno));
209 /* NOTREACHED */
210 }
211
212 void
213 usage()
214 {
215 (void)fprintf(stderr, "usage: %s [-nqd] [-- <boot string>]\n",
216 __progname);
217 exit(1);
218 }
219