]> git.saurik.com Git - apple/system_cmds.git/blob - shutdown.tproj/shutdown.c
system_cmds-230.7.tar.gz
[apple/system_cmds.git] / shutdown.tproj / shutdown.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
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
13 * file.
14 *
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.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /*
26 * Copyright (c) 1988, 1990, 1993
27 * The Regents of the University of California. All rights reserved.
28 *
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
31 * are met:
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.
44 *
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
55 * SUCH DAMAGE.
56 */
57
58 #include <sys/param.h>
59 #include <sys/time.h>
60 #include <sys/resource.h>
61 #include <sys/syslog.h>
62
63 #include <ctype.h>
64 #include <fcntl.h>
65 #include <pwd.h>
66 #include <setjmp.h>
67 #include <signal.h>
68 #include <stdio.h>
69 #include <stdlib.h>
70 #include <string.h>
71 #include <tzfile.h>
72 #include <unistd.h>
73
74 #include "pathnames.h"
75
76 #ifdef DEBUG
77 #undef _PATH_NOLOGIN
78 #define _PATH_NOLOGIN "./nologin"
79 #undef _PATH_FASTBOOT
80 #define _PATH_FASTBOOT "./fastboot"
81 #endif
82
83 #define H *60*60
84 #define M *60
85 #define S *1
86 #define NOLOG_TIME 5*60
87 struct interval {
88 int timeleft, timetowait;
89 } tlist[] = {
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,
93 0, 0,
94 };
95 #undef H
96 #undef M
97 #undef S
98
99 static time_t offset, shuttime;
100 static int dofast, dohalt, doreboot, killflg, mbuflen;
101 static char *nosync, *whom, mbuf[BUFSIZ];
102
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));
113
114 int
115 main(argc, argv)
116 int argc;
117 char *argv[];
118 {
119 extern int optind;
120 register char *p, *endp;
121 struct passwd *pw;
122 int arglen, ch, len, readstdin;
123
124 #ifndef DEBUG
125 if (geteuid()) {
126 (void)fprintf(stderr, "shutdown: NOT super-user\n");
127 exit(1);
128 }
129 #endif
130 nosync = NULL;
131 readstdin = 0;
132 while ((ch = getopt(argc, argv, "-fhknr")) != EOF)
133 switch (ch) {
134 case '-':
135 readstdin = 1;
136 break;
137 case 'f':
138 dofast = 1;
139 break;
140 case 'h':
141 dohalt = 1;
142 break;
143 case 'k':
144 killflg = 1;
145 break;
146 case 'n':
147 nosync = "-n";
148 break;
149 case 'r':
150 doreboot = 1;
151 break;
152 case '?':
153 default:
154 usage();
155 }
156 argc -= optind;
157 argv += optind;
158
159 if (argc < 1)
160 usage();
161
162 if (dofast && nosync) {
163 (void)fprintf(stderr,
164 "shutdown: incompatible switches -f and -n.\n");
165 usage();
166 }
167 if (doreboot && dohalt) {
168 (void)fprintf(stderr,
169 "shutdown: incompatible switches -h and -r.\n");
170 usage();
171 }
172 getoffset(*argv++);
173
174 if (*argv) {
175 for (p = mbuf, len = sizeof(mbuf); *argv; ++argv) {
176 arglen = strlen(*argv);
177 if ((len -= arglen) <= 2)
178 break;
179 if (p != mbuf)
180 *p++ = ' ';
181 memmove(p, *argv, arglen);
182 p += arglen;
183 }
184 *p = '\n';
185 *++p = '\0';
186 }
187
188 if (readstdin) {
189 p = mbuf;
190 endp = mbuf + sizeof(mbuf) - 2;
191 for (;;) {
192 if (!fgets(p, endp - p + 1, stdin))
193 break;
194 for (; *p && p < endp; ++p);
195 if (p == endp) {
196 *p = '\n';
197 *++p = '\0';
198 break;
199 }
200 }
201 }
202 mbuflen = strlen(mbuf);
203
204 if (offset)
205 (void)printf("Shutdown at %.24s.\n", ctime(&shuttime));
206 else
207 (void)printf("Shutdown NOW!\n");
208
209 if (!(whom = getlogin()))
210 whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???";
211
212 #ifdef DEBUG
213 (void)putc('\n', stdout);
214 #else
215 (void)setpriority(PRIO_PROCESS, 0, PRIO_MIN);
216 {
217 int forkpid;
218
219 forkpid = fork();
220 if (forkpid == -1) {
221 perror("shutdown: fork");
222 exit(1);
223 }
224 if (forkpid) {
225 (void)printf("shutdown: [pid %d]\n", forkpid);
226 exit(0);
227 }
228 }
229 #endif
230 openlog("shutdown", LOG_CONS, LOG_AUTH);
231 loop();
232 /* NOTREACHED */
233 }
234
235 void
236 loop()
237 {
238 struct interval *tp;
239 u_int sltime;
240 int logged;
241
242 if (offset <= NOLOG_TIME) {
243 logged = 1;
244 nolog();
245 }
246 else
247 logged = 0;
248 tp = tlist;
249 if (tp->timeleft < offset)
250 (void)sleep((u_int)(offset - tp->timeleft));
251 else {
252 while (offset < tp->timeleft)
253 ++tp;
254 /*
255 * Warn now, if going to sleep more than a fifth of
256 * the next wait time.
257 */
258 if (sltime = offset - tp->timeleft) {
259 if (sltime > tp->timetowait / 5)
260 timewarn(offset);
261 (void)sleep(sltime);
262 }
263 }
264 for (;; ++tp) {
265 timewarn(tp->timeleft);
266 if (!logged && tp->timeleft <= NOLOG_TIME) {
267 logged = 1;
268 nolog();
269 }
270 (void)sleep((u_int)tp->timetowait);
271 if (!tp->timeleft)
272 break;
273 }
274 die_you_gravy_sucking_pig_dog();
275 }
276
277 static jmp_buf alarmbuf;
278
279 void
280 timewarn(timeleft)
281 int timeleft;
282 {
283 static int first;
284 static char hostname[MAXHOSTNAMELEN + 1];
285 FILE *pf;
286 char wcmd[MAXPATHLEN + 4];
287
288 if (!first++)
289 (void)gethostname(hostname, sizeof(hostname));
290
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);
295 return;
296 }
297
298 (void)fprintf(pf,
299 "\007*** %sSystem shutdown message from %s@%s ***\007\n",
300 timeleft ? "": "FINAL ", whom, hostname);
301
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" : "");
308 else if (timeleft)
309 (void)fprintf(pf, "System going down in 30 seconds\n\n");
310 else
311 (void)fprintf(pf, "System going down IMMEDIATELY\n\n");
312
313 if (mbuflen)
314 (void)fwrite(mbuf, sizeof(*mbuf), mbuflen, pf);
315
316 /*
317 * play some games, just in case wall doesn't come back
318 * probably unecessary, given that wall is careful.
319 */
320 if (!setjmp(alarmbuf)) {
321 (void)signal(SIGALRM, timeout);
322 (void)alarm((u_int)30);
323 (void)pclose(pf);
324 (void)alarm((u_int)0);
325 (void)signal(SIGALRM, SIG_DFL);
326 }
327 }
328
329 void
330 timeout(signo)
331 int signo;
332 {
333 longjmp(alarmbuf, 1);
334 }
335
336 void
337 die_you_gravy_sucking_pig_dog()
338 {
339
340 syslog(LOG_NOTICE, "%s by %s: %s",
341 doreboot ? "reboot" : dohalt ? "halt" : "shutdown", whom, mbuf);
342 (void)sleep(2);
343
344 (void)printf("\r\nSystem shutdown time has arrived\007\007\r\n");
345 if (killflg) {
346 (void)printf("\rbut you'll have to do it yourself\r\n");
347 finish(0);
348 }
349 if (dofast)
350 doitfast();
351 #ifdef DEBUG
352 if (doreboot)
353 (void)printf("reboot");
354 else if (dohalt)
355 (void)printf("halt");
356 if (nosync)
357 (void)printf(" no sync");
358 if (dofast)
359 (void)printf(" no fsck");
360 (void)printf("\nkill -HUP 1\n");
361 #else
362 if (doreboot) {
363 execle(_PATH_REBOOT, "reboot", "-l", nosync, 0);
364 syslog(LOG_ERR, "shutdown: can't exec %s: %m.", _PATH_REBOOT);
365 perror("shutdown");
366 }
367 else if (dohalt) {
368 execle(_PATH_HALT, "halt", "-l", nosync, 0);
369 syslog(LOG_ERR, "shutdown: can't exec %s: %m.", _PATH_HALT);
370 perror("shutdown");
371 }
372 (void)kill(1, SIGTERM); /* to single user */
373 #endif
374 finish(0);
375 }
376
377 #define ATOI2(p) (p[0] - '0') * 10 + (p[1] - '0'); p += 2;
378
379 void
380 getoffset(timearg)
381 register char *timearg;
382 {
383 register struct tm *lt;
384 register char *p;
385 time_t now;
386
387 if (!strcasecmp(timearg, "now")) { /* now */
388 offset = 0;
389 return;
390 }
391
392 (void)time(&now);
393 if (*timearg == '+') { /* +minutes */
394 if (!isdigit(*++timearg))
395 badtime();
396 offset = atoi(timearg) * 60;
397 shuttime = now + offset;
398 return;
399 }
400
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) {
405 p[0] = p[1];
406 p[1] = p[2];
407 p[2] = '\0';
408 }
409 else
410 badtime();
411
412 unsetenv("TZ"); /* OUR timezone */
413 lt = localtime(&now); /* current time val */
414
415 switch(strlen(timearg)) {
416 case 10:
417 lt->tm_year = ATOI2(timearg);
418 /* FALLTHROUGH */
419 case 8:
420 lt->tm_mon = ATOI2(timearg);
421 if (--lt->tm_mon < 0 || lt->tm_mon > 11)
422 badtime();
423 /* FALLTHROUGH */
424 case 6:
425 lt->tm_mday = ATOI2(timearg);
426 if (lt->tm_mday < 1 || lt->tm_mday > 31)
427 badtime();
428 /* FALLTHROUGH */
429 case 4:
430 lt->tm_hour = ATOI2(timearg);
431 if (lt->tm_hour < 0 || lt->tm_hour > 23)
432 badtime();
433 lt->tm_min = ATOI2(timearg);
434 if (lt->tm_min < 0 || lt->tm_min > 59)
435 badtime();
436 lt->tm_sec = 0;
437 if ((shuttime = mktime(lt)) == -1)
438 badtime();
439 if ((offset = shuttime - now) < 0) {
440 (void)fprintf(stderr,
441 "shutdown: that time is already past.\n");
442 exit(1);
443 }
444 break;
445 default:
446 badtime();
447 }
448 }
449
450 #define FSMSG "fastboot file for fsck\n"
451 void
452 doitfast()
453 {
454 int fastfd;
455
456 if ((fastfd = open(_PATH_FASTBOOT, O_WRONLY|O_CREAT|O_TRUNC,
457 0664)) >= 0) {
458 (void)write(fastfd, FSMSG, sizeof(FSMSG) - 1);
459 (void)close(fastfd);
460 }
461 }
462
463 #define NOMSG "\n\nNO LOGINS: System going down at "
464 void
465 nolog()
466 {
467 int logfd;
468 char *ct;
469
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,
476 0664)) >= 0) {
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));
482 (void)close(logfd);
483 }
484 }
485
486 void
487 finish(signo)
488 int signo;
489 {
490 if (!killflg)
491 (void)unlink(_PATH_NOLOGIN);
492 exit(0);
493 }
494
495 void
496 badtime()
497 {
498 (void)fprintf(stderr, "shutdown: bad time format.\n");
499 exit(1);
500 }
501
502 void
503 usage()
504 {
505 fprintf(stderr, "usage: shutdown [-fhknr] shutdowntime [ message ]\n");
506 exit(1);
507 }