]> git.saurik.com Git - apple/system_cmds.git/blame - reboot.tproj/reboot.c
system_cmds-279.6.tar.gz
[apple/system_cmds.git] / reboot.tproj / reboot.c
CommitLineData
1815bff5
A
1/*
2 * Copyright (c) 1980, 1986, 1993
20e66415 3 * The Regents of the University of California. All rights reserved.
1815bff5
A
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
20e66415
A
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
1815bff5
A
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
20e66415
A
34#ifndef lint
35static const char copyright[] =
36"@(#) Copyright (c) 1980, 1986, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
38#endif /* not lint */
39
40#ifndef lint
41#if 0
42static char sccsid[] = "@(#)reboot.c 8.1 (Berkeley) 6/5/93";
43#endif
44static const char rcsid[] =
45 "$FreeBSD: src/sbin/reboot/reboot.c,v 1.17 2002/10/06 16:24:36 thomas Exp $";
46#endif /* not lint */
47
1815bff5 48#include <sys/reboot.h>
20e66415
A
49#include <sys/types.h>
50#include <sys/sysctl.h>
1815bff5 51#include <signal.h>
20e66415 52#include <err.h>
1815bff5 53#include <errno.h>
20e66415
A
54#include <fcntl.h>
55#include <util.h>
56#include <pwd.h>
1815bff5 57#include <syslog.h>
1815bff5
A
58#include <stdio.h>
59#include <stdlib.h>
60#include <string.h>
20e66415 61#include <unistd.h>
1815bff5 62
20e66415
A
63void usage(void);
64u_int get_pageins(void);
1815bff5
A
65
66int dohalt;
67
68int
20e66415 69main(int argc, char *argv[])
1815bff5 70{
1815bff5 71 struct passwd *pw;
20e66415
A
72 int ch, howto, i, fd, kflag, lflag, nflag, qflag, pflag, sverrno;
73 u_int pageins;
74 char *kernel, *p;
75 const char *user;
1815bff5 76
20e66415 77 if (strstr((p = rindex(*argv, '/')) ? p + 1 : *argv, "halt")) {
1815bff5
A
78 dohalt = 1;
79 howto = RB_HALT;
80 } else
81 howto = 0;
20e66415
A
82 kflag = lflag = nflag = qflag = 0;
83#ifndef __APPLE__
84 while ((ch = getopt(argc, argv, "dk:lnpq")) != -1)
85#else
86 while ((ch = getopt(argc, argv, "k:lnq")) != -1)
87#endif
1815bff5 88 switch(ch) {
20e66415
A
89#ifndef __APPLE__
90 case 'd':
91 howto |= RB_DUMP;
92 break;
93#endif
94 case 'k':
95 kflag = 1;
96 kernel = optarg;
97 break;
98 case 'l':
1815bff5
A
99 lflag = 1;
100 break;
101 case 'n':
102 nflag = 1;
103 howto |= RB_NOSYNC;
104 break;
20e66415
A
105/* -p is irrelevant on OS X. It does that anyway. */
106#ifndef __APPLE__
107 case 'p':
108 pflag = 1;
109 howto |= RB_POWEROFF;
110 break;
111#endif
1815bff5
A
112 case 'q':
113 qflag = 1;
114 break;
115 case '?':
116 default:
117 usage();
118 }
119 argc -= optind;
120 argv += optind;
121
20e66415
A
122#ifndef __APPLE__
123 if ((howto & (RB_DUMP | RB_HALT)) == (RB_DUMP | RB_HALT))
124 errx(1, "cannot dump (-d) when halting; must reboot instead");
125#endif
126 if (geteuid()) {
127 errno = EPERM;
128 err(1, NULL);
129 }
1815bff5
A
130
131 if (qflag) {
132 reboot(howto);
20e66415
A
133 err(1, NULL);
134 }
135
136 if (kflag) {
137 fd = open("/boot/nextboot.conf", O_WRONLY | O_CREAT, 0444);
138 if (fd > -1) {
139 (void)write(fd, "nextboot_enable=\"YES\"\n", 22);
140 (void)write(fd, "kernel=\"", 8L);
141 (void)write(fd, kernel, strlen(kernel));
142 (void)write(fd, "\"\n", 2);
143 close(fd);
144 }
1815bff5
A
145 }
146
147 /* Log the reboot. */
148 if (!lflag) {
149 if ((user = getlogin()) == NULL)
150 user = (pw = getpwuid(getuid())) ?
151 pw->pw_name : "???";
152 if (dohalt) {
153 openlog("halt", 0, LOG_AUTH | LOG_CONS);
154 syslog(LOG_CRIT, "halted by %s", user);
155 } else {
156 openlog("reboot", 0, LOG_AUTH | LOG_CONS);
157 syslog(LOG_CRIT, "rebooted by %s", user);
158 }
159 }
160 logwtmp("~", "shutdown", "");
161
162 /*
163 * Do a sync early on, so disks start transfers while we're off
164 * killing processes. Don't worry about writes done before the
165 * processes die, the reboot system call syncs the disks.
166 */
167 if (!nflag)
168 sync();
169
170 /* Just stop init -- if we fail, we'll restart it. */
171 if (kill(1, SIGTSTP) == -1)
20e66415 172 err(1, "SIGTSTP init");
1815bff5
A
173
174 /* Ignore the SIGHUP we get when our parent shell dies. */
175 (void)signal(SIGHUP, SIG_IGN);
176
20e66415 177#ifndef __APPLE__
1815bff5
A
178 /* Send a SIGTERM first, a chance to save the buffers. */
179 if (kill(-1, SIGTERM) == -1)
20e66415 180 err(1, "SIGTERM processes");
1815bff5
A
181
182 /*
183 * After the processes receive the signal, start the rest of the
184 * buffers on their way. Wait 5 seconds between the SIGTERM and
20e66415
A
185 * the SIGKILL to give everybody a chance. If there is a lot of
186 * paging activity then wait longer, up to a maximum of approx
187 * 60 seconds.
1815bff5
A
188 */
189 sleep(2);
20e66415
A
190 for (i = 0; i < 20; i++) {
191 pageins = get_pageins();
192 if (!nflag)
193 sync();
194 sleep(3);
195 if (get_pageins() == pageins)
196 break;
197 }
1815bff5
A
198
199 for (i = 1;; ++i) {
200 if (kill(-1, SIGKILL) == -1) {
201 if (errno == ESRCH)
202 break;
203 goto restart;
204 }
205 if (i > 5) {
206 (void)fprintf(stderr,
207 "WARNING: some process(es) wouldn't die\n");
208 break;
209 }
210 (void)sleep(2 * i);
211 }
212#endif
20e66415 213
1815bff5
A
214 reboot(howto);
215 /* FALLTHROUGH */
216
217restart:
218 sverrno = errno;
20e66415 219 errx(1, "%s%s", kill(1, SIGHUP) == -1 ? "(can't restart init): " : "",
1815bff5
A
220 strerror(sverrno));
221 /* NOTREACHED */
222}
223
224void
225usage()
226{
20e66415
A
227 (void)fprintf(stderr, "usage: %s [-dnpq] [-k kernel]\n",
228 dohalt ? "halt" : "reboot");
1815bff5
A
229 exit(1);
230}
231
20e66415
A
232u_int
233get_pageins()
1815bff5 234{
20e66415
A
235 u_int pageins;
236 size_t len;
237
238 len = sizeof(pageins);
239 if (sysctlbyname("vm.stats.vm.v_swappgsin", &pageins, &len, NULL, 0)
240 != 0) {
241 warnx("v_swappgsin");
242 return (0);
243 }
244 return pageins;
1815bff5 245}