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>
63 #define _PATH_NOLOGIN "./nologin"
69 #define NOLOG_TIME 5*60
71 int timeleft, timetowait;
90 static time_t offset, shuttime;
91 static int dohalt, dopower, doreboot, killflg, mbuflen, oflag;
92 static char mbuf[BUFSIZ];
93 static const char *nosync, *whom;
95 static void badtime(void);
96 static void perform_shutdown(void);
97 static void finish(int);
98 static void getoffset(char *);
99 static void loop(void);
100 static void nolog(void);
101 static void timeout(int);
102 static void timewarn(int);
103 static void usage(const char *);
105 extern const char **environ;
108 main(int argc, char **argv)
112 int arglen, ch, len, readstdin;
116 errx(1, "NOT super-user");
123 * Test for the special case where the utility is called as
124 * "poweroff", for which it runs 'shutdown -p now'.
126 if ((p = rindex(argv[0], '/')) == NULL)
130 if (strcmp(p, "poweroff") == 0) {
131 if (getopt(argc, argv, "") != -1)
139 (void)time(&shuttime);
143 while ((ch = getopt(argc, argv, "-hknopr")) != -1)
176 if (killflg + doreboot + dohalt + dopower > 1)
177 usage("incompatible switches -h, -k, -p and -r");
179 if (oflag && !(dohalt || dopower || doreboot))
180 usage("-o requires -h, -p or -r");
182 if (nosync != NULL && !oflag)
183 usage("-n requires -o");
189 for (p = mbuf, len = sizeof(mbuf); *argv; ++argv) {
190 arglen = strlen(*argv);
191 if ((len -= arglen) <= 2)
195 memmove(p, *argv, arglen);
204 endp = mbuf + sizeof(mbuf) - 2;
206 if (!fgets(p, endp - p + 1, stdin))
208 for (; *p && p < endp; ++p);
216 mbuflen = strlen(mbuf);
219 (void)printf("Shutdown at %.24s.\n", ctime(&shuttime));
221 (void)printf("Shutdown NOW!\n");
223 if (!(whom = getlogin()))
224 whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???";
227 (void)putc('\n', stdout);
229 (void)setpriority(PRIO_PROCESS, 0, PRIO_MIN);
237 errx(0, "[pid %d]", forkpid);
241 openlog("shutdown", LOG_CONS, LOG_AUTH);
253 if (offset <= NOLOG_TIME) {
260 if (tp->timeleft < offset)
261 (void)sleep((u_int)(offset - tp->timeleft));
263 while (tp->timeleft && offset < tp->timeleft)
266 * Warn now, if going to sleep more than a fifth of
267 * the next wait time.
269 if ((sltime = offset - tp->timeleft)) {
270 if (sltime > (u_int)(tp->timetowait / 5))
276 timewarn(tp->timeleft);
277 if (!logged && tp->timeleft <= NOLOG_TIME) {
281 (void)sleep((u_int)tp->timetowait);
288 static jmp_buf alarmbuf;
290 static const char *restricted_environ[] = {
291 "PATH=" _PATH_STDPATH,
296 timewarn(int timeleft)
299 static char hostname[MAXHOSTNAMELEN + 1];
301 char wcmd[MAXPATHLEN + 4];
304 (void)gethostname(hostname, sizeof(hostname));
306 /* undoc -n option to wall suppresses normal wall banner */
307 (void)snprintf(wcmd, sizeof(wcmd), "%s -n", _PATH_WALL);
308 environ = restricted_environ;
309 if (!(pf = popen(wcmd, "w"))) {
310 syslog(LOG_ERR, "shutdown: can't find %s: %m", _PATH_WALL);
315 "\007*** %sSystem shutdown message from %s@%s ***\007\n",
316 timeleft ? "": "FINAL ", whom, hostname);
318 if (timeleft > 10*60)
319 (void)fprintf(pf, "System going down at %5.5s\n\n",
320 ctime(&shuttime) + 11);
321 else if (timeleft > 59)
322 (void)fprintf(pf, "System going down in %d minute%s\n\n",
323 timeleft / 60, (timeleft > 60) ? "s" : "");
325 (void)fprintf(pf, "System going down in 30 seconds\n\n");
327 (void)fprintf(pf, "System going down IMMEDIATELY\n\n");
330 (void)fwrite(mbuf, sizeof(*mbuf), mbuflen, pf);
333 * play some games, just in case wall doesn't come back
334 * probably unnecessary, given that wall is careful.
336 if (!setjmp(alarmbuf)) {
337 (void)signal(SIGALRM, timeout);
338 (void)alarm((u_int)30);
340 (void)alarm((u_int)0);
341 (void)signal(SIGALRM, SIG_DFL);
346 timeout(int signo __unused)
348 longjmp(alarmbuf, 1);
352 perform_shutdown(void)
354 char *empty_environ[] = { NULL };
356 syslog(LOG_NOTICE, "%s by %s: %s",
357 doreboot ? "reboot" : dohalt ? "halt" : dopower ? "power-down" :
358 "shutdown", whom, mbuf);
361 (void)printf("\r\nSystem shutdown time has arrived\007\007\r\n");
363 (void)printf("\rbut you'll have to do it yourself\r\n");
368 (void)printf("reboot");
370 (void)printf("halt");
372 (void)printf("power-down");
374 (void)printf(" no sync");
375 (void)printf("\nkill -HUP 1\n");
378 (void)kill(1, doreboot ? SIGINT : /* reboot */
379 dohalt ? SIGUSR1 : /* halt */
380 dopower ? SIGUSR2 : /* power-down */
381 SIGTERM); /* single-user */
384 execle(_PATH_REBOOT, "reboot", "-l", nosync,
385 (char *)NULL, empty_environ);
386 syslog(LOG_ERR, "shutdown: can't exec %s: %m.",
391 execle(_PATH_HALT, "halt", "-l", nosync,
392 (char *)NULL, empty_environ);
393 syslog(LOG_ERR, "shutdown: can't exec %s: %m.",
398 execle(_PATH_HALT, "halt", "-l", "-p", nosync,
399 (char *)NULL, empty_environ);
400 syslog(LOG_ERR, "shutdown: can't exec %s: %m.",
404 (void)kill(1, SIGTERM); /* to single-user */
410 #define ATOI2(p) (p[0] - '0') * 10 + (p[1] - '0'); p += 2;
413 getoffset(char *timearg)
422 if (!strcasecmp(timearg, "now")) { /* now */
428 if (*timearg == '+') { /* +minutes */
429 if (!isdigit(*++timearg))
431 if ((offset = atoi(timearg) * 60) < 0)
433 shuttime = now + offset;
437 /* handle hh:mm by getting rid of the colon */
438 for (p = timearg; *p; ++p)
439 if (!isascii(*p) || !isdigit(*p)) {
440 if (*p == ':' && strlen(p) == 3) {
449 unsetenv("TZ"); /* OUR timezone */
450 lt = localtime(&now); /* current time val */
452 switch(strlen(timearg)) {
454 this_year = lt->tm_year;
455 lt->tm_year = ATOI2(timearg);
457 * check if the specified year is in the next century.
458 * allow for one year of user error as many people will
459 * enter n - 1 at the start of year n.
461 if (lt->tm_year < (this_year % 100) - 1)
463 /* adjust for the year 2000 and beyond */
464 lt->tm_year += (this_year - (this_year % 100));
467 lt->tm_mon = ATOI2(timearg);
468 if (--lt->tm_mon < 0 || lt->tm_mon > 11)
472 lt->tm_mday = ATOI2(timearg);
473 if (lt->tm_mday < 1 || lt->tm_mday > 31)
477 lt->tm_hour = ATOI2(timearg);
478 if (lt->tm_hour < 0 || lt->tm_hour > 23)
480 lt->tm_min = ATOI2(timearg);
481 if (lt->tm_min < 0 || lt->tm_min > 59)
484 if ((shuttime = mktime(lt)) == -1)
486 if ((offset = shuttime - now) < 0)
487 errx(1, "that time is already past.");
494 #define NOMSG "\n\nNO LOGINS: System going down at "
501 (void)unlink(_PATH_NOLOGIN); /* in case linked to another file */
502 (void)signal(SIGINT, finish);
503 (void)signal(SIGHUP, finish);
504 (void)signal(SIGQUIT, finish);
505 (void)signal(SIGTERM, finish);
506 if ((logfd = open(_PATH_NOLOGIN, O_WRONLY|O_CREAT|O_TRUNC,
508 (void)write(logfd, NOMSG, sizeof(NOMSG) - 1);
509 ct = ctime(&shuttime);
510 (void)write(logfd, ct + 11, 5);
511 (void)write(logfd, "\n\n", 2);
512 (void)write(logfd, mbuf, strlen(mbuf));
518 finish(int signo __unused)
521 (void)unlink(_PATH_NOLOGIN);
528 errx(1, "bad time format");
532 usage(const char *cp)
536 (void)fprintf(stderr,
537 "usage: shutdown [-] [-h | -p | -r | -k] [-o [-n]] time [warning-message ...]\n"