xref: /titanic_53/usr/src/cmd/ttymon/tmchild.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate #include	<stdio.h>
34*7c478bd9Sstevel@tonic-gate #include	<stdlib.h>
35*7c478bd9Sstevel@tonic-gate #include	<fcntl.h>
36*7c478bd9Sstevel@tonic-gate #include	<errno.h>
37*7c478bd9Sstevel@tonic-gate #include	<sys/types.h>
38*7c478bd9Sstevel@tonic-gate #include	<termio.h>
39*7c478bd9Sstevel@tonic-gate #include	<string.h>
40*7c478bd9Sstevel@tonic-gate #include	<signal.h>
41*7c478bd9Sstevel@tonic-gate #include	<poll.h>
42*7c478bd9Sstevel@tonic-gate #include	<unistd.h>
43*7c478bd9Sstevel@tonic-gate #include 	"sys/stropts.h"
44*7c478bd9Sstevel@tonic-gate #include 	<sys/resource.h>
45*7c478bd9Sstevel@tonic-gate #include	"sac.h"
46*7c478bd9Sstevel@tonic-gate #include	"ttymon.h"
47*7c478bd9Sstevel@tonic-gate #include	"tmstruct.h"
48*7c478bd9Sstevel@tonic-gate #include	"tmextern.h"
49*7c478bd9Sstevel@tonic-gate #ifdef	SYS_NAME
50*7c478bd9Sstevel@tonic-gate #include	<sys/utsname.h>
51*7c478bd9Sstevel@tonic-gate #endif
52*7c478bd9Sstevel@tonic-gate 
53*7c478bd9Sstevel@tonic-gate static void openline();
54*7c478bd9Sstevel@tonic-gate static void invoke_service();
55*7c478bd9Sstevel@tonic-gate static char	*do_autobaud();
56*7c478bd9Sstevel@tonic-gate static	struct	Gdef	*next_speed();
57*7c478bd9Sstevel@tonic-gate static int check_hup();
58*7c478bd9Sstevel@tonic-gate 
59*7c478bd9Sstevel@tonic-gate extern	struct	Gdef	*get_speed();
60*7c478bd9Sstevel@tonic-gate extern struct strbuf *peek_ptr, *do_peek();
61*7c478bd9Sstevel@tonic-gate 
62*7c478bd9Sstevel@tonic-gate /*
63*7c478bd9Sstevel@tonic-gate  * tmchild	- process that handles peeking data, determine baud rate
64*7c478bd9Sstevel@tonic-gate  *		  and invoke service on each individual port.
65*7c478bd9Sstevel@tonic-gate  *
66*7c478bd9Sstevel@tonic-gate  */
67*7c478bd9Sstevel@tonic-gate void
68*7c478bd9Sstevel@tonic-gate tmchild(pmtab)
69*7c478bd9Sstevel@tonic-gate struct	pmtab	*pmtab;
70*7c478bd9Sstevel@tonic-gate {
71*7c478bd9Sstevel@tonic-gate 	register struct Gdef *speedef;
72*7c478bd9Sstevel@tonic-gate 	char	*auto_speed = "";
73*7c478bd9Sstevel@tonic-gate 	struct	sigaction sigact;
74*7c478bd9Sstevel@tonic-gate 
75*7c478bd9Sstevel@tonic-gate #ifdef	DEBUG
76*7c478bd9Sstevel@tonic-gate 	debug("in tmchild");
77*7c478bd9Sstevel@tonic-gate #endif
78*7c478bd9Sstevel@tonic-gate 	peek_ptr = NULL;
79*7c478bd9Sstevel@tonic-gate 	if (pmtab->p_status != GETTY) {
80*7c478bd9Sstevel@tonic-gate 		child_sigcatch();
81*7c478bd9Sstevel@tonic-gate 		(void)close(PCpipe[0]);	  /* close parent end of the pipe */
82*7c478bd9Sstevel@tonic-gate 		if (ioctl(PCpipe[1], I_SETSIG, S_HANGUP) == -1) {
83*7c478bd9Sstevel@tonic-gate 			log("I_SETSIG failed: %s", strerror(errno));
84*7c478bd9Sstevel@tonic-gate 			exit(1);
85*7c478bd9Sstevel@tonic-gate 		}
86*7c478bd9Sstevel@tonic-gate 		/*
87*7c478bd9Sstevel@tonic-gate 		 * the following check is to make sure no hangup
88*7c478bd9Sstevel@tonic-gate 		 * happens before registering for SIGPOLL
89*7c478bd9Sstevel@tonic-gate 		 */
90*7c478bd9Sstevel@tonic-gate 		if (check_hup(PCpipe[1])) {
91*7c478bd9Sstevel@tonic-gate #ifdef	DEBUG
92*7c478bd9Sstevel@tonic-gate 			debug("PCpipe hungup, tmchild exiting");
93*7c478bd9Sstevel@tonic-gate #endif
94*7c478bd9Sstevel@tonic-gate 			exit(1);
95*7c478bd9Sstevel@tonic-gate 		}
96*7c478bd9Sstevel@tonic-gate 
97*7c478bd9Sstevel@tonic-gate 		if (pmtab->p_ttyflags & (C_FLAG|B_FLAG)) {
98*7c478bd9Sstevel@tonic-gate 			if (pmtab->p_fd > 0) {
99*7c478bd9Sstevel@tonic-gate 				(void)close(pmtab->p_fd);
100*7c478bd9Sstevel@tonic-gate 				pmtab->p_fd = 0;
101*7c478bd9Sstevel@tonic-gate 			}
102*7c478bd9Sstevel@tonic-gate 		}
103*7c478bd9Sstevel@tonic-gate 
104*7c478bd9Sstevel@tonic-gate 		/*
105*7c478bd9Sstevel@tonic-gate 		 * become the session leader so that a controlling tty
106*7c478bd9Sstevel@tonic-gate 		 * will be allocated.
107*7c478bd9Sstevel@tonic-gate 		 */
108*7c478bd9Sstevel@tonic-gate 		(void)setsid();
109*7c478bd9Sstevel@tonic-gate 	}
110*7c478bd9Sstevel@tonic-gate 	speedef = get_speed(pmtab->p_ttylabel);
111*7c478bd9Sstevel@tonic-gate 	openline(pmtab, speedef);
112*7c478bd9Sstevel@tonic-gate 	if (pmtab->p_ttyflags & (C_FLAG|B_FLAG)) {
113*7c478bd9Sstevel@tonic-gate 	    if (pmtab->p_fd >= 0) {
114*7c478bd9Sstevel@tonic-gate 		if ((pmtab->p_modules != NULL)&&(*(pmtab->p_modules) != '\0')) {
115*7c478bd9Sstevel@tonic-gate 		    if (push_linedisc(pmtab->p_fd, pmtab->p_modules,pmtab->p_device) == -1) {
116*7c478bd9Sstevel@tonic-gate 			(void)close(pmtab->p_fd);
117*7c478bd9Sstevel@tonic-gate 			return;
118*7c478bd9Sstevel@tonic-gate 		    }
119*7c478bd9Sstevel@tonic-gate 		}
120*7c478bd9Sstevel@tonic-gate 	    }
121*7c478bd9Sstevel@tonic-gate 	}
122*7c478bd9Sstevel@tonic-gate 	if ((pmtab->p_ttyflags & C_FLAG) &&
123*7c478bd9Sstevel@tonic-gate 	    (State != PM_DISABLED) &&
124*7c478bd9Sstevel@tonic-gate 	    (!(pmtab->p_flags & X_FLAG))) {
125*7c478bd9Sstevel@tonic-gate 		/*
126*7c478bd9Sstevel@tonic-gate 		 * if "c" flag is set, and the port is not disabled
127*7c478bd9Sstevel@tonic-gate 		 * invoke service immediately
128*7c478bd9Sstevel@tonic-gate 		 */
129*7c478bd9Sstevel@tonic-gate 		if (set_termio(0,speedef->g_fflags,NULL,FALSE,CANON) == -1) {
130*7c478bd9Sstevel@tonic-gate 			log("set final termio failed");
131*7c478bd9Sstevel@tonic-gate 			exit(1);
132*7c478bd9Sstevel@tonic-gate 		}
133*7c478bd9Sstevel@tonic-gate 		invoke_service(pmtab);
134*7c478bd9Sstevel@tonic-gate 		exit(1);	/*NOTREACHED*/
135*7c478bd9Sstevel@tonic-gate 	}
136*7c478bd9Sstevel@tonic-gate 	if (speedef->g_autobaud & A_FLAG) {
137*7c478bd9Sstevel@tonic-gate 		auto_speed = do_autobaud(pmtab,speedef);
138*7c478bd9Sstevel@tonic-gate 	}
139*7c478bd9Sstevel@tonic-gate 	if (set_termio(0,speedef->g_fflags,NULL,FALSE,CANON) == -1) {
140*7c478bd9Sstevel@tonic-gate 		log("set final termio failed");
141*7c478bd9Sstevel@tonic-gate 		exit(1);
142*7c478bd9Sstevel@tonic-gate 	}
143*7c478bd9Sstevel@tonic-gate 	if ( (pmtab->p_ttyflags & (R_FLAG|A_FLAG)) ||
144*7c478bd9Sstevel@tonic-gate 		(pmtab->p_status == GETTY) || (pmtab->p_timeout > 0) ) {
145*7c478bd9Sstevel@tonic-gate 		write_prompt(1,pmtab,TRUE,TRUE);
146*7c478bd9Sstevel@tonic-gate 		if(pmtab->p_timeout) {
147*7c478bd9Sstevel@tonic-gate 			sigact.sa_flags = 0;
148*7c478bd9Sstevel@tonic-gate 			sigact.sa_handler = timedout;
149*7c478bd9Sstevel@tonic-gate 			(void)sigemptyset(&sigact.sa_mask);
150*7c478bd9Sstevel@tonic-gate 			(void)sigaction(SIGALRM, &sigact, NULL);
151*7c478bd9Sstevel@tonic-gate 			(void)alarm((unsigned)pmtab->p_timeout);
152*7c478bd9Sstevel@tonic-gate 		}
153*7c478bd9Sstevel@tonic-gate 	}
154*7c478bd9Sstevel@tonic-gate 	else if ((pmtab->p_ttyflags & (B_FLAG)))
155*7c478bd9Sstevel@tonic-gate 			write_prompt(pmtab->p_fd,pmtab,TRUE,TRUE);
156*7c478bd9Sstevel@tonic-gate 
157*7c478bd9Sstevel@tonic-gate 
158*7c478bd9Sstevel@tonic-gate 	/* Loop until user is successful in invoking service. */
159*7c478bd9Sstevel@tonic-gate 	for(;;) {
160*7c478bd9Sstevel@tonic-gate 
161*7c478bd9Sstevel@tonic-gate 		/* Peek the user's typed response and respond appropriately. */
162*7c478bd9Sstevel@tonic-gate 		switch(poll_data()) {
163*7c478bd9Sstevel@tonic-gate 		case GOODNAME:
164*7c478bd9Sstevel@tonic-gate #ifdef	DEBUG
165*7c478bd9Sstevel@tonic-gate 			debug("got GOODNAME");
166*7c478bd9Sstevel@tonic-gate #endif
167*7c478bd9Sstevel@tonic-gate 			if (pmtab->p_timeout) {
168*7c478bd9Sstevel@tonic-gate 				(void)alarm((unsigned)0);
169*7c478bd9Sstevel@tonic-gate 				sigact.sa_flags = 0;
170*7c478bd9Sstevel@tonic-gate 				sigact.sa_handler = SIG_DFL;
171*7c478bd9Sstevel@tonic-gate 				(void)sigemptyset(&sigact.sa_mask);
172*7c478bd9Sstevel@tonic-gate 				(void)sigaction(SIGALRM, &sigact, NULL);
173*7c478bd9Sstevel@tonic-gate 			}
174*7c478bd9Sstevel@tonic-gate 			if ((State == PM_DISABLED)||(pmtab->p_flags & X_FLAG)){
175*7c478bd9Sstevel@tonic-gate 				write_prompt(1,pmtab,TRUE,FALSE);
176*7c478bd9Sstevel@tonic-gate 				break;
177*7c478bd9Sstevel@tonic-gate 			}
178*7c478bd9Sstevel@tonic-gate 			if (set_termio(0,speedef->g_fflags,auto_speed,
179*7c478bd9Sstevel@tonic-gate 				FALSE,CANON)==-1) {
180*7c478bd9Sstevel@tonic-gate 				log("set final termio failed");
181*7c478bd9Sstevel@tonic-gate 				exit(1);
182*7c478bd9Sstevel@tonic-gate 			}
183*7c478bd9Sstevel@tonic-gate 			invoke_service(pmtab);
184*7c478bd9Sstevel@tonic-gate 			exit(1);	/*NOTREACHED*/
185*7c478bd9Sstevel@tonic-gate 
186*7c478bd9Sstevel@tonic-gate 		case BADSPEED:
187*7c478bd9Sstevel@tonic-gate 			/* wrong speed! try next speed in the list. */
188*7c478bd9Sstevel@tonic-gate 			speedef = next_speed(speedef);
189*7c478bd9Sstevel@tonic-gate #ifdef	DEBUG
190*7c478bd9Sstevel@tonic-gate 			debug("BADSPEED: setup next speed");
191*7c478bd9Sstevel@tonic-gate #endif
192*7c478bd9Sstevel@tonic-gate 			if (speedef->g_autobaud & A_FLAG) {
193*7c478bd9Sstevel@tonic-gate 				if (auto_termio(0) == -1) {
194*7c478bd9Sstevel@tonic-gate 					exit(1);
195*7c478bd9Sstevel@tonic-gate 				}
196*7c478bd9Sstevel@tonic-gate 				auto_speed = do_autobaud(pmtab,speedef);
197*7c478bd9Sstevel@tonic-gate 			}
198*7c478bd9Sstevel@tonic-gate 			else {
199*7c478bd9Sstevel@tonic-gate 				auto_speed = NULL;
200*7c478bd9Sstevel@tonic-gate 				/*
201*7c478bd9Sstevel@tonic-gate 				 * this reset may fail if the speed is not
202*7c478bd9Sstevel@tonic-gate 				 * supported by the system
203*7c478bd9Sstevel@tonic-gate 				 * we just cycle through it to the next one
204*7c478bd9Sstevel@tonic-gate 				 */
205*7c478bd9Sstevel@tonic-gate 				if (set_termio(0,speedef->g_iflags,NULL,
206*7c478bd9Sstevel@tonic-gate 						FALSE,CANON) != 0) {
207*7c478bd9Sstevel@tonic-gate 					log("Warning -- speed of <%s> may "
208*7c478bd9Sstevel@tonic-gate 					    "be not supported by the system",
209*7c478bd9Sstevel@tonic-gate 					    speedef->g_id);
210*7c478bd9Sstevel@tonic-gate 				}
211*7c478bd9Sstevel@tonic-gate 			}
212*7c478bd9Sstevel@tonic-gate 			write_prompt(1,pmtab,TRUE,TRUE);
213*7c478bd9Sstevel@tonic-gate 			break;
214*7c478bd9Sstevel@tonic-gate 
215*7c478bd9Sstevel@tonic-gate 		case NONAME:
216*7c478bd9Sstevel@tonic-gate #ifdef	DEBUG
217*7c478bd9Sstevel@tonic-gate 			debug("got NONAME");
218*7c478bd9Sstevel@tonic-gate #endif
219*7c478bd9Sstevel@tonic-gate 			write_prompt(1,pmtab,FALSE,FALSE);
220*7c478bd9Sstevel@tonic-gate 			break;
221*7c478bd9Sstevel@tonic-gate 
222*7c478bd9Sstevel@tonic-gate 		}  /* end switch */
223*7c478bd9Sstevel@tonic-gate 
224*7c478bd9Sstevel@tonic-gate 		peek_ptr = NULL;
225*7c478bd9Sstevel@tonic-gate 		if(pmtab->p_timeout) {
226*7c478bd9Sstevel@tonic-gate 			sigact.sa_flags = 0;
227*7c478bd9Sstevel@tonic-gate 			sigact.sa_handler = timedout;
228*7c478bd9Sstevel@tonic-gate 			(void)sigemptyset(&sigact.sa_mask);
229*7c478bd9Sstevel@tonic-gate 			(void)sigaction(SIGALRM, &sigact, NULL);
230*7c478bd9Sstevel@tonic-gate 			(void)alarm((unsigned)pmtab->p_timeout);
231*7c478bd9Sstevel@tonic-gate 		}
232*7c478bd9Sstevel@tonic-gate 	} /* end for loop */
233*7c478bd9Sstevel@tonic-gate }
234*7c478bd9Sstevel@tonic-gate 
235*7c478bd9Sstevel@tonic-gate static void
236*7c478bd9Sstevel@tonic-gate openline(pmtab, speedef)
237*7c478bd9Sstevel@tonic-gate struct	pmtab 	*pmtab;
238*7c478bd9Sstevel@tonic-gate struct Gdef *speedef;
239*7c478bd9Sstevel@tonic-gate {
240*7c478bd9Sstevel@tonic-gate 	char	 buffer[5];
241*7c478bd9Sstevel@tonic-gate 	int	 rtn = 0;
242*7c478bd9Sstevel@tonic-gate 	int	 line_count;
243*7c478bd9Sstevel@tonic-gate 
244*7c478bd9Sstevel@tonic-gate #ifdef	DEBUG
245*7c478bd9Sstevel@tonic-gate 	debug("in openline");
246*7c478bd9Sstevel@tonic-gate #endif
247*7c478bd9Sstevel@tonic-gate 	if (pmtab->p_status != GETTY) {
248*7c478bd9Sstevel@tonic-gate 		(void)close(0);
249*7c478bd9Sstevel@tonic-gate 		/* open should return fd 0, if not, then close it */
250*7c478bd9Sstevel@tonic-gate 		if ((pmtab->p_fd = open(pmtab->p_device, O_RDWR)) != 0) {
251*7c478bd9Sstevel@tonic-gate 			log("open \"%s\" failed: %s", pmtab->p_device,
252*7c478bd9Sstevel@tonic-gate 			    strerror(errno));
253*7c478bd9Sstevel@tonic-gate 			exit(1);
254*7c478bd9Sstevel@tonic-gate 		}
255*7c478bd9Sstevel@tonic-gate 	}
256*7c478bd9Sstevel@tonic-gate 	(void)close(1);
257*7c478bd9Sstevel@tonic-gate 	(void)close(2);
258*7c478bd9Sstevel@tonic-gate 	(void)dup(0);
259*7c478bd9Sstevel@tonic-gate 	(void)dup(0);
260*7c478bd9Sstevel@tonic-gate 
261*7c478bd9Sstevel@tonic-gate 	if (pmtab->p_ttyflags & R_FLAG) { /* wait_read is needed */
262*7c478bd9Sstevel@tonic-gate 		if (pmtab->p_count) {
263*7c478bd9Sstevel@tonic-gate 			if ( peek_ptr != NULL )
264*7c478bd9Sstevel@tonic-gate 				if ((peek_ptr->buf[0]&0x7F)=='\n' ||
265*7c478bd9Sstevel@tonic-gate 			     	    (peek_ptr->buf[0]&0x7F)=='\r')
266*7c478bd9Sstevel@tonic-gate 					pmtab->p_count--;
267*7c478bd9Sstevel@tonic-gate 
268*7c478bd9Sstevel@tonic-gate 			/*
269*7c478bd9Sstevel@tonic-gate 			 * - wait for "p_count" lines
270*7c478bd9Sstevel@tonic-gate 			 * - datakit switch does not
271*7c478bd9Sstevel@tonic-gate 			 *   know you are a host or a terminal
272*7c478bd9Sstevel@tonic-gate 			 * - so it send you several lines of msg
273*7c478bd9Sstevel@tonic-gate 			 * - we need to swallow that msg
274*7c478bd9Sstevel@tonic-gate 			 * - we assume the baud rate is correct
275*7c478bd9Sstevel@tonic-gate 			 * - if it is not, '\n' will not look like '\n'
276*7c478bd9Sstevel@tonic-gate  			 *   and we will wait forever here
277*7c478bd9Sstevel@tonic-gate 			 */
278*7c478bd9Sstevel@tonic-gate 			if (set_termio(0,speedef->g_fflags,NULL,TRUE,CANON) == -1) {
279*7c478bd9Sstevel@tonic-gate 				log("set final termio failed");
280*7c478bd9Sstevel@tonic-gate 				exit(1);
281*7c478bd9Sstevel@tonic-gate 			}
282*7c478bd9Sstevel@tonic-gate 			for (line_count=0;line_count < pmtab->p_count;) {
283*7c478bd9Sstevel@tonic-gate 				if ( read(0, buffer, 1) < 0
284*7c478bd9Sstevel@tonic-gate 				     || *buffer == '\0'
285*7c478bd9Sstevel@tonic-gate 				     || *buffer == '\004') {
286*7c478bd9Sstevel@tonic-gate 					(void)close(0);
287*7c478bd9Sstevel@tonic-gate 					exit(0);
288*7c478bd9Sstevel@tonic-gate 				}
289*7c478bd9Sstevel@tonic-gate 				if (*buffer == '\n')
290*7c478bd9Sstevel@tonic-gate 					line_count++;
291*7c478bd9Sstevel@tonic-gate 			}
292*7c478bd9Sstevel@tonic-gate 		}
293*7c478bd9Sstevel@tonic-gate 		else { /* wait for 1 char */
294*7c478bd9Sstevel@tonic-gate 			if ( peek_ptr == NULL ) {
295*7c478bd9Sstevel@tonic-gate 				if (set_termio(0,NULL,NULL,TRUE,RAW) == -1) {
296*7c478bd9Sstevel@tonic-gate 					log("set termio RAW failed");
297*7c478bd9Sstevel@tonic-gate 					exit(1);
298*7c478bd9Sstevel@tonic-gate 				}
299*7c478bd9Sstevel@tonic-gate 				rtn = read(0, buffer, 1);
300*7c478bd9Sstevel@tonic-gate 			} else
301*7c478bd9Sstevel@tonic-gate 				*buffer = (peek_ptr->buf[0]&0x7F);
302*7c478bd9Sstevel@tonic-gate 
303*7c478bd9Sstevel@tonic-gate 			/*
304*7c478bd9Sstevel@tonic-gate 			 * NOTE: Cu on a direct line when ~. is encountered will
305*7c478bd9Sstevel@tonic-gate 			 * send EOTs to the other side.  EOT=\004
306*7c478bd9Sstevel@tonic-gate 			 */
307*7c478bd9Sstevel@tonic-gate 			if ( rtn < 0 || *buffer == '\004') {
308*7c478bd9Sstevel@tonic-gate 				(void)close(0);
309*7c478bd9Sstevel@tonic-gate 				exit(0);
310*7c478bd9Sstevel@tonic-gate 			}
311*7c478bd9Sstevel@tonic-gate 		}
312*7c478bd9Sstevel@tonic-gate 		peek_ptr = NULL;
313*7c478bd9Sstevel@tonic-gate 		if (!(pmtab->p_ttyflags & A_FLAG)) { /* autobaud not enabled */
314*7c478bd9Sstevel@tonic-gate 			if (set_termio(0,speedef->g_fflags,NULL,TRUE,CANON) == -1) {
315*7c478bd9Sstevel@tonic-gate 				log("set final termio failed");
316*7c478bd9Sstevel@tonic-gate 				exit(1);
317*7c478bd9Sstevel@tonic-gate 			}
318*7c478bd9Sstevel@tonic-gate 		}
319*7c478bd9Sstevel@tonic-gate 	}
320*7c478bd9Sstevel@tonic-gate 	if (pmtab->p_ttyflags & B_FLAG) { /* port is bi-directional */
321*7c478bd9Sstevel@tonic-gate 		/* set advisory lock on the line */
322*7c478bd9Sstevel@tonic-gate 		if (tm_lock(0) != 0) {
323*7c478bd9Sstevel@tonic-gate 			/*
324*7c478bd9Sstevel@tonic-gate 			 * device is locked
325*7c478bd9Sstevel@tonic-gate 			 * child exits and let the parent wait for
326*7c478bd9Sstevel@tonic-gate 			 * the lock to go away
327*7c478bd9Sstevel@tonic-gate 			 */
328*7c478bd9Sstevel@tonic-gate 			exit(0);
329*7c478bd9Sstevel@tonic-gate 		}
330*7c478bd9Sstevel@tonic-gate 		/* change ownership back to root */
331*7c478bd9Sstevel@tonic-gate 		(void)fchown(0, ROOTUID, Tty_gid);
332*7c478bd9Sstevel@tonic-gate 		(void)fchmod(0, 0620);
333*7c478bd9Sstevel@tonic-gate 	}
334*7c478bd9Sstevel@tonic-gate 	return;
335*7c478bd9Sstevel@tonic-gate }
336*7c478bd9Sstevel@tonic-gate 
337*7c478bd9Sstevel@tonic-gate /*
338*7c478bd9Sstevel@tonic-gate  *	write_prompt	- write the msg to fd
339*7c478bd9Sstevel@tonic-gate  *			- if flush is set, flush input queue
340*7c478bd9Sstevel@tonic-gate  *			- if clear is set, write a new line
341*7c478bd9Sstevel@tonic-gate  */
342*7c478bd9Sstevel@tonic-gate void
343*7c478bd9Sstevel@tonic-gate write_prompt(fd,pmtab,flush,clear)
344*7c478bd9Sstevel@tonic-gate int	fd;
345*7c478bd9Sstevel@tonic-gate struct	pmtab	*pmtab;
346*7c478bd9Sstevel@tonic-gate int	flush, clear;
347*7c478bd9Sstevel@tonic-gate {
348*7c478bd9Sstevel@tonic-gate 
349*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
350*7c478bd9Sstevel@tonic-gate 	debug("in write_prompt");
351*7c478bd9Sstevel@tonic-gate #endif
352*7c478bd9Sstevel@tonic-gate 	if (flush)
353*7c478bd9Sstevel@tonic-gate 		flush_input(fd);
354*7c478bd9Sstevel@tonic-gate 	if (clear) {
355*7c478bd9Sstevel@tonic-gate 		(void)write(fd,"\r\n",2);
356*7c478bd9Sstevel@tonic-gate 	}
357*7c478bd9Sstevel@tonic-gate #ifdef SYS_NAME
358*7c478bd9Sstevel@tonic-gate 	sys_name(fd);
359*7c478bd9Sstevel@tonic-gate #endif
360*7c478bd9Sstevel@tonic-gate 	/* Print prompt/disable message. */
361*7c478bd9Sstevel@tonic-gate 	if ((State == PM_DISABLED)||(pmtab->p_flags & X_FLAG))
362*7c478bd9Sstevel@tonic-gate 		(void)write(fd, pmtab->p_dmsg, (unsigned)strlen(pmtab->p_dmsg));
363*7c478bd9Sstevel@tonic-gate 	else
364*7c478bd9Sstevel@tonic-gate 		(void)write(fd, pmtab->p_prompt,
365*7c478bd9Sstevel@tonic-gate 			(unsigned)strlen(pmtab->p_prompt));
366*7c478bd9Sstevel@tonic-gate }
367*7c478bd9Sstevel@tonic-gate 
368*7c478bd9Sstevel@tonic-gate /*
369*7c478bd9Sstevel@tonic-gate  *	timedout	- input period timed out
370*7c478bd9Sstevel@tonic-gate  */
371*7c478bd9Sstevel@tonic-gate void
372*7c478bd9Sstevel@tonic-gate timedout()
373*7c478bd9Sstevel@tonic-gate {
374*7c478bd9Sstevel@tonic-gate 	exit(1);
375*7c478bd9Sstevel@tonic-gate }
376*7c478bd9Sstevel@tonic-gate 
377*7c478bd9Sstevel@tonic-gate #ifdef SYS_NAME
378*7c478bd9Sstevel@tonic-gate /*
379*7c478bd9Sstevel@tonic-gate  * void sys_name() - generate a msg with system id
380*7c478bd9Sstevel@tonic-gate  *		   - print out /etc/issue file if it exists
381*7c478bd9Sstevel@tonic-gate  */
382*7c478bd9Sstevel@tonic-gate void
383*7c478bd9Sstevel@tonic-gate sys_name(fd)
384*7c478bd9Sstevel@tonic-gate int	fd;
385*7c478bd9Sstevel@tonic-gate {
386*7c478bd9Sstevel@tonic-gate 	char	*ptr, buffer[BUFSIZ];
387*7c478bd9Sstevel@tonic-gate 	FILE	*fp;
388*7c478bd9Sstevel@tonic-gate 
389*7c478bd9Sstevel@tonic-gate #if 0	/* 1111333 - don't print node name, we already do this elsewhere */
390*7c478bd9Sstevel@tonic-gate 	struct	utsname utsname;
391*7c478bd9Sstevel@tonic-gate 
392*7c478bd9Sstevel@tonic-gate 	if (uname(&utsname) != FAILURE) {
393*7c478bd9Sstevel@tonic-gate 		(void)sprintf(buffer,"%.9s\r\n", utsname.nodename);
394*7c478bd9Sstevel@tonic-gate 		(void) write(fd,buffer,strlen(buffer));
395*7c478bd9Sstevel@tonic-gate 	}
396*7c478bd9Sstevel@tonic-gate #endif
397*7c478bd9Sstevel@tonic-gate 
398*7c478bd9Sstevel@tonic-gate 	if ((fp = fopen(ISSUEFILE,"r")) != NULL) {
399*7c478bd9Sstevel@tonic-gate 		while ((ptr = fgets(buffer,sizeof(buffer),fp)) != NULL) {
400*7c478bd9Sstevel@tonic-gate 			(void)write(fd,ptr,strlen(ptr));
401*7c478bd9Sstevel@tonic-gate 		}
402*7c478bd9Sstevel@tonic-gate 		(void)fclose(fp);
403*7c478bd9Sstevel@tonic-gate 	}
404*7c478bd9Sstevel@tonic-gate }
405*7c478bd9Sstevel@tonic-gate #endif
406*7c478bd9Sstevel@tonic-gate 
407*7c478bd9Sstevel@tonic-gate 
408*7c478bd9Sstevel@tonic-gate /*
409*7c478bd9Sstevel@tonic-gate  *	do_autobaud	- do autobaud
410*7c478bd9Sstevel@tonic-gate  *			- if it succeed, set the new speed and return
411*7c478bd9Sstevel@tonic-gate  *			- if it failed, it will get the nextlabel
412*7c478bd9Sstevel@tonic-gate  *			- if next entry is also autobaud,
413*7c478bd9Sstevel@tonic-gate  *			  it will loop back to do autobaud again
414*7c478bd9Sstevel@tonic-gate  *			- otherwise, it will set new termio and return
415*7c478bd9Sstevel@tonic-gate  */
416*7c478bd9Sstevel@tonic-gate static	char	*
417*7c478bd9Sstevel@tonic-gate do_autobaud(pmtab,speedef)
418*7c478bd9Sstevel@tonic-gate struct	pmtab	*pmtab;
419*7c478bd9Sstevel@tonic-gate struct	Gdef	*speedef;
420*7c478bd9Sstevel@tonic-gate {
421*7c478bd9Sstevel@tonic-gate 	int	done = FALSE;
422*7c478bd9Sstevel@tonic-gate 	char	*auto_speed;
423*7c478bd9Sstevel@tonic-gate #ifdef	DEBUG
424*7c478bd9Sstevel@tonic-gate 	debug("in do_autobaud");
425*7c478bd9Sstevel@tonic-gate #endif
426*7c478bd9Sstevel@tonic-gate 	while (!done) {
427*7c478bd9Sstevel@tonic-gate 		if ((auto_speed = autobaud(0,pmtab->p_timeout)) == NULL) {
428*7c478bd9Sstevel@tonic-gate 			speedef = next_speed(speedef);
429*7c478bd9Sstevel@tonic-gate 			if (speedef->g_autobaud & A_FLAG) {
430*7c478bd9Sstevel@tonic-gate 				continue;
431*7c478bd9Sstevel@tonic-gate 			}
432*7c478bd9Sstevel@tonic-gate 			else {
433*7c478bd9Sstevel@tonic-gate 				if (set_termio(0,speedef->g_iflags,NULL,
434*7c478bd9Sstevel@tonic-gate 						TRUE,CANON) != 0) {
435*7c478bd9Sstevel@tonic-gate 					exit(1);
436*7c478bd9Sstevel@tonic-gate 				}
437*7c478bd9Sstevel@tonic-gate 				done = TRUE;
438*7c478bd9Sstevel@tonic-gate 			}
439*7c478bd9Sstevel@tonic-gate 		}
440*7c478bd9Sstevel@tonic-gate 		else {
441*7c478bd9Sstevel@tonic-gate 			if (set_termio(0,speedef->g_fflags,auto_speed,
442*7c478bd9Sstevel@tonic-gate 					TRUE,CANON) != 0) {
443*7c478bd9Sstevel@tonic-gate 				exit(1);
444*7c478bd9Sstevel@tonic-gate 			}
445*7c478bd9Sstevel@tonic-gate 			done = TRUE;
446*7c478bd9Sstevel@tonic-gate 		}
447*7c478bd9Sstevel@tonic-gate 	}
448*7c478bd9Sstevel@tonic-gate #ifdef	DEBUG
449*7c478bd9Sstevel@tonic-gate 	debug("autobaud done");
450*7c478bd9Sstevel@tonic-gate #endif
451*7c478bd9Sstevel@tonic-gate 	return(auto_speed);
452*7c478bd9Sstevel@tonic-gate }
453*7c478bd9Sstevel@tonic-gate 
454*7c478bd9Sstevel@tonic-gate /*
455*7c478bd9Sstevel@tonic-gate  * 	next_speed(speedef)
456*7c478bd9Sstevel@tonic-gate  *	- find the next entry according to nextlabel. If "nextlabel"
457*7c478bd9Sstevel@tonic-gate  *	  is not valid, go back to the old ttylabel.
458*7c478bd9Sstevel@tonic-gate  */
459*7c478bd9Sstevel@tonic-gate 
460*7c478bd9Sstevel@tonic-gate static	struct	Gdef *
461*7c478bd9Sstevel@tonic-gate next_speed(speedef)
462*7c478bd9Sstevel@tonic-gate struct	Gdef *speedef;
463*7c478bd9Sstevel@tonic-gate {
464*7c478bd9Sstevel@tonic-gate 	struct	Gdef *sp;
465*7c478bd9Sstevel@tonic-gate 
466*7c478bd9Sstevel@tonic-gate 	if (strcmp(speedef->g_nextid,speedef->g_id) == 0)
467*7c478bd9Sstevel@tonic-gate 		return(speedef);
468*7c478bd9Sstevel@tonic-gate 	if ((sp = find_def(speedef->g_nextid)) == NULL) {
469*7c478bd9Sstevel@tonic-gate 		log("%s's next speed-label (%s) is bad.", speedef->g_id,
470*7c478bd9Sstevel@tonic-gate 		    speedef->g_nextid);
471*7c478bd9Sstevel@tonic-gate 
472*7c478bd9Sstevel@tonic-gate 		/* go back to the original entry. */
473*7c478bd9Sstevel@tonic-gate 		if((sp = find_def(speedef->g_id)) == NULL) {
474*7c478bd9Sstevel@tonic-gate 			 /* if failed, complain and quit. */
475*7c478bd9Sstevel@tonic-gate 			log("unable to find (%s) again", speedef->g_id);
476*7c478bd9Sstevel@tonic-gate 			exit(1);
477*7c478bd9Sstevel@tonic-gate 		}
478*7c478bd9Sstevel@tonic-gate 	}
479*7c478bd9Sstevel@tonic-gate 	return(sp);
480*7c478bd9Sstevel@tonic-gate }
481*7c478bd9Sstevel@tonic-gate 
482*7c478bd9Sstevel@tonic-gate /*
483*7c478bd9Sstevel@tonic-gate  * inform_parent()	- inform ttymon that tmchild is going to exec service
484*7c478bd9Sstevel@tonic-gate  */
485*7c478bd9Sstevel@tonic-gate static	void
486*7c478bd9Sstevel@tonic-gate inform_parent(fd)
487*7c478bd9Sstevel@tonic-gate int	fd;
488*7c478bd9Sstevel@tonic-gate {
489*7c478bd9Sstevel@tonic-gate 	pid_t	pid;
490*7c478bd9Sstevel@tonic-gate 
491*7c478bd9Sstevel@tonic-gate 	pid = getpid();
492*7c478bd9Sstevel@tonic-gate 	(void)write(fd, &pid, sizeof(pid));
493*7c478bd9Sstevel@tonic-gate }
494*7c478bd9Sstevel@tonic-gate 
495*7c478bd9Sstevel@tonic-gate static	char	 pbuf[BUFSIZ];	/* static buf for TTYPROMPT 	*/
496*7c478bd9Sstevel@tonic-gate static	char	 hbuf[BUFSIZ];	/* static buf for HOME 		*/
497*7c478bd9Sstevel@tonic-gate static	char	 tbuf[BUFSIZ];	/* static buf for TERM		*/
498*7c478bd9Sstevel@tonic-gate 
499*7c478bd9Sstevel@tonic-gate /*
500*7c478bd9Sstevel@tonic-gate  * void invoke_service	- invoke the service
501*7c478bd9Sstevel@tonic-gate  */
502*7c478bd9Sstevel@tonic-gate 
503*7c478bd9Sstevel@tonic-gate static	void
504*7c478bd9Sstevel@tonic-gate invoke_service(pmtab)
505*7c478bd9Sstevel@tonic-gate struct	pmtab	*pmtab;
506*7c478bd9Sstevel@tonic-gate {
507*7c478bd9Sstevel@tonic-gate 	char	 *argvp[MAXARGS];		/* service cmd args */
508*7c478bd9Sstevel@tonic-gate 	int	 cnt = 0;			/* arg counter */
509*7c478bd9Sstevel@tonic-gate 	int	 i, fd;
510*7c478bd9Sstevel@tonic-gate 	struct	 sigaction	sigact;
511*7c478bd9Sstevel@tonic-gate 	extern	 struct	rlimit	Rlimit;
512*7c478bd9Sstevel@tonic-gate 
513*7c478bd9Sstevel@tonic-gate #ifdef 	DEBUG
514*7c478bd9Sstevel@tonic-gate 	debug("in invoke_service");
515*7c478bd9Sstevel@tonic-gate #endif
516*7c478bd9Sstevel@tonic-gate 
517*7c478bd9Sstevel@tonic-gate 	if (tcgetsid(0) != getsid(getpid())) {
518*7c478bd9Sstevel@tonic-gate 		cons_printf("Warning -- ttymon cannot allocate controlling "
519*7c478bd9Sstevel@tonic-gate 		    "tty on \"%s\",\n", pmtab->p_device);
520*7c478bd9Sstevel@tonic-gate 		cons_printf("\tThere may be another session active on this "
521*7c478bd9Sstevel@tonic-gate 		    "port.\n");
522*7c478bd9Sstevel@tonic-gate 
523*7c478bd9Sstevel@tonic-gate 		if (strcmp("/dev/console", pmtab->p_device) != 0) {
524*7c478bd9Sstevel@tonic-gate 			/*
525*7c478bd9Sstevel@tonic-gate 			 * if not on console, write to stderr to warn the user
526*7c478bd9Sstevel@tonic-gate 			 * also.
527*7c478bd9Sstevel@tonic-gate 			 */
528*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "Warning -- ttymon cannot "
529*7c478bd9Sstevel@tonic-gate 			    "allocate controlling tty on \"%s\",\n",
530*7c478bd9Sstevel@tonic-gate 			    pmtab->p_device);
531*7c478bd9Sstevel@tonic-gate 			(void)fprintf(stderr, "\tthere may be another session "
532*7c478bd9Sstevel@tonic-gate 			    "active on this port.\n");
533*7c478bd9Sstevel@tonic-gate 		}
534*7c478bd9Sstevel@tonic-gate 	}
535*7c478bd9Sstevel@tonic-gate 
536*7c478bd9Sstevel@tonic-gate 	if (pmtab->p_status != GETTY) {
537*7c478bd9Sstevel@tonic-gate 		inform_parent(PCpipe[1]);
538*7c478bd9Sstevel@tonic-gate 		sigact.sa_flags = 0;
539*7c478bd9Sstevel@tonic-gate 		sigact.sa_handler = SIG_DFL;
540*7c478bd9Sstevel@tonic-gate 		(void)sigemptyset(&sigact.sa_mask);
541*7c478bd9Sstevel@tonic-gate 		(void)sigaction(SIGPOLL, &sigact, NULL);
542*7c478bd9Sstevel@tonic-gate 	}
543*7c478bd9Sstevel@tonic-gate 
544*7c478bd9Sstevel@tonic-gate 	if (pmtab->p_flags & U_FLAG) {
545*7c478bd9Sstevel@tonic-gate 		if (account(pmtab->p_device) != 0) {
546*7c478bd9Sstevel@tonic-gate 			log("invoke_service: account failed");
547*7c478bd9Sstevel@tonic-gate 			exit(1);
548*7c478bd9Sstevel@tonic-gate 		}
549*7c478bd9Sstevel@tonic-gate 	}
550*7c478bd9Sstevel@tonic-gate 
551*7c478bd9Sstevel@tonic-gate 	/* parse command line */
552*7c478bd9Sstevel@tonic-gate 	mkargv(pmtab->p_server,&argvp[0],&cnt,MAXARGS-1);
553*7c478bd9Sstevel@tonic-gate 
554*7c478bd9Sstevel@tonic-gate 	if (!(pmtab->p_ttyflags & C_FLAG)) {
555*7c478bd9Sstevel@tonic-gate 		(void) sprintf(pbuf, "TTYPROMPT=%s", pmtab->p_prompt);
556*7c478bd9Sstevel@tonic-gate 		if (putenv(pbuf)) {
557*7c478bd9Sstevel@tonic-gate 			log("cannot expand service <%s> environment", argvp[0]);
558*7c478bd9Sstevel@tonic-gate 			exit(1);
559*7c478bd9Sstevel@tonic-gate 		}
560*7c478bd9Sstevel@tonic-gate 	}
561*7c478bd9Sstevel@tonic-gate 	if (pmtab->p_status != GETTY) {
562*7c478bd9Sstevel@tonic-gate 		(void) sprintf(hbuf, "HOME=%s", pmtab->p_dir);
563*7c478bd9Sstevel@tonic-gate 		if (putenv(hbuf)) {
564*7c478bd9Sstevel@tonic-gate 			log("cannot expand service <%s> environment",argvp[0]);
565*7c478bd9Sstevel@tonic-gate 			exit(1);
566*7c478bd9Sstevel@tonic-gate 		}
567*7c478bd9Sstevel@tonic-gate #ifdef	DEBUG
568*7c478bd9Sstevel@tonic-gate 		debug("about to run config script");
569*7c478bd9Sstevel@tonic-gate #endif
570*7c478bd9Sstevel@tonic-gate 		if ((i = doconfig(0, pmtab->p_tag, 0)) != 0) {
571*7c478bd9Sstevel@tonic-gate 			if (i < 0) {
572*7c478bd9Sstevel@tonic-gate 				log("doconfig failed, system error");
573*7c478bd9Sstevel@tonic-gate 			}
574*7c478bd9Sstevel@tonic-gate 			else {
575*7c478bd9Sstevel@tonic-gate 				log("doconfig failed on line %d of script %s",
576*7c478bd9Sstevel@tonic-gate 				    i, pmtab->p_tag);
577*7c478bd9Sstevel@tonic-gate 			}
578*7c478bd9Sstevel@tonic-gate 			exit(1);
579*7c478bd9Sstevel@tonic-gate 		}
580*7c478bd9Sstevel@tonic-gate 	}
581*7c478bd9Sstevel@tonic-gate 
582*7c478bd9Sstevel@tonic-gate 	if (setgid(pmtab->p_gid)) {
583*7c478bd9Sstevel@tonic-gate 		log("cannot set group id to %ld: %s", pmtab->p_gid,
584*7c478bd9Sstevel@tonic-gate 		    strerror(errno));
585*7c478bd9Sstevel@tonic-gate 		exit(1);
586*7c478bd9Sstevel@tonic-gate 	}
587*7c478bd9Sstevel@tonic-gate 
588*7c478bd9Sstevel@tonic-gate 	if (setuid(pmtab->p_uid)) {
589*7c478bd9Sstevel@tonic-gate 		log("cannot set user id to %ld: %s", pmtab->p_uid,
590*7c478bd9Sstevel@tonic-gate 		    strerror(errno));
591*7c478bd9Sstevel@tonic-gate 		exit(1);
592*7c478bd9Sstevel@tonic-gate 	}
593*7c478bd9Sstevel@tonic-gate 
594*7c478bd9Sstevel@tonic-gate 	if (chdir(pmtab->p_dir)) {
595*7c478bd9Sstevel@tonic-gate 		log("cannot chdir to %s: %s", pmtab->p_dir, strerror(errno));
596*7c478bd9Sstevel@tonic-gate 		exit(1);
597*7c478bd9Sstevel@tonic-gate 	}
598*7c478bd9Sstevel@tonic-gate 
599*7c478bd9Sstevel@tonic-gate 	if (pmtab->p_uid != ROOTUID) {
600*7c478bd9Sstevel@tonic-gate 		/* change ownership and mode of device */
601*7c478bd9Sstevel@tonic-gate 		(void)fchown(0,pmtab->p_uid,Tty_gid);
602*7c478bd9Sstevel@tonic-gate 		(void)fchmod(0,0620);
603*7c478bd9Sstevel@tonic-gate 	}
604*7c478bd9Sstevel@tonic-gate 
605*7c478bd9Sstevel@tonic-gate 
606*7c478bd9Sstevel@tonic-gate 	if (pmtab->p_status != GETTY) {
607*7c478bd9Sstevel@tonic-gate 		sigact.sa_flags = 0;
608*7c478bd9Sstevel@tonic-gate 		sigact.sa_handler = SIG_DFL;
609*7c478bd9Sstevel@tonic-gate 		(void)sigemptyset(&sigact.sa_mask);
610*7c478bd9Sstevel@tonic-gate 		(void)sigaction(SIGINT, &sigact, NULL);
611*7c478bd9Sstevel@tonic-gate 		if (setrlimit(RLIMIT_NOFILE, &Rlimit) == -1) {
612*7c478bd9Sstevel@tonic-gate 			log("setrlimit failed: %s", strerror(errno));
613*7c478bd9Sstevel@tonic-gate 			exit(1);
614*7c478bd9Sstevel@tonic-gate 		}
615*7c478bd9Sstevel@tonic-gate 		/* invoke the service */
616*7c478bd9Sstevel@tonic-gate 		log("Starting service (%s) on %s", argvp[0], pmtab->p_device);
617*7c478bd9Sstevel@tonic-gate 	}
618*7c478bd9Sstevel@tonic-gate 
619*7c478bd9Sstevel@tonic-gate 	if (pmtab->p_termtype != (char *)NULL) {
620*7c478bd9Sstevel@tonic-gate 		(void) sprintf(tbuf, "TERM=%s", pmtab->p_termtype);
621*7c478bd9Sstevel@tonic-gate 		if (putenv(tbuf)) {
622*7c478bd9Sstevel@tonic-gate 			log("cannot expand service <%s> environment", argvp[0]);
623*7c478bd9Sstevel@tonic-gate 			exit(1);
624*7c478bd9Sstevel@tonic-gate 		}
625*7c478bd9Sstevel@tonic-gate 	}
626*7c478bd9Sstevel@tonic-gate 	/* restore signal handlers and mask */
627*7c478bd9Sstevel@tonic-gate 	(void)sigaction(SIGINT, &Sigint, NULL);
628*7c478bd9Sstevel@tonic-gate 	(void)sigaction(SIGALRM, &Sigalrm, NULL);
629*7c478bd9Sstevel@tonic-gate 	(void)sigaction(SIGPOLL, &Sigpoll, NULL);
630*7c478bd9Sstevel@tonic-gate 	(void)sigaction(SIGCLD, &Sigcld, NULL);
631*7c478bd9Sstevel@tonic-gate 	(void)sigaction(SIGTERM, &Sigterm, NULL);
632*7c478bd9Sstevel@tonic-gate #ifdef	DEBUG
633*7c478bd9Sstevel@tonic-gate 	(void)sigaction(SIGUSR1, &Sigusr1, NULL);
634*7c478bd9Sstevel@tonic-gate 	(void)sigaction(SIGUSR2, &Sigusr2, NULL);
635*7c478bd9Sstevel@tonic-gate #endif
636*7c478bd9Sstevel@tonic-gate 	(void)sigprocmask(SIG_SETMASK, &Origmask, NULL);
637*7c478bd9Sstevel@tonic-gate 	(void) execve(argvp[0], argvp, environ);
638*7c478bd9Sstevel@tonic-gate 
639*7c478bd9Sstevel@tonic-gate 	/* exec returns only on failure! */
640*7c478bd9Sstevel@tonic-gate 	log("tmchild: exec service failed: %s", strerror(errno));
641*7c478bd9Sstevel@tonic-gate 	exit(1);
642*7c478bd9Sstevel@tonic-gate }
643*7c478bd9Sstevel@tonic-gate 
644*7c478bd9Sstevel@tonic-gate /*
645*7c478bd9Sstevel@tonic-gate  *	check_hup(fd)	- do a poll on fd to check if it is in hangup state
646*7c478bd9Sstevel@tonic-gate  *			- return 1 if hangup, otherwise return 0
647*7c478bd9Sstevel@tonic-gate  */
648*7c478bd9Sstevel@tonic-gate 
649*7c478bd9Sstevel@tonic-gate static	int
650*7c478bd9Sstevel@tonic-gate check_hup(fd)
651*7c478bd9Sstevel@tonic-gate int	fd;
652*7c478bd9Sstevel@tonic-gate {
653*7c478bd9Sstevel@tonic-gate 	int	ret;
654*7c478bd9Sstevel@tonic-gate 	struct	pollfd	pfd[1];
655*7c478bd9Sstevel@tonic-gate 
656*7c478bd9Sstevel@tonic-gate 	pfd[0].fd = fd;
657*7c478bd9Sstevel@tonic-gate 	pfd[0].events = POLLHUP;
658*7c478bd9Sstevel@tonic-gate 	for (;;) {
659*7c478bd9Sstevel@tonic-gate 		ret = poll(pfd, 1, 0);
660*7c478bd9Sstevel@tonic-gate 		if (ret < 0) {
661*7c478bd9Sstevel@tonic-gate 			if (errno == EINTR)
662*7c478bd9Sstevel@tonic-gate 				continue;
663*7c478bd9Sstevel@tonic-gate 			log("check_hup: poll failed: %s", strerror(errno));
664*7c478bd9Sstevel@tonic-gate 			exit(1);
665*7c478bd9Sstevel@tonic-gate 		}
666*7c478bd9Sstevel@tonic-gate 		else if (ret > 0) {
667*7c478bd9Sstevel@tonic-gate 			if (pfd[0].revents & POLLHUP) {
668*7c478bd9Sstevel@tonic-gate 				return(1);
669*7c478bd9Sstevel@tonic-gate 			}
670*7c478bd9Sstevel@tonic-gate 		}
671*7c478bd9Sstevel@tonic-gate 		return(0);
672*7c478bd9Sstevel@tonic-gate 	}
673*7c478bd9Sstevel@tonic-gate }
674*7c478bd9Sstevel@tonic-gate 
675*7c478bd9Sstevel@tonic-gate /*
676*7c478bd9Sstevel@tonic-gate  * sigpoll()	- SIGPOLL handle for tmchild
677*7c478bd9Sstevel@tonic-gate  *		- when SIGPOLL is received by tmchild,
678*7c478bd9Sstevel@tonic-gate  *		  the pipe between ttymon and tmchild is broken.
679*7c478bd9Sstevel@tonic-gate  *		  Something must happen to ttymon.
680*7c478bd9Sstevel@tonic-gate  */
681*7c478bd9Sstevel@tonic-gate void
682*7c478bd9Sstevel@tonic-gate sigpoll()
683*7c478bd9Sstevel@tonic-gate {
684*7c478bd9Sstevel@tonic-gate #ifdef	DEBUG
685*7c478bd9Sstevel@tonic-gate 	debug("tmchild got SIGPOLL, exiting");
686*7c478bd9Sstevel@tonic-gate #endif
687*7c478bd9Sstevel@tonic-gate 	exit(1);
688*7c478bd9Sstevel@tonic-gate }
689