xref: /titanic_41/usr/src/cmd/ttymon/ttymon.c (revision d67944fbe3fa0b31893a7116a09b0718eecf6078)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5004388ebScasper  * Common Development and Distribution License (the "License").
6004388ebScasper  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22b77c815bSNobutomo Nakano  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
277c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
287c478bd9Sstevel@tonic-gate 
29004388ebScasper #include <stdio_ext.h>
307c478bd9Sstevel@tonic-gate #include <stdlib.h>
317c478bd9Sstevel@tonic-gate #include <fcntl.h>
327c478bd9Sstevel@tonic-gate #include <errno.h>
337c478bd9Sstevel@tonic-gate #include <poll.h>
347c478bd9Sstevel@tonic-gate #include <string.h>
357c478bd9Sstevel@tonic-gate #include <signal.h>
367c478bd9Sstevel@tonic-gate #include <sys/types.h>
377c478bd9Sstevel@tonic-gate #include <sys/stat.h>
387c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
397c478bd9Sstevel@tonic-gate #include <sys/resource.h>
407c478bd9Sstevel@tonic-gate #include <sys/termios.h>
417c478bd9Sstevel@tonic-gate #include <pwd.h>
427c478bd9Sstevel@tonic-gate #include <grp.h>
437c478bd9Sstevel@tonic-gate #include <unistd.h>
447c478bd9Sstevel@tonic-gate #include <ulimit.h>
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate #include "sac.h"
477c478bd9Sstevel@tonic-gate #include "ttymon.h"
487c478bd9Sstevel@tonic-gate #include "tmstruct.h"
497c478bd9Sstevel@tonic-gate #include "tmextern.h"
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate static	int	Initialized;
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate extern	int	Retry;
547c478bd9Sstevel@tonic-gate extern	struct	pollfd	*Pollp;
557c478bd9Sstevel@tonic-gate static	void	initialize();
567c478bd9Sstevel@tonic-gate static	void	open_all();
577c478bd9Sstevel@tonic-gate static	int	set_poll();
587c478bd9Sstevel@tonic-gate static	int	check_spawnlimit();
597c478bd9Sstevel@tonic-gate static	int	mod_ttydefs();
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate void	open_device();
627c478bd9Sstevel@tonic-gate void	set_softcar();
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate extern	int	check_session();
657c478bd9Sstevel@tonic-gate extern	void	sigalarm();
667c478bd9Sstevel@tonic-gate extern	void	revokedevaccess(char *, uid_t, gid_t, mode_t);
677c478bd9Sstevel@tonic-gate /* can't include libdevinfo.h */
687c478bd9Sstevel@tonic-gate extern int di_devperm_logout(const char *);
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate /*
717c478bd9Sstevel@tonic-gate  * 	ttymon	- a port monitor under SAC
727c478bd9Sstevel@tonic-gate  *		- monitor ports, set terminal modes, baud rate
737c478bd9Sstevel@tonic-gate  *		  and line discipline for the port
747c478bd9Sstevel@tonic-gate  *		- invoke service on port if connection request received
757c478bd9Sstevel@tonic-gate  *		- Usage: ttymon
767c478bd9Sstevel@tonic-gate  *			 ttymon -g [options]
777c478bd9Sstevel@tonic-gate  *			 Valid options are
787c478bd9Sstevel@tonic-gate  *			 -h
797c478bd9Sstevel@tonic-gate  *			 -d device
807c478bd9Sstevel@tonic-gate  *			 -l ttylabel
817c478bd9Sstevel@tonic-gate  *			 -t timeout
827c478bd9Sstevel@tonic-gate  *			 -m modules
837c478bd9Sstevel@tonic-gate  *			 -p prompt
847c478bd9Sstevel@tonic-gate  *
857c478bd9Sstevel@tonic-gate  *		- ttymon without args is invoked by SAC
867c478bd9Sstevel@tonic-gate  *		- ttymon -g is invoked by process that needs to
877c478bd9Sstevel@tonic-gate  *		  have login service on the fly
887c478bd9Sstevel@tonic-gate  */
897c478bd9Sstevel@tonic-gate 
9034e48580Sdp int
main(int argc,char * argv[])9134e48580Sdp main(int argc, char *argv[])
927c478bd9Sstevel@tonic-gate {
937c478bd9Sstevel@tonic-gate 	int	nfds;
947c478bd9Sstevel@tonic-gate 	extern	char	*lastname();
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 	/*
977c478bd9Sstevel@tonic-gate 	 * Only the superuser should execute this command.
987c478bd9Sstevel@tonic-gate 	 */
997c478bd9Sstevel@tonic-gate 	if (getuid() != 0)
100b77c815bSNobutomo Nakano 		return (1);
101b77c815bSNobutomo Nakano 
102b77c815bSNobutomo Nakano 	/* remember original signal mask and dispositions */
103b77c815bSNobutomo Nakano 	(void) sigprocmask(SIG_SETMASK, NULL, &Origmask);
104b77c815bSNobutomo Nakano 	(void) sigaction(SIGINT, NULL, &Sigint);
105b77c815bSNobutomo Nakano 	(void) sigaction(SIGALRM, NULL, &Sigalrm);
106b77c815bSNobutomo Nakano 	(void) sigaction(SIGPOLL, NULL, &Sigpoll);
107b77c815bSNobutomo Nakano 	(void) sigaction(SIGQUIT, NULL, &Sigquit);
108b77c815bSNobutomo Nakano 	(void) sigaction(SIGCLD, NULL, &Sigcld);
109b77c815bSNobutomo Nakano 	(void) sigaction(SIGTERM, NULL, &Sigterm);
110b77c815bSNobutomo Nakano #ifdef	DEBUG
111b77c815bSNobutomo Nakano 	(void) sigaction(SIGUSR1, NULL, &Sigusr1);
112b77c815bSNobutomo Nakano 	(void) sigaction(SIGUSR2, NULL, &Sigusr2);
113b77c815bSNobutomo Nakano #endif
114b77c815bSNobutomo Nakano 
115b77c815bSNobutomo Nakano 	/*
116b77c815bSNobutomo Nakano 	 * SIGQUIT needs to be ignored. Otherwise, hitting ^\ from
117b77c815bSNobutomo Nakano 	 * console kills ttymon.
118b77c815bSNobutomo Nakano 	 */
119b77c815bSNobutomo Nakano 	(void) signal(SIGQUIT, SIG_IGN);
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 	if ((argc > 1) || (strcmp(lastname(argv[0]), "getty") == 0)) {
1227c478bd9Sstevel@tonic-gate 		ttymon_express(argc, argv);
1237c478bd9Sstevel@tonic-gate 		return (1);	/*NOTREACHED*/
1247c478bd9Sstevel@tonic-gate 	}
125b77c815bSNobutomo Nakano 
1267c478bd9Sstevel@tonic-gate 	initialize();
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 	for (;;) {
1297c478bd9Sstevel@tonic-gate 		nfds = set_poll(Pollp);
1307c478bd9Sstevel@tonic-gate 		if (!Reread_flag) {
1317c478bd9Sstevel@tonic-gate 			if (nfds > 0)
1327c478bd9Sstevel@tonic-gate 				do_poll(Pollp, nfds);
1337c478bd9Sstevel@tonic-gate 			else
1347c478bd9Sstevel@tonic-gate 				(void) pause();
1357c478bd9Sstevel@tonic-gate 		}
1367c478bd9Sstevel@tonic-gate 		/*
1377c478bd9Sstevel@tonic-gate 		 * READDB messages may arrive during poll or pause.
1387c478bd9Sstevel@tonic-gate 		 * So the flag needs to be checked again.
1397c478bd9Sstevel@tonic-gate 		 */
1407c478bd9Sstevel@tonic-gate 		if (Reread_flag) {
1417c478bd9Sstevel@tonic-gate 			Reread_flag = FALSE;
1427c478bd9Sstevel@tonic-gate 			re_read();
1437c478bd9Sstevel@tonic-gate 		}
1447c478bd9Sstevel@tonic-gate 		while (Retry) {
1457c478bd9Sstevel@tonic-gate 			Retry = FALSE;
1467c478bd9Sstevel@tonic-gate 			open_all();
1477c478bd9Sstevel@tonic-gate 		}
1487c478bd9Sstevel@tonic-gate 	}
1497c478bd9Sstevel@tonic-gate }
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate static	void
initialize()1527c478bd9Sstevel@tonic-gate initialize()
1537c478bd9Sstevel@tonic-gate {
1547c478bd9Sstevel@tonic-gate 	struct	pmtab	*tp;
1557c478bd9Sstevel@tonic-gate 	register struct passwd *pwdp;
1567c478bd9Sstevel@tonic-gate 	register struct	group	*gp;
1577c478bd9Sstevel@tonic-gate 	struct	rlimit rlimit;
1587c478bd9Sstevel@tonic-gate 	extern	struct	rlimit	Rlimit;
1597c478bd9Sstevel@tonic-gate 	extern	 uid_t	Uucp_uid;
1607c478bd9Sstevel@tonic-gate 	extern	 gid_t	Tty_gid;
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate #ifdef 	DEBUG
1637c478bd9Sstevel@tonic-gate 	extern	opendebug();
1647c478bd9Sstevel@tonic-gate #endif
1657c478bd9Sstevel@tonic-gate 	Initialized = FALSE;
1667c478bd9Sstevel@tonic-gate 	/*
1677c478bd9Sstevel@tonic-gate 	 * get_environ() must be called first,
1687c478bd9Sstevel@tonic-gate 	 * otherwise we don't know where the log file is
1697c478bd9Sstevel@tonic-gate 	 */
1707c478bd9Sstevel@tonic-gate 	get_environ();
1717c478bd9Sstevel@tonic-gate 	openttymonlog();
1727c478bd9Sstevel@tonic-gate 	openpid();
1737c478bd9Sstevel@tonic-gate 	openpipes();
1747c478bd9Sstevel@tonic-gate 	setup_PCpipe();
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	log("PMTAG: %s", Tag);
1777c478bd9Sstevel@tonic-gate 	log("Starting state: %s",
1787c478bd9Sstevel@tonic-gate 	    (State == PM_ENABLED) ? "enabled" : "disabled");
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate #ifdef 	DEBUG
1817c478bd9Sstevel@tonic-gate 	opendebug(FALSE);
1827c478bd9Sstevel@tonic-gate 	debug("***** ttymon in initialize *****");
1837c478bd9Sstevel@tonic-gate 	log("debug mode is \t on");
1847c478bd9Sstevel@tonic-gate #endif
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 	catch_signals();
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 	/* register to receive SIGPOLL when data comes to pmpipe */
1897c478bd9Sstevel@tonic-gate 	if (ioctl(Pfd, I_SETSIG, S_INPUT) < 0)
1907c478bd9Sstevel@tonic-gate 		fatal("I_SETSIG on pmpipe failed: %s", strerror(errno));
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	sacpoll(); /* this is needed because there may be data already */
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 	Maxfiles = (int)ulimit(4, 0L);	/* get max number of open files */
1957c478bd9Sstevel@tonic-gate 	if (Maxfiles < 0)
1967c478bd9Sstevel@tonic-gate 		fatal("ulimit(4,0L) failed: %s", strerror(errno));
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 	if (getrlimit(RLIMIT_NOFILE, &Rlimit) == -1)
1997c478bd9Sstevel@tonic-gate 		fatal("getrlimit failed: %s", strerror(errno));
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	rlimit.rlim_cur = rlimit.rlim_max = Rlimit.rlim_max;
2027c478bd9Sstevel@tonic-gate 	if (setrlimit(RLIMIT_NOFILE, &rlimit) == -1)
2037c478bd9Sstevel@tonic-gate 		fatal("setrlimit failed: %s", strerror(errno));
2047c478bd9Sstevel@tonic-gate 
205004388ebScasper 	(void) enable_extended_FILE_stdio(-1, -1);
206004388ebScasper 
2077c478bd9Sstevel@tonic-gate 	Maxfiles = rlimit.rlim_cur;
2087c478bd9Sstevel@tonic-gate 	Maxfds = Maxfiles - FILE_RESERVED;
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 	log("max open files = %d", Maxfiles);
2117c478bd9Sstevel@tonic-gate 	log("max ports ttymon can monitor = %d", Maxfds);
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 	read_pmtab();
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	/*
2167c478bd9Sstevel@tonic-gate 	 * setup poll array
2177c478bd9Sstevel@tonic-gate 	 * 	- we allocate 10 extra pollfd so that
2187c478bd9Sstevel@tonic-gate 	 *	  we do not have to re-malloc when there is
2197c478bd9Sstevel@tonic-gate 	 *	  minor fluctuation in Nentries
2207c478bd9Sstevel@tonic-gate 	 */
2217c478bd9Sstevel@tonic-gate 	Npollfd = Nentries + 10;
2227c478bd9Sstevel@tonic-gate 	if (Npollfd > Maxfds)
2237c478bd9Sstevel@tonic-gate 		Npollfd = Maxfds;
2247c478bd9Sstevel@tonic-gate 	if ((Pollp = (struct pollfd *)
2257c478bd9Sstevel@tonic-gate 	    malloc((unsigned)(Npollfd * sizeof (struct pollfd))))
2267c478bd9Sstevel@tonic-gate 	    == (struct pollfd *)NULL)
2277c478bd9Sstevel@tonic-gate 		fatal("malloc for Pollp failed");
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 	(void) mod_ttydefs();	/* just to initialize Mtime */
2307c478bd9Sstevel@tonic-gate 	if (check_version(TTYDEFS_VERS, TTYDEFS) != 0)
2317c478bd9Sstevel@tonic-gate 		fatal("check /etc/ttydefs version failed");
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 	read_ttydefs(NULL, FALSE);
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 	/* initialize global variables, Uucp_uid & Tty_gid */
2367c478bd9Sstevel@tonic-gate 	if ((pwdp = getpwnam(UUCP)) != NULL)
2377c478bd9Sstevel@tonic-gate 		Uucp_uid = pwdp->pw_uid;
2387c478bd9Sstevel@tonic-gate 	if ((gp = getgrnam(TTY)) == NULL)
2397c478bd9Sstevel@tonic-gate 		log("no group entry for <tty>, default is used");
2407c478bd9Sstevel@tonic-gate 	else
2417c478bd9Sstevel@tonic-gate 		Tty_gid = gp->gr_gid;
2427c478bd9Sstevel@tonic-gate 	endgrent();
2437c478bd9Sstevel@tonic-gate 	endpwent();
2447c478bd9Sstevel@tonic-gate #ifdef	DEBUG
245f48205beScasper 	debug("Uucp_uid = %u, Tty_gid = %u", Uucp_uid, Tty_gid);
2467c478bd9Sstevel@tonic-gate #endif
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 	log("Initialization Completed");
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 	/* open the devices ttymon monitors */
2517c478bd9Sstevel@tonic-gate 	Retry = TRUE;
2527c478bd9Sstevel@tonic-gate 	while (Retry) {
2537c478bd9Sstevel@tonic-gate 		Retry = FALSE;
2547c478bd9Sstevel@tonic-gate 		for (tp = PMtab; tp; tp = tp->p_next) {
2557c478bd9Sstevel@tonic-gate 			if ((tp->p_status > 0) && (tp->p_fd == 0) &&
2567c478bd9Sstevel@tonic-gate 			    (tp->p_pid == 0) && !(tp->p_ttyflags & I_FLAG) &&
2577c478bd9Sstevel@tonic-gate 			    (!((State == PM_DISABLED) &&
2587c478bd9Sstevel@tonic-gate 			    ((tp->p_dmsg == NULL)||(*(tp->p_dmsg) == '\0'))))) {
2597c478bd9Sstevel@tonic-gate 				open_device(tp);
2607c478bd9Sstevel@tonic-gate 				if (tp->p_fd > 0)
2617c478bd9Sstevel@tonic-gate 					got_carrier(tp);
2627c478bd9Sstevel@tonic-gate 			}
2637c478bd9Sstevel@tonic-gate 		}
2647c478bd9Sstevel@tonic-gate 	}
2657c478bd9Sstevel@tonic-gate 	Initialized = TRUE;
2667c478bd9Sstevel@tonic-gate }
2677c478bd9Sstevel@tonic-gate 
268*d67944fbSScott Rotondo static	void	free_defs();
269*d67944fbSScott Rotondo 
2707c478bd9Sstevel@tonic-gate /*
2717c478bd9Sstevel@tonic-gate  *	open_all - open devices in pmtab if the entry is
2727c478bd9Sstevel@tonic-gate  *	         - valid, fd = 0, and pid = 0
2737c478bd9Sstevel@tonic-gate  */
2747c478bd9Sstevel@tonic-gate static void
open_all()2757c478bd9Sstevel@tonic-gate open_all()
2767c478bd9Sstevel@tonic-gate {
2777c478bd9Sstevel@tonic-gate 	struct	pmtab	*tp;
2787c478bd9Sstevel@tonic-gate 	int	check_modtime;
2797c478bd9Sstevel@tonic-gate 	sigset_t cset;
2807c478bd9Sstevel@tonic-gate 	sigset_t tset;
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate #ifdef	DEBUG
2837c478bd9Sstevel@tonic-gate 	debug("in open_all");
2847c478bd9Sstevel@tonic-gate #endif
2857c478bd9Sstevel@tonic-gate 	check_modtime = TRUE;
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 	for (tp = PMtab; tp; tp = tp->p_next) {
2887c478bd9Sstevel@tonic-gate 		if ((tp->p_status > 0) && (tp->p_fd == 0) &&
2897c478bd9Sstevel@tonic-gate 		    (tp->p_pid == 0) &&
2907c478bd9Sstevel@tonic-gate 		    !(tp->p_ttyflags & I_FLAG) && (!((State == PM_DISABLED) &&
2917c478bd9Sstevel@tonic-gate 		    ((tp->p_dmsg == NULL)||(*(tp->p_dmsg) == '\0'))))) {
2927c478bd9Sstevel@tonic-gate 			/*
2937c478bd9Sstevel@tonic-gate 			 * if we have not check modification time and
2947c478bd9Sstevel@tonic-gate 			 * /etc/ttydefs was modified, need to re-read it
2957c478bd9Sstevel@tonic-gate 			 */
2967c478bd9Sstevel@tonic-gate 			if (check_modtime && mod_ttydefs()) {
2977c478bd9Sstevel@tonic-gate 				check_modtime = FALSE;
2987c478bd9Sstevel@tonic-gate 				(void) sigprocmask(SIG_SETMASK, NULL, &cset);
2997c478bd9Sstevel@tonic-gate 				tset = cset;
3007c478bd9Sstevel@tonic-gate 				(void) sigaddset(&tset, SIGCLD);
3017c478bd9Sstevel@tonic-gate 				(void) sigprocmask(SIG_SETMASK, &tset, NULL);
3027c478bd9Sstevel@tonic-gate 				free_defs();
3037c478bd9Sstevel@tonic-gate #ifdef	DEBUG
3047c478bd9Sstevel@tonic-gate 				debug("/etc/ttydefs is modified, re-read it");
3057c478bd9Sstevel@tonic-gate #endif
3067c478bd9Sstevel@tonic-gate 				read_ttydefs(NULL, FALSE);
3077c478bd9Sstevel@tonic-gate 				(void) sigprocmask(SIG_SETMASK, &cset, NULL);
3087c478bd9Sstevel@tonic-gate 			}
3097c478bd9Sstevel@tonic-gate 			open_device(tp);
3107c478bd9Sstevel@tonic-gate 			if (tp->p_fd > 0)
3117c478bd9Sstevel@tonic-gate 				got_carrier(tp);
3127c478bd9Sstevel@tonic-gate 		} else if (((tp->p_status == LOCKED) ||
3137c478bd9Sstevel@tonic-gate 		    (tp->p_status == SESSION) ||
3147c478bd9Sstevel@tonic-gate 		    (tp->p_status == UNACCESS)) &&
3157c478bd9Sstevel@tonic-gate 		    (tp->p_fd > 0) &&
3167c478bd9Sstevel@tonic-gate 		    (!((State == PM_DISABLED) &&
3177c478bd9Sstevel@tonic-gate 		    ((tp->p_dmsg == NULL)||(*(tp->p_dmsg) == '\0'))))) {
3187c478bd9Sstevel@tonic-gate 			if (check_modtime && mod_ttydefs()) {
3197c478bd9Sstevel@tonic-gate 				check_modtime = FALSE;
3207c478bd9Sstevel@tonic-gate 				(void) sigprocmask(SIG_SETMASK, NULL, &cset);
3217c478bd9Sstevel@tonic-gate 				tset = cset;
3227c478bd9Sstevel@tonic-gate 				(void) sigaddset(&tset, SIGCLD);
3237c478bd9Sstevel@tonic-gate 				(void) sigprocmask(SIG_SETMASK, &tset, NULL);
3247c478bd9Sstevel@tonic-gate 				free_defs();
3257c478bd9Sstevel@tonic-gate #ifdef	DEBUG
3267c478bd9Sstevel@tonic-gate 				debug("/etc/ttydefs is modified, re-read it");
3277c478bd9Sstevel@tonic-gate #endif
3287c478bd9Sstevel@tonic-gate 				read_ttydefs(NULL, FALSE);
3297c478bd9Sstevel@tonic-gate 				(void) sigprocmask(SIG_SETMASK, &cset, NULL);
3307c478bd9Sstevel@tonic-gate 			}
3317c478bd9Sstevel@tonic-gate 			tp->p_status = VALID;
3327c478bd9Sstevel@tonic-gate 			open_device(tp);
3337c478bd9Sstevel@tonic-gate 			if (tp->p_fd > 0)
3347c478bd9Sstevel@tonic-gate 				got_carrier(tp);
3357c478bd9Sstevel@tonic-gate 		}
3367c478bd9Sstevel@tonic-gate 	}
3377c478bd9Sstevel@tonic-gate }
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate void
set_softcar(pmptr)3407c478bd9Sstevel@tonic-gate set_softcar(pmptr)
3417c478bd9Sstevel@tonic-gate struct	pmtab	*pmptr;
3427c478bd9Sstevel@tonic-gate {
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 	int fd, val = 0;
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate #ifdef	DEBUG
3477c478bd9Sstevel@tonic-gate 	debug("in set_softcar");
3487c478bd9Sstevel@tonic-gate #endif
3497c478bd9Sstevel@tonic-gate 	/*
3507c478bd9Sstevel@tonic-gate 	 * If soft carrier is not set one way or
3517c478bd9Sstevel@tonic-gate 	 * the other, leave it alone.
3527c478bd9Sstevel@tonic-gate 	 */
3537c478bd9Sstevel@tonic-gate 	if (*pmptr->p_softcar == '\0')
3547c478bd9Sstevel@tonic-gate 		return;
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 	if (*pmptr->p_softcar == 'y')
3577c478bd9Sstevel@tonic-gate 		val = 1;
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate 	if ((fd = open(pmptr->p_device, O_RDONLY|O_NONBLOCK|O_NOCTTY)) < 0) {
3607c478bd9Sstevel@tonic-gate 		log("open (%s) failed: %s", pmptr->p_device, strerror(errno));
3617c478bd9Sstevel@tonic-gate 		return;
3627c478bd9Sstevel@tonic-gate 	}
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 	if (ioctl(fd, TIOCSSOFTCAR, &val) < 0)
3657c478bd9Sstevel@tonic-gate 		log("set soft-carrier (%s) failed: %s", pmptr->p_device,
3667c478bd9Sstevel@tonic-gate 		    strerror(errno));
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 	close(fd);
3697c478bd9Sstevel@tonic-gate }
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate /*
3737c478bd9Sstevel@tonic-gate  *	open_device(pmptr)	- open the device
3747c478bd9Sstevel@tonic-gate  *				- check device lock
3757c478bd9Sstevel@tonic-gate  *				- change owner of device
3767c478bd9Sstevel@tonic-gate  *				- push line disciplines
3777c478bd9Sstevel@tonic-gate  *				- set termio
3787c478bd9Sstevel@tonic-gate  */
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate void
open_device(pmptr)3817c478bd9Sstevel@tonic-gate open_device(pmptr)
3827c478bd9Sstevel@tonic-gate struct	pmtab	*pmptr;
3837c478bd9Sstevel@tonic-gate {
3847c478bd9Sstevel@tonic-gate 	int	fd, tmpfd;
3857c478bd9Sstevel@tonic-gate 	struct	sigaction	sigact;
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate #ifdef	DEBUG
3887c478bd9Sstevel@tonic-gate 	debug("in open_device");
3897c478bd9Sstevel@tonic-gate #endif
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 	if (pmptr->p_status == GETTY) {
3927c478bd9Sstevel@tonic-gate 		revokedevaccess(pmptr->p_device, 0, 0, 0);
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 		if ((fd = open(pmptr->p_device, O_RDWR)) == -1)
3957c478bd9Sstevel@tonic-gate 			fatal("open (%s) failed: %s", pmptr->p_device,
3967c478bd9Sstevel@tonic-gate 			    strerror(errno));
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 	} else {
3997c478bd9Sstevel@tonic-gate 		if (check_spawnlimit(pmptr) == -1) {
4007c478bd9Sstevel@tonic-gate 			pmptr->p_status = NOTVALID;
4017c478bd9Sstevel@tonic-gate 			log("service <%s> is respawning too rapidly",
4027c478bd9Sstevel@tonic-gate 			    pmptr->p_tag);
4037c478bd9Sstevel@tonic-gate 			return;
4047c478bd9Sstevel@tonic-gate 		}
4057c478bd9Sstevel@tonic-gate 		if (pmptr->p_fd > 0) { /* file already open */
4067c478bd9Sstevel@tonic-gate 			fd = pmptr->p_fd;
4077c478bd9Sstevel@tonic-gate 			pmptr->p_fd = 0;
4087c478bd9Sstevel@tonic-gate 		} else if ((fd = open(pmptr->p_device, O_RDWR|O_NONBLOCK))
4097c478bd9Sstevel@tonic-gate 		    == -1) {
4107c478bd9Sstevel@tonic-gate 			log("open (%s) failed: %s", pmptr->p_device,
4117c478bd9Sstevel@tonic-gate 			    strerror(errno));
4127c478bd9Sstevel@tonic-gate 			if ((errno ==  ENODEV) || (errno == EBUSY)) {
4137c478bd9Sstevel@tonic-gate 				pmptr->p_status = UNACCESS;
4147c478bd9Sstevel@tonic-gate 				Nlocked++;
4157c478bd9Sstevel@tonic-gate 				if (Nlocked == 1) {
4167c478bd9Sstevel@tonic-gate 				    sigact.sa_flags = 0;
4177c478bd9Sstevel@tonic-gate 				    sigact.sa_handler = sigalarm;
4187c478bd9Sstevel@tonic-gate 				    (void) sigemptyset(&sigact.sa_mask);
4197c478bd9Sstevel@tonic-gate 				    (void) sigaction(SIGALRM, &sigact, NULL);
4207c478bd9Sstevel@tonic-gate 				    (void) alarm(ALARMTIME);
4217c478bd9Sstevel@tonic-gate 				}
4227c478bd9Sstevel@tonic-gate 			} else
4237c478bd9Sstevel@tonic-gate 				Retry = TRUE;
4247c478bd9Sstevel@tonic-gate 			return;
4257c478bd9Sstevel@tonic-gate 		}
4267c478bd9Sstevel@tonic-gate 		/* set close-on-exec flag */
4277c478bd9Sstevel@tonic-gate 		if (fcntl(fd, F_SETFD, 1) == -1)
4287c478bd9Sstevel@tonic-gate 			fatal("F_SETFD fcntl failed: %s", strerror(errno));
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate 		if (tm_checklock(fd) != 0) {
4317c478bd9Sstevel@tonic-gate 			pmptr->p_status = LOCKED;
4327c478bd9Sstevel@tonic-gate 			(void) close(fd);
4337c478bd9Sstevel@tonic-gate 			Nlocked++;
4347c478bd9Sstevel@tonic-gate 			if (Nlocked == 1) {
4357c478bd9Sstevel@tonic-gate 				sigact.sa_flags = 0;
4367c478bd9Sstevel@tonic-gate 				sigact.sa_handler = sigalarm;
4377c478bd9Sstevel@tonic-gate 				(void) sigemptyset(&sigact.sa_mask);
4387c478bd9Sstevel@tonic-gate 				(void) sigaction(SIGALRM, &sigact, NULL);
4397c478bd9Sstevel@tonic-gate 				(void) alarm(ALARMTIME);
4407c478bd9Sstevel@tonic-gate 			}
4417c478bd9Sstevel@tonic-gate 			return;
4427c478bd9Sstevel@tonic-gate 		}
4437c478bd9Sstevel@tonic-gate 		if (check_session(fd) != 0) {
4447c478bd9Sstevel@tonic-gate 			if ((Initialized) && (pmptr->p_inservice != SESSION)) {
4457c478bd9Sstevel@tonic-gate 				log("Warning -- active session exists on <%s>",
4467c478bd9Sstevel@tonic-gate 				    pmptr->p_device);
4477c478bd9Sstevel@tonic-gate 			} else {
4487c478bd9Sstevel@tonic-gate 				/*
4497c478bd9Sstevel@tonic-gate 				 * this may happen if a service is running
4507c478bd9Sstevel@tonic-gate 				 * and ttymon dies and is restarted,
4517c478bd9Sstevel@tonic-gate 				 * or another process is running on the
4527c478bd9Sstevel@tonic-gate 				 * port.
4537c478bd9Sstevel@tonic-gate 				 */
4547c478bd9Sstevel@tonic-gate 				pmptr->p_status = SESSION;
4557c478bd9Sstevel@tonic-gate 				pmptr->p_inservice = 0;
4567c478bd9Sstevel@tonic-gate 				(void) close(fd);
4577c478bd9Sstevel@tonic-gate 				Nlocked++;
4587c478bd9Sstevel@tonic-gate 				if (Nlocked == 1) {
4597c478bd9Sstevel@tonic-gate 					sigact.sa_flags = 0;
4607c478bd9Sstevel@tonic-gate 					sigact.sa_handler = sigalarm;
4617c478bd9Sstevel@tonic-gate 					(void) sigemptyset(&sigact.sa_mask);
4627c478bd9Sstevel@tonic-gate 					(void) sigaction(SIGALRM, &sigact,
4637c478bd9Sstevel@tonic-gate 					    NULL);
4647c478bd9Sstevel@tonic-gate 					(void) alarm(ALARMTIME);
4657c478bd9Sstevel@tonic-gate 				}
4667c478bd9Sstevel@tonic-gate 				return;
4677c478bd9Sstevel@tonic-gate 			}
4687c478bd9Sstevel@tonic-gate 		}
4697c478bd9Sstevel@tonic-gate 		pmptr->p_inservice = 0;
4707c478bd9Sstevel@tonic-gate 	}
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate 	if (pmptr->p_ttyflags & H_FLAG) {
4737c478bd9Sstevel@tonic-gate 		/* drop DTR */
4747c478bd9Sstevel@tonic-gate 		(void) hang_up_line(fd);
4757c478bd9Sstevel@tonic-gate 		/*
4767c478bd9Sstevel@tonic-gate 		 * After hang_up_line, the stream is in STRHUP state.
4777c478bd9Sstevel@tonic-gate 		 * We need to do another open to reinitialize streams
4787c478bd9Sstevel@tonic-gate 		 * then we can close one fd
4797c478bd9Sstevel@tonic-gate 		 */
4807c478bd9Sstevel@tonic-gate 		if ((tmpfd = open(pmptr->p_device, O_RDWR|O_NONBLOCK)) == -1) {
4817c478bd9Sstevel@tonic-gate 			log("open (%s) failed: %s", pmptr->p_device,
4827c478bd9Sstevel@tonic-gate 			    strerror(errno));
4837c478bd9Sstevel@tonic-gate 			Retry = TRUE;
4847c478bd9Sstevel@tonic-gate 			(void) close(fd);
4857c478bd9Sstevel@tonic-gate 			return;
4867c478bd9Sstevel@tonic-gate 		}
4877c478bd9Sstevel@tonic-gate 		(void) close(tmpfd);
4887c478bd9Sstevel@tonic-gate 	}
4897c478bd9Sstevel@tonic-gate 
4907c478bd9Sstevel@tonic-gate #ifdef DEBUG
4917c478bd9Sstevel@tonic-gate 	debug("open_device (%s), fd = %d", pmptr->p_device, fd);
4927c478bd9Sstevel@tonic-gate #endif
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate 	/* Change ownership of the tty line to root/uucp and */
4957c478bd9Sstevel@tonic-gate 	/* set protections to only allow root/uucp to read the line. */
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate 	if (pmptr->p_ttyflags & (B_FLAG|C_FLAG))
4987c478bd9Sstevel@tonic-gate 		(void) fchown(fd, Uucp_uid, Tty_gid);
4997c478bd9Sstevel@tonic-gate 	else
5007c478bd9Sstevel@tonic-gate 		(void) fchown(fd, ROOTUID, Tty_gid);
5017c478bd9Sstevel@tonic-gate 	(void) fchmod(fd, 0620);
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate 	if ((pmptr->p_modules != NULL)&&(*(pmptr->p_modules) != '\0')) {
5047c478bd9Sstevel@tonic-gate 		if (push_linedisc(fd, pmptr->p_modules, pmptr->p_device)
5057c478bd9Sstevel@tonic-gate 		    == -1) {
5067c478bd9Sstevel@tonic-gate 			Retry = TRUE;
5077c478bd9Sstevel@tonic-gate 			(void) close(fd);
5087c478bd9Sstevel@tonic-gate 			return;
5097c478bd9Sstevel@tonic-gate 		}
5107c478bd9Sstevel@tonic-gate 	}
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate 	if (initial_termio(fd, pmptr) == -1)  {
5137c478bd9Sstevel@tonic-gate 		Retry = TRUE;
5147c478bd9Sstevel@tonic-gate 		(void) close(fd);
5157c478bd9Sstevel@tonic-gate 		return;
5167c478bd9Sstevel@tonic-gate 	}
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate 	di_devperm_logout((const char *)pmptr->p_device);
5197c478bd9Sstevel@tonic-gate 	pmptr->p_fd = fd;
5207c478bd9Sstevel@tonic-gate }
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate /*
5237c478bd9Sstevel@tonic-gate  *	set_poll(fdp)	- put all fd's in a pollfd array
5247c478bd9Sstevel@tonic-gate  *			- set poll event to POLLIN and POLLMSG
5257c478bd9Sstevel@tonic-gate  *			- return number of fd to be polled
5267c478bd9Sstevel@tonic-gate  */
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate static	int
set_poll(fdp)5297c478bd9Sstevel@tonic-gate set_poll(fdp)
5307c478bd9Sstevel@tonic-gate struct pollfd *fdp;
5317c478bd9Sstevel@tonic-gate {
5327c478bd9Sstevel@tonic-gate 	struct	pmtab	*tp;
5337c478bd9Sstevel@tonic-gate 	int 	nfd = 0;
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate 	for (tp = PMtab; tp; tp = tp->p_next) {
5367c478bd9Sstevel@tonic-gate 		if (tp->p_fd > 0)  {
5377c478bd9Sstevel@tonic-gate 			fdp->fd = tp->p_fd;
5387c478bd9Sstevel@tonic-gate 			fdp->events = POLLIN;
5397c478bd9Sstevel@tonic-gate 			fdp++;
5407c478bd9Sstevel@tonic-gate 			nfd++;
5417c478bd9Sstevel@tonic-gate 		}
5427c478bd9Sstevel@tonic-gate 	}
5437c478bd9Sstevel@tonic-gate 	return (nfd);
5447c478bd9Sstevel@tonic-gate }
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate /*
5477c478bd9Sstevel@tonic-gate  *	check_spawnlimit	- return 0 if spawnlimit is not reached
5487c478bd9Sstevel@tonic-gate  *				- otherwise return -1
5497c478bd9Sstevel@tonic-gate  */
5507c478bd9Sstevel@tonic-gate static	int
check_spawnlimit(pmptr)5517c478bd9Sstevel@tonic-gate check_spawnlimit(pmptr)
5527c478bd9Sstevel@tonic-gate struct	pmtab	*pmptr;
5537c478bd9Sstevel@tonic-gate {
5547c478bd9Sstevel@tonic-gate 	time_t	now;
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate 	(void) time(&now);
5577c478bd9Sstevel@tonic-gate 	if (pmptr->p_time == 0L)
5587c478bd9Sstevel@tonic-gate 		pmptr->p_time = now;
5597c478bd9Sstevel@tonic-gate 	if (pmptr->p_respawn >= SPAWN_LIMIT) {
5607c478bd9Sstevel@tonic-gate 		if ((now - pmptr->p_time) < SPAWN_INTERVAL) {
5617c478bd9Sstevel@tonic-gate 			pmptr->p_time = now;
5627c478bd9Sstevel@tonic-gate 			pmptr->p_respawn = 0;
5637c478bd9Sstevel@tonic-gate 			return (-1);
5647c478bd9Sstevel@tonic-gate 		}
5657c478bd9Sstevel@tonic-gate 		pmptr->p_time = now;
5667c478bd9Sstevel@tonic-gate 		pmptr->p_respawn = 0;
5677c478bd9Sstevel@tonic-gate 	}
5687c478bd9Sstevel@tonic-gate 	pmptr->p_respawn++;
5697c478bd9Sstevel@tonic-gate 	return (0);
5707c478bd9Sstevel@tonic-gate }
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate /*
5737c478bd9Sstevel@tonic-gate  * mod_ttydefs	- to check if /etc/ttydefs has been modified
5747c478bd9Sstevel@tonic-gate  *		- return TRUE if file modified
5757c478bd9Sstevel@tonic-gate  *		- otherwise, return FALSE
5767c478bd9Sstevel@tonic-gate  */
5777c478bd9Sstevel@tonic-gate static	int
mod_ttydefs()5787c478bd9Sstevel@tonic-gate mod_ttydefs()
5797c478bd9Sstevel@tonic-gate {
5807c478bd9Sstevel@tonic-gate 	struct	stat	statbuf;
5817c478bd9Sstevel@tonic-gate 	extern	long	Mtime;
5827c478bd9Sstevel@tonic-gate 	if (stat(TTYDEFS, &statbuf) == -1) {
5837c478bd9Sstevel@tonic-gate 		/* if stat failed, don't bother reread ttydefs */
5847c478bd9Sstevel@tonic-gate 		return (FALSE);
5857c478bd9Sstevel@tonic-gate 	}
5867c478bd9Sstevel@tonic-gate 	if ((long)statbuf.st_mtime != Mtime) {
5877c478bd9Sstevel@tonic-gate 		Mtime = (long)statbuf.st_mtime;
5887c478bd9Sstevel@tonic-gate 		return (TRUE);
5897c478bd9Sstevel@tonic-gate 	}
5907c478bd9Sstevel@tonic-gate 	return (FALSE);
5917c478bd9Sstevel@tonic-gate }
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate /*
5947c478bd9Sstevel@tonic-gate  *	free_defs - free the Gdef table
5957c478bd9Sstevel@tonic-gate  */
5967c478bd9Sstevel@tonic-gate static	void
free_defs()5977c478bd9Sstevel@tonic-gate free_defs()
5987c478bd9Sstevel@tonic-gate {
5997c478bd9Sstevel@tonic-gate 	int	i;
6007c478bd9Sstevel@tonic-gate 	struct	Gdef	*tp;
6017c478bd9Sstevel@tonic-gate 	tp = &Gdef[0];
6027c478bd9Sstevel@tonic-gate 	for (i = 0; i < Ndefs; i++, tp++) {
6037c478bd9Sstevel@tonic-gate 		free(tp->g_id);
6047c478bd9Sstevel@tonic-gate 		free(tp->g_iflags);
6057c478bd9Sstevel@tonic-gate 		free(tp->g_fflags);
6067c478bd9Sstevel@tonic-gate 		free(tp->g_nextid);
6077c478bd9Sstevel@tonic-gate 		tp->g_id = NULL;
6087c478bd9Sstevel@tonic-gate 		tp->g_iflags = NULL;
6097c478bd9Sstevel@tonic-gate 		tp->g_fflags = NULL;
6107c478bd9Sstevel@tonic-gate 		tp->g_nextid = NULL;
6117c478bd9Sstevel@tonic-gate 	}
6127c478bd9Sstevel@tonic-gate 	Ndefs = 0;
6137c478bd9Sstevel@tonic-gate }
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate /*
6167c478bd9Sstevel@tonic-gate  * struct Gdef *get_speed(ttylabel)
6177c478bd9Sstevel@tonic-gate  *	- search "/etc/ttydefs" for speed and term. specification
6187c478bd9Sstevel@tonic-gate  *	  using "ttylabel". If "ttylabel" is NULL, default
6197c478bd9Sstevel@tonic-gate  *	  to DEFAULT
6207c478bd9Sstevel@tonic-gate  * arg:	  ttylabel - label/id of speed settings.
6217c478bd9Sstevel@tonic-gate  */
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate struct Gdef *
get_speed(char * ttylabel)6247c478bd9Sstevel@tonic-gate get_speed(char *ttylabel)
6257c478bd9Sstevel@tonic-gate {
6267c478bd9Sstevel@tonic-gate 	register struct Gdef *sp;
6277c478bd9Sstevel@tonic-gate 	extern   struct Gdef DEFAULT;
6287c478bd9Sstevel@tonic-gate 
6297c478bd9Sstevel@tonic-gate 	if ((ttylabel != NULL) && (*ttylabel != '\0')) {
6307c478bd9Sstevel@tonic-gate 		if ((sp = find_def(ttylabel)) == NULL) {
6317c478bd9Sstevel@tonic-gate 			log("unable to find <%s> in \"%s\"", ttylabel, TTYDEFS);
6327c478bd9Sstevel@tonic-gate 			sp = &DEFAULT; /* use default */
6337c478bd9Sstevel@tonic-gate 		}
6347c478bd9Sstevel@tonic-gate 	} else sp = &DEFAULT; /* use default */
6357c478bd9Sstevel@tonic-gate 	return (sp);
6367c478bd9Sstevel@tonic-gate }
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate /*
6397c478bd9Sstevel@tonic-gate  * setup_PCpipe()	- setup the pipe between Parent and Children
6407c478bd9Sstevel@tonic-gate  *			- the pipe is used for a tmchild to send its
6417c478bd9Sstevel@tonic-gate  *			  pid to inform ttymon that it is about to
6427c478bd9Sstevel@tonic-gate  *			  invoke service
6437c478bd9Sstevel@tonic-gate  *			- the pipe also serves as a mean for tmchild
6447c478bd9Sstevel@tonic-gate  *			  to detect failure of ttymon
6457c478bd9Sstevel@tonic-gate  */
6467c478bd9Sstevel@tonic-gate void
setup_PCpipe()6477c478bd9Sstevel@tonic-gate setup_PCpipe()
6487c478bd9Sstevel@tonic-gate {
6497c478bd9Sstevel@tonic-gate 	int	flag = 0;
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate 	if (pipe(PCpipe) == -1)
6527c478bd9Sstevel@tonic-gate 		fatal("pipe() failed: %s", strerror(errno));
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate 	/* set close-on-exec flag */
6557c478bd9Sstevel@tonic-gate 	if (fcntl(PCpipe[0], F_SETFD, 1) == -1)
6567c478bd9Sstevel@tonic-gate 		fatal("F_SETFD fcntl failed: %s", strerror(errno));
6577c478bd9Sstevel@tonic-gate 
6587c478bd9Sstevel@tonic-gate 	if (fcntl(PCpipe[1], F_SETFD, 1) == -1)
6597c478bd9Sstevel@tonic-gate 		fatal("F_SETFD fcntl failed: %s", strerror(errno));
6607c478bd9Sstevel@tonic-gate 
6617c478bd9Sstevel@tonic-gate 	/* set O_NONBLOCK flag */
6627c478bd9Sstevel@tonic-gate 	if (fcntl(PCpipe[0], F_GETFL, flag) == -1)
6637c478bd9Sstevel@tonic-gate 		fatal("F_GETFL failed: %s", strerror(errno));
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate 	flag |= O_NONBLOCK;
6667c478bd9Sstevel@tonic-gate 	if (fcntl(PCpipe[0], F_SETFL, flag) == -1)
6677c478bd9Sstevel@tonic-gate 		fatal("F_SETFL failed: %s", strerror(errno));
6687c478bd9Sstevel@tonic-gate 
6697c478bd9Sstevel@tonic-gate 	/* set message discard mode */
6707c478bd9Sstevel@tonic-gate 	if (ioctl(PCpipe[0], I_SRDOPT, RMSGD) == -1)
6717c478bd9Sstevel@tonic-gate 		fatal("I_SRDOPT RMSGD failed: %s", strerror(errno));
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate 	/* register to receive SIGPOLL when data come */
6747c478bd9Sstevel@tonic-gate 	if (ioctl(PCpipe[0], I_SETSIG, S_INPUT) == -1)
6757c478bd9Sstevel@tonic-gate 		fatal("I_SETSIG S_INPUT failed: %s", strerror(errno));
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate #ifdef 	DEBUG
6787c478bd9Sstevel@tonic-gate 	log("PCpipe[0]\t = %d", PCpipe[0]);
6797c478bd9Sstevel@tonic-gate 	log("PCpipe[1]\t = %d", PCpipe[1]);
6807c478bd9Sstevel@tonic-gate #endif
6817c478bd9Sstevel@tonic-gate }
682