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)
172 if (killflg + doreboot + dohalt + dopower > 1)
173 usage("incompatible switches -h, -k, -p and -r");
175 if (oflag && !(dohalt || dopower || doreboot))
176 usage("-o requires -h, -p or -r");
178 if (nosync != NULL && !oflag)
179 usage("-n requires -o");
185 for (p = mbuf, len = sizeof(mbuf); *argv; ++argv) {
186 arglen = strlen(*argv);
187 if ((len -= arglen) <= 2)
191 memmove(p, *argv, arglen);
200 endp = mbuf + sizeof(mbuf) - 2;
202 if (!fgets(p, endp - p + 1, stdin))
204 for (; *p && p < endp; ++p);
212 mbuflen = strlen(mbuf);
215 printf("Shutdown at %.24s.\n", ctime(&shuttime));
217 printf("Shutdown NOW!\n");
219 if (!(whom = getlogin()))
220 whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???";
225 setpriority(PRIO_PROCESS, 0, PRIO_MIN);
233 errx(0, "[pid %d]", forkpid);
237 openlog("shutdown", LOG_CONS, LOG_AUTH);
249 if (offset <= NOLOG_TIME) {
256 if (tp->timeleft < offset)
257 sleep((u_int)(offset - tp->timeleft));
259 while (tp->timeleft && offset < tp->timeleft)
262 * Warn now, if going to sleep more than a fifth of
263 * the next wait time.
265 if ((sltime = offset - tp->timeleft)) {
266 if (sltime > (u_int)(tp->timetowait / 5))
272 timewarn(tp->timeleft);
273 if (!logged && tp->timeleft <= NOLOG_TIME) {
277 sleep((u_int)tp->timetowait);
281 die_you_gravy_sucking_pig_dog();
284 static jmp_buf alarmbuf;
286 static const char *restricted_environ[] = {
287 "PATH=" _PATH_STDPATH,
292 timewarn(int timeleft)
295 static char hostname[MAXHOSTNAMELEN + 1];
297 char wcmd[MAXPATHLEN + 4];
300 gethostname(hostname, sizeof(hostname));
302 /* undoc -n option to wall suppresses normal wall banner */
303 snprintf(wcmd, sizeof(wcmd), "%s -n", _PATH_WALL);
304 environ = restricted_environ;
305 if (!(pf = popen(wcmd, "w"))) {
306 syslog(LOG_ERR, "shutdown: can't find %s: %m", _PATH_WALL);
311 "\007*** %sSystem shutdown message from %s@%s ***\007\n",
312 timeleft ? "": "FINAL ", whom, hostname);
314 if (timeleft > 10*60)
315 fprintf(pf, "System going down at %5.5s\n\n",
316 ctime(&shuttime) + 11);
317 else if (timeleft > 59)
318 fprintf(pf, "System going down in %d minute%s\n\n",
319 timeleft / 60, (timeleft > 60) ? "s" : "");
321 fprintf(pf, "System going down in %s30 seconds\n\n",
322 (offset > 0 && offset < 30 ? "less than " : ""));
324 fprintf(pf, "System going down IMMEDIATELY\n\n");
327 fwrite(mbuf, sizeof(*mbuf), mbuflen, pf);
330 * play some games, just in case wall doesn't come back
331 * probably unnecessary, given that wall is careful.
333 if (!setjmp(alarmbuf)) {
334 signal(SIGALRM, timeout);
338 signal(SIGALRM, SIG_DFL);
343 timeout(int signo __unused)
345 longjmp(alarmbuf, 1);
349 die_you_gravy_sucking_pig_dog(void)
352 char *empty_environ[] = { NULL };
355 syslog(LOG_NOTICE, "%s by %s: %s",
356 doreboot ? "reboot" : dohalt ? "halt" : dopower ? "power-down" :
357 "shutdown", whom, mbuf);
360 printf("\r\nSystem shutdown time has arrived\007\007\r\n");
362 printf("\rbut you'll have to do it yourself\r\n");
371 printf("power-down");
374 printf("\nkill -HUP 1\n");
377 kill(1, doreboot ? SIGINT : /* reboot */
378 dohalt ? SIGUSR1 : /* halt */
379 dopower ? SIGUSR2 : /* power-down */
380 SIGTERM); /* single-user */
383 execle(_PATH_REBOOT, "reboot", "-l", nosync,
384 NULL, empty_environ);
385 syslog(LOG_ERR, "shutdown: can't exec %s: %m.",
390 execle(_PATH_HALT, "halt", "-l", nosync,
391 NULL, empty_environ);
392 syslog(LOG_ERR, "shutdown: can't exec %s: %m.",
397 execle(_PATH_HALT, "halt", "-l", "-p", nosync,
398 NULL, empty_environ);
399 syslog(LOG_ERR, "shutdown: can't exec %s: %m.",
403 kill(1, SIGTERM); /* to single-user */
409 #define ATOI2(p) (p[0] - '0') * 10 + (p[1] - '0'); p += 2;
412 getoffset(char *timearg)
417 int maybe_today, this_year;
422 if (!strcasecmp(timearg, "now")) { /* now */
428 if (*timearg == '+') { /* +minutes */
429 if (!isdigit(*++timearg))
432 offset = strtol(timearg, &timeunit, 10);
433 if (offset < 0 || offset == LONG_MAX || errno != 0)
435 if (timeunit[0] == '\0' || strcasecmp(timeunit, "m") == 0 ||
436 strcasecmp(timeunit, "min") == 0 ||
437 strcasecmp(timeunit, "mins") == 0) {
439 } else if (strcasecmp(timeunit, "h") == 0 ||
440 strcasecmp(timeunit, "hour") == 0 ||
441 strcasecmp(timeunit, "hours") == 0) {
443 } else if (strcasecmp(timeunit, "s") == 0 ||
444 strcasecmp(timeunit, "sec") == 0 ||
445 strcasecmp(timeunit, "secs") == 0) {
450 /* if ((offset = atoi(timearg) * 60) < 0) */
452 shuttime = now + offset;
456 /* handle hh:mm by getting rid of the colon */
457 for (p = timearg; *p; ++p)
458 if (!isascii(*p) || !isdigit(*p)) {
459 if (*p == ':' && strlen(p) == 3) {
468 unsetenv("TZ"); /* OUR timezone */
469 lt = localtime(&now); /* current time val */
472 switch(strlen(timearg)) {
474 this_year = lt->tm_year;
475 lt->tm_year = ATOI2(timearg);
477 * check if the specified year is in the next century.
478 * allow for one year of user error as many people will
479 * enter n - 1 at the start of year n.
481 if (lt->tm_year < (this_year % 100) - 1)
483 /* adjust for the year 2000 and beyond */
484 lt->tm_year += (this_year - (this_year % 100));
487 lt->tm_mon = ATOI2(timearg);
488 if (--lt->tm_mon < 0 || lt->tm_mon > 11)
493 lt->tm_mday = ATOI2(timearg);
494 if (lt->tm_mday < 1 || lt->tm_mday > 31)
498 lt->tm_hour = ATOI2(timearg);
499 if (lt->tm_hour < 0 || lt->tm_hour > 23)
501 lt->tm_min = ATOI2(timearg);
502 if (lt->tm_min < 0 || lt->tm_min > 59)
505 if ((shuttime = mktime(lt)) == -1)
508 if ((offset = shuttime - now) < 0) {
510 errx(1, "that time is already past.");
513 * If the user only gave a time, assume that
514 * any time earlier than the current time
515 * was intended to be that time tomorrow.
518 if ((shuttime = mktime(lt)) == -1)
520 if ((offset = shuttime - now) < 0) {
521 errx(1, "tomorrow is before today?");
530 #define NOMSG "\n\nNO LOGINS: System going down at "
537 unlink(_PATH_NOLOGIN); /* in case linked to another file */
538 signal(SIGINT, finish);
539 signal(SIGHUP, finish);
540 signal(SIGQUIT, finish);
541 signal(SIGTERM, finish);
542 if ((logfd = open(_PATH_NOLOGIN, O_WRONLY|O_CREAT|O_TRUNC,
544 write(logfd, NOMSG, sizeof(NOMSG) - 1);
545 ct = ctime(&shuttime);
546 write(logfd, ct + 11, 5);
547 write(logfd, "\n\n", 2);
548 write(logfd, mbuf, strlen(mbuf));
554 finish(int signo __unused)
557 unlink(_PATH_NOLOGIN);
564 errx(1, "bad time format");
568 usage(const char *cp)
573 "usage: shutdown [-] [-h | -p | -r | -k] [-o [-n]]"
574 " time [warning-message ...]\n"