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