xref: /titanic_41/usr/src/cmd/ttymon/tmchild.c (revision b77c815b748d4c5da996e5a2b1ae900c714628b8)
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
5*b77c815bSNobutomo Nakano  * Common Development and Distribution License (the "License").
6*b77c815bSNobutomo Nakano  * 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 /*
22*b77c815bSNobutomo 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 
297c478bd9Sstevel@tonic-gate #include	<stdio.h>
307c478bd9Sstevel@tonic-gate #include	<stdlib.h>
317c478bd9Sstevel@tonic-gate #include	<fcntl.h>
327c478bd9Sstevel@tonic-gate #include	<errno.h>
337c478bd9Sstevel@tonic-gate #include	<sys/types.h>
347c478bd9Sstevel@tonic-gate #include	<termio.h>
357c478bd9Sstevel@tonic-gate #include	<string.h>
367c478bd9Sstevel@tonic-gate #include	<signal.h>
377c478bd9Sstevel@tonic-gate #include	<poll.h>
387c478bd9Sstevel@tonic-gate #include	<unistd.h>
397c478bd9Sstevel@tonic-gate #include 	"sys/stropts.h"
407c478bd9Sstevel@tonic-gate #include 	<sys/resource.h>
417c478bd9Sstevel@tonic-gate #include	"sac.h"
427c478bd9Sstevel@tonic-gate #include	"ttymon.h"
437c478bd9Sstevel@tonic-gate #include	"tmstruct.h"
447c478bd9Sstevel@tonic-gate #include	"tmextern.h"
457c478bd9Sstevel@tonic-gate #ifdef	SYS_NAME
467c478bd9Sstevel@tonic-gate #include	<sys/utsname.h>
477c478bd9Sstevel@tonic-gate #endif
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate static void openline();
507c478bd9Sstevel@tonic-gate static void invoke_service();
517c478bd9Sstevel@tonic-gate static char	*do_autobaud();
527c478bd9Sstevel@tonic-gate static	struct	Gdef	*next_speed();
537c478bd9Sstevel@tonic-gate static int check_hup();
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate extern	struct	Gdef	*get_speed();
567c478bd9Sstevel@tonic-gate extern struct strbuf *peek_ptr, *do_peek();
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate /*
597c478bd9Sstevel@tonic-gate  * tmchild	- process that handles peeking data, determine baud rate
607c478bd9Sstevel@tonic-gate  *		  and invoke service on each individual port.
617c478bd9Sstevel@tonic-gate  *
627c478bd9Sstevel@tonic-gate  */
637c478bd9Sstevel@tonic-gate void
tmchild(pmtab)647c478bd9Sstevel@tonic-gate tmchild(pmtab)
657c478bd9Sstevel@tonic-gate struct	pmtab	*pmtab;
667c478bd9Sstevel@tonic-gate {
677c478bd9Sstevel@tonic-gate 	register struct Gdef *speedef;
687c478bd9Sstevel@tonic-gate 	char	*auto_speed = "";
697c478bd9Sstevel@tonic-gate 	struct	sigaction sigact;
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate #ifdef	DEBUG
727c478bd9Sstevel@tonic-gate 	debug("in tmchild");
737c478bd9Sstevel@tonic-gate #endif
747c478bd9Sstevel@tonic-gate 	peek_ptr = NULL;
757c478bd9Sstevel@tonic-gate 	if (pmtab->p_status != GETTY) {
767c478bd9Sstevel@tonic-gate 		child_sigcatch();
777c478bd9Sstevel@tonic-gate 		(void) close(PCpipe[0]); /* close parent end of the pipe */
787c478bd9Sstevel@tonic-gate 		if (ioctl(PCpipe[1], I_SETSIG, S_HANGUP) == -1) {
797c478bd9Sstevel@tonic-gate 			log("I_SETSIG failed: %s", strerror(errno));
807c478bd9Sstevel@tonic-gate 			exit(1);
817c478bd9Sstevel@tonic-gate 		}
827c478bd9Sstevel@tonic-gate 		/*
837c478bd9Sstevel@tonic-gate 		 * the following check is to make sure no hangup
847c478bd9Sstevel@tonic-gate 		 * happens before registering for SIGPOLL
857c478bd9Sstevel@tonic-gate 		 */
867c478bd9Sstevel@tonic-gate 		if (check_hup(PCpipe[1])) {
877c478bd9Sstevel@tonic-gate #ifdef	DEBUG
887c478bd9Sstevel@tonic-gate 			debug("PCpipe hungup, tmchild exiting");
897c478bd9Sstevel@tonic-gate #endif
907c478bd9Sstevel@tonic-gate 			exit(1);
917c478bd9Sstevel@tonic-gate 		}
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 		if (pmtab->p_ttyflags & (C_FLAG|B_FLAG)) {
947c478bd9Sstevel@tonic-gate 			if (pmtab->p_fd > 0) {
957c478bd9Sstevel@tonic-gate 				(void) close(pmtab->p_fd);
967c478bd9Sstevel@tonic-gate 				pmtab->p_fd = 0;
977c478bd9Sstevel@tonic-gate 			}
987c478bd9Sstevel@tonic-gate 		}
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 		/*
1017c478bd9Sstevel@tonic-gate 		 * become the session leader so that a controlling tty
1027c478bd9Sstevel@tonic-gate 		 * will be allocated.
1037c478bd9Sstevel@tonic-gate 		 */
1047c478bd9Sstevel@tonic-gate 		(void) setsid();
1057c478bd9Sstevel@tonic-gate 	}
1067c478bd9Sstevel@tonic-gate 	speedef = get_speed(pmtab->p_ttylabel);
1077c478bd9Sstevel@tonic-gate 	openline(pmtab, speedef);
1087c478bd9Sstevel@tonic-gate 	if (pmtab->p_ttyflags & (C_FLAG|B_FLAG)) {
1097c478bd9Sstevel@tonic-gate 	    if (pmtab->p_fd >= 0) {
1107c478bd9Sstevel@tonic-gate 		if ((pmtab->p_modules != NULL)&&(*(pmtab->p_modules) != '\0')) {
1117c478bd9Sstevel@tonic-gate 		    if (push_linedisc(pmtab->p_fd, pmtab->p_modules, pmtab->p_device) == -1) {
1127c478bd9Sstevel@tonic-gate 			(void) close(pmtab->p_fd);
1137c478bd9Sstevel@tonic-gate 			return;
1147c478bd9Sstevel@tonic-gate 		    }
1157c478bd9Sstevel@tonic-gate 		}
1167c478bd9Sstevel@tonic-gate 	    }
1177c478bd9Sstevel@tonic-gate 	}
1187c478bd9Sstevel@tonic-gate 	if ((pmtab->p_ttyflags & C_FLAG) &&
1197c478bd9Sstevel@tonic-gate 	    (State != PM_DISABLED) &&
1207c478bd9Sstevel@tonic-gate 	    (!(pmtab->p_flags & X_FLAG))) {
1217c478bd9Sstevel@tonic-gate 		/*
1227c478bd9Sstevel@tonic-gate 		 * if "c" flag is set, and the port is not disabled
1237c478bd9Sstevel@tonic-gate 		 * invoke service immediately
1247c478bd9Sstevel@tonic-gate 		 */
1257c478bd9Sstevel@tonic-gate 		if (set_termio(0, speedef->g_fflags, NULL,FALSE,CANON) == -1) {
1267c478bd9Sstevel@tonic-gate 			log("set final termio failed");
1277c478bd9Sstevel@tonic-gate 			exit(1);
1287c478bd9Sstevel@tonic-gate 		}
1297c478bd9Sstevel@tonic-gate 		invoke_service(pmtab);
1307c478bd9Sstevel@tonic-gate 		exit(1);	/*NOTREACHED*/
1317c478bd9Sstevel@tonic-gate 	}
1327c478bd9Sstevel@tonic-gate 	if (speedef->g_autobaud & A_FLAG) {
1337c478bd9Sstevel@tonic-gate 		auto_speed = do_autobaud(pmtab, speedef);
1347c478bd9Sstevel@tonic-gate 	}
1357c478bd9Sstevel@tonic-gate 	if (set_termio(0, speedef->g_fflags, NULL, FALSE, CANON) == -1) {
1367c478bd9Sstevel@tonic-gate 		log("set final termio failed");
1377c478bd9Sstevel@tonic-gate 		exit(1);
1387c478bd9Sstevel@tonic-gate 	}
1397c478bd9Sstevel@tonic-gate 	if ((pmtab->p_ttyflags & (R_FLAG|A_FLAG)) ||
1407c478bd9Sstevel@tonic-gate 	    (pmtab->p_status == GETTY) || (pmtab->p_timeout > 0)) {
1417c478bd9Sstevel@tonic-gate 		write_prompt(1, pmtab, TRUE, TRUE);
1427c478bd9Sstevel@tonic-gate 		if (pmtab->p_timeout) {
1437c478bd9Sstevel@tonic-gate 			sigact.sa_flags = 0;
1447c478bd9Sstevel@tonic-gate 			sigact.sa_handler = timedout;
1457c478bd9Sstevel@tonic-gate 			(void) sigemptyset(&sigact.sa_mask);
1467c478bd9Sstevel@tonic-gate 			(void) sigaction(SIGALRM, &sigact, NULL);
1477c478bd9Sstevel@tonic-gate 			(void) alarm((unsigned)pmtab->p_timeout);
1487c478bd9Sstevel@tonic-gate 		}
1497c478bd9Sstevel@tonic-gate 	}
1507c478bd9Sstevel@tonic-gate 	else if ((pmtab->p_ttyflags & (B_FLAG)))
1517c478bd9Sstevel@tonic-gate 			write_prompt(pmtab->p_fd, pmtab, TRUE, TRUE);
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 	/* Loop until user is successful in invoking service. */
1557c478bd9Sstevel@tonic-gate 	for (;;) {
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 		/* Peek the user's typed response and respond appropriately. */
1587c478bd9Sstevel@tonic-gate 		switch (poll_data()) {
1597c478bd9Sstevel@tonic-gate 		case GOODNAME:
1607c478bd9Sstevel@tonic-gate #ifdef	DEBUG
1617c478bd9Sstevel@tonic-gate 			debug("got GOODNAME");
1627c478bd9Sstevel@tonic-gate #endif
1637c478bd9Sstevel@tonic-gate 			if (pmtab->p_timeout) {
1647c478bd9Sstevel@tonic-gate 				(void) alarm((unsigned)0);
1657c478bd9Sstevel@tonic-gate 				sigact.sa_flags = 0;
1667c478bd9Sstevel@tonic-gate 				sigact.sa_handler = SIG_DFL;
1677c478bd9Sstevel@tonic-gate 				(void) sigemptyset(&sigact.sa_mask);
1687c478bd9Sstevel@tonic-gate 				(void) sigaction(SIGALRM, &sigact, NULL);
1697c478bd9Sstevel@tonic-gate 			}
1707c478bd9Sstevel@tonic-gate 			if ((State == PM_DISABLED)||(pmtab->p_flags & X_FLAG)){
1717c478bd9Sstevel@tonic-gate 				write_prompt(1, pmtab, TRUE, FALSE);
1727c478bd9Sstevel@tonic-gate 				break;
1737c478bd9Sstevel@tonic-gate 			}
1747c478bd9Sstevel@tonic-gate 			if (set_termio(0, speedef->g_fflags, auto_speed,
1757c478bd9Sstevel@tonic-gate 			    FALSE, CANON) == -1) {
1767c478bd9Sstevel@tonic-gate 				log("set final termio failed");
1777c478bd9Sstevel@tonic-gate 				exit(1);
1787c478bd9Sstevel@tonic-gate 			}
1797c478bd9Sstevel@tonic-gate 			invoke_service(pmtab);
1807c478bd9Sstevel@tonic-gate 			exit(1);	/*NOTREACHED*/
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 		case BADSPEED:
1837c478bd9Sstevel@tonic-gate 			/* wrong speed! try next speed in the list. */
1847c478bd9Sstevel@tonic-gate 			speedef = next_speed(speedef);
1857c478bd9Sstevel@tonic-gate #ifdef	DEBUG
1867c478bd9Sstevel@tonic-gate 			debug("BADSPEED: setup next speed");
1877c478bd9Sstevel@tonic-gate #endif
1887c478bd9Sstevel@tonic-gate 			if (speedef->g_autobaud & A_FLAG) {
1897c478bd9Sstevel@tonic-gate 				if (auto_termio(0) == -1) {
1907c478bd9Sstevel@tonic-gate 					exit(1);
1917c478bd9Sstevel@tonic-gate 				}
1927c478bd9Sstevel@tonic-gate 				auto_speed = do_autobaud(pmtab, speedef);
1937c478bd9Sstevel@tonic-gate 			}
1947c478bd9Sstevel@tonic-gate 			else {
1957c478bd9Sstevel@tonic-gate 				auto_speed = NULL;
1967c478bd9Sstevel@tonic-gate 				/*
1977c478bd9Sstevel@tonic-gate 				 * this reset may fail if the speed is not
1987c478bd9Sstevel@tonic-gate 				 * supported by the system
1997c478bd9Sstevel@tonic-gate 				 * we just cycle through it to the next one
2007c478bd9Sstevel@tonic-gate 				 */
2017c478bd9Sstevel@tonic-gate 				if (set_termio(0, speedef->g_iflags, NULL,
2027c478bd9Sstevel@tonic-gate 				    FALSE, CANON) != 0) {
2037c478bd9Sstevel@tonic-gate 					log("Warning -- speed of <%s> may "
2047c478bd9Sstevel@tonic-gate 					    "be not supported by the system",
2057c478bd9Sstevel@tonic-gate 					    speedef->g_id);
2067c478bd9Sstevel@tonic-gate 				}
2077c478bd9Sstevel@tonic-gate 			}
2087c478bd9Sstevel@tonic-gate 			write_prompt(1, pmtab, TRUE, TRUE);
2097c478bd9Sstevel@tonic-gate 			break;
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 		case NONAME:
2127c478bd9Sstevel@tonic-gate #ifdef	DEBUG
2137c478bd9Sstevel@tonic-gate 			debug("got NONAME");
2147c478bd9Sstevel@tonic-gate #endif
2157c478bd9Sstevel@tonic-gate 			write_prompt(1, pmtab, FALSE, FALSE);
2167c478bd9Sstevel@tonic-gate 			break;
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 		}  /* end switch */
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 		peek_ptr = NULL;
2217c478bd9Sstevel@tonic-gate 		if (pmtab->p_timeout) {
2227c478bd9Sstevel@tonic-gate 			sigact.sa_flags = 0;
2237c478bd9Sstevel@tonic-gate 			sigact.sa_handler = timedout;
2247c478bd9Sstevel@tonic-gate 			(void) sigemptyset(&sigact.sa_mask);
2257c478bd9Sstevel@tonic-gate 			(void) sigaction(SIGALRM, &sigact, NULL);
2267c478bd9Sstevel@tonic-gate 			(void) alarm((unsigned)pmtab->p_timeout);
2277c478bd9Sstevel@tonic-gate 		}
2287c478bd9Sstevel@tonic-gate 	} /* end for loop */
2297c478bd9Sstevel@tonic-gate }
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate static void
openline(pmtab,speedef)2327c478bd9Sstevel@tonic-gate openline(pmtab, speedef)
2337c478bd9Sstevel@tonic-gate struct	pmtab 	*pmtab;
2347c478bd9Sstevel@tonic-gate struct Gdef *speedef;
2357c478bd9Sstevel@tonic-gate {
2367c478bd9Sstevel@tonic-gate 	char	 buffer[5];
2377c478bd9Sstevel@tonic-gate 	int	 rtn = 0;
2387c478bd9Sstevel@tonic-gate 	int	 line_count;
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate #ifdef	DEBUG
2417c478bd9Sstevel@tonic-gate 	debug("in openline");
2427c478bd9Sstevel@tonic-gate #endif
2437c478bd9Sstevel@tonic-gate 	if (pmtab->p_status != GETTY) {
2447c478bd9Sstevel@tonic-gate 		(void) close(0);
2457c478bd9Sstevel@tonic-gate 		/* open should return fd 0, if not, then close it */
2467c478bd9Sstevel@tonic-gate 		if ((pmtab->p_fd = open(pmtab->p_device, O_RDWR)) != 0) {
2477c478bd9Sstevel@tonic-gate 			log("open \"%s\" failed: %s", pmtab->p_device,
2487c478bd9Sstevel@tonic-gate 			    strerror(errno));
2497c478bd9Sstevel@tonic-gate 			exit(1);
2507c478bd9Sstevel@tonic-gate 		}
2517c478bd9Sstevel@tonic-gate 	}
2527c478bd9Sstevel@tonic-gate 	(void) close(1);
2537c478bd9Sstevel@tonic-gate 	(void) close(2);
2547c478bd9Sstevel@tonic-gate 	(void) dup(0);
2557c478bd9Sstevel@tonic-gate 	(void) dup(0);
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 	if (pmtab->p_ttyflags & R_FLAG) { /* wait_read is needed */
2587c478bd9Sstevel@tonic-gate 		if (pmtab->p_count) {
2597c478bd9Sstevel@tonic-gate 			if (peek_ptr != NULL)
2607c478bd9Sstevel@tonic-gate 				if ((peek_ptr->buf[0]&0x7F) == '\n' ||
2617c478bd9Sstevel@tonic-gate 				    (peek_ptr->buf[0]&0x7F) == '\r')
2627c478bd9Sstevel@tonic-gate 					pmtab->p_count--;
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 			/*
2657c478bd9Sstevel@tonic-gate 			 * - wait for "p_count" lines
2667c478bd9Sstevel@tonic-gate 			 * - datakit switch does not
2677c478bd9Sstevel@tonic-gate 			 *   know you are a host or a terminal
2687c478bd9Sstevel@tonic-gate 			 * - so it send you several lines of msg
2697c478bd9Sstevel@tonic-gate 			 * - we need to swallow that msg
2707c478bd9Sstevel@tonic-gate 			 * - we assume the baud rate is correct
2717c478bd9Sstevel@tonic-gate 			 * - if it is not, '\n' will not look like '\n'
2727c478bd9Sstevel@tonic-gate 			 * and we will wait forever here
2737c478bd9Sstevel@tonic-gate 			 */
2747c478bd9Sstevel@tonic-gate 			if (set_termio(0, speedef->g_fflags, NULL, TRUE, CANON) == -1) {
2757c478bd9Sstevel@tonic-gate 				log("set final termio failed");
2767c478bd9Sstevel@tonic-gate 				exit(1);
2777c478bd9Sstevel@tonic-gate 			}
2787c478bd9Sstevel@tonic-gate 			for (line_count = 0; line_count < pmtab->p_count; ) {
2797c478bd9Sstevel@tonic-gate 				if (read(0, buffer, 1) < 0
2807c478bd9Sstevel@tonic-gate 				    || *buffer == '\0'
2817c478bd9Sstevel@tonic-gate 				    || *buffer == '\004') {
2827c478bd9Sstevel@tonic-gate 					(void) close(0);
2837c478bd9Sstevel@tonic-gate 					exit(0);
2847c478bd9Sstevel@tonic-gate 				}
2857c478bd9Sstevel@tonic-gate 				if (*buffer == '\n')
2867c478bd9Sstevel@tonic-gate 					line_count++;
2877c478bd9Sstevel@tonic-gate 			}
2887c478bd9Sstevel@tonic-gate 		}
2897c478bd9Sstevel@tonic-gate 		else { /* wait for 1 char */
2907c478bd9Sstevel@tonic-gate 			if (peek_ptr == NULL) {
2917c478bd9Sstevel@tonic-gate 				if (set_termio(0, NULL, NULL,TRUE,RAW) == -1) {
2927c478bd9Sstevel@tonic-gate 					log("set termio RAW failed");
2937c478bd9Sstevel@tonic-gate 					exit(1);
2947c478bd9Sstevel@tonic-gate 				}
2957c478bd9Sstevel@tonic-gate 				rtn = read(0, buffer, 1);
2967c478bd9Sstevel@tonic-gate 			} else
2977c478bd9Sstevel@tonic-gate 				*buffer = (peek_ptr->buf[0]&0x7F);
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 			/*
3007c478bd9Sstevel@tonic-gate 			 * NOTE: Cu on a direct line when ~. is encountered will
3017c478bd9Sstevel@tonic-gate 			 * send EOTs to the other side.  EOT=\004
3027c478bd9Sstevel@tonic-gate 			 */
3037c478bd9Sstevel@tonic-gate 			if (rtn < 0 || *buffer == '\004') {
3047c478bd9Sstevel@tonic-gate 				(void) close(0);
3057c478bd9Sstevel@tonic-gate 				exit(0);
3067c478bd9Sstevel@tonic-gate 			}
3077c478bd9Sstevel@tonic-gate 		}
3087c478bd9Sstevel@tonic-gate 		peek_ptr = NULL;
3097c478bd9Sstevel@tonic-gate 		if (!(pmtab->p_ttyflags & A_FLAG)) { /* autobaud not enabled */
3107c478bd9Sstevel@tonic-gate 			if (set_termio(0, speedef->g_fflags, NULL, TRUE, CANON) == -1) {
3117c478bd9Sstevel@tonic-gate 				log("set final termio failed");
3127c478bd9Sstevel@tonic-gate 				exit(1);
3137c478bd9Sstevel@tonic-gate 			}
3147c478bd9Sstevel@tonic-gate 		}
3157c478bd9Sstevel@tonic-gate 	}
3167c478bd9Sstevel@tonic-gate 	if (pmtab->p_ttyflags & B_FLAG) { /* port is bi-directional */
3177c478bd9Sstevel@tonic-gate 		/* set advisory lock on the line */
3187c478bd9Sstevel@tonic-gate 		if (tm_lock(0) != 0) {
3197c478bd9Sstevel@tonic-gate 			/*
3207c478bd9Sstevel@tonic-gate 			 * device is locked
3217c478bd9Sstevel@tonic-gate 			 * child exits and let the parent wait for
3227c478bd9Sstevel@tonic-gate 			 * the lock to go away
3237c478bd9Sstevel@tonic-gate 			 */
3247c478bd9Sstevel@tonic-gate 			exit(0);
3257c478bd9Sstevel@tonic-gate 		}
3267c478bd9Sstevel@tonic-gate 		/* change ownership back to root */
3277c478bd9Sstevel@tonic-gate 		(void) fchown(0, ROOTUID, Tty_gid);
3287c478bd9Sstevel@tonic-gate 		(void) fchmod(0, 0620);
3297c478bd9Sstevel@tonic-gate 	}
3307c478bd9Sstevel@tonic-gate 	return;
3317c478bd9Sstevel@tonic-gate }
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate /*
3347c478bd9Sstevel@tonic-gate  *	write_prompt	- write the msg to fd
3357c478bd9Sstevel@tonic-gate  *			- if flush is set, flush input queue
3367c478bd9Sstevel@tonic-gate  *			- if clear is set, write a new line
3377c478bd9Sstevel@tonic-gate  */
3387c478bd9Sstevel@tonic-gate void
write_prompt(fd,pmtab,flush,clear)3397c478bd9Sstevel@tonic-gate write_prompt(fd, pmtab, flush, clear)
3407c478bd9Sstevel@tonic-gate int	fd;
3417c478bd9Sstevel@tonic-gate struct	pmtab	*pmtab;
3427c478bd9Sstevel@tonic-gate int	flush, clear;
3437c478bd9Sstevel@tonic-gate {
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate #ifdef DEBUG
3467c478bd9Sstevel@tonic-gate 	debug("in write_prompt");
3477c478bd9Sstevel@tonic-gate #endif
3487c478bd9Sstevel@tonic-gate 	if (flush)
3497c478bd9Sstevel@tonic-gate 		flush_input(fd);
3507c478bd9Sstevel@tonic-gate 	if (clear) {
3517c478bd9Sstevel@tonic-gate 		(void) write(fd, "\r\n", 2);
3527c478bd9Sstevel@tonic-gate 	}
3537c478bd9Sstevel@tonic-gate #ifdef SYS_NAME
3547c478bd9Sstevel@tonic-gate 	sys_name(fd);
3557c478bd9Sstevel@tonic-gate #endif
3567c478bd9Sstevel@tonic-gate 	/* Print prompt/disable message. */
3577c478bd9Sstevel@tonic-gate 	if ((State == PM_DISABLED) || (pmtab->p_flags & X_FLAG))
3587c478bd9Sstevel@tonic-gate 		(void)write(fd, pmtab->p_dmsg, (unsigned)strlen(pmtab->p_dmsg));
3597c478bd9Sstevel@tonic-gate 	else
3607c478bd9Sstevel@tonic-gate 		(void) write(fd, pmtab->p_prompt,
3617c478bd9Sstevel@tonic-gate 			(unsigned)strlen(pmtab->p_prompt));
3627c478bd9Sstevel@tonic-gate }
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate /*
3657c478bd9Sstevel@tonic-gate  *	timedout	- input period timed out
3667c478bd9Sstevel@tonic-gate  */
3677c478bd9Sstevel@tonic-gate void
timedout()3687c478bd9Sstevel@tonic-gate timedout()
3697c478bd9Sstevel@tonic-gate {
3707c478bd9Sstevel@tonic-gate 	exit(1);
3717c478bd9Sstevel@tonic-gate }
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate #ifdef SYS_NAME
3747c478bd9Sstevel@tonic-gate /*
3757c478bd9Sstevel@tonic-gate  * void sys_name() - generate a msg with system id
3767c478bd9Sstevel@tonic-gate  *		   - print out /etc/issue file if it exists
3777c478bd9Sstevel@tonic-gate  */
3787c478bd9Sstevel@tonic-gate void
sys_name(fd)3797c478bd9Sstevel@tonic-gate sys_name(fd)
3807c478bd9Sstevel@tonic-gate int	fd;
3817c478bd9Sstevel@tonic-gate {
3827c478bd9Sstevel@tonic-gate 	char	*ptr, buffer[BUFSIZ];
3837c478bd9Sstevel@tonic-gate 	FILE	*fp;
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate #if 0	/* 1111333 - don't print node name, we already do this elsewhere */
3867c478bd9Sstevel@tonic-gate 	struct	utsname utsname;
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate 	if (uname(&utsname) != FAILURE) {
3897c478bd9Sstevel@tonic-gate 		(void) sprintf(buffer, "%.9s\r\n", utsname.nodename);
3907c478bd9Sstevel@tonic-gate 		(void) write(fd, buffer, strlen(buffer));
3917c478bd9Sstevel@tonic-gate 	}
3927c478bd9Sstevel@tonic-gate #endif
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 	if ((fp = fopen(ISSUEFILE, "r")) != NULL) {
3957c478bd9Sstevel@tonic-gate 		while ((ptr = fgets(buffer, sizeof (buffer), fp)) != NULL) {
3967c478bd9Sstevel@tonic-gate 			(void) write(fd, ptr, strlen(ptr));
3977c478bd9Sstevel@tonic-gate 		}
3987c478bd9Sstevel@tonic-gate 		(void) fclose(fp);
3997c478bd9Sstevel@tonic-gate 	}
4007c478bd9Sstevel@tonic-gate }
4017c478bd9Sstevel@tonic-gate #endif
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate /*
4057c478bd9Sstevel@tonic-gate  *	do_autobaud	- do autobaud
4067c478bd9Sstevel@tonic-gate  *			- if it succeed, set the new speed and return
4077c478bd9Sstevel@tonic-gate  *			- if it failed, it will get the nextlabel
4087c478bd9Sstevel@tonic-gate  *			- if next entry is also autobaud,
4097c478bd9Sstevel@tonic-gate  *			  it will loop back to do autobaud again
4107c478bd9Sstevel@tonic-gate  *			- otherwise, it will set new termio and return
4117c478bd9Sstevel@tonic-gate  */
4127c478bd9Sstevel@tonic-gate static	char	*
do_autobaud(pmtab,speedef)4137c478bd9Sstevel@tonic-gate do_autobaud(pmtab, speedef)
4147c478bd9Sstevel@tonic-gate struct	pmtab	*pmtab;
4157c478bd9Sstevel@tonic-gate struct	Gdef	*speedef;
4167c478bd9Sstevel@tonic-gate {
4177c478bd9Sstevel@tonic-gate 	int	done = FALSE;
4187c478bd9Sstevel@tonic-gate 	char	*auto_speed;
4197c478bd9Sstevel@tonic-gate #ifdef	DEBUG
4207c478bd9Sstevel@tonic-gate 	debug("in do_autobaud");
4217c478bd9Sstevel@tonic-gate #endif
4227c478bd9Sstevel@tonic-gate 	while (!done) {
4237c478bd9Sstevel@tonic-gate 		if ((auto_speed = autobaud(0, pmtab->p_timeout)) == NULL) {
4247c478bd9Sstevel@tonic-gate 			speedef = next_speed(speedef);
4257c478bd9Sstevel@tonic-gate 			if (speedef->g_autobaud & A_FLAG) {
4267c478bd9Sstevel@tonic-gate 				continue;
4277c478bd9Sstevel@tonic-gate 			}
4287c478bd9Sstevel@tonic-gate 			else {
4297c478bd9Sstevel@tonic-gate 				if (set_termio(0, speedef->g_iflags, NULL,
4307c478bd9Sstevel@tonic-gate 						TRUE, CANON) != 0) {
4317c478bd9Sstevel@tonic-gate 					exit(1);
4327c478bd9Sstevel@tonic-gate 				}
4337c478bd9Sstevel@tonic-gate 				done = TRUE;
4347c478bd9Sstevel@tonic-gate 			}
4357c478bd9Sstevel@tonic-gate 		}
4367c478bd9Sstevel@tonic-gate 		else {
4377c478bd9Sstevel@tonic-gate 			if (set_termio(0, speedef->g_fflags, auto_speed,
4387c478bd9Sstevel@tonic-gate 					TRUE, CANON) != 0) {
4397c478bd9Sstevel@tonic-gate 				exit(1);
4407c478bd9Sstevel@tonic-gate 			}
4417c478bd9Sstevel@tonic-gate 			done = TRUE;
4427c478bd9Sstevel@tonic-gate 		}
4437c478bd9Sstevel@tonic-gate 	}
4447c478bd9Sstevel@tonic-gate #ifdef	DEBUG
4457c478bd9Sstevel@tonic-gate 	debug("autobaud done");
4467c478bd9Sstevel@tonic-gate #endif
4477c478bd9Sstevel@tonic-gate 	return (auto_speed);
4487c478bd9Sstevel@tonic-gate }
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate /*
4517c478bd9Sstevel@tonic-gate  * 	next_speed(speedef)
4527c478bd9Sstevel@tonic-gate  *	- find the next entry according to nextlabel. If "nextlabel"
4537c478bd9Sstevel@tonic-gate  *	  is not valid, go back to the old ttylabel.
4547c478bd9Sstevel@tonic-gate  */
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate static	struct	Gdef *
next_speed(speedef)4577c478bd9Sstevel@tonic-gate next_speed(speedef)
4587c478bd9Sstevel@tonic-gate struct	Gdef *speedef;
4597c478bd9Sstevel@tonic-gate {
4607c478bd9Sstevel@tonic-gate 	struct	Gdef *sp;
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 	if (strcmp(speedef->g_nextid, speedef->g_id) == 0)
4637c478bd9Sstevel@tonic-gate 		return (speedef);
4647c478bd9Sstevel@tonic-gate 	if ((sp = find_def(speedef->g_nextid)) == NULL) {
4657c478bd9Sstevel@tonic-gate 		log("%s's next speed-label (%s) is bad.", speedef->g_id,
4667c478bd9Sstevel@tonic-gate 		    speedef->g_nextid);
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate 		/* go back to the original entry. */
4697c478bd9Sstevel@tonic-gate 		if ((sp = find_def(speedef->g_id)) == NULL) {
4707c478bd9Sstevel@tonic-gate 			/* if failed, complain and quit. */
4717c478bd9Sstevel@tonic-gate 			log("unable to find (%s) again", speedef->g_id);
4727c478bd9Sstevel@tonic-gate 			exit(1);
4737c478bd9Sstevel@tonic-gate 		}
4747c478bd9Sstevel@tonic-gate 	}
4757c478bd9Sstevel@tonic-gate 	return (sp);
4767c478bd9Sstevel@tonic-gate }
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate /*
4797c478bd9Sstevel@tonic-gate  * inform_parent()	- inform ttymon that tmchild is going to exec service
4807c478bd9Sstevel@tonic-gate  */
4817c478bd9Sstevel@tonic-gate static	void
inform_parent(fd)4827c478bd9Sstevel@tonic-gate inform_parent(fd)
4837c478bd9Sstevel@tonic-gate int	fd;
4847c478bd9Sstevel@tonic-gate {
4857c478bd9Sstevel@tonic-gate 	pid_t	pid;
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate 	pid = getpid();
4887c478bd9Sstevel@tonic-gate 	(void) write(fd, &pid, sizeof (pid));
4897c478bd9Sstevel@tonic-gate }
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate static	char	 pbuf[BUFSIZ];	/* static buf for TTYPROMPT 	*/
4927c478bd9Sstevel@tonic-gate static	char	 hbuf[BUFSIZ];	/* static buf for HOME 		*/
4937c478bd9Sstevel@tonic-gate static	char	 tbuf[BUFSIZ];	/* static buf for TERM		*/
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate /*
4967c478bd9Sstevel@tonic-gate  * void invoke_service	- invoke the service
4977c478bd9Sstevel@tonic-gate  */
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate static	void
invoke_service(pmtab)5007c478bd9Sstevel@tonic-gate invoke_service(pmtab)
5017c478bd9Sstevel@tonic-gate struct	pmtab	*pmtab;
5027c478bd9Sstevel@tonic-gate {
5037c478bd9Sstevel@tonic-gate 	char	 *argvp[MAXARGS];		/* service cmd args */
5047c478bd9Sstevel@tonic-gate 	int	 cnt = 0;			/* arg counter */
5057c478bd9Sstevel@tonic-gate 	int	 i, fd;
5067c478bd9Sstevel@tonic-gate 	struct	 sigaction	sigact;
5077c478bd9Sstevel@tonic-gate 	extern	 struct	rlimit	Rlimit;
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate #ifdef 	DEBUG
5107c478bd9Sstevel@tonic-gate 	debug("in invoke_service");
5117c478bd9Sstevel@tonic-gate #endif
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate 	if (tcgetsid(0) != getsid(getpid())) {
5147c478bd9Sstevel@tonic-gate 		cons_printf("Warning -- ttymon cannot allocate controlling "
5157c478bd9Sstevel@tonic-gate 		    "tty on \"%s\",\n", pmtab->p_device);
5167c478bd9Sstevel@tonic-gate 		cons_printf("\tThere may be another session active on this "
5177c478bd9Sstevel@tonic-gate 		    "port.\n");
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate 		if (strcmp("/dev/console", pmtab->p_device) != 0) {
5207c478bd9Sstevel@tonic-gate 			/*
5217c478bd9Sstevel@tonic-gate 			 * if not on console, write to stderr to warn the user
5227c478bd9Sstevel@tonic-gate 			 * also.
5237c478bd9Sstevel@tonic-gate 			 */
5247c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "Warning -- ttymon cannot "
5257c478bd9Sstevel@tonic-gate 			    "allocate controlling tty on \"%s\",\n",
5267c478bd9Sstevel@tonic-gate 			    pmtab->p_device);
5277c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "\tthere may be another session "
5287c478bd9Sstevel@tonic-gate 			    "active on this port.\n");
5297c478bd9Sstevel@tonic-gate 		}
5307c478bd9Sstevel@tonic-gate 	}
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate 	if (pmtab->p_status != GETTY) {
5337c478bd9Sstevel@tonic-gate 		inform_parent(PCpipe[1]);
5347c478bd9Sstevel@tonic-gate 		sigact.sa_flags = 0;
5357c478bd9Sstevel@tonic-gate 		sigact.sa_handler = SIG_DFL;
5367c478bd9Sstevel@tonic-gate 		(void) sigemptyset(&sigact.sa_mask);
5377c478bd9Sstevel@tonic-gate 		(void) sigaction(SIGPOLL, &sigact, NULL);
5387c478bd9Sstevel@tonic-gate 	}
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate 	if (pmtab->p_flags & U_FLAG) {
5417c478bd9Sstevel@tonic-gate 		if (account(pmtab->p_device) != 0) {
5427c478bd9Sstevel@tonic-gate 			log("invoke_service: account failed");
5437c478bd9Sstevel@tonic-gate 			exit(1);
5447c478bd9Sstevel@tonic-gate 		}
5457c478bd9Sstevel@tonic-gate 	}
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 	/* parse command line */
5487c478bd9Sstevel@tonic-gate 	mkargv(pmtab->p_server, &argvp[0], &cnt, MAXARGS-1);
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate 	if (!(pmtab->p_ttyflags & C_FLAG)) {
5517c478bd9Sstevel@tonic-gate 		(void) sprintf(pbuf, "TTYPROMPT=%s", pmtab->p_prompt);
5527c478bd9Sstevel@tonic-gate 		if (putenv(pbuf)) {
5537c478bd9Sstevel@tonic-gate 			log("cannot expand service <%s> environment", argvp[0]);
5547c478bd9Sstevel@tonic-gate 			exit(1);
5557c478bd9Sstevel@tonic-gate 		}
5567c478bd9Sstevel@tonic-gate 	}
5577c478bd9Sstevel@tonic-gate 	if (pmtab->p_status != GETTY) {
5587c478bd9Sstevel@tonic-gate 		(void) sprintf(hbuf, "HOME=%s", pmtab->p_dir);
5597c478bd9Sstevel@tonic-gate 		if (putenv(hbuf)) {
5607c478bd9Sstevel@tonic-gate 			log("cannot expand service <%s> environment", argvp[0]);
5617c478bd9Sstevel@tonic-gate 			exit(1);
5627c478bd9Sstevel@tonic-gate 		}
5637c478bd9Sstevel@tonic-gate #ifdef	DEBUG
5647c478bd9Sstevel@tonic-gate 		debug("about to run config script");
5657c478bd9Sstevel@tonic-gate #endif
5667c478bd9Sstevel@tonic-gate 		if ((i = doconfig(0, pmtab->p_tag, 0)) != 0) {
5677c478bd9Sstevel@tonic-gate 			if (i < 0) {
5687c478bd9Sstevel@tonic-gate 				log("doconfig failed, system error");
5697c478bd9Sstevel@tonic-gate 			}
5707c478bd9Sstevel@tonic-gate 			else {
5717c478bd9Sstevel@tonic-gate 				log("doconfig failed on line %d of script %s",
5727c478bd9Sstevel@tonic-gate 				    i, pmtab->p_tag);
5737c478bd9Sstevel@tonic-gate 			}
5747c478bd9Sstevel@tonic-gate 			exit(1);
5757c478bd9Sstevel@tonic-gate 		}
5767c478bd9Sstevel@tonic-gate 	}
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate 	if (setgid(pmtab->p_gid)) {
5797c478bd9Sstevel@tonic-gate 		log("cannot set group id to %ld: %s", pmtab->p_gid,
5807c478bd9Sstevel@tonic-gate 		    strerror(errno));
5817c478bd9Sstevel@tonic-gate 		exit(1);
5827c478bd9Sstevel@tonic-gate 	}
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate 	if (setuid(pmtab->p_uid)) {
5857c478bd9Sstevel@tonic-gate 		log("cannot set user id to %ld: %s", pmtab->p_uid,
5867c478bd9Sstevel@tonic-gate 		    strerror(errno));
5877c478bd9Sstevel@tonic-gate 		exit(1);
5887c478bd9Sstevel@tonic-gate 	}
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@tonic-gate 	if (chdir(pmtab->p_dir)) {
5917c478bd9Sstevel@tonic-gate 		log("cannot chdir to %s: %s", pmtab->p_dir, strerror(errno));
5927c478bd9Sstevel@tonic-gate 		exit(1);
5937c478bd9Sstevel@tonic-gate 	}
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 	if (pmtab->p_uid != ROOTUID) {
5967c478bd9Sstevel@tonic-gate 		/* change ownership and mode of device */
5977c478bd9Sstevel@tonic-gate 		(void) fchown(0, pmtab->p_uid, Tty_gid);
5987c478bd9Sstevel@tonic-gate 		(void) fchmod(0, 0620);
5997c478bd9Sstevel@tonic-gate 	}
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate 	if (pmtab->p_status != GETTY) {
6037c478bd9Sstevel@tonic-gate 		sigact.sa_flags = 0;
6047c478bd9Sstevel@tonic-gate 		sigact.sa_handler = SIG_DFL;
6057c478bd9Sstevel@tonic-gate 		(void) sigemptyset(&sigact.sa_mask);
6067c478bd9Sstevel@tonic-gate 		(void) sigaction(SIGINT, &sigact, NULL);
6077c478bd9Sstevel@tonic-gate 		if (setrlimit(RLIMIT_NOFILE, &Rlimit) == -1) {
6087c478bd9Sstevel@tonic-gate 			log("setrlimit failed: %s", strerror(errno));
6097c478bd9Sstevel@tonic-gate 			exit(1);
6107c478bd9Sstevel@tonic-gate 		}
6117c478bd9Sstevel@tonic-gate 		/* invoke the service */
6127c478bd9Sstevel@tonic-gate 		log("Starting service (%s) on %s", argvp[0], pmtab->p_device);
6137c478bd9Sstevel@tonic-gate 	}
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate 	if (pmtab->p_termtype != (char *)NULL) {
6167c478bd9Sstevel@tonic-gate 		(void) sprintf(tbuf, "TERM=%s", pmtab->p_termtype);
6177c478bd9Sstevel@tonic-gate 		if (putenv(tbuf)) {
6187c478bd9Sstevel@tonic-gate 			log("cannot expand service <%s> environment", argvp[0]);
6197c478bd9Sstevel@tonic-gate 			exit(1);
6207c478bd9Sstevel@tonic-gate 		}
6217c478bd9Sstevel@tonic-gate 	}
6227c478bd9Sstevel@tonic-gate 	/* restore signal handlers and mask */
6237c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGINT, &Sigint, NULL);
6247c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGALRM, &Sigalrm, NULL);
6257c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGPOLL, &Sigpoll, NULL);
626*b77c815bSNobutomo Nakano 	(void) sigaction(SIGQUIT, &Sigquit, NULL);
6277c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGCLD, &Sigcld, NULL);
6287c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGTERM, &Sigterm, NULL);
6297c478bd9Sstevel@tonic-gate #ifdef	DEBUG
6307c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGUSR1, &Sigusr1, NULL);
6317c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGUSR2, &Sigusr2, NULL);
6327c478bd9Sstevel@tonic-gate #endif
6337c478bd9Sstevel@tonic-gate 	(void) sigprocmask(SIG_SETMASK, &Origmask, NULL);
6347c478bd9Sstevel@tonic-gate 	(void) execve(argvp[0], argvp, environ);
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate 	/* exec returns only on failure! */
6377c478bd9Sstevel@tonic-gate 	log("tmchild: exec service failed: %s", strerror(errno));
6387c478bd9Sstevel@tonic-gate 	exit(1);
6397c478bd9Sstevel@tonic-gate }
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate /*
6427c478bd9Sstevel@tonic-gate  *	check_hup(fd)	- do a poll on fd to check if it is in hangup state
6437c478bd9Sstevel@tonic-gate  *			- return 1 if hangup, otherwise return 0
6447c478bd9Sstevel@tonic-gate  */
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate static	int
check_hup(fd)6477c478bd9Sstevel@tonic-gate check_hup(fd)
6487c478bd9Sstevel@tonic-gate int	fd;
6497c478bd9Sstevel@tonic-gate {
6507c478bd9Sstevel@tonic-gate 	int	ret;
6517c478bd9Sstevel@tonic-gate 	struct	pollfd	pfd[1];
6527c478bd9Sstevel@tonic-gate 
6537c478bd9Sstevel@tonic-gate 	pfd[0].fd = fd;
6547c478bd9Sstevel@tonic-gate 	pfd[0].events = POLLHUP;
6557c478bd9Sstevel@tonic-gate 	for (;;) {
6567c478bd9Sstevel@tonic-gate 		ret = poll(pfd, 1, 0);
6577c478bd9Sstevel@tonic-gate 		if (ret < 0) {
6587c478bd9Sstevel@tonic-gate 			if (errno == EINTR)
6597c478bd9Sstevel@tonic-gate 				continue;
6607c478bd9Sstevel@tonic-gate 			log("check_hup: poll failed: %s", strerror(errno));
6617c478bd9Sstevel@tonic-gate 			exit(1);
6627c478bd9Sstevel@tonic-gate 		}
6637c478bd9Sstevel@tonic-gate 		else if (ret > 0) {
6647c478bd9Sstevel@tonic-gate 			if (pfd[0].revents & POLLHUP) {
6657c478bd9Sstevel@tonic-gate 				return (1);
6667c478bd9Sstevel@tonic-gate 			}
6677c478bd9Sstevel@tonic-gate 		}
6687c478bd9Sstevel@tonic-gate 		return (0);
6697c478bd9Sstevel@tonic-gate 	}
6707c478bd9Sstevel@tonic-gate }
6717c478bd9Sstevel@tonic-gate 
6727c478bd9Sstevel@tonic-gate /*
6737c478bd9Sstevel@tonic-gate  * sigpoll()	- SIGPOLL handle for tmchild
6747c478bd9Sstevel@tonic-gate  *		- when SIGPOLL is received by tmchild,
6757c478bd9Sstevel@tonic-gate  *		  the pipe between ttymon and tmchild is broken.
6767c478bd9Sstevel@tonic-gate  *		  Something must happen to ttymon.
6777c478bd9Sstevel@tonic-gate  */
6787c478bd9Sstevel@tonic-gate void
sigpoll()6797c478bd9Sstevel@tonic-gate sigpoll()
6807c478bd9Sstevel@tonic-gate {
6817c478bd9Sstevel@tonic-gate #ifdef	DEBUG
6827c478bd9Sstevel@tonic-gate 	debug("tmchild got SIGPOLL, exiting");
6837c478bd9Sstevel@tonic-gate #endif
6847c478bd9Sstevel@tonic-gate 	exit(1);
6857c478bd9Sstevel@tonic-gate }
686