2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * Copyright (c) 1988, 1990, 1993
27 * The Regents of the University of California. All rights reserved.
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
32 * 1. Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in the
36 * documentation and/or other materials provided with the distribution.
37 * 3. All advertising materials mentioning features or use of this software
38 * must display the following acknowledgement:
39 * This product includes software developed by the University of
40 * California, Berkeley and its contributors.
41 * 4. Neither the name of the University nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
45 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 #include <sys/param.h>
60 #include <sys/resource.h>
61 #include <sys/syslog.h>
74 #include "pathnames.h"
78 #define _PATH_NOLOGIN "./nologin"
80 #define _PATH_FASTBOOT "./fastboot"
86 #define NOLOG_TIME 5*60
88 int timeleft
, timetowait
;
90 10 H
, 5 H
, 5 H
, 3 H
, 2 H
, 1 H
, 1 H
, 30 M
,
91 30 M
, 10 M
, 20 M
, 10 M
, 10 M
, 5 M
, 5 M
, 3 M
,
92 2 M
, 1 M
, 1 M
, 30 S
, 30 S
, 30 S
,
99 static time_t offset
, shuttime
;
100 static int dofast
, dohalt
, doreboot
, killflg
, mbuflen
;
101 static char *nosync
, *whom
, mbuf
[BUFSIZ
];
103 void badtime
__P((void));
104 void die_you_gravy_sucking_pig_dog
__P((void));
105 void doitfast
__P((void));
106 void finish
__P((int));
107 void getoffset
__P((char *));
108 void loop
__P((void));
109 void nolog
__P((void));
110 void timeout
__P((int));
111 void timewarn
__P((int));
112 void usage
__P((void));
120 register char *p
, *endp
;
122 int arglen
, ch
, len
, readstdin
;
126 (void)fprintf(stderr
, "shutdown: NOT super-user\n");
132 while ((ch
= getopt(argc
, argv
, "-fhknr")) != EOF
)
162 if (dofast
&& nosync
) {
163 (void)fprintf(stderr
,
164 "shutdown: incompatible switches -f and -n.\n");
167 if (doreboot
&& dohalt
) {
168 (void)fprintf(stderr
,
169 "shutdown: incompatible switches -h and -r.\n");
175 for (p
= mbuf
, len
= sizeof(mbuf
); *argv
; ++argv
) {
176 arglen
= strlen(*argv
);
177 if ((len
-= arglen
) <= 2)
181 memmove(p
, *argv
, arglen
);
190 endp
= mbuf
+ sizeof(mbuf
) - 2;
192 if (!fgets(p
, endp
- p
+ 1, stdin
))
194 for (; *p
&& p
< endp
; ++p
);
202 mbuflen
= strlen(mbuf
);
205 (void)printf("Shutdown at %.24s.\n", ctime(&shuttime
));
207 (void)printf("Shutdown NOW!\n");
209 if (!(whom
= getlogin()))
210 whom
= (pw
= getpwuid(getuid())) ? pw
->pw_name
: "???";
213 (void)putc('\n', stdout
);
215 (void)setpriority(PRIO_PROCESS
, 0, PRIO_MIN
);
221 perror("shutdown: fork");
225 (void)printf("shutdown: [pid %d]\n", forkpid
);
230 openlog("shutdown", LOG_CONS
, LOG_AUTH
);
242 if (offset
<= NOLOG_TIME
) {
249 if (tp
->timeleft
< offset
)
250 (void)sleep((u_int
)(offset
- tp
->timeleft
));
252 while (offset
< tp
->timeleft
)
255 * Warn now, if going to sleep more than a fifth of
256 * the next wait time.
258 if (sltime
= offset
- tp
->timeleft
) {
259 if (sltime
> tp
->timetowait
/ 5)
265 timewarn(tp
->timeleft
);
266 if (!logged
&& tp
->timeleft
<= NOLOG_TIME
) {
270 (void)sleep((u_int
)tp
->timetowait
);
274 die_you_gravy_sucking_pig_dog();
277 static jmp_buf alarmbuf
;
284 static char hostname
[MAXHOSTNAMELEN
+ 1];
286 char wcmd
[MAXPATHLEN
+ 4];
289 (void)gethostname(hostname
, sizeof(hostname
));
291 /* undoc -n option to wall suppresses normal wall banner */
292 (void)snprintf(wcmd
, sizeof(wcmd
), "%s -n", _PATH_WALL
);
293 if (!(pf
= popen(wcmd
, "w"))) {
294 syslog(LOG_ERR
, "shutdown: can't find %s: %m", _PATH_WALL
);
299 "\007*** %sSystem shutdown message from %s@%s ***\007\n",
300 timeleft
? "": "FINAL ", whom
, hostname
);
302 if (timeleft
> 10*60)
303 (void)fprintf(pf
, "System going down at %5.5s\n\n",
304 ctime(&shuttime
) + 11);
305 else if (timeleft
> 59)
306 (void)fprintf(pf
, "System going down in %d minute%s\n\n",
307 timeleft
/ 60, (timeleft
> 60) ? "s" : "");
309 (void)fprintf(pf
, "System going down in 30 seconds\n\n");
311 (void)fprintf(pf
, "System going down IMMEDIATELY\n\n");
314 (void)fwrite(mbuf
, sizeof(*mbuf
), mbuflen
, pf
);
317 * play some games, just in case wall doesn't come back
318 * probably unecessary, given that wall is careful.
320 if (!setjmp(alarmbuf
)) {
321 (void)signal(SIGALRM
, timeout
);
322 (void)alarm((u_int
)30);
324 (void)alarm((u_int
)0);
325 (void)signal(SIGALRM
, SIG_DFL
);
333 longjmp(alarmbuf
, 1);
337 die_you_gravy_sucking_pig_dog()
340 syslog(LOG_NOTICE
, "%s by %s: %s",
341 doreboot
? "reboot" : dohalt
? "halt" : "shutdown", whom
, mbuf
);
344 (void)printf("\r\nSystem shutdown time has arrived\007\007\r\n");
346 (void)printf("\rbut you'll have to do it yourself\r\n");
353 (void)printf("reboot");
355 (void)printf("halt");
357 (void)printf(" no sync");
359 (void)printf(" no fsck");
360 (void)printf("\nkill -HUP 1\n");
363 execle(_PATH_REBOOT
, "reboot", "-l", nosync
, 0);
364 syslog(LOG_ERR
, "shutdown: can't exec %s: %m.", _PATH_REBOOT
);
368 execle(_PATH_HALT
, "halt", "-l", nosync
, 0);
369 syslog(LOG_ERR
, "shutdown: can't exec %s: %m.", _PATH_HALT
);
372 (void)kill(1, SIGTERM
); /* to single user */
377 #define ATOI2(p) (p[0] - '0') * 10 + (p[1] - '0'); p += 2;
381 register char *timearg
;
383 register struct tm
*lt
;
387 if (!strcasecmp(timearg
, "now")) { /* now */
393 if (*timearg
== '+') { /* +minutes */
394 if (!isdigit(*++timearg
))
396 offset
= atoi(timearg
) * 60;
397 shuttime
= now
+ offset
;
401 /* handle hh:mm by getting rid of the colon */
402 for (p
= timearg
; *p
; ++p
)
403 if (!isascii(*p
) || !isdigit(*p
))
404 if (*p
== ':' && strlen(p
) == 3) {
412 unsetenv("TZ"); /* OUR timezone */
413 lt
= localtime(&now
); /* current time val */
415 switch(strlen(timearg
)) {
417 lt
->tm_year
= ATOI2(timearg
);
420 lt
->tm_mon
= ATOI2(timearg
);
421 if (--lt
->tm_mon
< 0 || lt
->tm_mon
> 11)
425 lt
->tm_mday
= ATOI2(timearg
);
426 if (lt
->tm_mday
< 1 || lt
->tm_mday
> 31)
430 lt
->tm_hour
= ATOI2(timearg
);
431 if (lt
->tm_hour
< 0 || lt
->tm_hour
> 23)
433 lt
->tm_min
= ATOI2(timearg
);
434 if (lt
->tm_min
< 0 || lt
->tm_min
> 59)
437 if ((shuttime
= mktime(lt
)) == -1)
439 if ((offset
= shuttime
- now
) < 0) {
440 (void)fprintf(stderr
,
441 "shutdown: that time is already past.\n");
450 #define FSMSG "fastboot file for fsck\n"
456 if ((fastfd
= open(_PATH_FASTBOOT
, O_WRONLY
|O_CREAT
|O_TRUNC
,
458 (void)write(fastfd
, FSMSG
, sizeof(FSMSG
) - 1);
463 #define NOMSG "\n\nNO LOGINS: System going down at "
470 (void)unlink(_PATH_NOLOGIN
); /* in case linked to another file */
471 (void)signal(SIGINT
, finish
);
472 (void)signal(SIGHUP
, finish
);
473 (void)signal(SIGQUIT
, finish
);
474 (void)signal(SIGTERM
, finish
);
475 if ((logfd
= open(_PATH_NOLOGIN
, O_WRONLY
|O_CREAT
|O_TRUNC
,
477 (void)write(logfd
, NOMSG
, sizeof(NOMSG
) - 1);
478 ct
= ctime(&shuttime
);
479 (void)write(logfd
, ct
+ 11, 5);
480 (void)write(logfd
, "\n\n", 2);
481 (void)write(logfd
, mbuf
, strlen(mbuf
));
491 (void)unlink(_PATH_NOLOGIN
);
498 (void)fprintf(stderr
, "shutdown: bad time format.\n");
505 fprintf(stderr
, "usage: shutdown [-fhknr] shutdowntime [ message ]\n");