10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 70Sstevel@tonic-gate * with the License. 80Sstevel@tonic-gate * 90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 110Sstevel@tonic-gate * See the License for the specific language governing permissions 120Sstevel@tonic-gate * and limitations under the License. 130Sstevel@tonic-gate * 140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 190Sstevel@tonic-gate * 200Sstevel@tonic-gate * CDDL HEADER END 210Sstevel@tonic-gate */ 220Sstevel@tonic-gate /* 23*334Sdp * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 280Sstevel@tonic-gate /* All Rights Reserved */ 290Sstevel@tonic-gate 300Sstevel@tonic-gate 310Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 320Sstevel@tonic-gate 330Sstevel@tonic-gate 340Sstevel@tonic-gate #include <stdlib.h> 350Sstevel@tonic-gate #include <fcntl.h> 360Sstevel@tonic-gate #include <errno.h> 370Sstevel@tonic-gate #include <poll.h> 380Sstevel@tonic-gate #include <string.h> 390Sstevel@tonic-gate #include <signal.h> 400Sstevel@tonic-gate #include <sys/types.h> 410Sstevel@tonic-gate #include <sys/stat.h> 420Sstevel@tonic-gate #include <sys/stropts.h> 430Sstevel@tonic-gate #include <sys/resource.h> 440Sstevel@tonic-gate #include <sys/termios.h> 450Sstevel@tonic-gate #include <pwd.h> 460Sstevel@tonic-gate #include <grp.h> 470Sstevel@tonic-gate #include <unistd.h> 480Sstevel@tonic-gate #include <ulimit.h> 490Sstevel@tonic-gate 500Sstevel@tonic-gate #include "sac.h" 510Sstevel@tonic-gate #include "ttymon.h" 520Sstevel@tonic-gate #include "tmstruct.h" 530Sstevel@tonic-gate #include "tmextern.h" 540Sstevel@tonic-gate 550Sstevel@tonic-gate static int Initialized; 560Sstevel@tonic-gate 570Sstevel@tonic-gate extern int Retry; 580Sstevel@tonic-gate extern struct pollfd *Pollp; 590Sstevel@tonic-gate static void initialize(); 600Sstevel@tonic-gate static void open_all(); 610Sstevel@tonic-gate static int set_poll(); 620Sstevel@tonic-gate static int check_spawnlimit(); 630Sstevel@tonic-gate static int mod_ttydefs(); 640Sstevel@tonic-gate 650Sstevel@tonic-gate void open_device(); 660Sstevel@tonic-gate void set_softcar(); 670Sstevel@tonic-gate 680Sstevel@tonic-gate extern int check_session(); 690Sstevel@tonic-gate extern void sigalarm(); 700Sstevel@tonic-gate extern void revokedevaccess(char *, uid_t, gid_t, mode_t); 710Sstevel@tonic-gate /* can't include libdevinfo.h */ 720Sstevel@tonic-gate extern int di_devperm_logout(const char *); 730Sstevel@tonic-gate 740Sstevel@tonic-gate /* 750Sstevel@tonic-gate * ttymon - a port monitor under SAC 760Sstevel@tonic-gate * - monitor ports, set terminal modes, baud rate 770Sstevel@tonic-gate * and line discipline for the port 780Sstevel@tonic-gate * - invoke service on port if connection request received 790Sstevel@tonic-gate * - Usage: ttymon 800Sstevel@tonic-gate * ttymon -g [options] 810Sstevel@tonic-gate * Valid options are 820Sstevel@tonic-gate * -h 830Sstevel@tonic-gate * -d device 840Sstevel@tonic-gate * -l ttylabel 850Sstevel@tonic-gate * -t timeout 860Sstevel@tonic-gate * -m modules 870Sstevel@tonic-gate * -p prompt 880Sstevel@tonic-gate * 890Sstevel@tonic-gate * - ttymon without args is invoked by SAC 900Sstevel@tonic-gate * - ttymon -g is invoked by process that needs to 910Sstevel@tonic-gate * have login service on the fly 920Sstevel@tonic-gate */ 930Sstevel@tonic-gate 94*334Sdp int 95*334Sdp main(int argc, char *argv[]) 960Sstevel@tonic-gate { 970Sstevel@tonic-gate int nfds; 980Sstevel@tonic-gate extern char *lastname(); 990Sstevel@tonic-gate 1000Sstevel@tonic-gate /* 1010Sstevel@tonic-gate * Only the superuser should execute this command. 1020Sstevel@tonic-gate */ 1030Sstevel@tonic-gate if (getuid() != 0) 1040Sstevel@tonic-gate return (1); /*NOTREACHED*/ 1050Sstevel@tonic-gate 1060Sstevel@tonic-gate if ((argc > 1) || (strcmp(lastname(argv[0]), "getty") == 0)) { 1070Sstevel@tonic-gate ttymon_express(argc, argv); 1080Sstevel@tonic-gate return (1); /*NOTREACHED*/ 1090Sstevel@tonic-gate } 1100Sstevel@tonic-gate /* remember original signal mask and dispositions */ 1110Sstevel@tonic-gate (void) sigprocmask(SIG_SETMASK, NULL, &Origmask); 1120Sstevel@tonic-gate (void) sigaction(SIGINT, &Sigint, NULL); 1130Sstevel@tonic-gate (void) sigaction(SIGALRM, &Sigalrm, NULL); 1140Sstevel@tonic-gate (void) sigaction(SIGPOLL, &Sigpoll, NULL); 1150Sstevel@tonic-gate (void) sigaction(SIGCLD, &Sigcld, NULL); 1160Sstevel@tonic-gate (void) sigaction(SIGTERM, &Sigterm, NULL); 1170Sstevel@tonic-gate #ifdef DEBUG 1180Sstevel@tonic-gate (void) sigaction(SIGUSR1, &Sigusr1, NULL); 1190Sstevel@tonic-gate (void) sigaction(SIGUSR2, &Sigusr2, NULL); 1200Sstevel@tonic-gate #endif 1210Sstevel@tonic-gate initialize(); 1220Sstevel@tonic-gate 1230Sstevel@tonic-gate for (;;) { 1240Sstevel@tonic-gate nfds = set_poll(Pollp); 1250Sstevel@tonic-gate if (!Reread_flag) { 1260Sstevel@tonic-gate if (nfds > 0) 1270Sstevel@tonic-gate do_poll(Pollp, nfds); 1280Sstevel@tonic-gate else 1290Sstevel@tonic-gate (void) pause(); 1300Sstevel@tonic-gate } 1310Sstevel@tonic-gate /* 1320Sstevel@tonic-gate * READDB messages may arrive during poll or pause. 1330Sstevel@tonic-gate * So the flag needs to be checked again. 1340Sstevel@tonic-gate */ 1350Sstevel@tonic-gate if (Reread_flag) { 1360Sstevel@tonic-gate Reread_flag = FALSE; 1370Sstevel@tonic-gate re_read(); 1380Sstevel@tonic-gate } 1390Sstevel@tonic-gate while (Retry) { 1400Sstevel@tonic-gate Retry = FALSE; 1410Sstevel@tonic-gate open_all(); 1420Sstevel@tonic-gate } 1430Sstevel@tonic-gate } 1440Sstevel@tonic-gate } 1450Sstevel@tonic-gate 1460Sstevel@tonic-gate static void 1470Sstevel@tonic-gate initialize() 1480Sstevel@tonic-gate { 1490Sstevel@tonic-gate struct pmtab *tp; 1500Sstevel@tonic-gate register struct passwd *pwdp; 1510Sstevel@tonic-gate register struct group *gp; 1520Sstevel@tonic-gate struct rlimit rlimit; 1530Sstevel@tonic-gate extern struct rlimit Rlimit; 1540Sstevel@tonic-gate extern uid_t Uucp_uid; 1550Sstevel@tonic-gate extern gid_t Tty_gid; 1560Sstevel@tonic-gate 1570Sstevel@tonic-gate #ifdef DEBUG 1580Sstevel@tonic-gate extern opendebug(); 1590Sstevel@tonic-gate #endif 1600Sstevel@tonic-gate Initialized = FALSE; 1610Sstevel@tonic-gate /* 1620Sstevel@tonic-gate * get_environ() must be called first, 1630Sstevel@tonic-gate * otherwise we don't know where the log file is 1640Sstevel@tonic-gate */ 1650Sstevel@tonic-gate get_environ(); 1660Sstevel@tonic-gate openttymonlog(); 1670Sstevel@tonic-gate openpid(); 1680Sstevel@tonic-gate openpipes(); 1690Sstevel@tonic-gate setup_PCpipe(); 1700Sstevel@tonic-gate 1710Sstevel@tonic-gate log("PMTAG: %s", Tag); 1720Sstevel@tonic-gate log("Starting state: %s", 1730Sstevel@tonic-gate (State == PM_ENABLED) ? "enabled" : "disabled"); 1740Sstevel@tonic-gate 1750Sstevel@tonic-gate #ifdef DEBUG 1760Sstevel@tonic-gate opendebug(FALSE); 1770Sstevel@tonic-gate debug("***** ttymon in initialize *****"); 1780Sstevel@tonic-gate log("debug mode is \t on"); 1790Sstevel@tonic-gate #endif 1800Sstevel@tonic-gate 1810Sstevel@tonic-gate catch_signals(); 1820Sstevel@tonic-gate 1830Sstevel@tonic-gate /* register to receive SIGPOLL when data comes to pmpipe */ 1840Sstevel@tonic-gate if (ioctl(Pfd, I_SETSIG, S_INPUT) < 0) 1850Sstevel@tonic-gate fatal("I_SETSIG on pmpipe failed: %s", strerror(errno)); 1860Sstevel@tonic-gate 1870Sstevel@tonic-gate sacpoll(); /* this is needed because there may be data already */ 1880Sstevel@tonic-gate 1890Sstevel@tonic-gate Maxfiles = (int)ulimit(4, 0L); /* get max number of open files */ 1900Sstevel@tonic-gate if (Maxfiles < 0) 1910Sstevel@tonic-gate fatal("ulimit(4,0L) failed: %s", strerror(errno)); 1920Sstevel@tonic-gate 1930Sstevel@tonic-gate if (getrlimit(RLIMIT_NOFILE, &Rlimit) == -1) 1940Sstevel@tonic-gate fatal("getrlimit failed: %s", strerror(errno)); 1950Sstevel@tonic-gate 1960Sstevel@tonic-gate rlimit.rlim_cur = rlimit.rlim_max = Rlimit.rlim_max; 1970Sstevel@tonic-gate if (setrlimit(RLIMIT_NOFILE, &rlimit) == -1) 1980Sstevel@tonic-gate fatal("setrlimit failed: %s", strerror(errno)); 1990Sstevel@tonic-gate 2000Sstevel@tonic-gate Maxfiles = rlimit.rlim_cur; 2010Sstevel@tonic-gate Maxfds = Maxfiles - FILE_RESERVED; 2020Sstevel@tonic-gate 2030Sstevel@tonic-gate log("max open files = %d", Maxfiles); 2040Sstevel@tonic-gate log("max ports ttymon can monitor = %d", Maxfds); 2050Sstevel@tonic-gate 2060Sstevel@tonic-gate read_pmtab(); 2070Sstevel@tonic-gate 2080Sstevel@tonic-gate /* 2090Sstevel@tonic-gate * setup poll array 2100Sstevel@tonic-gate * - we allocate 10 extra pollfd so that 2110Sstevel@tonic-gate * we do not have to re-malloc when there is 2120Sstevel@tonic-gate * minor fluctuation in Nentries 2130Sstevel@tonic-gate */ 2140Sstevel@tonic-gate Npollfd = Nentries + 10; 2150Sstevel@tonic-gate if (Npollfd > Maxfds) 2160Sstevel@tonic-gate Npollfd = Maxfds; 2170Sstevel@tonic-gate if ((Pollp = (struct pollfd *) 2180Sstevel@tonic-gate malloc((unsigned)(Npollfd * sizeof (struct pollfd)))) 2190Sstevel@tonic-gate == (struct pollfd *)NULL) 2200Sstevel@tonic-gate fatal("malloc for Pollp failed"); 2210Sstevel@tonic-gate 2220Sstevel@tonic-gate (void) mod_ttydefs(); /* just to initialize Mtime */ 2230Sstevel@tonic-gate if (check_version(TTYDEFS_VERS, TTYDEFS) != 0) 2240Sstevel@tonic-gate fatal("check /etc/ttydefs version failed"); 2250Sstevel@tonic-gate 2260Sstevel@tonic-gate read_ttydefs(NULL, FALSE); 2270Sstevel@tonic-gate 2280Sstevel@tonic-gate /* initialize global variables, Uucp_uid & Tty_gid */ 2290Sstevel@tonic-gate if ((pwdp = getpwnam(UUCP)) != NULL) 2300Sstevel@tonic-gate Uucp_uid = pwdp->pw_uid; 2310Sstevel@tonic-gate if ((gp = getgrnam(TTY)) == NULL) 2320Sstevel@tonic-gate log("no group entry for <tty>, default is used"); 2330Sstevel@tonic-gate else 2340Sstevel@tonic-gate Tty_gid = gp->gr_gid; 2350Sstevel@tonic-gate endgrent(); 2360Sstevel@tonic-gate endpwent(); 2370Sstevel@tonic-gate #ifdef DEBUG 2380Sstevel@tonic-gate debug("Uucp_uid = %ld, Tty_gid = %ld", Uucp_uid, Tty_gid); 2390Sstevel@tonic-gate #endif 2400Sstevel@tonic-gate 2410Sstevel@tonic-gate log("Initialization Completed"); 2420Sstevel@tonic-gate 2430Sstevel@tonic-gate /* open the devices ttymon monitors */ 2440Sstevel@tonic-gate Retry = TRUE; 2450Sstevel@tonic-gate while (Retry) { 2460Sstevel@tonic-gate Retry = FALSE; 2470Sstevel@tonic-gate for (tp = PMtab; tp; tp = tp->p_next) { 2480Sstevel@tonic-gate if ((tp->p_status > 0) && (tp->p_fd == 0) && 2490Sstevel@tonic-gate (tp->p_pid == 0) && !(tp->p_ttyflags & I_FLAG) && 2500Sstevel@tonic-gate (!((State == PM_DISABLED) && 2510Sstevel@tonic-gate ((tp->p_dmsg == NULL)||(*(tp->p_dmsg) == '\0'))))) { 2520Sstevel@tonic-gate open_device(tp); 2530Sstevel@tonic-gate if (tp->p_fd > 0) 2540Sstevel@tonic-gate got_carrier(tp); 2550Sstevel@tonic-gate } 2560Sstevel@tonic-gate } 2570Sstevel@tonic-gate } 2580Sstevel@tonic-gate Initialized = TRUE; 2590Sstevel@tonic-gate } 2600Sstevel@tonic-gate 2610Sstevel@tonic-gate /* 2620Sstevel@tonic-gate * open_all - open devices in pmtab if the entry is 2630Sstevel@tonic-gate * - valid, fd = 0, and pid = 0 2640Sstevel@tonic-gate */ 2650Sstevel@tonic-gate static void 2660Sstevel@tonic-gate open_all() 2670Sstevel@tonic-gate { 2680Sstevel@tonic-gate struct pmtab *tp; 2690Sstevel@tonic-gate int check_modtime; 2700Sstevel@tonic-gate static void free_defs(); 2710Sstevel@tonic-gate sigset_t cset; 2720Sstevel@tonic-gate sigset_t tset; 2730Sstevel@tonic-gate 2740Sstevel@tonic-gate #ifdef DEBUG 2750Sstevel@tonic-gate debug("in open_all"); 2760Sstevel@tonic-gate #endif 2770Sstevel@tonic-gate check_modtime = TRUE; 2780Sstevel@tonic-gate 2790Sstevel@tonic-gate for (tp = PMtab; tp; tp = tp->p_next) { 2800Sstevel@tonic-gate if ((tp->p_status > 0) && (tp->p_fd == 0) && 2810Sstevel@tonic-gate (tp->p_pid == 0) && 2820Sstevel@tonic-gate !(tp->p_ttyflags & I_FLAG) && (!((State == PM_DISABLED) && 2830Sstevel@tonic-gate ((tp->p_dmsg == NULL)||(*(tp->p_dmsg) == '\0'))))) { 2840Sstevel@tonic-gate /* 2850Sstevel@tonic-gate * if we have not check modification time and 2860Sstevel@tonic-gate * /etc/ttydefs was modified, need to re-read it 2870Sstevel@tonic-gate */ 2880Sstevel@tonic-gate if (check_modtime && mod_ttydefs()) { 2890Sstevel@tonic-gate check_modtime = FALSE; 2900Sstevel@tonic-gate (void) sigprocmask(SIG_SETMASK, NULL, &cset); 2910Sstevel@tonic-gate tset = cset; 2920Sstevel@tonic-gate (void) sigaddset(&tset, SIGCLD); 2930Sstevel@tonic-gate (void) sigprocmask(SIG_SETMASK, &tset, NULL); 2940Sstevel@tonic-gate free_defs(); 2950Sstevel@tonic-gate #ifdef DEBUG 2960Sstevel@tonic-gate debug("/etc/ttydefs is modified, re-read it"); 2970Sstevel@tonic-gate #endif 2980Sstevel@tonic-gate read_ttydefs(NULL, FALSE); 2990Sstevel@tonic-gate (void) sigprocmask(SIG_SETMASK, &cset, NULL); 3000Sstevel@tonic-gate } 3010Sstevel@tonic-gate open_device(tp); 3020Sstevel@tonic-gate if (tp->p_fd > 0) 3030Sstevel@tonic-gate got_carrier(tp); 3040Sstevel@tonic-gate } else if (((tp->p_status == LOCKED) || 3050Sstevel@tonic-gate (tp->p_status == SESSION) || 3060Sstevel@tonic-gate (tp->p_status == UNACCESS)) && 3070Sstevel@tonic-gate (tp->p_fd > 0) && 3080Sstevel@tonic-gate (!((State == PM_DISABLED) && 3090Sstevel@tonic-gate ((tp->p_dmsg == NULL)||(*(tp->p_dmsg) == '\0'))))) { 3100Sstevel@tonic-gate if (check_modtime && mod_ttydefs()) { 3110Sstevel@tonic-gate check_modtime = FALSE; 3120Sstevel@tonic-gate (void) sigprocmask(SIG_SETMASK, NULL, &cset); 3130Sstevel@tonic-gate tset = cset; 3140Sstevel@tonic-gate (void) sigaddset(&tset, SIGCLD); 3150Sstevel@tonic-gate (void) sigprocmask(SIG_SETMASK, &tset, NULL); 3160Sstevel@tonic-gate free_defs(); 3170Sstevel@tonic-gate #ifdef DEBUG 3180Sstevel@tonic-gate debug("/etc/ttydefs is modified, re-read it"); 3190Sstevel@tonic-gate #endif 3200Sstevel@tonic-gate read_ttydefs(NULL, FALSE); 3210Sstevel@tonic-gate (void) sigprocmask(SIG_SETMASK, &cset, NULL); 3220Sstevel@tonic-gate } 3230Sstevel@tonic-gate tp->p_status = VALID; 3240Sstevel@tonic-gate open_device(tp); 3250Sstevel@tonic-gate if (tp->p_fd > 0) 3260Sstevel@tonic-gate got_carrier(tp); 3270Sstevel@tonic-gate } 3280Sstevel@tonic-gate } 3290Sstevel@tonic-gate } 3300Sstevel@tonic-gate 3310Sstevel@tonic-gate void 3320Sstevel@tonic-gate set_softcar(pmptr) 3330Sstevel@tonic-gate struct pmtab *pmptr; 3340Sstevel@tonic-gate { 3350Sstevel@tonic-gate 3360Sstevel@tonic-gate int fd, val = 0; 3370Sstevel@tonic-gate 3380Sstevel@tonic-gate #ifdef DEBUG 3390Sstevel@tonic-gate debug("in set_softcar"); 3400Sstevel@tonic-gate #endif 3410Sstevel@tonic-gate /* 3420Sstevel@tonic-gate * If soft carrier is not set one way or 3430Sstevel@tonic-gate * the other, leave it alone. 3440Sstevel@tonic-gate */ 3450Sstevel@tonic-gate if (*pmptr->p_softcar == '\0') 3460Sstevel@tonic-gate return; 3470Sstevel@tonic-gate 3480Sstevel@tonic-gate if (*pmptr->p_softcar == 'y') 3490Sstevel@tonic-gate val = 1; 3500Sstevel@tonic-gate 3510Sstevel@tonic-gate if ((fd = open(pmptr->p_device, O_RDONLY|O_NONBLOCK|O_NOCTTY)) < 0) { 3520Sstevel@tonic-gate log("open (%s) failed: %s", pmptr->p_device, strerror(errno)); 3530Sstevel@tonic-gate return; 3540Sstevel@tonic-gate } 3550Sstevel@tonic-gate 3560Sstevel@tonic-gate if (ioctl(fd, TIOCSSOFTCAR, &val) < 0) 3570Sstevel@tonic-gate log("set soft-carrier (%s) failed: %s", pmptr->p_device, 3580Sstevel@tonic-gate strerror(errno)); 3590Sstevel@tonic-gate 3600Sstevel@tonic-gate close(fd); 3610Sstevel@tonic-gate } 3620Sstevel@tonic-gate 3630Sstevel@tonic-gate 3640Sstevel@tonic-gate /* 3650Sstevel@tonic-gate * open_device(pmptr) - open the device 3660Sstevel@tonic-gate * - check device lock 3670Sstevel@tonic-gate * - change owner of device 3680Sstevel@tonic-gate * - push line disciplines 3690Sstevel@tonic-gate * - set termio 3700Sstevel@tonic-gate */ 3710Sstevel@tonic-gate 3720Sstevel@tonic-gate void 3730Sstevel@tonic-gate open_device(pmptr) 3740Sstevel@tonic-gate struct pmtab *pmptr; 3750Sstevel@tonic-gate { 3760Sstevel@tonic-gate int fd, tmpfd; 3770Sstevel@tonic-gate struct sigaction sigact; 3780Sstevel@tonic-gate 3790Sstevel@tonic-gate #ifdef DEBUG 3800Sstevel@tonic-gate debug("in open_device"); 3810Sstevel@tonic-gate #endif 3820Sstevel@tonic-gate 3830Sstevel@tonic-gate if (pmptr->p_status == GETTY) { 3840Sstevel@tonic-gate revokedevaccess(pmptr->p_device, 0, 0, 0); 3850Sstevel@tonic-gate 3860Sstevel@tonic-gate if ((fd = open(pmptr->p_device, O_RDWR)) == -1) 3870Sstevel@tonic-gate fatal("open (%s) failed: %s", pmptr->p_device, 3880Sstevel@tonic-gate strerror(errno)); 3890Sstevel@tonic-gate 3900Sstevel@tonic-gate } else { 3910Sstevel@tonic-gate if (check_spawnlimit(pmptr) == -1) { 3920Sstevel@tonic-gate pmptr->p_status = NOTVALID; 3930Sstevel@tonic-gate log("service <%s> is respawning too rapidly", 3940Sstevel@tonic-gate pmptr->p_tag); 3950Sstevel@tonic-gate return; 3960Sstevel@tonic-gate } 3970Sstevel@tonic-gate if (pmptr->p_fd > 0) { /* file already open */ 3980Sstevel@tonic-gate fd = pmptr->p_fd; 3990Sstevel@tonic-gate pmptr->p_fd = 0; 4000Sstevel@tonic-gate } else if ((fd = open(pmptr->p_device, O_RDWR|O_NONBLOCK)) 4010Sstevel@tonic-gate == -1) { 4020Sstevel@tonic-gate log("open (%s) failed: %s", pmptr->p_device, 4030Sstevel@tonic-gate strerror(errno)); 4040Sstevel@tonic-gate if ((errno == ENODEV) || (errno == EBUSY)) { 4050Sstevel@tonic-gate pmptr->p_status = UNACCESS; 4060Sstevel@tonic-gate Nlocked++; 4070Sstevel@tonic-gate if (Nlocked == 1) { 4080Sstevel@tonic-gate sigact.sa_flags = 0; 4090Sstevel@tonic-gate sigact.sa_handler = sigalarm; 4100Sstevel@tonic-gate (void) sigemptyset(&sigact.sa_mask); 4110Sstevel@tonic-gate (void) sigaction(SIGALRM, &sigact, NULL); 4120Sstevel@tonic-gate (void) alarm(ALARMTIME); 4130Sstevel@tonic-gate } 4140Sstevel@tonic-gate } else 4150Sstevel@tonic-gate Retry = TRUE; 4160Sstevel@tonic-gate return; 4170Sstevel@tonic-gate } 4180Sstevel@tonic-gate /* set close-on-exec flag */ 4190Sstevel@tonic-gate if (fcntl(fd, F_SETFD, 1) == -1) 4200Sstevel@tonic-gate fatal("F_SETFD fcntl failed: %s", strerror(errno)); 4210Sstevel@tonic-gate 4220Sstevel@tonic-gate if (tm_checklock(fd) != 0) { 4230Sstevel@tonic-gate pmptr->p_status = LOCKED; 4240Sstevel@tonic-gate (void) close(fd); 4250Sstevel@tonic-gate Nlocked++; 4260Sstevel@tonic-gate if (Nlocked == 1) { 4270Sstevel@tonic-gate sigact.sa_flags = 0; 4280Sstevel@tonic-gate sigact.sa_handler = sigalarm; 4290Sstevel@tonic-gate (void) sigemptyset(&sigact.sa_mask); 4300Sstevel@tonic-gate (void) sigaction(SIGALRM, &sigact, NULL); 4310Sstevel@tonic-gate (void) alarm(ALARMTIME); 4320Sstevel@tonic-gate } 4330Sstevel@tonic-gate return; 4340Sstevel@tonic-gate } 4350Sstevel@tonic-gate if (check_session(fd) != 0) { 4360Sstevel@tonic-gate if ((Initialized) && (pmptr->p_inservice != SESSION)) { 4370Sstevel@tonic-gate log("Warning -- active session exists on <%s>", 4380Sstevel@tonic-gate pmptr->p_device); 4390Sstevel@tonic-gate } else { 4400Sstevel@tonic-gate /* 4410Sstevel@tonic-gate * this may happen if a service is running 4420Sstevel@tonic-gate * and ttymon dies and is restarted, 4430Sstevel@tonic-gate * or another process is running on the 4440Sstevel@tonic-gate * port. 4450Sstevel@tonic-gate */ 4460Sstevel@tonic-gate pmptr->p_status = SESSION; 4470Sstevel@tonic-gate pmptr->p_inservice = 0; 4480Sstevel@tonic-gate (void) close(fd); 4490Sstevel@tonic-gate Nlocked++; 4500Sstevel@tonic-gate if (Nlocked == 1) { 4510Sstevel@tonic-gate sigact.sa_flags = 0; 4520Sstevel@tonic-gate sigact.sa_handler = sigalarm; 4530Sstevel@tonic-gate (void) sigemptyset(&sigact.sa_mask); 4540Sstevel@tonic-gate (void) sigaction(SIGALRM, &sigact, 4550Sstevel@tonic-gate NULL); 4560Sstevel@tonic-gate (void) alarm(ALARMTIME); 4570Sstevel@tonic-gate } 4580Sstevel@tonic-gate return; 4590Sstevel@tonic-gate } 4600Sstevel@tonic-gate } 4610Sstevel@tonic-gate pmptr->p_inservice = 0; 4620Sstevel@tonic-gate } 4630Sstevel@tonic-gate 4640Sstevel@tonic-gate if (pmptr->p_ttyflags & H_FLAG) { 4650Sstevel@tonic-gate /* drop DTR */ 4660Sstevel@tonic-gate (void) hang_up_line(fd); 4670Sstevel@tonic-gate /* 4680Sstevel@tonic-gate * After hang_up_line, the stream is in STRHUP state. 4690Sstevel@tonic-gate * We need to do another open to reinitialize streams 4700Sstevel@tonic-gate * then we can close one fd 4710Sstevel@tonic-gate */ 4720Sstevel@tonic-gate if ((tmpfd = open(pmptr->p_device, O_RDWR|O_NONBLOCK)) == -1) { 4730Sstevel@tonic-gate log("open (%s) failed: %s", pmptr->p_device, 4740Sstevel@tonic-gate strerror(errno)); 4750Sstevel@tonic-gate Retry = TRUE; 4760Sstevel@tonic-gate (void) close(fd); 4770Sstevel@tonic-gate return; 4780Sstevel@tonic-gate } 4790Sstevel@tonic-gate (void) close(tmpfd); 4800Sstevel@tonic-gate } 4810Sstevel@tonic-gate 4820Sstevel@tonic-gate #ifdef DEBUG 4830Sstevel@tonic-gate debug("open_device (%s), fd = %d", pmptr->p_device, fd); 4840Sstevel@tonic-gate #endif 4850Sstevel@tonic-gate 4860Sstevel@tonic-gate /* Change ownership of the tty line to root/uucp and */ 4870Sstevel@tonic-gate /* set protections to only allow root/uucp to read the line. */ 4880Sstevel@tonic-gate 4890Sstevel@tonic-gate if (pmptr->p_ttyflags & (B_FLAG|C_FLAG)) 4900Sstevel@tonic-gate (void) fchown(fd, Uucp_uid, Tty_gid); 4910Sstevel@tonic-gate else 4920Sstevel@tonic-gate (void) fchown(fd, ROOTUID, Tty_gid); 4930Sstevel@tonic-gate (void) fchmod(fd, 0620); 4940Sstevel@tonic-gate 4950Sstevel@tonic-gate if ((pmptr->p_modules != NULL)&&(*(pmptr->p_modules) != '\0')) { 4960Sstevel@tonic-gate if (push_linedisc(fd, pmptr->p_modules, pmptr->p_device) 4970Sstevel@tonic-gate == -1) { 4980Sstevel@tonic-gate Retry = TRUE; 4990Sstevel@tonic-gate (void) close(fd); 5000Sstevel@tonic-gate return; 5010Sstevel@tonic-gate } 5020Sstevel@tonic-gate } 5030Sstevel@tonic-gate 5040Sstevel@tonic-gate if (initial_termio(fd, pmptr) == -1) { 5050Sstevel@tonic-gate Retry = TRUE; 5060Sstevel@tonic-gate (void) close(fd); 5070Sstevel@tonic-gate return; 5080Sstevel@tonic-gate } 5090Sstevel@tonic-gate 5100Sstevel@tonic-gate di_devperm_logout((const char *)pmptr->p_device); 5110Sstevel@tonic-gate pmptr->p_fd = fd; 5120Sstevel@tonic-gate } 5130Sstevel@tonic-gate 5140Sstevel@tonic-gate /* 5150Sstevel@tonic-gate * set_poll(fdp) - put all fd's in a pollfd array 5160Sstevel@tonic-gate * - set poll event to POLLIN and POLLMSG 5170Sstevel@tonic-gate * - return number of fd to be polled 5180Sstevel@tonic-gate */ 5190Sstevel@tonic-gate 5200Sstevel@tonic-gate static int 5210Sstevel@tonic-gate set_poll(fdp) 5220Sstevel@tonic-gate struct pollfd *fdp; 5230Sstevel@tonic-gate { 5240Sstevel@tonic-gate struct pmtab *tp; 5250Sstevel@tonic-gate int nfd = 0; 5260Sstevel@tonic-gate 5270Sstevel@tonic-gate for (tp = PMtab; tp; tp = tp->p_next) { 5280Sstevel@tonic-gate if (tp->p_fd > 0) { 5290Sstevel@tonic-gate fdp->fd = tp->p_fd; 5300Sstevel@tonic-gate fdp->events = POLLIN; 5310Sstevel@tonic-gate fdp++; 5320Sstevel@tonic-gate nfd++; 5330Sstevel@tonic-gate } 5340Sstevel@tonic-gate } 5350Sstevel@tonic-gate return (nfd); 5360Sstevel@tonic-gate } 5370Sstevel@tonic-gate 5380Sstevel@tonic-gate /* 5390Sstevel@tonic-gate * check_spawnlimit - return 0 if spawnlimit is not reached 5400Sstevel@tonic-gate * - otherwise return -1 5410Sstevel@tonic-gate */ 5420Sstevel@tonic-gate static int 5430Sstevel@tonic-gate check_spawnlimit(pmptr) 5440Sstevel@tonic-gate struct pmtab *pmptr; 5450Sstevel@tonic-gate { 5460Sstevel@tonic-gate time_t now; 5470Sstevel@tonic-gate 5480Sstevel@tonic-gate (void) time(&now); 5490Sstevel@tonic-gate if (pmptr->p_time == 0L) 5500Sstevel@tonic-gate pmptr->p_time = now; 5510Sstevel@tonic-gate if (pmptr->p_respawn >= SPAWN_LIMIT) { 5520Sstevel@tonic-gate if ((now - pmptr->p_time) < SPAWN_INTERVAL) { 5530Sstevel@tonic-gate pmptr->p_time = now; 5540Sstevel@tonic-gate pmptr->p_respawn = 0; 5550Sstevel@tonic-gate return (-1); 5560Sstevel@tonic-gate } 5570Sstevel@tonic-gate pmptr->p_time = now; 5580Sstevel@tonic-gate pmptr->p_respawn = 0; 5590Sstevel@tonic-gate } 5600Sstevel@tonic-gate pmptr->p_respawn++; 5610Sstevel@tonic-gate return (0); 5620Sstevel@tonic-gate } 5630Sstevel@tonic-gate 5640Sstevel@tonic-gate /* 5650Sstevel@tonic-gate * mod_ttydefs - to check if /etc/ttydefs has been modified 5660Sstevel@tonic-gate * - return TRUE if file modified 5670Sstevel@tonic-gate * - otherwise, return FALSE 5680Sstevel@tonic-gate */ 5690Sstevel@tonic-gate static int 5700Sstevel@tonic-gate mod_ttydefs() 5710Sstevel@tonic-gate { 5720Sstevel@tonic-gate struct stat statbuf; 5730Sstevel@tonic-gate extern long Mtime; 5740Sstevel@tonic-gate if (stat(TTYDEFS, &statbuf) == -1) { 5750Sstevel@tonic-gate /* if stat failed, don't bother reread ttydefs */ 5760Sstevel@tonic-gate return (FALSE); 5770Sstevel@tonic-gate } 5780Sstevel@tonic-gate if ((long)statbuf.st_mtime != Mtime) { 5790Sstevel@tonic-gate Mtime = (long)statbuf.st_mtime; 5800Sstevel@tonic-gate return (TRUE); 5810Sstevel@tonic-gate } 5820Sstevel@tonic-gate return (FALSE); 5830Sstevel@tonic-gate } 5840Sstevel@tonic-gate 5850Sstevel@tonic-gate /* 5860Sstevel@tonic-gate * free_defs - free the Gdef table 5870Sstevel@tonic-gate */ 5880Sstevel@tonic-gate static void 5890Sstevel@tonic-gate free_defs() 5900Sstevel@tonic-gate { 5910Sstevel@tonic-gate int i; 5920Sstevel@tonic-gate struct Gdef *tp; 5930Sstevel@tonic-gate tp = &Gdef[0]; 5940Sstevel@tonic-gate for (i = 0; i < Ndefs; i++, tp++) { 5950Sstevel@tonic-gate free(tp->g_id); 5960Sstevel@tonic-gate free(tp->g_iflags); 5970Sstevel@tonic-gate free(tp->g_fflags); 5980Sstevel@tonic-gate free(tp->g_nextid); 5990Sstevel@tonic-gate tp->g_id = NULL; 6000Sstevel@tonic-gate tp->g_iflags = NULL; 6010Sstevel@tonic-gate tp->g_fflags = NULL; 6020Sstevel@tonic-gate tp->g_nextid = NULL; 6030Sstevel@tonic-gate } 6040Sstevel@tonic-gate Ndefs = 0; 6050Sstevel@tonic-gate } 6060Sstevel@tonic-gate 6070Sstevel@tonic-gate /* 6080Sstevel@tonic-gate * struct Gdef *get_speed(ttylabel) 6090Sstevel@tonic-gate * - search "/etc/ttydefs" for speed and term. specification 6100Sstevel@tonic-gate * using "ttylabel". If "ttylabel" is NULL, default 6110Sstevel@tonic-gate * to DEFAULT 6120Sstevel@tonic-gate * arg: ttylabel - label/id of speed settings. 6130Sstevel@tonic-gate */ 6140Sstevel@tonic-gate 6150Sstevel@tonic-gate struct Gdef * 6160Sstevel@tonic-gate get_speed(char *ttylabel) 6170Sstevel@tonic-gate { 6180Sstevel@tonic-gate register struct Gdef *sp; 6190Sstevel@tonic-gate extern struct Gdef DEFAULT; 6200Sstevel@tonic-gate 6210Sstevel@tonic-gate if ((ttylabel != NULL) && (*ttylabel != '\0')) { 6220Sstevel@tonic-gate if ((sp = find_def(ttylabel)) == NULL) { 6230Sstevel@tonic-gate log("unable to find <%s> in \"%s\"", ttylabel, TTYDEFS); 6240Sstevel@tonic-gate sp = &DEFAULT; /* use default */ 6250Sstevel@tonic-gate } 6260Sstevel@tonic-gate } else sp = &DEFAULT; /* use default */ 6270Sstevel@tonic-gate return (sp); 6280Sstevel@tonic-gate } 6290Sstevel@tonic-gate 6300Sstevel@tonic-gate /* 6310Sstevel@tonic-gate * setup_PCpipe() - setup the pipe between Parent and Children 6320Sstevel@tonic-gate * - the pipe is used for a tmchild to send its 6330Sstevel@tonic-gate * pid to inform ttymon that it is about to 6340Sstevel@tonic-gate * invoke service 6350Sstevel@tonic-gate * - the pipe also serves as a mean for tmchild 6360Sstevel@tonic-gate * to detect failure of ttymon 6370Sstevel@tonic-gate */ 6380Sstevel@tonic-gate void 6390Sstevel@tonic-gate setup_PCpipe() 6400Sstevel@tonic-gate { 6410Sstevel@tonic-gate int flag = 0; 6420Sstevel@tonic-gate 6430Sstevel@tonic-gate if (pipe(PCpipe) == -1) 6440Sstevel@tonic-gate fatal("pipe() failed: %s", strerror(errno)); 6450Sstevel@tonic-gate 6460Sstevel@tonic-gate /* set close-on-exec flag */ 6470Sstevel@tonic-gate if (fcntl(PCpipe[0], F_SETFD, 1) == -1) 6480Sstevel@tonic-gate fatal("F_SETFD fcntl failed: %s", strerror(errno)); 6490Sstevel@tonic-gate 6500Sstevel@tonic-gate if (fcntl(PCpipe[1], F_SETFD, 1) == -1) 6510Sstevel@tonic-gate fatal("F_SETFD fcntl failed: %s", strerror(errno)); 6520Sstevel@tonic-gate 6530Sstevel@tonic-gate /* set O_NONBLOCK flag */ 6540Sstevel@tonic-gate if (fcntl(PCpipe[0], F_GETFL, flag) == -1) 6550Sstevel@tonic-gate fatal("F_GETFL failed: %s", strerror(errno)); 6560Sstevel@tonic-gate 6570Sstevel@tonic-gate flag |= O_NONBLOCK; 6580Sstevel@tonic-gate if (fcntl(PCpipe[0], F_SETFL, flag) == -1) 6590Sstevel@tonic-gate fatal("F_SETFL failed: %s", strerror(errno)); 6600Sstevel@tonic-gate 6610Sstevel@tonic-gate /* set message discard mode */ 6620Sstevel@tonic-gate if (ioctl(PCpipe[0], I_SRDOPT, RMSGD) == -1) 6630Sstevel@tonic-gate fatal("I_SRDOPT RMSGD failed: %s", strerror(errno)); 6640Sstevel@tonic-gate 6650Sstevel@tonic-gate /* register to receive SIGPOLL when data come */ 6660Sstevel@tonic-gate if (ioctl(PCpipe[0], I_SETSIG, S_INPUT) == -1) 6670Sstevel@tonic-gate fatal("I_SETSIG S_INPUT failed: %s", strerror(errno)); 6680Sstevel@tonic-gate 6690Sstevel@tonic-gate #ifdef DEBUG 6700Sstevel@tonic-gate log("PCpipe[0]\t = %d", PCpipe[0]); 6710Sstevel@tonic-gate log("PCpipe[1]\t = %d", PCpipe[1]); 6720Sstevel@tonic-gate #endif 6730Sstevel@tonic-gate } 674