reboot.c revision 1.8 1 /* $NetBSD: reboot.c,v 1.8 1995/10/05 05:36:22 mycroft 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 #ifndef lint
37 static char copyright[] =
38 "@(#) Copyright (c) 1980, 1986, 1993\n\
39 The Regents of the University of California. All rights reserved.\n";
40 #endif /* not lint */
41
42 #ifndef lint
43 #if 0
44 static char sccsid[] = "@(#)reboot.c 8.1 (Berkeley) 6/5/93";
45 #else
46 static char rcsid[] = "$NetBSD: reboot.c,v 1.8 1995/10/05 05:36:22 mycroft Exp $";
47 #endif
48 #endif /* not lint */
49
50 #include <sys/reboot.h>
51 #include <signal.h>
52 #include <pwd.h>
53 #include <errno.h>
54 #include <syslog.h>
55 #include <unistd.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
59
60 void err __P((const char *fmt, ...));
61 void usage __P((void));
62
63 int dohalt;
64
65 int
66 main(argc, argv)
67 int argc;
68 char *argv[];
69 {
70 register int i;
71 struct passwd *pw;
72 int ch, howto, lflag, nflag, qflag, sverrno;
73 char *p, *user;
74
75 if (!strcmp((p = rindex(*argv, '/')) ? p + 1 : *argv, "halt")) {
76 dohalt = 1;
77 howto = RB_HALT;
78 } else
79 howto = 0;
80 lflag = nflag = qflag = 0;
81 while ((ch = getopt(argc, argv, "lnqd")) != EOF)
82 switch(ch) {
83 case 'l': /* Undocumented; used by shutdown. */
84 lflag = 1;
85 break;
86 case 'n':
87 nflag = 1;
88 howto |= RB_NOSYNC;
89 break;
90 case 'q':
91 qflag = 1;
92 break;
93 case 'd':
94 howto |= RB_DUMP;
95 break;
96 case '?':
97 default:
98 usage();
99 }
100 argc -= optind;
101 argv += optind;
102
103 if (geteuid())
104 err("%s", strerror(EPERM));
105
106 if (qflag) {
107 reboot(howto);
108 err("%s", strerror(errno));
109 }
110
111 /* Log the reboot. */
112 if (!lflag) {
113 if ((user = getlogin()) == NULL)
114 user = (pw = getpwuid(getuid())) ?
115 pw->pw_name : "???";
116 if (dohalt) {
117 openlog("halt", 0, LOG_AUTH | LOG_CONS);
118 syslog(LOG_CRIT, "halted by %s", user);
119 } else {
120 openlog("reboot", 0, LOG_AUTH | LOG_CONS);
121 syslog(LOG_CRIT, "rebooted by %s", user);
122 }
123 }
124 logwtmp("~", "shutdown", "");
125
126 /*
127 * Do a sync early on, so disks start transfers while we're off
128 * killing processes. Don't worry about writes done before the
129 * processes die, the reboot system call syncs the disks.
130 */
131 if (!nflag)
132 sync();
133
134 /* Just stop init -- if we fail, we'll restart it. */
135 if (kill(1, SIGTSTP) == -1)
136 err("SIGTSTP init: %s", strerror(errno));
137
138 /* Ignore the SIGHUP we get when our parent shell dies. */
139 (void)signal(SIGHUP, SIG_IGN);
140
141 /* Send a SIGTERM first, a chance to save the buffers. */
142 if (kill(-1, SIGTERM) == -1)
143 err("SIGTERM processes: %s", strerror(errno));
144
145 /*
146 * After the processes receive the signal, start the rest of the
147 * buffers on their way. Wait 5 seconds between the SIGTERM and
148 * the SIGKILL to give everybody a chance.
149 */
150 sleep(2);
151 if (!nflag)
152 sync();
153 sleep(3);
154
155 for (i = 1;; ++i) {
156 if (kill(-1, SIGKILL) == -1) {
157 if (errno == ESRCH)
158 break;
159 goto restart;
160 }
161 if (i > 5) {
162 (void)fprintf(stderr,
163 "WARNING: some process(es) wouldn't die\n");
164 break;
165 }
166 (void)sleep(2 * i);
167 }
168
169 reboot(howto);
170 /* FALLTHROUGH */
171
172 restart:
173 sverrno = errno;
174 err("%s%s", kill(1, SIGHUP) == -1 ? "(can't restart init): " : "",
175 strerror(sverrno));
176 /* NOTREACHED */
177 }
178
179 void
180 usage()
181 {
182 (void)fprintf(stderr, "usage: %s [-nqd]\n", dohalt ? "halt" : "reboot");
183 exit(1);
184 }
185
186 #if __STDC__
187 #include <stdarg.h>
188 #else
189 #include <varargs.h>
190 #endif
191
192 void
193 #if __STDC__
194 err(const char *fmt, ...)
195 #else
196 err(fmt, va_alist)
197 char *fmt;
198 va_dcl
199 #endif
200 {
201 va_list ap;
202 #if __STDC__
203 va_start(ap, fmt);
204 #else
205 va_start(ap);
206 #endif
207 (void)fprintf(stderr, "%s: ", dohalt ? "halt" : "reboot");
208 (void)vfprintf(stderr, fmt, ap);
209 va_end(ap);
210 (void)fprintf(stderr, "\n");
211 exit(1);
212 /* NOTREACHED */
213 }
214