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>
48 #include <sys/resource.h>
49 #include <sys/syslog.h>
66 #define _PATH_NOLOGIN "./nologin"
72 #define NOLOG_TIME 5*60
73 static struct interval {
74 int timeleft, timetowait;
93 static time_t offset, shuttime;
94 static int docycle, dohalt, dopower, doreboot, killflg, mbuflen, oflag;
95 static char mbuf[BUFSIZ];
96 static const char *nosync, *whom;
98 static void badtime(void);
99 static void die_you_gravy_sucking_pig_dog(void);
100 static void finish(int);
101 static void getoffset(char *);
102 static void loop(void);
103 static void nolog(void);
104 static void timeout(int);
105 static void timewarn(int);
106 static void usage(const char *);
108 extern const char **environ;
111 main(int argc, char **argv)
115 int arglen, ch, len, readstdin;
119 errx(1, "NOT super-user");
126 * Test for the special case where the utility is called as
127 * "poweroff", for which it runs 'shutdown -p now'.
129 if ((p = strrchr(argv[0], '/')) == NULL)
133 if (strcmp(p, "poweroff") == 0) {
134 if (getopt(argc, argv, "") != -1)
142 (void)time(&shuttime);
146 while ((ch = getopt(argc, argv, "-chknopr")) != -1)
182 if (killflg + doreboot + dohalt + dopower + docycle > 1)
183 usage("incompatible switches -c, -h, -k, -p and -r");
185 if (oflag && !(dohalt || dopower || doreboot || docycle))
186 usage("-o requires -c, -h, -p or -r");
188 if (nosync != NULL && !oflag)
189 usage("-n requires -o");
195 for (p = mbuf, len = sizeof(mbuf); *argv; ++argv) {
196 arglen = strlen(*argv);
197 if ((len -= arglen) <= 2)
201 memmove(p, *argv, arglen);
210 endp = mbuf + sizeof(mbuf) - 2;
212 if (!fgets(p, endp - p + 1, stdin))
214 for (; *p && p < endp; ++p);
222 mbuflen = strlen(mbuf);
225 (void)printf("Shutdown at %.24s.\n", ctime(&shuttime));
227 (void)printf("Shutdown NOW!\n");
229 if (!(whom = getlogin()))
230 whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???";
233 (void)putc('\n', stdout);
235 (void)setpriority(PRIO_PROCESS, 0, PRIO_MIN);
243 errx(0, "[pid %d]", forkpid);
247 openlog("shutdown", LOG_CONS, LOG_AUTH);
259 if (offset <= NOLOG_TIME) {
266 if (tp->timeleft < offset)
267 (void)sleep((u_int)(offset - tp->timeleft));
269 while (tp->timeleft && offset < tp->timeleft)
272 * Warn now, if going to sleep more than a fifth of
273 * the next wait time.
275 if ((sltime = offset - tp->timeleft)) {
276 if (sltime > (u_int)(tp->timetowait / 5))
282 timewarn(tp->timeleft);
283 if (!logged && tp->timeleft <= NOLOG_TIME) {
287 (void)sleep((u_int)tp->timetowait);
291 die_you_gravy_sucking_pig_dog();
294 static jmp_buf alarmbuf;
296 static const char *restricted_environ[] = {
297 "PATH=" _PATH_STDPATH,
302 timewarn(int timeleft)
305 static char hostname[MAXHOSTNAMELEN + 1];
307 char wcmd[MAXPATHLEN + 4];
310 (void)gethostname(hostname, sizeof(hostname));
312 /* undoc -n option to wall suppresses normal wall banner */
313 (void)snprintf(wcmd, sizeof(wcmd), "%s -n", _PATH_WALL);
314 environ = restricted_environ;
315 if (!(pf = popen(wcmd, "w"))) {
316 syslog(LOG_ERR, "shutdown: can't find %s: %m", _PATH_WALL);
321 "\007*** %sSystem shutdown message from %s@%s ***\007\n",
322 timeleft ? "": "FINAL ", whom, hostname);
324 if (timeleft > 10*60)
325 (void)fprintf(pf, "System going down at %5.5s\n\n",
326 ctime(&shuttime) + 11);
327 else if (timeleft > 59)
328 (void)fprintf(pf, "System going down in %d minute%s\n\n",
329 timeleft / 60, (timeleft > 60) ? "s" : "");
331 (void)fprintf(pf, "System going down in %s30 seconds\n\n",
332 (offset > 0 && offset < 30 ? "less than " : ""));
334 (void)fprintf(pf, "System going down IMMEDIATELY\n\n");
337 (void)fwrite(mbuf, sizeof(*mbuf), mbuflen, pf);
340 * play some games, just in case wall doesn't come back
341 * probably unnecessary, given that wall is careful.
343 if (!setjmp(alarmbuf)) {
344 (void)signal(SIGALRM, timeout);
345 (void)alarm((u_int)30);
347 (void)alarm((u_int)0);
348 (void)signal(SIGALRM, SIG_DFL);
353 timeout(int signo __unused)
355 longjmp(alarmbuf, 1);
359 die_you_gravy_sucking_pig_dog(void)
361 char *empty_environ[] = { NULL };
363 syslog(LOG_NOTICE, "%s by %s: %s",
364 doreboot ? "reboot" : dohalt ? "halt" : dopower ? "power-down" :
365 docycle ? "power-cycle" : "shutdown", whom, mbuf);
367 (void)printf("\r\nSystem shutdown time has arrived\007\007\r\n");
369 (void)printf("\rbut you'll have to do it yourself\r\n");
374 (void)printf("reboot");
376 (void)printf("power-cycle");
378 (void)printf("halt");
380 (void)printf("power-down");
382 (void)printf(" no sync");
383 (void)printf("\nkill -HUP 1\n");
386 (void)kill(1, doreboot ? SIGINT : /* reboot */
387 dohalt ? SIGUSR1 : /* halt */
388 dopower ? SIGUSR2 : /* power-down */
389 docycle ? SIGWINCH : /* power-cycle */
390 SIGTERM); /* single-user */
393 execle(_PATH_REBOOT, "reboot", "-l", nosync,
394 (char *)NULL, empty_environ);
395 syslog(LOG_ERR, "shutdown: can't exec %s: %m.",
400 execle(_PATH_HALT, "halt", "-l", nosync,
401 (char *)NULL, empty_environ);
402 syslog(LOG_ERR, "shutdown: can't exec %s: %m.",
407 execle(_PATH_HALT, "halt", "-l", "-p", nosync,
408 (char *)NULL, empty_environ);
409 syslog(LOG_ERR, "shutdown: can't exec %s: %m.",
414 execle(_PATH_HALT, "halt", "-l", "-c", nosync,
415 (char *)NULL, empty_environ);
416 syslog(LOG_ERR, "shutdown: can't exec %s: %m.",
420 (void)kill(1, SIGTERM); /* to single-user */
426 #define ATOI2(p) (p[0] - '0') * 10 + (p[1] - '0'); p += 2;
429 getoffset(char *timearg)
434 int maybe_today, this_year;
439 if (!strcasecmp(timearg, "now")) { /* now */
445 if (*timearg == '+') { /* +minutes */
446 if (!isdigit(*++timearg))
449 offset = strtol(timearg, &timeunit, 10);
450 if (offset < 0 || offset == LONG_MAX || errno != 0)
452 if (timeunit[0] == '\0' || strcasecmp(timeunit, "m") == 0 ||
453 strcasecmp(timeunit, "min") == 0 ||
454 strcasecmp(timeunit, "mins") == 0) {
456 } else if (strcasecmp(timeunit, "h") == 0 ||
457 strcasecmp(timeunit, "hour") == 0 ||
458 strcasecmp(timeunit, "hours") == 0) {
460 } else if (strcasecmp(timeunit, "s") == 0 ||
461 strcasecmp(timeunit, "sec") == 0 ||
462 strcasecmp(timeunit, "secs") == 0) {
467 shuttime = now + offset;
471 /* handle hh:mm by getting rid of the colon */
472 for (p = timearg; *p; ++p)
473 if (!isascii(*p) || !isdigit(*p)) {
474 if (*p == ':' && strlen(p) == 3) {
483 unsetenv("TZ"); /* OUR timezone */
484 lt = localtime(&now); /* current time val */
487 switch(strlen(timearg)) {
489 this_year = lt->tm_year;
490 lt->tm_year = ATOI2(timearg);
492 * check if the specified year is in the next century.
493 * allow for one year of user error as many people will
494 * enter n - 1 at the start of year n.
496 if (lt->tm_year < (this_year % 100) - 1)
498 /* adjust for the year 2000 and beyond */
499 lt->tm_year += (this_year - (this_year % 100));
502 lt->tm_mon = ATOI2(timearg);
503 if (--lt->tm_mon < 0 || lt->tm_mon > 11)
508 lt->tm_mday = ATOI2(timearg);
509 if (lt->tm_mday < 1 || lt->tm_mday > 31)
513 lt->tm_hour = ATOI2(timearg);
514 if (lt->tm_hour < 0 || lt->tm_hour > 23)
516 lt->tm_min = ATOI2(timearg);
517 if (lt->tm_min < 0 || lt->tm_min > 59)
520 if ((shuttime = mktime(lt)) == -1)
523 if ((offset = shuttime - now) < 0) {
525 errx(1, "that time is already past.");
528 * If the user only gave a time, assume that
529 * any time earlier than the current time
530 * was intended to be that time tomorrow.
533 if ((shuttime = mktime(lt)) == -1)
535 if ((offset = shuttime - now) < 0) {
536 errx(1, "tomorrow is before today?");
545 #define NOMSG "\n\nNO LOGINS: System going down at "
552 (void)unlink(_PATH_NOLOGIN); /* in case linked to another file */
553 (void)signal(SIGINT, finish);
554 (void)signal(SIGHUP, finish);
555 (void)signal(SIGQUIT, finish);
556 (void)signal(SIGTERM, finish);
557 if ((logfd = open(_PATH_NOLOGIN, O_WRONLY|O_CREAT|O_TRUNC,
559 (void)write(logfd, NOMSG, sizeof(NOMSG) - 1);
560 ct = ctime(&shuttime);
561 (void)write(logfd, ct + 11, 5);
562 (void)write(logfd, "\n\n", 2);
563 (void)write(logfd, mbuf, strlen(mbuf));
569 finish(int signo __unused)
572 (void)unlink(_PATH_NOLOGIN);
579 errx(1, "bad time format");
583 usage(const char *cp)
587 (void)fprintf(stderr,
588 "usage: shutdown [-] [-c | -h | -p | -r | -k] [-o [-n]] time [warning-message ...]\n"