Bug Summary

File:data/src/freebsd-head/sbin/reboot/reboot.c
Location:line 117, column 6
Description:Branch condition evaluates to a garbage value

Annotated Source Code

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 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#if 0
31#ifndef lint
32static const char copyright[] =
33"@(#) Copyright (c) 1980, 1986, 1993\n\
34 The Regents of the University of California. All rights reserved.\n";
35#endif /* not lint */
36
37#ifndef lint
38static char sccsid[] = "@(#)reboot.c 8.1 (Berkeley) 6/5/93";
39#endif /* not lint */
40#endif
41#include <sys/cdefs.h>
42__FBSDID("$FreeBSD: head/sbin/reboot/reboot.c 292266 2015-12-15 14:17:07Z smh $")__asm__(".ident\t\"" "$FreeBSD: head/sbin/reboot/reboot.c 292266 2015-12-15 14:17:07Z smh $"
"\"")
;
43
44#include <sys/reboot.h>
45#include <sys/time.h>
46#include <sys/types.h>
47#include <sys/sysctl.h>
48#include <signal.h>
49#include <err.h>
50#include <errno(* __error()).h>
51#include <fcntl.h>
52#include <pwd.h>
53#include <syslog.h>
54#include <stdio.h>
55#include <stdlib.h>
56#include <string.h>
57#include <unistd.h>
58#include <utmpx.h>
59
60static void usage(void);
61static u_int get_pageins(void);
62
63static int dohalt;
64
65int
66main(int argc, char *argv[])
67{
68 struct utmpx utx;
69 const struct passwd *pw;
70 int ch, howto, i, fd, lflag, nflag, qflag, sverrno, Nflag;
1
'Nflag' declared without an initial value
71 u_int pageins;
72 const char *user, *kernel = NULL((void *)0);
73
74 if (strcmp(getprogname(), "halt") == 0) {
2
Taking false branch
75 dohalt = 1;
76 howto = RB_HALT0x008;
77 } else
78 howto = 0;
79 lflag = nflag = qflag = 0;
80 while ((ch = getopt(argc, argv, "dk:lNnpqr")) != -1)
3
Loop condition is false. Execution continues on line 112
81 switch(ch) {
82 case 'd':
83 howto |= RB_DUMP0x100;
84 break;
85 case 'k':
86 kernel = optarg;
87 break;
88 case 'l':
89 lflag = 1;
90 break;
91 case 'n':
92 nflag = 1;
93 howto |= RB_NOSYNC0x004;
94 break;
95 case 'N':
96 nflag = 1;
97 Nflag = 1;
98 break;
99 case 'p':
100 howto |= RB_POWEROFF0x4000;
101 break;
102 case 'q':
103 qflag = 1;
104 break;
105 case 'r':
106 howto |= RB_REROOT0x200000;
107 break;
108 case '?':
109 default:
110 usage();
111 }
112 argc -= optind;
113 argv += optind;
114
115 if ((howto & (RB_DUMP0x100 | RB_HALT0x008)) == (RB_DUMP0x100 | RB_HALT0x008))
4
Taking false branch
116 errx(1, "cannot dump (-d) when halting; must reboot instead");
117 if (Nflag && (howto & RB_NOSYNC0x004) != 0)
5
Branch condition evaluates to a garbage value
118 errx(1, "-N cannot be used with -n");
119 if ((howto & RB_REROOT0x200000) != 0 && howto != RB_REROOT0x200000)
120 errx(1, "-r cannot be used with -d, -n, or -p");
121 if (geteuid()) {
122 errno(* __error()) = EPERM1;
123 err(1, NULL((void *)0));
124 }
125
126 if (qflag) {
127 reboot(howto);
128 err(1, NULL((void *)0));
129 }
130
131 if (kernel != NULL((void *)0)) {
132 fd = open("/boot/nextboot.conf", O_WRONLY0x0001 | O_CREAT0x0200 | O_TRUNC0x0400,
133 0444);
134 if (fd > -1) {
135 (void)write(fd, "nextboot_enable=\"YES\"\n", 22);
136 (void)write(fd, "kernel=\"", 8L);
137 (void)write(fd, kernel, strlen(kernel));
138 (void)write(fd, "\"\n", 2);
139 close(fd);
140 }
141 }
142
143 /* Log the reboot. */
144 if (!lflag) {
145 if ((user = getlogin()) == NULL((void *)0))
146 user = (pw = getpwuid(getuid())) ?
147 pw->pw_name : "???";
148 if (dohalt) {
149 openlog("halt", 0, LOG_AUTH(4<<3) | LOG_CONS0x02);
150 syslog(LOG_CRIT2, "halted by %s", user);
151 } else if (howto & RB_REROOT0x200000) {
152 openlog("reroot", 0, LOG_AUTH(4<<3) | LOG_CONS0x02);
153 syslog(LOG_CRIT2, "rerooted by %s", user);
154 } else {
155 openlog("reboot", 0, LOG_AUTH(4<<3) | LOG_CONS0x02);
156 syslog(LOG_CRIT2, "rebooted by %s", user);
157 }
158 }
159 utx.ut_type = SHUTDOWN_TIME8;
160 gettimeofday(&utx.ut_tv, NULL((void *)0));
161 pututxline(&utx);
162
163 /*
164 * Do a sync early on, so disks start transfers while we're off
165 * killing processes. Don't worry about writes done before the
166 * processes die, the reboot system call syncs the disks.
167 */
168 if (!nflag)
169 sync();
170
171 /*
172 * Ignore signals that we can get as a result of killing
173 * parents, group leaders, etc.
174 */
175 (void)signal(SIGHUP1, SIG_IGN((__sighandler_t *)1));
176 (void)signal(SIGINT2, SIG_IGN((__sighandler_t *)1));
177 (void)signal(SIGQUIT3, SIG_IGN((__sighandler_t *)1));
178 (void)signal(SIGTERM15, SIG_IGN((__sighandler_t *)1));
179 (void)signal(SIGTSTP18, SIG_IGN((__sighandler_t *)1));
180
181 /*
182 * If we're running in a pipeline, we don't want to die
183 * after killing whatever we're writing to.
184 */
185 (void)signal(SIGPIPE13, SIG_IGN((__sighandler_t *)1));
186
187 /*
188 * Only init(8) can perform rerooting.
189 */
190 if (howto & RB_REROOT0x200000) {
191 if (kill(1, SIGEMT7) == -1)
192 err(1, "SIGEMT init");
193
194 return (0);
195 }
196
197 /* Just stop init -- if we fail, we'll restart it. */
198 if (kill(1, SIGTSTP18) == -1)
199 err(1, "SIGTSTP init");
200
201 /* Send a SIGTERM first, a chance to save the buffers. */
202 if (kill(-1, SIGTERM15) == -1 && errno(* __error()) != ESRCH3)
203 err(1, "SIGTERM processes");
204
205 /*
206 * After the processes receive the signal, start the rest of the
207 * buffers on their way. Wait 5 seconds between the SIGTERM and
208 * the SIGKILL to give everybody a chance. If there is a lot of
209 * paging activity then wait longer, up to a maximum of approx
210 * 60 seconds.
211 */
212 sleep(2);
213 for (i = 0; i < 20; i++) {
214 pageins = get_pageins();
215 if (!nflag)
216 sync();
217 sleep(3);
218 if (get_pageins() == pageins)
219 break;
220 }
221
222 for (i = 1;; ++i) {
223 if (kill(-1, SIGKILL9) == -1) {
224 if (errno(* __error()) == ESRCH3)
225 break;
226 goto restart;
227 }
228 if (i > 5) {
229 (void)fprintf(stderr__stderrp,
230 "WARNING: some process(es) wouldn't die\n");
231 break;
232 }
233 (void)sleep(2 * i);
234 }
235
236 reboot(howto);
237 /* FALLTHROUGH */
238
239restart:
240 sverrno = errno(* __error());
241 errx(1, "%s%s", kill(1, SIGHUP1) == -1 ? "(can't restart init): " : "",
242 strerror(sverrno));
243 /* NOTREACHED */
244}
245
246static void
247usage(void)
248{
249
250 (void)fprintf(stderr__stderrp, dohalt ?
251 "usage: halt [-lnpq] [-k kernel]\n" :
252 "usage: reboot [-dlnpq] [-k kernel]\n");
253 exit(1);
254}
255
256static u_int
257get_pageins(void)
258{
259 u_int pageins;
260 size_t len;
261
262 len = sizeof(pageins);
263 if (sysctlbyname("vm.stats.vm.v_swappgsin", &pageins, &len, NULL((void *)0), 0)
264 != 0) {
265 warnx("v_swappgsin");
266 return (0);
267 }
268 return pageins;
269}