Deleted Added
full compact
reboot.c (112991) reboot.c (114589)
1/*
2 * Copyright (c) 1980, 1986, 1993
3 * The Regents of the University of California. All rights reserved.
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:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
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
1/*
2 * Copyright (c) 1980, 1986, 1993
3 * The Regents of the University of California. All rights reserved.
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:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
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
34#if 0
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
35#ifndef lint
36static const char copyright[] =
37"@(#) Copyright (c) 1980, 1986, 1993\n\
38 The Regents of the University of California. All rights reserved.\n";
39#endif /* not lint */
40
41#ifndef lint
41#if 0
42static char sccsid[] = "@(#)reboot.c 8.1 (Berkeley) 6/5/93";
42static char sccsid[] = "@(#)reboot.c 8.1 (Berkeley) 6/5/93";
43#endif
44static const char rcsid[] =
45 "$FreeBSD: head/sbin/reboot/reboot.c 112991 2003-04-02 22:13:56Z rwatson $";
46#endif /* not lint */
43#endif /* not lint */
44#endif
45#include <sys/cdefs.h>
46__FBSDID("$FreeBSD: head/sbin/reboot/reboot.c 114589 2003-05-03 18:41:59Z obrien $");
47
48#include <sys/reboot.h>
49#include <sys/types.h>
50#include <sys/sysctl.h>
51#include <signal.h>
52#include <err.h>
53#include <errno.h>
54#include <fcntl.h>
55#include <libutil.h>
56#include <pwd.h>
57#include <syslog.h>
58#include <stdio.h>
59#include <stdlib.h>
60#include <string.h>
61#include <unistd.h>
62
63void usage(void);
64u_int get_pageins(void);
65
66int dohalt;
67
68int
69main(int argc, char *argv[])
70{
71 struct passwd *pw;
72 int ch, howto, i, fd, kflag, lflag, nflag, qflag, pflag, sverrno;
73 u_int pageins;
74 char *kernel, *p;
75 const char *user;
76
77 if (strstr((p = rindex(*argv, '/')) ? p + 1 : *argv, "halt")) {
78 dohalt = 1;
79 howto = RB_HALT;
80 } else
81 howto = 0;
82 kflag = lflag = nflag = qflag = 0;
83 while ((ch = getopt(argc, argv, "dk:lnpq")) != -1)
84 switch(ch) {
85 case 'd':
86 howto |= RB_DUMP;
87 break;
88 case 'k':
89 kflag = 1;
90 kernel = optarg;
91 break;
92 case 'l':
93 lflag = 1;
94 break;
95 case 'n':
96 nflag = 1;
97 howto |= RB_NOSYNC;
98 break;
99 case 'p':
100 pflag = 1;
101 howto |= RB_POWEROFF;
102 break;
103 case 'q':
104 qflag = 1;
105 break;
106 case '?':
107 default:
108 usage();
109 }
110 argc -= optind;
111 argv += optind;
112
113 if ((howto & (RB_DUMP | RB_HALT)) == (RB_DUMP | RB_HALT))
114 errx(1, "cannot dump (-d) when halting; must reboot instead");
115 if (geteuid()) {
116 errno = EPERM;
117 err(1, NULL);
118 }
119
120 if (qflag) {
121 reboot(howto);
122 err(1, NULL);
123 }
124
125 if (kflag) {
126 fd = open("/boot/nextboot.conf", O_WRONLY | O_CREAT, 0444);
127 if (fd > -1) {
128 (void)write(fd, "nextboot_enable=\"YES\"\n", 22);
129 (void)write(fd, "kernel=\"", 8L);
130 (void)write(fd, kernel, strlen(kernel));
131 (void)write(fd, "\"\n", 2);
132 close(fd);
133 }
134 }
135
136 /* Log the reboot. */
137 if (!lflag) {
138 if ((user = getlogin()) == NULL)
139 user = (pw = getpwuid(getuid())) ?
140 pw->pw_name : "???";
141 if (dohalt) {
142 openlog("halt", 0, LOG_AUTH | LOG_CONS);
143 syslog(LOG_CRIT, "halted by %s", user);
144 } else {
145 openlog("reboot", 0, LOG_AUTH | LOG_CONS);
146 syslog(LOG_CRIT, "rebooted by %s", user);
147 }
148 }
149 logwtmp("~", "shutdown", "");
150
151 /*
152 * Do a sync early on, so disks start transfers while we're off
153 * killing processes. Don't worry about writes done before the
154 * processes die, the reboot system call syncs the disks.
155 */
156 if (!nflag)
157 sync();
158
159 /* Just stop init -- if we fail, we'll restart it. */
160 if (kill(1, SIGTSTP) == -1)
161 err(1, "SIGTSTP init");
162
163 /* Ignore the SIGHUP we get when our parent shell dies. */
164 (void)signal(SIGHUP, SIG_IGN);
165
166 /* Send a SIGTERM first, a chance to save the buffers. */
167 if (kill(-1, SIGTERM) == -1 && errno != ESRCH)
168 err(1, "SIGTERM processes");
169
170 /*
171 * After the processes receive the signal, start the rest of the
172 * buffers on their way. Wait 5 seconds between the SIGTERM and
173 * the SIGKILL to give everybody a chance. If there is a lot of
174 * paging activity then wait longer, up to a maximum of approx
175 * 60 seconds.
176 */
177 sleep(2);
178 for (i = 0; i < 20; i++) {
179 pageins = get_pageins();
180 if (!nflag)
181 sync();
182 sleep(3);
183 if (get_pageins() == pageins)
184 break;
185 }
186
187 for (i = 1;; ++i) {
188 if (kill(-1, SIGKILL) == -1) {
189 if (errno == ESRCH)
190 break;
191 goto restart;
192 }
193 if (i > 5) {
194 (void)fprintf(stderr,
195 "WARNING: some process(es) wouldn't die\n");
196 break;
197 }
198 (void)sleep(2 * i);
199 }
200
201 reboot(howto);
202 /* FALLTHROUGH */
203
204restart:
205 sverrno = errno;
206 errx(1, "%s%s", kill(1, SIGHUP) == -1 ? "(can't restart init): " : "",
207 strerror(sverrno));
208 /* NOTREACHED */
209}
210
211void
212usage()
213{
214 (void)fprintf(stderr, "usage: %s [-dnpq] [-k kernel]\n",
215 dohalt ? "halt" : "reboot");
216 exit(1);
217}
218
219u_int
220get_pageins()
221{
222 u_int pageins;
223 size_t len;
224
225 len = sizeof(pageins);
226 if (sysctlbyname("vm.stats.vm.v_swappgsin", &pageins, &len, NULL, 0)
227 != 0) {
228 warnx("v_swappgsin");
229 return (0);
230 }
231 return pageins;
232}
47
48#include <sys/reboot.h>
49#include <sys/types.h>
50#include <sys/sysctl.h>
51#include <signal.h>
52#include <err.h>
53#include <errno.h>
54#include <fcntl.h>
55#include <libutil.h>
56#include <pwd.h>
57#include <syslog.h>
58#include <stdio.h>
59#include <stdlib.h>
60#include <string.h>
61#include <unistd.h>
62
63void usage(void);
64u_int get_pageins(void);
65
66int dohalt;
67
68int
69main(int argc, char *argv[])
70{
71 struct passwd *pw;
72 int ch, howto, i, fd, kflag, lflag, nflag, qflag, pflag, sverrno;
73 u_int pageins;
74 char *kernel, *p;
75 const char *user;
76
77 if (strstr((p = rindex(*argv, '/')) ? p + 1 : *argv, "halt")) {
78 dohalt = 1;
79 howto = RB_HALT;
80 } else
81 howto = 0;
82 kflag = lflag = nflag = qflag = 0;
83 while ((ch = getopt(argc, argv, "dk:lnpq")) != -1)
84 switch(ch) {
85 case 'd':
86 howto |= RB_DUMP;
87 break;
88 case 'k':
89 kflag = 1;
90 kernel = optarg;
91 break;
92 case 'l':
93 lflag = 1;
94 break;
95 case 'n':
96 nflag = 1;
97 howto |= RB_NOSYNC;
98 break;
99 case 'p':
100 pflag = 1;
101 howto |= RB_POWEROFF;
102 break;
103 case 'q':
104 qflag = 1;
105 break;
106 case '?':
107 default:
108 usage();
109 }
110 argc -= optind;
111 argv += optind;
112
113 if ((howto & (RB_DUMP | RB_HALT)) == (RB_DUMP | RB_HALT))
114 errx(1, "cannot dump (-d) when halting; must reboot instead");
115 if (geteuid()) {
116 errno = EPERM;
117 err(1, NULL);
118 }
119
120 if (qflag) {
121 reboot(howto);
122 err(1, NULL);
123 }
124
125 if (kflag) {
126 fd = open("/boot/nextboot.conf", O_WRONLY | O_CREAT, 0444);
127 if (fd > -1) {
128 (void)write(fd, "nextboot_enable=\"YES\"\n", 22);
129 (void)write(fd, "kernel=\"", 8L);
130 (void)write(fd, kernel, strlen(kernel));
131 (void)write(fd, "\"\n", 2);
132 close(fd);
133 }
134 }
135
136 /* Log the reboot. */
137 if (!lflag) {
138 if ((user = getlogin()) == NULL)
139 user = (pw = getpwuid(getuid())) ?
140 pw->pw_name : "???";
141 if (dohalt) {
142 openlog("halt", 0, LOG_AUTH | LOG_CONS);
143 syslog(LOG_CRIT, "halted by %s", user);
144 } else {
145 openlog("reboot", 0, LOG_AUTH | LOG_CONS);
146 syslog(LOG_CRIT, "rebooted by %s", user);
147 }
148 }
149 logwtmp("~", "shutdown", "");
150
151 /*
152 * Do a sync early on, so disks start transfers while we're off
153 * killing processes. Don't worry about writes done before the
154 * processes die, the reboot system call syncs the disks.
155 */
156 if (!nflag)
157 sync();
158
159 /* Just stop init -- if we fail, we'll restart it. */
160 if (kill(1, SIGTSTP) == -1)
161 err(1, "SIGTSTP init");
162
163 /* Ignore the SIGHUP we get when our parent shell dies. */
164 (void)signal(SIGHUP, SIG_IGN);
165
166 /* Send a SIGTERM first, a chance to save the buffers. */
167 if (kill(-1, SIGTERM) == -1 && errno != ESRCH)
168 err(1, "SIGTERM processes");
169
170 /*
171 * After the processes receive the signal, start the rest of the
172 * buffers on their way. Wait 5 seconds between the SIGTERM and
173 * the SIGKILL to give everybody a chance. If there is a lot of
174 * paging activity then wait longer, up to a maximum of approx
175 * 60 seconds.
176 */
177 sleep(2);
178 for (i = 0; i < 20; i++) {
179 pageins = get_pageins();
180 if (!nflag)
181 sync();
182 sleep(3);
183 if (get_pageins() == pageins)
184 break;
185 }
186
187 for (i = 1;; ++i) {
188 if (kill(-1, SIGKILL) == -1) {
189 if (errno == ESRCH)
190 break;
191 goto restart;
192 }
193 if (i > 5) {
194 (void)fprintf(stderr,
195 "WARNING: some process(es) wouldn't die\n");
196 break;
197 }
198 (void)sleep(2 * i);
199 }
200
201 reboot(howto);
202 /* FALLTHROUGH */
203
204restart:
205 sverrno = errno;
206 errx(1, "%s%s", kill(1, SIGHUP) == -1 ? "(can't restart init): " : "",
207 strerror(sverrno));
208 /* NOTREACHED */
209}
210
211void
212usage()
213{
214 (void)fprintf(stderr, "usage: %s [-dnpq] [-k kernel]\n",
215 dohalt ? "halt" : "reboot");
216 exit(1);
217}
218
219u_int
220get_pageins()
221{
222 u_int pageins;
223 size_t len;
224
225 len = sizeof(pageins);
226 if (sysctlbyname("vm.stats.vm.v_swappgsin", &pageins, &len, NULL, 0)
227 != 0) {
228 warnx("v_swappgsin");
229 return (0);
230 }
231 return pageins;
232}