xref: /onnv-gate/usr/src/cmd/ttymon/ttymon.c (revision 9694:78fafb281255)
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 /*
229300SNobutomo.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
main(int argc,char * argv[])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)
1009300SNobutomo.Nakano@Sun.COM 		return (1);
1019300SNobutomo.Nakano@Sun.COM 
1029300SNobutomo.Nakano@Sun.COM 	/* remember original signal mask and dispositions */
1039300SNobutomo.Nakano@Sun.COM 	(void) sigprocmask(SIG_SETMASK, NULL, &Origmask);
1049300SNobutomo.Nakano@Sun.COM 	(void) sigaction(SIGINT, NULL, &Sigint);
1059300SNobutomo.Nakano@Sun.COM 	(void) sigaction(SIGALRM, NULL, &Sigalrm);
1069300SNobutomo.Nakano@Sun.COM 	(void) sigaction(SIGPOLL, NULL, &Sigpoll);
1079300SNobutomo.Nakano@Sun.COM 	(void) sigaction(SIGQUIT, NULL, &Sigquit);
1089300SNobutomo.Nakano@Sun.COM 	(void) sigaction(SIGCLD, NULL, &Sigcld);
1099300SNobutomo.Nakano@Sun.COM 	(void) sigaction(SIGTERM, NULL, &Sigterm);
1109300SNobutomo.Nakano@Sun.COM #ifdef	DEBUG
1119300SNobutomo.Nakano@Sun.COM 	(void) sigaction(SIGUSR1, NULL, &Sigusr1);
1129300SNobutomo.Nakano@Sun.COM 	(void) sigaction(SIGUSR2, NULL, &Sigusr2);
1139300SNobutomo.Nakano@Sun.COM #endif
1149300SNobutomo.Nakano@Sun.COM 
1159300SNobutomo.Nakano@Sun.COM 	/*
1169300SNobutomo.Nakano@Sun.COM 	 * SIGQUIT needs to be ignored. Otherwise, hitting ^\ from
1179300SNobutomo.Nakano@Sun.COM 	 * console kills ttymon.
1189300SNobutomo.Nakano@Sun.COM 	 */
1199300SNobutomo.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 	}
1259300SNobutomo.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
initialize()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 
268*9694SScott.Rotondo@Sun.COM static	void	free_defs();
269*9694SScott.Rotondo@Sun.COM 
2700Sstevel@tonic-gate /*
2710Sstevel@tonic-gate  *	open_all - open devices in pmtab if the entry is
2720Sstevel@tonic-gate  *	         - valid, fd = 0, and pid = 0
2730Sstevel@tonic-gate  */
2740Sstevel@tonic-gate static void
open_all()2750Sstevel@tonic-gate open_all()
2760Sstevel@tonic-gate {
2770Sstevel@tonic-gate 	struct	pmtab	*tp;
2780Sstevel@tonic-gate 	int	check_modtime;
2790Sstevel@tonic-gate 	sigset_t cset;
2800Sstevel@tonic-gate 	sigset_t tset;
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate #ifdef	DEBUG
2830Sstevel@tonic-gate 	debug("in open_all");
2840Sstevel@tonic-gate #endif
2850Sstevel@tonic-gate 	check_modtime = TRUE;
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate 	for (tp = PMtab; tp; tp = tp->p_next) {
2880Sstevel@tonic-gate 		if ((tp->p_status > 0) && (tp->p_fd == 0) &&
2890Sstevel@tonic-gate 		    (tp->p_pid == 0) &&
2900Sstevel@tonic-gate 		    !(tp->p_ttyflags & I_FLAG) && (!((State == PM_DISABLED) &&
2910Sstevel@tonic-gate 		    ((tp->p_dmsg == NULL)||(*(tp->p_dmsg) == '\0'))))) {
2920Sstevel@tonic-gate 			/*
2930Sstevel@tonic-gate 			 * if we have not check modification time and
2940Sstevel@tonic-gate 			 * /etc/ttydefs was modified, need to re-read it
2950Sstevel@tonic-gate 			 */
2960Sstevel@tonic-gate 			if (check_modtime && mod_ttydefs()) {
2970Sstevel@tonic-gate 				check_modtime = FALSE;
2980Sstevel@tonic-gate 				(void) sigprocmask(SIG_SETMASK, NULL, &cset);
2990Sstevel@tonic-gate 				tset = cset;
3000Sstevel@tonic-gate 				(void) sigaddset(&tset, SIGCLD);
3010Sstevel@tonic-gate 				(void) sigprocmask(SIG_SETMASK, &tset, NULL);
3020Sstevel@tonic-gate 				free_defs();
3030Sstevel@tonic-gate #ifdef	DEBUG
3040Sstevel@tonic-gate 				debug("/etc/ttydefs is modified, re-read it");
3050Sstevel@tonic-gate #endif
3060Sstevel@tonic-gate 				read_ttydefs(NULL, FALSE);
3070Sstevel@tonic-gate 				(void) sigprocmask(SIG_SETMASK, &cset, NULL);
3080Sstevel@tonic-gate 			}
3090Sstevel@tonic-gate 			open_device(tp);
3100Sstevel@tonic-gate 			if (tp->p_fd > 0)
3110Sstevel@tonic-gate 				got_carrier(tp);
3120Sstevel@tonic-gate 		} else if (((tp->p_status == LOCKED) ||
3130Sstevel@tonic-gate 		    (tp->p_status == SESSION) ||
3140Sstevel@tonic-gate 		    (tp->p_status == UNACCESS)) &&
3150Sstevel@tonic-gate 		    (tp->p_fd > 0) &&
3160Sstevel@tonic-gate 		    (!((State == PM_DISABLED) &&
3170Sstevel@tonic-gate 		    ((tp->p_dmsg == NULL)||(*(tp->p_dmsg) == '\0'))))) {
3180Sstevel@tonic-gate 			if (check_modtime && mod_ttydefs()) {
3190Sstevel@tonic-gate 				check_modtime = FALSE;
3200Sstevel@tonic-gate 				(void) sigprocmask(SIG_SETMASK, NULL, &cset);
3210Sstevel@tonic-gate 				tset = cset;
3220Sstevel@tonic-gate 				(void) sigaddset(&tset, SIGCLD);
3230Sstevel@tonic-gate 				(void) sigprocmask(SIG_SETMASK, &tset, NULL);
3240Sstevel@tonic-gate 				free_defs();
3250Sstevel@tonic-gate #ifdef	DEBUG
3260Sstevel@tonic-gate 				debug("/etc/ttydefs is modified, re-read it");
3270Sstevel@tonic-gate #endif
3280Sstevel@tonic-gate 				read_ttydefs(NULL, FALSE);
3290Sstevel@tonic-gate 				(void) sigprocmask(SIG_SETMASK, &cset, NULL);
3300Sstevel@tonic-gate 			}
3310Sstevel@tonic-gate 			tp->p_status = VALID;
3320Sstevel@tonic-gate 			open_device(tp);
3330Sstevel@tonic-gate 			if (tp->p_fd > 0)
3340Sstevel@tonic-gate 				got_carrier(tp);
3350Sstevel@tonic-gate 		}
3360Sstevel@tonic-gate 	}
3370Sstevel@tonic-gate }
3380Sstevel@tonic-gate 
3390Sstevel@tonic-gate void
set_softcar(pmptr)3400Sstevel@tonic-gate set_softcar(pmptr)
3410Sstevel@tonic-gate struct	pmtab	*pmptr;
3420Sstevel@tonic-gate {
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate 	int fd, val = 0;
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate #ifdef	DEBUG
3470Sstevel@tonic-gate 	debug("in set_softcar");
3480Sstevel@tonic-gate #endif
3490Sstevel@tonic-gate 	/*
3500Sstevel@tonic-gate 	 * If soft carrier is not set one way or
3510Sstevel@tonic-gate 	 * the other, leave it alone.
3520Sstevel@tonic-gate 	 */
3530Sstevel@tonic-gate 	if (*pmptr->p_softcar == '\0')
3540Sstevel@tonic-gate 		return;
3550Sstevel@tonic-gate 
3560Sstevel@tonic-gate 	if (*pmptr->p_softcar == 'y')
3570Sstevel@tonic-gate 		val = 1;
3580Sstevel@tonic-gate 
3590Sstevel@tonic-gate 	if ((fd = open(pmptr->p_device, O_RDONLY|O_NONBLOCK|O_NOCTTY)) < 0) {
3600Sstevel@tonic-gate 		log("open (%s) failed: %s", pmptr->p_device, strerror(errno));
3610Sstevel@tonic-gate 		return;
3620Sstevel@tonic-gate 	}
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate 	if (ioctl(fd, TIOCSSOFTCAR, &val) < 0)
3650Sstevel@tonic-gate 		log("set soft-carrier (%s) failed: %s", pmptr->p_device,
3660Sstevel@tonic-gate 		    strerror(errno));
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate 	close(fd);
3690Sstevel@tonic-gate }
3700Sstevel@tonic-gate 
3710Sstevel@tonic-gate 
3720Sstevel@tonic-gate /*
3730Sstevel@tonic-gate  *	open_device(pmptr)	- open the device
3740Sstevel@tonic-gate  *				- check device lock
3750Sstevel@tonic-gate  *				- change owner of device
3760Sstevel@tonic-gate  *				- push line disciplines
3770Sstevel@tonic-gate  *				- set termio
3780Sstevel@tonic-gate  */
3790Sstevel@tonic-gate 
3800Sstevel@tonic-gate void
open_device(pmptr)3810Sstevel@tonic-gate open_device(pmptr)
3820Sstevel@tonic-gate struct	pmtab	*pmptr;
3830Sstevel@tonic-gate {
3840Sstevel@tonic-gate 	int	fd, tmpfd;
3850Sstevel@tonic-gate 	struct	sigaction	sigact;
3860Sstevel@tonic-gate 
3870Sstevel@tonic-gate #ifdef	DEBUG
3880Sstevel@tonic-gate 	debug("in open_device");
3890Sstevel@tonic-gate #endif
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate 	if (pmptr->p_status == GETTY) {
3920Sstevel@tonic-gate 		revokedevaccess(pmptr->p_device, 0, 0, 0);
3930Sstevel@tonic-gate 
3940Sstevel@tonic-gate 		if ((fd = open(pmptr->p_device, O_RDWR)) == -1)
3950Sstevel@tonic-gate 			fatal("open (%s) failed: %s", pmptr->p_device,
3960Sstevel@tonic-gate 			    strerror(errno));
3970Sstevel@tonic-gate 
3980Sstevel@tonic-gate 	} else {
3990Sstevel@tonic-gate 		if (check_spawnlimit(pmptr) == -1) {
4000Sstevel@tonic-gate 			pmptr->p_status = NOTVALID;
4010Sstevel@tonic-gate 			log("service <%s> is respawning too rapidly",
4020Sstevel@tonic-gate 			    pmptr->p_tag);
4030Sstevel@tonic-gate 			return;
4040Sstevel@tonic-gate 		}
4050Sstevel@tonic-gate 		if (pmptr->p_fd > 0) { /* file already open */
4060Sstevel@tonic-gate 			fd = pmptr->p_fd;
4070Sstevel@tonic-gate 			pmptr->p_fd = 0;
4080Sstevel@tonic-gate 		} else if ((fd = open(pmptr->p_device, O_RDWR|O_NONBLOCK))
4090Sstevel@tonic-gate 		    == -1) {
4100Sstevel@tonic-gate 			log("open (%s) failed: %s", pmptr->p_device,
4110Sstevel@tonic-gate 			    strerror(errno));
4120Sstevel@tonic-gate 			if ((errno ==  ENODEV) || (errno == EBUSY)) {
4130Sstevel@tonic-gate 				pmptr->p_status = UNACCESS;
4140Sstevel@tonic-gate 				Nlocked++;
4150Sstevel@tonic-gate 				if (Nlocked == 1) {
4160Sstevel@tonic-gate 				    sigact.sa_flags = 0;
4170Sstevel@tonic-gate 				    sigact.sa_handler = sigalarm;
4180Sstevel@tonic-gate 				    (void) sigemptyset(&sigact.sa_mask);
4190Sstevel@tonic-gate 				    (void) sigaction(SIGALRM, &sigact, NULL);
4200Sstevel@tonic-gate 				    (void) alarm(ALARMTIME);
4210Sstevel@tonic-gate 				}
4220Sstevel@tonic-gate 			} else
4230Sstevel@tonic-gate 				Retry = TRUE;
4240Sstevel@tonic-gate 			return;
4250Sstevel@tonic-gate 		}
4260Sstevel@tonic-gate 		/* set close-on-exec flag */
4270Sstevel@tonic-gate 		if (fcntl(fd, F_SETFD, 1) == -1)
4280Sstevel@tonic-gate 			fatal("F_SETFD fcntl failed: %s", strerror(errno));
4290Sstevel@tonic-gate 
4300Sstevel@tonic-gate 		if (tm_checklock(fd) != 0) {
4310Sstevel@tonic-gate 			pmptr->p_status = LOCKED;
4320Sstevel@tonic-gate 			(void) close(fd);
4330Sstevel@tonic-gate 			Nlocked++;
4340Sstevel@tonic-gate 			if (Nlocked == 1) {
4350Sstevel@tonic-gate 				sigact.sa_flags = 0;
4360Sstevel@tonic-gate 				sigact.sa_handler = sigalarm;
4370Sstevel@tonic-gate 				(void) sigemptyset(&sigact.sa_mask);
4380Sstevel@tonic-gate 				(void) sigaction(SIGALRM, &sigact, NULL);
4390Sstevel@tonic-gate 				(void) alarm(ALARMTIME);
4400Sstevel@tonic-gate 			}
4410Sstevel@tonic-gate 			return;
4420Sstevel@tonic-gate 		}
4430Sstevel@tonic-gate 		if (check_session(fd) != 0) {
4440Sstevel@tonic-gate 			if ((Initialized) && (pmptr->p_inservice != SESSION)) {
4450Sstevel@tonic-gate 				log("Warning -- active session exists on <%s>",
4460Sstevel@tonic-gate 				    pmptr->p_device);
4470Sstevel@tonic-gate 			} else {
4480Sstevel@tonic-gate 				/*
4490Sstevel@tonic-gate 				 * this may happen if a service is running
4500Sstevel@tonic-gate 				 * and ttymon dies and is restarted,
4510Sstevel@tonic-gate 				 * or another process is running on the
4520Sstevel@tonic-gate 				 * port.
4530Sstevel@tonic-gate 				 */
4540Sstevel@tonic-gate 				pmptr->p_status = SESSION;
4550Sstevel@tonic-gate 				pmptr->p_inservice = 0;
4560Sstevel@tonic-gate 				(void) close(fd);
4570Sstevel@tonic-gate 				Nlocked++;
4580Sstevel@tonic-gate 				if (Nlocked == 1) {
4590Sstevel@tonic-gate 					sigact.sa_flags = 0;
4600Sstevel@tonic-gate 					sigact.sa_handler = sigalarm;
4610Sstevel@tonic-gate 					(void) sigemptyset(&sigact.sa_mask);
4620Sstevel@tonic-gate 					(void) sigaction(SIGALRM, &sigact,
4630Sstevel@tonic-gate 					    NULL);
4640Sstevel@tonic-gate 					(void) alarm(ALARMTIME);
4650Sstevel@tonic-gate 				}
4660Sstevel@tonic-gate 				return;
4670Sstevel@tonic-gate 			}
4680Sstevel@tonic-gate 		}
4690Sstevel@tonic-gate 		pmptr->p_inservice = 0;
4700Sstevel@tonic-gate 	}
4710Sstevel@tonic-gate 
4720Sstevel@tonic-gate 	if (pmptr->p_ttyflags & H_FLAG) {
4730Sstevel@tonic-gate 		/* drop DTR */
4740Sstevel@tonic-gate 		(void) hang_up_line(fd);
4750Sstevel@tonic-gate 		/*
4760Sstevel@tonic-gate 		 * After hang_up_line, the stream is in STRHUP state.
4770Sstevel@tonic-gate 		 * We need to do another open to reinitialize streams
4780Sstevel@tonic-gate 		 * then we can close one fd
4790Sstevel@tonic-gate 		 */
4800Sstevel@tonic-gate 		if ((tmpfd = open(pmptr->p_device, O_RDWR|O_NONBLOCK)) == -1) {
4810Sstevel@tonic-gate 			log("open (%s) failed: %s", pmptr->p_device,
4820Sstevel@tonic-gate 			    strerror(errno));
4830Sstevel@tonic-gate 			Retry = TRUE;
4840Sstevel@tonic-gate 			(void) close(fd);
4850Sstevel@tonic-gate 			return;
4860Sstevel@tonic-gate 		}
4870Sstevel@tonic-gate 		(void) close(tmpfd);
4880Sstevel@tonic-gate 	}
4890Sstevel@tonic-gate 
4900Sstevel@tonic-gate #ifdef DEBUG
4910Sstevel@tonic-gate 	debug("open_device (%s), fd = %d", pmptr->p_device, fd);
4920Sstevel@tonic-gate #endif
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate 	/* Change ownership of the tty line to root/uucp and */
4950Sstevel@tonic-gate 	/* set protections to only allow root/uucp to read the line. */
4960Sstevel@tonic-gate 
4970Sstevel@tonic-gate 	if (pmptr->p_ttyflags & (B_FLAG|C_FLAG))
4980Sstevel@tonic-gate 		(void) fchown(fd, Uucp_uid, Tty_gid);
4990Sstevel@tonic-gate 	else
5000Sstevel@tonic-gate 		(void) fchown(fd, ROOTUID, Tty_gid);
5010Sstevel@tonic-gate 	(void) fchmod(fd, 0620);
5020Sstevel@tonic-gate 
5030Sstevel@tonic-gate 	if ((pmptr->p_modules != NULL)&&(*(pmptr->p_modules) != '\0')) {
5040Sstevel@tonic-gate 		if (push_linedisc(fd, pmptr->p_modules, pmptr->p_device)
5050Sstevel@tonic-gate 		    == -1) {
5060Sstevel@tonic-gate 			Retry = TRUE;
5070Sstevel@tonic-gate 			(void) close(fd);
5080Sstevel@tonic-gate 			return;
5090Sstevel@tonic-gate 		}
5100Sstevel@tonic-gate 	}
5110Sstevel@tonic-gate 
5120Sstevel@tonic-gate 	if (initial_termio(fd, pmptr) == -1)  {
5130Sstevel@tonic-gate 		Retry = TRUE;
5140Sstevel@tonic-gate 		(void) close(fd);
5150Sstevel@tonic-gate 		return;
5160Sstevel@tonic-gate 	}
5170Sstevel@tonic-gate 
5180Sstevel@tonic-gate 	di_devperm_logout((const char *)pmptr->p_device);
5190Sstevel@tonic-gate 	pmptr->p_fd = fd;
5200Sstevel@tonic-gate }
5210Sstevel@tonic-gate 
5220Sstevel@tonic-gate /*
5230Sstevel@tonic-gate  *	set_poll(fdp)	- put all fd's in a pollfd array
5240Sstevel@tonic-gate  *			- set poll event to POLLIN and POLLMSG
5250Sstevel@tonic-gate  *			- return number of fd to be polled
5260Sstevel@tonic-gate  */
5270Sstevel@tonic-gate 
5280Sstevel@tonic-gate static	int
set_poll(fdp)5290Sstevel@tonic-gate set_poll(fdp)
5300Sstevel@tonic-gate struct pollfd *fdp;
5310Sstevel@tonic-gate {
5320Sstevel@tonic-gate 	struct	pmtab	*tp;
5330Sstevel@tonic-gate 	int 	nfd = 0;
5340Sstevel@tonic-gate 
5350Sstevel@tonic-gate 	for (tp = PMtab; tp; tp = tp->p_next) {
5360Sstevel@tonic-gate 		if (tp->p_fd > 0)  {
5370Sstevel@tonic-gate 			fdp->fd = tp->p_fd;
5380Sstevel@tonic-gate 			fdp->events = POLLIN;
5390Sstevel@tonic-gate 			fdp++;
5400Sstevel@tonic-gate 			nfd++;
5410Sstevel@tonic-gate 		}
5420Sstevel@tonic-gate 	}
5430Sstevel@tonic-gate 	return (nfd);
5440Sstevel@tonic-gate }
5450Sstevel@tonic-gate 
5460Sstevel@tonic-gate /*
5470Sstevel@tonic-gate  *	check_spawnlimit	- return 0 if spawnlimit is not reached
5480Sstevel@tonic-gate  *				- otherwise return -1
5490Sstevel@tonic-gate  */
5500Sstevel@tonic-gate static	int
check_spawnlimit(pmptr)5510Sstevel@tonic-gate check_spawnlimit(pmptr)
5520Sstevel@tonic-gate struct	pmtab	*pmptr;
5530Sstevel@tonic-gate {
5540Sstevel@tonic-gate 	time_t	now;
5550Sstevel@tonic-gate 
5560Sstevel@tonic-gate 	(void) time(&now);
5570Sstevel@tonic-gate 	if (pmptr->p_time == 0L)
5580Sstevel@tonic-gate 		pmptr->p_time = now;
5590Sstevel@tonic-gate 	if (pmptr->p_respawn >= SPAWN_LIMIT) {
5600Sstevel@tonic-gate 		if ((now - pmptr->p_time) < SPAWN_INTERVAL) {
5610Sstevel@tonic-gate 			pmptr->p_time = now;
5620Sstevel@tonic-gate 			pmptr->p_respawn = 0;
5630Sstevel@tonic-gate 			return (-1);
5640Sstevel@tonic-gate 		}
5650Sstevel@tonic-gate 		pmptr->p_time = now;
5660Sstevel@tonic-gate 		pmptr->p_respawn = 0;
5670Sstevel@tonic-gate 	}
5680Sstevel@tonic-gate 	pmptr->p_respawn++;
5690Sstevel@tonic-gate 	return (0);
5700Sstevel@tonic-gate }
5710Sstevel@tonic-gate 
5720Sstevel@tonic-gate /*
5730Sstevel@tonic-gate  * mod_ttydefs	- to check if /etc/ttydefs has been modified
5740Sstevel@tonic-gate  *		- return TRUE if file modified
5750Sstevel@tonic-gate  *		- otherwise, return FALSE
5760Sstevel@tonic-gate  */
5770Sstevel@tonic-gate static	int
mod_ttydefs()5780Sstevel@tonic-gate mod_ttydefs()
5790Sstevel@tonic-gate {
5800Sstevel@tonic-gate 	struct	stat	statbuf;
5810Sstevel@tonic-gate 	extern	long	Mtime;
5820Sstevel@tonic-gate 	if (stat(TTYDEFS, &statbuf) == -1) {
5830Sstevel@tonic-gate 		/* if stat failed, don't bother reread ttydefs */
5840Sstevel@tonic-gate 		return (FALSE);
5850Sstevel@tonic-gate 	}
5860Sstevel@tonic-gate 	if ((long)statbuf.st_mtime != Mtime) {
5870Sstevel@tonic-gate 		Mtime = (long)statbuf.st_mtime;
5880Sstevel@tonic-gate 		return (TRUE);
5890Sstevel@tonic-gate 	}
5900Sstevel@tonic-gate 	return (FALSE);
5910Sstevel@tonic-gate }
5920Sstevel@tonic-gate 
5930Sstevel@tonic-gate /*
5940Sstevel@tonic-gate  *	free_defs - free the Gdef table
5950Sstevel@tonic-gate  */
5960Sstevel@tonic-gate static	void
free_defs()5970Sstevel@tonic-gate free_defs()
5980Sstevel@tonic-gate {
5990Sstevel@tonic-gate 	int	i;
6000Sstevel@tonic-gate 	struct	Gdef	*tp;
6010Sstevel@tonic-gate 	tp = &Gdef[0];
6020Sstevel@tonic-gate 	for (i = 0; i < Ndefs; i++, tp++) {
6030Sstevel@tonic-gate 		free(tp->g_id);
6040Sstevel@tonic-gate 		free(tp->g_iflags);
6050Sstevel@tonic-gate 		free(tp->g_fflags);
6060Sstevel@tonic-gate 		free(tp->g_nextid);
6070Sstevel@tonic-gate 		tp->g_id = NULL;
6080Sstevel@tonic-gate 		tp->g_iflags = NULL;
6090Sstevel@tonic-gate 		tp->g_fflags = NULL;
6100Sstevel@tonic-gate 		tp->g_nextid = NULL;
6110Sstevel@tonic-gate 	}
6120Sstevel@tonic-gate 	Ndefs = 0;
6130Sstevel@tonic-gate }
6140Sstevel@tonic-gate 
6150Sstevel@tonic-gate /*
6160Sstevel@tonic-gate  * struct Gdef *get_speed(ttylabel)
6170Sstevel@tonic-gate  *	- search "/etc/ttydefs" for speed and term. specification
6180Sstevel@tonic-gate  *	  using "ttylabel". If "ttylabel" is NULL, default
6190Sstevel@tonic-gate  *	  to DEFAULT
6200Sstevel@tonic-gate  * arg:	  ttylabel - label/id of speed settings.
6210Sstevel@tonic-gate  */
6220Sstevel@tonic-gate 
6230Sstevel@tonic-gate struct Gdef *
get_speed(char * ttylabel)6240Sstevel@tonic-gate get_speed(char *ttylabel)
6250Sstevel@tonic-gate {
6260Sstevel@tonic-gate 	register struct Gdef *sp;
6270Sstevel@tonic-gate 	extern   struct Gdef DEFAULT;
6280Sstevel@tonic-gate 
6290Sstevel@tonic-gate 	if ((ttylabel != NULL) && (*ttylabel != '\0')) {
6300Sstevel@tonic-gate 		if ((sp = find_def(ttylabel)) == NULL) {
6310Sstevel@tonic-gate 			log("unable to find <%s> in \"%s\"", ttylabel, TTYDEFS);
6320Sstevel@tonic-gate 			sp = &DEFAULT; /* use default */
6330Sstevel@tonic-gate 		}
6340Sstevel@tonic-gate 	} else sp = &DEFAULT; /* use default */
6350Sstevel@tonic-gate 	return (sp);
6360Sstevel@tonic-gate }
6370Sstevel@tonic-gate 
6380Sstevel@tonic-gate /*
6390Sstevel@tonic-gate  * setup_PCpipe()	- setup the pipe between Parent and Children
6400Sstevel@tonic-gate  *			- the pipe is used for a tmchild to send its
6410Sstevel@tonic-gate  *			  pid to inform ttymon that it is about to
6420Sstevel@tonic-gate  *			  invoke service
6430Sstevel@tonic-gate  *			- the pipe also serves as a mean for tmchild
6440Sstevel@tonic-gate  *			  to detect failure of ttymon
6450Sstevel@tonic-gate  */
6460Sstevel@tonic-gate void
setup_PCpipe()6470Sstevel@tonic-gate setup_PCpipe()
6480Sstevel@tonic-gate {
6490Sstevel@tonic-gate 	int	flag = 0;
6500Sstevel@tonic-gate 
6510Sstevel@tonic-gate 	if (pipe(PCpipe) == -1)
6520Sstevel@tonic-gate 		fatal("pipe() failed: %s", strerror(errno));
6530Sstevel@tonic-gate 
6540Sstevel@tonic-gate 	/* set close-on-exec flag */
6550Sstevel@tonic-gate 	if (fcntl(PCpipe[0], F_SETFD, 1) == -1)
6560Sstevel@tonic-gate 		fatal("F_SETFD fcntl failed: %s", strerror(errno));
6570Sstevel@tonic-gate 
6580Sstevel@tonic-gate 	if (fcntl(PCpipe[1], F_SETFD, 1) == -1)
6590Sstevel@tonic-gate 		fatal("F_SETFD fcntl failed: %s", strerror(errno));
6600Sstevel@tonic-gate 
6610Sstevel@tonic-gate 	/* set O_NONBLOCK flag */
6620Sstevel@tonic-gate 	if (fcntl(PCpipe[0], F_GETFL, flag) == -1)
6630Sstevel@tonic-gate 		fatal("F_GETFL failed: %s", strerror(errno));
6640Sstevel@tonic-gate 
6650Sstevel@tonic-gate 	flag |= O_NONBLOCK;
6660Sstevel@tonic-gate 	if (fcntl(PCpipe[0], F_SETFL, flag) == -1)
6670Sstevel@tonic-gate 		fatal("F_SETFL failed: %s", strerror(errno));
6680Sstevel@tonic-gate 
6690Sstevel@tonic-gate 	/* set message discard mode */
6700Sstevel@tonic-gate 	if (ioctl(PCpipe[0], I_SRDOPT, RMSGD) == -1)
6710Sstevel@tonic-gate 		fatal("I_SRDOPT RMSGD failed: %s", strerror(errno));
6720Sstevel@tonic-gate 
6730Sstevel@tonic-gate 	/* register to receive SIGPOLL when data come */
6740Sstevel@tonic-gate 	if (ioctl(PCpipe[0], I_SETSIG, S_INPUT) == -1)
6750Sstevel@tonic-gate 		fatal("I_SETSIG S_INPUT failed: %s", strerror(errno));
6760Sstevel@tonic-gate 
6770Sstevel@tonic-gate #ifdef 	DEBUG
6780Sstevel@tonic-gate 	log("PCpipe[0]\t = %d", PCpipe[0]);
6790Sstevel@tonic-gate 	log("PCpipe[1]\t = %d", PCpipe[1]);
6800Sstevel@tonic-gate #endif
6810Sstevel@tonic-gate }
682