2 * SPDX-License-Identifier: BSD-3-Clause
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
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
34 static const char copyright[] =
35 "@(#) Copyright (c) 1988, 1990, 1993\n\
36 The Regents of the University of California. All rights reserved.\n";
40 static char sccsid[] = "@(#)shutdown.c 8.4 (Berkeley) 4/28/95";
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
46 #include <sys/param.h>
47 #include <sys/boottrace.h>
48 #include <sys/resource.h>
49 #include <sys/syslog.h>
67 #define _PATH_NOLOGIN "./nologin"
73 #define NOLOG_TIME 5*60
74 static struct interval {
75 int timeleft, timetowait;
94 static time_t offset, shuttime;
95 static int docycle, dohalt, dopower, doreboot, killflg, mbuflen, oflag;
96 static char mbuf[BUFSIZ];
97 static const char *nosync, *whom;
99 static void badtime(void);
100 static void die_you_gravy_sucking_pig_dog(void);
101 static void finish(int);
102 static void getoffset(char *);
103 static void loop(void);
104 static void nolog(void);
105 static void timeout(int);
106 static void timewarn(int);
107 static void usage(const char *);
109 extern const char **environ;
112 main(int argc, char **argv)
116 int arglen, ch, len, readstdin;
120 errx(1, "NOT super-user");
127 * Test for the special case where the utility is called as
128 * "poweroff", for which it runs 'shutdown -p now'.
130 if ((p = strrchr(argv[0], '/')) == NULL)
134 if (strcmp(p, "poweroff") == 0) {
135 if (getopt(argc, argv, "") != -1)
143 (void)time(&shuttime);
147 while ((ch = getopt(argc, argv, "-chknopr")) != -1)
183 if (killflg + doreboot + dohalt + dopower + docycle > 1)
184 usage("incompatible switches -c, -h, -k, -p and -r");
186 if (oflag && !(dohalt || dopower || doreboot || docycle))
187 usage("-o requires -c, -h, -p or -r");
189 if (nosync != NULL && !oflag)
190 usage("-n requires -o");
196 for (p = mbuf, len = sizeof(mbuf); *argv; ++argv) {
197 arglen = strlen(*argv);
198 if ((len -= arglen) <= 2)
202 memmove(p, *argv, arglen);
211 endp = mbuf + sizeof(mbuf) - 2;
213 if (!fgets(p, endp - p + 1, stdin))
215 for (; *p && p < endp; ++p);
223 mbuflen = strlen(mbuf);
226 BOOTTRACE("Shutdown at %s", ctime(&shuttime));
227 (void)printf("Shutdown at %.24s.\n", ctime(&shuttime));
229 BOOTTRACE("Shutdown NOW!");
230 (void)printf("Shutdown NOW!\n");
233 if (!(whom = getlogin()))
234 whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???";
237 (void)putc('\n', stdout);
239 (void)setpriority(PRIO_PROCESS, 0, PRIO_MIN);
247 errx(0, "[pid %d]", forkpid);
251 openlog("shutdown", LOG_CONS, LOG_AUTH);
263 if (offset <= NOLOG_TIME) {
270 if (tp->timeleft < offset)
271 (void)sleep((u_int)(offset - tp->timeleft));
273 while (tp->timeleft && offset < tp->timeleft)
276 * Warn now, if going to sleep more than a fifth of
277 * the next wait time.
279 if ((sltime = offset - tp->timeleft)) {
280 if (sltime > (u_int)(tp->timetowait / 5))
286 timewarn(tp->timeleft);
287 if (!logged && tp->timeleft <= NOLOG_TIME) {
291 (void)sleep((u_int)tp->timetowait);
295 die_you_gravy_sucking_pig_dog();
298 static jmp_buf alarmbuf;
300 static const char *restricted_environ[] = {
301 "PATH=" _PATH_STDPATH,
306 timewarn(int timeleft)
309 static char hostname[MAXHOSTNAMELEN + 1];
311 char wcmd[MAXPATHLEN + 4];
314 (void)gethostname(hostname, sizeof(hostname));
316 /* undoc -n option to wall suppresses normal wall banner */
317 (void)snprintf(wcmd, sizeof(wcmd), "%s -n", _PATH_WALL);
318 environ = restricted_environ;
319 if (!(pf = popen(wcmd, "w"))) {
320 syslog(LOG_ERR, "shutdown: can't find %s: %m", _PATH_WALL);
325 "\007*** %sSystem shutdown message from %s@%s ***\007\n",
326 timeleft ? "": "FINAL ", whom, hostname);
328 if (timeleft > 10*60)
329 (void)fprintf(pf, "System going down at %5.5s\n\n",
330 ctime(&shuttime) + 11);
331 else if (timeleft > 59)
332 (void)fprintf(pf, "System going down in %d minute%s\n\n",
333 timeleft / 60, (timeleft > 60) ? "s" : "");
335 (void)fprintf(pf, "System going down in %s30 seconds\n\n",
336 (offset > 0 && offset < 30 ? "less than " : ""));
338 (void)fprintf(pf, "System going down IMMEDIATELY\n\n");
341 (void)fwrite(mbuf, sizeof(*mbuf), mbuflen, pf);
344 * play some games, just in case wall doesn't come back
345 * probably unnecessary, given that wall is careful.
347 if (!setjmp(alarmbuf)) {
348 (void)signal(SIGALRM, timeout);
349 (void)alarm((u_int)30);
351 (void)alarm((u_int)0);
352 (void)signal(SIGALRM, SIG_DFL);
357 timeout(int signo __unused)
359 longjmp(alarmbuf, 1);
363 die_you_gravy_sucking_pig_dog(void)
365 char *empty_environ[] = { NULL };
367 BOOTTRACE("%s by %s",
368 doreboot ? "reboot" : dohalt ? "halt" : dopower ? "power-down" :
369 docycle ? "power-cycle" : "shutdown", whom);
370 syslog(LOG_NOTICE, "%s by %s: %s",
371 doreboot ? "reboot" : dohalt ? "halt" : dopower ? "power-down" :
372 docycle ? "power-cycle" : "shutdown", whom, mbuf);
374 (void)printf("\r\nSystem shutdown time has arrived\007\007\r\n");
376 BOOTTRACE("fake shutdown...");
377 (void)printf("\rbut you'll have to do it yourself\r\n");
382 (void)printf("reboot");
384 (void)printf("power-cycle");
386 (void)printf("halt");
388 (void)printf("power-down");
390 (void)printf(" no sync");
391 (void)printf("\nkill -HUP 1\n");
394 BOOTTRACE("signal to init(8)...");
395 (void)kill(1, doreboot ? SIGINT : /* reboot */
396 dohalt ? SIGUSR1 : /* halt */
397 dopower ? SIGUSR2 : /* power-down */
398 docycle ? SIGWINCH : /* power-cycle */
399 SIGTERM); /* single-user */
402 BOOTTRACE("exec reboot(8) -l...");
403 execle(_PATH_REBOOT, "reboot", "-l", nosync,
404 (char *)NULL, empty_environ);
405 syslog(LOG_ERR, "shutdown: can't exec %s: %m.",
410 BOOTTRACE("exec halt(8) -l...");
411 execle(_PATH_HALT, "halt", "-l", nosync,
412 (char *)NULL, empty_environ);
413 syslog(LOG_ERR, "shutdown: can't exec %s: %m.",
418 BOOTTRACE("exec halt(8) -l -p...");
419 execle(_PATH_HALT, "halt", "-l", "-p", nosync,
420 (char *)NULL, empty_environ);
421 syslog(LOG_ERR, "shutdown: can't exec %s: %m.",
426 execle(_PATH_HALT, "halt", "-l", "-c", nosync,
427 (char *)NULL, empty_environ);
428 syslog(LOG_ERR, "shutdown: can't exec %s: %m.",
432 BOOTTRACE("SIGTERM to init(8)...");
433 (void)kill(1, SIGTERM); /* to single-user */
439 #define ATOI2(p) (p[0] - '0') * 10 + (p[1] - '0'); p += 2;
442 getoffset(char *timearg)
447 int maybe_today, this_year;
452 if (!strcasecmp(timearg, "now")) { /* now */
458 if (*timearg == '+') { /* +minutes */
459 if (!isdigit(*++timearg))
462 offset = strtol(timearg, &timeunit, 10);
463 if (offset < 0 || offset == LONG_MAX || errno != 0)
465 if (timeunit[0] == '\0' || strcasecmp(timeunit, "m") == 0 ||
466 strcasecmp(timeunit, "min") == 0 ||
467 strcasecmp(timeunit, "mins") == 0) {
469 } else if (strcasecmp(timeunit, "h") == 0 ||
470 strcasecmp(timeunit, "hour") == 0 ||
471 strcasecmp(timeunit, "hours") == 0) {
473 } else if (strcasecmp(timeunit, "s") == 0 ||
474 strcasecmp(timeunit, "sec") == 0 ||
475 strcasecmp(timeunit, "secs") == 0) {
480 shuttime = now + offset;
484 /* handle hh:mm by getting rid of the colon */
485 for (p = timearg; *p; ++p)
486 if (!isascii(*p) || !isdigit(*p)) {
487 if (*p == ':' && strlen(p) == 3) {
496 unsetenv("TZ"); /* OUR timezone */
497 lt = localtime(&now); /* current time val */
500 switch(strlen(timearg)) {
502 this_year = lt->tm_year;
503 lt->tm_year = ATOI2(timearg);
505 * check if the specified year is in the next century.
506 * allow for one year of user error as many people will
507 * enter n - 1 at the start of year n.
509 if (lt->tm_year < (this_year % 100) - 1)
511 /* adjust for the year 2000 and beyond */
512 lt->tm_year += (this_year - (this_year % 100));
515 lt->tm_mon = ATOI2(timearg);
516 if (--lt->tm_mon < 0 || lt->tm_mon > 11)
521 lt->tm_mday = ATOI2(timearg);
522 if (lt->tm_mday < 1 || lt->tm_mday > 31)
526 lt->tm_hour = ATOI2(timearg);
527 if (lt->tm_hour < 0 || lt->tm_hour > 23)
529 lt->tm_min = ATOI2(timearg);
530 if (lt->tm_min < 0 || lt->tm_min > 59)
533 if ((shuttime = mktime(lt)) == -1)
536 if ((offset = shuttime - now) < 0) {
538 errx(1, "that time is already past.");
541 * If the user only gave a time, assume that
542 * any time earlier than the current time
543 * was intended to be that time tomorrow.
546 if ((shuttime = mktime(lt)) == -1)
548 if ((offset = shuttime - now) < 0) {
549 errx(1, "tomorrow is before today?");
558 #define NOMSG "\n\nNO LOGINS: System going down at "
565 (void)unlink(_PATH_NOLOGIN); /* in case linked to another file */
566 (void)signal(SIGINT, finish);
567 (void)signal(SIGHUP, finish);
568 (void)signal(SIGQUIT, finish);
569 (void)signal(SIGTERM, finish);
570 if ((logfd = open(_PATH_NOLOGIN, O_WRONLY|O_CREAT|O_TRUNC,
572 (void)write(logfd, NOMSG, sizeof(NOMSG) - 1);
573 ct = ctime(&shuttime);
574 (void)write(logfd, ct + 11, 5);
575 (void)write(logfd, "\n\n", 2);
576 (void)write(logfd, mbuf, strlen(mbuf));
582 finish(int signo __unused)
585 (void)unlink(_PATH_NOLOGIN);
592 errx(1, "bad time format");
596 usage(const char *cp)
600 (void)fprintf(stderr,
601 "usage: shutdown [-] [-c | -h | -p | -r | -k] [-o [-n]] time [warning-message ...]\n"