1 /* 2 * Copyright (c) 1988, 1990, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 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. 16 * 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 27 * SUCH DAMAGE. 28 */ 29 30 #if 0 31 #ifndef lint 32 static const char copyright[] = 33 "@(#) Copyright (c) 1988, 1990, 1993\n\ 34 The Regents of the University of California. All rights reserved.\n"; 35 #endif /* not lint */ 36 37 #ifndef lint 38 static char sccsid[] = "@(#)shutdown.c 8.4 (Berkeley) 4/28/95"; 39 #endif /* not lint */ 40 #endif 41 #include <sys/cdefs.h> 42 __FBSDID("$FreeBSD$"); 43 44 #include <sys/param.h> 45 #include <sys/time.h> 46 #include <sys/resource.h> 47 #include <sys/syslog.h> 48 49 #include <ctype.h> 50 #include <err.h> 51 #include <errno.h> 52 #include <fcntl.h> 53 #include <paths.h> 54 #include <pwd.h> 55 #include <setjmp.h> 56 #include <signal.h> 57 #include <stdio.h> 58 #include <stdlib.h> 59 #include <string.h> 60 #include <unistd.h> 61 62 #ifdef DEBUG 63 #undef _PATH_NOLOGIN 64 #define _PATH_NOLOGIN "./nologin" 65 #endif 66 67 #define H *60*60 68 #define M *60 69 #define S *1 70 #define NOLOG_TIME 5*60 71 static struct interval { 72 int timeleft, timetowait; 73 } tlist[] = { 74 { 10 H, 5 H }, 75 { 5 H, 3 H }, 76 { 2 H, 1 H }, 77 { 1 H, 30 M }, 78 { 30 M, 10 M }, 79 { 20 M, 10 M }, 80 { 10 M, 5 M }, 81 { 5 M, 3 M }, 82 { 2 M, 1 M }, 83 { 1 M, 30 S }, 84 { 30 S, 30 S }, 85 { 0 , 0 } 86 }; 87 #undef H 88 #undef M 89 #undef S 90 91 static time_t offset, shuttime; 92 static int docycle, dohalt, dopower, doreboot, killflg, mbuflen, oflag; 93 static char mbuf[BUFSIZ]; 94 static const char *nosync, *whom; 95 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 *); 105 106 extern const char **environ; 107 108 int 109 main(int argc, char **argv) 110 { 111 char *p, *endp; 112 struct passwd *pw; 113 int arglen, ch, len, readstdin; 114 115 #ifndef DEBUG 116 if (geteuid()) 117 errx(1, "NOT super-user"); 118 #endif 119 120 nosync = NULL; 121 readstdin = 0; 122 123 /* 124 * Test for the special case where the utility is called as 125 * "poweroff", for which it runs 'shutdown -p now'. 126 */ 127 if ((p = strrchr(argv[0], '/')) == NULL) 128 p = argv[0]; 129 else 130 ++p; 131 if (strcmp(p, "poweroff") == 0) { 132 if (getopt(argc, argv, "") != -1) 133 usage((char *)NULL); 134 argc -= optind; 135 argv += optind; 136 if (argc != 0) 137 usage((char *)NULL); 138 dopower = 1; 139 offset = 0; 140 (void)time(&shuttime); 141 goto poweroff; 142 } 143 144 while ((ch = getopt(argc, argv, "-chknopr")) != -1) 145 switch (ch) { 146 case '-': 147 readstdin = 1; 148 break; 149 case 'c': 150 docycle = 1; 151 break; 152 case 'h': 153 dohalt = 1; 154 break; 155 case 'k': 156 killflg = 1; 157 break; 158 case 'n': 159 nosync = "-n"; 160 break; 161 case 'o': 162 oflag = 1; 163 break; 164 case 'p': 165 dopower = 1; 166 break; 167 case 'r': 168 doreboot = 1; 169 break; 170 case '?': 171 default: 172 usage((char *)NULL); 173 } 174 argc -= optind; 175 argv += optind; 176 177 if (argc < 1) 178 usage((char *)NULL); 179 180 if (killflg + doreboot + dohalt + dopower + docycle > 1) 181 usage("incompatible switches -c, -h, -k, -p and -r"); 182 183 if (oflag && !(dohalt || dopower || doreboot || docycle)) 184 usage("-o requires -c, -h, -p or -r"); 185 186 if (nosync != NULL && !oflag) 187 usage("-n requires -o"); 188 189 getoffset(*argv++); 190 191 poweroff: 192 if (*argv) { 193 for (p = mbuf, len = sizeof(mbuf); *argv; ++argv) { 194 arglen = strlen(*argv); 195 if ((len -= arglen) <= 2) 196 break; 197 if (p != mbuf) 198 *p++ = ' '; 199 memmove(p, *argv, arglen); 200 p += arglen; 201 } 202 *p = '\n'; 203 *++p = '\0'; 204 } 205 206 if (readstdin) { 207 p = mbuf; 208 endp = mbuf + sizeof(mbuf) - 2; 209 for (;;) { 210 if (!fgets(p, endp - p + 1, stdin)) 211 break; 212 for (; *p && p < endp; ++p); 213 if (p == endp) { 214 *p = '\n'; 215 *++p = '\0'; 216 break; 217 } 218 } 219 } 220 mbuflen = strlen(mbuf); 221 222 if (offset) 223 (void)printf("Shutdown at %.24s.\n", ctime(&shuttime)); 224 else 225 (void)printf("Shutdown NOW!\n"); 226 227 if (!(whom = getlogin())) 228 whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???"; 229 230 #ifdef DEBUG 231 (void)putc('\n', stdout); 232 #else 233 (void)setpriority(PRIO_PROCESS, 0, PRIO_MIN); 234 { 235 int forkpid; 236 237 forkpid = fork(); 238 if (forkpid == -1) 239 err(1, "fork"); 240 if (forkpid) 241 errx(0, "[pid %d]", forkpid); 242 } 243 setsid(); 244 #endif 245 openlog("shutdown", LOG_CONS, LOG_AUTH); 246 loop(); 247 return(0); 248 } 249 250 static void 251 loop(void) 252 { 253 struct interval *tp; 254 u_int sltime; 255 int logged; 256 257 if (offset <= NOLOG_TIME) { 258 logged = 1; 259 nolog(); 260 } 261 else 262 logged = 0; 263 tp = tlist; 264 if (tp->timeleft < offset) 265 (void)sleep((u_int)(offset - tp->timeleft)); 266 else { 267 while (tp->timeleft && offset < tp->timeleft) 268 ++tp; 269 /* 270 * Warn now, if going to sleep more than a fifth of 271 * the next wait time. 272 */ 273 if ((sltime = offset - tp->timeleft)) { 274 if (sltime > (u_int)(tp->timetowait / 5)) 275 timewarn(offset); 276 (void)sleep(sltime); 277 } 278 } 279 for (;; ++tp) { 280 timewarn(tp->timeleft); 281 if (!logged && tp->timeleft <= NOLOG_TIME) { 282 logged = 1; 283 nolog(); 284 } 285 (void)sleep((u_int)tp->timetowait); 286 if (!tp->timeleft) 287 break; 288 } 289 die_you_gravy_sucking_pig_dog(); 290 } 291 292 static jmp_buf alarmbuf; 293 294 static const char *restricted_environ[] = { 295 "PATH=" _PATH_STDPATH, 296 NULL 297 }; 298 299 static void 300 timewarn(int timeleft) 301 { 302 static int first; 303 static char hostname[MAXHOSTNAMELEN + 1]; 304 FILE *pf; 305 char wcmd[MAXPATHLEN + 4]; 306 307 if (!first++) 308 (void)gethostname(hostname, sizeof(hostname)); 309 310 /* undoc -n option to wall suppresses normal wall banner */ 311 (void)snprintf(wcmd, sizeof(wcmd), "%s -n", _PATH_WALL); 312 environ = restricted_environ; 313 if (!(pf = popen(wcmd, "w"))) { 314 syslog(LOG_ERR, "shutdown: can't find %s: %m", _PATH_WALL); 315 return; 316 } 317 318 (void)fprintf(pf, 319 "\007*** %sSystem shutdown message from %s@%s ***\007\n", 320 timeleft ? "": "FINAL ", whom, hostname); 321 322 if (timeleft > 10*60) 323 (void)fprintf(pf, "System going down at %5.5s\n\n", 324 ctime(&shuttime) + 11); 325 else if (timeleft > 59) 326 (void)fprintf(pf, "System going down in %d minute%s\n\n", 327 timeleft / 60, (timeleft > 60) ? "s" : ""); 328 else if (timeleft) 329 (void)fprintf(pf, "System going down in %s30 seconds\n\n", 330 (offset > 0 && offset < 30 ? "less than " : "")); 331 else 332 (void)fprintf(pf, "System going down IMMEDIATELY\n\n"); 333 334 if (mbuflen) 335 (void)fwrite(mbuf, sizeof(*mbuf), mbuflen, pf); 336 337 /* 338 * play some games, just in case wall doesn't come back 339 * probably unnecessary, given that wall is careful. 340 */ 341 if (!setjmp(alarmbuf)) { 342 (void)signal(SIGALRM, timeout); 343 (void)alarm((u_int)30); 344 (void)pclose(pf); 345 (void)alarm((u_int)0); 346 (void)signal(SIGALRM, SIG_DFL); 347 } 348 } 349 350 static void 351 timeout(int signo __unused) 352 { 353 longjmp(alarmbuf, 1); 354 } 355 356 static void 357 die_you_gravy_sucking_pig_dog(void) 358 { 359 char *empty_environ[] = { NULL }; 360 361 syslog(LOG_NOTICE, "%s by %s: %s", 362 doreboot ? "reboot" : dohalt ? "halt" : dopower ? "power-down" : 363 docycle ? "power-cycle" : "shutdown", whom, mbuf); 364 365 (void)printf("\r\nSystem shutdown time has arrived\007\007\r\n"); 366 if (killflg) { 367 (void)printf("\rbut you'll have to do it yourself\r\n"); 368 exit(0); 369 } 370 #ifdef DEBUG 371 if (doreboot) 372 (void)printf("reboot"); 373 else if (docycle) 374 (void)printf("power-cycle"); 375 else if (dohalt) 376 (void)printf("halt"); 377 else if (dopower) 378 (void)printf("power-down"); 379 if (nosync != NULL) 380 (void)printf(" no sync"); 381 (void)printf("\nkill -HUP 1\n"); 382 #else 383 if (!oflag) { 384 (void)kill(1, doreboot ? SIGINT : /* reboot */ 385 dohalt ? SIGUSR1 : /* halt */ 386 dopower ? SIGUSR2 : /* power-down */ 387 docycle ? SIGWINCH : /* power-cycle */ 388 SIGTERM); /* single-user */ 389 } else { 390 if (doreboot) { 391 execle(_PATH_REBOOT, "reboot", "-l", nosync, 392 (char *)NULL, empty_environ); 393 syslog(LOG_ERR, "shutdown: can't exec %s: %m.", 394 _PATH_REBOOT); 395 warn(_PATH_REBOOT); 396 } 397 else if (dohalt) { 398 execle(_PATH_HALT, "halt", "-l", nosync, 399 (char *)NULL, empty_environ); 400 syslog(LOG_ERR, "shutdown: can't exec %s: %m.", 401 _PATH_HALT); 402 warn(_PATH_HALT); 403 } 404 else if (dopower) { 405 execle(_PATH_HALT, "halt", "-l", "-p", nosync, 406 (char *)NULL, empty_environ); 407 syslog(LOG_ERR, "shutdown: can't exec %s: %m.", 408 _PATH_HALT); 409 warn(_PATH_HALT); 410 } 411 else if (docycle) { 412 execle(_PATH_HALT, "halt", "-l", "-c", nosync, 413 (char *)NULL, empty_environ); 414 syslog(LOG_ERR, "shutdown: can't exec %s: %m.", 415 _PATH_HALT); 416 warn(_PATH_HALT); 417 } 418 (void)kill(1, SIGTERM); /* to single-user */ 419 } 420 #endif 421 finish(0); 422 } 423 424 #define ATOI2(p) (p[0] - '0') * 10 + (p[1] - '0'); p += 2; 425 426 static void 427 getoffset(char *timearg) 428 { 429 struct tm *lt; 430 char *p; 431 time_t now; 432 int this_year; 433 char *timeunit; 434 435 (void)time(&now); 436 437 if (!strcasecmp(timearg, "now")) { /* now */ 438 offset = 0; 439 shuttime = now; 440 return; 441 } 442 443 if (*timearg == '+') { /* +minutes */ 444 if (!isdigit(*++timearg)) 445 badtime(); 446 errno = 0; 447 offset = strtol(timearg, &timeunit, 10); 448 if (offset < 0 || offset == LONG_MAX || errno != 0) 449 badtime(); 450 if (timeunit[0] == '\0' || strcasecmp(timeunit, "m") == 0 || 451 strcasecmp(timeunit, "min") == 0 || 452 strcasecmp(timeunit, "mins") == 0) { 453 offset *= 60; 454 } else if (strcasecmp(timeunit, "h") == 0 || 455 strcasecmp(timeunit, "hour") == 0 || 456 strcasecmp(timeunit, "hours") == 0) { 457 offset *= 60 * 60; 458 } else if (strcasecmp(timeunit, "s") == 0 || 459 strcasecmp(timeunit, "sec") == 0 || 460 strcasecmp(timeunit, "secs") == 0) { 461 offset *= 1; 462 } else { 463 badtime(); 464 } 465 shuttime = now + offset; 466 return; 467 } 468 469 /* handle hh:mm by getting rid of the colon */ 470 for (p = timearg; *p; ++p) 471 if (!isascii(*p) || !isdigit(*p)) { 472 if (*p == ':' && strlen(p) == 3) { 473 p[0] = p[1]; 474 p[1] = p[2]; 475 p[2] = '\0'; 476 } 477 else 478 badtime(); 479 } 480 481 unsetenv("TZ"); /* OUR timezone */ 482 lt = localtime(&now); /* current time val */ 483 484 switch(strlen(timearg)) { 485 case 10: 486 this_year = lt->tm_year; 487 lt->tm_year = ATOI2(timearg); 488 /* 489 * check if the specified year is in the next century. 490 * allow for one year of user error as many people will 491 * enter n - 1 at the start of year n. 492 */ 493 if (lt->tm_year < (this_year % 100) - 1) 494 lt->tm_year += 100; 495 /* adjust for the year 2000 and beyond */ 496 lt->tm_year += (this_year - (this_year % 100)); 497 /* FALLTHROUGH */ 498 case 8: 499 lt->tm_mon = ATOI2(timearg); 500 if (--lt->tm_mon < 0 || lt->tm_mon > 11) 501 badtime(); 502 /* FALLTHROUGH */ 503 case 6: 504 lt->tm_mday = ATOI2(timearg); 505 if (lt->tm_mday < 1 || lt->tm_mday > 31) 506 badtime(); 507 /* FALLTHROUGH */ 508 case 4: 509 lt->tm_hour = ATOI2(timearg); 510 if (lt->tm_hour < 0 || lt->tm_hour > 23) 511 badtime(); 512 lt->tm_min = ATOI2(timearg); 513 if (lt->tm_min < 0 || lt->tm_min > 59) 514 badtime(); 515 lt->tm_sec = 0; 516 if ((shuttime = mktime(lt)) == -1) 517 badtime(); 518 if ((offset = shuttime - now) < 0) 519 errx(1, "that time is already past."); 520 break; 521 default: 522 badtime(); 523 } 524 } 525 526 #define NOMSG "\n\nNO LOGINS: System going down at " 527 static void 528 nolog(void) 529 { 530 int logfd; 531 char *ct; 532 533 (void)unlink(_PATH_NOLOGIN); /* in case linked to another file */ 534 (void)signal(SIGINT, finish); 535 (void)signal(SIGHUP, finish); 536 (void)signal(SIGQUIT, finish); 537 (void)signal(SIGTERM, finish); 538 if ((logfd = open(_PATH_NOLOGIN, O_WRONLY|O_CREAT|O_TRUNC, 539 0664)) >= 0) { 540 (void)write(logfd, NOMSG, sizeof(NOMSG) - 1); 541 ct = ctime(&shuttime); 542 (void)write(logfd, ct + 11, 5); 543 (void)write(logfd, "\n\n", 2); 544 (void)write(logfd, mbuf, strlen(mbuf)); 545 (void)close(logfd); 546 } 547 } 548 549 static void 550 finish(int signo __unused) 551 { 552 if (!killflg) 553 (void)unlink(_PATH_NOLOGIN); 554 exit(0); 555 } 556 557 static void 558 badtime(void) 559 { 560 errx(1, "bad time format"); 561 } 562 563 static void 564 usage(const char *cp) 565 { 566 if (cp != NULL) 567 warnx("%s", cp); 568 (void)fprintf(stderr, 569 "usage: shutdown [-] [-c | -h | -p | -r | -k] [-o [-n]] time [warning-message ...]\n" 570 " poweroff\n"); 571 exit(1); 572 } 573