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