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