shutdown.c revision 1.25 1 /* $NetBSD: shutdown.c,v 1.25 1998/03/23 05:00:16 fair 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.25 1998/03/23 05:00:16 fair 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 <tzfile.h>
65 #include <unistd.h>
66
67 #include "pathnames.h"
68
69 #ifdef DEBUG
70 #undef _PATH_NOLOGIN
71 #define _PATH_NOLOGIN "./nologin"
72 #undef _PATH_FASTBOOT
73 #define _PATH_FASTBOOT "./fastboot"
74 #endif
75
76 #define H *60*60
77 #define M *60
78 #define S *1
79 #define NOLOG_TIME 5*60
80 struct interval {
81 int timeleft, timetowait;
82 } tlist[] = {
83 { 10 H, 5 H }, { 5 H, 3 H }, { 2 H, 1 H }, { 1 H, 30 M },
84 { 30 M, 10 M }, { 20 M, 10 M }, { 10 M, 5 M }, { 5 M, 3 M },
85 { 2 M, 1 M }, { 1 M, 30 S }, { 30 S, 30 S },
86 { 0, 0 }
87 };
88 #undef H
89 #undef M
90 #undef S
91
92 static time_t offset, shuttime;
93 static int dofast, dohalt, doreboot, killflg, mbuflen, nofork, nosync, dodump;
94 static char *whom, mbuf[BUFSIZ];
95
96 void badtime __P((void));
97 void die_you_gravy_sucking_pig_dog __P((void));
98 void doitfast __P((void));
99 void finish __P((int));
100 void getoffset __P((char *));
101 void loop __P((void));
102 int main __P((int, char *[]));
103 void nolog __P((void));
104 void timeout __P((int));
105 void timewarn __P((int));
106 void usage __P((void));
107
108 int
109 main(argc, argv)
110 int argc;
111 char *argv[];
112 {
113 char *p, *endp;
114 struct passwd *pw;
115 int arglen, ch, len;
116
117 #ifndef DEBUG
118 if (geteuid())
119 errx(1, "NOT super-user");
120 #endif
121 while ((ch = getopt(argc, argv, "Ddfhknr")) != -1)
122 switch (ch) {
123 case 'd':
124 dodump = 1;
125 break;
126 case 'D':
127 nofork = 1;
128 break;
129 case 'f':
130 dofast = 1;
131 break;
132 case 'h':
133 dohalt = 1;
134 break;
135 case 'k':
136 killflg = 1;
137 break;
138 case 'n':
139 nosync = 1;
140 break;
141 case 'r':
142 doreboot = 1;
143 break;
144 case '?':
145 default:
146 usage();
147 }
148 argc -= optind;
149 argv += optind;
150
151 if (argc < 1)
152 usage();
153
154 if (dodump && !dohalt && !doreboot)
155 doreboot = 1;
156
157 if (dofast && nosync) {
158 warnx("incompatible switches -f and -n");
159 usage();
160 }
161 if (dohalt && doreboot) {
162 warnx("incompatible switches -h and -r");
163 usage();
164 }
165
166 getoffset(*argv++);
167
168 if (argv[0])
169 if (strcmp(argv[0], "-") || argv[1]) {
170 for (p = mbuf, len = sizeof(mbuf); *argv; ++argv) {
171 arglen = strlen(*argv);
172 if ((len -= arglen) <= 2)
173 break;
174 if (p != mbuf)
175 *p++ = ' ';
176 memmove(p, *argv, arglen);
177 p += arglen;
178 }
179 *p = '\n';
180 *++p = '\0';
181 } else {
182 p = mbuf;
183 endp = mbuf + sizeof(mbuf) - 2;
184 for (;;) {
185 if (!fgets(p, endp - p + 1, stdin))
186 break;
187 for (; *p && p < endp; ++p);
188 if (p == endp) {
189 *p = '\n';
190 *++p = '\0';
191 break;
192 }
193 }
194 }
195 mbuflen = strlen(mbuf);
196
197 if (offset)
198 (void)printf("Shutdown at %.24s.\n", ctime(&shuttime));
199 else
200 (void)printf("Shutdown NOW!\n");
201
202 if (!(whom = getlogin()))
203 whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???";
204
205 #ifdef DEBUG
206 (void)putc('\n', stdout);
207 #else
208 (void)setpriority(PRIO_PROCESS, 0, PRIO_MIN);
209 if (nofork == 0) {
210 int forkpid;
211
212 forkpid = fork();
213 if (forkpid == -1) {
214 perror("shutdown: fork");
215 exit(1);
216 }
217 if (forkpid) {
218 (void)printf("shutdown: [pid %d]\n", forkpid);
219 exit(0);
220 }
221 }
222 #endif
223 openlog("shutdown", LOG_CONS, LOG_AUTH);
224 loop();
225 /* NOTREACHED */
226 #ifdef __GNUC__
227 return 1;
228 #endif
229 }
230
231 void
232 loop()
233 {
234 struct interval *tp;
235 u_int sltime;
236 int logged;
237
238 if (offset <= NOLOG_TIME) {
239 logged = 1;
240 nolog();
241 }
242 else
243 logged = 0;
244 tp = tlist;
245 if (tp->timeleft < offset)
246 (void)sleep((u_int)(offset - tp->timeleft));
247 else {
248 while (offset < tp->timeleft)
249 ++tp;
250 /*
251 * Warn now, if going to sleep more than a fifth of
252 * the next wait time.
253 */
254 if ((sltime = offset - tp->timeleft) != 0) {
255 if (sltime > tp->timetowait / 5)
256 timewarn(offset);
257 (void)sleep(sltime);
258 }
259 }
260 for (;; ++tp) {
261 timewarn(tp->timeleft);
262 if (!logged && tp->timeleft <= NOLOG_TIME) {
263 logged = 1;
264 nolog();
265 }
266 (void)sleep((u_int)tp->timetowait);
267 if (!tp->timeleft)
268 break;
269 }
270 die_you_gravy_sucking_pig_dog();
271 }
272
273 static jmp_buf alarmbuf;
274
275 void
276 timewarn(timeleft)
277 int timeleft;
278 {
279 static int first;
280 static char hostname[MAXHOSTNAMELEN + 1];
281 FILE *pf;
282 char wcmd[MAXPATHLEN + 4];
283
284 if (!first++)
285 (void)gethostname(hostname, sizeof(hostname));
286
287 /* undoc -n option to wall suppresses normal wall banner */
288 (void)snprintf(wcmd, sizeof(wcmd), "%s -n", _PATH_WALL);
289 if (!(pf = popen(wcmd, "w"))) {
290 syslog(LOG_ERR, "shutdown: can't find %s: %m", _PATH_WALL);
291 return;
292 }
293
294 (void)fprintf(pf,
295 "\007*** %sSystem shutdown message from %s@%s ***\007\n",
296 timeleft ? "": "FINAL ", whom, hostname);
297
298 if (timeleft > 10*60)
299 (void)fprintf(pf, "System going down at %5.5s\n\n",
300 ctime(&shuttime) + 11);
301 else if (timeleft > 59)
302 (void)fprintf(pf, "System going down in %d minute%s\n\n",
303 timeleft / 60, (timeleft > 60) ? "s" : "");
304 else if (timeleft)
305 (void)fprintf(pf, "System going down in 30 seconds\n\n");
306 else
307 (void)fprintf(pf, "System going down IMMEDIATELY\n\n");
308
309 if (mbuflen)
310 (void)fwrite(mbuf, sizeof(*mbuf), mbuflen, pf);
311
312 /*
313 * play some games, just in case wall doesn't come back
314 * probably unecessary, given that wall is careful.
315 */
316 if (!setjmp(alarmbuf)) {
317 (void)signal(SIGALRM, timeout);
318 (void)alarm((u_int)30);
319 (void)pclose(pf);
320 (void)alarm((u_int)0);
321 (void)signal(SIGALRM, SIG_DFL);
322 }
323 }
324
325 void
326 timeout(signo)
327 int signo;
328 {
329 longjmp(alarmbuf, 1);
330 }
331
332 void
333 die_you_gravy_sucking_pig_dog()
334 {
335
336 syslog(LOG_NOTICE, "%s by %s: %s",
337 doreboot ? "reboot" : dohalt ? "halt" : "shutdown", whom, mbuf);
338 (void)sleep(2);
339
340 (void)printf("\r\nSystem shutdown time has arrived\007\007\r\n");
341 if (killflg) {
342 (void)printf("\rbut you'll have to do it yourself\r\n");
343 finish(0);
344 }
345 if (dofast)
346 doitfast();
347 if (doreboot || dohalt) {
348 char *args[8], **arg, *path;
349
350 arg = &args[0];
351 if (doreboot) {
352 path = _PATH_REBOOT;
353 *arg++ = "reboot";
354 } else {
355 path = _PATH_HALT;
356 *arg++ = "halt";
357 }
358 if (dodump)
359 *arg++ = "-d";
360 if (nosync)
361 *arg++ = "-n";
362 *arg++ = "-l";
363 *arg++ = 0;
364 #ifndef DEBUG
365 exect(path, args, (char **)0);
366 syslog(LOG_ERR, "shutdown: can't exec %s: %m", path);
367 perror("shutdown");
368 #else
369 printf("%s", path);
370 for (arg = &args[0]; *arg; arg++)
371 printf(" %s", *arg);
372 printf("\n");
373 #endif
374 } else {
375 #ifndef DEBUG
376 (void)kill(1, SIGTERM); /* to single user */
377 #else
378 printf("kill 1\n");
379 #endif
380 }
381 finish(0);
382 }
383
384 #define ATOI2(s) ((s) += 2, ((s)[-2] - '0') * 10 + ((s)[-1] - '0'))
385
386 void
387 getoffset(timearg)
388 char *timearg;
389 {
390 struct tm *lt;
391 char *p;
392 time_t now;
393 int yearset;
394
395 if (!strcasecmp(timearg, "now")) { /* now */
396 offset = 0;
397 return;
398 }
399
400 (void)time(&now);
401 if (*timearg == '+') { /* +minutes */
402 if (!isdigit(*++timearg))
403 badtime();
404 offset = atoi(timearg) * 60;
405 shuttime = now + offset;
406 return;
407 }
408
409 /* handle hh:mm by getting rid of the colon */
410 for (p = timearg; *p; ++p)
411 if (!isascii(*p) || !isdigit(*p))
412 if (*p == ':' && strlen(p) == 3) {
413 p[0] = p[1];
414 p[1] = p[2];
415 p[2] = '\0';
416 }
417 else
418 badtime();
419
420 unsetenv("TZ"); /* OUR timezone */
421 lt = localtime(&now); /* current time val */
422
423 lt->tm_sec = 0;
424
425 yearset = 0;
426 switch (strlen(timearg)) {
427 case 12:
428 lt->tm_year = ATOI2(timearg) * 100 - TM_YEAR_BASE;
429 yearset = 1;
430 /* FALLTHROUGH */
431 case 10:
432 if (yearset) {
433 lt->tm_year += ATOI2(timearg);
434 } else {
435 yearset = ATOI2(timearg);
436 if (yearset < 69)
437 lt->tm_year = yearset + 2000 - TM_YEAR_BASE;
438 else
439 lt->tm_year = yearset + 1900 - TM_YEAR_BASE;
440 }
441 /* FALLTHROUGH */
442 case 8:
443 lt->tm_mon = ATOI2(timearg);
444 --lt->tm_mon;
445 /* FALLTHROUGH */
446 case 6:
447 lt->tm_mday = ATOI2(timearg);
448 /* FALLTHROUGH */
449 case 4:
450 lt->tm_hour = ATOI2(timearg);
451 /* FALLTHROUGH */
452 case 2:
453 lt->tm_min = ATOI2(timearg);
454 break;
455 default:
456 badtime();
457 }
458
459 if ((shuttime = mktime(lt)) == -1)
460 badtime();
461 if ((offset = shuttime - now) < 0)
462 errx(1, "time is already past");
463 }
464
465 #define FSMSG "fastboot file for fsck\n"
466 void
467 doitfast()
468 {
469 int fastfd;
470
471 if ((fastfd = open(_PATH_FASTBOOT, O_WRONLY|O_CREAT|O_TRUNC,
472 0664)) >= 0) {
473 (void)write(fastfd, FSMSG, sizeof(FSMSG) - 1);
474 (void)close(fastfd);
475 }
476 }
477
478 #define NOMSG "\n\nNO LOGINS: System going down at "
479 void
480 nolog()
481 {
482 int logfd;
483 char *ct;
484
485 (void)unlink(_PATH_NOLOGIN); /* in case linked to another file */
486 (void)signal(SIGINT, finish);
487 (void)signal(SIGHUP, finish);
488 (void)signal(SIGQUIT, finish);
489 (void)signal(SIGTERM, finish);
490 if ((logfd = open(_PATH_NOLOGIN, O_WRONLY|O_CREAT|O_TRUNC,
491 0664)) >= 0) {
492 (void)write(logfd, NOMSG, sizeof(NOMSG) - 1);
493 ct = ctime(&shuttime);
494 (void)write(logfd, ct + 11, 5);
495 (void)write(logfd, "\n\n", 2);
496 (void)write(logfd, mbuf, strlen(mbuf));
497 (void)close(logfd);
498 }
499 }
500
501 void
502 finish(signo)
503 int signo;
504 {
505 if (!killflg)
506 (void)unlink(_PATH_NOLOGIN);
507 exit(0);
508 }
509
510 void
511 badtime()
512 {
513 warnx("illegal time format");
514 usage();
515 }
516
517 void
518 usage()
519 {
520 (void)fprintf(stderr,
521 "usage: shutdown [-Ddfhknr] time [message ... | -]\n");
522 exit(1);
523 }
524