]> git.saurik.com Git - apple/system_cmds.git/blame - shutdown.tproj/shutdown.c
system_cmds-336.tar.gz
[apple/system_cmds.git] / shutdown.tproj / shutdown.c
CommitLineData
1815bff5
A
1/*
2 * Copyright (c) 1988, 1990, 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) 1988, 1990, 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[] = "@(#)shutdown.c 8.4 (Berkeley) 4/28/95";
43#endif
44static const char rcsid[] =
45 "$FreeBSD: src/sbin/shutdown/shutdown.c,v 1.23 2002/03/21 13:20:48 imp Exp $";
46#endif /* not lint */
47
1815bff5
A
48#include <sys/param.h>
49#include <sys/time.h>
50#include <sys/resource.h>
51#include <sys/syslog.h>
52
53#include <ctype.h>
20e66415 54#include <err.h>
1815bff5
A
55#include <fcntl.h>
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
2fc1e207
A
64#include <errno.h>
65#include <bsm/libbsm.h>
66#include <bsm/audit_uevents.h>
67
68
1815bff5
A
69#include "pathnames.h"
70
20e66415
A
71#ifdef __APPLE__
72#define __unused
73#endif
74
1815bff5
A
75#ifdef DEBUG
76#undef _PATH_NOLOGIN
77#define _PATH_NOLOGIN "./nologin"
1815bff5
A
78#endif
79
80#define H *60*60
81#define M *60
82#define S *1
83#define NOLOG_TIME 5*60
84struct interval {
85 int timeleft, timetowait;
86} tlist[] = {
20e66415
A
87 { 10 H, 5 H },
88 { 5 H, 3 H },
89 { 2 H, 1 H },
90 { 1 H, 30 M },
91 { 30 M, 10 M },
92 { 20 M, 10 M },
93 { 10 M, 5 M },
94 { 5 M, 3 M },
95 { 2 M, 1 M },
96 { 1 M, 30 S },
97 { 30 S, 30 S },
98 { 0 , 0 }
1815bff5
A
99};
100#undef H
101#undef M
102#undef S
103
104static time_t offset, shuttime;
20e66415
A
105static int dohalt, dopower, doreboot, killflg, mbuflen, oflag = 1;
106static char mbuf[BUFSIZ];
107static const char *nosync, *whom;
108
109void badtime(void);
83f6dbe8
A
110#ifdef __APPLE__
111void log_and_exec_reboot_or_halt(void);
112#else
20e66415 113void die_you_gravy_sucking_pig_dog(void);
83f6dbe8 114#endif
20e66415
A
115void finish(int);
116void getoffset(char *);
117void loop(void);
118void nolog(void);
119void timeout(int);
120void timewarn(int);
121void usage(const char *);
2fc1e207 122int audit_shutdown(int);
1815bff5
A
123
124int
125main(argc, argv)
126 int argc;
127 char *argv[];
128{
20e66415 129 char *p, *endp;
1815bff5
A
130 struct passwd *pw;
131 int arglen, ch, len, readstdin;
132
133#ifndef DEBUG
20e66415
A
134 if (geteuid())
135 errx(1, "NOT super-user");
1815bff5
A
136#endif
137 nosync = NULL;
138 readstdin = 0;
20e66415
A
139#ifndef __APPLE__
140 while ((ch = getopt(argc, argv, "-hknopr")) != -1)
141#else
142 while ((ch = getopt(argc, argv, "-hknor")) != -1)
143#endif
1815bff5
A
144 switch (ch) {
145 case '-':
146 readstdin = 1;
147 break;
1815bff5
A
148 case 'h':
149 dohalt = 1;
150 break;
151 case 'k':
152 killflg = 1;
153 break;
154 case 'n':
155 nosync = "-n";
156 break;
20e66415
A
157 case 'o':
158 oflag = 1;
159 break;
160#ifndef __APPLE__
161 case 'p':
162 dopower = 1;
163 break;
164#endif
1815bff5
A
165 case 'r':
166 doreboot = 1;
167 break;
168 case '?':
169 default:
20e66415 170 usage((char *)NULL);
1815bff5
A
171 }
172 argc -= optind;
173 argv += optind;
174
175 if (argc < 1)
20e66415
A
176 usage((char *)NULL);
177
178#ifndef __APPLE__
179 if (killflg + doreboot + dohalt + dopower > 1)
180 usage("incompatible switches -h, -k, -p and -r");
181
182 if (oflag && !(dohalt || dopower || doreboot))
183 usage("-o requires -h, -p or -r");
184#else
185 if (killflg + doreboot + dohalt > 1)
186 usage("incompatible switches -h, -k, and -r");
187
188 if (oflag && !(dohalt || doreboot))
189 usage("-o requires -h or -r");
190#endif
191
192 if (nosync != NULL && !oflag)
193 usage("-n requires -o");
1815bff5 194
1815bff5
A
195 getoffset(*argv++);
196
197 if (*argv) {
198 for (p = mbuf, len = sizeof(mbuf); *argv; ++argv) {
199 arglen = strlen(*argv);
200 if ((len -= arglen) <= 2)
201 break;
202 if (p != mbuf)
203 *p++ = ' ';
204 memmove(p, *argv, arglen);
205 p += arglen;
206 }
207 *p = '\n';
208 *++p = '\0';
209 }
210
211 if (readstdin) {
212 p = mbuf;
213 endp = mbuf + sizeof(mbuf) - 2;
214 for (;;) {
215 if (!fgets(p, endp - p + 1, stdin))
216 break;
217 for (; *p && p < endp; ++p);
218 if (p == endp) {
219 *p = '\n';
220 *++p = '\0';
221 break;
222 }
223 }
224 }
225 mbuflen = strlen(mbuf);
226
227 if (offset)
228 (void)printf("Shutdown at %.24s.\n", ctime(&shuttime));
229 else
230 (void)printf("Shutdown NOW!\n");
231
232 if (!(whom = getlogin()))
233 whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???";
234
235#ifdef DEBUG
2fc1e207 236 audit_shutdown(0);
1815bff5
A
237 (void)putc('\n', stdout);
238#else
239 (void)setpriority(PRIO_PROCESS, 0, PRIO_MIN);
240 {
241 int forkpid;
242
243 forkpid = fork();
2fc1e207
A
244 if (forkpid == -1) {
245 audit_shutdown(1);
20e66415 246 err(1, "fork");
2fc1e207
A
247 }
248 if (forkpid) {
20e66415 249 errx(0, "[pid %d]", forkpid);
2fc1e207 250 }
1815bff5 251 }
2fc1e207 252 audit_shutdown(0);
20e66415 253 setsid();
1815bff5
A
254#endif
255 openlog("shutdown", LOG_CONS, LOG_AUTH);
256 loop();
20e66415 257 return(0);
1815bff5
A
258}
259
260void
261loop()
262{
263 struct interval *tp;
264 u_int sltime;
265 int logged;
266
267 if (offset <= NOLOG_TIME) {
268 logged = 1;
269 nolog();
270 }
271 else
272 logged = 0;
273 tp = tlist;
274 if (tp->timeleft < offset)
275 (void)sleep((u_int)(offset - tp->timeleft));
276 else {
20e66415 277 while (tp->timeleft && offset < tp->timeleft)
1815bff5
A
278 ++tp;
279 /*
280 * Warn now, if going to sleep more than a fifth of
281 * the next wait time.
282 */
20e66415
A
283 if ((sltime = offset - tp->timeleft)) {
284 if (sltime > (u_int)(tp->timetowait / 5))
1815bff5
A
285 timewarn(offset);
286 (void)sleep(sltime);
287 }
288 }
289 for (;; ++tp) {
290 timewarn(tp->timeleft);
291 if (!logged && tp->timeleft <= NOLOG_TIME) {
292 logged = 1;
293 nolog();
294 }
295 (void)sleep((u_int)tp->timetowait);
296 if (!tp->timeleft)
297 break;
298 }
83f6dbe8
A
299#ifdef __APPLE__
300 log_and_exec_reboot_or_halt();
301#else
1815bff5 302 die_you_gravy_sucking_pig_dog();
83f6dbe8 303#endif
1815bff5
A
304}
305
306static jmp_buf alarmbuf;
307
20e66415
A
308static const char *restricted_environ[] = {
309 "PATH=" _PATH_STDPATH,
310 NULL
311};
312
1815bff5
A
313void
314timewarn(timeleft)
315 int timeleft;
316{
317 static int first;
318 static char hostname[MAXHOSTNAMELEN + 1];
319 FILE *pf;
320 char wcmd[MAXPATHLEN + 4];
20e66415 321 extern const char **environ;
1815bff5
A
322
323 if (!first++)
324 (void)gethostname(hostname, sizeof(hostname));
325
326 /* undoc -n option to wall suppresses normal wall banner */
327 (void)snprintf(wcmd, sizeof(wcmd), "%s -n", _PATH_WALL);
20e66415 328 environ = restricted_environ;
1815bff5
A
329 if (!(pf = popen(wcmd, "w"))) {
330 syslog(LOG_ERR, "shutdown: can't find %s: %m", _PATH_WALL);
331 return;
332 }
333
334 (void)fprintf(pf,
335 "\007*** %sSystem shutdown message from %s@%s ***\007\n",
336 timeleft ? "": "FINAL ", whom, hostname);
337
338 if (timeleft > 10*60)
339 (void)fprintf(pf, "System going down at %5.5s\n\n",
340 ctime(&shuttime) + 11);
341 else if (timeleft > 59)
342 (void)fprintf(pf, "System going down in %d minute%s\n\n",
343 timeleft / 60, (timeleft > 60) ? "s" : "");
344 else if (timeleft)
345 (void)fprintf(pf, "System going down in 30 seconds\n\n");
346 else
347 (void)fprintf(pf, "System going down IMMEDIATELY\n\n");
348
349 if (mbuflen)
350 (void)fwrite(mbuf, sizeof(*mbuf), mbuflen, pf);
351
352 /*
353 * play some games, just in case wall doesn't come back
20e66415 354 * probably unnecessary, given that wall is careful.
1815bff5
A
355 */
356 if (!setjmp(alarmbuf)) {
357 (void)signal(SIGALRM, timeout);
358 (void)alarm((u_int)30);
359 (void)pclose(pf);
360 (void)alarm((u_int)0);
361 (void)signal(SIGALRM, SIG_DFL);
362 }
363}
364
365void
366timeout(signo)
20e66415 367 int signo __unused;
1815bff5
A
368{
369 longjmp(alarmbuf, 1);
370}
371
372void
83f6dbe8
A
373#ifdef __APPLE__
374log_and_exec_reboot_or_halt()
375#else
1815bff5 376die_you_gravy_sucking_pig_dog()
83f6dbe8 377#endif
1815bff5 378{
20e66415 379 char *empty_environ[] = { NULL };
1815bff5
A
380
381 syslog(LOG_NOTICE, "%s by %s: %s",
20e66415
A
382#ifndef __APPLE__
383 doreboot ? "reboot" : dohalt ? "halt" : dopower ? "power-down" :
384#else
385 doreboot ? "reboot" : dohalt ? "halt" :
386#endif
387 "shutdown", whom, mbuf);
83f6dbe8 388#ifndef __APPLE__
1815bff5 389 (void)sleep(2);
83f6dbe8 390#endif
1815bff5
A
391
392 (void)printf("\r\nSystem shutdown time has arrived\007\007\r\n");
393 if (killflg) {
394 (void)printf("\rbut you'll have to do it yourself\r\n");
20e66415 395 exit(0);
1815bff5 396 }
1815bff5
A
397#ifdef DEBUG
398 if (doreboot)
399 (void)printf("reboot");
400 else if (dohalt)
401 (void)printf("halt");
20e66415
A
402#ifndef __APPLE__
403 else if (dopower)
404 (void)printf("power-down");
405#endif
406 if (nosync != NULL)
1815bff5 407 (void)printf(" no sync");
1815bff5
A
408 (void)printf("\nkill -HUP 1\n");
409#else
2fc1e207
A
410#ifdef __APPLE__
411 {
412 int ws = 0;
413 int fp = fork();
414 if (fp == 0)
415 execl(_PATH_BSHELL, _PATH_BSHELL, "/etc/rc.shutdown", NULL);
416 else if (fp > 0)
417 waitpid(fp, &ws, 0);
418 }
419#endif
20e66415
A
420 if (!oflag) {
421 (void)kill(1, doreboot ? SIGINT : /* reboot */
422 dohalt ? SIGUSR1 : /* halt */
423#ifndef __APPLE__
424 dopower ? SIGUSR2 : /* power-down */
425#endif
426 SIGTERM); /* single-user */
427 } else {
428 if (doreboot) {
429 execle(_PATH_REBOOT, "reboot", "-l", nosync,
430 (char *)NULL, empty_environ);
431 syslog(LOG_ERR, "shutdown: can't exec %s: %m.",
432 _PATH_REBOOT);
433 warn(_PATH_REBOOT);
434 }
435 else if (dohalt) {
436 execle(_PATH_HALT, "halt", "-l", nosync,
437 (char *)NULL, empty_environ);
438 syslog(LOG_ERR, "shutdown: can't exec %s: %m.",
439 _PATH_HALT);
440 warn(_PATH_HALT);
441 }
442#ifndef __APPLE__
443 else if (dopower) {
444 execle(_PATH_HALT, "halt", "-l", "-p", nosync,
445 (char *)NULL, empty_environ);
446 syslog(LOG_ERR, "shutdown: can't exec %s: %m.",
447 _PATH_HALT);
448 warn(_PATH_HALT);
449 }
450#endif
451 (void)kill(1, SIGTERM); /* to single-user */
1815bff5 452 }
1815bff5
A
453#endif
454 finish(0);
455}
456
457#define ATOI2(p) (p[0] - '0') * 10 + (p[1] - '0'); p += 2;
458
459void
460getoffset(timearg)
20e66415 461 char *timearg;
1815bff5 462{
20e66415
A
463 struct tm *lt;
464 char *p;
1815bff5 465 time_t now;
20e66415
A
466 int this_year;
467
468 (void)time(&now);
1815bff5
A
469
470 if (!strcasecmp(timearg, "now")) { /* now */
471 offset = 0;
20e66415 472 shuttime = now;
1815bff5
A
473 return;
474 }
475
1815bff5
A
476 if (*timearg == '+') { /* +minutes */
477 if (!isdigit(*++timearg))
478 badtime();
20e66415
A
479 if ((offset = atoi(timearg) * 60) < 0)
480 badtime();
1815bff5
A
481 shuttime = now + offset;
482 return;
483 }
484
485 /* handle hh:mm by getting rid of the colon */
486 for (p = timearg; *p; ++p)
20e66415 487 if (!isascii(*p) || !isdigit(*p)) {
1815bff5
A
488 if (*p == ':' && strlen(p) == 3) {
489 p[0] = p[1];
490 p[1] = p[2];
491 p[2] = '\0';
492 }
493 else
494 badtime();
20e66415 495 }
1815bff5
A
496
497 unsetenv("TZ"); /* OUR timezone */
498 lt = localtime(&now); /* current time val */
499
500 switch(strlen(timearg)) {
501 case 10:
20e66415 502 this_year = lt->tm_year;
1815bff5 503 lt->tm_year = ATOI2(timearg);
20e66415
A
504 /*
505 * check if the specified year is in the next century.
506 * allow for one year of user error as many people will
507 * enter n - 1 at the start of year n.
508 */
509 if (lt->tm_year < (this_year % 100) - 1)
510 lt->tm_year += 100;
511 /* adjust for the year 2000 and beyond */
512 lt->tm_year += (this_year - (this_year % 100));
1815bff5
A
513 /* FALLTHROUGH */
514 case 8:
515 lt->tm_mon = ATOI2(timearg);
516 if (--lt->tm_mon < 0 || lt->tm_mon > 11)
517 badtime();
518 /* FALLTHROUGH */
519 case 6:
520 lt->tm_mday = ATOI2(timearg);
521 if (lt->tm_mday < 1 || lt->tm_mday > 31)
522 badtime();
523 /* FALLTHROUGH */
524 case 4:
525 lt->tm_hour = ATOI2(timearg);
526 if (lt->tm_hour < 0 || lt->tm_hour > 23)
527 badtime();
528 lt->tm_min = ATOI2(timearg);
529 if (lt->tm_min < 0 || lt->tm_min > 59)
530 badtime();
531 lt->tm_sec = 0;
532 if ((shuttime = mktime(lt)) == -1)
533 badtime();
20e66415
A
534 if ((offset = shuttime - now) < 0)
535 errx(1, "that time is already past.");
1815bff5
A
536 break;
537 default:
538 badtime();
539 }
540}
541
1815bff5
A
542#define NOMSG "\n\nNO LOGINS: System going down at "
543void
544nolog()
545{
20e66415 546#ifndef __APPLE__
1815bff5
A
547 int logfd;
548 char *ct;
549
550 (void)unlink(_PATH_NOLOGIN); /* in case linked to another file */
551 (void)signal(SIGINT, finish);
552 (void)signal(SIGHUP, finish);
553 (void)signal(SIGQUIT, finish);
554 (void)signal(SIGTERM, finish);
555 if ((logfd = open(_PATH_NOLOGIN, O_WRONLY|O_CREAT|O_TRUNC,
556 0664)) >= 0) {
557 (void)write(logfd, NOMSG, sizeof(NOMSG) - 1);
558 ct = ctime(&shuttime);
559 (void)write(logfd, ct + 11, 5);
560 (void)write(logfd, "\n\n", 2);
561 (void)write(logfd, mbuf, strlen(mbuf));
562 (void)close(logfd);
563 }
20e66415 564#endif
1815bff5
A
565}
566
567void
568finish(signo)
20e66415 569 int signo __unused;
1815bff5 570{
20e66415 571#ifndef __APPLE__
1815bff5
A
572 if (!killflg)
573 (void)unlink(_PATH_NOLOGIN);
20e66415 574#endif
1815bff5
A
575 exit(0);
576}
577
578void
579badtime()
580{
20e66415 581 errx(1, "bad time format");
1815bff5
A
582}
583
584void
20e66415
A
585usage(cp)
586 const char *cp;
1815bff5 587{
20e66415
A
588 if (cp != NULL)
589 warnx("%s", cp);
590 (void)fprintf(stderr,
591 "usage: shutdown [-] [-h | -p | -r | -k] [-o [-n]]"
592 " time [warning-message ...]\n");
1815bff5
A
593 exit(1);
594}
2fc1e207
A
595
596/*
597 * The following tokens are included in the audit record for shutdown
598 * header
599 * subject
600 * return
601 */
602int audit_shutdown(int exitstatus)
603{
604 int aufd;
605 token_t *tok;
606 long au_cond;
607
608 /* If we are not auditing, don't cut an audit record; just return */
609 if (auditon(A_GETCOND, &au_cond, sizeof(long)) < 0) {
610 fprintf(stderr, "shutdown: Could not determine audit condition\n");
611 return 0;
612 }
613 if (au_cond == AUC_NOAUDIT)
614 return 0;
615
616 if((aufd = au_open()) == -1) {
617 fprintf(stderr, "shutdown: Audit Error: au_open() failed\n");
618 exit(1);
619 }
620
621 /* The subject that performed the operation */
622 if((tok = au_to_me()) == NULL) {
623 fprintf(stderr, "shutdown: Audit Error: au_to_me() failed\n");
624 exit(1);
625 }
626 au_write(aufd, tok);
627
628 /* success and failure status */
629 if((tok = au_to_return32(exitstatus, errno)) == NULL) {
630 fprintf(stderr, "shutdown: Audit Error: au_to_return32() failed\n");
631 exit(1);
632 }
633 au_write(aufd, tok);
634
635 if(au_close(aufd, 1, AUE_shutdown) == -1) {
636 fprintf(stderr, "shutdown: Audit Error: au_close() failed\n");
637 exit(1);
638 }
639 return 1;
640}