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