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