]>
Commit | Line | Data |
---|---|---|
1815bff5 A |
1 | /* |
2 | * Copyright (c) 1988, 1990, 1993 | |
20e66415 | 3 | * The Regents of the University of California. All rights reserved. |
34d340d7 | 4 | * Portions copyright (c) 2007 Apple Inc. All rights reserved. |
1815bff5 A |
5 | * |
6 | * Redistribution and use in source and binary forms, with or without | |
7 | * modification, are permitted provided that the following conditions | |
8 | * are met: | |
9 | * 1. Redistributions of source code must retain the above copyright | |
10 | * notice, this list of conditions and the following disclaimer. | |
11 | * 2. Redistributions in binary form must reproduce the above copyright | |
12 | * notice, this list of conditions and the following disclaimer in the | |
13 | * documentation and/or other materials provided with the distribution. | |
34d340d7 | 14 | * 3. Neither the name of the University nor the names of its contributors |
1815bff5 A |
15 | * may be used to endorse or promote products derived from this software |
16 | * without specific prior written permission. | |
17 | * | |
18 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
19 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
24 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
26 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
27 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
28 | * SUCH DAMAGE. | |
29 | */ | |
30 | ||
34d340d7 | 31 | #if 0 |
20e66415 A |
32 | #ifndef lint |
33 | static const char copyright[] = | |
34 | "@(#) Copyright (c) 1988, 1990, 1993\n\ | |
35 | The Regents of the University of California. All rights reserved.\n"; | |
36 | #endif /* not lint */ | |
37 | ||
38 | #ifndef lint | |
20e66415 | 39 | static char sccsid[] = "@(#)shutdown.c 8.4 (Berkeley) 4/28/95"; |
20e66415 | 40 | #endif /* not lint */ |
34d340d7 A |
41 | #endif |
42 | #include <sys/cdefs.h> | |
43 | #ifndef __APPLE__ | |
44 | __FBSDID("$FreeBSD: src/sbin/shutdown/shutdown.c,v 1.28 2005/01/25 08:40:51 delphij Exp $"); | |
45 | #endif | |
20e66415 | 46 | |
1815bff5 A |
47 | #include <sys/param.h> |
48 | #include <sys/time.h> | |
49 | #include <sys/resource.h> | |
50 | #include <sys/syslog.h> | |
51 | ||
52 | #include <ctype.h> | |
20e66415 | 53 | #include <err.h> |
1815bff5 | 54 | #include <fcntl.h> |
34d340d7 | 55 | #include <paths.h> |
1815bff5 A |
56 | #include <pwd.h> |
57 | #include <setjmp.h> | |
58 | #include <signal.h> | |
59 | #include <stdio.h> | |
60 | #include <stdlib.h> | |
61 | #include <string.h> | |
1815bff5 A |
62 | #include <unistd.h> |
63 | ||
34d340d7 | 64 | #ifdef __APPLE__ |
2fc1e207 | 65 | #include <errno.h> |
34d340d7 | 66 | #include <util.h> |
2fc1e207 A |
67 | #include <bsm/libbsm.h> |
68 | #include <bsm/audit_uevents.h> | |
c0bbac3a A |
69 | #include <sys/types.h> |
70 | #include <sys/sysctl.h> | |
ef8ad44b A |
71 | #include <vproc.h> |
72 | #include <vproc_priv.h> | |
2fc1e207 | 73 | |
b180c247 A |
74 | #include "kextmanager.h" |
75 | #include <IOKit/kext/kextmanager_types.h> | |
34d340d7 | 76 | #include <IOKit/pwr_mgt/IOPMLib.h> |
b180c247 A |
77 | #include <mach/mach_port.h> // allocate |
78 | #include <mach/mach.h> // task_self, etc | |
79 | #include <servers/bootstrap.h> // bootstrap | |
fc6d9e4b | 80 | #include <bootstrap_priv.h> |
34d340d7 | 81 | #include <reboot2.h> |
ef8ad44b | 82 | #include <utmpx.h> |
2fc1e207 | 83 | |
1815bff5 | 84 | #include "pathnames.h" |
34d340d7 | 85 | #endif /* __APPLE__ */ |
20e66415 | 86 | |
1815bff5 A |
87 | #ifdef DEBUG |
88 | #undef _PATH_NOLOGIN | |
89 | #define _PATH_NOLOGIN "./nologin" | |
1815bff5 A |
90 | #endif |
91 | ||
92 | #define H *60*60 | |
93 | #define M *60 | |
94 | #define S *1 | |
95 | #define NOLOG_TIME 5*60 | |
96 | struct interval { | |
97 | int timeleft, timetowait; | |
98 | } tlist[] = { | |
20e66415 A |
99 | { 10 H, 5 H }, |
100 | { 5 H, 3 H }, | |
101 | { 2 H, 1 H }, | |
102 | { 1 H, 30 M }, | |
103 | { 30 M, 10 M }, | |
104 | { 20 M, 10 M }, | |
105 | { 10 M, 5 M }, | |
106 | { 5 M, 3 M }, | |
107 | { 2 M, 1 M }, | |
108 | { 1 M, 30 S }, | |
109 | { 30 S, 30 S }, | |
110 | { 0 , 0 } | |
1815bff5 A |
111 | }; |
112 | #undef H | |
113 | #undef M | |
114 | #undef S | |
115 | ||
116 | static time_t offset, shuttime; | |
34d340d7 | 117 | #ifdef __APPLE__ |
cf37c299 A |
118 | static int dohalt, doreboot, doups, killflg, oflag; |
119 | static size_t mbuflen; | |
34d340d7 A |
120 | #else |
121 | static int dohalt, dopower, doreboot, killflg, mbuflen, oflag; | |
122 | #endif | |
20e66415 A |
123 | static char mbuf[BUFSIZ]; |
124 | static const char *nosync, *whom; | |
34d340d7 A |
125 | #ifdef __APPLE__ |
126 | static int dosleep; | |
127 | #endif | |
20e66415 A |
128 | |
129 | void badtime(void); | |
83f6dbe8 A |
130 | #ifdef __APPLE__ |
131 | void log_and_exec_reboot_or_halt(void); | |
132 | #else | |
20e66415 | 133 | void die_you_gravy_sucking_pig_dog(void); |
83f6dbe8 | 134 | #endif |
20e66415 A |
135 | void finish(int); |
136 | void getoffset(char *); | |
137 | void loop(void); | |
138 | void nolog(void); | |
139 | void timeout(int); | |
cf37c299 | 140 | void timewarn(time_t); |
20e66415 | 141 | void usage(const char *); |
b180c247 | 142 | #ifdef __APPLE__ |
34d340d7 | 143 | int audit_shutdown(int); |
b180c247 A |
144 | int reserve_reboot(void); |
145 | #endif | |
1815bff5 | 146 | |
34d340d7 A |
147 | extern const char **environ; |
148 | ||
1815bff5 | 149 | int |
34d340d7 | 150 | main(int argc, char **argv) |
1815bff5 | 151 | { |
20e66415 | 152 | char *p, *endp; |
1815bff5 | 153 | struct passwd *pw; |
cf37c299 A |
154 | size_t arglen; |
155 | int ch, len, readstdin; | |
1815bff5 A |
156 | |
157 | #ifndef DEBUG | |
20e66415 A |
158 | if (geteuid()) |
159 | errx(1, "NOT super-user"); | |
1815bff5 A |
160 | #endif |
161 | nosync = NULL; | |
162 | readstdin = 0; | |
20e66415 A |
163 | #ifndef __APPLE__ |
164 | while ((ch = getopt(argc, argv, "-hknopr")) != -1) | |
165 | #else | |
34d340d7 | 166 | while ((ch = getopt(argc, argv, "-hknorsu")) != -1) |
20e66415 | 167 | #endif |
1815bff5 A |
168 | switch (ch) { |
169 | case '-': | |
170 | readstdin = 1; | |
171 | break; | |
1815bff5 A |
172 | case 'h': |
173 | dohalt = 1; | |
174 | break; | |
175 | case 'k': | |
176 | killflg = 1; | |
177 | break; | |
178 | case 'n': | |
179 | nosync = "-n"; | |
180 | break; | |
20e66415 A |
181 | case 'o': |
182 | oflag = 1; | |
183 | break; | |
184 | #ifndef __APPLE__ | |
185 | case 'p': | |
186 | dopower = 1; | |
187 | break; | |
188 | #endif | |
b180c247 A |
189 | case 'u': |
190 | doups = 1; | |
191 | break; | |
1815bff5 A |
192 | case 'r': |
193 | doreboot = 1; | |
194 | break; | |
34d340d7 A |
195 | #ifdef __APPLE__ |
196 | case 's': | |
197 | dosleep = 1; | |
198 | break; | |
199 | #endif | |
1815bff5 A |
200 | case '?': |
201 | default: | |
20e66415 | 202 | usage((char *)NULL); |
1815bff5 A |
203 | } |
204 | argc -= optind; | |
205 | argv += optind; | |
206 | ||
207 | if (argc < 1) | |
20e66415 A |
208 | usage((char *)NULL); |
209 | ||
210 | #ifndef __APPLE__ | |
211 | if (killflg + doreboot + dohalt + dopower > 1) | |
212 | usage("incompatible switches -h, -k, -p and -r"); | |
213 | ||
214 | if (oflag && !(dohalt || dopower || doreboot)) | |
215 | usage("-o requires -h, -p or -r"); | |
20e66415 | 216 | |
34d340d7 A |
217 | if (nosync != NULL && !oflag) |
218 | usage("-n requires -o"); | |
219 | #else /* !__APPLE__ */ | |
220 | if (killflg + doreboot + dohalt + dosleep > 1) | |
221 | usage("incompatible switches -h, -k, -r, and -s"); | |
222 | ||
223 | if (!(dohalt || doreboot || dosleep || killflg)) | |
224 | usage("-h, -r, -s, or -k is required"); | |
cf37c299 | 225 | |
b180c247 A |
226 | if (doups && !dohalt) |
227 | usage("-u requires -h"); | |
34d340d7 | 228 | #endif /* !__APPLE__ */ |
1815bff5 | 229 | |
1815bff5 A |
230 | getoffset(*argv++); |
231 | ||
232 | if (*argv) { | |
233 | for (p = mbuf, len = sizeof(mbuf); *argv; ++argv) { | |
234 | arglen = strlen(*argv); | |
235 | if ((len -= arglen) <= 2) | |
236 | break; | |
237 | if (p != mbuf) | |
238 | *p++ = ' '; | |
239 | memmove(p, *argv, arglen); | |
240 | p += arglen; | |
241 | } | |
242 | *p = '\n'; | |
243 | *++p = '\0'; | |
244 | } | |
245 | ||
246 | if (readstdin) { | |
247 | p = mbuf; | |
248 | endp = mbuf + sizeof(mbuf) - 2; | |
249 | for (;;) { | |
cf37c299 | 250 | if (!fgets(p, (int)(endp - p + 1), stdin)) |
1815bff5 A |
251 | break; |
252 | for (; *p && p < endp; ++p); | |
253 | if (p == endp) { | |
254 | *p = '\n'; | |
255 | *++p = '\0'; | |
256 | break; | |
257 | } | |
258 | } | |
259 | } | |
260 | mbuflen = strlen(mbuf); | |
261 | ||
262 | if (offset) | |
263 | (void)printf("Shutdown at %.24s.\n", ctime(&shuttime)); | |
264 | else | |
265 | (void)printf("Shutdown NOW!\n"); | |
266 | ||
267 | if (!(whom = getlogin())) | |
268 | whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???"; | |
269 | ||
270 | #ifdef DEBUG | |
2fc1e207 | 271 | audit_shutdown(0); |
1815bff5 A |
272 | (void)putc('\n', stdout); |
273 | #else | |
274 | (void)setpriority(PRIO_PROCESS, 0, PRIO_MIN); | |
34d340d7 A |
275 | #ifdef __APPLE__ |
276 | if (offset) { | |
277 | #else | |
1815bff5 | 278 | { |
34d340d7 | 279 | #endif |
1815bff5 A |
280 | int forkpid; |
281 | ||
282 | forkpid = fork(); | |
2fc1e207 A |
283 | if (forkpid == -1) { |
284 | audit_shutdown(1); | |
20e66415 | 285 | err(1, "fork"); |
2fc1e207 | 286 | } |
34d340d7 | 287 | if (forkpid) |
20e66415 | 288 | errx(0, "[pid %d]", forkpid); |
ef8ad44b A |
289 | #ifdef __APPLE__ |
290 | /* 5863185: reboot2() needs to talk to launchd. */ | |
291 | if (_vprocmgr_detach_from_console(0) != NULL) | |
292 | warnx("can't detach from console"); | |
293 | #endif /* __APPLE__ */ | |
1815bff5 | 294 | } |
2fc1e207 | 295 | audit_shutdown(0); |
20e66415 | 296 | setsid(); |
1815bff5 A |
297 | #endif |
298 | openlog("shutdown", LOG_CONS, LOG_AUTH); | |
299 | loop(); | |
20e66415 | 300 | return(0); |
1815bff5 A |
301 | } |
302 | ||
303 | void | |
cf37c299 | 304 | loop(void) |
1815bff5 A |
305 | { |
306 | struct interval *tp; | |
307 | u_int sltime; | |
308 | int logged; | |
309 | ||
310 | if (offset <= NOLOG_TIME) { | |
311 | logged = 1; | |
312 | nolog(); | |
313 | } | |
314 | else | |
315 | logged = 0; | |
316 | tp = tlist; | |
317 | if (tp->timeleft < offset) | |
318 | (void)sleep((u_int)(offset - tp->timeleft)); | |
319 | else { | |
20e66415 | 320 | while (tp->timeleft && offset < tp->timeleft) |
1815bff5 A |
321 | ++tp; |
322 | /* | |
323 | * Warn now, if going to sleep more than a fifth of | |
324 | * the next wait time. | |
325 | */ | |
cf37c299 A |
326 | if ((sltime = (u_int)(offset - tp->timeleft))) { |
327 | if (sltime > (tp->timetowait / 5)) | |
1815bff5 A |
328 | timewarn(offset); |
329 | (void)sleep(sltime); | |
330 | } | |
331 | } | |
332 | for (;; ++tp) { | |
333 | timewarn(tp->timeleft); | |
334 | if (!logged && tp->timeleft <= NOLOG_TIME) { | |
335 | logged = 1; | |
336 | nolog(); | |
337 | } | |
338 | (void)sleep((u_int)tp->timetowait); | |
339 | if (!tp->timeleft) | |
340 | break; | |
341 | } | |
83f6dbe8 A |
342 | #ifdef __APPLE__ |
343 | log_and_exec_reboot_or_halt(); | |
344 | #else | |
1815bff5 | 345 | die_you_gravy_sucking_pig_dog(); |
83f6dbe8 | 346 | #endif |
1815bff5 A |
347 | } |
348 | ||
349 | static jmp_buf alarmbuf; | |
350 | ||
20e66415 A |
351 | static const char *restricted_environ[] = { |
352 | "PATH=" _PATH_STDPATH, | |
353 | NULL | |
354 | }; | |
355 | ||
1815bff5 | 356 | void |
cf37c299 | 357 | timewarn(time_t timeleft) |
1815bff5 A |
358 | { |
359 | static int first; | |
360 | static char hostname[MAXHOSTNAMELEN + 1]; | |
361 | FILE *pf; | |
362 | char wcmd[MAXPATHLEN + 4]; | |
34d340d7 A |
363 | |
364 | /* wall is sometimes missing, e.g. on install media */ | |
365 | if (access(_PATH_WALL, X_OK) == -1) return; | |
1815bff5 A |
366 | |
367 | if (!first++) | |
368 | (void)gethostname(hostname, sizeof(hostname)); | |
369 | ||
370 | /* undoc -n option to wall suppresses normal wall banner */ | |
371 | (void)snprintf(wcmd, sizeof(wcmd), "%s -n", _PATH_WALL); | |
20e66415 | 372 | environ = restricted_environ; |
1815bff5 A |
373 | if (!(pf = popen(wcmd, "w"))) { |
374 | syslog(LOG_ERR, "shutdown: can't find %s: %m", _PATH_WALL); | |
375 | return; | |
376 | } | |
377 | ||
378 | (void)fprintf(pf, | |
379 | "\007*** %sSystem shutdown message from %s@%s ***\007\n", | |
380 | timeleft ? "": "FINAL ", whom, hostname); | |
381 | ||
382 | if (timeleft > 10*60) | |
383 | (void)fprintf(pf, "System going down at %5.5s\n\n", | |
384 | ctime(&shuttime) + 11); | |
385 | else if (timeleft > 59) | |
cf37c299 | 386 | (void)fprintf(pf, "System going down in %ld minute%s\n\n", |
1815bff5 A |
387 | timeleft / 60, (timeleft > 60) ? "s" : ""); |
388 | else if (timeleft) | |
389 | (void)fprintf(pf, "System going down in 30 seconds\n\n"); | |
390 | else | |
391 | (void)fprintf(pf, "System going down IMMEDIATELY\n\n"); | |
392 | ||
393 | if (mbuflen) | |
394 | (void)fwrite(mbuf, sizeof(*mbuf), mbuflen, pf); | |
395 | ||
396 | /* | |
397 | * play some games, just in case wall doesn't come back | |
20e66415 | 398 | * probably unnecessary, given that wall is careful. |
1815bff5 A |
399 | */ |
400 | if (!setjmp(alarmbuf)) { | |
401 | (void)signal(SIGALRM, timeout); | |
402 | (void)alarm((u_int)30); | |
403 | (void)pclose(pf); | |
404 | (void)alarm((u_int)0); | |
405 | (void)signal(SIGALRM, SIG_DFL); | |
406 | } | |
407 | } | |
408 | ||
409 | void | |
34d340d7 | 410 | timeout(int signo __unused) |
1815bff5 A |
411 | { |
412 | longjmp(alarmbuf, 1); | |
413 | } | |
414 | ||
415 | void | |
83f6dbe8 A |
416 | #ifdef __APPLE__ |
417 | log_and_exec_reboot_or_halt() | |
418 | #else | |
1815bff5 | 419 | die_you_gravy_sucking_pig_dog() |
83f6dbe8 | 420 | #endif |
1815bff5 | 421 | { |
34d340d7 | 422 | #ifndef __APPLE__ |
20e66415 | 423 | char *empty_environ[] = { NULL }; |
34d340d7 | 424 | #else |
ef8ad44b A |
425 | if ((errno = reserve_reboot())) { |
426 | warn("couldn't lock for reboot"); | |
427 | finish(0); | |
428 | } | |
b180c247 A |
429 | #endif |
430 | ||
431 | syslog(LOG_NOTICE, "%s%s by %s: %s", | |
20e66415 | 432 | #ifndef __APPLE__ |
cf37c299 | 433 | doreboot ? "reboot" : dohalt ? "halt" : dopower ? "power-down" : |
20e66415 | 434 | #else |
34d340d7 | 435 | doreboot ? "reboot" : dohalt ? "halt" : dosleep ? "sleep" : |
20e66415 | 436 | #endif |
34d340d7 | 437 | "shutdown", doups?" with UPS delay":"", whom, mbuf); |
83f6dbe8 | 438 | #ifndef __APPLE__ |
1815bff5 | 439 | (void)sleep(2); |
83f6dbe8 | 440 | #endif |
1815bff5 A |
441 | |
442 | (void)printf("\r\nSystem shutdown time has arrived\007\007\r\n"); | |
443 | if (killflg) { | |
444 | (void)printf("\rbut you'll have to do it yourself\r\n"); | |
20e66415 | 445 | exit(0); |
1815bff5 | 446 | } |
1815bff5 A |
447 | #ifdef DEBUG |
448 | if (doreboot) | |
449 | (void)printf("reboot"); | |
450 | else if (dohalt) | |
451 | (void)printf("halt"); | |
20e66415 A |
452 | #ifndef __APPLE__ |
453 | else if (dopower) | |
454 | (void)printf("power-down"); | |
20e66415 | 455 | if (nosync != NULL) |
1815bff5 | 456 | (void)printf(" no sync"); |
34d340d7 A |
457 | #else |
458 | else if (dosleep) | |
459 | (void)printf("sleep"); | |
460 | #endif | |
1815bff5 A |
461 | (void)printf("\nkill -HUP 1\n"); |
462 | #else | |
2fc1e207 | 463 | #ifdef __APPLE__ |
34d340d7 A |
464 | if (dosleep) { |
465 | mach_port_t mp; | |
466 | io_connect_t fb; | |
467 | kern_return_t kr = IOMasterPort(bootstrap_port, &mp); | |
468 | if (kr == kIOReturnSuccess) { | |
469 | fb = IOPMFindPowerManagement(mp); | |
470 | if (fb != IO_OBJECT_NULL) { | |
471 | IOReturn err = IOPMSleepSystem(fb); | |
472 | if (err != kIOReturnSuccess) { | |
473 | fprintf(stderr, "shutdown: sleep failed (0x%08x)\n", err); | |
474 | kr = -1; | |
475 | } | |
476 | } | |
477 | } | |
34d340d7 A |
478 | } else { |
479 | int howto = 0; | |
480 | ||
cf37c299 | 481 | #if defined(__APPLE__) |
ef8ad44b A |
482 | { |
483 | struct utmpx utx; | |
484 | bzero(&utx, sizeof(utx)); | |
485 | utx.ut_type = SHUTDOWN_TIME; | |
486 | gettimeofday(&utx.ut_tv, NULL); | |
487 | pututxline(&utx); | |
ef8ad44b A |
488 | } |
489 | #else | |
34d340d7 | 490 | logwtmp("~", "shutdown", ""); |
ef8ad44b | 491 | #endif |
34d340d7 A |
492 | |
493 | if (dohalt) howto |= RB_HALT; | |
494 | if (doups) howto |= RB_UPSDELAY; | |
495 | if (nosync) howto |= RB_NOSYNC; | |
496 | ||
497 | // launchd(8) handles reboot. This call returns NULL on success. | |
cf37c299 | 498 | if (reboot3(howto)) { |
ef8ad44b A |
499 | syslog(LOG_ERR, "shutdown: launchd reboot failed."); |
500 | } | |
2fc1e207 | 501 | } |
34d340d7 | 502 | #else /* __APPLE__ */ |
20e66415 A |
503 | if (!oflag) { |
504 | (void)kill(1, doreboot ? SIGINT : /* reboot */ | |
505 | dohalt ? SIGUSR1 : /* halt */ | |
20e66415 | 506 | dopower ? SIGUSR2 : /* power-down */ |
20e66415 A |
507 | SIGTERM); /* single-user */ |
508 | } else { | |
509 | if (doreboot) { | |
cf37c299 | 510 | execle(_PATH_REBOOT, "reboot", "-l", nosync, |
20e66415 A |
511 | (char *)NULL, empty_environ); |
512 | syslog(LOG_ERR, "shutdown: can't exec %s: %m.", | |
513 | _PATH_REBOOT); | |
514 | warn(_PATH_REBOOT); | |
515 | } | |
516 | else if (dohalt) { | |
34d340d7 | 517 | execle(_PATH_HALT, "halt", "-l", nosync, |
20e66415 A |
518 | (char *)NULL, empty_environ); |
519 | syslog(LOG_ERR, "shutdown: can't exec %s: %m.", | |
520 | _PATH_HALT); | |
521 | warn(_PATH_HALT); | |
522 | } | |
20e66415 A |
523 | else if (dopower) { |
524 | execle(_PATH_HALT, "halt", "-l", "-p", nosync, | |
525 | (char *)NULL, empty_environ); | |
526 | syslog(LOG_ERR, "shutdown: can't exec %s: %m.", | |
527 | _PATH_HALT); | |
528 | warn(_PATH_HALT); | |
529 | } | |
20e66415 | 530 | (void)kill(1, SIGTERM); /* to single-user */ |
1815bff5 | 531 | } |
34d340d7 | 532 | #endif /* __APPLE__ */ |
1815bff5 A |
533 | #endif |
534 | finish(0); | |
535 | } | |
536 | ||
537 | #define ATOI2(p) (p[0] - '0') * 10 + (p[1] - '0'); p += 2; | |
538 | ||
539 | void | |
34d340d7 | 540 | getoffset(char *timearg) |
1815bff5 | 541 | { |
20e66415 A |
542 | struct tm *lt; |
543 | char *p; | |
1815bff5 | 544 | time_t now; |
20e66415 A |
545 | int this_year; |
546 | ||
547 | (void)time(&now); | |
1815bff5 A |
548 | |
549 | if (!strcasecmp(timearg, "now")) { /* now */ | |
550 | offset = 0; | |
20e66415 | 551 | shuttime = now; |
1815bff5 A |
552 | return; |
553 | } | |
554 | ||
1815bff5 A |
555 | if (*timearg == '+') { /* +minutes */ |
556 | if (!isdigit(*++timearg)) | |
557 | badtime(); | |
20e66415 A |
558 | if ((offset = atoi(timearg) * 60) < 0) |
559 | badtime(); | |
1815bff5 A |
560 | shuttime = now + offset; |
561 | return; | |
562 | } | |
563 | ||
564 | /* handle hh:mm by getting rid of the colon */ | |
565 | for (p = timearg; *p; ++p) | |
20e66415 | 566 | if (!isascii(*p) || !isdigit(*p)) { |
1815bff5 A |
567 | if (*p == ':' && strlen(p) == 3) { |
568 | p[0] = p[1]; | |
569 | p[1] = p[2]; | |
570 | p[2] = '\0'; | |
571 | } | |
572 | else | |
573 | badtime(); | |
20e66415 | 574 | } |
1815bff5 A |
575 | |
576 | unsetenv("TZ"); /* OUR timezone */ | |
577 | lt = localtime(&now); /* current time val */ | |
578 | ||
579 | switch(strlen(timearg)) { | |
580 | case 10: | |
20e66415 | 581 | this_year = lt->tm_year; |
1815bff5 | 582 | lt->tm_year = ATOI2(timearg); |
20e66415 A |
583 | /* |
584 | * check if the specified year is in the next century. | |
585 | * allow for one year of user error as many people will | |
586 | * enter n - 1 at the start of year n. | |
587 | */ | |
588 | if (lt->tm_year < (this_year % 100) - 1) | |
589 | lt->tm_year += 100; | |
590 | /* adjust for the year 2000 and beyond */ | |
591 | lt->tm_year += (this_year - (this_year % 100)); | |
1815bff5 A |
592 | /* FALLTHROUGH */ |
593 | case 8: | |
594 | lt->tm_mon = ATOI2(timearg); | |
595 | if (--lt->tm_mon < 0 || lt->tm_mon > 11) | |
596 | badtime(); | |
597 | /* FALLTHROUGH */ | |
598 | case 6: | |
599 | lt->tm_mday = ATOI2(timearg); | |
600 | if (lt->tm_mday < 1 || lt->tm_mday > 31) | |
601 | badtime(); | |
602 | /* FALLTHROUGH */ | |
603 | case 4: | |
604 | lt->tm_hour = ATOI2(timearg); | |
605 | if (lt->tm_hour < 0 || lt->tm_hour > 23) | |
606 | badtime(); | |
607 | lt->tm_min = ATOI2(timearg); | |
608 | if (lt->tm_min < 0 || lt->tm_min > 59) | |
609 | badtime(); | |
610 | lt->tm_sec = 0; | |
611 | if ((shuttime = mktime(lt)) == -1) | |
612 | badtime(); | |
20e66415 A |
613 | if ((offset = shuttime - now) < 0) |
614 | errx(1, "that time is already past."); | |
1815bff5 A |
615 | break; |
616 | default: | |
617 | badtime(); | |
618 | } | |
619 | } | |
620 | ||
1815bff5 A |
621 | #define NOMSG "\n\nNO LOGINS: System going down at " |
622 | void | |
cf37c299 | 623 | nolog(void) |
1815bff5 A |
624 | { |
625 | int logfd; | |
626 | char *ct; | |
627 | ||
628 | (void)unlink(_PATH_NOLOGIN); /* in case linked to another file */ | |
629 | (void)signal(SIGINT, finish); | |
630 | (void)signal(SIGHUP, finish); | |
631 | (void)signal(SIGQUIT, finish); | |
632 | (void)signal(SIGTERM, finish); | |
633 | if ((logfd = open(_PATH_NOLOGIN, O_WRONLY|O_CREAT|O_TRUNC, | |
634 | 0664)) >= 0) { | |
635 | (void)write(logfd, NOMSG, sizeof(NOMSG) - 1); | |
636 | ct = ctime(&shuttime); | |
637 | (void)write(logfd, ct + 11, 5); | |
638 | (void)write(logfd, "\n\n", 2); | |
639 | (void)write(logfd, mbuf, strlen(mbuf)); | |
640 | (void)close(logfd); | |
641 | } | |
642 | } | |
643 | ||
644 | void | |
34d340d7 | 645 | finish(int signo __unused) |
1815bff5 A |
646 | { |
647 | if (!killflg) | |
648 | (void)unlink(_PATH_NOLOGIN); | |
649 | exit(0); | |
650 | } | |
651 | ||
652 | void | |
cf37c299 | 653 | badtime(void) |
1815bff5 | 654 | { |
20e66415 | 655 | errx(1, "bad time format"); |
1815bff5 A |
656 | } |
657 | ||
658 | void | |
34d340d7 | 659 | usage(const char *cp) |
1815bff5 | 660 | { |
20e66415 A |
661 | if (cp != NULL) |
662 | warnx("%s", cp); | |
663 | (void)fprintf(stderr, | |
34d340d7 A |
664 | #ifdef __APPLE__ |
665 | "usage: shutdown [-] [-h [-u] [-n] | -r [-n] | -s | -k]" | |
666 | #else | |
667 | "usage: shutdown [-] [-h | -p | -r | -k] [-o [-n]]" | |
668 | #endif | |
20e66415 | 669 | " time [warning-message ...]\n"); |
1815bff5 A |
670 | exit(1); |
671 | } | |
2fc1e207 | 672 | |
34d340d7 | 673 | #ifdef __APPLE__ |
2fc1e207 A |
674 | /* |
675 | * The following tokens are included in the audit record for shutdown | |
676 | * header | |
677 | * subject | |
678 | * return | |
cf37c299 A |
679 | */ |
680 | int | |
681 | audit_shutdown(int exitstatus) | |
2fc1e207 A |
682 | { |
683 | int aufd; | |
684 | token_t *tok; | |
685 | long au_cond; | |
686 | ||
687 | /* If we are not auditing, don't cut an audit record; just return */ | |
688 | if (auditon(A_GETCOND, &au_cond, sizeof(long)) < 0) { | |
689 | fprintf(stderr, "shutdown: Could not determine audit condition\n"); | |
690 | return 0; | |
691 | } | |
692 | if (au_cond == AUC_NOAUDIT) | |
693 | return 0; | |
694 | ||
695 | if((aufd = au_open()) == -1) { | |
696 | fprintf(stderr, "shutdown: Audit Error: au_open() failed\n"); | |
cf37c299 | 697 | exit(1); |
2fc1e207 A |
698 | } |
699 | ||
700 | /* The subject that performed the operation */ | |
701 | if((tok = au_to_me()) == NULL) { | |
702 | fprintf(stderr, "shutdown: Audit Error: au_to_me() failed\n"); | |
703 | exit(1); | |
704 | } | |
705 | au_write(aufd, tok); | |
706 | ||
707 | /* success and failure status */ | |
708 | if((tok = au_to_return32(exitstatus, errno)) == NULL) { | |
709 | fprintf(stderr, "shutdown: Audit Error: au_to_return32() failed\n"); | |
710 | exit(1); | |
711 | } | |
712 | au_write(aufd, tok); | |
713 | ||
714 | if(au_close(aufd, 1, AUE_shutdown) == -1) { | |
715 | fprintf(stderr, "shutdown: Audit Error: au_close() failed\n"); | |
716 | exit(1); | |
717 | } | |
718 | return 1; | |
719 | } | |
b180c247 A |
720 | |
721 | ||
c0bbac3a A |
722 | static bool |
723 | kextdDisabled(void) | |
724 | { | |
725 | uint32_t disabled = 0; | |
726 | size_t sizeOfDisabled = sizeof(disabled); | |
727 | if (sysctlbyname("hw.use_kernelmanagerd", &disabled, &sizeOfDisabled, NULL, 0) != 0) { | |
728 | return false; | |
729 | } | |
730 | return (disabled != 0); | |
731 | } | |
732 | ||
733 | ||
34d340d7 | 734 | // XX copied from reboot.tproj/reboot.c; it would be nice to share the code |
b180c247 | 735 | |
34d340d7 | 736 | #define WAITFORLOCK 1 |
b180c247 A |
737 | /* |
738 | * contact kextd to lock for reboot | |
739 | */ | |
740 | int | |
cf37c299 | 741 | reserve_reboot(void) |
b180c247 | 742 | { |
34d340d7 A |
743 | int rval = ELAST + 1; |
744 | kern_return_t macherr = KERN_FAILURE; | |
745 | mach_port_t kxport, tport = MACH_PORT_NULL, myport = MACH_PORT_NULL; | |
746 | int busyStatus = ELAST + 1; | |
747 | mountpoint_t busyVol; | |
748 | ||
c0bbac3a A |
749 | if (kextdDisabled()) { |
750 | /* no need to talk with kextd if it's not running */ | |
751 | return 0; | |
752 | } | |
753 | ||
fc6d9e4b | 754 | macherr = bootstrap_look_up2(bootstrap_port, KEXTD_SERVER_NAME, &kxport, 0, BOOTSTRAP_PRIVILEGED_SERVER); |
34d340d7 A |
755 | if (macherr) goto finish; |
756 | ||
757 | // allocate a port to pass to kextd (in case we die) | |
758 | tport = mach_task_self(); | |
759 | if (tport == MACH_PORT_NULL) goto finish; | |
760 | macherr = mach_port_allocate(tport, MACH_PORT_RIGHT_RECEIVE, &myport); | |
761 | if (macherr) goto finish; | |
762 | ||
763 | // try to lock for reboot | |
764 | macherr = kextmanager_lock_reboot(kxport, myport, !WAITFORLOCK, busyVol, | |
765 | &busyStatus); | |
766 | if (macherr) goto finish; | |
767 | ||
768 | if (busyStatus == EBUSY) { | |
769 | warnx("%s is busy updating; waiting for lock", busyVol); | |
770 | macherr = kextmanager_lock_reboot(kxport, myport, WAITFORLOCK, | |
771 | busyVol, &busyStatus); | |
772 | if (macherr) goto finish; | |
773 | } | |
774 | ||
775 | if (busyStatus == EALREADY) { | |
776 | // reboot already in progress | |
777 | rval = 0; | |
778 | } else { | |
779 | rval = busyStatus; | |
780 | } | |
b180c247 A |
781 | |
782 | finish: | |
34d340d7 A |
783 | // in general, we want to err on the side of allowing the reboot |
784 | if (macherr) { | |
785 | if (macherr != BOOTSTRAP_UNKNOWN_SERVICE) | |
786 | warnx("WARNING: couldn't lock kext manager for reboot: %s", | |
787 | mach_error_string(macherr)); | |
788 | rval = 0; | |
789 | } | |
790 | // unless we got the lock, clean up our port | |
791 | if (busyStatus != 0 && myport != MACH_PORT_NULL) | |
792 | mach_port_mod_refs(tport, myport, MACH_PORT_RIGHT_RECEIVE, -1); | |
793 | ||
794 | return rval; | |
b180c247 | 795 | } |
34d340d7 | 796 | #endif /* __APPLE__ */ |