xref: /illumos-gate/usr/src/cmd/ttymon/ttymon.c (revision 902bba37)
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	*/
273bb2c156SToomas Soome /*	  All Rights Reserved	*/
287c478bd9Sstevel@tonic-gate 
29*902bba37SToomas Soome #include <ctype.h>
30004388ebScasper #include <stdio_ext.h>
317c478bd9Sstevel@tonic-gate #include <stdlib.h>
327c478bd9Sstevel@tonic-gate #include <fcntl.h>
337c478bd9Sstevel@tonic-gate #include <errno.h>
347c478bd9Sstevel@tonic-gate #include <poll.h>
357c478bd9Sstevel@tonic-gate #include <string.h>
367c478bd9Sstevel@tonic-gate #include <signal.h>
377c478bd9Sstevel@tonic-gate #include <sys/types.h>
387c478bd9Sstevel@tonic-gate #include <sys/stat.h>
397c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
407c478bd9Sstevel@tonic-gate #include <sys/resource.h>
417c478bd9Sstevel@tonic-gate #include <sys/termios.h>
427c478bd9Sstevel@tonic-gate #include <pwd.h>
437c478bd9Sstevel@tonic-gate #include <grp.h>
447c478bd9Sstevel@tonic-gate #include <unistd.h>
457c478bd9Sstevel@tonic-gate #include <ulimit.h>
463bb2c156SToomas Soome #include <libdevinfo.h>
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate #include "sac.h"
497c478bd9Sstevel@tonic-gate #include "ttymon.h"
507c478bd9Sstevel@tonic-gate #include "tmstruct.h"
517c478bd9Sstevel@tonic-gate #include "tmextern.h"
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate static	int	Initialized;
547c478bd9Sstevel@tonic-gate 
553bb2c156SToomas Soome static	void	initialize(void);
563bb2c156SToomas Soome static	void	open_all(void);
573bb2c156SToomas Soome static	int	set_poll(struct pollfd *);
583bb2c156SToomas Soome static	int	check_spawnlimit(struct pmtab *);
593bb2c156SToomas Soome static	int	mod_ttydefs(void);
607c478bd9Sstevel@tonic-gate 
613bb2c156SToomas Soome void	open_device(struct pmtab *);
623bb2c156SToomas Soome void	set_softcar(struct pmtab *);
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate /*
653bb2c156SToomas Soome  *	ttymon	- a port monitor under SAC
667c478bd9Sstevel@tonic-gate  *		- monitor ports, set terminal modes, baud rate
677c478bd9Sstevel@tonic-gate  *		  and line discipline for the port
687c478bd9Sstevel@tonic-gate  *		- invoke service on port if connection request received
697c478bd9Sstevel@tonic-gate  *		- Usage: ttymon
707c478bd9Sstevel@tonic-gate  *			 ttymon -g [options]
717c478bd9Sstevel@tonic-gate  *			 Valid options are
727c478bd9Sstevel@tonic-gate  *			 -h
737c478bd9Sstevel@tonic-gate  *			 -d device
747c478bd9Sstevel@tonic-gate  *			 -l ttylabel
757c478bd9Sstevel@tonic-gate  *			 -t timeout
767c478bd9Sstevel@tonic-gate  *			 -m modules
777c478bd9Sstevel@tonic-gate  *			 -p prompt
787c478bd9Sstevel@tonic-gate  *
797c478bd9Sstevel@tonic-gate  *		- ttymon without args is invoked by SAC
807c478bd9Sstevel@tonic-gate  *		- ttymon -g is invoked by process that needs to
817c478bd9Sstevel@tonic-gate  *		  have login service on the fly
827c478bd9Sstevel@tonic-gate  */
837c478bd9Sstevel@tonic-gate 
8434e48580Sdp int
main(int argc,char * argv[])8534e48580Sdp main(int argc, char *argv[])
867c478bd9Sstevel@tonic-gate {
877c478bd9Sstevel@tonic-gate 	int	nfds;
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 	/*
907c478bd9Sstevel@tonic-gate 	 * Only the superuser should execute this command.
917c478bd9Sstevel@tonic-gate 	 */
927c478bd9Sstevel@tonic-gate 	if (getuid() != 0)
93b77c815bSNobutomo Nakano 		return (1);
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate 	/* remember original signal mask and dispositions */
967c478bd9Sstevel@tonic-gate 	(void) sigprocmask(SIG_SETMASK, NULL, &Origmask);
97b77c815bSNobutomo Nakano 	(void) sigaction(SIGINT, NULL, &Sigint);
98b77c815bSNobutomo Nakano 	(void) sigaction(SIGALRM, NULL, &Sigalrm);
99b77c815bSNobutomo Nakano 	(void) sigaction(SIGPOLL, NULL, &Sigpoll);
100b77c815bSNobutomo Nakano 	(void) sigaction(SIGQUIT, NULL, &Sigquit);
101b77c815bSNobutomo Nakano 	(void) sigaction(SIGCLD, NULL, &Sigcld);
102b77c815bSNobutomo Nakano 	(void) sigaction(SIGTERM, NULL, &Sigterm);
1037c478bd9Sstevel@tonic-gate #ifdef	DEBUG
104b77c815bSNobutomo Nakano 	(void) sigaction(SIGUSR1, NULL, &Sigusr1);
105b77c815bSNobutomo Nakano 	(void) sigaction(SIGUSR2, NULL, &Sigusr2);
1067c478bd9Sstevel@tonic-gate #endif
107b77c815bSNobutomo Nakano 
108b77c815bSNobutomo Nakano 	/*
109b77c815bSNobutomo Nakano 	 * SIGQUIT needs to be ignored. Otherwise, hitting ^\ from
110b77c815bSNobutomo Nakano 	 * console kills ttymon.
111b77c815bSNobutomo Nakano 	 */
112b77c815bSNobutomo Nakano 	(void) signal(SIGQUIT, SIG_IGN);
113b77c815bSNobutomo Nakano 
114b77c815bSNobutomo Nakano 	if ((argc > 1) || (strcmp(lastname(argv[0]), "getty") == 0)) {
115b77c815bSNobutomo Nakano 		ttymon_express(argc, argv);
116b77c815bSNobutomo Nakano 		return (1);	/*NOTREACHED*/
117b77c815bSNobutomo Nakano 	}
118b77c815bSNobutomo Nakano 
1197c478bd9Sstevel@tonic-gate 	initialize();
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 	for (;;) {
1227c478bd9Sstevel@tonic-gate 		nfds = set_poll(Pollp);
1237c478bd9Sstevel@tonic-gate 		if (!Reread_flag) {
1247c478bd9Sstevel@tonic-gate 			if (nfds > 0)
1257c478bd9Sstevel@tonic-gate 				do_poll(Pollp, nfds);
1267c478bd9Sstevel@tonic-gate 			else
1277c478bd9Sstevel@tonic-gate 				(void) pause();
1287c478bd9Sstevel@tonic-gate 		}
1297c478bd9Sstevel@tonic-gate 		/*
1307c478bd9Sstevel@tonic-gate 		 * READDB messages may arrive during poll or pause.
1317c478bd9Sstevel@tonic-gate 		 * So the flag needs to be checked again.
1327c478bd9Sstevel@tonic-gate 		 */
1337c478bd9Sstevel@tonic-gate 		if (Reread_flag) {
1347c478bd9Sstevel@tonic-gate 			Reread_flag = FALSE;
1357c478bd9Sstevel@tonic-gate 			re_read();
1367c478bd9Sstevel@tonic-gate 		}
1377c478bd9Sstevel@tonic-gate 		while (Retry) {
1387c478bd9Sstevel@tonic-gate 			Retry = FALSE;
1397c478bd9Sstevel@tonic-gate 			open_all();
1407c478bd9Sstevel@tonic-gate 		}
1417c478bd9Sstevel@tonic-gate 	}
1427c478bd9Sstevel@tonic-gate }
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate static	void
initialize(void)1453bb2c156SToomas Soome initialize(void)
1467c478bd9Sstevel@tonic-gate {
1477c478bd9Sstevel@tonic-gate 	struct	pmtab	*tp;
1483bb2c156SToomas Soome 	struct passwd *pwdp;
1493bb2c156SToomas Soome 	struct	group	*gp;
1507c478bd9Sstevel@tonic-gate 	struct	rlimit rlimit;
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	Initialized = FALSE;
1537c478bd9Sstevel@tonic-gate 	/*
1547c478bd9Sstevel@tonic-gate 	 * get_environ() must be called first,
1557c478bd9Sstevel@tonic-gate 	 * otherwise we don't know where the log file is
1567c478bd9Sstevel@tonic-gate 	 */
1577c478bd9Sstevel@tonic-gate 	get_environ();
1587c478bd9Sstevel@tonic-gate 	openttymonlog();
1597c478bd9Sstevel@tonic-gate 	openpid();
1607c478bd9Sstevel@tonic-gate 	openpipes();
1617c478bd9Sstevel@tonic-gate 	setup_PCpipe();
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 	log("PMTAG: %s", Tag);
1647c478bd9Sstevel@tonic-gate 	log("Starting state: %s",
1657c478bd9Sstevel@tonic-gate 	    (State == PM_ENABLED) ? "enabled" : "disabled");
1667c478bd9Sstevel@tonic-gate 
1673bb2c156SToomas Soome #ifdef	DEBUG
1687c478bd9Sstevel@tonic-gate 	opendebug(FALSE);
1697c478bd9Sstevel@tonic-gate 	debug("***** ttymon in initialize *****");
1707c478bd9Sstevel@tonic-gate 	log("debug mode is \t on");
1717c478bd9Sstevel@tonic-gate #endif
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	catch_signals();
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	/* register to receive SIGPOLL when data comes to pmpipe */
1767c478bd9Sstevel@tonic-gate 	if (ioctl(Pfd, I_SETSIG, S_INPUT) < 0)
1777c478bd9Sstevel@tonic-gate 		fatal("I_SETSIG on pmpipe failed: %s", strerror(errno));
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 	sacpoll(); /* this is needed because there may be data already */
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	Maxfiles = (int)ulimit(4, 0L);	/* get max number of open files */
1827c478bd9Sstevel@tonic-gate 	if (Maxfiles < 0)
1837c478bd9Sstevel@tonic-gate 		fatal("ulimit(4,0L) failed: %s", strerror(errno));
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 	if (getrlimit(RLIMIT_NOFILE, &Rlimit) == -1)
1867c478bd9Sstevel@tonic-gate 		fatal("getrlimit failed: %s", strerror(errno));
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 	rlimit.rlim_cur = rlimit.rlim_max = Rlimit.rlim_max;
1897c478bd9Sstevel@tonic-gate 	if (setrlimit(RLIMIT_NOFILE, &rlimit) == -1)
1907c478bd9Sstevel@tonic-gate 		fatal("setrlimit failed: %s", strerror(errno));
1917c478bd9Sstevel@tonic-gate 
192004388ebScasper 	(void) enable_extended_FILE_stdio(-1, -1);
193004388ebScasper 
1947c478bd9Sstevel@tonic-gate 	Maxfiles = rlimit.rlim_cur;
1957c478bd9Sstevel@tonic-gate 	Maxfds = Maxfiles - FILE_RESERVED;
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	log("max open files = %d", Maxfiles);
1987c478bd9Sstevel@tonic-gate 	log("max ports ttymon can monitor = %d", Maxfds);
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 	read_pmtab();
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 	/*
2037c478bd9Sstevel@tonic-gate 	 * setup poll array
2043bb2c156SToomas Soome 	 *	- we allocate 10 extra pollfd so that
2057c478bd9Sstevel@tonic-gate 	 *	  we do not have to re-malloc when there is
2067c478bd9Sstevel@tonic-gate 	 *	  minor fluctuation in Nentries
2077c478bd9Sstevel@tonic-gate 	 */
2087c478bd9Sstevel@tonic-gate 	Npollfd = Nentries + 10;
2097c478bd9Sstevel@tonic-gate 	if (Npollfd > Maxfds)
2107c478bd9Sstevel@tonic-gate 		Npollfd = Maxfds;
2117c478bd9Sstevel@tonic-gate 	if ((Pollp = (struct pollfd *)
2127c478bd9Sstevel@tonic-gate 	    malloc((unsigned)(Npollfd * sizeof (struct pollfd))))
2137c478bd9Sstevel@tonic-gate 	    == (struct pollfd *)NULL)
2147c478bd9Sstevel@tonic-gate 		fatal("malloc for Pollp failed");
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 	(void) mod_ttydefs();	/* just to initialize Mtime */
2177c478bd9Sstevel@tonic-gate 	if (check_version(TTYDEFS_VERS, TTYDEFS) != 0)
2187c478bd9Sstevel@tonic-gate 		fatal("check /etc/ttydefs version failed");
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 	read_ttydefs(NULL, FALSE);
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	/* initialize global variables, Uucp_uid & Tty_gid */
2237c478bd9Sstevel@tonic-gate 	if ((pwdp = getpwnam(UUCP)) != NULL)
2247c478bd9Sstevel@tonic-gate 		Uucp_uid = pwdp->pw_uid;
2257c478bd9Sstevel@tonic-gate 	if ((gp = getgrnam(TTY)) == NULL)
2267c478bd9Sstevel@tonic-gate 		log("no group entry for <tty>, default is used");
2277c478bd9Sstevel@tonic-gate 	else
2287c478bd9Sstevel@tonic-gate 		Tty_gid = gp->gr_gid;
2297c478bd9Sstevel@tonic-gate 	endgrent();
2307c478bd9Sstevel@tonic-gate 	endpwent();
2317c478bd9Sstevel@tonic-gate #ifdef	DEBUG
232f48205beScasper 	debug("Uucp_uid = %u, Tty_gid = %u", Uucp_uid, Tty_gid);
2337c478bd9Sstevel@tonic-gate #endif
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 	log("Initialization Completed");
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 	/* open the devices ttymon monitors */
2387c478bd9Sstevel@tonic-gate 	Retry = TRUE;
2397c478bd9Sstevel@tonic-gate 	while (Retry) {
2407c478bd9Sstevel@tonic-gate 		Retry = FALSE;
2417c478bd9Sstevel@tonic-gate 		for (tp = PMtab; tp; tp = tp->p_next) {
2427c478bd9Sstevel@tonic-gate 			if ((tp->p_status > 0) && (tp->p_fd == 0) &&
2433bb2c156SToomas Soome 			    (tp->p_childpid == 0) &&
2443bb2c156SToomas Soome 			    !(tp->p_ttyflags & I_FLAG) &&
2457c478bd9Sstevel@tonic-gate 			    (!((State == PM_DISABLED) &&
2467c478bd9Sstevel@tonic-gate 			    ((tp->p_dmsg == NULL)||(*(tp->p_dmsg) == '\0'))))) {
2477c478bd9Sstevel@tonic-gate 				open_device(tp);
2487c478bd9Sstevel@tonic-gate 				if (tp->p_fd > 0)
2497c478bd9Sstevel@tonic-gate 					got_carrier(tp);
2507c478bd9Sstevel@tonic-gate 			}
2517c478bd9Sstevel@tonic-gate 		}
2527c478bd9Sstevel@tonic-gate 	}
2537c478bd9Sstevel@tonic-gate 	Initialized = TRUE;
2547c478bd9Sstevel@tonic-gate }
2557c478bd9Sstevel@tonic-gate 
2563bb2c156SToomas Soome static	void	free_defs(void);
257d67944fbSScott Rotondo 
2587c478bd9Sstevel@tonic-gate /*
2597c478bd9Sstevel@tonic-gate  *	open_all - open devices in pmtab if the entry is
2607c478bd9Sstevel@tonic-gate  *	         - valid, fd = 0, and pid = 0
2617c478bd9Sstevel@tonic-gate  */
2627c478bd9Sstevel@tonic-gate static void
open_all(void)2633bb2c156SToomas Soome open_all(void)
2647c478bd9Sstevel@tonic-gate {
2657c478bd9Sstevel@tonic-gate 	struct	pmtab	*tp;
2667c478bd9Sstevel@tonic-gate 	int	check_modtime;
2677c478bd9Sstevel@tonic-gate 	sigset_t cset;
2687c478bd9Sstevel@tonic-gate 	sigset_t tset;
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate #ifdef	DEBUG
2717c478bd9Sstevel@tonic-gate 	debug("in open_all");
2727c478bd9Sstevel@tonic-gate #endif
2737c478bd9Sstevel@tonic-gate 	check_modtime = TRUE;
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 	for (tp = PMtab; tp; tp = tp->p_next) {
2767c478bd9Sstevel@tonic-gate 		if ((tp->p_status > 0) && (tp->p_fd == 0) &&
2773bb2c156SToomas Soome 		    (tp->p_childpid == 0) &&
2787c478bd9Sstevel@tonic-gate 		    !(tp->p_ttyflags & I_FLAG) && (!((State == PM_DISABLED) &&
2797c478bd9Sstevel@tonic-gate 		    ((tp->p_dmsg == NULL)||(*(tp->p_dmsg) == '\0'))))) {
2807c478bd9Sstevel@tonic-gate 			/*
2817c478bd9Sstevel@tonic-gate 			 * if we have not check modification time and
2827c478bd9Sstevel@tonic-gate 			 * /etc/ttydefs was modified, need to re-read it
2837c478bd9Sstevel@tonic-gate 			 */
2847c478bd9Sstevel@tonic-gate 			if (check_modtime && mod_ttydefs()) {
2857c478bd9Sstevel@tonic-gate 				check_modtime = FALSE;
2867c478bd9Sstevel@tonic-gate 				(void) sigprocmask(SIG_SETMASK, NULL, &cset);
2877c478bd9Sstevel@tonic-gate 				tset = cset;
2887c478bd9Sstevel@tonic-gate 				(void) sigaddset(&tset, SIGCLD);
2897c478bd9Sstevel@tonic-gate 				(void) sigprocmask(SIG_SETMASK, &tset, NULL);
2907c478bd9Sstevel@tonic-gate 				free_defs();
2917c478bd9Sstevel@tonic-gate #ifdef	DEBUG
2927c478bd9Sstevel@tonic-gate 				debug("/etc/ttydefs is modified, re-read it");
2937c478bd9Sstevel@tonic-gate #endif
2947c478bd9Sstevel@tonic-gate 				read_ttydefs(NULL, FALSE);
2957c478bd9Sstevel@tonic-gate 				(void) sigprocmask(SIG_SETMASK, &cset, NULL);
2967c478bd9Sstevel@tonic-gate 			}
2977c478bd9Sstevel@tonic-gate 			open_device(tp);
2987c478bd9Sstevel@tonic-gate 			if (tp->p_fd > 0)
2997c478bd9Sstevel@tonic-gate 				got_carrier(tp);
3007c478bd9Sstevel@tonic-gate 		} else if (((tp->p_status == LOCKED) ||
3017c478bd9Sstevel@tonic-gate 		    (tp->p_status == SESSION) ||
3027c478bd9Sstevel@tonic-gate 		    (tp->p_status == UNACCESS)) &&
3037c478bd9Sstevel@tonic-gate 		    (tp->p_fd > 0) &&
3047c478bd9Sstevel@tonic-gate 		    (!((State == PM_DISABLED) &&
3057c478bd9Sstevel@tonic-gate 		    ((tp->p_dmsg == NULL)||(*(tp->p_dmsg) == '\0'))))) {
3067c478bd9Sstevel@tonic-gate 			if (check_modtime && mod_ttydefs()) {
3077c478bd9Sstevel@tonic-gate 				check_modtime = FALSE;
3087c478bd9Sstevel@tonic-gate 				(void) sigprocmask(SIG_SETMASK, NULL, &cset);
3097c478bd9Sstevel@tonic-gate 				tset = cset;
3107c478bd9Sstevel@tonic-gate 				(void) sigaddset(&tset, SIGCLD);
3117c478bd9Sstevel@tonic-gate 				(void) sigprocmask(SIG_SETMASK, &tset, NULL);
3127c478bd9Sstevel@tonic-gate 				free_defs();
3137c478bd9Sstevel@tonic-gate #ifdef	DEBUG
3147c478bd9Sstevel@tonic-gate 				debug("/etc/ttydefs is modified, re-read it");
3157c478bd9Sstevel@tonic-gate #endif
3167c478bd9Sstevel@tonic-gate 				read_ttydefs(NULL, FALSE);
3177c478bd9Sstevel@tonic-gate 				(void) sigprocmask(SIG_SETMASK, &cset, NULL);
3187c478bd9Sstevel@tonic-gate 			}
3197c478bd9Sstevel@tonic-gate 			tp->p_status = VALID;
3207c478bd9Sstevel@tonic-gate 			open_device(tp);
3217c478bd9Sstevel@tonic-gate 			if (tp->p_fd > 0)
3227c478bd9Sstevel@tonic-gate 				got_carrier(tp);
3237c478bd9Sstevel@tonic-gate 		}
3247c478bd9Sstevel@tonic-gate 	}
3257c478bd9Sstevel@tonic-gate }
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate void
set_softcar(struct pmtab * pmptr)3283bb2c156SToomas Soome set_softcar(struct pmtab *pmptr)
3297c478bd9Sstevel@tonic-gate {
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 	int fd, val = 0;
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate #ifdef	DEBUG
3347c478bd9Sstevel@tonic-gate 	debug("in set_softcar");
3357c478bd9Sstevel@tonic-gate #endif
3367c478bd9Sstevel@tonic-gate 	/*
3377c478bd9Sstevel@tonic-gate 	 * If soft carrier is not set one way or
3387c478bd9Sstevel@tonic-gate 	 * the other, leave it alone.
3397c478bd9Sstevel@tonic-gate 	 */
3407c478bd9Sstevel@tonic-gate 	if (*pmptr->p_softcar == '\0')
3417c478bd9Sstevel@tonic-gate 		return;
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 	if (*pmptr->p_softcar == 'y')
3447c478bd9Sstevel@tonic-gate 		val = 1;
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate 	if ((fd = open(pmptr->p_device, O_RDONLY|O_NONBLOCK|O_NOCTTY)) < 0) {
3477c478bd9Sstevel@tonic-gate 		log("open (%s) failed: %s", pmptr->p_device, strerror(errno));
3487c478bd9Sstevel@tonic-gate 		return;
3497c478bd9Sstevel@tonic-gate 	}
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 	if (ioctl(fd, TIOCSSOFTCAR, &val) < 0)
3527c478bd9Sstevel@tonic-gate 		log("set soft-carrier (%s) failed: %s", pmptr->p_device,
3537c478bd9Sstevel@tonic-gate 		    strerror(errno));
3547c478bd9Sstevel@tonic-gate 
3553bb2c156SToomas Soome 	(void) close(fd);
3567c478bd9Sstevel@tonic-gate }
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate /*
3607c478bd9Sstevel@tonic-gate  *	open_device(pmptr)	- open the device
3617c478bd9Sstevel@tonic-gate  *				- check device lock
3627c478bd9Sstevel@tonic-gate  *				- change owner of device
3637c478bd9Sstevel@tonic-gate  *				- push line disciplines
3647c478bd9Sstevel@tonic-gate  *				- set termio
3657c478bd9Sstevel@tonic-gate  */
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate void
open_device(struct pmtab * pmptr)3683bb2c156SToomas Soome open_device(struct pmtab *pmptr)
3697c478bd9Sstevel@tonic-gate {
3707c478bd9Sstevel@tonic-gate 	int	fd, tmpfd;
3717c478bd9Sstevel@tonic-gate 	struct	sigaction	sigact;
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate #ifdef	DEBUG
3747c478bd9Sstevel@tonic-gate 	debug("in open_device");
3757c478bd9Sstevel@tonic-gate #endif
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate 	if (pmptr->p_status == GETTY) {
3787c478bd9Sstevel@tonic-gate 		revokedevaccess(pmptr->p_device, 0, 0, 0);
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 		if ((fd = open(pmptr->p_device, O_RDWR)) == -1)
3817c478bd9Sstevel@tonic-gate 			fatal("open (%s) failed: %s", pmptr->p_device,
3827c478bd9Sstevel@tonic-gate 			    strerror(errno));
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 	} else {
3857c478bd9Sstevel@tonic-gate 		if (check_spawnlimit(pmptr) == -1) {
3867c478bd9Sstevel@tonic-gate 			pmptr->p_status = NOTVALID;
3877c478bd9Sstevel@tonic-gate 			log("service <%s> is respawning too rapidly",
3887c478bd9Sstevel@tonic-gate 			    pmptr->p_tag);
3897c478bd9Sstevel@tonic-gate 			return;
3907c478bd9Sstevel@tonic-gate 		}
3917c478bd9Sstevel@tonic-gate 		if (pmptr->p_fd > 0) { /* file already open */
3927c478bd9Sstevel@tonic-gate 			fd = pmptr->p_fd;
3937c478bd9Sstevel@tonic-gate 			pmptr->p_fd = 0;
3947c478bd9Sstevel@tonic-gate 		} else if ((fd = open(pmptr->p_device, O_RDWR|O_NONBLOCK))
3957c478bd9Sstevel@tonic-gate 		    == -1) {
3967c478bd9Sstevel@tonic-gate 			log("open (%s) failed: %s", pmptr->p_device,
3977c478bd9Sstevel@tonic-gate 			    strerror(errno));
3987c478bd9Sstevel@tonic-gate 			if ((errno ==  ENODEV) || (errno == EBUSY)) {
3997c478bd9Sstevel@tonic-gate 				pmptr->p_status = UNACCESS;
4007c478bd9Sstevel@tonic-gate 				Nlocked++;
4017c478bd9Sstevel@tonic-gate 				if (Nlocked == 1) {
4023bb2c156SToomas Soome 					sigact.sa_flags = 0;
4033bb2c156SToomas Soome 					sigact.sa_handler = sigalarm;
4043bb2c156SToomas Soome 					(void) sigemptyset(&sigact.sa_mask);
4053bb2c156SToomas Soome 					(void) sigaction(SIGALRM, &sigact,
4063bb2c156SToomas Soome 					    NULL);
4073bb2c156SToomas Soome 					(void) alarm(ALARMTIME);
4087c478bd9Sstevel@tonic-gate 				}
4097c478bd9Sstevel@tonic-gate 			} else
4107c478bd9Sstevel@tonic-gate 				Retry = TRUE;
4117c478bd9Sstevel@tonic-gate 			return;
4127c478bd9Sstevel@tonic-gate 		}
4137c478bd9Sstevel@tonic-gate 		/* set close-on-exec flag */
4147c478bd9Sstevel@tonic-gate 		if (fcntl(fd, F_SETFD, 1) == -1)
4157c478bd9Sstevel@tonic-gate 			fatal("F_SETFD fcntl failed: %s", strerror(errno));
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 		if (tm_checklock(fd) != 0) {
4187c478bd9Sstevel@tonic-gate 			pmptr->p_status = LOCKED;
4197c478bd9Sstevel@tonic-gate 			(void) close(fd);
4207c478bd9Sstevel@tonic-gate 			Nlocked++;
4217c478bd9Sstevel@tonic-gate 			if (Nlocked == 1) {
4227c478bd9Sstevel@tonic-gate 				sigact.sa_flags = 0;
4237c478bd9Sstevel@tonic-gate 				sigact.sa_handler = sigalarm;
4247c478bd9Sstevel@tonic-gate 				(void) sigemptyset(&sigact.sa_mask);
4257c478bd9Sstevel@tonic-gate 				(void) sigaction(SIGALRM, &sigact, NULL);
4267c478bd9Sstevel@tonic-gate 				(void) alarm(ALARMTIME);
4277c478bd9Sstevel@tonic-gate 			}
4287c478bd9Sstevel@tonic-gate 			return;
4297c478bd9Sstevel@tonic-gate 		}
4307c478bd9Sstevel@tonic-gate 		if (check_session(fd) != 0) {
4317c478bd9Sstevel@tonic-gate 			if ((Initialized) && (pmptr->p_inservice != SESSION)) {
4327c478bd9Sstevel@tonic-gate 				log("Warning -- active session exists on <%s>",
4337c478bd9Sstevel@tonic-gate 				    pmptr->p_device);
4347c478bd9Sstevel@tonic-gate 			} else {
4357c478bd9Sstevel@tonic-gate 				/*
4367c478bd9Sstevel@tonic-gate 				 * this may happen if a service is running
4377c478bd9Sstevel@tonic-gate 				 * and ttymon dies and is restarted,
4387c478bd9Sstevel@tonic-gate 				 * or another process is running on the
4397c478bd9Sstevel@tonic-gate 				 * port.
4407c478bd9Sstevel@tonic-gate 				 */
4417c478bd9Sstevel@tonic-gate 				pmptr->p_status = SESSION;
4427c478bd9Sstevel@tonic-gate 				pmptr->p_inservice = 0;
4437c478bd9Sstevel@tonic-gate 				(void) close(fd);
4447c478bd9Sstevel@tonic-gate 				Nlocked++;
4457c478bd9Sstevel@tonic-gate 				if (Nlocked == 1) {
4467c478bd9Sstevel@tonic-gate 					sigact.sa_flags = 0;
4477c478bd9Sstevel@tonic-gate 					sigact.sa_handler = sigalarm;
4487c478bd9Sstevel@tonic-gate 					(void) sigemptyset(&sigact.sa_mask);
4497c478bd9Sstevel@tonic-gate 					(void) sigaction(SIGALRM, &sigact,
4507c478bd9Sstevel@tonic-gate 					    NULL);
4517c478bd9Sstevel@tonic-gate 					(void) alarm(ALARMTIME);
4527c478bd9Sstevel@tonic-gate 				}
4537c478bd9Sstevel@tonic-gate 				return;
4547c478bd9Sstevel@tonic-gate 			}
4557c478bd9Sstevel@tonic-gate 		}
4567c478bd9Sstevel@tonic-gate 		pmptr->p_inservice = 0;
4577c478bd9Sstevel@tonic-gate 	}
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate 	if (pmptr->p_ttyflags & H_FLAG) {
4607c478bd9Sstevel@tonic-gate 		/* drop DTR */
4617c478bd9Sstevel@tonic-gate 		(void) hang_up_line(fd);
4627c478bd9Sstevel@tonic-gate 		/*
4637c478bd9Sstevel@tonic-gate 		 * After hang_up_line, the stream is in STRHUP state.
4647c478bd9Sstevel@tonic-gate 		 * We need to do another open to reinitialize streams
4657c478bd9Sstevel@tonic-gate 		 * then we can close one fd
4667c478bd9Sstevel@tonic-gate 		 */
4677c478bd9Sstevel@tonic-gate 		if ((tmpfd = open(pmptr->p_device, O_RDWR|O_NONBLOCK)) == -1) {
4687c478bd9Sstevel@tonic-gate 			log("open (%s) failed: %s", pmptr->p_device,
4697c478bd9Sstevel@tonic-gate 			    strerror(errno));
4707c478bd9Sstevel@tonic-gate 			Retry = TRUE;
4717c478bd9Sstevel@tonic-gate 			(void) close(fd);
4727c478bd9Sstevel@tonic-gate 			return;
4737c478bd9Sstevel@tonic-gate 		}
4747c478bd9Sstevel@tonic-gate 		(void) close(tmpfd);
4757c478bd9Sstevel@tonic-gate 	}
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate #ifdef DEBUG
4787c478bd9Sstevel@tonic-gate 	debug("open_device (%s), fd = %d", pmptr->p_device, fd);
4797c478bd9Sstevel@tonic-gate #endif
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate 	/* Change ownership of the tty line to root/uucp and */
4827c478bd9Sstevel@tonic-gate 	/* set protections to only allow root/uucp to read the line. */
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate 	if (pmptr->p_ttyflags & (B_FLAG|C_FLAG))
4857c478bd9Sstevel@tonic-gate 		(void) fchown(fd, Uucp_uid, Tty_gid);
4867c478bd9Sstevel@tonic-gate 	else
4877c478bd9Sstevel@tonic-gate 		(void) fchown(fd, ROOTUID, Tty_gid);
4887c478bd9Sstevel@tonic-gate 	(void) fchmod(fd, 0620);
4897c478bd9Sstevel@tonic-gate 
4907c478bd9Sstevel@tonic-gate 	if ((pmptr->p_modules != NULL)&&(*(pmptr->p_modules) != '\0')) {
4917c478bd9Sstevel@tonic-gate 		if (push_linedisc(fd, pmptr->p_modules, pmptr->p_device)
4927c478bd9Sstevel@tonic-gate 		    == -1) {
4937c478bd9Sstevel@tonic-gate 			Retry = TRUE;
4947c478bd9Sstevel@tonic-gate 			(void) close(fd);
4957c478bd9Sstevel@tonic-gate 			return;
4967c478bd9Sstevel@tonic-gate 		}
4977c478bd9Sstevel@tonic-gate 	}
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate 	if (initial_termio(fd, pmptr) == -1)  {
5007c478bd9Sstevel@tonic-gate 		Retry = TRUE;
5017c478bd9Sstevel@tonic-gate 		(void) close(fd);
5027c478bd9Sstevel@tonic-gate 		return;
5037c478bd9Sstevel@tonic-gate 	}
5047c478bd9Sstevel@tonic-gate 
5053bb2c156SToomas Soome 	(void) di_devperm_logout((const char *)pmptr->p_device);
5067c478bd9Sstevel@tonic-gate 	pmptr->p_fd = fd;
5077c478bd9Sstevel@tonic-gate }
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate /*
5107c478bd9Sstevel@tonic-gate  *	set_poll(fdp)	- put all fd's in a pollfd array
5117c478bd9Sstevel@tonic-gate  *			- set poll event to POLLIN and POLLMSG
5127c478bd9Sstevel@tonic-gate  *			- return number of fd to be polled
5137c478bd9Sstevel@tonic-gate  */
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate static	int
set_poll(struct pollfd * fdp)5163bb2c156SToomas Soome set_poll(struct pollfd *fdp)
5177c478bd9Sstevel@tonic-gate {
5187c478bd9Sstevel@tonic-gate 	struct	pmtab	*tp;
5193bb2c156SToomas Soome 	int	nfd = 0;
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 	for (tp = PMtab; tp; tp = tp->p_next) {
5227c478bd9Sstevel@tonic-gate 		if (tp->p_fd > 0)  {
5237c478bd9Sstevel@tonic-gate 			fdp->fd = tp->p_fd;
5247c478bd9Sstevel@tonic-gate 			fdp->events = POLLIN;
5257c478bd9Sstevel@tonic-gate 			fdp++;
5267c478bd9Sstevel@tonic-gate 			nfd++;
5277c478bd9Sstevel@tonic-gate 		}
5287c478bd9Sstevel@tonic-gate 	}
5297c478bd9Sstevel@tonic-gate 	return (nfd);
5307c478bd9Sstevel@tonic-gate }
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate /*
5337c478bd9Sstevel@tonic-gate  *	check_spawnlimit	- return 0 if spawnlimit is not reached
5347c478bd9Sstevel@tonic-gate  *				- otherwise return -1
5357c478bd9Sstevel@tonic-gate  */
5367c478bd9Sstevel@tonic-gate static	int
check_spawnlimit(struct pmtab * pmptr)5373bb2c156SToomas Soome check_spawnlimit(struct pmtab *pmptr)
5387c478bd9Sstevel@tonic-gate {
5397c478bd9Sstevel@tonic-gate 	time_t	now;
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate 	(void) time(&now);
5427c478bd9Sstevel@tonic-gate 	if (pmptr->p_time == 0L)
5437c478bd9Sstevel@tonic-gate 		pmptr->p_time = now;
5447c478bd9Sstevel@tonic-gate 	if (pmptr->p_respawn >= SPAWN_LIMIT) {
5457c478bd9Sstevel@tonic-gate 		if ((now - pmptr->p_time) < SPAWN_INTERVAL) {
5467c478bd9Sstevel@tonic-gate 			pmptr->p_time = now;
5477c478bd9Sstevel@tonic-gate 			pmptr->p_respawn = 0;
5487c478bd9Sstevel@tonic-gate 			return (-1);
5497c478bd9Sstevel@tonic-gate 		}
5507c478bd9Sstevel@tonic-gate 		pmptr->p_time = now;
5517c478bd9Sstevel@tonic-gate 		pmptr->p_respawn = 0;
5527c478bd9Sstevel@tonic-gate 	}
5537c478bd9Sstevel@tonic-gate 	pmptr->p_respawn++;
5547c478bd9Sstevel@tonic-gate 	return (0);
5557c478bd9Sstevel@tonic-gate }
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate /*
5587c478bd9Sstevel@tonic-gate  * mod_ttydefs	- to check if /etc/ttydefs has been modified
5597c478bd9Sstevel@tonic-gate  *		- return TRUE if file modified
5607c478bd9Sstevel@tonic-gate  *		- otherwise, return FALSE
5617c478bd9Sstevel@tonic-gate  */
5627c478bd9Sstevel@tonic-gate static	int
mod_ttydefs(void)5633bb2c156SToomas Soome mod_ttydefs(void)
5647c478bd9Sstevel@tonic-gate {
5657c478bd9Sstevel@tonic-gate 	struct	stat	statbuf;
5663bb2c156SToomas Soome 
5677c478bd9Sstevel@tonic-gate 	if (stat(TTYDEFS, &statbuf) == -1) {
5687c478bd9Sstevel@tonic-gate 		/* if stat failed, don't bother reread ttydefs */
5697c478bd9Sstevel@tonic-gate 		return (FALSE);
5707c478bd9Sstevel@tonic-gate 	}
5717c478bd9Sstevel@tonic-gate 	if ((long)statbuf.st_mtime != Mtime) {
5727c478bd9Sstevel@tonic-gate 		Mtime = (long)statbuf.st_mtime;
5737c478bd9Sstevel@tonic-gate 		return (TRUE);
5747c478bd9Sstevel@tonic-gate 	}
5757c478bd9Sstevel@tonic-gate 	return (FALSE);
5767c478bd9Sstevel@tonic-gate }
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate /*
5797c478bd9Sstevel@tonic-gate  *	free_defs - free the Gdef table
5807c478bd9Sstevel@tonic-gate  */
5817c478bd9Sstevel@tonic-gate static	void
free_defs(void)5823bb2c156SToomas Soome free_defs(void)
5837c478bd9Sstevel@tonic-gate {
5847c478bd9Sstevel@tonic-gate 	int	i;
5857c478bd9Sstevel@tonic-gate 	struct	Gdef	*tp;
5867c478bd9Sstevel@tonic-gate 	tp = &Gdef[0];
5877c478bd9Sstevel@tonic-gate 	for (i = 0; i < Ndefs; i++, tp++) {
5887c478bd9Sstevel@tonic-gate 		free(tp->g_id);
5897c478bd9Sstevel@tonic-gate 		free(tp->g_iflags);
5907c478bd9Sstevel@tonic-gate 		free(tp->g_fflags);
5917c478bd9Sstevel@tonic-gate 		free(tp->g_nextid);
5927c478bd9Sstevel@tonic-gate 		tp->g_id = NULL;
5937c478bd9Sstevel@tonic-gate 		tp->g_iflags = NULL;
5947c478bd9Sstevel@tonic-gate 		tp->g_fflags = NULL;
5957c478bd9Sstevel@tonic-gate 		tp->g_nextid = NULL;
5967c478bd9Sstevel@tonic-gate 	}
5977c478bd9Sstevel@tonic-gate 	Ndefs = 0;
5987c478bd9Sstevel@tonic-gate }
5997c478bd9Sstevel@tonic-gate 
600*902bba37SToomas Soome /*
601*902bba37SToomas Soome  * rebuild flags entry using speed from ttymode.
602*902bba37SToomas Soome  */
603*902bba37SToomas Soome static char *
merge_flags(char * src,char * ttymode)604*902bba37SToomas Soome merge_flags(char *src, char *ttymode)
605*902bba37SToomas Soome {
606*902bba37SToomas Soome 	char *data, *ptr, *flags;
607*902bba37SToomas Soome 
608*902bba37SToomas Soome 	/* copy speed entry */
609*902bba37SToomas Soome 	data = strsave(src);
610*902bba37SToomas Soome 	flags = strsave(ttymode);
611*902bba37SToomas Soome 	ptr = strchr(flags, ',');
612*902bba37SToomas Soome 	if (ptr == NULL) {	/* ttymode is corrupted */
613*902bba37SToomas Soome 		free(flags);
614*902bba37SToomas Soome 		return (data);
615*902bba37SToomas Soome 	}
616*902bba37SToomas Soome 	*ptr = '\0';
617*902bba37SToomas Soome 	ptr = flags;
618*902bba37SToomas Soome 	flags = strsave(flags);
619*902bba37SToomas Soome 	free(ptr);
620*902bba37SToomas Soome 
621*902bba37SToomas Soome 	/*
622*902bba37SToomas Soome 	 * The flags line is supposed to have stty keywords separated by space.
623*902bba37SToomas Soome 	 * We need to split up the keywords, replace the speed and
624*902bba37SToomas Soome 	 * reconstruct the flags line.
625*902bba37SToomas Soome 	 */
626*902bba37SToomas Soome 
627*902bba37SToomas Soome 	ptr = strtok(data, " \t");
628*902bba37SToomas Soome 	if (ptr == NULL) {
629*902bba37SToomas Soome 		free(data);
630*902bba37SToomas Soome 		return (flags);
631*902bba37SToomas Soome 	}
632*902bba37SToomas Soome 
633*902bba37SToomas Soome 	do {
634*902bba37SToomas Soome 		char *tmp;
635*902bba37SToomas Soome 
636*902bba37SToomas Soome 		/* skip speed */
637*902bba37SToomas Soome 		if (isdigit(*ptr))
638*902bba37SToomas Soome 			continue;
639*902bba37SToomas Soome 
640*902bba37SToomas Soome 		if (asprintf(&tmp, "%s %s", flags, ptr) <= 0) {
641*902bba37SToomas Soome 			/* should we complain? */
642*902bba37SToomas Soome 			break;
643*902bba37SToomas Soome 		}
644*902bba37SToomas Soome 		free(flags);
645*902bba37SToomas Soome 		flags = tmp;
646*902bba37SToomas Soome 	} while ((ptr = strtok(NULL, " \t")) != NULL);
647*902bba37SToomas Soome 
648*902bba37SToomas Soome 	free(data);
649*902bba37SToomas Soome 	return (flags);
650*902bba37SToomas Soome }
651*902bba37SToomas Soome 
6527c478bd9Sstevel@tonic-gate /*
6537c478bd9Sstevel@tonic-gate  * struct Gdef *get_speed(ttylabel)
6547c478bd9Sstevel@tonic-gate  *	- search "/etc/ttydefs" for speed and term. specification
6557c478bd9Sstevel@tonic-gate  *	  using "ttylabel". If "ttylabel" is NULL, default
6567c478bd9Sstevel@tonic-gate  *	  to DEFAULT
657*902bba37SToomas Soome  *	- for /dev/console, if we are in fact using serial console,
658*902bba37SToomas Soome  *	  use ttyX-mode value to get speed. This allows us to use
659*902bba37SToomas Soome  *	  the value set for serial console either from firmware (or BMC sol),
660*902bba37SToomas Soome  *	  or boot loader default.
6617c478bd9Sstevel@tonic-gate  * arg:	  ttylabel - label/id of speed settings.
6627c478bd9Sstevel@tonic-gate  */
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate struct Gdef *
get_speed(struct pmtab * pmptr)665*902bba37SToomas Soome get_speed(struct pmtab *pmptr)
6667c478bd9Sstevel@tonic-gate {
667*902bba37SToomas Soome 	static struct Gdef serial = { 0 };
6683bb2c156SToomas Soome 	struct Gdef *sp;
669*902bba37SToomas Soome 	char *ttylabel = pmptr->p_ttylabel;
6707c478bd9Sstevel@tonic-gate 
6717c478bd9Sstevel@tonic-gate 	if ((ttylabel != NULL) && (*ttylabel != '\0')) {
6727c478bd9Sstevel@tonic-gate 		if ((sp = find_def(ttylabel)) == NULL) {
6737c478bd9Sstevel@tonic-gate 			log("unable to find <%s> in \"%s\"", ttylabel, TTYDEFS);
6747c478bd9Sstevel@tonic-gate 			sp = &DEFAULT; /* use default */
6757c478bd9Sstevel@tonic-gate 		}
676*902bba37SToomas Soome 	} else {
677*902bba37SToomas Soome 		sp = &DEFAULT; /* use default */
678*902bba37SToomas Soome 	}
679*902bba37SToomas Soome 
680*902bba37SToomas Soome 	/*
681*902bba37SToomas Soome 	 * if this is not /dev/console or /dev/console is not using serial,
682*902bba37SToomas Soome 	 * we are done.
683*902bba37SToomas Soome 	 */
684*902bba37SToomas Soome 	if (pmptr->p_ttymode == NULL ||
685*902bba37SToomas Soome 	    strcmp(pmptr->p_device, "/dev/console") != 0)
686*902bba37SToomas Soome 		return (sp);
687*902bba37SToomas Soome 
688*902bba37SToomas Soome 	/* is entry for serial set up? */
689*902bba37SToomas Soome 	if (serial.g_id == NULL) {
690*902bba37SToomas Soome 		/*
691*902bba37SToomas Soome 		 * Copy data from sp, except we need to update inital and
692*902bba37SToomas Soome 		 * final flags.
693*902bba37SToomas Soome 		 */
694*902bba37SToomas Soome 		serial.g_id = strsave(sp->g_id);
695*902bba37SToomas Soome 		serial.g_iflags = merge_flags(sp->g_iflags, pmptr->p_ttymode);
696*902bba37SToomas Soome 		serial.g_fflags = merge_flags(sp->g_fflags, pmptr->p_ttymode);
697*902bba37SToomas Soome 		serial.g_autobaud = sp->g_autobaud;
698*902bba37SToomas Soome 		serial.g_nextid = strsave(sp->g_nextid);
699*902bba37SToomas Soome 	}
700*902bba37SToomas Soome 	return (&serial);
7017c478bd9Sstevel@tonic-gate }
7027c478bd9Sstevel@tonic-gate 
7037c478bd9Sstevel@tonic-gate /*
7047c478bd9Sstevel@tonic-gate  * setup_PCpipe()	- setup the pipe between Parent and Children
7057c478bd9Sstevel@tonic-gate  *			- the pipe is used for a tmchild to send its
7067c478bd9Sstevel@tonic-gate  *			  pid to inform ttymon that it is about to
7077c478bd9Sstevel@tonic-gate  *			  invoke service
7087c478bd9Sstevel@tonic-gate  *			- the pipe also serves as a mean for tmchild
7097c478bd9Sstevel@tonic-gate  *			  to detect failure of ttymon
7107c478bd9Sstevel@tonic-gate  */
7117c478bd9Sstevel@tonic-gate void
setup_PCpipe(void)7123bb2c156SToomas Soome setup_PCpipe(void)
7137c478bd9Sstevel@tonic-gate {
7147c478bd9Sstevel@tonic-gate 	int	flag = 0;
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate 	if (pipe(PCpipe) == -1)
7177c478bd9Sstevel@tonic-gate 		fatal("pipe() failed: %s", strerror(errno));
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate 	/* set close-on-exec flag */
7207c478bd9Sstevel@tonic-gate 	if (fcntl(PCpipe[0], F_SETFD, 1) == -1)
7217c478bd9Sstevel@tonic-gate 		fatal("F_SETFD fcntl failed: %s", strerror(errno));
7227c478bd9Sstevel@tonic-gate 
7237c478bd9Sstevel@tonic-gate 	if (fcntl(PCpipe[1], F_SETFD, 1) == -1)
7247c478bd9Sstevel@tonic-gate 		fatal("F_SETFD fcntl failed: %s", strerror(errno));
7257c478bd9Sstevel@tonic-gate 
7267c478bd9Sstevel@tonic-gate 	/* set O_NONBLOCK flag */
7277c478bd9Sstevel@tonic-gate 	if (fcntl(PCpipe[0], F_GETFL, flag) == -1)
7287c478bd9Sstevel@tonic-gate 		fatal("F_GETFL failed: %s", strerror(errno));
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate 	flag |= O_NONBLOCK;
7317c478bd9Sstevel@tonic-gate 	if (fcntl(PCpipe[0], F_SETFL, flag) == -1)
7327c478bd9Sstevel@tonic-gate 		fatal("F_SETFL failed: %s", strerror(errno));
7337c478bd9Sstevel@tonic-gate 
7347c478bd9Sstevel@tonic-gate 	/* set message discard mode */
7357c478bd9Sstevel@tonic-gate 	if (ioctl(PCpipe[0], I_SRDOPT, RMSGD) == -1)
7367c478bd9Sstevel@tonic-gate 		fatal("I_SRDOPT RMSGD failed: %s", strerror(errno));
7377c478bd9Sstevel@tonic-gate 
7387c478bd9Sstevel@tonic-gate 	/* register to receive SIGPOLL when data come */
7397c478bd9Sstevel@tonic-gate 	if (ioctl(PCpipe[0], I_SETSIG, S_INPUT) == -1)
7407c478bd9Sstevel@tonic-gate 		fatal("I_SETSIG S_INPUT failed: %s", strerror(errno));
7417c478bd9Sstevel@tonic-gate 
7423bb2c156SToomas Soome #ifdef	DEBUG
7437c478bd9Sstevel@tonic-gate 	log("PCpipe[0]\t = %d", PCpipe[0]);
7447c478bd9Sstevel@tonic-gate 	log("PCpipe[1]\t = %d", PCpipe[1]);
7457c478bd9Sstevel@tonic-gate #endif
7467c478bd9Sstevel@tonic-gate }
747