reboot.c revision 1.15 1 /* $NetBSD: reboot.c,v 1.15 1997/09/15 07:38:34 lukem 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.15 1997/09/15 07:38:34 lukem 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);
111 bootstr = malloc(len + 1);
112 *bootstr = '\0';
113 for (av = argv; *av; av++)
114 strcpy(bootstr, *av);
115 howto |= RB_STRING;
116 } else
117 bootstr = NULL;
118
119 if (geteuid())
120 errx(1, "%s", strerror(EPERM));
121
122 if (qflag) {
123 reboot(howto, bootstr);
124 err(1, "reboot");
125 }
126
127 /* Log the reboot. */
128 if (!lflag) {
129 if ((user = getlogin()) == NULL)
130 user = (pw = getpwuid(getuid())) ?
131 pw->pw_name : "???";
132 if (dohalt) {
133 openlog("halt", 0, LOG_AUTH | LOG_CONS);
134 syslog(LOG_CRIT, "halted by %s", user);
135 } else {
136 openlog("reboot", 0, LOG_AUTH | LOG_CONS);
137 if (bootstr)
138 syslog(LOG_CRIT, "rebooted by %s: %s", user,
139 bootstr);
140 else
141 syslog(LOG_CRIT, "rebooted by %s", user);
142 }
143 }
144 logwtmp("~", "shutdown", "");
145
146 /*
147 * Do a sync early on, so disks start transfers while we're off
148 * killing processes. Don't worry about writes done before the
149 * processes die, the reboot system call syncs the disks.
150 */
151 if (!nflag)
152 sync();
153
154 /* Just stop init -- if we fail, we'll restart it. */
155 if (kill(1, SIGTSTP) == -1)
156 err(1, "SIGTSTP init");
157
158 /* Ignore the SIGHUP we get when our parent shell dies. */
159 (void)signal(SIGHUP, SIG_IGN);
160
161 /* Send a SIGTERM first, a chance to save the buffers. */
162 if (kill(-1, SIGTERM) == -1) {
163 /*
164 * If ESRCH, everything's OK: we're the only non-system
165 * process! That can happen e.g. via 'exec reboot' in
166 * single-user mode.
167 */
168 if (errno != ESRCH) {
169 (void)fprintf(stderr, "%s: SIGTERM processes: %s",
170 dohalt ? "halt" : "reboot", strerror(errno));
171 goto restart;
172 }
173 }
174
175 /*
176 * After the processes receive the signal, start the rest of the
177 * buffers on their way. Wait 5 seconds between the SIGTERM and
178 * the SIGKILL to give everybody a chance.
179 */
180 sleep(2);
181 if (!nflag)
182 sync();
183 sleep(3);
184
185 for (i = 1;; ++i) {
186 if (kill(-1, SIGKILL) == -1) {
187 if (errno == ESRCH)
188 break;
189 goto restart;
190 }
191 if (i > 5) {
192 (void)fprintf(stderr,
193 "WARNING: some process(es) wouldn't die\n");
194 break;
195 }
196 (void)sleep(2 * i);
197 }
198
199 reboot(howto, bootstr);
200 /* FALLTHROUGH */
201
202 restart:
203 sverrno = errno;
204 errx(1, "%s%s", kill(1, SIGHUP) == -1 ? "(can't restart init): " : "",
205 strerror(sverrno));
206 /* NOTREACHED */
207 }
208
209 void
210 usage()
211 {
212 (void)fprintf(stderr, "usage: %s [-nqd] [-- <boot string>]\n",
213 __progname);
214 exit(1);
215 }
216