2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License'). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
25 * Copyright (c) 1988, 1990, 1993
26 * The Regents of the University of California. All rights reserved.
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
31 * 1. Redistributions of source code must retain the above copyright
32 * notice, this list of conditions and the following disclaimer.
33 * 2. Redistributions in binary form must reproduce the above copyright
34 * notice, this list of conditions and the following disclaimer in the
35 * documentation and/or other materials provided with the distribution.
36 * 3. All advertising materials mentioning features or use of this software
37 * must display the following acknowledgement:
38 * This product includes software developed by the University of
39 * California, Berkeley and its contributors.
40 * 4. Neither the name of the University nor the names of its contributors
41 * may be used to endorse or promote products derived from this software
42 * without specific prior written permission.
44 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
45 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57 #include <sys/param.h>
59 #include <sys/resource.h>
60 #include <sys/syslog.h>
73 #include "pathnames.h"
77 #define _PATH_NOLOGIN "./nologin"
79 #define _PATH_FASTBOOT "./fastboot"
85 #define NOLOG_TIME 5*60
87 int timeleft
, timetowait
;
89 10 H
, 5 H
, 5 H
, 3 H
, 2 H
, 1 H
, 1 H
, 30 M
,
90 30 M
, 10 M
, 20 M
, 10 M
, 10 M
, 5 M
, 5 M
, 3 M
,
91 2 M
, 1 M
, 1 M
, 30 S
, 30 S
, 30 S
,
98 static time_t offset
, shuttime
;
99 static int dofast
, dohalt
, doreboot
, killflg
, mbuflen
;
100 static char *nosync
, *whom
, mbuf
[BUFSIZ
];
102 void badtime
__P((void));
103 void die_you_gravy_sucking_pig_dog
__P((void));
104 void doitfast
__P((void));
105 void finish
__P((int));
106 void getoffset
__P((char *));
107 void loop
__P((void));
108 void nolog
__P((void));
109 void timeout
__P((int));
110 void timewarn
__P((int));
111 void usage
__P((void));
119 register char *p
, *endp
;
121 int arglen
, ch
, len
, readstdin
;
125 (void)fprintf(stderr
, "shutdown: NOT super-user\n");
131 while ((ch
= getopt(argc
, argv
, "-fhknr")) != EOF
)
161 if (dofast
&& nosync
) {
162 (void)fprintf(stderr
,
163 "shutdown: incompatible switches -f and -n.\n");
166 if (doreboot
&& dohalt
) {
167 (void)fprintf(stderr
,
168 "shutdown: incompatible switches -h and -r.\n");
174 for (p
= mbuf
, len
= sizeof(mbuf
); *argv
; ++argv
) {
175 arglen
= strlen(*argv
);
176 if ((len
-= arglen
) <= 2)
180 memmove(p
, *argv
, arglen
);
189 endp
= mbuf
+ sizeof(mbuf
) - 2;
191 if (!fgets(p
, endp
- p
+ 1, stdin
))
193 for (; *p
&& p
< endp
; ++p
);
201 mbuflen
= strlen(mbuf
);
204 (void)printf("Shutdown at %.24s.\n", ctime(&shuttime
));
206 (void)printf("Shutdown NOW!\n");
208 if (!(whom
= getlogin()))
209 whom
= (pw
= getpwuid(getuid())) ? pw
->pw_name
: "???";
212 (void)putc('\n', stdout
);
214 (void)setpriority(PRIO_PROCESS
, 0, PRIO_MIN
);
220 perror("shutdown: fork");
224 (void)printf("shutdown: [pid %d]\n", forkpid
);
229 openlog("shutdown", LOG_CONS
, LOG_AUTH
);
241 if (offset
<= NOLOG_TIME
) {
248 if (tp
->timeleft
< offset
)
249 (void)sleep((u_int
)(offset
- tp
->timeleft
));
251 while (offset
< tp
->timeleft
)
254 * Warn now, if going to sleep more than a fifth of
255 * the next wait time.
257 if (sltime
= offset
- tp
->timeleft
) {
258 if (sltime
> tp
->timetowait
/ 5)
264 timewarn(tp
->timeleft
);
265 if (!logged
&& tp
->timeleft
<= NOLOG_TIME
) {
269 (void)sleep((u_int
)tp
->timetowait
);
273 die_you_gravy_sucking_pig_dog();
276 static jmp_buf alarmbuf
;
283 static char hostname
[MAXHOSTNAMELEN
+ 1];
285 char wcmd
[MAXPATHLEN
+ 4];
288 (void)gethostname(hostname
, sizeof(hostname
));
290 /* undoc -n option to wall suppresses normal wall banner */
291 (void)snprintf(wcmd
, sizeof(wcmd
), "%s -n", _PATH_WALL
);
292 if (!(pf
= popen(wcmd
, "w"))) {
293 syslog(LOG_ERR
, "shutdown: can't find %s: %m", _PATH_WALL
);
298 "\007*** %sSystem shutdown message from %s@%s ***\007\n",
299 timeleft
? "": "FINAL ", whom
, hostname
);
301 if (timeleft
> 10*60)
302 (void)fprintf(pf
, "System going down at %5.5s\n\n",
303 ctime(&shuttime
) + 11);
304 else if (timeleft
> 59)
305 (void)fprintf(pf
, "System going down in %d minute%s\n\n",
306 timeleft
/ 60, (timeleft
> 60) ? "s" : "");
308 (void)fprintf(pf
, "System going down in 30 seconds\n\n");
310 (void)fprintf(pf
, "System going down IMMEDIATELY\n\n");
313 (void)fwrite(mbuf
, sizeof(*mbuf
), mbuflen
, pf
);
316 * play some games, just in case wall doesn't come back
317 * probably unecessary, given that wall is careful.
319 if (!setjmp(alarmbuf
)) {
320 (void)signal(SIGALRM
, timeout
);
321 (void)alarm((u_int
)30);
323 (void)alarm((u_int
)0);
324 (void)signal(SIGALRM
, SIG_DFL
);
332 longjmp(alarmbuf
, 1);
336 die_you_gravy_sucking_pig_dog()
339 syslog(LOG_NOTICE
, "%s by %s: %s",
340 doreboot
? "reboot" : dohalt
? "halt" : "shutdown", whom
, mbuf
);
343 (void)printf("\r\nSystem shutdown time has arrived\007\007\r\n");
345 (void)printf("\rbut you'll have to do it yourself\r\n");
352 (void)printf("reboot");
354 (void)printf("halt");
356 (void)printf(" no sync");
358 (void)printf(" no fsck");
359 (void)printf("\nkill -HUP 1\n");
362 execle(_PATH_REBOOT
, "reboot", "-l", nosync
, 0);
363 syslog(LOG_ERR
, "shutdown: can't exec %s: %m.", _PATH_REBOOT
);
367 execle(_PATH_HALT
, "halt", "-l", nosync
, 0);
368 syslog(LOG_ERR
, "shutdown: can't exec %s: %m.", _PATH_HALT
);
371 (void)kill(1, SIGTERM
); /* to single user */
376 #define ATOI2(p) (p[0] - '0') * 10 + (p[1] - '0'); p += 2;
380 register char *timearg
;
382 register struct tm
*lt
;
386 if (!strcasecmp(timearg
, "now")) { /* now */
392 if (*timearg
== '+') { /* +minutes */
393 if (!isdigit(*++timearg
))
395 offset
= atoi(timearg
) * 60;
396 shuttime
= now
+ offset
;
400 /* handle hh:mm by getting rid of the colon */
401 for (p
= timearg
; *p
; ++p
)
402 if (!isascii(*p
) || !isdigit(*p
))
403 if (*p
== ':' && strlen(p
) == 3) {
411 unsetenv("TZ"); /* OUR timezone */
412 lt
= localtime(&now
); /* current time val */
414 switch(strlen(timearg
)) {
416 lt
->tm_year
= ATOI2(timearg
);
419 lt
->tm_mon
= ATOI2(timearg
);
420 if (--lt
->tm_mon
< 0 || lt
->tm_mon
> 11)
424 lt
->tm_mday
= ATOI2(timearg
);
425 if (lt
->tm_mday
< 1 || lt
->tm_mday
> 31)
429 lt
->tm_hour
= ATOI2(timearg
);
430 if (lt
->tm_hour
< 0 || lt
->tm_hour
> 23)
432 lt
->tm_min
= ATOI2(timearg
);
433 if (lt
->tm_min
< 0 || lt
->tm_min
> 59)
436 if ((shuttime
= mktime(lt
)) == -1)
438 if ((offset
= shuttime
- now
) < 0) {
439 (void)fprintf(stderr
,
440 "shutdown: that time is already past.\n");
449 #define FSMSG "fastboot file for fsck\n"
455 if ((fastfd
= open(_PATH_FASTBOOT
, O_WRONLY
|O_CREAT
|O_TRUNC
,
457 (void)write(fastfd
, FSMSG
, sizeof(FSMSG
) - 1);
462 #define NOMSG "\n\nNO LOGINS: System going down at "
469 (void)unlink(_PATH_NOLOGIN
); /* in case linked to another file */
470 (void)signal(SIGINT
, finish
);
471 (void)signal(SIGHUP
, finish
);
472 (void)signal(SIGQUIT
, finish
);
473 (void)signal(SIGTERM
, finish
);
474 if ((logfd
= open(_PATH_NOLOGIN
, O_WRONLY
|O_CREAT
|O_TRUNC
,
476 (void)write(logfd
, NOMSG
, sizeof(NOMSG
) - 1);
477 ct
= ctime(&shuttime
);
478 (void)write(logfd
, ct
+ 11, 5);
479 (void)write(logfd
, "\n\n", 2);
480 (void)write(logfd
, mbuf
, strlen(mbuf
));
490 (void)unlink(_PATH_NOLOGIN
);
497 (void)fprintf(stderr
, "shutdown: bad time format.\n");
504 fprintf(stderr
, "usage: shutdown [-fhknr] shutdowntime [ message ]\n");