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 "kextmanager.h"
70 #include <IOKit/kext/kextmanager_types.h>
71 #include <mach/mach_port.h> // allocate
72 #include <mach/mach.h> // task_self, etc
73 #include <servers/bootstrap.h> // bootstrap
76 #include "pathnames.h"
84 #define _PATH_NOLOGIN "./nologin"
90 #define NOLOG_TIME 5*60
92 int timeleft
, timetowait
;
111 static time_t offset
, shuttime
;
112 static int dohalt
, dopower
, doreboot
, doups
, killflg
, mbuflen
, oflag
= 1;
113 static char mbuf
[BUFSIZ
];
114 static const char *nosync
, *whom
;
118 void log_and_exec_reboot_or_halt(void);
120 void die_you_gravy_sucking_pig_dog(void);
123 void getoffset(char *);
128 void usage(const char *);
129 int audit_shutdown(int);
131 int reserve_reboot(void);
141 int arglen
, ch
, len
, readstdin
;
145 errx(1, "NOT super-user");
150 while ((ch
= getopt(argc
, argv
, "-hknopr")) != -1)
152 while ((ch
= getopt(argc
, argv
, "-hknoru")) != -1)
193 if (killflg
+ doreboot
+ dohalt
+ dopower
> 1)
194 usage("incompatible switches -h, -k, -p and -r");
196 if (oflag
&& !(dohalt
|| dopower
|| doreboot
))
197 usage("-o requires -h, -p or -r");
199 if (killflg
+ doreboot
+ dohalt
> 1)
200 usage("incompatible switches -h, -k, and -r");
202 if (oflag
&& !(dohalt
|| doreboot
))
203 usage("-o requires -h or -r");
205 if (doups
&& !dohalt
)
206 usage("-u requires -h");
209 if (nosync
!= NULL
&& !oflag
)
210 usage("-n requires -o");
215 for (p
= mbuf
, len
= sizeof(mbuf
); *argv
; ++argv
) {
216 arglen
= strlen(*argv
);
217 if ((len
-= arglen
) <= 2)
221 memmove(p
, *argv
, arglen
);
230 endp
= mbuf
+ sizeof(mbuf
) - 2;
232 if (!fgets(p
, endp
- p
+ 1, stdin
))
234 for (; *p
&& p
< endp
; ++p
);
242 mbuflen
= strlen(mbuf
);
245 (void)printf("Shutdown at %.24s.\n", ctime(&shuttime
));
247 (void)printf("Shutdown NOW!\n");
249 if (!(whom
= getlogin()))
250 whom
= (pw
= getpwuid(getuid())) ? pw
->pw_name
: "???";
254 (void)putc('\n', stdout
);
256 (void)setpriority(PRIO_PROCESS
, 0, PRIO_MIN
);
266 errx(0, "[pid %d]", forkpid
);
272 openlog("shutdown", LOG_CONS
, LOG_AUTH
);
284 if (offset
<= NOLOG_TIME
) {
291 if (tp
->timeleft
< offset
)
292 (void)sleep((u_int
)(offset
- tp
->timeleft
));
294 while (tp
->timeleft
&& offset
< tp
->timeleft
)
297 * Warn now, if going to sleep more than a fifth of
298 * the next wait time.
300 if ((sltime
= offset
- tp
->timeleft
)) {
301 if (sltime
> (u_int
)(tp
->timetowait
/ 5))
307 timewarn(tp
->timeleft
);
308 if (!logged
&& tp
->timeleft
<= NOLOG_TIME
) {
312 (void)sleep((u_int
)tp
->timetowait
);
317 log_and_exec_reboot_or_halt();
319 die_you_gravy_sucking_pig_dog();
323 static jmp_buf alarmbuf
;
325 static const char *restricted_environ
[] = {
326 "PATH=" _PATH_STDPATH
,
335 static char hostname
[MAXHOSTNAMELEN
+ 1];
337 char wcmd
[MAXPATHLEN
+ 4];
338 extern const char **environ
;
341 (void)gethostname(hostname
, sizeof(hostname
));
343 /* undoc -n option to wall suppresses normal wall banner */
344 (void)snprintf(wcmd
, sizeof(wcmd
), "%s -n", _PATH_WALL
);
345 environ
= restricted_environ
;
346 if (!(pf
= popen(wcmd
, "w"))) {
347 syslog(LOG_ERR
, "shutdown: can't find %s: %m", _PATH_WALL
);
352 "\007*** %sSystem shutdown message from %s@%s ***\007\n",
353 timeleft
? "": "FINAL ", whom
, hostname
);
355 if (timeleft
> 10*60)
356 (void)fprintf(pf
, "System going down at %5.5s\n\n",
357 ctime(&shuttime
) + 11);
358 else if (timeleft
> 59)
359 (void)fprintf(pf
, "System going down in %d minute%s\n\n",
360 timeleft
/ 60, (timeleft
> 60) ? "s" : "");
362 (void)fprintf(pf
, "System going down in 30 seconds\n\n");
364 (void)fprintf(pf
, "System going down IMMEDIATELY\n\n");
367 (void)fwrite(mbuf
, sizeof(*mbuf
), mbuflen
, pf
);
370 * play some games, just in case wall doesn't come back
371 * probably unnecessary, given that wall is careful.
373 if (!setjmp(alarmbuf
)) {
374 (void)signal(SIGALRM
, timeout
);
375 (void)alarm((u_int
)30);
377 (void)alarm((u_int
)0);
378 (void)signal(SIGALRM
, SIG_DFL
);
386 longjmp(alarmbuf
, 1);
391 log_and_exec_reboot_or_halt()
393 die_you_gravy_sucking_pig_dog()
396 char *empty_environ
[] = { NULL
};
399 if ((errno
= reserve_reboot()))
400 err(1, "couldn't lock for reboot");
403 syslog(LOG_NOTICE
, "%s%s by %s: %s",
405 doreboot
? "reboot" : dohalt
? "halt" : dopower
? "power-down" :
407 doreboot
? "reboot" : dohalt
? "halt" :
409 "shutdown", doups
?"with UPS delay":"", whom
, mbuf
);
414 (void)printf("\r\nSystem shutdown time has arrived\007\007\r\n");
416 (void)printf("\rbut you'll have to do it yourself\r\n");
421 (void)printf("reboot");
423 (void)printf("halt");
426 (void)printf("power-down");
429 (void)printf(" no sync");
430 (void)printf("\nkill -HUP 1\n");
437 execl(_PATH_BSHELL
, _PATH_BSHELL
, "/etc/rc.shutdown", NULL
);
443 (void)kill(1, doreboot
? SIGINT
: /* reboot */
444 dohalt
? SIGUSR1
: /* halt */
446 dopower
? SIGUSR2
: /* power-down */
448 SIGTERM
); /* single-user */
451 execle(_PATH_REBOOT
, "reboot", "-l", nosync
,
452 (char *)NULL
, empty_environ
);
453 syslog(LOG_ERR
, "shutdown: can't exec %s: %m.",
464 execle(_PATH_HALT
, "halt", halt_args
, nosync
,
465 (char *)NULL
, empty_environ
);
466 syslog(LOG_ERR
, "shutdown: can't exec %s: %m.",
472 execle(_PATH_HALT
, "halt", "-l", "-p", nosync
,
473 (char *)NULL
, empty_environ
);
474 syslog(LOG_ERR
, "shutdown: can't exec %s: %m.",
479 (void)kill(1, SIGTERM
); /* to single-user */
485 #define ATOI2(p) (p[0] - '0') * 10 + (p[1] - '0'); p += 2;
498 if (!strcasecmp(timearg
, "now")) { /* now */
504 if (*timearg
== '+') { /* +minutes */
505 if (!isdigit(*++timearg
))
507 if ((offset
= atoi(timearg
) * 60) < 0)
509 shuttime
= now
+ offset
;
513 /* handle hh:mm by getting rid of the colon */
514 for (p
= timearg
; *p
; ++p
)
515 if (!isascii(*p
) || !isdigit(*p
)) {
516 if (*p
== ':' && strlen(p
) == 3) {
525 unsetenv("TZ"); /* OUR timezone */
526 lt
= localtime(&now
); /* current time val */
528 switch(strlen(timearg
)) {
530 this_year
= lt
->tm_year
;
531 lt
->tm_year
= ATOI2(timearg
);
533 * check if the specified year is in the next century.
534 * allow for one year of user error as many people will
535 * enter n - 1 at the start of year n.
537 if (lt
->tm_year
< (this_year
% 100) - 1)
539 /* adjust for the year 2000 and beyond */
540 lt
->tm_year
+= (this_year
- (this_year
% 100));
543 lt
->tm_mon
= ATOI2(timearg
);
544 if (--lt
->tm_mon
< 0 || lt
->tm_mon
> 11)
548 lt
->tm_mday
= ATOI2(timearg
);
549 if (lt
->tm_mday
< 1 || lt
->tm_mday
> 31)
553 lt
->tm_hour
= ATOI2(timearg
);
554 if (lt
->tm_hour
< 0 || lt
->tm_hour
> 23)
556 lt
->tm_min
= ATOI2(timearg
);
557 if (lt
->tm_min
< 0 || lt
->tm_min
> 59)
560 if ((shuttime
= mktime(lt
)) == -1)
562 if ((offset
= shuttime
- now
) < 0)
563 errx(1, "that time is already past.");
570 #define NOMSG "\n\nNO LOGINS: System going down at "
578 (void)unlink(_PATH_NOLOGIN
); /* in case linked to another file */
579 (void)signal(SIGINT
, finish
);
580 (void)signal(SIGHUP
, finish
);
581 (void)signal(SIGQUIT
, finish
);
582 (void)signal(SIGTERM
, finish
);
583 if ((logfd
= open(_PATH_NOLOGIN
, O_WRONLY
|O_CREAT
|O_TRUNC
,
585 (void)write(logfd
, NOMSG
, sizeof(NOMSG
) - 1);
586 ct
= ctime(&shuttime
);
587 (void)write(logfd
, ct
+ 11, 5);
588 (void)write(logfd
, "\n\n", 2);
589 (void)write(logfd
, mbuf
, strlen(mbuf
));
601 (void)unlink(_PATH_NOLOGIN
);
609 errx(1, "bad time format");
618 (void)fprintf(stderr
,
619 "usage: shutdown [-] [-h [-u] | -r | -k] [-o [-n]]"
620 " time [warning-message ...]\n");
625 * The following tokens are included in the audit record for shutdown
630 int audit_shutdown(int exitstatus
)
636 /* If we are not auditing, don't cut an audit record; just return */
637 if (auditon(A_GETCOND
, &au_cond
, sizeof(long)) < 0) {
638 fprintf(stderr
, "shutdown: Could not determine audit condition\n");
641 if (au_cond
== AUC_NOAUDIT
)
644 if((aufd
= au_open()) == -1) {
645 fprintf(stderr
, "shutdown: Audit Error: au_open() failed\n");
649 /* The subject that performed the operation */
650 if((tok
= au_to_me()) == NULL
) {
651 fprintf(stderr
, "shutdown: Audit Error: au_to_me() failed\n");
656 /* success and failure status */
657 if((tok
= au_to_return32(exitstatus
, errno
)) == NULL
) {
658 fprintf(stderr
, "shutdown: Audit Error: au_to_return32() failed\n");
663 if(au_close(aufd
, 1, AUE_shutdown
) == -1) {
664 fprintf(stderr
, "shutdown: Audit Error: au_close() failed\n");
672 // XX copied from reboot.c; would be nice to share the code
674 #define LCK_MAXTRIES 10
677 * contact kextd to lock for reboot
683 kern_return_t macherr
= KERN_FAILURE
;
684 mach_port_t tport
, bsport
, kxport
, myport
= MACH_PORT_NULL
;
685 int busyStatus
, nretries
= LCK_MAXTRIES
;
686 dev_path_t busyDev
= "<unknown>";
689 tport
= mach_task_self();
690 if (tport
== MACH_PORT_NULL
) goto finish
;
691 macherr
= task_get_bootstrap_port(tport
, &bsport
);
692 if (macherr
) goto finish
;
693 macherr
= bootstrap_look_up(bsport
, KEXTD_SERVER_NAME
, &kxport
);
694 if (macherr
) goto finish
;
696 // allocate a port to pass to kextd (in case we die)
697 macherr
= mach_port_allocate(tport
, MACH_PORT_RIGHT_RECEIVE
, &myport
);
698 if (macherr
) goto finish
;
700 // loop trying to lock for reboot (i.e. no volumes are busy)
703 macherr
= kextmanager_lock_reboot(kxport
, myport
, busyDev
, &busyStatus
);
704 if (macherr
) goto finish
;
706 if (busyStatus
== EBUSY
) {
708 warnx("%s is busy updating; delaying reboot (%d retries left)",
711 warnx("kextd still starting up");
712 if (nretries
) sleep(LCK_DELAY
); // don't sleep the last time
714 } while (busyStatus
== EBUSY
&& nretries
> 0);
719 if (macherr
== BOOTSTRAP_UNKNOWN_SERVICE
) {
720 mach_port_mod_refs(tport
, myport
, MACH_PORT_RIGHT_RECEIVE
, -1);
722 } else if (macherr
) {
723 warnx("couldn't lock kext manager for reboot: %s",
724 mach_error_string(macherr
));
727 if (rval
&& myport
!= MACH_PORT_NULL
) {
728 mach_port_mod_refs(tport
, myport
, MACH_PORT_RIGHT_RECEIVE
, -1);