Sync usage with man page.
[netbsd-mini2440.git] / sbin / shutdown / shutdown.c
blob32a77f7ff773b17ccb6c96c74b07f22bfa2a6fcc
1 /* $NetBSD: shutdown.c,v 1.50 2008/02/09 04:09:22 dholland Exp $ */
3 /*
4 * Copyright (c) 1988, 1990, 1993
5 * The Regents of the University of California. All rights reserved.
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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __COPYRIGHT("@(#) Copyright (c) 1988, 1990, 1993\
35 The Regents of the University of California. All rights reserved.");
36 #endif /* not lint */
38 #ifndef lint
39 #if 0
40 static char sccsid[] = "@(#)shutdown.c 8.4 (Berkeley) 4/28/95";
41 #else
42 __RCSID("$NetBSD: shutdown.c,v 1.50 2008/02/09 04:09:22 dholland Exp $");
43 #endif
44 #endif /* not lint */
46 #include <sys/param.h>
47 #include <sys/time.h>
48 #include <sys/resource.h>
49 #include <sys/syslog.h>
51 #include <ctype.h>
52 #include <err.h>
53 #include <fcntl.h>
54 #include <pwd.h>
55 #include <setjmp.h>
56 #include <signal.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include <time.h>
61 #include <tzfile.h>
62 #include <unistd.h>
63 #include <errno.h>
65 #include "pathnames.h"
67 #ifdef DEBUG
68 #undef _PATH_NOLOGIN
69 #define _PATH_NOLOGIN "./nologin"
70 #undef _PATH_FASTBOOT
71 #define _PATH_FASTBOOT "./fastboot"
72 #endif
74 #define H *60*60
75 #define M *60
76 #define S *1
77 #define NOLOG_TIME 5*60
78 static const struct interval {
79 time_t timeleft, timetowait;
80 } tlist[] = {
81 { 10 H, 5 H }, { 5 H, 3 H }, { 2 H, 1 H }, { 1 H, 30 M },
82 { 30 M, 10 M }, { 20 M, 10 M }, { 10 M, 5 M }, { 5 M, 3 M },
83 { 2 M, 1 M }, { 1 M, 30 S }, { 30 S, 30 S },
84 { 0, 0 }
86 #undef H
87 #undef M
88 #undef S
90 static time_t offset, shuttime;
91 static int dofast, dohalt, doreboot, killflg, nofork, nosync, dodump;
92 static size_t mbuflen;
93 static int dopowerdown;
94 static const char *whom;
95 static char mbuf[BUFSIZ];
96 static char *bootstr;
98 static void badtime(void) __dead;
99 static void die_you_gravy_sucking_pig_dog(void) __dead;
100 static void doitfast(void);
101 void dorcshutdown(void);
102 static void finish(int) __dead;
103 static void getoffset(char *);
104 static void loop(void);
105 static void nolog(void);
106 static void timeout(int);
107 static void timewarn(time_t);
108 static void usage(void) __dead;
111 main(int argc, char *argv[])
113 char *p, *endp;
114 struct passwd *pw;
115 size_t arglen, len;
116 int ch;
118 (void)setprogname(argv[0]);
119 #ifndef DEBUG
120 if (geteuid())
121 errx(1, "%s: Not super-user", strerror(EPERM));
122 #endif
123 while ((ch = getopt(argc, argv, "b:Ddfhknpr")) != -1)
124 switch (ch) {
125 case 'b':
126 bootstr = optarg;
127 break;
128 case 'd':
129 dodump = 1;
130 break;
131 case 'D':
132 nofork = 1;
133 break;
134 case 'f':
135 dofast = 1;
136 break;
137 case 'p':
138 dopowerdown = 1;
139 /* FALLTHROUGH */
140 case 'h':
141 dohalt = 1;
142 break;
143 case 'k':
144 killflg = 1;
145 break;
146 case 'n':
147 nosync = 1;
148 break;
149 case 'r':
150 doreboot = 1;
151 break;
152 case '?':
153 default:
154 usage();
156 argc -= optind;
157 argv += optind;
159 if (argc < 1)
160 usage();
162 if (dodump && !dohalt && !doreboot)
163 doreboot = 1;
165 if (dofast && nosync) {
166 warnx("Incompatible options -f and -n");
167 usage();
169 if (dohalt && doreboot) {
170 const char *which_flag = dopowerdown ? "p" : "h";
172 warnx("Incompatible options -%s and -r", which_flag);
173 usage();
176 getoffset(*argv++);
178 if (argv[0]) {
179 if (strcmp(argv[0], "-") || argv[1]) {
180 for (p = mbuf, len = sizeof(mbuf); *argv; ++argv) {
181 arglen = strlen(*argv);
182 if ((len -= arglen) <= 2)
183 break;
184 if (p != mbuf)
185 *p++ = ' ';
186 (void)memmove(p, *argv, arglen);
187 p += arglen;
189 *p = '\n';
190 *++p = '\0';
191 } else {
192 p = mbuf;
193 endp = mbuf + sizeof(mbuf) - 2;
194 for (;;) {
195 if (!fgets(p, endp - p + 1, stdin))
196 break;
197 for (; *p && p < endp; ++p);
198 if (p == endp) {
199 *p = '\n';
200 *++p = '\0';
201 break;
206 mbuflen = strlen(mbuf);
208 if (offset)
209 (void)printf("Shutdown at %.24s.\n", ctime(&shuttime));
210 else
211 (void)printf("Shutdown NOW!\n");
213 if (!(whom = getlogin()))
214 whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???";
216 #ifdef DEBUG
217 (void)putc('\n', stdout);
218 #else
219 (void)setpriority(PRIO_PROCESS, 0, PRIO_MIN);
220 if (nofork == 0) {
221 int forkpid;
223 forkpid = fork();
224 if (forkpid == -1) {
225 perror("shutdown: fork");
226 exit(1);
228 if (forkpid) {
229 (void)printf("shutdown: [pid %d]\n", forkpid);
230 exit(0);
232 (void)setsid();
234 #endif
235 openlog("shutdown", LOG_CONS, LOG_AUTH);
236 loop();
237 /* NOTREACHED */
238 #ifdef __GNUC__
239 return 1;
240 #endif
243 void
244 loop(void)
246 const struct interval *tp;
247 u_int sltime;
248 int logged;
250 if (offset <= NOLOG_TIME) {
251 logged = 1;
252 nolog();
254 else
255 logged = 0;
256 tp = tlist;
257 if (tp->timeleft < offset)
258 (void)sleep((u_int)(offset - tp->timeleft));
259 else {
260 while (offset < tp->timeleft)
261 ++tp;
263 * Warn now, if going to sleep more than a fifth of
264 * the next wait time.
266 if ((sltime = offset - tp->timeleft) != 0) {
267 if (sltime > tp->timetowait / 5)
268 timewarn(offset);
269 (void)sleep(sltime);
272 for (;; ++tp) {
273 timewarn(tp->timeleft);
274 if (!logged && tp->timeleft <= NOLOG_TIME) {
275 logged = 1;
276 nolog();
278 (void)sleep((u_int)tp->timetowait);
279 if (!tp->timeleft)
280 break;
282 die_you_gravy_sucking_pig_dog();
285 static jmp_buf alarmbuf;
287 void
288 timewarn(time_t timeleft)
290 static int first;
291 static char hostname[MAXHOSTNAMELEN + 1];
292 FILE *pf;
293 char wcmd[MAXPATHLEN + 4];
295 if (!first++) {
296 (void)gethostname(hostname, sizeof(hostname));
297 hostname[sizeof(hostname) - 1] = '\0';
300 /* undoc -n option to wall suppresses normal wall banner */
301 (void)snprintf(wcmd, sizeof wcmd, "%s -n", _PATH_WALL);
302 if ((pf = popen(wcmd, "w")) == NULL) {
303 syslog(LOG_ERR, "%s: Can't find `%s' (%m)", getprogname(),
304 _PATH_WALL);
305 return;
308 (void)fprintf(pf,
309 "\007*** %sSystem shutdown message from %s@%s ***\007\n",
310 timeleft ? "": "FINAL ", whom, hostname);
312 if (timeleft > 10*60)
313 (void)fprintf(pf, "System going down at %5.5s\n\n",
314 ctime(&shuttime) + 11);
315 else if (timeleft > 59)
316 (void)fprintf(pf, "System going down in %ld minute%s\n\n",
317 (long)timeleft / 60, (timeleft > 60) ? "s" : "");
318 else if (timeleft)
319 (void)fprintf(pf, "System going down in 30 seconds\n\n");
320 else
321 (void)fprintf(pf, "System going down IMMEDIATELY\n\n");
323 if (mbuflen)
324 (void)fwrite(mbuf, sizeof(*mbuf), mbuflen, pf);
327 * play some games, just in case wall doesn't come back
328 * probably unnecessary, given that wall is careful.
330 if (!setjmp(alarmbuf)) {
331 (void)signal(SIGALRM, timeout);
332 (void)alarm((u_int)30);
333 (void)pclose(pf);
334 (void)alarm((u_int)0);
335 (void)signal(SIGALRM, SIG_DFL);
339 static void
340 /*ARGSUSED*/
341 timeout(int signo)
343 longjmp(alarmbuf, 1);
346 static void
347 die_you_gravy_sucking_pig_dog(void)
349 const char *what;
351 if (doreboot) {
352 what = "reboot";
353 } else if (dohalt && dopowerdown) {
354 what = "poweroff";
355 } else if (dohalt) {
356 what = "halt";
357 } else {
358 what = "shutdown";
361 syslog(LOG_NOTICE, "%s by %s: %s", what, whom, mbuf);
362 (void)sleep(2);
364 (void)printf("\r\nSystem shutdown time has arrived\007\007\r\n");
365 if (killflg) {
366 (void)printf("\rbut you'll have to do it yourself\r\n");
367 finish(0);
369 if (dofast)
370 doitfast();
371 dorcshutdown();
372 if (doreboot || dohalt) {
373 const char *args[16];
374 const char **arg, *path;
375 #ifndef DEBUG
376 int serrno;
377 #endif
379 arg = &args[0];
380 if (doreboot) {
381 path = _PATH_REBOOT;
382 *arg++ = "reboot";
383 } else {
384 path = _PATH_HALT;
385 *arg++ = "halt";
387 if (dodump)
388 *arg++ = "-d";
389 if (nosync)
390 *arg++ = "-n";
391 if (dopowerdown)
392 *arg++ = "-p";
393 *arg++ = "-l";
394 if (bootstr)
395 *arg++ = bootstr;
396 *arg++ = 0;
397 #ifndef DEBUG
398 (void)execve(path, __UNCONST(args), NULL);
399 serrno = errno;
400 syslog(LOG_ERR, "Can't exec `%s' (%m)", path);
401 errno = serrno;
402 warn("Can't exec `%s'", path);
403 #else
404 printf("%s", path);
405 for (arg = &args[0]; *arg; arg++)
406 printf(" %s", *arg);
407 printf("\n");
408 #endif
409 } else {
410 #ifndef DEBUG
411 (void)kill(1, SIGTERM); /* to single user */
412 #else
413 printf("kill 1\n");
414 #endif
416 finish(0);
419 #define ATOI2(s) ((s) += 2, ((s)[-2] - '0') * 10 + ((s)[-1] - '0'))
421 void
422 getoffset(char *timearg)
424 struct tm *lt;
425 char *p;
426 time_t now;
427 int yearset;
429 (void)time(&now);
430 if (!strcasecmp(timearg, "now")) { /* now */
431 offset = 0;
432 shuttime = now;
433 return;
436 if (*timearg == '+') { /* +minutes */
437 if (!isdigit((unsigned char)*++timearg))
438 badtime();
439 offset = atoi(timearg) * 60;
440 shuttime = now + offset;
441 return;
444 /* handle hh:mm by getting rid of the colon */
445 for (p = timearg; *p; ++p)
446 if (!isascii(*p) || !isdigit((unsigned char)*p)) {
447 if (*p == ':' && strlen(p) == 3) {
448 p[0] = p[1];
449 p[1] = p[2];
450 p[2] = '\0';
452 else
453 badtime();
456 (void)unsetenv("TZ"); /* OUR timezone */
457 lt = localtime(&now); /* current time val */
459 lt->tm_sec = 0;
461 yearset = 0;
462 switch (strlen(timearg)) {
463 case 12:
464 lt->tm_year = ATOI2(timearg) * 100 - TM_YEAR_BASE;
465 yearset = 1;
466 /* FALLTHROUGH */
467 case 10:
468 if (yearset) {
469 lt->tm_year += ATOI2(timearg);
470 } else {
471 yearset = ATOI2(timearg);
472 if (yearset < 69)
473 lt->tm_year = yearset + 2000 - TM_YEAR_BASE;
474 else
475 lt->tm_year = yearset + 1900 - TM_YEAR_BASE;
477 /* FALLTHROUGH */
478 case 8:
479 lt->tm_mon = ATOI2(timearg);
480 --lt->tm_mon;
481 /* FALLTHROUGH */
482 case 6:
483 lt->tm_mday = ATOI2(timearg);
484 /* FALLTHROUGH */
485 case 4:
486 lt->tm_hour = ATOI2(timearg);
487 /* FALLTHROUGH */
488 case 2:
489 lt->tm_min = ATOI2(timearg);
490 break;
491 default:
492 badtime();
495 if ((shuttime = mktime(lt)) == -1)
496 badtime();
497 if ((offset = shuttime - now) < 0)
498 errx(1, "time is already past");
501 void
502 dorcshutdown(void)
504 (void)printf("\r\nAbout to run shutdown hooks...\r\n");
505 #ifndef DEBUG
506 (void)setuid(0);
507 (void)system(". " _PATH_RCSHUTDOWN);
508 #endif
509 (void)sleep(5); /* Give operator a chance to abort this. */
510 (void)printf("\r\nDone running shutdown hooks.\r\n");
513 #define FSMSG "fastboot file for fsck\n"
514 void
515 doitfast(void)
517 int fastfd;
519 if ((fastfd = open(_PATH_FASTBOOT, O_WRONLY|O_CREAT|O_TRUNC,
520 0664)) >= 0) {
521 (void)write(fastfd, FSMSG, sizeof(FSMSG) - 1);
522 (void)close(fastfd);
526 #define NOMSG "\n\nNO LOGINS: System going down at "
527 void
528 nolog(void)
530 int logfd;
531 char *ct;
533 (void)unlink(_PATH_NOLOGIN); /* in case linked to another file */
534 (void)signal(SIGINT, finish);
535 (void)signal(SIGHUP, finish);
536 (void)signal(SIGQUIT, finish);
537 (void)signal(SIGTERM, finish);
538 if ((logfd = open(_PATH_NOLOGIN, O_WRONLY|O_CREAT|O_TRUNC,
539 0664)) >= 0) {
540 (void)write(logfd, NOMSG, sizeof(NOMSG) - 1);
541 ct = ctime(&shuttime);
542 (void)write(logfd, ct + 11, 5);
543 (void)write(logfd, "\n\n", 2);
544 (void)write(logfd, mbuf, strlen(mbuf));
545 (void)close(logfd);
549 static void
550 /*ARGSUSED*/
551 finish(int signo)
554 if (!killflg)
555 (void)unlink(_PATH_NOLOGIN);
556 exit(0);
559 static void
560 badtime(void)
563 warnx("illegal time format");
564 usage();
567 static void
568 usage(void)
571 (void)fprintf(stderr,
572 "Usage: %s [-Ddfhknpr] time [message ... | -]\n",
573 getprogname());
574 exit(1);