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