1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* 23*0Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 24*0Sstevel@tonic-gate * Use is subject to license terms. 25*0Sstevel@tonic-gate */ 26*0Sstevel@tonic-gate 27*0Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 28*0Sstevel@tonic-gate /* All Rights Reserved */ 29*0Sstevel@tonic-gate 30*0Sstevel@tonic-gate /* 31*0Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988 32*0Sstevel@tonic-gate * The Regents of the University of California 33*0Sstevel@tonic-gate * All Rights Reserved 34*0Sstevel@tonic-gate * 35*0Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from 36*0Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its 37*0Sstevel@tonic-gate * contributors. 38*0Sstevel@tonic-gate */ 39*0Sstevel@tonic-gate 40*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 41*0Sstevel@tonic-gate 42*0Sstevel@tonic-gate #include <stdio.h> 43*0Sstevel@tonic-gate #include <ctype.h> 44*0Sstevel@tonic-gate #include <setjmp.h> 45*0Sstevel@tonic-gate #include <utmpx.h> 46*0Sstevel@tonic-gate #include <pwd.h> 47*0Sstevel@tonic-gate #include <time.h> 48*0Sstevel@tonic-gate #include <sys/time.h> 49*0Sstevel@tonic-gate #include <sys/resource.h> 50*0Sstevel@tonic-gate #include <sys/param.h> 51*0Sstevel@tonic-gate #include <sys/types.h> 52*0Sstevel@tonic-gate #include <sys/errno.h> 53*0Sstevel@tonic-gate #include <rpc/rpc.h> 54*0Sstevel@tonic-gate #include <rpc/pmap_clnt.h> 55*0Sstevel@tonic-gate #include <rpcsvc/mount.h> 56*0Sstevel@tonic-gate #include <rpcsvc/rwall.h> 57*0Sstevel@tonic-gate #include <sys/socket.h> 58*0Sstevel@tonic-gate #include <netinet/in.h> 59*0Sstevel@tonic-gate #include <netdb.h> 60*0Sstevel@tonic-gate #include <locale.h> 61*0Sstevel@tonic-gate #include <sys/syslog.h> 62*0Sstevel@tonic-gate #include <zone.h> 63*0Sstevel@tonic-gate #include <signal.h> 64*0Sstevel@tonic-gate 65*0Sstevel@tonic-gate /* 66*0Sstevel@tonic-gate * /usr/etc/shutdown when [messages] 67*0Sstevel@tonic-gate * 68*0Sstevel@tonic-gate * allow super users to tell users and remind users 69*0Sstevel@tonic-gate * of iminent shutdown of unix 70*0Sstevel@tonic-gate * and shut it down automatically 71*0Sstevel@tonic-gate * and even reboot or halt the machine if they desire 72*0Sstevel@tonic-gate */ 73*0Sstevel@tonic-gate 74*0Sstevel@tonic-gate #define EPATH "PATH=/usr/ucb:/usr/bin:/usr/sbin:" 75*0Sstevel@tonic-gate #define REBOOT "/usr/sbin/reboot" 76*0Sstevel@tonic-gate #define HALT "/usr/sbin/halt" 77*0Sstevel@tonic-gate #define MAXINTS 20 78*0Sstevel@tonic-gate #define HOURS *3600 79*0Sstevel@tonic-gate #define MINUTES *60 80*0Sstevel@tonic-gate #define SECONDS 81*0Sstevel@tonic-gate #define NLOG 600 /* no of bytes possible for message */ 82*0Sstevel@tonic-gate #define NOLOGTIME 5 MINUTES 83*0Sstevel@tonic-gate #define IGNOREUSER "sleeper" 84*0Sstevel@tonic-gate 85*0Sstevel@tonic-gate struct hostlist { 86*0Sstevel@tonic-gate char *host; 87*0Sstevel@tonic-gate struct hostlist *nxt; 88*0Sstevel@tonic-gate } *hostlist; 89*0Sstevel@tonic-gate 90*0Sstevel@tonic-gate char hostname[MAXHOSTNAMELEN]; 91*0Sstevel@tonic-gate char mbuf[BUFSIZ]; 92*0Sstevel@tonic-gate 93*0Sstevel@tonic-gate void timeout(); 94*0Sstevel@tonic-gate time_t getsdt(); 95*0Sstevel@tonic-gate 96*0Sstevel@tonic-gate extern char *malloc(); 97*0Sstevel@tonic-gate 98*0Sstevel@tonic-gate extern char *ctime(); 99*0Sstevel@tonic-gate extern struct tm *localtime(); 100*0Sstevel@tonic-gate 101*0Sstevel@tonic-gate extern char *strcpy(); 102*0Sstevel@tonic-gate extern char *strncat(); 103*0Sstevel@tonic-gate extern off_t lseek(); 104*0Sstevel@tonic-gate 105*0Sstevel@tonic-gate struct utmpx *utmpx; 106*0Sstevel@tonic-gate 107*0Sstevel@tonic-gate int sint; 108*0Sstevel@tonic-gate int stogo; 109*0Sstevel@tonic-gate char tpath[] = "/dev/"; 110*0Sstevel@tonic-gate int nlflag = 1; /* nolog yet to be done */ 111*0Sstevel@tonic-gate int killflg = 1; 112*0Sstevel@tonic-gate int doreboot = 0; 113*0Sstevel@tonic-gate int halt = 0; 114*0Sstevel@tonic-gate int fast = 0; 115*0Sstevel@tonic-gate char *nosync = NULL; 116*0Sstevel@tonic-gate char nosyncflag[] = "-n"; 117*0Sstevel@tonic-gate char term[sizeof tpath + sizeof (utmpx->ut_line)]; 118*0Sstevel@tonic-gate char tbuf[BUFSIZ]; 119*0Sstevel@tonic-gate char nolog1[] = "\n\nNO LOGINS: System going down at %5.5s\n\n"; 120*0Sstevel@tonic-gate char mesg[NLOG+1]; 121*0Sstevel@tonic-gate #ifdef DEBUG 122*0Sstevel@tonic-gate char fastboot[] = "fastboot"; 123*0Sstevel@tonic-gate #else 124*0Sstevel@tonic-gate char fastboot[] = "/fastboot"; 125*0Sstevel@tonic-gate #endif 126*0Sstevel@tonic-gate char nologin[] = "/etc/nologin"; 127*0Sstevel@tonic-gate time_t nowtime; 128*0Sstevel@tonic-gate jmp_buf alarmbuf; 129*0Sstevel@tonic-gate 130*0Sstevel@tonic-gate struct interval { 131*0Sstevel@tonic-gate int stogo; 132*0Sstevel@tonic-gate int sint; 133*0Sstevel@tonic-gate } interval[] = { 134*0Sstevel@tonic-gate 4 HOURS, 1 HOURS, 135*0Sstevel@tonic-gate 2 HOURS, 30 MINUTES, 136*0Sstevel@tonic-gate 1 HOURS, 15 MINUTES, 137*0Sstevel@tonic-gate 30 MINUTES, 10 MINUTES, 138*0Sstevel@tonic-gate 15 MINUTES, 5 MINUTES, 139*0Sstevel@tonic-gate 10 MINUTES, 5 MINUTES, 140*0Sstevel@tonic-gate 5 MINUTES, 3 MINUTES, 141*0Sstevel@tonic-gate 2 MINUTES, 1 MINUTES, 142*0Sstevel@tonic-gate 1 MINUTES, 30 SECONDS, 143*0Sstevel@tonic-gate 0 SECONDS, 0 SECONDS 144*0Sstevel@tonic-gate }; 145*0Sstevel@tonic-gate 146*0Sstevel@tonic-gate char *msg1 = "shutdown: '%c' - unknown flag\n"; 147*0Sstevel@tonic-gate char *msg2 = "Usage: shutdown [ -krhfn ] shutdowntime [ message ]\n"; 148*0Sstevel@tonic-gate char *msg3 = "Usage: shutdown [ -krhfn ] shutdowntime [ message ]"; 149*0Sstevel@tonic-gate char *msg4 = "Usage: shutdown [ -krhfn ] shutdowntime [ message ]\n"; 150*0Sstevel@tonic-gate char *msg5 = "Usage: shutdown [ -krhfn ] shutdowntime [ message ]"; 151*0Sstevel@tonic-gate char *msg6 = "\n\007\007System shutdown time has arrived\007\007\n"; 152*0Sstevel@tonic-gate char *msg7 = "but you'll have to do it yourself\n"; 153*0Sstevel@tonic-gate char *msg8 = "but you'll have to do it yourself"; 154*0Sstevel@tonic-gate char *msg9 = "-l (without fsck's)\n"; 155*0Sstevel@tonic-gate char *msg10 = "-l %s\n"; 156*0Sstevel@tonic-gate char *msg11 = " (without fsck's)\n"; 157*0Sstevel@tonic-gate char *msg12 = "That must be tomorrow\nCan't you wait till then?\n"; 158*0Sstevel@tonic-gate char *msg13 = "That must be tomorrow"; 159*0Sstevel@tonic-gate char *msg14 = "Can't you wait till then?"; 160*0Sstevel@tonic-gate char *msg15 = "\007\007\t*** %sSystem shutdown message from %s@%s ***\r\n\n"; 161*0Sstevel@tonic-gate char *msg16 = "System going down at %5.5s\r\n"; 162*0Sstevel@tonic-gate char *msg17 = "System going down in %d minute%s\r\n"; 163*0Sstevel@tonic-gate char *msg18 = "System going down in %d second%s\r\n"; 164*0Sstevel@tonic-gate char *msg19 = "System going down IMMEDIATELY\r\n"; 165*0Sstevel@tonic-gate char *msg20 = "Can't get PID for init\n"; 166*0Sstevel@tonic-gate 167*0Sstevel@tonic-gate char *shutter, *getlogin(); 168*0Sstevel@tonic-gate 169*0Sstevel@tonic-gate 170*0Sstevel@tonic-gate pid_t 171*0Sstevel@tonic-gate get_initpid() 172*0Sstevel@tonic-gate { 173*0Sstevel@tonic-gate pid_t init_pid; 174*0Sstevel@tonic-gate 175*0Sstevel@tonic-gate if (zone_getattr(getzoneid(), ZONE_ATTR_INITPID, &init_pid, 176*0Sstevel@tonic-gate sizeof (init_pid)) != sizeof (init_pid)) { 177*0Sstevel@tonic-gate (void) fprintf(stderr, gettext(msg20)); 178*0Sstevel@tonic-gate exit(1); 179*0Sstevel@tonic-gate } 180*0Sstevel@tonic-gate return (init_pid); 181*0Sstevel@tonic-gate } 182*0Sstevel@tonic-gate 183*0Sstevel@tonic-gate int 184*0Sstevel@tonic-gate main(int argc, char **argv) 185*0Sstevel@tonic-gate { 186*0Sstevel@tonic-gate int i; 187*0Sstevel@tonic-gate char *f; 188*0Sstevel@tonic-gate char *ts; 189*0Sstevel@tonic-gate time_t sdt; 190*0Sstevel@tonic-gate int h, m; 191*0Sstevel@tonic-gate int first; 192*0Sstevel@tonic-gate void finish_sig(); 193*0Sstevel@tonic-gate FILE *termf; 194*0Sstevel@tonic-gate struct passwd *pw, *getpwuid(); 195*0Sstevel@tonic-gate extern char *strcat(); 196*0Sstevel@tonic-gate extern uid_t geteuid(); 197*0Sstevel@tonic-gate struct hostlist *hl; 198*0Sstevel@tonic-gate char *shutdown_program; 199*0Sstevel@tonic-gate char *shutdown_action; 200*0Sstevel@tonic-gate int fd; 201*0Sstevel@tonic-gate 202*0Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 203*0Sstevel@tonic-gate 204*0Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) 205*0Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" 206*0Sstevel@tonic-gate #endif 207*0Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 208*0Sstevel@tonic-gate 209*0Sstevel@tonic-gate audit_shutdown_setup(argc, argv); 210*0Sstevel@tonic-gate 211*0Sstevel@tonic-gate shutter = getlogin(); 212*0Sstevel@tonic-gate if (shutter == 0 && (pw = getpwuid(getuid()))) 213*0Sstevel@tonic-gate shutter = pw->pw_name; 214*0Sstevel@tonic-gate if (shutter == 0) 215*0Sstevel@tonic-gate shutter = "???"; 216*0Sstevel@tonic-gate (void) gethostname(hostname, sizeof (hostname)); 217*0Sstevel@tonic-gate openlog("shutdown", 0, LOG_AUTH); 218*0Sstevel@tonic-gate argc--, argv++; 219*0Sstevel@tonic-gate while (argc > 0 && (f = argv[0], *f++ == '-')) { 220*0Sstevel@tonic-gate while (i = *f++) { 221*0Sstevel@tonic-gate switch (i) { 222*0Sstevel@tonic-gate case 'k': 223*0Sstevel@tonic-gate killflg = 0; 224*0Sstevel@tonic-gate continue; 225*0Sstevel@tonic-gate case 'n': 226*0Sstevel@tonic-gate nosync = nosyncflag; 227*0Sstevel@tonic-gate continue; 228*0Sstevel@tonic-gate case 'f': 229*0Sstevel@tonic-gate fast = 1; 230*0Sstevel@tonic-gate continue; 231*0Sstevel@tonic-gate case 'r': 232*0Sstevel@tonic-gate doreboot = 1; 233*0Sstevel@tonic-gate continue; 234*0Sstevel@tonic-gate case 'h': 235*0Sstevel@tonic-gate halt = 1; 236*0Sstevel@tonic-gate continue; 237*0Sstevel@tonic-gate default: 238*0Sstevel@tonic-gate (void) fprintf(stderr, gettext(msg1), 239*0Sstevel@tonic-gate i); 240*0Sstevel@tonic-gate (void) fprintf(stderr, gettext(msg2)); 241*0Sstevel@tonic-gate finish(gettext(msg3), "", 1); 242*0Sstevel@tonic-gate } 243*0Sstevel@tonic-gate } 244*0Sstevel@tonic-gate argc--, argv++; 245*0Sstevel@tonic-gate } 246*0Sstevel@tonic-gate if (argc < 1) { 247*0Sstevel@tonic-gate (void) fprintf(stderr, gettext(msg4)); 248*0Sstevel@tonic-gate finish(gettext(msg5), "", 1); 249*0Sstevel@tonic-gate } 250*0Sstevel@tonic-gate if (doreboot && halt) { 251*0Sstevel@tonic-gate (void) fprintf(stderr, 252*0Sstevel@tonic-gate gettext("shutdown: Incompatible switches '-r' & '-h'\n")); 253*0Sstevel@tonic-gate finish(gettext("shutdown: Incompatible switches '-r' & '-h'"), 254*0Sstevel@tonic-gate "", 1); 255*0Sstevel@tonic-gate } 256*0Sstevel@tonic-gate if (fast && (nosync == nosyncflag)) { 257*0Sstevel@tonic-gate (void) fprintf(stderr, 258*0Sstevel@tonic-gate gettext("shutdown: Incompatible switches '-f' & '-n'\n")); 259*0Sstevel@tonic-gate finish(gettext("shutdown: Incompatible switches '-f' & '-n'"), 260*0Sstevel@tonic-gate "", 1); 261*0Sstevel@tonic-gate } 262*0Sstevel@tonic-gate if (geteuid()) { 263*0Sstevel@tonic-gate (void) fprintf(stderr, gettext("shutdown: NOT super-user\n")); 264*0Sstevel@tonic-gate finish(gettext("shutdown: NOT super-user"), "", 1); 265*0Sstevel@tonic-gate } 266*0Sstevel@tonic-gate gethostlist(); 267*0Sstevel@tonic-gate nowtime = time((time_t *)NULL); 268*0Sstevel@tonic-gate sdt = getsdt(argv[0]); 269*0Sstevel@tonic-gate argc--, argv++; 270*0Sstevel@tonic-gate mesg[0] = '\0'; 271*0Sstevel@tonic-gate i = 0; 272*0Sstevel@tonic-gate while (argc-- > 0) { 273*0Sstevel@tonic-gate if (i + strlen(*argv) > NLOG) 274*0Sstevel@tonic-gate break; /* no more room for the message */ 275*0Sstevel@tonic-gate i += strlen(*argv) + 1; 276*0Sstevel@tonic-gate (void) strcat(mesg, *argv++); 277*0Sstevel@tonic-gate (void) strcat(mesg, " "); 278*0Sstevel@tonic-gate } 279*0Sstevel@tonic-gate if (i != 0) 280*0Sstevel@tonic-gate mesg[i - 1] = '\0'; /* remove trailing blank */ 281*0Sstevel@tonic-gate m = ((stogo = sdt - nowtime) + 30)/60; 282*0Sstevel@tonic-gate h = m/60; 283*0Sstevel@tonic-gate m %= 60; 284*0Sstevel@tonic-gate ts = ctime(&sdt); 285*0Sstevel@tonic-gate (void) printf(gettext("Shutdown at %5.5s (in "), ts+11); 286*0Sstevel@tonic-gate if (h > 0) 287*0Sstevel@tonic-gate (void) printf("%d hour%s ", h, h != 1 ? "s" : ""); 288*0Sstevel@tonic-gate (void) printf("%d minute%s) ", m, m != 1 ? "s" : ""); 289*0Sstevel@tonic-gate #ifndef DEBUG 290*0Sstevel@tonic-gate (void) signal(SIGHUP, SIG_IGN); 291*0Sstevel@tonic-gate (void) signal(SIGQUIT, SIG_IGN); 292*0Sstevel@tonic-gate (void) signal(SIGINT, SIG_IGN); 293*0Sstevel@tonic-gate #endif 294*0Sstevel@tonic-gate (void) signal(SIGTTOU, SIG_IGN); 295*0Sstevel@tonic-gate (void) signal(SIGINT, finish_sig); 296*0Sstevel@tonic-gate (void) signal(SIGALRM, timeout); 297*0Sstevel@tonic-gate (void) setpriority(PRIO_PROCESS, 0, PRIO_MIN); 298*0Sstevel@tonic-gate (void) fflush(stdout); 299*0Sstevel@tonic-gate #ifndef DEBUG 300*0Sstevel@tonic-gate if (i = fork()) { 301*0Sstevel@tonic-gate (void) printf(gettext("[pid %d]\n"), i); 302*0Sstevel@tonic-gate exit(0); 303*0Sstevel@tonic-gate } 304*0Sstevel@tonic-gate #else 305*0Sstevel@tonic-gate (void) putc('\n', stdout); 306*0Sstevel@tonic-gate #endif 307*0Sstevel@tonic-gate sint = 1 HOURS; 308*0Sstevel@tonic-gate f = ""; 309*0Sstevel@tonic-gate first = 1; 310*0Sstevel@tonic-gate if (doreboot) { 311*0Sstevel@tonic-gate shutdown_program = REBOOT; 312*0Sstevel@tonic-gate shutdown_action = "reboot"; 313*0Sstevel@tonic-gate } else if (halt) { 314*0Sstevel@tonic-gate shutdown_program = HALT; 315*0Sstevel@tonic-gate shutdown_action = "halt"; 316*0Sstevel@tonic-gate } else { 317*0Sstevel@tonic-gate shutdown_program = NULL; 318*0Sstevel@tonic-gate shutdown_action = "shutdown"; 319*0Sstevel@tonic-gate } 320*0Sstevel@tonic-gate for (;;) { 321*0Sstevel@tonic-gate for (i = 0; stogo <= interval[i].stogo && interval[i].sint; i++) 322*0Sstevel@tonic-gate sint = interval[i].sint; 323*0Sstevel@tonic-gate if (stogo > 0 && (stogo-sint) < interval[i].stogo) 324*0Sstevel@tonic-gate sint = stogo - interval[i].stogo; 325*0Sstevel@tonic-gate if (stogo <= NOLOGTIME && nlflag) { 326*0Sstevel@tonic-gate nlflag = 0; 327*0Sstevel@tonic-gate nolog(sdt); 328*0Sstevel@tonic-gate } 329*0Sstevel@tonic-gate if (sint >= stogo || sint == 0) 330*0Sstevel@tonic-gate f = "FINAL "; 331*0Sstevel@tonic-gate nowtime = time((time_t *)NULL); 332*0Sstevel@tonic-gate 333*0Sstevel@tonic-gate setutxent(); 334*0Sstevel@tonic-gate 335*0Sstevel@tonic-gate while ((utmpx = getutxent()) != NULL) { 336*0Sstevel@tonic-gate if (utmpx->ut_name[0] && 337*0Sstevel@tonic-gate strncmp(utmpx->ut_name, IGNOREUSER, 338*0Sstevel@tonic-gate sizeof (utmpx->ut_name))) { 339*0Sstevel@tonic-gate /* 340*0Sstevel@tonic-gate * don't write to pty's unless they're rlogin sessions 341*0Sstevel@tonic-gate */ 342*0Sstevel@tonic-gate if (utmpx->ut_type != USER_PROCESS && 343*0Sstevel@tonic-gate utmpx->ut_user[0] != '\0') 344*0Sstevel@tonic-gate continue; 345*0Sstevel@tonic-gate 346*0Sstevel@tonic-gate if (setjmp(alarmbuf)) 347*0Sstevel@tonic-gate continue; 348*0Sstevel@tonic-gate (void) strcpy(term, tpath); 349*0Sstevel@tonic-gate (void) strncat(term, utmpx->ut_line, 350*0Sstevel@tonic-gate sizeof (utmpx->ut_line)); 351*0Sstevel@tonic-gate (void) alarm(5); 352*0Sstevel@tonic-gate 353*0Sstevel@tonic-gate /* check if device is really a tty */ 354*0Sstevel@tonic-gate if ((fd = open(term, O_WRONLY|O_NOCTTY)) == -1) { 355*0Sstevel@tonic-gate fprintf(stderr, gettext("Cannot open %s.\n"), 356*0Sstevel@tonic-gate term); 357*0Sstevel@tonic-gate (void) alarm(0); 358*0Sstevel@tonic-gate continue; 359*0Sstevel@tonic-gate } else { 360*0Sstevel@tonic-gate if (!isatty(fd)) { 361*0Sstevel@tonic-gate fprintf(stderr, 362*0Sstevel@tonic-gate gettext("%.*s in utmpx is not a tty\n"), 363*0Sstevel@tonic-gate sizeof (utmpx->ut_line), utmpx->ut_line); 364*0Sstevel@tonic-gate syslog(LOG_CRIT, "%.*s in utmpx is not " 365*0Sstevel@tonic-gate "a tty\n", sizeof (utmpx->ut_line), 366*0Sstevel@tonic-gate utmpx->ut_line); 367*0Sstevel@tonic-gate close(fd); 368*0Sstevel@tonic-gate (void) alarm(0); 369*0Sstevel@tonic-gate continue; 370*0Sstevel@tonic-gate } 371*0Sstevel@tonic-gate } 372*0Sstevel@tonic-gate close(fd); 373*0Sstevel@tonic-gate #ifdef DEBUG 374*0Sstevel@tonic-gate if ((termf = stdout) != NULL) 375*0Sstevel@tonic-gate #else 376*0Sstevel@tonic-gate if ((termf = fopen(term, "w")) != NULL) 377*0Sstevel@tonic-gate #endif 378*0Sstevel@tonic-gate { 379*0Sstevel@tonic-gate (void) alarm(0); 380*0Sstevel@tonic-gate setbuf(termf, tbuf); 381*0Sstevel@tonic-gate (void) fprintf(termf, "\n\r\n"); 382*0Sstevel@tonic-gate warn(termf, sdt, nowtime, f, first); 383*0Sstevel@tonic-gate (void) alarm(5); 384*0Sstevel@tonic-gate #ifdef DEBUG 385*0Sstevel@tonic-gate (void) fflush(termf); 386*0Sstevel@tonic-gate #else 387*0Sstevel@tonic-gate (void) fclose(termf); 388*0Sstevel@tonic-gate #endif 389*0Sstevel@tonic-gate (void) alarm(0); 390*0Sstevel@tonic-gate } 391*0Sstevel@tonic-gate } 392*0Sstevel@tonic-gate } /* while */ 393*0Sstevel@tonic-gate 394*0Sstevel@tonic-gate endutxent(); 395*0Sstevel@tonic-gate 396*0Sstevel@tonic-gate for (hl = hostlist; hl != NULL; hl = hl->nxt) 397*0Sstevel@tonic-gate rwarn(hl->host, sdt, nowtime, f, first); 398*0Sstevel@tonic-gate if (stogo <= 0) { 399*0Sstevel@tonic-gate (void) printf(gettext(msg6)); 400*0Sstevel@tonic-gate if (*mesg) 401*0Sstevel@tonic-gate syslog(LOG_CRIT, "%s by %s: %s", 402*0Sstevel@tonic-gate shutdown_action, shutter, mesg); 403*0Sstevel@tonic-gate else 404*0Sstevel@tonic-gate syslog(LOG_CRIT, "%s by %s", 405*0Sstevel@tonic-gate shutdown_action, shutter); 406*0Sstevel@tonic-gate sleep(2); 407*0Sstevel@tonic-gate (void) unlink(nologin); 408*0Sstevel@tonic-gate if (!killflg) { 409*0Sstevel@tonic-gate (void) printf(gettext(msg7)); 410*0Sstevel@tonic-gate finish(gettext(msg8), "", 0); 411*0Sstevel@tonic-gate } 412*0Sstevel@tonic-gate if (fast) 413*0Sstevel@tonic-gate doitfast(); 414*0Sstevel@tonic-gate #ifndef DEBUG 415*0Sstevel@tonic-gate (void) putenv(EPATH); 416*0Sstevel@tonic-gate if (shutdown_program != NULL) { 417*0Sstevel@tonic-gate audit_shutdown_success(); 418*0Sstevel@tonic-gate execlp(shutdown_program, shutdown_program, 419*0Sstevel@tonic-gate "-l", nosync, (char *)0); 420*0Sstevel@tonic-gate } else { 421*0Sstevel@tonic-gate if (geteuid() == 0) { 422*0Sstevel@tonic-gate audit_shutdown_success(); 423*0Sstevel@tonic-gate sleep(5); 424*0Sstevel@tonic-gate } 425*0Sstevel@tonic-gate if (getzoneid() == GLOBAL_ZONEID) { 426*0Sstevel@tonic-gate (void) system( 427*0Sstevel@tonic-gate "/sbin/bootadm -a update_all"); 428*0Sstevel@tonic-gate } 429*0Sstevel@tonic-gate 430*0Sstevel@tonic-gate (void) kill(get_initpid(), SIGINT); /* sync */ 431*0Sstevel@tonic-gate (void) kill(get_initpid(), SIGINT); /* sync */ 432*0Sstevel@tonic-gate sleep(20); 433*0Sstevel@tonic-gate } 434*0Sstevel@tonic-gate #else 435*0Sstevel@tonic-gate if (shutdown_program) { 436*0Sstevel@tonic-gate (void) printf("%s ", shutdown_program); 437*0Sstevel@tonic-gate if (fast) 438*0Sstevel@tonic-gate (void) printf(gettext(msg9)); 439*0Sstevel@tonic-gate else if (nosync != NULL) 440*0Sstevel@tonic-gate (void) printf(gettext(msg10), nosync); 441*0Sstevel@tonic-gate else 442*0Sstevel@tonic-gate (void) printf(gettext("-l\n")); 443*0Sstevel@tonic-gate } else { 444*0Sstevel@tonic-gate (void) printf("/sbin/bootadm -a update_all"); 445*0Sstevel@tonic-gate (void) printf("kill -INT 1"); 446*0Sstevel@tonic-gate if (fast) 447*0Sstevel@tonic-gate (void) printf(gettext(msg11)); 448*0Sstevel@tonic-gate else 449*0Sstevel@tonic-gate (void) printf("\n"); 450*0Sstevel@tonic-gate } 451*0Sstevel@tonic-gate #endif 452*0Sstevel@tonic-gate finish("", "", 0); 453*0Sstevel@tonic-gate } 454*0Sstevel@tonic-gate stogo = sdt - time((time_t *)NULL); 455*0Sstevel@tonic-gate if (stogo > 0 && sint > 0) 456*0Sstevel@tonic-gate sleep((unsigned)(sint < stogo ? sint : stogo)); 457*0Sstevel@tonic-gate stogo -= sint; 458*0Sstevel@tonic-gate first = 0; 459*0Sstevel@tonic-gate } 460*0Sstevel@tonic-gate /* NOTREACHED */ 461*0Sstevel@tonic-gate } 462*0Sstevel@tonic-gate 463*0Sstevel@tonic-gate time_t 464*0Sstevel@tonic-gate getsdt(s) 465*0Sstevel@tonic-gate char *s; 466*0Sstevel@tonic-gate { 467*0Sstevel@tonic-gate time_t t, t1, tim; 468*0Sstevel@tonic-gate char c; 469*0Sstevel@tonic-gate struct tm *lt; 470*0Sstevel@tonic-gate int c_count; 471*0Sstevel@tonic-gate 472*0Sstevel@tonic-gate if (strcmp(s, "now") == 0) 473*0Sstevel@tonic-gate return (nowtime); 474*0Sstevel@tonic-gate if (*s == '+') { 475*0Sstevel@tonic-gate ++s; 476*0Sstevel@tonic-gate t = 0; 477*0Sstevel@tonic-gate for (c_count = 1; ; c_count++) { 478*0Sstevel@tonic-gate c = *s++; 479*0Sstevel@tonic-gate if (!isdigit(c)) { 480*0Sstevel@tonic-gate if (c_count == 1) { 481*0Sstevel@tonic-gate goto badform; 482*0Sstevel@tonic-gate } else { 483*0Sstevel@tonic-gate break; 484*0Sstevel@tonic-gate } 485*0Sstevel@tonic-gate } 486*0Sstevel@tonic-gate t = t * 10 + c - '0'; 487*0Sstevel@tonic-gate } 488*0Sstevel@tonic-gate if (t <= 0) 489*0Sstevel@tonic-gate t = 5; 490*0Sstevel@tonic-gate t *= 60; 491*0Sstevel@tonic-gate tim = time((time_t *)NULL) + t; 492*0Sstevel@tonic-gate return (tim); 493*0Sstevel@tonic-gate } 494*0Sstevel@tonic-gate t = 0; 495*0Sstevel@tonic-gate while (strlen(s) > 2 && isdigit(*s)) 496*0Sstevel@tonic-gate t = t * 10 + *s++ - '0'; 497*0Sstevel@tonic-gate if (*s == ':') 498*0Sstevel@tonic-gate s++; 499*0Sstevel@tonic-gate if (t > 23) 500*0Sstevel@tonic-gate goto badform; 501*0Sstevel@tonic-gate tim = t*60; 502*0Sstevel@tonic-gate t = 0; 503*0Sstevel@tonic-gate while (isdigit(*s)) 504*0Sstevel@tonic-gate t = t * 10 + *s++ - '0'; 505*0Sstevel@tonic-gate if (t > 59) 506*0Sstevel@tonic-gate goto badform; 507*0Sstevel@tonic-gate tim += t; 508*0Sstevel@tonic-gate tim *= 60; 509*0Sstevel@tonic-gate t1 = time((time_t *)NULL); 510*0Sstevel@tonic-gate lt = localtime(&t1); 511*0Sstevel@tonic-gate t = lt->tm_sec + lt->tm_min*60 + lt->tm_hour*3600; 512*0Sstevel@tonic-gate if (tim < t || tim >= (24*3600)) { 513*0Sstevel@tonic-gate /* before now or after midnight */ 514*0Sstevel@tonic-gate (void) printf(gettext(msg12)); 515*0Sstevel@tonic-gate finish(gettext(msg13), gettext(msg14), 0); 516*0Sstevel@tonic-gate } 517*0Sstevel@tonic-gate return (t1 + tim - t); 518*0Sstevel@tonic-gate badform: 519*0Sstevel@tonic-gate (void) printf(gettext("Bad time format\n")); 520*0Sstevel@tonic-gate finish(gettext("Bad time format"), "", 0); 521*0Sstevel@tonic-gate /*NOTREACHED*/ 522*0Sstevel@tonic-gate } 523*0Sstevel@tonic-gate 524*0Sstevel@tonic-gate warn(termf, sdt, now, type, first) 525*0Sstevel@tonic-gate FILE *termf; 526*0Sstevel@tonic-gate time_t sdt, now; 527*0Sstevel@tonic-gate char *type; 528*0Sstevel@tonic-gate int first; 529*0Sstevel@tonic-gate { 530*0Sstevel@tonic-gate char *ts; 531*0Sstevel@tonic-gate time_t delay = sdt - now; 532*0Sstevel@tonic-gate 533*0Sstevel@tonic-gate if (delay > 8) 534*0Sstevel@tonic-gate while (delay % 5) 535*0Sstevel@tonic-gate delay++; 536*0Sstevel@tonic-gate 537*0Sstevel@tonic-gate (void) fprintf(termf, gettext(msg15), type, shutter, hostname); 538*0Sstevel@tonic-gate 539*0Sstevel@tonic-gate ts = ctime(&sdt); 540*0Sstevel@tonic-gate if (delay > 10 MINUTES) 541*0Sstevel@tonic-gate (void) fprintf(termf, gettext(msg16), ts+11); 542*0Sstevel@tonic-gate else if (delay > 95 SECONDS) { 543*0Sstevel@tonic-gate (void) fprintf(termf, gettext(msg17), (delay+30)/60, 544*0Sstevel@tonic-gate (delay+30)/60 != 1 ? "s" : ""); 545*0Sstevel@tonic-gate } else if (delay > 0) { 546*0Sstevel@tonic-gate (void) fprintf(termf, gettext(msg18), delay, 547*0Sstevel@tonic-gate delay != 1 ? "s" : ""); 548*0Sstevel@tonic-gate } else 549*0Sstevel@tonic-gate (void) fprintf(termf, gettext(msg19)); 550*0Sstevel@tonic-gate 551*0Sstevel@tonic-gate if (first || sdt - now > 1 MINUTES) { 552*0Sstevel@tonic-gate if (*mesg) 553*0Sstevel@tonic-gate (void) fprintf(termf, "\t...%s\r\n", mesg); 554*0Sstevel@tonic-gate } 555*0Sstevel@tonic-gate } 556*0Sstevel@tonic-gate 557*0Sstevel@tonic-gate doitfast() 558*0Sstevel@tonic-gate { 559*0Sstevel@tonic-gate FILE *fastd; 560*0Sstevel@tonic-gate 561*0Sstevel@tonic-gate if ((fastd = fopen(fastboot, "w")) != NULL) { 562*0Sstevel@tonic-gate (void) putc('\n', fastd); 563*0Sstevel@tonic-gate (void) fclose(fastd); 564*0Sstevel@tonic-gate } 565*0Sstevel@tonic-gate } 566*0Sstevel@tonic-gate 567*0Sstevel@tonic-gate rwarn(host, sdt, now, type, first) 568*0Sstevel@tonic-gate char *host; 569*0Sstevel@tonic-gate time_t sdt, now; 570*0Sstevel@tonic-gate char *type; 571*0Sstevel@tonic-gate int first; 572*0Sstevel@tonic-gate { 573*0Sstevel@tonic-gate char *ts; 574*0Sstevel@tonic-gate time_t delay = sdt - now; 575*0Sstevel@tonic-gate char *bufp; 576*0Sstevel@tonic-gate 577*0Sstevel@tonic-gate if (delay > 8) 578*0Sstevel@tonic-gate while (delay % 5) 579*0Sstevel@tonic-gate delay++; 580*0Sstevel@tonic-gate 581*0Sstevel@tonic-gate (void) sprintf(mbuf, 582*0Sstevel@tonic-gate "\007\007\t*** %sShutdown message for %s from %s@%s ***\r\n\n", 583*0Sstevel@tonic-gate type, hostname, shutter, hostname); 584*0Sstevel@tonic-gate ts = ctime(&sdt); 585*0Sstevel@tonic-gate bufp = mbuf + strlen(mbuf); 586*0Sstevel@tonic-gate if (delay > 10 MINUTES) { 587*0Sstevel@tonic-gate (void) sprintf(bufp, "%s going down at %5.5s\r\n", hostname, 588*0Sstevel@tonic-gate ts+11); 589*0Sstevel@tonic-gate } else if (delay > 95 SECONDS) { 590*0Sstevel@tonic-gate (void) sprintf(bufp, "%s going down in %d minute%s\r\n", 591*0Sstevel@tonic-gate hostname, (delay+30)/60, (delay+30)/60 != 1 ? "s" : ""); 592*0Sstevel@tonic-gate } else if (delay > 0) { 593*0Sstevel@tonic-gate (void) sprintf(bufp, "%s going down in %d second%s\r\n", 594*0Sstevel@tonic-gate hostname, delay, delay != 1 ? "s" : ""); 595*0Sstevel@tonic-gate } else { 596*0Sstevel@tonic-gate (void) sprintf(bufp, "%s going down IMMEDIATELY\r\n", 597*0Sstevel@tonic-gate hostname); 598*0Sstevel@tonic-gate } 599*0Sstevel@tonic-gate bufp = mbuf + strlen(mbuf); 600*0Sstevel@tonic-gate if (first || sdt - now > 1 MINUTES) { 601*0Sstevel@tonic-gate if (*mesg) 602*0Sstevel@tonic-gate (void) sprintf(bufp, "\t...%s\r\n", mesg); 603*0Sstevel@tonic-gate } 604*0Sstevel@tonic-gate rprintf(host, mbuf); 605*0Sstevel@tonic-gate } 606*0Sstevel@tonic-gate 607*0Sstevel@tonic-gate rprintf(host, bufp) 608*0Sstevel@tonic-gate char *host, *bufp; 609*0Sstevel@tonic-gate { 610*0Sstevel@tonic-gate int err; 611*0Sstevel@tonic-gate 612*0Sstevel@tonic-gate #ifdef DEBUG 613*0Sstevel@tonic-gate (void) fprintf(stderr, gettext("about to call %s\n"), host); 614*0Sstevel@tonic-gate #endif 615*0Sstevel@tonic-gate if (err = callrpcfast(host, (rpcprog_t)WALLPROG, (rpcvers_t)WALLVERS, 616*0Sstevel@tonic-gate (rpcproc_t)WALLPROC_WALL, xdr_dirpath, (char *)&bufp, xdr_void, 617*0Sstevel@tonic-gate (char *)NULL)) { 618*0Sstevel@tonic-gate #ifdef DEBUG 619*0Sstevel@tonic-gate (void) fprintf(stderr, gettext("couldn't make rpc call: ")); 620*0Sstevel@tonic-gate clnt_perrno(err); 621*0Sstevel@tonic-gate (void) fprintf(stderr, "\n"); 622*0Sstevel@tonic-gate #endif 623*0Sstevel@tonic-gate } 624*0Sstevel@tonic-gate } 625*0Sstevel@tonic-gate 626*0Sstevel@tonic-gate nolog(sdt) 627*0Sstevel@tonic-gate time_t sdt; 628*0Sstevel@tonic-gate { 629*0Sstevel@tonic-gate FILE *nologf; 630*0Sstevel@tonic-gate 631*0Sstevel@tonic-gate (void) unlink(nologin); /* in case linked to std file */ 632*0Sstevel@tonic-gate if ((nologf = fopen(nologin, "w")) != NULL) { 633*0Sstevel@tonic-gate (void) fprintf(nologf, nolog1, (ctime(&sdt)) + 11); 634*0Sstevel@tonic-gate if (*mesg) 635*0Sstevel@tonic-gate (void) fprintf(nologf, "\t%s\n", mesg); 636*0Sstevel@tonic-gate (void) fclose(nologf); 637*0Sstevel@tonic-gate } 638*0Sstevel@tonic-gate } 639*0Sstevel@tonic-gate 640*0Sstevel@tonic-gate void 641*0Sstevel@tonic-gate finish_sig() 642*0Sstevel@tonic-gate { 643*0Sstevel@tonic-gate finish("SIGINT", "", 1); 644*0Sstevel@tonic-gate } 645*0Sstevel@tonic-gate 646*0Sstevel@tonic-gate finish(s1, s2, exitcode) 647*0Sstevel@tonic-gate char *s1; 648*0Sstevel@tonic-gate char *s2; 649*0Sstevel@tonic-gate { 650*0Sstevel@tonic-gate (void) signal(SIGINT, SIG_IGN); 651*0Sstevel@tonic-gate exit(exitcode); 652*0Sstevel@tonic-gate } 653*0Sstevel@tonic-gate 654*0Sstevel@tonic-gate void 655*0Sstevel@tonic-gate timeout() 656*0Sstevel@tonic-gate { 657*0Sstevel@tonic-gate (void) signal(SIGALRM, (void(*)())timeout); 658*0Sstevel@tonic-gate longjmp(alarmbuf, 1); 659*0Sstevel@tonic-gate } 660*0Sstevel@tonic-gate 661*0Sstevel@tonic-gate gethostlist() 662*0Sstevel@tonic-gate { 663*0Sstevel@tonic-gate int s; 664*0Sstevel@tonic-gate struct mountbody *ml; 665*0Sstevel@tonic-gate struct hostlist *hl; 666*0Sstevel@tonic-gate struct sockaddr_in addr; 667*0Sstevel@tonic-gate CLIENT *cl; 668*0Sstevel@tonic-gate static struct timeval TIMEOUT = { 25, 0 }; 669*0Sstevel@tonic-gate 670*0Sstevel@tonic-gate /* 671*0Sstevel@tonic-gate * check for portmapper 672*0Sstevel@tonic-gate */ 673*0Sstevel@tonic-gate get_myaddress(&addr); 674*0Sstevel@tonic-gate s = socket(AF_INET, SOCK_STREAM, 0); 675*0Sstevel@tonic-gate if (s < 0) 676*0Sstevel@tonic-gate return; 677*0Sstevel@tonic-gate if (connect(s, (struct sockaddr *)&addr, sizeof (addr)) < 0) 678*0Sstevel@tonic-gate return; 679*0Sstevel@tonic-gate (void) close(s); 680*0Sstevel@tonic-gate 681*0Sstevel@tonic-gate /* 682*0Sstevel@tonic-gate * First try tcp, then drop back to udp if 683*0Sstevel@tonic-gate * tcp is unavailable (an old version of mountd perhaps) 684*0Sstevel@tonic-gate * Using tcp is preferred because it can handle 685*0Sstevel@tonic-gate * arbitrarily long export lists. 686*0Sstevel@tonic-gate */ 687*0Sstevel@tonic-gate cl = clnt_create(hostname, (ulong_t)MOUNTPROG, (ulong_t)MOUNTVERS, 688*0Sstevel@tonic-gate "tcp"); 689*0Sstevel@tonic-gate if (cl == NULL) { 690*0Sstevel@tonic-gate cl = clnt_create(hostname, (ulong_t)MOUNTPROG, 691*0Sstevel@tonic-gate (ulong_t)MOUNTVERS, "udp"); 692*0Sstevel@tonic-gate if (cl == NULL) { 693*0Sstevel@tonic-gate if (rpc_createerr.cf_stat != RPC_PROGNOTREGISTERED) { 694*0Sstevel@tonic-gate clnt_pcreateerror("shutdown warning"); 695*0Sstevel@tonic-gate } 696*0Sstevel@tonic-gate return; 697*0Sstevel@tonic-gate } 698*0Sstevel@tonic-gate } 699*0Sstevel@tonic-gate 700*0Sstevel@tonic-gate ml = NULL; 701*0Sstevel@tonic-gate if (clnt_call(cl, MOUNTPROC_DUMP, 702*0Sstevel@tonic-gate xdr_void, 0, xdr_mountlist, (char *)&ml, TIMEOUT) != RPC_SUCCESS) { 703*0Sstevel@tonic-gate clnt_perror(cl, "shutdown warning"); 704*0Sstevel@tonic-gate return; 705*0Sstevel@tonic-gate } 706*0Sstevel@tonic-gate for (; ml != NULL; ml = ml->ml_next) { 707*0Sstevel@tonic-gate for (hl = hostlist; hl != NULL; hl = hl->nxt) 708*0Sstevel@tonic-gate if (strcmp(ml->ml_hostname, hl->host) == 0) 709*0Sstevel@tonic-gate goto again; 710*0Sstevel@tonic-gate hl = (struct hostlist *)malloc(sizeof (struct hostlist)); 711*0Sstevel@tonic-gate hl->host = ml->ml_hostname; 712*0Sstevel@tonic-gate hl->nxt = hostlist; 713*0Sstevel@tonic-gate hostlist = hl; 714*0Sstevel@tonic-gate again:; 715*0Sstevel@tonic-gate } 716*0Sstevel@tonic-gate } 717*0Sstevel@tonic-gate 718*0Sstevel@tonic-gate /* 719*0Sstevel@tonic-gate * Don't want to wait for usual portmapper timeout you get with 720*0Sstevel@tonic-gate * callrpc or clnt_call, so use rmtcall instead. Use timeout 721*0Sstevel@tonic-gate * of 8 secs, based on the per try timeout of 3 secs for rmtcall 722*0Sstevel@tonic-gate */ 723*0Sstevel@tonic-gate callrpcfast(host, prognum, versnum, procnum, inproc, in, outproc, out) 724*0Sstevel@tonic-gate char *host; 725*0Sstevel@tonic-gate rpcprog_t prognum; 726*0Sstevel@tonic-gate rpcvers_t versnum; 727*0Sstevel@tonic-gate rpcproc_t procnum; 728*0Sstevel@tonic-gate xdrproc_t inproc, outproc; 729*0Sstevel@tonic-gate char *in, *out; 730*0Sstevel@tonic-gate { 731*0Sstevel@tonic-gate struct sockaddr_in server_addr; 732*0Sstevel@tonic-gate struct hostent *hp; 733*0Sstevel@tonic-gate struct timeval rpctimeout; 734*0Sstevel@tonic-gate rpcport_t port; 735*0Sstevel@tonic-gate 736*0Sstevel@tonic-gate if ((hp = gethostbyname(host)) == NULL) 737*0Sstevel@tonic-gate return ((int)RPC_UNKNOWNHOST); 738*0Sstevel@tonic-gate bcopy(hp->h_addr, (char *)&server_addr.sin_addr, hp->h_length); 739*0Sstevel@tonic-gate server_addr.sin_family = AF_INET; 740*0Sstevel@tonic-gate server_addr.sin_port = 0; 741*0Sstevel@tonic-gate rpctimeout.tv_sec = 8; 742*0Sstevel@tonic-gate rpctimeout.tv_usec = 0; 743*0Sstevel@tonic-gate return ((int)pmap_rmtcall(&server_addr, prognum, versnum, procnum, 744*0Sstevel@tonic-gate inproc, in, outproc, out, rpctimeout, &port)); 745*0Sstevel@tonic-gate } 746