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