shutdown.c revision 38036
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. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34#ifndef lint 35static const char copyright[] = 36"@(#) Copyright (c) 1988, 1990, 1993\n\ 37 The Regents of the University of California. All rights reserved.\n"; 38#endif /* not lint */ 39 40#ifndef lint 41#if 0 42static char sccsid[] = "@(#)shutdown.c 8.2 (Berkeley) 2/16/94"; 43#endif 44static const char rcsid[] = 45 "$Id$"; 46#endif /* not lint */ 47 48#include <sys/param.h> 49#include <sys/time.h> 50#include <sys/resource.h> 51#include <sys/syslog.h> 52 53#include <ctype.h> 54#include <err.h> 55#include <fcntl.h> 56#include <pwd.h> 57#include <setjmp.h> 58#include <signal.h> 59#include <stdio.h> 60#include <stdlib.h> 61#include <string.h> 62#include <unistd.h> 63 64#include "pathnames.h" 65 66#ifdef DEBUG 67#undef _PATH_NOLOGIN 68#define _PATH_NOLOGIN "./nologin" 69#endif 70 71#define H *60*60 72#define M *60 73#define S *1 74#define NOLOG_TIME 5*60 75struct interval { 76 int timeleft, timetowait; 77} tlist[] = { 78 { 10 H, 5 H }, 79 { 5 H, 3 H }, 80 { 2 H, 1 H }, 81 { 1 H, 30 M }, 82 { 30 M, 10 M }, 83 { 20 M, 10 M }, 84 { 10 M, 5 M }, 85 { 5 M, 3 M }, 86 { 2 M, 1 M }, 87 { 1 M, 30 S }, 88 { 30 S, 30 S }, 89 { 0 , 0 } 90}; 91#undef H 92#undef M 93#undef S 94 95static time_t offset, shuttime; 96static int dohalt, doreboot, killflg, mbuflen; 97static char *nosync, *whom, mbuf[BUFSIZ]; 98 99void badtime __P((void)); 100void die_you_gravy_sucking_pig_dog __P((void)); 101void finish __P((int)); 102void getoffset __P((char *)); 103void loop __P((void)); 104void nolog __P((void)); 105void timeout __P((int)); 106void timewarn __P((int)); 107void usage __P((void)); 108 109int 110main(argc, argv) 111 int argc; 112 char *argv[]; 113{ 114 register char *p, *endp; 115 struct passwd *pw; 116 int arglen, ch, len, readstdin; 117 118#ifndef DEBUG 119 if (geteuid()) 120 errx(1, "NOT super-user"); 121#endif 122 nosync = NULL; 123 readstdin = 0; 124 while ((ch = getopt(argc, argv, "-hknr")) != -1) 125 switch (ch) { 126 case '-': 127 readstdin = 1; 128 break; 129 case 'h': 130 dohalt = 1; 131 break; 132 case 'k': 133 killflg = 1; 134 break; 135 case 'n': 136 nosync = "-n"; 137 break; 138 case 'r': 139 doreboot = 1; 140 break; 141 case '?': 142 default: 143 usage(); 144 } 145 argc -= optind; 146 argv += optind; 147 148 if (argc < 1) 149 usage(); 150 151 if (doreboot && dohalt) { 152 warnx("incompatible switches -h and -r"); 153 usage(); 154 } 155 getoffset(*argv++); 156 157 if (*argv) { 158 for (p = mbuf, len = sizeof(mbuf); *argv; ++argv) { 159 arglen = strlen(*argv); 160 if ((len -= arglen) <= 2) 161 break; 162 if (p != mbuf) 163 *p++ = ' '; 164 bcopy(*argv, p, arglen); 165 p += arglen; 166 } 167 *p = '\n'; 168 *++p = '\0'; 169 } 170 171 if (readstdin) { 172 p = mbuf; 173 endp = mbuf + sizeof(mbuf) - 2; 174 for (;;) { 175 if (!fgets(p, endp - p + 1, stdin)) 176 break; 177 for (; *p && p < endp; ++p); 178 if (p == endp) { 179 *p = '\n'; 180 *++p = '\0'; 181 break; 182 } 183 } 184 } 185 mbuflen = strlen(mbuf); 186 187 if (offset) 188 (void)printf("Shutdown at %.24s.\n", ctime(&shuttime)); 189 else 190 (void)printf("Shutdown NOW!\n"); 191 192 if (!(whom = getlogin())) 193 whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???"; 194 195#ifdef DEBUG 196 (void)putc('\n', stdout); 197#else 198 (void)setpriority(PRIO_PROCESS, 0, PRIO_MIN); 199 { 200 int forkpid; 201 202 forkpid = fork(); 203 if (forkpid == -1) 204 err(1, "fork"); 205 if (forkpid) 206 errx(0, "[pid %d]", forkpid); 207 } 208 setsid(); 209#endif 210 openlog("shutdown", LOG_CONS, LOG_AUTH); 211 loop(); 212 return(0); 213} 214 215void 216loop() 217{ 218 struct interval *tp; 219 u_int sltime; 220 int logged; 221 222 if (offset <= NOLOG_TIME) { 223 logged = 1; 224 nolog(); 225 } 226 else 227 logged = 0; 228 tp = tlist; 229 if (tp->timeleft < offset) 230 (void)sleep((u_int)(offset - tp->timeleft)); 231 else { 232 while (offset < tp->timeleft) 233 ++tp; 234 /* 235 * Warn now, if going to sleep more than a fifth of 236 * the next wait time. 237 */ 238 if ((sltime = offset - tp->timeleft)) { 239 if (sltime > tp->timetowait / 5) 240 timewarn(offset); 241 (void)sleep(sltime); 242 } 243 } 244 for (;; ++tp) { 245 timewarn(tp->timeleft); 246 if (!logged && tp->timeleft <= NOLOG_TIME) { 247 logged = 1; 248 nolog(); 249 } 250 (void)sleep((u_int)tp->timetowait); 251 if (!tp->timeleft) 252 break; 253 } 254 die_you_gravy_sucking_pig_dog(); 255} 256 257static jmp_buf alarmbuf; 258 259static char *restricted_environ[] = { 260 "PATH=" _PATH_STDPATH, 261 NULL 262}; 263 264void 265timewarn(timeleft) 266 int timeleft; 267{ 268 static int first; 269 static char hostname[MAXHOSTNAMELEN + 1]; 270 FILE *pf; 271 char wcmd[MAXPATHLEN + 4]; 272 extern char **environ; 273 274 if (!first++) 275 (void)gethostname(hostname, sizeof(hostname)); 276 277 /* undoc -n option to wall suppresses normal wall banner */ 278 (void)snprintf(wcmd, sizeof(wcmd), "%s -n", _PATH_WALL); 279 environ = restricted_environ; 280 if (!(pf = popen(wcmd, "w"))) { 281 syslog(LOG_ERR, "shutdown: can't find %s: %m", _PATH_WALL); 282 return; 283 } 284 285 (void)fprintf(pf, 286 "\007*** %sSystem shutdown message from %s@%s ***\007\n", 287 timeleft ? "": "FINAL ", whom, hostname); 288 289 if (timeleft > 10*60) 290 (void)fprintf(pf, "System going down at %5.5s\n\n", 291 ctime(&shuttime) + 11); 292 else if (timeleft > 59) 293 (void)fprintf(pf, "System going down in %d minute%s\n\n", 294 timeleft / 60, (timeleft > 60) ? "s" : ""); 295 else if (timeleft) 296 (void)fprintf(pf, "System going down in 30 seconds\n\n"); 297 else 298 (void)fprintf(pf, "System going down IMMEDIATELY\n\n"); 299 300 if (mbuflen) 301 (void)fwrite(mbuf, sizeof(*mbuf), mbuflen, pf); 302 303 /* 304 * play some games, just in case wall doesn't come back 305 * probably unnecessary, given that wall is careful. 306 */ 307 if (!setjmp(alarmbuf)) { 308 (void)signal(SIGALRM, timeout); 309 (void)alarm((u_int)30); 310 (void)pclose(pf); 311 (void)alarm((u_int)0); 312 (void)signal(SIGALRM, SIG_DFL); 313 } 314} 315 316void 317timeout(signo) 318 int signo; 319{ 320 longjmp(alarmbuf, 1); 321} 322 323void 324die_you_gravy_sucking_pig_dog() 325{ 326 char *empty_environ[] = { NULL }; 327 328 syslog(LOG_NOTICE, "%s by %s: %s", 329 doreboot ? "reboot" : dohalt ? "halt" : "shutdown", whom, mbuf); 330 (void)sleep(2); 331 332 (void)printf("\r\nSystem shutdown time has arrived\007\007\r\n"); 333 if (killflg) { 334 (void)printf("\rbut you'll have to do it yourself\r\n"); 335 exit(0); 336 } 337#ifdef DEBUG 338 if (doreboot) 339 (void)printf("reboot"); 340 else if (dohalt) 341 (void)printf("halt"); 342 if (nosync) 343 (void)printf(" no sync"); 344 (void)printf("\nkill -HUP 1\n"); 345#else 346 if (doreboot) { 347 execle(_PATH_REBOOT, "reboot", "-l", nosync, 348 (char *)NULL, empty_environ); 349 syslog(LOG_ERR, "shutdown: can't exec %s: %m.", _PATH_REBOOT); 350 warn(_PATH_REBOOT); 351 } 352 else if (dohalt) { 353 execle(_PATH_HALT, "halt", "-l", nosync, 354 (char *)NULL, empty_environ); 355 syslog(LOG_ERR, "shutdown: can't exec %s: %m.", _PATH_HALT); 356 warn(_PATH_HALT); 357 } 358 (void)kill(1, SIGTERM); /* to single user */ 359#endif 360 finish(0); 361} 362 363#define ATOI2(p) (p[0] - '0') * 10 + (p[1] - '0'); p += 2; 364 365void 366getoffset(timearg) 367 register char *timearg; 368{ 369 register struct tm *lt; 370 register char *p; 371 time_t now; 372 int this_year; 373 374 if (!strcasecmp(timearg, "now")) { /* now */ 375 offset = 0; 376 return; 377 } 378 379 (void)time(&now); 380 if (*timearg == '+') { /* +minutes */ 381 if (!isdigit(*++timearg)) 382 badtime(); 383 offset = atoi(timearg) * 60; 384 shuttime = now + offset; 385 return; 386 } 387 388 /* handle hh:mm by getting rid of the colon */ 389 for (p = timearg; *p; ++p) 390 if (!isascii(*p) || !isdigit(*p)) 391 if (*p == ':' && strlen(p) == 3) { 392 p[0] = p[1]; 393 p[1] = p[2]; 394 p[2] = '\0'; 395 } 396 else 397 badtime(); 398 399 unsetenv("TZ"); /* OUR timezone */ 400 lt = localtime(&now); /* current time val */ 401 402 switch(strlen(timearg)) { 403 case 10: 404 this_year = lt->tm_year; 405 lt->tm_year = ATOI2(timearg); 406 /* 407 * check if the specified year is in the next century. 408 * allow for one year of user error as many people will 409 * enter n - 1 at the start of year n. 410 */ 411 if (lt->tm_year < (this_year % 100) - 1) 412 lt->tm_year += 100; 413 /* adjust for the year 2000 and beyond */ 414 lt->tm_year += (this_year - (this_year % 100)); 415 /* FALLTHROUGH */ 416 case 8: 417 lt->tm_mon = ATOI2(timearg); 418 if (--lt->tm_mon < 0 || lt->tm_mon > 11) 419 badtime(); 420 /* FALLTHROUGH */ 421 case 6: 422 lt->tm_mday = ATOI2(timearg); 423 if (lt->tm_mday < 1 || lt->tm_mday > 31) 424 badtime(); 425 /* FALLTHROUGH */ 426 case 4: 427 lt->tm_hour = ATOI2(timearg); 428 if (lt->tm_hour < 0 || lt->tm_hour > 23) 429 badtime(); 430 lt->tm_min = ATOI2(timearg); 431 if (lt->tm_min < 0 || lt->tm_min > 59) 432 badtime(); 433 lt->tm_sec = 0; 434 if ((shuttime = mktime(lt)) == -1) 435 badtime(); 436 if ((offset = shuttime - now) < 0) 437 errx(1, "that time is already past."); 438 break; 439 default: 440 badtime(); 441 } 442} 443 444#define NOMSG "\n\nNO LOGINS: System going down at " 445void 446nolog() 447{ 448 int logfd; 449 char *ct; 450 451 (void)unlink(_PATH_NOLOGIN); /* in case linked to another file */ 452 (void)signal(SIGINT, finish); 453 (void)signal(SIGHUP, finish); 454 (void)signal(SIGQUIT, finish); 455 (void)signal(SIGTERM, finish); 456 if ((logfd = open(_PATH_NOLOGIN, O_WRONLY|O_CREAT|O_TRUNC, 457 0664)) >= 0) { 458 (void)write(logfd, NOMSG, sizeof(NOMSG) - 1); 459 ct = ctime(&shuttime); 460 (void)write(logfd, ct + 11, 5); 461 (void)write(logfd, "\n\n", 2); 462 (void)write(logfd, mbuf, strlen(mbuf)); 463 (void)close(logfd); 464 } 465} 466 467void 468finish(signo) 469 int signo; 470{ 471 (void)unlink(_PATH_NOLOGIN); 472 exit(0); 473} 474 475void 476badtime() 477{ 478 errx(1, "bad time format"); 479} 480 481void 482usage() 483{ 484 fprintf(stderr, "usage: shutdown [-hknr] shutdowntime [ message ]\n"); 485 exit(1); 486} 487