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 $
32 * $DragonFly: src/sbin/shutdown/shutdown.c,v 1.6 2005/01/02 01:22:49 cpressey Exp $
35 #include <sys/param.h>
37 #include <sys/resource.h>
38 #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 30 seconds\n\n");
319 fprintf(pf, "System going down IMMEDIATELY\n\n");
322 fwrite(mbuf, sizeof(*mbuf), mbuflen, pf);
325 * play some games, just in case wall doesn't come back
326 * probably unnecessary, given that wall is careful.
328 if (!setjmp(alarmbuf)) {
329 signal(SIGALRM, timeout);
333 signal(SIGALRM, SIG_DFL);
338 timeout(int signo __unused)
340 longjmp(alarmbuf, 1);
344 die_you_gravy_sucking_pig_dog(void)
346 char *empty_environ[] = { NULL };
348 syslog(LOG_NOTICE, "%s by %s: %s",
349 doreboot ? "reboot" : dohalt ? "halt" : dopower ? "power-down" :
350 "shutdown", whom, mbuf);
353 printf("\r\nSystem shutdown time has arrived\007\007\r\n");
355 printf("\rbut you'll have to do it yourself\r\n");
364 printf("power-down");
367 printf("\nkill -HUP 1\n");
370 kill(1, doreboot ? SIGINT : /* reboot */
371 dohalt ? SIGUSR1 : /* halt */
372 dopower ? SIGUSR2 : /* power-down */
373 SIGTERM); /* single-user */
376 execle(_PATH_REBOOT, "reboot", "-l", nosync,
377 NULL, empty_environ);
378 syslog(LOG_ERR, "shutdown: can't exec %s: %m.",
383 execle(_PATH_HALT, "halt", "-l", nosync,
384 NULL, empty_environ);
385 syslog(LOG_ERR, "shutdown: can't exec %s: %m.",
390 execle(_PATH_HALT, "halt", "-l", "-p", nosync,
391 NULL, empty_environ);
392 syslog(LOG_ERR, "shutdown: can't exec %s: %m.",
396 kill(1, SIGTERM); /* to single-user */
402 #define ATOI2(p) (p[0] - '0') * 10 + (p[1] - '0'); p += 2;
405 getoffset(char *timearg)
414 if (!strcasecmp(timearg, "now")) { /* now */
420 if (*timearg == '+') { /* +minutes */
421 if (!isdigit(*++timearg))
423 if ((offset = atoi(timearg) * 60) < 0)
425 shuttime = now + offset;
429 /* handle hh:mm by getting rid of the colon */
430 for (p = timearg; *p; ++p)
431 if (!isascii(*p) || !isdigit(*p)) {
432 if (*p == ':' && strlen(p) == 3) {
441 unsetenv("TZ"); /* OUR timezone */
442 lt = localtime(&now); /* current time val */
444 switch(strlen(timearg)) {
446 this_year = lt->tm_year;
447 lt->tm_year = ATOI2(timearg);
449 * check if the specified year is in the next century.
450 * allow for one year of user error as many people will
451 * enter n - 1 at the start of year n.
453 if (lt->tm_year < (this_year % 100) - 1)
455 /* adjust for the year 2000 and beyond */
456 lt->tm_year += (this_year - (this_year % 100));
459 lt->tm_mon = ATOI2(timearg);
460 if (--lt->tm_mon < 0 || lt->tm_mon > 11)
464 lt->tm_mday = ATOI2(timearg);
465 if (lt->tm_mday < 1 || lt->tm_mday > 31)
469 lt->tm_hour = ATOI2(timearg);
470 if (lt->tm_hour < 0 || lt->tm_hour > 23)
472 lt->tm_min = ATOI2(timearg);
473 if (lt->tm_min < 0 || lt->tm_min > 59)
476 if ((shuttime = mktime(lt)) == -1)
478 if ((offset = shuttime - now) < 0)
479 errx(1, "that time is already past.");
486 #define NOMSG "\n\nNO LOGINS: System going down at "
493 unlink(_PATH_NOLOGIN); /* in case linked to another file */
494 signal(SIGINT, finish);
495 signal(SIGHUP, finish);
496 signal(SIGQUIT, finish);
497 signal(SIGTERM, finish);
498 if ((logfd = open(_PATH_NOLOGIN, O_WRONLY|O_CREAT|O_TRUNC,
500 write(logfd, NOMSG, sizeof(NOMSG) - 1);
501 ct = ctime(&shuttime);
502 write(logfd, ct + 11, 5);
503 write(logfd, "\n\n", 2);
504 write(logfd, mbuf, strlen(mbuf));
510 finish(int signo __unused)
513 unlink(_PATH_NOLOGIN);
520 errx(1, "bad time format");
524 usage(const char *cp)
529 "usage: shutdown [-] [-h | -p | -r | -k] [-o [-n]]"
530 " time [warning-message ...]\n"