shutdown.c revision 1.27 1 /* $NetBSD: shutdown.c,v 1.27 1998/06/06 21:18:54 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 1988, 1990, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 __COPYRIGHT("@(#) Copyright (c) 1988, 1990, 1993\n\
39 The Regents of the University of California. All rights reserved.\n");
40 #endif /* not lint */
41
42 #ifndef lint
43 #if 0
44 static char sccsid[] = "@(#)shutdown.c 8.4 (Berkeley) 4/28/95";
45 #else
46 __RCSID("$NetBSD: shutdown.c,v 1.27 1998/06/06 21:18:54 thorpej Exp $");
47 #endif
48 #endif /* not lint */
49
50 #include <sys/param.h>
51 #include <sys/time.h>
52 #include <sys/resource.h>
53 #include <sys/syslog.h>
54
55 #include <ctype.h>
56 #include <err.h>
57 #include <fcntl.h>
58 #include <pwd.h>
59 #include <setjmp.h>
60 #include <signal.h>
61 #include <stdio.h>
62 #include <stdlib.h>
63 #include <string.h>
64 #include <time.h>
65 #include <tzfile.h>
66 #include <unistd.h>
67
68 #include "pathnames.h"
69
70 #ifdef DEBUG
71 #undef _PATH_NOLOGIN
72 #define _PATH_NOLOGIN "./nologin"
73 #undef _PATH_FASTBOOT
74 #define _PATH_FASTBOOT "./fastboot"
75 #endif
76
77 #define H *60*60
78 #define M *60
79 #define S *1
80 #define NOLOG_TIME 5*60
81 struct interval {
82 int timeleft, timetowait;
83 } tlist[] = {
84 { 10 H, 5 H }, { 5 H, 3 H }, { 2 H, 1 H }, { 1 H, 30 M },
85 { 30 M, 10 M }, { 20 M, 10 M }, { 10 M, 5 M }, { 5 M, 3 M },
86 { 2 M, 1 M }, { 1 M, 30 S }, { 30 S, 30 S },
87 { 0, 0 }
88 };
89 #undef H
90 #undef M
91 #undef S
92
93 static time_t offset, shuttime;
94 static int dofast, dohalt, doreboot, killflg, mbuflen, nofork, nosync, dodump;
95 static int dopowerdown;
96 static char *whom, mbuf[BUFSIZ];
97
98 void badtime __P((void));
99 void die_you_gravy_sucking_pig_dog __P((void));
100 void doitfast __P((void));
101 void finish __P((int));
102 void getoffset __P((char *));
103 void loop __P((void));
104 int main __P((int, char *[]));
105 void nolog __P((void));
106 void timeout __P((int));
107 void timewarn __P((int));
108 void usage __P((void));
109
110 int
111 main(argc, argv)
112 int argc;
113 char *argv[];
114 {
115 char *p, *endp;
116 struct passwd *pw;
117 int arglen, ch, len;
118
119 #ifndef DEBUG
120 if (geteuid())
121 errx(1, "NOT super-user");
122 #endif
123 while ((ch = getopt(argc, argv, "Ddfhknpr")) != -1)
124 switch (ch) {
125 case 'd':
126 dodump = 1;
127 break;
128 case 'D':
129 nofork = 1;
130 break;
131 case 'f':
132 dofast = 1;
133 break;
134 case 'p':
135 dopowerdown = 1;
136 /* FALLTHROUGH */
137 case 'h':
138 dohalt = 1;
139 break;
140 case 'k':
141 killflg = 1;
142 break;
143 case 'n':
144 nosync = 1;
145 break;
146 case 'r':
147 doreboot = 1;
148 break;
149 case '?':
150 default:
151 usage();
152 }
153 argc -= optind;
154 argv += optind;
155
156 if (argc < 1)
157 usage();
158
159 if (dodump && !dohalt && !doreboot)
160 doreboot = 1;
161
162 if (dofast && nosync) {
163 warnx("incompatible switches -f and -n");
164 usage();
165 }
166 if (dohalt && doreboot) {
167 const char *which_flag = dopowerdown ? "p" : "h";
168
169 warnx("incompatible switches -%s and -r", which_flag);
170 usage();
171 }
172
173 getoffset(*argv++);
174
175 if (argv[0])
176 if (strcmp(argv[0], "-") || argv[1]) {
177 for (p = mbuf, len = sizeof(mbuf); *argv; ++argv) {
178 arglen = strlen(*argv);
179 if ((len -= arglen) <= 2)
180 break;
181 if (p != mbuf)
182 *p++ = ' ';
183 memmove(p, *argv, arglen);
184 p += arglen;
185 }
186 *p = '\n';
187 *++p = '\0';
188 } else {
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 if (nofork == 0) {
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 #ifdef __GNUC__
234 return 1;
235 #endif
236 }
237
238 void
239 loop()
240 {
241 struct interval *tp;
242 u_int sltime;
243 int logged;
244
245 if (offset <= NOLOG_TIME) {
246 logged = 1;
247 nolog();
248 }
249 else
250 logged = 0;
251 tp = tlist;
252 if (tp->timeleft < offset)
253 (void)sleep((u_int)(offset - tp->timeleft));
254 else {
255 while (offset < tp->timeleft)
256 ++tp;
257 /*
258 * Warn now, if going to sleep more than a fifth of
259 * the next wait time.
260 */
261 if ((sltime = offset - tp->timeleft) != 0) {
262 if (sltime > tp->timetowait / 5)
263 timewarn(offset);
264 (void)sleep(sltime);
265 }
266 }
267 for (;; ++tp) {
268 timewarn(tp->timeleft);
269 if (!logged && tp->timeleft <= NOLOG_TIME) {
270 logged = 1;
271 nolog();
272 }
273 (void)sleep((u_int)tp->timetowait);
274 if (!tp->timeleft)
275 break;
276 }
277 die_you_gravy_sucking_pig_dog();
278 }
279
280 static jmp_buf alarmbuf;
281
282 void
283 timewarn(timeleft)
284 int timeleft;
285 {
286 static int first;
287 static char hostname[MAXHOSTNAMELEN + 1];
288 FILE *pf;
289 char wcmd[MAXPATHLEN + 4];
290
291 if (!first++)
292 (void)gethostname(hostname, sizeof(hostname));
293
294 /* undoc -n option to wall suppresses normal wall banner */
295 (void)snprintf(wcmd, sizeof(wcmd), "%s -n", _PATH_WALL);
296 if (!(pf = popen(wcmd, "w"))) {
297 syslog(LOG_ERR, "shutdown: can't find %s: %m", _PATH_WALL);
298 return;
299 }
300
301 (void)fprintf(pf,
302 "\007*** %sSystem shutdown message from %s@%s ***\007\n",
303 timeleft ? "": "FINAL ", whom, hostname);
304
305 if (timeleft > 10*60)
306 (void)fprintf(pf, "System going down at %5.5s\n\n",
307 ctime(&shuttime) + 11);
308 else if (timeleft > 59)
309 (void)fprintf(pf, "System going down in %d minute%s\n\n",
310 timeleft / 60, (timeleft > 60) ? "s" : "");
311 else if (timeleft)
312 (void)fprintf(pf, "System going down in 30 seconds\n\n");
313 else
314 (void)fprintf(pf, "System going down IMMEDIATELY\n\n");
315
316 if (mbuflen)
317 (void)fwrite(mbuf, sizeof(*mbuf), mbuflen, pf);
318
319 /*
320 * play some games, just in case wall doesn't come back
321 * probably unecessary, given that wall is careful.
322 */
323 if (!setjmp(alarmbuf)) {
324 (void)signal(SIGALRM, timeout);
325 (void)alarm((u_int)30);
326 (void)pclose(pf);
327 (void)alarm((u_int)0);
328 (void)signal(SIGALRM, SIG_DFL);
329 }
330 }
331
332 void
333 timeout(signo)
334 int signo;
335 {
336 longjmp(alarmbuf, 1);
337 }
338
339 void
340 die_you_gravy_sucking_pig_dog()
341 {
342
343 syslog(LOG_NOTICE, "%s by %s: %s",
344 doreboot ? "reboot" : dohalt ? "halt" : "shutdown", whom, mbuf);
345 (void)sleep(2);
346
347 (void)printf("\r\nSystem shutdown time has arrived\007\007\r\n");
348 if (killflg) {
349 (void)printf("\rbut you'll have to do it yourself\r\n");
350 finish(0);
351 }
352 if (dofast)
353 doitfast();
354 if (doreboot || dohalt) {
355 char *args[16], **arg, *path;
356
357 arg = &args[0];
358 if (doreboot) {
359 path = _PATH_REBOOT;
360 *arg++ = "reboot";
361 } else {
362 path = _PATH_HALT;
363 *arg++ = "halt";
364 }
365 if (dodump)
366 *arg++ = "-d";
367 if (nosync)
368 *arg++ = "-n";
369 if (dopowerdown)
370 *arg++ = "-p";
371 *arg++ = "-l";
372 *arg++ = 0;
373 #ifndef DEBUG
374 exect(path, args, (char **)0);
375 syslog(LOG_ERR, "shutdown: can't exec %s: %m", path);
376 perror("shutdown");
377 #else
378 printf("%s", path);
379 for (arg = &args[0]; *arg; arg++)
380 printf(" %s", *arg);
381 printf("\n");
382 #endif
383 } else {
384 #ifndef DEBUG
385 (void)kill(1, SIGTERM); /* to single user */
386 #else
387 printf("kill 1\n");
388 #endif
389 }
390 finish(0);
391 }
392
393 #define ATOI2(s) ((s) += 2, ((s)[-2] - '0') * 10 + ((s)[-1] - '0'))
394
395 void
396 getoffset(timearg)
397 char *timearg;
398 {
399 struct tm *lt;
400 char *p;
401 time_t now;
402 int yearset;
403
404 if (!strcasecmp(timearg, "now")) { /* now */
405 offset = 0;
406 return;
407 }
408
409 (void)time(&now);
410 if (*timearg == '+') { /* +minutes */
411 if (!isdigit(*++timearg))
412 badtime();
413 offset = atoi(timearg) * 60;
414 shuttime = now + offset;
415 return;
416 }
417
418 /* handle hh:mm by getting rid of the colon */
419 for (p = timearg; *p; ++p)
420 if (!isascii(*p) || !isdigit(*p))
421 if (*p == ':' && strlen(p) == 3) {
422 p[0] = p[1];
423 p[1] = p[2];
424 p[2] = '\0';
425 }
426 else
427 badtime();
428
429 unsetenv("TZ"); /* OUR timezone */
430 lt = localtime(&now); /* current time val */
431
432 lt->tm_sec = 0;
433
434 yearset = 0;
435 switch (strlen(timearg)) {
436 case 12:
437 lt->tm_year = ATOI2(timearg) * 100 - TM_YEAR_BASE;
438 yearset = 1;
439 /* FALLTHROUGH */
440 case 10:
441 if (yearset) {
442 lt->tm_year += ATOI2(timearg);
443 } else {
444 yearset = ATOI2(timearg);
445 if (yearset < 69)
446 lt->tm_year = yearset + 2000 - TM_YEAR_BASE;
447 else
448 lt->tm_year = yearset + 1900 - TM_YEAR_BASE;
449 }
450 /* FALLTHROUGH */
451 case 8:
452 lt->tm_mon = ATOI2(timearg);
453 --lt->tm_mon;
454 /* FALLTHROUGH */
455 case 6:
456 lt->tm_mday = ATOI2(timearg);
457 /* FALLTHROUGH */
458 case 4:
459 lt->tm_hour = ATOI2(timearg);
460 /* FALLTHROUGH */
461 case 2:
462 lt->tm_min = ATOI2(timearg);
463 break;
464 default:
465 badtime();
466 }
467
468 if ((shuttime = mktime(lt)) == -1)
469 badtime();
470 if ((offset = shuttime - now) < 0)
471 errx(1, "time is already past");
472 }
473
474 #define FSMSG "fastboot file for fsck\n"
475 void
476 doitfast()
477 {
478 int fastfd;
479
480 if ((fastfd = open(_PATH_FASTBOOT, O_WRONLY|O_CREAT|O_TRUNC,
481 0664)) >= 0) {
482 (void)write(fastfd, FSMSG, sizeof(FSMSG) - 1);
483 (void)close(fastfd);
484 }
485 }
486
487 #define NOMSG "\n\nNO LOGINS: System going down at "
488 void
489 nolog()
490 {
491 int logfd;
492 char *ct;
493
494 (void)unlink(_PATH_NOLOGIN); /* in case linked to another file */
495 (void)signal(SIGINT, finish);
496 (void)signal(SIGHUP, finish);
497 (void)signal(SIGQUIT, finish);
498 (void)signal(SIGTERM, finish);
499 if ((logfd = open(_PATH_NOLOGIN, O_WRONLY|O_CREAT|O_TRUNC,
500 0664)) >= 0) {
501 (void)write(logfd, NOMSG, sizeof(NOMSG) - 1);
502 ct = ctime(&shuttime);
503 (void)write(logfd, ct + 11, 5);
504 (void)write(logfd, "\n\n", 2);
505 (void)write(logfd, mbuf, strlen(mbuf));
506 (void)close(logfd);
507 }
508 }
509
510 void
511 finish(signo)
512 int signo;
513 {
514 if (!killflg)
515 (void)unlink(_PATH_NOLOGIN);
516 exit(0);
517 }
518
519 void
520 badtime()
521 {
522 warnx("illegal time format");
523 usage();
524 }
525
526 void
527 usage()
528 {
529 (void)fprintf(stderr,
530 "usage: shutdown [-Ddfhknr] time [message ... | -]\n");
531 exit(1);
532 }
533