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