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