]>
git.saurik.com Git - apple/system_cmds.git/blob - shutdown.tproj/shutdown.c
2 * Copyright (c) 1988, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
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.
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
35 static const char copyright
[] =
36 "@(#) Copyright (c) 1988, 1990, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
42 static char sccsid
[] = "@(#)shutdown.c 8.4 (Berkeley) 4/28/95";
44 static const char rcsid
[] =
45 "$FreeBSD: src/sbin/shutdown/shutdown.c,v 1.23 2002/03/21 13:20:48 imp Exp $";
48 #include <sys/param.h>
50 #include <sys/resource.h>
51 #include <sys/syslog.h>
65 #include <bsm/libbsm.h>
66 #include <bsm/audit_uevents.h>
69 #include "pathnames.h"
77 #define _PATH_NOLOGIN "./nologin"
83 #define NOLOG_TIME 5*60
85 int timeleft
, timetowait
;
104 static time_t offset
, shuttime
;
105 static int dohalt
, dopower
, doreboot
, killflg
, mbuflen
, oflag
= 1;
106 static char mbuf
[BUFSIZ
];
107 static const char *nosync
, *whom
;
111 void log_and_exec_reboot_or_halt(void);
113 void die_you_gravy_sucking_pig_dog(void);
116 void getoffset(char *);
121 void usage(const char *);
122 int audit_shutdown(int);
131 int arglen
, ch
, len
, readstdin
;
135 errx(1, "NOT super-user");
140 while ((ch
= getopt(argc
, argv
, "-hknopr")) != -1)
142 while ((ch
= getopt(argc
, argv
, "-hknor")) != -1)
179 if (killflg
+ doreboot
+ dohalt
+ dopower
> 1)
180 usage("incompatible switches -h, -k, -p and -r");
182 if (oflag
&& !(dohalt
|| dopower
|| doreboot
))
183 usage("-o requires -h, -p or -r");
185 if (killflg
+ doreboot
+ dohalt
> 1)
186 usage("incompatible switches -h, -k, and -r");
188 if (oflag
&& !(dohalt
|| doreboot
))
189 usage("-o requires -h or -r");
192 if (nosync
!= NULL
&& !oflag
)
193 usage("-n requires -o");
198 for (p
= mbuf
, len
= sizeof(mbuf
); *argv
; ++argv
) {
199 arglen
= strlen(*argv
);
200 if ((len
-= arglen
) <= 2)
204 memmove(p
, *argv
, arglen
);
213 endp
= mbuf
+ sizeof(mbuf
) - 2;
215 if (!fgets(p
, endp
- p
+ 1, stdin
))
217 for (; *p
&& p
< endp
; ++p
);
225 mbuflen
= strlen(mbuf
);
228 (void)printf("Shutdown at %.24s.\n", ctime(&shuttime
));
230 (void)printf("Shutdown NOW!\n");
232 if (!(whom
= getlogin()))
233 whom
= (pw
= getpwuid(getuid())) ? pw
->pw_name
: "???";
237 (void)putc('\n', stdout
);
239 (void)setpriority(PRIO_PROCESS
, 0, PRIO_MIN
);
249 errx(0, "[pid %d]", forkpid
);
255 openlog("shutdown", LOG_CONS
, LOG_AUTH
);
267 if (offset
<= NOLOG_TIME
) {
274 if (tp
->timeleft
< offset
)
275 (void)sleep((u_int
)(offset
- tp
->timeleft
));
277 while (tp
->timeleft
&& offset
< tp
->timeleft
)
280 * Warn now, if going to sleep more than a fifth of
281 * the next wait time.
283 if ((sltime
= offset
- tp
->timeleft
)) {
284 if (sltime
> (u_int
)(tp
->timetowait
/ 5))
290 timewarn(tp
->timeleft
);
291 if (!logged
&& tp
->timeleft
<= NOLOG_TIME
) {
295 (void)sleep((u_int
)tp
->timetowait
);
300 log_and_exec_reboot_or_halt();
302 die_you_gravy_sucking_pig_dog();
306 static jmp_buf alarmbuf
;
308 static const char *restricted_environ
[] = {
309 "PATH=" _PATH_STDPATH
,
318 static char hostname
[MAXHOSTNAMELEN
+ 1];
320 char wcmd
[MAXPATHLEN
+ 4];
321 extern const char **environ
;
324 (void)gethostname(hostname
, sizeof(hostname
));
326 /* undoc -n option to wall suppresses normal wall banner */
327 (void)snprintf(wcmd
, sizeof(wcmd
), "%s -n", _PATH_WALL
);
328 environ
= restricted_environ
;
329 if (!(pf
= popen(wcmd
, "w"))) {
330 syslog(LOG_ERR
, "shutdown: can't find %s: %m", _PATH_WALL
);
335 "\007*** %sSystem shutdown message from %s@%s ***\007\n",
336 timeleft
? "": "FINAL ", whom
, hostname
);
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" : "");
345 (void)fprintf(pf
, "System going down in 30 seconds\n\n");
347 (void)fprintf(pf
, "System going down IMMEDIATELY\n\n");
350 (void)fwrite(mbuf
, sizeof(*mbuf
), mbuflen
, pf
);
353 * play some games, just in case wall doesn't come back
354 * probably unnecessary, given that wall is careful.
356 if (!setjmp(alarmbuf
)) {
357 (void)signal(SIGALRM
, timeout
);
358 (void)alarm((u_int
)30);
360 (void)alarm((u_int
)0);
361 (void)signal(SIGALRM
, SIG_DFL
);
369 longjmp(alarmbuf
, 1);
374 log_and_exec_reboot_or_halt()
376 die_you_gravy_sucking_pig_dog()
379 char *empty_environ
[] = { NULL
};
381 syslog(LOG_NOTICE
, "%s by %s: %s",
383 doreboot
? "reboot" : dohalt
? "halt" : dopower
? "power-down" :
385 doreboot
? "reboot" : dohalt
? "halt" :
387 "shutdown", whom
, mbuf
);
392 (void)printf("\r\nSystem shutdown time has arrived\007\007\r\n");
394 (void)printf("\rbut you'll have to do it yourself\r\n");
399 (void)printf("reboot");
401 (void)printf("halt");
404 (void)printf("power-down");
407 (void)printf(" no sync");
408 (void)printf("\nkill -HUP 1\n");
415 execl(_PATH_BSHELL
, _PATH_BSHELL
, "/etc/rc.shutdown", NULL
);
421 (void)kill(1, doreboot
? SIGINT
: /* reboot */
422 dohalt
? SIGUSR1
: /* halt */
424 dopower
? SIGUSR2
: /* power-down */
426 SIGTERM
); /* single-user */
429 execle(_PATH_REBOOT
, "reboot", "-l", nosync
,
430 (char *)NULL
, empty_environ
);
431 syslog(LOG_ERR
, "shutdown: can't exec %s: %m.",
436 execle(_PATH_HALT
, "halt", "-l", nosync
,
437 (char *)NULL
, empty_environ
);
438 syslog(LOG_ERR
, "shutdown: can't exec %s: %m.",
444 execle(_PATH_HALT
, "halt", "-l", "-p", nosync
,
445 (char *)NULL
, empty_environ
);
446 syslog(LOG_ERR
, "shutdown: can't exec %s: %m.",
451 (void)kill(1, SIGTERM
); /* to single-user */
457 #define ATOI2(p) (p[0] - '0') * 10 + (p[1] - '0'); p += 2;
470 if (!strcasecmp(timearg
, "now")) { /* now */
476 if (*timearg
== '+') { /* +minutes */
477 if (!isdigit(*++timearg
))
479 if ((offset
= atoi(timearg
) * 60) < 0)
481 shuttime
= now
+ offset
;
485 /* handle hh:mm by getting rid of the colon */
486 for (p
= timearg
; *p
; ++p
)
487 if (!isascii(*p
) || !isdigit(*p
)) {
488 if (*p
== ':' && strlen(p
) == 3) {
497 unsetenv("TZ"); /* OUR timezone */
498 lt
= localtime(&now
); /* current time val */
500 switch(strlen(timearg
)) {
502 this_year
= lt
->tm_year
;
503 lt
->tm_year
= ATOI2(timearg
);
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.
509 if (lt
->tm_year
< (this_year
% 100) - 1)
511 /* adjust for the year 2000 and beyond */
512 lt
->tm_year
+= (this_year
- (this_year
% 100));
515 lt
->tm_mon
= ATOI2(timearg
);
516 if (--lt
->tm_mon
< 0 || lt
->tm_mon
> 11)
520 lt
->tm_mday
= ATOI2(timearg
);
521 if (lt
->tm_mday
< 1 || lt
->tm_mday
> 31)
525 lt
->tm_hour
= ATOI2(timearg
);
526 if (lt
->tm_hour
< 0 || lt
->tm_hour
> 23)
528 lt
->tm_min
= ATOI2(timearg
);
529 if (lt
->tm_min
< 0 || lt
->tm_min
> 59)
532 if ((shuttime
= mktime(lt
)) == -1)
534 if ((offset
= shuttime
- now
) < 0)
535 errx(1, "that time is already past.");
542 #define NOMSG "\n\nNO LOGINS: System going down at "
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
,
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
));
573 (void)unlink(_PATH_NOLOGIN
);
581 errx(1, "bad time format");
590 (void)fprintf(stderr
,
591 "usage: shutdown [-] [-h | -p | -r | -k] [-o [-n]]"
592 " time [warning-message ...]\n");
597 * The following tokens are included in the audit record for shutdown
602 int audit_shutdown(int exitstatus
)
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");
613 if (au_cond
== AUC_NOAUDIT
)
616 if((aufd
= au_open()) == -1) {
617 fprintf(stderr
, "shutdown: Audit Error: au_open() failed\n");
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");
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");
635 if(au_close(aufd
, 1, AUE_shutdown
) == -1) {
636 fprintf(stderr
, "shutdown: Audit Error: au_close() failed\n");