]> git.saurik.com Git - apple/system_cmds.git/blob - shutdown.tproj/shutdown.c
system_cmds-258.tar.gz
[apple/system_cmds.git] / shutdown.tproj / shutdown.c
1 /*
2 * Copyright (c) 1988, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
20 *
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
31 * SUCH DAMAGE.
32 */
33
34 #ifndef lint
35 static const char copyright[] =
36 "@(#) Copyright (c) 1988, 1990, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
38 #endif /* not lint */
39
40 #ifndef lint
41 #if 0
42 static char sccsid[] = "@(#)shutdown.c 8.4 (Berkeley) 4/28/95";
43 #endif
44 static const char rcsid[] =
45 "$FreeBSD: src/sbin/shutdown/shutdown.c,v 1.23 2002/03/21 13:20:48 imp Exp $";
46 #endif /* not lint */
47
48 #include <sys/param.h>
49 #include <sys/time.h>
50 #include <sys/resource.h>
51 #include <sys/syslog.h>
52
53 #include <ctype.h>
54 #include <err.h>
55 #include <fcntl.h>
56 #include <pwd.h>
57 #include <setjmp.h>
58 #include <signal.h>
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <string.h>
62 #include <unistd.h>
63
64 #include "pathnames.h"
65
66 #ifdef __APPLE__
67 #define __unused
68 #endif
69
70 #ifdef DEBUG
71 #undef _PATH_NOLOGIN
72 #define _PATH_NOLOGIN "./nologin"
73 #endif
74
75 #define H *60*60
76 #define M *60
77 #define S *1
78 #define NOLOG_TIME 5*60
79 struct interval {
80 int timeleft, timetowait;
81 } tlist[] = {
82 { 10 H, 5 H },
83 { 5 H, 3 H },
84 { 2 H, 1 H },
85 { 1 H, 30 M },
86 { 30 M, 10 M },
87 { 20 M, 10 M },
88 { 10 M, 5 M },
89 { 5 M, 3 M },
90 { 2 M, 1 M },
91 { 1 M, 30 S },
92 { 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 dohalt, dopower, doreboot, killflg, mbuflen, oflag = 1;
101 static char mbuf[BUFSIZ];
102 static const char *nosync, *whom;
103
104 void badtime(void);
105 void die_you_gravy_sucking_pig_dog(void);
106 void finish(int);
107 void getoffset(char *);
108 void loop(void);
109 void nolog(void);
110 void timeout(int);
111 void timewarn(int);
112 void usage(const char *);
113
114 int
115 main(argc, argv)
116 int argc;
117 char *argv[];
118 {
119 char *p, *endp;
120 struct passwd *pw;
121 int arglen, ch, len, readstdin;
122
123 #ifndef DEBUG
124 if (geteuid())
125 errx(1, "NOT super-user");
126 #endif
127 nosync = NULL;
128 readstdin = 0;
129 #ifndef __APPLE__
130 while ((ch = getopt(argc, argv, "-hknopr")) != -1)
131 #else
132 while ((ch = getopt(argc, argv, "-hknor")) != -1)
133 #endif
134 switch (ch) {
135 case '-':
136 readstdin = 1;
137 break;
138 case 'h':
139 dohalt = 1;
140 break;
141 case 'k':
142 killflg = 1;
143 break;
144 case 'n':
145 nosync = "-n";
146 break;
147 case 'o':
148 oflag = 1;
149 break;
150 #ifndef __APPLE__
151 case 'p':
152 dopower = 1;
153 break;
154 #endif
155 case 'r':
156 doreboot = 1;
157 break;
158 case '?':
159 default:
160 usage((char *)NULL);
161 }
162 argc -= optind;
163 argv += optind;
164
165 if (argc < 1)
166 usage((char *)NULL);
167
168 #ifndef __APPLE__
169 if (killflg + doreboot + dohalt + dopower > 1)
170 usage("incompatible switches -h, -k, -p and -r");
171
172 if (oflag && !(dohalt || dopower || doreboot))
173 usage("-o requires -h, -p or -r");
174 #else
175 if (killflg + doreboot + dohalt > 1)
176 usage("incompatible switches -h, -k, and -r");
177
178 if (oflag && !(dohalt || doreboot))
179 usage("-o requires -h or -r");
180 #endif
181
182 if (nosync != NULL && !oflag)
183 usage("-n requires -o");
184
185 getoffset(*argv++);
186
187 if (*argv) {
188 for (p = mbuf, len = sizeof(mbuf); *argv; ++argv) {
189 arglen = strlen(*argv);
190 if ((len -= arglen) <= 2)
191 break;
192 if (p != mbuf)
193 *p++ = ' ';
194 memmove(p, *argv, arglen);
195 p += arglen;
196 }
197 *p = '\n';
198 *++p = '\0';
199 }
200
201 if (readstdin) {
202 p = mbuf;
203 endp = mbuf + sizeof(mbuf) - 2;
204 for (;;) {
205 if (!fgets(p, endp - p + 1, stdin))
206 break;
207 for (; *p && p < endp; ++p);
208 if (p == endp) {
209 *p = '\n';
210 *++p = '\0';
211 break;
212 }
213 }
214 }
215 mbuflen = strlen(mbuf);
216
217 if (offset)
218 (void)printf("Shutdown at %.24s.\n", ctime(&shuttime));
219 else
220 (void)printf("Shutdown NOW!\n");
221
222 if (!(whom = getlogin()))
223 whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???";
224
225 #ifdef DEBUG
226 (void)putc('\n', stdout);
227 #else
228 (void)setpriority(PRIO_PROCESS, 0, PRIO_MIN);
229 {
230 int forkpid;
231
232 forkpid = fork();
233 if (forkpid == -1)
234 err(1, "fork");
235 if (forkpid)
236 errx(0, "[pid %d]", forkpid);
237 }
238 setsid();
239 #endif
240 openlog("shutdown", LOG_CONS, LOG_AUTH);
241 loop();
242 return(0);
243 }
244
245 void
246 loop()
247 {
248 struct interval *tp;
249 u_int sltime;
250 int logged;
251
252 if (offset <= NOLOG_TIME) {
253 logged = 1;
254 nolog();
255 }
256 else
257 logged = 0;
258 tp = tlist;
259 if (tp->timeleft < offset)
260 (void)sleep((u_int)(offset - tp->timeleft));
261 else {
262 while (tp->timeleft && offset < tp->timeleft)
263 ++tp;
264 /*
265 * Warn now, if going to sleep more than a fifth of
266 * the next wait time.
267 */
268 if ((sltime = offset - tp->timeleft)) {
269 if (sltime > (u_int)(tp->timetowait / 5))
270 timewarn(offset);
271 (void)sleep(sltime);
272 }
273 }
274 for (;; ++tp) {
275 timewarn(tp->timeleft);
276 if (!logged && tp->timeleft <= NOLOG_TIME) {
277 logged = 1;
278 nolog();
279 }
280 (void)sleep((u_int)tp->timetowait);
281 if (!tp->timeleft)
282 break;
283 }
284 die_you_gravy_sucking_pig_dog();
285 }
286
287 static jmp_buf alarmbuf;
288
289 static const char *restricted_environ[] = {
290 "PATH=" _PATH_STDPATH,
291 NULL
292 };
293
294 void
295 timewarn(timeleft)
296 int timeleft;
297 {
298 static int first;
299 static char hostname[MAXHOSTNAMELEN + 1];
300 FILE *pf;
301 char wcmd[MAXPATHLEN + 4];
302 extern const char **environ;
303
304 if (!first++)
305 (void)gethostname(hostname, sizeof(hostname));
306
307 /* undoc -n option to wall suppresses normal wall banner */
308 (void)snprintf(wcmd, sizeof(wcmd), "%s -n", _PATH_WALL);
309 environ = restricted_environ;
310 if (!(pf = popen(wcmd, "w"))) {
311 syslog(LOG_ERR, "shutdown: can't find %s: %m", _PATH_WALL);
312 return;
313 }
314
315 (void)fprintf(pf,
316 "\007*** %sSystem shutdown message from %s@%s ***\007\n",
317 timeleft ? "": "FINAL ", whom, hostname);
318
319 if (timeleft > 10*60)
320 (void)fprintf(pf, "System going down at %5.5s\n\n",
321 ctime(&shuttime) + 11);
322 else if (timeleft > 59)
323 (void)fprintf(pf, "System going down in %d minute%s\n\n",
324 timeleft / 60, (timeleft > 60) ? "s" : "");
325 else if (timeleft)
326 (void)fprintf(pf, "System going down in 30 seconds\n\n");
327 else
328 (void)fprintf(pf, "System going down IMMEDIATELY\n\n");
329
330 if (mbuflen)
331 (void)fwrite(mbuf, sizeof(*mbuf), mbuflen, pf);
332
333 /*
334 * play some games, just in case wall doesn't come back
335 * probably unnecessary, given that wall is careful.
336 */
337 if (!setjmp(alarmbuf)) {
338 (void)signal(SIGALRM, timeout);
339 (void)alarm((u_int)30);
340 (void)pclose(pf);
341 (void)alarm((u_int)0);
342 (void)signal(SIGALRM, SIG_DFL);
343 }
344 }
345
346 void
347 timeout(signo)
348 int signo __unused;
349 {
350 longjmp(alarmbuf, 1);
351 }
352
353 void
354 die_you_gravy_sucking_pig_dog()
355 {
356 char *empty_environ[] = { NULL };
357
358 syslog(LOG_NOTICE, "%s by %s: %s",
359 #ifndef __APPLE__
360 doreboot ? "reboot" : dohalt ? "halt" : dopower ? "power-down" :
361 #else
362 doreboot ? "reboot" : dohalt ? "halt" :
363 #endif
364 "shutdown", whom, mbuf);
365 (void)sleep(2);
366
367 (void)printf("\r\nSystem shutdown time has arrived\007\007\r\n");
368 if (killflg) {
369 (void)printf("\rbut you'll have to do it yourself\r\n");
370 exit(0);
371 }
372 #ifdef DEBUG
373 if (doreboot)
374 (void)printf("reboot");
375 else if (dohalt)
376 (void)printf("halt");
377 #ifndef __APPLE__
378 else if (dopower)
379 (void)printf("power-down");
380 #endif
381 if (nosync != NULL)
382 (void)printf(" no sync");
383 (void)printf("\nkill -HUP 1\n");
384 #else
385 if (!oflag) {
386 (void)kill(1, doreboot ? SIGINT : /* reboot */
387 dohalt ? SIGUSR1 : /* halt */
388 #ifndef __APPLE__
389 dopower ? SIGUSR2 : /* power-down */
390 #endif
391 SIGTERM); /* single-user */
392 } else {
393 if (doreboot) {
394 execle(_PATH_REBOOT, "reboot", "-l", nosync,
395 (char *)NULL, empty_environ);
396 syslog(LOG_ERR, "shutdown: can't exec %s: %m.",
397 _PATH_REBOOT);
398 warn(_PATH_REBOOT);
399 }
400 else if (dohalt) {
401 execle(_PATH_HALT, "halt", "-l", nosync,
402 (char *)NULL, empty_environ);
403 syslog(LOG_ERR, "shutdown: can't exec %s: %m.",
404 _PATH_HALT);
405 warn(_PATH_HALT);
406 }
407 #ifndef __APPLE__
408 else if (dopower) {
409 execle(_PATH_HALT, "halt", "-l", "-p", nosync,
410 (char *)NULL, empty_environ);
411 syslog(LOG_ERR, "shutdown: can't exec %s: %m.",
412 _PATH_HALT);
413 warn(_PATH_HALT);
414 }
415 #endif
416 (void)kill(1, SIGTERM); /* to single-user */
417 }
418 #endif
419 finish(0);
420 }
421
422 #define ATOI2(p) (p[0] - '0') * 10 + (p[1] - '0'); p += 2;
423
424 void
425 getoffset(timearg)
426 char *timearg;
427 {
428 struct tm *lt;
429 char *p;
430 time_t now;
431 int this_year;
432
433 (void)time(&now);
434
435 if (!strcasecmp(timearg, "now")) { /* now */
436 offset = 0;
437 shuttime = now;
438 return;
439 }
440
441 if (*timearg == '+') { /* +minutes */
442 if (!isdigit(*++timearg))
443 badtime();
444 if ((offset = atoi(timearg) * 60) < 0)
445 badtime();
446 shuttime = now + offset;
447 return;
448 }
449
450 /* handle hh:mm by getting rid of the colon */
451 for (p = timearg; *p; ++p)
452 if (!isascii(*p) || !isdigit(*p)) {
453 if (*p == ':' && strlen(p) == 3) {
454 p[0] = p[1];
455 p[1] = p[2];
456 p[2] = '\0';
457 }
458 else
459 badtime();
460 }
461
462 unsetenv("TZ"); /* OUR timezone */
463 lt = localtime(&now); /* current time val */
464
465 switch(strlen(timearg)) {
466 case 10:
467 this_year = lt->tm_year;
468 lt->tm_year = ATOI2(timearg);
469 /*
470 * check if the specified year is in the next century.
471 * allow for one year of user error as many people will
472 * enter n - 1 at the start of year n.
473 */
474 if (lt->tm_year < (this_year % 100) - 1)
475 lt->tm_year += 100;
476 /* adjust for the year 2000 and beyond */
477 lt->tm_year += (this_year - (this_year % 100));
478 /* FALLTHROUGH */
479 case 8:
480 lt->tm_mon = ATOI2(timearg);
481 if (--lt->tm_mon < 0 || lt->tm_mon > 11)
482 badtime();
483 /* FALLTHROUGH */
484 case 6:
485 lt->tm_mday = ATOI2(timearg);
486 if (lt->tm_mday < 1 || lt->tm_mday > 31)
487 badtime();
488 /* FALLTHROUGH */
489 case 4:
490 lt->tm_hour = ATOI2(timearg);
491 if (lt->tm_hour < 0 || lt->tm_hour > 23)
492 badtime();
493 lt->tm_min = ATOI2(timearg);
494 if (lt->tm_min < 0 || lt->tm_min > 59)
495 badtime();
496 lt->tm_sec = 0;
497 if ((shuttime = mktime(lt)) == -1)
498 badtime();
499 if ((offset = shuttime - now) < 0)
500 errx(1, "that time is already past.");
501 break;
502 default:
503 badtime();
504 }
505 }
506
507 #define NOMSG "\n\nNO LOGINS: System going down at "
508 void
509 nolog()
510 {
511 #ifndef __APPLE__
512 int logfd;
513 char *ct;
514
515 (void)unlink(_PATH_NOLOGIN); /* in case linked to another file */
516 (void)signal(SIGINT, finish);
517 (void)signal(SIGHUP, finish);
518 (void)signal(SIGQUIT, finish);
519 (void)signal(SIGTERM, finish);
520 if ((logfd = open(_PATH_NOLOGIN, O_WRONLY|O_CREAT|O_TRUNC,
521 0664)) >= 0) {
522 (void)write(logfd, NOMSG, sizeof(NOMSG) - 1);
523 ct = ctime(&shuttime);
524 (void)write(logfd, ct + 11, 5);
525 (void)write(logfd, "\n\n", 2);
526 (void)write(logfd, mbuf, strlen(mbuf));
527 (void)close(logfd);
528 }
529 #endif
530 }
531
532 void
533 finish(signo)
534 int signo __unused;
535 {
536 #ifndef __APPLE__
537 if (!killflg)
538 (void)unlink(_PATH_NOLOGIN);
539 #endif
540 exit(0);
541 }
542
543 void
544 badtime()
545 {
546 errx(1, "bad time format");
547 }
548
549 void
550 usage(cp)
551 const char *cp;
552 {
553 if (cp != NULL)
554 warnx("%s", cp);
555 (void)fprintf(stderr,
556 "usage: shutdown [-] [-h | -p | -r | -k] [-o [-n]]"
557 " time [warning-message ...]\n");
558 exit(1);
559 }