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