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