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