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 * 4. 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
32 static const char copyright[] =
33 "@(#) Copyright (c) 1988, 1990, 1993\n\
34 The Regents of the University of California. All rights reserved.\n";
38 static char sccsid[] = "@(#)shutdown.c 8.4 (Berkeley) 4/28/95";
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
44 #include <sys/param.h>
46 #include <sys/resource.h>
47 #include <sys/syslog.h>
64 #define _PATH_NOLOGIN "./nologin"
70 #define NOLOG_TIME 5*60
71 static struct interval {
72 int timeleft, timetowait;
91 static time_t offset, shuttime;
92 static int dohalt, dopower, doreboot, killflg, mbuflen, oflag;
93 static char mbuf[BUFSIZ];
94 static const char *nosync, *whom;
96 static void badtime(void);
97 static void die_you_gravy_sucking_pig_dog(void);
98 static void finish(int);
99 static void getoffset(char *);
100 static void loop(void);
101 static void nolog(void);
102 static void timeout(int);
103 static void timewarn(int);
104 static void usage(const char *);
106 extern const char **environ;
109 main(int argc, char **argv)
113 int arglen, ch, len, readstdin;
117 errx(1, "NOT super-user");
124 * Test for the special case where the utility is called as
125 * "poweroff", for which it runs 'shutdown -p now'.
127 if ((p = strrchr(argv[0], '/')) == NULL)
131 if (strcmp(p, "poweroff") == 0) {
132 if (getopt(argc, argv, "") != -1)
140 (void)time(&shuttime);
144 while ((ch = getopt(argc, argv, "-hknopr")) != -1)
177 if (killflg + doreboot + dohalt + dopower > 1)
178 usage("incompatible switches -h, -k, -p and -r");
180 if (oflag && !(dohalt || dopower || doreboot))
181 usage("-o requires -h, -p or -r");
183 if (nosync != NULL && !oflag)
184 usage("-n requires -o");
190 for (p = mbuf, len = sizeof(mbuf); *argv; ++argv) {
191 arglen = strlen(*argv);
192 if ((len -= arglen) <= 2)
196 memmove(p, *argv, arglen);
205 endp = mbuf + sizeof(mbuf) - 2;
207 if (!fgets(p, endp - p + 1, stdin))
209 for (; *p && p < endp; ++p);
217 mbuflen = strlen(mbuf);
220 (void)printf("Shutdown at %.24s.\n", ctime(&shuttime));
222 (void)printf("Shutdown NOW!\n");
224 if (!(whom = getlogin()))
225 whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???";
228 (void)putc('\n', stdout);
230 (void)setpriority(PRIO_PROCESS, 0, PRIO_MIN);
238 errx(0, "[pid %d]", forkpid);
242 openlog("shutdown", LOG_CONS, LOG_AUTH);
254 if (offset <= NOLOG_TIME) {
261 if (tp->timeleft < offset)
262 (void)sleep((u_int)(offset - tp->timeleft));
264 while (tp->timeleft && offset < tp->timeleft)
267 * Warn now, if going to sleep more than a fifth of
268 * the next wait time.
270 if ((sltime = offset - tp->timeleft)) {
271 if (sltime > (u_int)(tp->timetowait / 5))
277 timewarn(tp->timeleft);
278 if (!logged && tp->timeleft <= NOLOG_TIME) {
282 (void)sleep((u_int)tp->timetowait);
286 die_you_gravy_sucking_pig_dog();
289 static jmp_buf alarmbuf;
291 static const char *restricted_environ[] = {
292 "PATH=" _PATH_STDPATH,
297 timewarn(int timeleft)
300 static char hostname[MAXHOSTNAMELEN + 1];
302 char wcmd[MAXPATHLEN + 4];
305 (void)gethostname(hostname, sizeof(hostname));
307 /* undoc -n option to wall suppresses normal wall banner */
308 (void)snprintf(wcmd, sizeof(wcmd), "%s -n", _PATH_WALL);
309 environ = restricted_environ;
310 if (!(pf = popen(wcmd, "w"))) {
311 syslog(LOG_ERR, "shutdown: can't find %s: %m", _PATH_WALL);
316 "\007*** %sSystem shutdown message from %s@%s ***\007\n",
317 timeleft ? "": "FINAL ", whom, hostname);
319 if (timeleft > 10*60)
320 (void)fprintf(pf, "System going down at %5.5s\n\n",
321 ctime(&shuttime) + 11);
322 else if (timeleft > 59)
323 (void)fprintf(pf, "System going down in %d minute%s\n\n",
324 timeleft / 60, (timeleft > 60) ? "s" : "");
326 (void)fprintf(pf, "System going down in %s30 seconds\n\n",
327 (offset > 0 && offset < 30 ? "less than " : ""));
329 (void)fprintf(pf, "System going down IMMEDIATELY\n\n");
332 (void)fwrite(mbuf, sizeof(*mbuf), mbuflen, pf);
335 * play some games, just in case wall doesn't come back
336 * probably unnecessary, given that wall is careful.
338 if (!setjmp(alarmbuf)) {
339 (void)signal(SIGALRM, timeout);
340 (void)alarm((u_int)30);
342 (void)alarm((u_int)0);
343 (void)signal(SIGALRM, SIG_DFL);
348 timeout(int signo __unused)
350 longjmp(alarmbuf, 1);
354 die_you_gravy_sucking_pig_dog(void)
356 char *empty_environ[] = { NULL };
358 syslog(LOG_NOTICE, "%s by %s: %s",
359 doreboot ? "reboot" : dohalt ? "halt" : dopower ? "power-down" :
360 "shutdown", whom, mbuf);
362 (void)printf("\r\nSystem shutdown time has arrived\007\007\r\n");
364 (void)printf("\rbut you'll have to do it yourself\r\n");
369 (void)printf("reboot");
371 (void)printf("halt");
373 (void)printf("power-down");
375 (void)printf(" no sync");
376 (void)printf("\nkill -HUP 1\n");
379 (void)kill(1, doreboot ? SIGINT : /* reboot */
380 dohalt ? SIGUSR1 : /* halt */
381 dopower ? SIGUSR2 : /* power-down */
382 SIGTERM); /* single-user */
385 execle(_PATH_REBOOT, "reboot", "-l", nosync,
386 (char *)NULL, empty_environ);
387 syslog(LOG_ERR, "shutdown: can't exec %s: %m.",
392 execle(_PATH_HALT, "halt", "-l", nosync,
393 (char *)NULL, empty_environ);
394 syslog(LOG_ERR, "shutdown: can't exec %s: %m.",
399 execle(_PATH_HALT, "halt", "-l", "-p", nosync,
400 (char *)NULL, empty_environ);
401 syslog(LOG_ERR, "shutdown: can't exec %s: %m.",
405 (void)kill(1, SIGTERM); /* to single-user */
411 #define ATOI2(p) (p[0] - '0') * 10 + (p[1] - '0'); p += 2;
414 getoffset(char *timearg)
424 if (!strcasecmp(timearg, "now")) { /* now */
430 if (*timearg == '+') { /* +minutes */
431 if (!isdigit(*++timearg))
434 offset = strtol(timearg, &timeunit, 10);
435 if (offset < 0 || offset == LONG_MAX || errno != 0)
437 if (timeunit[0] == '\0' || strcasecmp(timeunit, "m") == 0 ||
438 strcasecmp(timeunit, "min") == 0 ||
439 strcasecmp(timeunit, "mins") == 0) {
441 } else if (strcasecmp(timeunit, "h") == 0 ||
442 strcasecmp(timeunit, "hour") == 0 ||
443 strcasecmp(timeunit, "hours") == 0) {
445 } else if (strcasecmp(timeunit, "s") == 0 ||
446 strcasecmp(timeunit, "sec") == 0 ||
447 strcasecmp(timeunit, "secs") == 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 */
471 switch(strlen(timearg)) {
473 this_year = lt->tm_year;
474 lt->tm_year = ATOI2(timearg);
476 * check if the specified year is in the next century.
477 * allow for one year of user error as many people will
478 * enter n - 1 at the start of year n.
480 if (lt->tm_year < (this_year % 100) - 1)
482 /* adjust for the year 2000 and beyond */
483 lt->tm_year += (this_year - (this_year % 100));
486 lt->tm_mon = ATOI2(timearg);
487 if (--lt->tm_mon < 0 || lt->tm_mon > 11)
491 lt->tm_mday = ATOI2(timearg);
492 if (lt->tm_mday < 1 || lt->tm_mday > 31)
496 lt->tm_hour = ATOI2(timearg);
497 if (lt->tm_hour < 0 || lt->tm_hour > 23)
499 lt->tm_min = ATOI2(timearg);
500 if (lt->tm_min < 0 || lt->tm_min > 59)
503 if ((shuttime = mktime(lt)) == -1)
505 if ((offset = shuttime - now) < 0)
506 errx(1, "that time is already past.");
513 #define NOMSG "\n\nNO LOGINS: System going down at "
520 (void)unlink(_PATH_NOLOGIN); /* in case linked to another file */
521 (void)signal(SIGINT, finish);
522 (void)signal(SIGHUP, finish);
523 (void)signal(SIGQUIT, finish);
524 (void)signal(SIGTERM, finish);
525 if ((logfd = open(_PATH_NOLOGIN, O_WRONLY|O_CREAT|O_TRUNC,
527 (void)write(logfd, NOMSG, sizeof(NOMSG) - 1);
528 ct = ctime(&shuttime);
529 (void)write(logfd, ct + 11, 5);
530 (void)write(logfd, "\n\n", 2);
531 (void)write(logfd, mbuf, strlen(mbuf));
537 finish(int signo __unused)
540 (void)unlink(_PATH_NOLOGIN);
547 errx(1, "bad time format");
551 usage(const char *cp)
555 (void)fprintf(stderr,
556 "usage: shutdown [-] [-h | -p | -r | -k] [-o [-n]] time [warning-message ...]\n"