shutdown.c revision 1.28 1 /* $NetBSD: shutdown.c,v 1.28 1998/07/05 08:34:25 mrg 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.28 1998/07/05 08:34:25 mrg 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 hostname[sizeof(hostname) - 1] = '\0';
294 }
295
296 /* undoc -n option to wall suppresses normal wall banner */
297 (void)snprintf(wcmd, sizeof wcmd, "%s -n", _PATH_WALL);
298 if (!(pf = popen(wcmd, "w"))) {
299 syslog(LOG_ERR, "shutdown: can't find %s: %m", _PATH_WALL);
300 return;
301 }
302
303 (void)fprintf(pf,
304 "\007*** %sSystem shutdown message from %s@%s ***\007\n",
305 timeleft ? "": "FINAL ", whom, hostname);
306
307 if (timeleft > 10*60)
308 (void)fprintf(pf, "System going down at %5.5s\n\n",
309 ctime(&shuttime) + 11);
310 else if (timeleft > 59)
311 (void)fprintf(pf, "System going down in %d minute%s\n\n",
312 timeleft / 60, (timeleft > 60) ? "s" : "");
313 else if (timeleft)
314 (void)fprintf(pf, "System going down in 30 seconds\n\n");
315 else
316 (void)fprintf(pf, "System going down IMMEDIATELY\n\n");
317
318 if (mbuflen)
319 (void)fwrite(mbuf, sizeof(*mbuf), mbuflen, pf);
320
321 /*
322 * play some games, just in case wall doesn't come back
323 * probably unecessary, given that wall is careful.
324 */
325 if (!setjmp(alarmbuf)) {
326 (void)signal(SIGALRM, timeout);
327 (void)alarm((u_int)30);
328 (void)pclose(pf);
329 (void)alarm((u_int)0);
330 (void)signal(SIGALRM, SIG_DFL);
331 }
332 }
333
334 void
335 timeout(signo)
336 int signo;
337 {
338 longjmp(alarmbuf, 1);
339 }
340
341 void
342 die_you_gravy_sucking_pig_dog()
343 {
344
345 syslog(LOG_NOTICE, "%s by %s: %s",
346 doreboot ? "reboot" : dohalt ? "halt" : "shutdown", whom, mbuf);
347 (void)sleep(2);
348
349 (void)printf("\r\nSystem shutdown time has arrived\007\007\r\n");
350 if (killflg) {
351 (void)printf("\rbut you'll have to do it yourself\r\n");
352 finish(0);
353 }
354 if (dofast)
355 doitfast();
356 if (doreboot || dohalt) {
357 char *args[16], **arg, *path;
358
359 arg = &args[0];
360 if (doreboot) {
361 path = _PATH_REBOOT;
362 *arg++ = "reboot";
363 } else {
364 path = _PATH_HALT;
365 *arg++ = "halt";
366 }
367 if (dodump)
368 *arg++ = "-d";
369 if (nosync)
370 *arg++ = "-n";
371 if (dopowerdown)
372 *arg++ = "-p";
373 *arg++ = "-l";
374 *arg++ = 0;
375 #ifndef DEBUG
376 exect(path, args, (char **)0);
377 syslog(LOG_ERR, "shutdown: can't exec %s: %m", path);
378 perror("shutdown");
379 #else
380 printf("%s", path);
381 for (arg = &args[0]; *arg; arg++)
382 printf(" %s", *arg);
383 printf("\n");
384 #endif
385 } else {
386 #ifndef DEBUG
387 (void)kill(1, SIGTERM); /* to single user */
388 #else
389 printf("kill 1\n");
390 #endif
391 }
392 finish(0);
393 }
394
395 #define ATOI2(s) ((s) += 2, ((s)[-2] - '0') * 10 + ((s)[-1] - '0'))
396
397 void
398 getoffset(timearg)
399 char *timearg;
400 {
401 struct tm *lt;
402 char *p;
403 time_t now;
404 int yearset;
405
406 if (!strcasecmp(timearg, "now")) { /* now */
407 offset = 0;
408 return;
409 }
410
411 (void)time(&now);
412 if (*timearg == '+') { /* +minutes */
413 if (!isdigit(*++timearg))
414 badtime();
415 offset = atoi(timearg) * 60;
416 shuttime = now + offset;
417 return;
418 }
419
420 /* handle hh:mm by getting rid of the colon */
421 for (p = timearg; *p; ++p)
422 if (!isascii(*p) || !isdigit(*p))
423 if (*p == ':' && strlen(p) == 3) {
424 p[0] = p[1];
425 p[1] = p[2];
426 p[2] = '\0';
427 }
428 else
429 badtime();
430
431 unsetenv("TZ"); /* OUR timezone */
432 lt = localtime(&now); /* current time val */
433
434 lt->tm_sec = 0;
435
436 yearset = 0;
437 switch (strlen(timearg)) {
438 case 12:
439 lt->tm_year = ATOI2(timearg) * 100 - TM_YEAR_BASE;
440 yearset = 1;
441 /* FALLTHROUGH */
442 case 10:
443 if (yearset) {
444 lt->tm_year += ATOI2(timearg);
445 } else {
446 yearset = ATOI2(timearg);
447 if (yearset < 69)
448 lt->tm_year = yearset + 2000 - TM_YEAR_BASE;
449 else
450 lt->tm_year = yearset + 1900 - TM_YEAR_BASE;
451 }
452 /* FALLTHROUGH */
453 case 8:
454 lt->tm_mon = ATOI2(timearg);
455 --lt->tm_mon;
456 /* FALLTHROUGH */
457 case 6:
458 lt->tm_mday = ATOI2(timearg);
459 /* FALLTHROUGH */
460 case 4:
461 lt->tm_hour = ATOI2(timearg);
462 /* FALLTHROUGH */
463 case 2:
464 lt->tm_min = ATOI2(timearg);
465 break;
466 default:
467 badtime();
468 }
469
470 if ((shuttime = mktime(lt)) == -1)
471 badtime();
472 if ((offset = shuttime - now) < 0)
473 errx(1, "time is already past");
474 }
475
476 #define FSMSG "fastboot file for fsck\n"
477 void
478 doitfast()
479 {
480 int fastfd;
481
482 if ((fastfd = open(_PATH_FASTBOOT, O_WRONLY|O_CREAT|O_TRUNC,
483 0664)) >= 0) {
484 (void)write(fastfd, FSMSG, sizeof(FSMSG) - 1);
485 (void)close(fastfd);
486 }
487 }
488
489 #define NOMSG "\n\nNO LOGINS: System going down at "
490 void
491 nolog()
492 {
493 int logfd;
494 char *ct;
495
496 (void)unlink(_PATH_NOLOGIN); /* in case linked to another file */
497 (void)signal(SIGINT, finish);
498 (void)signal(SIGHUP, finish);
499 (void)signal(SIGQUIT, finish);
500 (void)signal(SIGTERM, finish);
501 if ((logfd = open(_PATH_NOLOGIN, O_WRONLY|O_CREAT|O_TRUNC,
502 0664)) >= 0) {
503 (void)write(logfd, NOMSG, sizeof(NOMSG) - 1);
504 ct = ctime(&shuttime);
505 (void)write(logfd, ct + 11, 5);
506 (void)write(logfd, "\n\n", 2);
507 (void)write(logfd, mbuf, strlen(mbuf));
508 (void)close(logfd);
509 }
510 }
511
512 void
513 finish(signo)
514 int signo;
515 {
516
517 if (!killflg)
518 (void)unlink(_PATH_NOLOGIN);
519 exit(0);
520 }
521
522 void
523 badtime()
524 {
525
526 warnx("illegal time format");
527 usage();
528 }
529
530 void
531 usage()
532 {
533
534 (void)fprintf(stderr,
535 "usage: shutdown [-Ddfhknr] time [message ... | -]\n");
536 exit(1);
537 }
538