2 * Copyright (c) 1988, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#) Copyright (c) 1988, 1990, 1993 The Regents of the University of California. All rights reserved.
30 * @(#)shutdown.c 8.4 (Berkeley) 4/28/95
31 * $FreeBSD: src/sbin/shutdown/shutdown.c,v 1.21.2.1 2001/07/30 10:38:08 dd Exp $
34 #include <sys/param.h>
36 #include <sys/resource.h>
37 #include <sys/syslog.h>
51 #include "pathnames.h"
55 #define _PATH_NOLOGIN "./nologin"
61 #define NOLOG_TIME 5*60
62 static struct interval {
63 int timeleft, timetowait;
82 static time_t offset, shuttime;
83 static int dohalt, dopower, doreboot, killflg, mbuflen, oflag;
84 static char mbuf[BUFSIZ];
85 static const char *nosync, *whom;
87 static void badtime(void);
88 static void die_you_gravy_sucking_pig_dog(void);
89 static void finish(int);
90 static void getoffset(char *);
91 static void loop(void);
92 static void nolog(void);
93 static void timeout(int);
94 static void timewarn(int);
95 static void usage(const char *);
97 extern const char **environ;
100 main(int argc, char **argv)
104 int arglen, ch, len, readstdin;
108 errx(1, "NOT super-user");
115 * Test for the special case where the utility is called as
116 * "poweroff", for which it runs 'shutdown -p now'.
118 if ((p = strrchr(argv[0], '/')) == NULL)
122 if (strcmp(p, "poweroff") == 0) {
123 if (getopt(argc, argv, "") != -1)
131 (void)time(&shuttime);
135 while ((ch = getopt(argc, argv, "-hknopr")) != -1)
168 if (killflg + doreboot + dohalt + dopower > 1)
169 usage("incompatible switches -h, -k, -p and -r");
171 if (oflag && !(dohalt || dopower || doreboot))
172 usage("-o requires -h, -p or -r");
174 if (nosync != NULL && !oflag)
175 usage("-n requires -o");
181 for (p = mbuf, len = sizeof(mbuf); *argv; ++argv) {
182 arglen = strlen(*argv);
183 if ((len -= arglen) <= 2)
187 memmove(p, *argv, arglen);
196 endp = mbuf + sizeof(mbuf) - 2;
198 if (!fgets(p, endp - p + 1, stdin))
200 for (; *p && p < endp; ++p);
208 mbuflen = strlen(mbuf);
211 printf("Shutdown at %.24s.\n", ctime(&shuttime));
213 printf("Shutdown NOW!\n");
215 if (!(whom = getlogin()))
216 whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???";
221 setpriority(PRIO_PROCESS, 0, PRIO_MIN);
229 errx(0, "[pid %d]", forkpid);
233 openlog("shutdown", LOG_CONS, LOG_AUTH);
245 if (offset <= NOLOG_TIME) {
252 if (tp->timeleft < offset)
253 sleep((u_int)(offset - tp->timeleft));
255 while (tp->timeleft && offset < tp->timeleft)
258 * Warn now, if going to sleep more than a fifth of
259 * the next wait time.
261 if ((sltime = offset - tp->timeleft)) {
262 if (sltime > (u_int)(tp->timetowait / 5))
268 timewarn(tp->timeleft);
269 if (!logged && tp->timeleft <= NOLOG_TIME) {
273 sleep((u_int)tp->timetowait);
277 die_you_gravy_sucking_pig_dog();
280 static jmp_buf alarmbuf;
282 static const char *restricted_environ[] = {
283 "PATH=" _PATH_STDPATH,
288 timewarn(int timeleft)
291 static char hostname[MAXHOSTNAMELEN + 1];
293 char wcmd[MAXPATHLEN + 4];
296 gethostname(hostname, sizeof(hostname));
298 /* undoc -n option to wall suppresses normal wall banner */
299 snprintf(wcmd, sizeof(wcmd), "%s -n", _PATH_WALL);
300 environ = restricted_environ;
301 if (!(pf = popen(wcmd, "w"))) {
302 syslog(LOG_ERR, "shutdown: can't find %s: %m", _PATH_WALL);
307 "\007*** %sSystem shutdown message from %s@%s ***\007\n",
308 timeleft ? "": "FINAL ", whom, hostname);
310 if (timeleft > 10*60)
311 fprintf(pf, "System going down at %5.5s\n\n",
312 ctime(&shuttime) + 11);
313 else if (timeleft > 59)
314 fprintf(pf, "System going down in %d minute%s\n\n",
315 timeleft / 60, (timeleft > 60) ? "s" : "");
317 fprintf(pf, "System going down in %s30 seconds\n\n",
318 (offset > 0 && offset < 30 ? "less than " : ""));
320 fprintf(pf, "System going down IMMEDIATELY\n\n");
323 fwrite(mbuf, sizeof(*mbuf), mbuflen, pf);
326 * play some games, just in case wall doesn't come back
327 * probably unnecessary, given that wall is careful.
329 if (!setjmp(alarmbuf)) {
330 signal(SIGALRM, timeout);
334 signal(SIGALRM, SIG_DFL);
339 timeout(int signo __unused)
341 longjmp(alarmbuf, 1);
345 die_you_gravy_sucking_pig_dog(void)
348 char *empty_environ[] = { NULL };
351 syslog(LOG_NOTICE, "%s by %s: %s",
352 doreboot ? "reboot" : dohalt ? "halt" : dopower ? "power-down" :
353 "shutdown", whom, mbuf);
356 printf("\r\nSystem shutdown time has arrived\007\007\r\n");
358 printf("\rbut you'll have to do it yourself\r\n");
367 printf("power-down");
370 printf("\nkill -HUP 1\n");
373 kill(1, doreboot ? SIGINT : /* reboot */
374 dohalt ? SIGUSR1 : /* halt */
375 dopower ? SIGUSR2 : /* power-down */
376 SIGTERM); /* single-user */
379 execle(_PATH_REBOOT, "reboot", "-l", nosync,
380 NULL, empty_environ);
381 syslog(LOG_ERR, "shutdown: can't exec %s: %m.",
386 execle(_PATH_HALT, "halt", "-l", nosync,
387 NULL, empty_environ);
388 syslog(LOG_ERR, "shutdown: can't exec %s: %m.",
393 execle(_PATH_HALT, "halt", "-l", "-p", nosync,
394 NULL, empty_environ);
395 syslog(LOG_ERR, "shutdown: can't exec %s: %m.",
399 kill(1, SIGTERM); /* to single-user */
405 #define ATOI2(p) (p[0] - '0') * 10 + (p[1] - '0'); p += 2;
408 getoffset(char *timearg)
413 int maybe_today, this_year;
418 if (!strcasecmp(timearg, "now")) { /* now */
424 if (*timearg == '+') { /* +minutes */
425 if (!isdigit(*++timearg))
428 offset = strtol(timearg, &timeunit, 10);
429 if (offset < 0 || offset == LONG_MAX || errno != 0)
431 if (timeunit[0] == '\0' || strcasecmp(timeunit, "m") == 0 ||
432 strcasecmp(timeunit, "min") == 0 ||
433 strcasecmp(timeunit, "mins") == 0) {
435 } else if (strcasecmp(timeunit, "h") == 0 ||
436 strcasecmp(timeunit, "hour") == 0 ||
437 strcasecmp(timeunit, "hours") == 0) {
439 } else if (strcasecmp(timeunit, "s") == 0 ||
440 strcasecmp(timeunit, "sec") == 0 ||
441 strcasecmp(timeunit, "secs") == 0) {
446 /* if ((offset = atoi(timearg) * 60) < 0) */
448 shuttime = now + offset;
452 /* handle hh:mm by getting rid of the colon */
453 for (p = timearg; *p; ++p)
454 if (!isascii(*p) || !isdigit(*p)) {
455 if (*p == ':' && strlen(p) == 3) {
464 unsetenv("TZ"); /* OUR timezone */
465 lt = localtime(&now); /* current time val */
468 switch(strlen(timearg)) {
470 this_year = lt->tm_year;
471 lt->tm_year = ATOI2(timearg);
473 * check if the specified year is in the next century.
474 * allow for one year of user error as many people will
475 * enter n - 1 at the start of year n.
477 if (lt->tm_year < (this_year % 100) - 1)
479 /* adjust for the year 2000 and beyond */
480 lt->tm_year += (this_year - (this_year % 100));
483 lt->tm_mon = ATOI2(timearg);
484 if (--lt->tm_mon < 0 || lt->tm_mon > 11)
489 lt->tm_mday = ATOI2(timearg);
490 if (lt->tm_mday < 1 || lt->tm_mday > 31)
494 lt->tm_hour = ATOI2(timearg);
495 if (lt->tm_hour < 0 || lt->tm_hour > 23)
497 lt->tm_min = ATOI2(timearg);
498 if (lt->tm_min < 0 || lt->tm_min > 59)
501 if ((shuttime = mktime(lt)) == -1)
504 if ((offset = shuttime - now) < 0) {
506 errx(1, "that time is already past.");
509 * If the user only gave a time, assume that
510 * any time earlier than the current time
511 * was intended to be that time tomorrow.
514 if ((shuttime = mktime(lt)) == -1)
516 if ((offset = shuttime - now) < 0) {
517 errx(1, "tomorrow is before today?");
526 #define NOMSG "\n\nNO LOGINS: System going down at "
533 unlink(_PATH_NOLOGIN); /* in case linked to another file */
534 signal(SIGINT, finish);
535 signal(SIGHUP, finish);
536 signal(SIGQUIT, finish);
537 signal(SIGTERM, finish);
538 if ((logfd = open(_PATH_NOLOGIN, O_WRONLY|O_CREAT|O_TRUNC,
540 write(logfd, NOMSG, sizeof(NOMSG) - 1);
541 ct = ctime(&shuttime);
542 write(logfd, ct + 11, 5);
543 write(logfd, "\n\n", 2);
544 write(logfd, mbuf, strlen(mbuf));
550 finish(int signo __unused)
553 unlink(_PATH_NOLOGIN);
560 errx(1, "bad time format");
564 usage(const char *cp)
569 "usage: shutdown [-] [-h | -p | -r | -k] [-o [-n]]"
570 " time [warning-message ...]\n"