xref: /onnv-gate/usr/src/cmd/ttymon/tmchild.c (revision 9300:38fba18c3b97)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*9300SNobutomo.Nakano@Sun.COM  * Common Development and Distribution License (the "License").
6*9300SNobutomo.Nakano@Sun.COM  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*9300SNobutomo.Nakano@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
270Sstevel@tonic-gate /*	  All Rights Reserved  	*/
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include	<stdio.h>
300Sstevel@tonic-gate #include	<stdlib.h>
310Sstevel@tonic-gate #include	<fcntl.h>
320Sstevel@tonic-gate #include	<errno.h>
330Sstevel@tonic-gate #include	<sys/types.h>
340Sstevel@tonic-gate #include	<termio.h>
350Sstevel@tonic-gate #include	<string.h>
360Sstevel@tonic-gate #include	<signal.h>
370Sstevel@tonic-gate #include	<poll.h>
380Sstevel@tonic-gate #include	<unistd.h>
390Sstevel@tonic-gate #include 	"sys/stropts.h"
400Sstevel@tonic-gate #include 	<sys/resource.h>
410Sstevel@tonic-gate #include	"sac.h"
420Sstevel@tonic-gate #include	"ttymon.h"
430Sstevel@tonic-gate #include	"tmstruct.h"
440Sstevel@tonic-gate #include	"tmextern.h"
450Sstevel@tonic-gate #ifdef	SYS_NAME
460Sstevel@tonic-gate #include	<sys/utsname.h>
470Sstevel@tonic-gate #endif
480Sstevel@tonic-gate 
490Sstevel@tonic-gate static void openline();
500Sstevel@tonic-gate static void invoke_service();
510Sstevel@tonic-gate static char	*do_autobaud();
520Sstevel@tonic-gate static	struct	Gdef	*next_speed();
530Sstevel@tonic-gate static int check_hup();
540Sstevel@tonic-gate 
550Sstevel@tonic-gate extern	struct	Gdef	*get_speed();
560Sstevel@tonic-gate extern struct strbuf *peek_ptr, *do_peek();
570Sstevel@tonic-gate 
580Sstevel@tonic-gate /*
590Sstevel@tonic-gate  * tmchild	- process that handles peeking data, determine baud rate
600Sstevel@tonic-gate  *		  and invoke service on each individual port.
610Sstevel@tonic-gate  *
620Sstevel@tonic-gate  */
630Sstevel@tonic-gate void
tmchild(pmtab)640Sstevel@tonic-gate tmchild(pmtab)
650Sstevel@tonic-gate struct	pmtab	*pmtab;
660Sstevel@tonic-gate {
670Sstevel@tonic-gate 	register struct Gdef *speedef;
680Sstevel@tonic-gate 	char	*auto_speed = "";
690Sstevel@tonic-gate 	struct	sigaction sigact;
700Sstevel@tonic-gate 
710Sstevel@tonic-gate #ifdef	DEBUG
720Sstevel@tonic-gate 	debug("in tmchild");
730Sstevel@tonic-gate #endif
740Sstevel@tonic-gate 	peek_ptr = NULL;
750Sstevel@tonic-gate 	if (pmtab->p_status != GETTY) {
76*9300SNobutomo.Nakano@Sun.COM 		child_sigcatch();
77*9300SNobutomo.Nakano@Sun.COM 		(void) close(PCpipe[0]); /* close parent end of the pipe */
780Sstevel@tonic-gate 		if (ioctl(PCpipe[1], I_SETSIG, S_HANGUP) == -1) {
790Sstevel@tonic-gate 			log("I_SETSIG failed: %s", strerror(errno));
800Sstevel@tonic-gate 			exit(1);
810Sstevel@tonic-gate 		}
820Sstevel@tonic-gate 		/*
830Sstevel@tonic-gate 		 * the following check is to make sure no hangup
840Sstevel@tonic-gate 		 * happens before registering for SIGPOLL
850Sstevel@tonic-gate 		 */
860Sstevel@tonic-gate 		if (check_hup(PCpipe[1])) {
870Sstevel@tonic-gate #ifdef	DEBUG
880Sstevel@tonic-gate 			debug("PCpipe hungup, tmchild exiting");
890Sstevel@tonic-gate #endif
900Sstevel@tonic-gate 			exit(1);
910Sstevel@tonic-gate 		}
920Sstevel@tonic-gate 
930Sstevel@tonic-gate 		if (pmtab->p_ttyflags & (C_FLAG|B_FLAG)) {
940Sstevel@tonic-gate 			if (pmtab->p_fd > 0) {
95*9300SNobutomo.Nakano@Sun.COM 				(void) close(pmtab->p_fd);
96*9300SNobutomo.Nakano@Sun.COM 				pmtab->p_fd = 0;
970Sstevel@tonic-gate 			}
980Sstevel@tonic-gate 		}
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate 		/*
1010Sstevel@tonic-gate 		 * become the session leader so that a controlling tty
1020Sstevel@tonic-gate 		 * will be allocated.
1030Sstevel@tonic-gate 		 */
104*9300SNobutomo.Nakano@Sun.COM 		(void) setsid();
1050Sstevel@tonic-gate 	}
1060Sstevel@tonic-gate 	speedef = get_speed(pmtab->p_ttylabel);
107*9300SNobutomo.Nakano@Sun.COM 	openline(pmtab, speedef);
1080Sstevel@tonic-gate 	if (pmtab->p_ttyflags & (C_FLAG|B_FLAG)) {
1090Sstevel@tonic-gate 	    if (pmtab->p_fd >= 0) {
1100Sstevel@tonic-gate 		if ((pmtab->p_modules != NULL)&&(*(pmtab->p_modules) != '\0')) {
111*9300SNobutomo.Nakano@Sun.COM 		    if (push_linedisc(pmtab->p_fd, pmtab->p_modules, pmtab->p_device) == -1) {
112*9300SNobutomo.Nakano@Sun.COM 			(void) close(pmtab->p_fd);
1130Sstevel@tonic-gate 			return;
1140Sstevel@tonic-gate 		    }
1150Sstevel@tonic-gate 		}
1160Sstevel@tonic-gate 	    }
1170Sstevel@tonic-gate 	}
1180Sstevel@tonic-gate 	if ((pmtab->p_ttyflags & C_FLAG) &&
1190Sstevel@tonic-gate 	    (State != PM_DISABLED) &&
1200Sstevel@tonic-gate 	    (!(pmtab->p_flags & X_FLAG))) {
1210Sstevel@tonic-gate 		/*
1220Sstevel@tonic-gate 		 * if "c" flag is set, and the port is not disabled
123*9300SNobutomo.Nakano@Sun.COM 		 * invoke service immediately
1240Sstevel@tonic-gate 		 */
125*9300SNobutomo.Nakano@Sun.COM 		if (set_termio(0, speedef->g_fflags, NULL,FALSE,CANON) == -1) {
1260Sstevel@tonic-gate 			log("set final termio failed");
1270Sstevel@tonic-gate 			exit(1);
1280Sstevel@tonic-gate 		}
1290Sstevel@tonic-gate 		invoke_service(pmtab);
1300Sstevel@tonic-gate 		exit(1);	/*NOTREACHED*/
1310Sstevel@tonic-gate 	}
1320Sstevel@tonic-gate 	if (speedef->g_autobaud & A_FLAG) {
133*9300SNobutomo.Nakano@Sun.COM 		auto_speed = do_autobaud(pmtab, speedef);
1340Sstevel@tonic-gate 	}
135*9300SNobutomo.Nakano@Sun.COM 	if (set_termio(0, speedef->g_fflags, NULL, FALSE, CANON) == -1) {
1360Sstevel@tonic-gate 		log("set final termio failed");
1370Sstevel@tonic-gate 		exit(1);
1380Sstevel@tonic-gate 	}
139*9300SNobutomo.Nakano@Sun.COM 	if ((pmtab->p_ttyflags & (R_FLAG|A_FLAG)) ||
140*9300SNobutomo.Nakano@Sun.COM 	    (pmtab->p_status == GETTY) || (pmtab->p_timeout > 0)) {
141*9300SNobutomo.Nakano@Sun.COM 		write_prompt(1, pmtab, TRUE, TRUE);
142*9300SNobutomo.Nakano@Sun.COM 		if (pmtab->p_timeout) {
1430Sstevel@tonic-gate 			sigact.sa_flags = 0;
1440Sstevel@tonic-gate 			sigact.sa_handler = timedout;
145*9300SNobutomo.Nakano@Sun.COM 			(void) sigemptyset(&sigact.sa_mask);
146*9300SNobutomo.Nakano@Sun.COM 			(void) sigaction(SIGALRM, &sigact, NULL);
147*9300SNobutomo.Nakano@Sun.COM 			(void) alarm((unsigned)pmtab->p_timeout);
1480Sstevel@tonic-gate 		}
1490Sstevel@tonic-gate 	}
1500Sstevel@tonic-gate 	else if ((pmtab->p_ttyflags & (B_FLAG)))
151*9300SNobutomo.Nakano@Sun.COM 			write_prompt(pmtab->p_fd, pmtab, TRUE, TRUE);
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate 	/* Loop until user is successful in invoking service. */
155*9300SNobutomo.Nakano@Sun.COM 	for (;;) {
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate 		/* Peek the user's typed response and respond appropriately. */
158*9300SNobutomo.Nakano@Sun.COM 		switch (poll_data()) {
1590Sstevel@tonic-gate 		case GOODNAME:
1600Sstevel@tonic-gate #ifdef	DEBUG
1610Sstevel@tonic-gate 			debug("got GOODNAME");
162*9300SNobutomo.Nakano@Sun.COM #endif
1630Sstevel@tonic-gate 			if (pmtab->p_timeout) {
164*9300SNobutomo.Nakano@Sun.COM 				(void) alarm((unsigned)0);
1650Sstevel@tonic-gate 				sigact.sa_flags = 0;
1660Sstevel@tonic-gate 				sigact.sa_handler = SIG_DFL;
167*9300SNobutomo.Nakano@Sun.COM 				(void) sigemptyset(&sigact.sa_mask);
168*9300SNobutomo.Nakano@Sun.COM 				(void) sigaction(SIGALRM, &sigact, NULL);
1690Sstevel@tonic-gate 			}
1700Sstevel@tonic-gate 			if ((State == PM_DISABLED)||(pmtab->p_flags & X_FLAG)){
171*9300SNobutomo.Nakano@Sun.COM 				write_prompt(1, pmtab, TRUE, FALSE);
1720Sstevel@tonic-gate 				break;
1730Sstevel@tonic-gate 			}
174*9300SNobutomo.Nakano@Sun.COM 			if (set_termio(0, speedef->g_fflags, auto_speed,
175*9300SNobutomo.Nakano@Sun.COM 			    FALSE, CANON) == -1) {
1760Sstevel@tonic-gate 				log("set final termio failed");
1770Sstevel@tonic-gate 				exit(1);
1780Sstevel@tonic-gate 			}
1790Sstevel@tonic-gate 			invoke_service(pmtab);
1800Sstevel@tonic-gate 			exit(1);	/*NOTREACHED*/
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 		case BADSPEED:
1830Sstevel@tonic-gate 			/* wrong speed! try next speed in the list. */
1840Sstevel@tonic-gate 			speedef = next_speed(speedef);
1850Sstevel@tonic-gate #ifdef	DEBUG
1860Sstevel@tonic-gate 			debug("BADSPEED: setup next speed");
1870Sstevel@tonic-gate #endif
1880Sstevel@tonic-gate 			if (speedef->g_autobaud & A_FLAG) {
1890Sstevel@tonic-gate 				if (auto_termio(0) == -1) {
1900Sstevel@tonic-gate 					exit(1);
1910Sstevel@tonic-gate 				}
192*9300SNobutomo.Nakano@Sun.COM 				auto_speed = do_autobaud(pmtab, speedef);
1930Sstevel@tonic-gate 			}
1940Sstevel@tonic-gate 			else {
1950Sstevel@tonic-gate 				auto_speed = NULL;
1960Sstevel@tonic-gate 				/*
197*9300SNobutomo.Nakano@Sun.COM 				 * this reset may fail if the speed is not
1980Sstevel@tonic-gate 				 * supported by the system
1990Sstevel@tonic-gate 				 * we just cycle through it to the next one
2000Sstevel@tonic-gate 				 */
201*9300SNobutomo.Nakano@Sun.COM 				if (set_termio(0, speedef->g_iflags, NULL,
202*9300SNobutomo.Nakano@Sun.COM 				    FALSE, CANON) != 0) {
2030Sstevel@tonic-gate 					log("Warning -- speed of <%s> may "
2040Sstevel@tonic-gate 					    "be not supported by the system",
2050Sstevel@tonic-gate 					    speedef->g_id);
2060Sstevel@tonic-gate 				}
2070Sstevel@tonic-gate 			}
208*9300SNobutomo.Nakano@Sun.COM 			write_prompt(1, pmtab, TRUE, TRUE);
2090Sstevel@tonic-gate 			break;
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate 		case NONAME:
2120Sstevel@tonic-gate #ifdef	DEBUG
2130Sstevel@tonic-gate 			debug("got NONAME");
214*9300SNobutomo.Nakano@Sun.COM #endif
215*9300SNobutomo.Nakano@Sun.COM 			write_prompt(1, pmtab, FALSE, FALSE);
2160Sstevel@tonic-gate 			break;
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate 		}  /* end switch */
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate 		peek_ptr = NULL;
221*9300SNobutomo.Nakano@Sun.COM 		if (pmtab->p_timeout) {
2220Sstevel@tonic-gate 			sigact.sa_flags = 0;
2230Sstevel@tonic-gate 			sigact.sa_handler = timedout;
224*9300SNobutomo.Nakano@Sun.COM 			(void) sigemptyset(&sigact.sa_mask);
225*9300SNobutomo.Nakano@Sun.COM 			(void) sigaction(SIGALRM, &sigact, NULL);
226*9300SNobutomo.Nakano@Sun.COM 			(void) alarm((unsigned)pmtab->p_timeout);
2270Sstevel@tonic-gate 		}
2280Sstevel@tonic-gate 	} /* end for loop */
2290Sstevel@tonic-gate }
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate static void
openline(pmtab,speedef)2320Sstevel@tonic-gate openline(pmtab, speedef)
2330Sstevel@tonic-gate struct	pmtab 	*pmtab;
2340Sstevel@tonic-gate struct Gdef *speedef;
2350Sstevel@tonic-gate {
2360Sstevel@tonic-gate 	char	 buffer[5];
2370Sstevel@tonic-gate 	int	 rtn = 0;
2380Sstevel@tonic-gate 	int	 line_count;
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate #ifdef	DEBUG
2410Sstevel@tonic-gate 	debug("in openline");
2420Sstevel@tonic-gate #endif
2430Sstevel@tonic-gate 	if (pmtab->p_status != GETTY) {
244*9300SNobutomo.Nakano@Sun.COM 		(void) close(0);
2450Sstevel@tonic-gate 		/* open should return fd 0, if not, then close it */
2460Sstevel@tonic-gate 		if ((pmtab->p_fd = open(pmtab->p_device, O_RDWR)) != 0) {
2470Sstevel@tonic-gate 			log("open \"%s\" failed: %s", pmtab->p_device,
2480Sstevel@tonic-gate 			    strerror(errno));
2490Sstevel@tonic-gate 			exit(1);
2500Sstevel@tonic-gate 		}
2510Sstevel@tonic-gate 	}
252*9300SNobutomo.Nakano@Sun.COM 	(void) close(1);
253*9300SNobutomo.Nakano@Sun.COM 	(void) close(2);
254*9300SNobutomo.Nakano@Sun.COM 	(void) dup(0);
255*9300SNobutomo.Nakano@Sun.COM 	(void) dup(0);
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate 	if (pmtab->p_ttyflags & R_FLAG) { /* wait_read is needed */
258*9300SNobutomo.Nakano@Sun.COM 		if (pmtab->p_count) {
259*9300SNobutomo.Nakano@Sun.COM 			if (peek_ptr != NULL)
260*9300SNobutomo.Nakano@Sun.COM 				if ((peek_ptr->buf[0]&0x7F) == '\n' ||
261*9300SNobutomo.Nakano@Sun.COM 				    (peek_ptr->buf[0]&0x7F) == '\r')
2620Sstevel@tonic-gate 					pmtab->p_count--;
263*9300SNobutomo.Nakano@Sun.COM 
264*9300SNobutomo.Nakano@Sun.COM 			/*
265*9300SNobutomo.Nakano@Sun.COM 			 * - wait for "p_count" lines
2660Sstevel@tonic-gate 			 * - datakit switch does not
2670Sstevel@tonic-gate 			 *   know you are a host or a terminal
268*9300SNobutomo.Nakano@Sun.COM 			 * - so it send you several lines of msg
2690Sstevel@tonic-gate 			 * - we need to swallow that msg
2700Sstevel@tonic-gate 			 * - we assume the baud rate is correct
2710Sstevel@tonic-gate 			 * - if it is not, '\n' will not look like '\n'
272*9300SNobutomo.Nakano@Sun.COM 			 * and we will wait forever here
2730Sstevel@tonic-gate 			 */
274*9300SNobutomo.Nakano@Sun.COM 			if (set_termio(0, speedef->g_fflags, NULL, TRUE, CANON) == -1) {
2750Sstevel@tonic-gate 				log("set final termio failed");
2760Sstevel@tonic-gate 				exit(1);
2770Sstevel@tonic-gate 			}
278*9300SNobutomo.Nakano@Sun.COM 			for (line_count = 0; line_count < pmtab->p_count; ) {
279*9300SNobutomo.Nakano@Sun.COM 				if (read(0, buffer, 1) < 0
280*9300SNobutomo.Nakano@Sun.COM 				    || *buffer == '\0'
281*9300SNobutomo.Nakano@Sun.COM 				    || *buffer == '\004') {
282*9300SNobutomo.Nakano@Sun.COM 					(void) close(0);
2830Sstevel@tonic-gate 					exit(0);
2840Sstevel@tonic-gate 				}
2850Sstevel@tonic-gate 				if (*buffer == '\n')
2860Sstevel@tonic-gate 					line_count++;
2870Sstevel@tonic-gate 			}
2880Sstevel@tonic-gate 		}
2890Sstevel@tonic-gate 		else { /* wait for 1 char */
290*9300SNobutomo.Nakano@Sun.COM 			if (peek_ptr == NULL) {
291*9300SNobutomo.Nakano@Sun.COM 				if (set_termio(0, NULL, NULL,TRUE,RAW) == -1) {
2920Sstevel@tonic-gate 					log("set termio RAW failed");
2930Sstevel@tonic-gate 					exit(1);
2940Sstevel@tonic-gate 				}
2950Sstevel@tonic-gate 				rtn = read(0, buffer, 1);
2960Sstevel@tonic-gate 			} else
2970Sstevel@tonic-gate 				*buffer = (peek_ptr->buf[0]&0x7F);
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate 			/*
3000Sstevel@tonic-gate 			 * NOTE: Cu on a direct line when ~. is encountered will
3010Sstevel@tonic-gate 			 * send EOTs to the other side.  EOT=\004
3020Sstevel@tonic-gate 			 */
303*9300SNobutomo.Nakano@Sun.COM 			if (rtn < 0 || *buffer == '\004') {
304*9300SNobutomo.Nakano@Sun.COM 				(void) close(0);
3050Sstevel@tonic-gate 				exit(0);
3060Sstevel@tonic-gate 			}
3070Sstevel@tonic-gate 		}
3080Sstevel@tonic-gate 		peek_ptr = NULL;
3090Sstevel@tonic-gate 		if (!(pmtab->p_ttyflags & A_FLAG)) { /* autobaud not enabled */
310*9300SNobutomo.Nakano@Sun.COM 			if (set_termio(0, speedef->g_fflags, NULL, TRUE, CANON) == -1) {
3110Sstevel@tonic-gate 				log("set final termio failed");
3120Sstevel@tonic-gate 				exit(1);
3130Sstevel@tonic-gate 			}
3140Sstevel@tonic-gate 		}
3150Sstevel@tonic-gate 	}
3160Sstevel@tonic-gate 	if (pmtab->p_ttyflags & B_FLAG) { /* port is bi-directional */
3170Sstevel@tonic-gate 		/* set advisory lock on the line */
318*9300SNobutomo.Nakano@Sun.COM 		if (tm_lock(0) != 0) {
3190Sstevel@tonic-gate 			/*
320*9300SNobutomo.Nakano@Sun.COM 			 * device is locked
3210Sstevel@tonic-gate 			 * child exits and let the parent wait for
3220Sstevel@tonic-gate 			 * the lock to go away
3230Sstevel@tonic-gate 			 */
3240Sstevel@tonic-gate 			exit(0);
3250Sstevel@tonic-gate 		}
3260Sstevel@tonic-gate 		/* change ownership back to root */
327*9300SNobutomo.Nakano@Sun.COM 		(void) fchown(0, ROOTUID, Tty_gid);
328*9300SNobutomo.Nakano@Sun.COM 		(void) fchmod(0, 0620);
3290Sstevel@tonic-gate 	}
3300Sstevel@tonic-gate 	return;
3310Sstevel@tonic-gate }
3320Sstevel@tonic-gate 
3330Sstevel@tonic-gate /*
3340Sstevel@tonic-gate  *	write_prompt	- write the msg to fd
3350Sstevel@tonic-gate  *			- if flush is set, flush input queue
336*9300SNobutomo.Nakano@Sun.COM  *			- if clear is set, write a new line
3370Sstevel@tonic-gate  */
3380Sstevel@tonic-gate void
write_prompt(fd,pmtab,flush,clear)339*9300SNobutomo.Nakano@Sun.COM write_prompt(fd, pmtab, flush, clear)
3400Sstevel@tonic-gate int	fd;
3410Sstevel@tonic-gate struct	pmtab	*pmtab;
3420Sstevel@tonic-gate int	flush, clear;
3430Sstevel@tonic-gate {
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate #ifdef DEBUG
3460Sstevel@tonic-gate 	debug("in write_prompt");
3470Sstevel@tonic-gate #endif
3480Sstevel@tonic-gate 	if (flush)
3490Sstevel@tonic-gate 		flush_input(fd);
3500Sstevel@tonic-gate 	if (clear) {
351*9300SNobutomo.Nakano@Sun.COM 		(void) write(fd, "\r\n", 2);
3520Sstevel@tonic-gate 	}
3530Sstevel@tonic-gate #ifdef SYS_NAME
3540Sstevel@tonic-gate 	sys_name(fd);
3550Sstevel@tonic-gate #endif
3560Sstevel@tonic-gate 	/* Print prompt/disable message. */
357*9300SNobutomo.Nakano@Sun.COM 	if ((State == PM_DISABLED) || (pmtab->p_flags & X_FLAG))
3580Sstevel@tonic-gate 		(void)write(fd, pmtab->p_dmsg, (unsigned)strlen(pmtab->p_dmsg));
3590Sstevel@tonic-gate 	else
360*9300SNobutomo.Nakano@Sun.COM 		(void) write(fd, pmtab->p_prompt,
3610Sstevel@tonic-gate 			(unsigned)strlen(pmtab->p_prompt));
3620Sstevel@tonic-gate }
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate /*
3650Sstevel@tonic-gate  *	timedout	- input period timed out
3660Sstevel@tonic-gate  */
3670Sstevel@tonic-gate void
timedout()3680Sstevel@tonic-gate timedout()
3690Sstevel@tonic-gate {
3700Sstevel@tonic-gate 	exit(1);
3710Sstevel@tonic-gate }
3720Sstevel@tonic-gate 
3730Sstevel@tonic-gate #ifdef SYS_NAME
3740Sstevel@tonic-gate /*
3750Sstevel@tonic-gate  * void sys_name() - generate a msg with system id
3760Sstevel@tonic-gate  *		   - print out /etc/issue file if it exists
3770Sstevel@tonic-gate  */
3780Sstevel@tonic-gate void
sys_name(fd)3790Sstevel@tonic-gate sys_name(fd)
3800Sstevel@tonic-gate int	fd;
3810Sstevel@tonic-gate {
3820Sstevel@tonic-gate 	char	*ptr, buffer[BUFSIZ];
3830Sstevel@tonic-gate 	FILE	*fp;
3840Sstevel@tonic-gate 
3850Sstevel@tonic-gate #if 0	/* 1111333 - don't print node name, we already do this elsewhere */
3860Sstevel@tonic-gate 	struct	utsname utsname;
3870Sstevel@tonic-gate 
3880Sstevel@tonic-gate 	if (uname(&utsname) != FAILURE) {
389*9300SNobutomo.Nakano@Sun.COM 		(void) sprintf(buffer, "%.9s\r\n", utsname.nodename);
390*9300SNobutomo.Nakano@Sun.COM 		(void) write(fd, buffer, strlen(buffer));
3910Sstevel@tonic-gate 	}
3920Sstevel@tonic-gate #endif
3930Sstevel@tonic-gate 
394*9300SNobutomo.Nakano@Sun.COM 	if ((fp = fopen(ISSUEFILE, "r")) != NULL) {
395*9300SNobutomo.Nakano@Sun.COM 		while ((ptr = fgets(buffer, sizeof (buffer), fp)) != NULL) {
396*9300SNobutomo.Nakano@Sun.COM 			(void) write(fd, ptr, strlen(ptr));
3970Sstevel@tonic-gate 		}
398*9300SNobutomo.Nakano@Sun.COM 		(void) fclose(fp);
3990Sstevel@tonic-gate 	}
4000Sstevel@tonic-gate }
4010Sstevel@tonic-gate #endif
4020Sstevel@tonic-gate 
4030Sstevel@tonic-gate 
4040Sstevel@tonic-gate /*
4050Sstevel@tonic-gate  *	do_autobaud	- do autobaud
4060Sstevel@tonic-gate  *			- if it succeed, set the new speed and return
4070Sstevel@tonic-gate  *			- if it failed, it will get the nextlabel
4080Sstevel@tonic-gate  *			- if next entry is also autobaud,
4090Sstevel@tonic-gate  *			  it will loop back to do autobaud again
4100Sstevel@tonic-gate  *			- otherwise, it will set new termio and return
4110Sstevel@tonic-gate  */
4120Sstevel@tonic-gate static	char	*
do_autobaud(pmtab,speedef)413*9300SNobutomo.Nakano@Sun.COM do_autobaud(pmtab, speedef)
4140Sstevel@tonic-gate struct	pmtab	*pmtab;
4150Sstevel@tonic-gate struct	Gdef	*speedef;
4160Sstevel@tonic-gate {
4170Sstevel@tonic-gate 	int	done = FALSE;
4180Sstevel@tonic-gate 	char	*auto_speed;
4190Sstevel@tonic-gate #ifdef	DEBUG
4200Sstevel@tonic-gate 	debug("in do_autobaud");
4210Sstevel@tonic-gate #endif
4220Sstevel@tonic-gate 	while (!done) {
423*9300SNobutomo.Nakano@Sun.COM 		if ((auto_speed = autobaud(0, pmtab->p_timeout)) == NULL) {
4240Sstevel@tonic-gate 			speedef = next_speed(speedef);
4250Sstevel@tonic-gate 			if (speedef->g_autobaud & A_FLAG) {
4260Sstevel@tonic-gate 				continue;
4270Sstevel@tonic-gate 			}
4280Sstevel@tonic-gate 			else {
429*9300SNobutomo.Nakano@Sun.COM 				if (set_termio(0, speedef->g_iflags, NULL,
430*9300SNobutomo.Nakano@Sun.COM 						TRUE, CANON) != 0) {
4310Sstevel@tonic-gate 					exit(1);
4320Sstevel@tonic-gate 				}
4330Sstevel@tonic-gate 				done = TRUE;
4340Sstevel@tonic-gate 			}
4350Sstevel@tonic-gate 		}
4360Sstevel@tonic-gate 		else {
437*9300SNobutomo.Nakano@Sun.COM 			if (set_termio(0, speedef->g_fflags, auto_speed,
438*9300SNobutomo.Nakano@Sun.COM 					TRUE, CANON) != 0) {
4390Sstevel@tonic-gate 				exit(1);
4400Sstevel@tonic-gate 			}
4410Sstevel@tonic-gate 			done = TRUE;
4420Sstevel@tonic-gate 		}
4430Sstevel@tonic-gate 	}
4440Sstevel@tonic-gate #ifdef	DEBUG
4450Sstevel@tonic-gate 	debug("autobaud done");
4460Sstevel@tonic-gate #endif
447*9300SNobutomo.Nakano@Sun.COM 	return (auto_speed);
4480Sstevel@tonic-gate }
4490Sstevel@tonic-gate 
4500Sstevel@tonic-gate /*
451*9300SNobutomo.Nakano@Sun.COM  * 	next_speed(speedef)
4520Sstevel@tonic-gate  *	- find the next entry according to nextlabel. If "nextlabel"
4530Sstevel@tonic-gate  *	  is not valid, go back to the old ttylabel.
4540Sstevel@tonic-gate  */
4550Sstevel@tonic-gate 
4560Sstevel@tonic-gate static	struct	Gdef *
next_speed(speedef)4570Sstevel@tonic-gate next_speed(speedef)
4580Sstevel@tonic-gate struct	Gdef *speedef;
4590Sstevel@tonic-gate {
4600Sstevel@tonic-gate 	struct	Gdef *sp;
4610Sstevel@tonic-gate 
462*9300SNobutomo.Nakano@Sun.COM 	if (strcmp(speedef->g_nextid, speedef->g_id) == 0)
463*9300SNobutomo.Nakano@Sun.COM 		return (speedef);
4640Sstevel@tonic-gate 	if ((sp = find_def(speedef->g_nextid)) == NULL) {
4650Sstevel@tonic-gate 		log("%s's next speed-label (%s) is bad.", speedef->g_id,
4660Sstevel@tonic-gate 		    speedef->g_nextid);
4670Sstevel@tonic-gate 
4680Sstevel@tonic-gate 		/* go back to the original entry. */
469*9300SNobutomo.Nakano@Sun.COM 		if ((sp = find_def(speedef->g_id)) == NULL) {
470*9300SNobutomo.Nakano@Sun.COM 			/* if failed, complain and quit. */
4710Sstevel@tonic-gate 			log("unable to find (%s) again", speedef->g_id);
4720Sstevel@tonic-gate 			exit(1);
4730Sstevel@tonic-gate 		}
4740Sstevel@tonic-gate 	}
475*9300SNobutomo.Nakano@Sun.COM 	return (sp);
4760Sstevel@tonic-gate }
4770Sstevel@tonic-gate 
4780Sstevel@tonic-gate /*
4790Sstevel@tonic-gate  * inform_parent()	- inform ttymon that tmchild is going to exec service
4800Sstevel@tonic-gate  */
4810Sstevel@tonic-gate static	void
inform_parent(fd)4820Sstevel@tonic-gate inform_parent(fd)
4830Sstevel@tonic-gate int	fd;
4840Sstevel@tonic-gate {
4850Sstevel@tonic-gate 	pid_t	pid;
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate 	pid = getpid();
488*9300SNobutomo.Nakano@Sun.COM 	(void) write(fd, &pid, sizeof (pid));
4890Sstevel@tonic-gate }
4900Sstevel@tonic-gate 
4910Sstevel@tonic-gate static	char	 pbuf[BUFSIZ];	/* static buf for TTYPROMPT 	*/
4920Sstevel@tonic-gate static	char	 hbuf[BUFSIZ];	/* static buf for HOME 		*/
4930Sstevel@tonic-gate static	char	 tbuf[BUFSIZ];	/* static buf for TERM		*/
4940Sstevel@tonic-gate 
4950Sstevel@tonic-gate /*
4960Sstevel@tonic-gate  * void invoke_service	- invoke the service
4970Sstevel@tonic-gate  */
4980Sstevel@tonic-gate 
4990Sstevel@tonic-gate static	void
invoke_service(pmtab)5000Sstevel@tonic-gate invoke_service(pmtab)
5010Sstevel@tonic-gate struct	pmtab	*pmtab;
5020Sstevel@tonic-gate {
5030Sstevel@tonic-gate 	char	 *argvp[MAXARGS];		/* service cmd args */
5040Sstevel@tonic-gate 	int	 cnt = 0;			/* arg counter */
5050Sstevel@tonic-gate 	int	 i, fd;
5060Sstevel@tonic-gate 	struct	 sigaction	sigact;
5070Sstevel@tonic-gate 	extern	 struct	rlimit	Rlimit;
5080Sstevel@tonic-gate 
5090Sstevel@tonic-gate #ifdef 	DEBUG
5100Sstevel@tonic-gate 	debug("in invoke_service");
5110Sstevel@tonic-gate #endif
5120Sstevel@tonic-gate 
5130Sstevel@tonic-gate 	if (tcgetsid(0) != getsid(getpid())) {
5140Sstevel@tonic-gate 		cons_printf("Warning -- ttymon cannot allocate controlling "
5150Sstevel@tonic-gate 		    "tty on \"%s\",\n", pmtab->p_device);
5160Sstevel@tonic-gate 		cons_printf("\tThere may be another session active on this "
5170Sstevel@tonic-gate 		    "port.\n");
5180Sstevel@tonic-gate 
5190Sstevel@tonic-gate 		if (strcmp("/dev/console", pmtab->p_device) != 0) {
5200Sstevel@tonic-gate 			/*
5210Sstevel@tonic-gate 			 * if not on console, write to stderr to warn the user
5220Sstevel@tonic-gate 			 * also.
5230Sstevel@tonic-gate 			 */
5240Sstevel@tonic-gate 			(void) fprintf(stderr, "Warning -- ttymon cannot "
5250Sstevel@tonic-gate 			    "allocate controlling tty on \"%s\",\n",
5260Sstevel@tonic-gate 			    pmtab->p_device);
527*9300SNobutomo.Nakano@Sun.COM 			(void) fprintf(stderr, "\tthere may be another session "
5280Sstevel@tonic-gate 			    "active on this port.\n");
5290Sstevel@tonic-gate 		}
5300Sstevel@tonic-gate 	}
5310Sstevel@tonic-gate 
5320Sstevel@tonic-gate 	if (pmtab->p_status != GETTY) {
5330Sstevel@tonic-gate 		inform_parent(PCpipe[1]);
5340Sstevel@tonic-gate 		sigact.sa_flags = 0;
5350Sstevel@tonic-gate 		sigact.sa_handler = SIG_DFL;
536*9300SNobutomo.Nakano@Sun.COM 		(void) sigemptyset(&sigact.sa_mask);
537*9300SNobutomo.Nakano@Sun.COM 		(void) sigaction(SIGPOLL, &sigact, NULL);
5380Sstevel@tonic-gate 	}
5390Sstevel@tonic-gate 
5400Sstevel@tonic-gate 	if (pmtab->p_flags & U_FLAG) {
5410Sstevel@tonic-gate 		if (account(pmtab->p_device) != 0) {
5420Sstevel@tonic-gate 			log("invoke_service: account failed");
5430Sstevel@tonic-gate 			exit(1);
5440Sstevel@tonic-gate 		}
5450Sstevel@tonic-gate 	}
5460Sstevel@tonic-gate 
5470Sstevel@tonic-gate 	/* parse command line */
548*9300SNobutomo.Nakano@Sun.COM 	mkargv(pmtab->p_server, &argvp[0], &cnt, MAXARGS-1);
5490Sstevel@tonic-gate 
5500Sstevel@tonic-gate 	if (!(pmtab->p_ttyflags & C_FLAG)) {
5510Sstevel@tonic-gate 		(void) sprintf(pbuf, "TTYPROMPT=%s", pmtab->p_prompt);
5520Sstevel@tonic-gate 		if (putenv(pbuf)) {
5530Sstevel@tonic-gate 			log("cannot expand service <%s> environment", argvp[0]);
5540Sstevel@tonic-gate 			exit(1);
5550Sstevel@tonic-gate 		}
5560Sstevel@tonic-gate 	}
5570Sstevel@tonic-gate 	if (pmtab->p_status != GETTY) {
5580Sstevel@tonic-gate 		(void) sprintf(hbuf, "HOME=%s", pmtab->p_dir);
5590Sstevel@tonic-gate 		if (putenv(hbuf)) {
560*9300SNobutomo.Nakano@Sun.COM 			log("cannot expand service <%s> environment", argvp[0]);
5610Sstevel@tonic-gate 			exit(1);
5620Sstevel@tonic-gate 		}
5630Sstevel@tonic-gate #ifdef	DEBUG
5640Sstevel@tonic-gate 		debug("about to run config script");
5650Sstevel@tonic-gate #endif
5660Sstevel@tonic-gate 		if ((i = doconfig(0, pmtab->p_tag, 0)) != 0) {
5670Sstevel@tonic-gate 			if (i < 0) {
5680Sstevel@tonic-gate 				log("doconfig failed, system error");
5690Sstevel@tonic-gate 			}
5700Sstevel@tonic-gate 			else {
5710Sstevel@tonic-gate 				log("doconfig failed on line %d of script %s",
5720Sstevel@tonic-gate 				    i, pmtab->p_tag);
5730Sstevel@tonic-gate 			}
5740Sstevel@tonic-gate 			exit(1);
5750Sstevel@tonic-gate 		}
5760Sstevel@tonic-gate 	}
5770Sstevel@tonic-gate 
5780Sstevel@tonic-gate 	if (setgid(pmtab->p_gid)) {
5790Sstevel@tonic-gate 		log("cannot set group id to %ld: %s", pmtab->p_gid,
5800Sstevel@tonic-gate 		    strerror(errno));
5810Sstevel@tonic-gate 		exit(1);
5820Sstevel@tonic-gate 	}
5830Sstevel@tonic-gate 
5840Sstevel@tonic-gate 	if (setuid(pmtab->p_uid)) {
5850Sstevel@tonic-gate 		log("cannot set user id to %ld: %s", pmtab->p_uid,
5860Sstevel@tonic-gate 		    strerror(errno));
5870Sstevel@tonic-gate 		exit(1);
5880Sstevel@tonic-gate 	}
5890Sstevel@tonic-gate 
5900Sstevel@tonic-gate 	if (chdir(pmtab->p_dir)) {
5910Sstevel@tonic-gate 		log("cannot chdir to %s: %s", pmtab->p_dir, strerror(errno));
5920Sstevel@tonic-gate 		exit(1);
5930Sstevel@tonic-gate 	}
5940Sstevel@tonic-gate 
5950Sstevel@tonic-gate 	if (pmtab->p_uid != ROOTUID) {
5960Sstevel@tonic-gate 		/* change ownership and mode of device */
597*9300SNobutomo.Nakano@Sun.COM 		(void) fchown(0, pmtab->p_uid, Tty_gid);
598*9300SNobutomo.Nakano@Sun.COM 		(void) fchmod(0, 0620);
5990Sstevel@tonic-gate 	}
6000Sstevel@tonic-gate 
6010Sstevel@tonic-gate 
6020Sstevel@tonic-gate 	if (pmtab->p_status != GETTY) {
6030Sstevel@tonic-gate 		sigact.sa_flags = 0;
6040Sstevel@tonic-gate 		sigact.sa_handler = SIG_DFL;
605*9300SNobutomo.Nakano@Sun.COM 		(void) sigemptyset(&sigact.sa_mask);
606*9300SNobutomo.Nakano@Sun.COM 		(void) sigaction(SIGINT, &sigact, NULL);
6070Sstevel@tonic-gate 		if (setrlimit(RLIMIT_NOFILE, &Rlimit) == -1) {
6080Sstevel@tonic-gate 			log("setrlimit failed: %s", strerror(errno));
6090Sstevel@tonic-gate 			exit(1);
6100Sstevel@tonic-gate 		}
6110Sstevel@tonic-gate 		/* invoke the service */
6120Sstevel@tonic-gate 		log("Starting service (%s) on %s", argvp[0], pmtab->p_device);
6130Sstevel@tonic-gate 	}
6140Sstevel@tonic-gate 
6150Sstevel@tonic-gate 	if (pmtab->p_termtype != (char *)NULL) {
6160Sstevel@tonic-gate 		(void) sprintf(tbuf, "TERM=%s", pmtab->p_termtype);
6170Sstevel@tonic-gate 		if (putenv(tbuf)) {
6180Sstevel@tonic-gate 			log("cannot expand service <%s> environment", argvp[0]);
6190Sstevel@tonic-gate 			exit(1);
6200Sstevel@tonic-gate 		}
6210Sstevel@tonic-gate 	}
6220Sstevel@tonic-gate 	/* restore signal handlers and mask */
623*9300SNobutomo.Nakano@Sun.COM 	(void) sigaction(SIGINT, &Sigint, NULL);
624*9300SNobutomo.Nakano@Sun.COM 	(void) sigaction(SIGALRM, &Sigalrm, NULL);
625*9300SNobutomo.Nakano@Sun.COM 	(void) sigaction(SIGPOLL, &Sigpoll, NULL);
626*9300SNobutomo.Nakano@Sun.COM 	(void) sigaction(SIGQUIT, &Sigquit, NULL);
627*9300SNobutomo.Nakano@Sun.COM 	(void) sigaction(SIGCLD, &Sigcld, NULL);
628*9300SNobutomo.Nakano@Sun.COM 	(void) sigaction(SIGTERM, &Sigterm, NULL);
6290Sstevel@tonic-gate #ifdef	DEBUG
630*9300SNobutomo.Nakano@Sun.COM 	(void) sigaction(SIGUSR1, &Sigusr1, NULL);
631*9300SNobutomo.Nakano@Sun.COM 	(void) sigaction(SIGUSR2, &Sigusr2, NULL);
6320Sstevel@tonic-gate #endif
633*9300SNobutomo.Nakano@Sun.COM 	(void) sigprocmask(SIG_SETMASK, &Origmask, NULL);
6340Sstevel@tonic-gate 	(void) execve(argvp[0], argvp, environ);
6350Sstevel@tonic-gate 
6360Sstevel@tonic-gate 	/* exec returns only on failure! */
6370Sstevel@tonic-gate 	log("tmchild: exec service failed: %s", strerror(errno));
6380Sstevel@tonic-gate 	exit(1);
6390Sstevel@tonic-gate }
6400Sstevel@tonic-gate 
6410Sstevel@tonic-gate /*
6420Sstevel@tonic-gate  *	check_hup(fd)	- do a poll on fd to check if it is in hangup state
6430Sstevel@tonic-gate  *			- return 1 if hangup, otherwise return 0
6440Sstevel@tonic-gate  */
6450Sstevel@tonic-gate 
6460Sstevel@tonic-gate static	int
check_hup(fd)6470Sstevel@tonic-gate check_hup(fd)
6480Sstevel@tonic-gate int	fd;
6490Sstevel@tonic-gate {
6500Sstevel@tonic-gate 	int	ret;
6510Sstevel@tonic-gate 	struct	pollfd	pfd[1];
6520Sstevel@tonic-gate 
6530Sstevel@tonic-gate 	pfd[0].fd = fd;
6540Sstevel@tonic-gate 	pfd[0].events = POLLHUP;
6550Sstevel@tonic-gate 	for (;;) {
6560Sstevel@tonic-gate 		ret = poll(pfd, 1, 0);
6570Sstevel@tonic-gate 		if (ret < 0) {
658*9300SNobutomo.Nakano@Sun.COM 			if (errno == EINTR)
6590Sstevel@tonic-gate 				continue;
6600Sstevel@tonic-gate 			log("check_hup: poll failed: %s", strerror(errno));
6610Sstevel@tonic-gate 			exit(1);
6620Sstevel@tonic-gate 		}
6630Sstevel@tonic-gate 		else if (ret > 0) {
6640Sstevel@tonic-gate 			if (pfd[0].revents & POLLHUP) {
665*9300SNobutomo.Nakano@Sun.COM 				return (1);
6660Sstevel@tonic-gate 			}
6670Sstevel@tonic-gate 		}
668*9300SNobutomo.Nakano@Sun.COM 		return (0);
6690Sstevel@tonic-gate 	}
6700Sstevel@tonic-gate }
6710Sstevel@tonic-gate 
6720Sstevel@tonic-gate /*
6730Sstevel@tonic-gate  * sigpoll()	- SIGPOLL handle for tmchild
6740Sstevel@tonic-gate  *		- when SIGPOLL is received by tmchild,
6750Sstevel@tonic-gate  *		  the pipe between ttymon and tmchild is broken.
6760Sstevel@tonic-gate  *		  Something must happen to ttymon.
6770Sstevel@tonic-gate  */
6780Sstevel@tonic-gate void
sigpoll()6790Sstevel@tonic-gate sigpoll()
6800Sstevel@tonic-gate {
6810Sstevel@tonic-gate #ifdef	DEBUG
6820Sstevel@tonic-gate 	debug("tmchild got SIGPOLL, exiting");
6830Sstevel@tonic-gate #endif
6840Sstevel@tonic-gate 	exit(1);
6850Sstevel@tonic-gate }
686