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