17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5b77c815bSNobutomo Nakano * Common Development and Distribution License (the "License").
6b77c815bSNobutomo Nakano * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate /*
22b77c815bSNobutomo Nakano * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
237c478bd9Sstevel@tonic-gate * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate */
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
273bb2c156SToomas Soome /* All Rights Reserved */
287c478bd9Sstevel@tonic-gate
297c478bd9Sstevel@tonic-gate #include <stdio.h>
307c478bd9Sstevel@tonic-gate #include <stdlib.h>
317c478bd9Sstevel@tonic-gate #include <fcntl.h>
327c478bd9Sstevel@tonic-gate #include <errno.h>
337c478bd9Sstevel@tonic-gate #include <sys/types.h>
347c478bd9Sstevel@tonic-gate #include <termio.h>
357c478bd9Sstevel@tonic-gate #include <string.h>
367c478bd9Sstevel@tonic-gate #include <signal.h>
377c478bd9Sstevel@tonic-gate #include <poll.h>
387c478bd9Sstevel@tonic-gate #include <unistd.h>
393bb2c156SToomas Soome #include "sys/stropts.h"
403bb2c156SToomas Soome #include <sys/resource.h>
417c478bd9Sstevel@tonic-gate #include "sac.h"
427c478bd9Sstevel@tonic-gate #include "ttymon.h"
437c478bd9Sstevel@tonic-gate #include "tmstruct.h"
447c478bd9Sstevel@tonic-gate #include "tmextern.h"
457c478bd9Sstevel@tonic-gate #ifdef SYS_NAME
467c478bd9Sstevel@tonic-gate #include <sys/utsname.h>
477c478bd9Sstevel@tonic-gate #endif
487c478bd9Sstevel@tonic-gate
493bb2c156SToomas Soome static void openline(struct pmtab *, struct Gdef *);
503bb2c156SToomas Soome static void invoke_service(struct pmtab *);
513bb2c156SToomas Soome static char *do_autobaud(struct pmtab *, struct Gdef *);
523bb2c156SToomas Soome static struct Gdef *next_speed(struct Gdef *);
533bb2c156SToomas Soome static int check_hup(int);
547c478bd9Sstevel@tonic-gate
557c478bd9Sstevel@tonic-gate /*
567c478bd9Sstevel@tonic-gate * tmchild - process that handles peeking data, determine baud rate
577c478bd9Sstevel@tonic-gate * and invoke service on each individual port.
587c478bd9Sstevel@tonic-gate *
597c478bd9Sstevel@tonic-gate */
607c478bd9Sstevel@tonic-gate void
tmchild(struct pmtab * pmtab)613bb2c156SToomas Soome tmchild(struct pmtab *pmtab)
627c478bd9Sstevel@tonic-gate {
633bb2c156SToomas Soome struct Gdef *speedef;
647c478bd9Sstevel@tonic-gate char *auto_speed = "";
657c478bd9Sstevel@tonic-gate struct sigaction sigact;
667c478bd9Sstevel@tonic-gate
677c478bd9Sstevel@tonic-gate #ifdef DEBUG
687c478bd9Sstevel@tonic-gate debug("in tmchild");
697c478bd9Sstevel@tonic-gate #endif
707c478bd9Sstevel@tonic-gate peek_ptr = NULL;
717c478bd9Sstevel@tonic-gate if (pmtab->p_status != GETTY) {
72b77c815bSNobutomo Nakano child_sigcatch();
73b77c815bSNobutomo Nakano (void) close(PCpipe[0]); /* close parent end of the pipe */
747c478bd9Sstevel@tonic-gate if (ioctl(PCpipe[1], I_SETSIG, S_HANGUP) == -1) {
757c478bd9Sstevel@tonic-gate log("I_SETSIG failed: %s", strerror(errno));
767c478bd9Sstevel@tonic-gate exit(1);
777c478bd9Sstevel@tonic-gate }
787c478bd9Sstevel@tonic-gate /*
797c478bd9Sstevel@tonic-gate * the following check is to make sure no hangup
807c478bd9Sstevel@tonic-gate * happens before registering for SIGPOLL
817c478bd9Sstevel@tonic-gate */
827c478bd9Sstevel@tonic-gate if (check_hup(PCpipe[1])) {
837c478bd9Sstevel@tonic-gate #ifdef DEBUG
847c478bd9Sstevel@tonic-gate debug("PCpipe hungup, tmchild exiting");
857c478bd9Sstevel@tonic-gate #endif
867c478bd9Sstevel@tonic-gate exit(1);
877c478bd9Sstevel@tonic-gate }
887c478bd9Sstevel@tonic-gate
897c478bd9Sstevel@tonic-gate if (pmtab->p_ttyflags & (C_FLAG|B_FLAG)) {
907c478bd9Sstevel@tonic-gate if (pmtab->p_fd > 0) {
91b77c815bSNobutomo Nakano (void) close(pmtab->p_fd);
92b77c815bSNobutomo Nakano pmtab->p_fd = 0;
937c478bd9Sstevel@tonic-gate }
947c478bd9Sstevel@tonic-gate }
957c478bd9Sstevel@tonic-gate
967c478bd9Sstevel@tonic-gate /*
977c478bd9Sstevel@tonic-gate * become the session leader so that a controlling tty
987c478bd9Sstevel@tonic-gate * will be allocated.
997c478bd9Sstevel@tonic-gate */
100b77c815bSNobutomo Nakano (void) setsid();
1017c478bd9Sstevel@tonic-gate }
102902bba37SToomas Soome speedef = get_speed(pmtab);
103b77c815bSNobutomo Nakano openline(pmtab, speedef);
1047c478bd9Sstevel@tonic-gate if (pmtab->p_ttyflags & (C_FLAG|B_FLAG)) {
1053bb2c156SToomas Soome if (pmtab->p_fd >= 0) {
1063bb2c156SToomas Soome if ((pmtab->p_modules != NULL) &&
1073bb2c156SToomas Soome (*(pmtab->p_modules) != '\0')) {
1083bb2c156SToomas Soome if (push_linedisc(pmtab->p_fd,
1093bb2c156SToomas Soome pmtab->p_modules, pmtab->p_device) == -1) {
1103bb2c156SToomas Soome (void) close(pmtab->p_fd);
1113bb2c156SToomas Soome return;
1123bb2c156SToomas Soome }
1133bb2c156SToomas Soome }
1147c478bd9Sstevel@tonic-gate }
1157c478bd9Sstevel@tonic-gate }
1167c478bd9Sstevel@tonic-gate if ((pmtab->p_ttyflags & C_FLAG) &&
1177c478bd9Sstevel@tonic-gate (State != PM_DISABLED) &&
1187c478bd9Sstevel@tonic-gate (!(pmtab->p_flags & X_FLAG))) {
1197c478bd9Sstevel@tonic-gate /*
1207c478bd9Sstevel@tonic-gate * if "c" flag is set, and the port is not disabled
121b77c815bSNobutomo Nakano * invoke service immediately
1227c478bd9Sstevel@tonic-gate */
1233bb2c156SToomas Soome if (set_termio(0, speedef->g_fflags, NULL, FALSE,
1243bb2c156SToomas Soome CANON) == -1) {
1257c478bd9Sstevel@tonic-gate log("set final termio failed");
1267c478bd9Sstevel@tonic-gate exit(1);
1277c478bd9Sstevel@tonic-gate }
1287c478bd9Sstevel@tonic-gate invoke_service(pmtab);
1297c478bd9Sstevel@tonic-gate exit(1); /*NOTREACHED*/
1307c478bd9Sstevel@tonic-gate }
1317c478bd9Sstevel@tonic-gate if (speedef->g_autobaud & A_FLAG) {
132b77c815bSNobutomo Nakano auto_speed = do_autobaud(pmtab, speedef);
1337c478bd9Sstevel@tonic-gate }
134b77c815bSNobutomo Nakano if (set_termio(0, speedef->g_fflags, NULL, FALSE, CANON) == -1) {
1357c478bd9Sstevel@tonic-gate log("set final termio failed");
1367c478bd9Sstevel@tonic-gate exit(1);
1377c478bd9Sstevel@tonic-gate }
138b77c815bSNobutomo Nakano if ((pmtab->p_ttyflags & (R_FLAG|A_FLAG)) ||
139b77c815bSNobutomo Nakano (pmtab->p_status == GETTY) || (pmtab->p_timeout > 0)) {
140b77c815bSNobutomo Nakano write_prompt(1, pmtab, TRUE, TRUE);
141b77c815bSNobutomo Nakano if (pmtab->p_timeout) {
1427c478bd9Sstevel@tonic-gate sigact.sa_flags = 0;
1437c478bd9Sstevel@tonic-gate sigact.sa_handler = timedout;
144b77c815bSNobutomo Nakano (void) sigemptyset(&sigact.sa_mask);
145b77c815bSNobutomo Nakano (void) sigaction(SIGALRM, &sigact, NULL);
146b77c815bSNobutomo Nakano (void) alarm((unsigned)pmtab->p_timeout);
1477c478bd9Sstevel@tonic-gate }
1483bb2c156SToomas Soome } else if ((pmtab->p_ttyflags & (B_FLAG)))
1493bb2c156SToomas Soome write_prompt(pmtab->p_fd, pmtab, TRUE, TRUE);
1507c478bd9Sstevel@tonic-gate
1517c478bd9Sstevel@tonic-gate
1527c478bd9Sstevel@tonic-gate /* Loop until user is successful in invoking service. */
153b77c815bSNobutomo Nakano for (;;) {
1547c478bd9Sstevel@tonic-gate
1557c478bd9Sstevel@tonic-gate /* Peek the user's typed response and respond appropriately. */
156b77c815bSNobutomo Nakano switch (poll_data()) {
1577c478bd9Sstevel@tonic-gate case GOODNAME:
1587c478bd9Sstevel@tonic-gate #ifdef DEBUG
1597c478bd9Sstevel@tonic-gate debug("got GOODNAME");
160b77c815bSNobutomo Nakano #endif
1617c478bd9Sstevel@tonic-gate if (pmtab->p_timeout) {
162b77c815bSNobutomo Nakano (void) alarm((unsigned)0);
1637c478bd9Sstevel@tonic-gate sigact.sa_flags = 0;
1647c478bd9Sstevel@tonic-gate sigact.sa_handler = SIG_DFL;
165b77c815bSNobutomo Nakano (void) sigemptyset(&sigact.sa_mask);
166b77c815bSNobutomo Nakano (void) sigaction(SIGALRM, &sigact, NULL);
1677c478bd9Sstevel@tonic-gate }
1683bb2c156SToomas Soome if ((State == PM_DISABLED) ||
1693bb2c156SToomas Soome (pmtab->p_flags & X_FLAG)) {
170b77c815bSNobutomo Nakano write_prompt(1, pmtab, TRUE, FALSE);
1717c478bd9Sstevel@tonic-gate break;
1727c478bd9Sstevel@tonic-gate }
173b77c815bSNobutomo Nakano if (set_termio(0, speedef->g_fflags, auto_speed,
174b77c815bSNobutomo Nakano FALSE, CANON) == -1) {
1757c478bd9Sstevel@tonic-gate log("set final termio failed");
1767c478bd9Sstevel@tonic-gate exit(1);
1777c478bd9Sstevel@tonic-gate }
1787c478bd9Sstevel@tonic-gate invoke_service(pmtab);
1797c478bd9Sstevel@tonic-gate exit(1); /*NOTREACHED*/
1807c478bd9Sstevel@tonic-gate
1817c478bd9Sstevel@tonic-gate case BADSPEED:
1827c478bd9Sstevel@tonic-gate /* wrong speed! try next speed in the list. */
1837c478bd9Sstevel@tonic-gate speedef = next_speed(speedef);
1847c478bd9Sstevel@tonic-gate #ifdef DEBUG
1857c478bd9Sstevel@tonic-gate debug("BADSPEED: setup next speed");
1867c478bd9Sstevel@tonic-gate #endif
1877c478bd9Sstevel@tonic-gate if (speedef->g_autobaud & A_FLAG) {
1887c478bd9Sstevel@tonic-gate if (auto_termio(0) == -1) {
1897c478bd9Sstevel@tonic-gate exit(1);
1907c478bd9Sstevel@tonic-gate }
191b77c815bSNobutomo Nakano auto_speed = do_autobaud(pmtab, speedef);
1923bb2c156SToomas Soome } else {
1937c478bd9Sstevel@tonic-gate auto_speed = NULL;
1947c478bd9Sstevel@tonic-gate /*
195b77c815bSNobutomo Nakano * this reset may fail if the speed is not
1967c478bd9Sstevel@tonic-gate * supported by the system
1977c478bd9Sstevel@tonic-gate * we just cycle through it to the next one
1987c478bd9Sstevel@tonic-gate */
199b77c815bSNobutomo Nakano if (set_termio(0, speedef->g_iflags, NULL,
200b77c815bSNobutomo Nakano FALSE, CANON) != 0) {
2017c478bd9Sstevel@tonic-gate log("Warning -- speed of <%s> may "
2027c478bd9Sstevel@tonic-gate "be not supported by the system",
2037c478bd9Sstevel@tonic-gate speedef->g_id);
2047c478bd9Sstevel@tonic-gate }
2057c478bd9Sstevel@tonic-gate }
206b77c815bSNobutomo Nakano write_prompt(1, pmtab, TRUE, TRUE);
2077c478bd9Sstevel@tonic-gate break;
2087c478bd9Sstevel@tonic-gate
2097c478bd9Sstevel@tonic-gate case NONAME:
2107c478bd9Sstevel@tonic-gate #ifdef DEBUG
2117c478bd9Sstevel@tonic-gate debug("got NONAME");
212b77c815bSNobutomo Nakano #endif
213b77c815bSNobutomo Nakano write_prompt(1, pmtab, FALSE, FALSE);
2147c478bd9Sstevel@tonic-gate break;
2157c478bd9Sstevel@tonic-gate
2167c478bd9Sstevel@tonic-gate } /* end switch */
2177c478bd9Sstevel@tonic-gate
2187c478bd9Sstevel@tonic-gate peek_ptr = NULL;
219b77c815bSNobutomo Nakano if (pmtab->p_timeout) {
2207c478bd9Sstevel@tonic-gate sigact.sa_flags = 0;
2217c478bd9Sstevel@tonic-gate sigact.sa_handler = timedout;
222b77c815bSNobutomo Nakano (void) sigemptyset(&sigact.sa_mask);
223b77c815bSNobutomo Nakano (void) sigaction(SIGALRM, &sigact, NULL);
224b77c815bSNobutomo Nakano (void) alarm((unsigned)pmtab->p_timeout);
2257c478bd9Sstevel@tonic-gate }
2267c478bd9Sstevel@tonic-gate } /* end for loop */
2277c478bd9Sstevel@tonic-gate }
2287c478bd9Sstevel@tonic-gate
2297c478bd9Sstevel@tonic-gate static void
openline(struct pmtab * pmtab,struct Gdef * speedef)2303bb2c156SToomas Soome openline(struct pmtab *pmtab, struct Gdef *speedef)
2317c478bd9Sstevel@tonic-gate {
2327c478bd9Sstevel@tonic-gate char buffer[5];
2337c478bd9Sstevel@tonic-gate int rtn = 0;
2347c478bd9Sstevel@tonic-gate int line_count;
2357c478bd9Sstevel@tonic-gate
2367c478bd9Sstevel@tonic-gate #ifdef DEBUG
2377c478bd9Sstevel@tonic-gate debug("in openline");
2387c478bd9Sstevel@tonic-gate #endif
2397c478bd9Sstevel@tonic-gate if (pmtab->p_status != GETTY) {
240b77c815bSNobutomo Nakano (void) close(0);
2417c478bd9Sstevel@tonic-gate /* open should return fd 0, if not, then close it */
2427c478bd9Sstevel@tonic-gate if ((pmtab->p_fd = open(pmtab->p_device, O_RDWR)) != 0) {
2437c478bd9Sstevel@tonic-gate log("open \"%s\" failed: %s", pmtab->p_device,
2447c478bd9Sstevel@tonic-gate strerror(errno));
2457c478bd9Sstevel@tonic-gate exit(1);
2467c478bd9Sstevel@tonic-gate }
2477c478bd9Sstevel@tonic-gate }
248b77c815bSNobutomo Nakano (void) close(1);
249b77c815bSNobutomo Nakano (void) close(2);
250b77c815bSNobutomo Nakano (void) dup(0);
251b77c815bSNobutomo Nakano (void) dup(0);
2527c478bd9Sstevel@tonic-gate
2537c478bd9Sstevel@tonic-gate if (pmtab->p_ttyflags & R_FLAG) { /* wait_read is needed */
254b77c815bSNobutomo Nakano if (pmtab->p_count) {
255b77c815bSNobutomo Nakano if (peek_ptr != NULL)
256b77c815bSNobutomo Nakano if ((peek_ptr->buf[0]&0x7F) == '\n' ||
257b77c815bSNobutomo Nakano (peek_ptr->buf[0]&0x7F) == '\r')
2587c478bd9Sstevel@tonic-gate pmtab->p_count--;
259b77c815bSNobutomo Nakano
260b77c815bSNobutomo Nakano /*
261b77c815bSNobutomo Nakano * - wait for "p_count" lines
2627c478bd9Sstevel@tonic-gate * - datakit switch does not
2637c478bd9Sstevel@tonic-gate * know you are a host or a terminal
264b77c815bSNobutomo Nakano * - so it send you several lines of msg
2657c478bd9Sstevel@tonic-gate * - we need to swallow that msg
2667c478bd9Sstevel@tonic-gate * - we assume the baud rate is correct
2677c478bd9Sstevel@tonic-gate * - if it is not, '\n' will not look like '\n'
268b77c815bSNobutomo Nakano * and we will wait forever here
2697c478bd9Sstevel@tonic-gate */
2703bb2c156SToomas Soome if (set_termio(0, speedef->g_fflags, NULL, TRUE,
2713bb2c156SToomas Soome CANON) == -1) {
2727c478bd9Sstevel@tonic-gate log("set final termio failed");
2737c478bd9Sstevel@tonic-gate exit(1);
2747c478bd9Sstevel@tonic-gate }
275b77c815bSNobutomo Nakano for (line_count = 0; line_count < pmtab->p_count; ) {
2763bb2c156SToomas Soome if (read(0, buffer, 1) < 0 ||
2773bb2c156SToomas Soome *buffer == '\0' ||
2783bb2c156SToomas Soome *buffer == '\004') {
279b77c815bSNobutomo Nakano (void) close(0);
2807c478bd9Sstevel@tonic-gate exit(0);
2817c478bd9Sstevel@tonic-gate }
2827c478bd9Sstevel@tonic-gate if (*buffer == '\n')
2837c478bd9Sstevel@tonic-gate line_count++;
2847c478bd9Sstevel@tonic-gate }
2853bb2c156SToomas Soome } else { /* wait for 1 char */
286b77c815bSNobutomo Nakano if (peek_ptr == NULL) {
2873bb2c156SToomas Soome if (set_termio(0, NULL, NULL, TRUE,
2883bb2c156SToomas Soome RAW) == -1) {
2897c478bd9Sstevel@tonic-gate log("set termio RAW failed");
2907c478bd9Sstevel@tonic-gate exit(1);
2917c478bd9Sstevel@tonic-gate }
2927c478bd9Sstevel@tonic-gate rtn = read(0, buffer, 1);
2937c478bd9Sstevel@tonic-gate } else
2947c478bd9Sstevel@tonic-gate *buffer = (peek_ptr->buf[0]&0x7F);
2957c478bd9Sstevel@tonic-gate
2967c478bd9Sstevel@tonic-gate /*
2977c478bd9Sstevel@tonic-gate * NOTE: Cu on a direct line when ~. is encountered will
2987c478bd9Sstevel@tonic-gate * send EOTs to the other side. EOT=\004
2997c478bd9Sstevel@tonic-gate */
300b77c815bSNobutomo Nakano if (rtn < 0 || *buffer == '\004') {
301b77c815bSNobutomo Nakano (void) close(0);
3027c478bd9Sstevel@tonic-gate exit(0);
3037c478bd9Sstevel@tonic-gate }
3047c478bd9Sstevel@tonic-gate }
3057c478bd9Sstevel@tonic-gate peek_ptr = NULL;
3067c478bd9Sstevel@tonic-gate if (!(pmtab->p_ttyflags & A_FLAG)) { /* autobaud not enabled */
3073bb2c156SToomas Soome if (set_termio(0, speedef->g_fflags, NULL, TRUE,
3083bb2c156SToomas Soome CANON) == -1) {
3097c478bd9Sstevel@tonic-gate log("set final termio failed");
3107c478bd9Sstevel@tonic-gate exit(1);
3117c478bd9Sstevel@tonic-gate }
3127c478bd9Sstevel@tonic-gate }
3137c478bd9Sstevel@tonic-gate }
3147c478bd9Sstevel@tonic-gate if (pmtab->p_ttyflags & B_FLAG) { /* port is bi-directional */
3157c478bd9Sstevel@tonic-gate /* set advisory lock on the line */
316b77c815bSNobutomo Nakano if (tm_lock(0) != 0) {
3177c478bd9Sstevel@tonic-gate /*
318b77c815bSNobutomo Nakano * device is locked
3197c478bd9Sstevel@tonic-gate * child exits and let the parent wait for
3207c478bd9Sstevel@tonic-gate * the lock to go away
3217c478bd9Sstevel@tonic-gate */
3227c478bd9Sstevel@tonic-gate exit(0);
3237c478bd9Sstevel@tonic-gate }
3247c478bd9Sstevel@tonic-gate /* change ownership back to root */
325b77c815bSNobutomo Nakano (void) fchown(0, ROOTUID, Tty_gid);
326b77c815bSNobutomo Nakano (void) fchmod(0, 0620);
3277c478bd9Sstevel@tonic-gate }
3287c478bd9Sstevel@tonic-gate }
3297c478bd9Sstevel@tonic-gate
3307c478bd9Sstevel@tonic-gate /*
3317c478bd9Sstevel@tonic-gate * write_prompt - write the msg to fd
3327c478bd9Sstevel@tonic-gate * - if flush is set, flush input queue
333b77c815bSNobutomo Nakano * - if clear is set, write a new line
3347c478bd9Sstevel@tonic-gate */
3357c478bd9Sstevel@tonic-gate void
write_prompt(int fd,struct pmtab * pmtab,int flush,int clear)3363bb2c156SToomas Soome write_prompt(int fd, struct pmtab *pmtab, int flush, int clear)
3377c478bd9Sstevel@tonic-gate {
3387c478bd9Sstevel@tonic-gate
3397c478bd9Sstevel@tonic-gate #ifdef DEBUG
3407c478bd9Sstevel@tonic-gate debug("in write_prompt");
3417c478bd9Sstevel@tonic-gate #endif
3427c478bd9Sstevel@tonic-gate if (flush)
3437c478bd9Sstevel@tonic-gate flush_input(fd);
3447c478bd9Sstevel@tonic-gate if (clear) {
345b77c815bSNobutomo Nakano (void) write(fd, "\r\n", 2);
3467c478bd9Sstevel@tonic-gate }
3477c478bd9Sstevel@tonic-gate #ifdef SYS_NAME
3487c478bd9Sstevel@tonic-gate sys_name(fd);
3497c478bd9Sstevel@tonic-gate #endif
3507c478bd9Sstevel@tonic-gate /* Print prompt/disable message. */
351b77c815bSNobutomo Nakano if ((State == PM_DISABLED) || (pmtab->p_flags & X_FLAG))
3523bb2c156SToomas Soome (void) write(fd, pmtab->p_dmsg,
3533bb2c156SToomas Soome (unsigned)strlen(pmtab->p_dmsg));
3547c478bd9Sstevel@tonic-gate else
355b77c815bSNobutomo Nakano (void) write(fd, pmtab->p_prompt,
3563bb2c156SToomas Soome (unsigned)strlen(pmtab->p_prompt));
3577c478bd9Sstevel@tonic-gate }
3587c478bd9Sstevel@tonic-gate
3597c478bd9Sstevel@tonic-gate /*
3607c478bd9Sstevel@tonic-gate * timedout - input period timed out
3617c478bd9Sstevel@tonic-gate */
3627c478bd9Sstevel@tonic-gate void
timedout(int signal __unused)363*30699046SRichard Lowe timedout(int signal __unused)
3647c478bd9Sstevel@tonic-gate {
3657c478bd9Sstevel@tonic-gate exit(1);
3667c478bd9Sstevel@tonic-gate }
3677c478bd9Sstevel@tonic-gate
3687c478bd9Sstevel@tonic-gate #ifdef SYS_NAME
3697c478bd9Sstevel@tonic-gate /*
3707c478bd9Sstevel@tonic-gate * void sys_name() - generate a msg with system id
3717c478bd9Sstevel@tonic-gate * - print out /etc/issue file if it exists
3727c478bd9Sstevel@tonic-gate */
3737c478bd9Sstevel@tonic-gate void
sys_name(int fd)3743bb2c156SToomas Soome sys_name(int fd)
3757c478bd9Sstevel@tonic-gate {
3767c478bd9Sstevel@tonic-gate char *ptr, buffer[BUFSIZ];
3777c478bd9Sstevel@tonic-gate FILE *fp;
3787c478bd9Sstevel@tonic-gate
3797c478bd9Sstevel@tonic-gate #if 0 /* 1111333 - don't print node name, we already do this elsewhere */
3807c478bd9Sstevel@tonic-gate struct utsname utsname;
3817c478bd9Sstevel@tonic-gate
3827c478bd9Sstevel@tonic-gate if (uname(&utsname) != FAILURE) {
383b77c815bSNobutomo Nakano (void) sprintf(buffer, "%.9s\r\n", utsname.nodename);
384b77c815bSNobutomo Nakano (void) write(fd, buffer, strlen(buffer));
3857c478bd9Sstevel@tonic-gate }
3867c478bd9Sstevel@tonic-gate #endif
3877c478bd9Sstevel@tonic-gate
388b77c815bSNobutomo Nakano if ((fp = fopen(ISSUEFILE, "r")) != NULL) {
389b77c815bSNobutomo Nakano while ((ptr = fgets(buffer, sizeof (buffer), fp)) != NULL) {
390b77c815bSNobutomo Nakano (void) write(fd, ptr, strlen(ptr));
3917c478bd9Sstevel@tonic-gate }
392b77c815bSNobutomo Nakano (void) fclose(fp);
3937c478bd9Sstevel@tonic-gate }
3947c478bd9Sstevel@tonic-gate }
3957c478bd9Sstevel@tonic-gate #endif
3967c478bd9Sstevel@tonic-gate
3977c478bd9Sstevel@tonic-gate
3987c478bd9Sstevel@tonic-gate /*
3997c478bd9Sstevel@tonic-gate * do_autobaud - do autobaud
4007c478bd9Sstevel@tonic-gate * - if it succeed, set the new speed and return
4017c478bd9Sstevel@tonic-gate * - if it failed, it will get the nextlabel
4027c478bd9Sstevel@tonic-gate * - if next entry is also autobaud,
4037c478bd9Sstevel@tonic-gate * it will loop back to do autobaud again
4047c478bd9Sstevel@tonic-gate * - otherwise, it will set new termio and return
4057c478bd9Sstevel@tonic-gate */
4067c478bd9Sstevel@tonic-gate static char *
do_autobaud(struct pmtab * pmtab,struct Gdef * speedef)4073bb2c156SToomas Soome do_autobaud(struct pmtab *pmtab, struct Gdef *speedef)
4087c478bd9Sstevel@tonic-gate {
4097c478bd9Sstevel@tonic-gate int done = FALSE;
4107c478bd9Sstevel@tonic-gate char *auto_speed;
4113bb2c156SToomas Soome
4127c478bd9Sstevel@tonic-gate #ifdef DEBUG
4137c478bd9Sstevel@tonic-gate debug("in do_autobaud");
4147c478bd9Sstevel@tonic-gate #endif
4157c478bd9Sstevel@tonic-gate while (!done) {
416b77c815bSNobutomo Nakano if ((auto_speed = autobaud(0, pmtab->p_timeout)) == NULL) {
4177c478bd9Sstevel@tonic-gate speedef = next_speed(speedef);
4187c478bd9Sstevel@tonic-gate if (speedef->g_autobaud & A_FLAG) {
4197c478bd9Sstevel@tonic-gate continue;
4203bb2c156SToomas Soome } else {
421b77c815bSNobutomo Nakano if (set_termio(0, speedef->g_iflags, NULL,
4223bb2c156SToomas Soome TRUE, CANON) != 0) {
4237c478bd9Sstevel@tonic-gate exit(1);
4247c478bd9Sstevel@tonic-gate }
4257c478bd9Sstevel@tonic-gate done = TRUE;
4267c478bd9Sstevel@tonic-gate }
4273bb2c156SToomas Soome } else {
428b77c815bSNobutomo Nakano if (set_termio(0, speedef->g_fflags, auto_speed,
4293bb2c156SToomas Soome TRUE, CANON) != 0) {
4307c478bd9Sstevel@tonic-gate exit(1);
4317c478bd9Sstevel@tonic-gate }
4327c478bd9Sstevel@tonic-gate done = TRUE;
4337c478bd9Sstevel@tonic-gate }
4347c478bd9Sstevel@tonic-gate }
4357c478bd9Sstevel@tonic-gate #ifdef DEBUG
4367c478bd9Sstevel@tonic-gate debug("autobaud done");
4377c478bd9Sstevel@tonic-gate #endif
438b77c815bSNobutomo Nakano return (auto_speed);
4397c478bd9Sstevel@tonic-gate }
4407c478bd9Sstevel@tonic-gate
4417c478bd9Sstevel@tonic-gate /*
4423bb2c156SToomas Soome * next_speed(speedef)
4437c478bd9Sstevel@tonic-gate * - find the next entry according to nextlabel. If "nextlabel"
4447c478bd9Sstevel@tonic-gate * is not valid, go back to the old ttylabel.
4457c478bd9Sstevel@tonic-gate */
4467c478bd9Sstevel@tonic-gate
4477c478bd9Sstevel@tonic-gate static struct Gdef *
next_speed(struct Gdef * speedef)4483bb2c156SToomas Soome next_speed(struct Gdef *speedef)
4497c478bd9Sstevel@tonic-gate {
4507c478bd9Sstevel@tonic-gate struct Gdef *sp;
4517c478bd9Sstevel@tonic-gate
452b77c815bSNobutomo Nakano if (strcmp(speedef->g_nextid, speedef->g_id) == 0)
453b77c815bSNobutomo Nakano return (speedef);
4547c478bd9Sstevel@tonic-gate if ((sp = find_def(speedef->g_nextid)) == NULL) {
4557c478bd9Sstevel@tonic-gate log("%s's next speed-label (%s) is bad.", speedef->g_id,
4567c478bd9Sstevel@tonic-gate speedef->g_nextid);
4577c478bd9Sstevel@tonic-gate
4587c478bd9Sstevel@tonic-gate /* go back to the original entry. */
459b77c815bSNobutomo Nakano if ((sp = find_def(speedef->g_id)) == NULL) {
460b77c815bSNobutomo Nakano /* if failed, complain and quit. */
4617c478bd9Sstevel@tonic-gate log("unable to find (%s) again", speedef->g_id);
4627c478bd9Sstevel@tonic-gate exit(1);
4637c478bd9Sstevel@tonic-gate }
4647c478bd9Sstevel@tonic-gate }
465b77c815bSNobutomo Nakano return (sp);
4667c478bd9Sstevel@tonic-gate }
4677c478bd9Sstevel@tonic-gate
4687c478bd9Sstevel@tonic-gate /*
4697c478bd9Sstevel@tonic-gate * inform_parent() - inform ttymon that tmchild is going to exec service
4707c478bd9Sstevel@tonic-gate */
4717c478bd9Sstevel@tonic-gate static void
inform_parent(int fd)4723bb2c156SToomas Soome inform_parent(int fd)
4737c478bd9Sstevel@tonic-gate {
4747c478bd9Sstevel@tonic-gate pid_t pid;
4757c478bd9Sstevel@tonic-gate
4767c478bd9Sstevel@tonic-gate pid = getpid();
477b77c815bSNobutomo Nakano (void) write(fd, &pid, sizeof (pid));
4787c478bd9Sstevel@tonic-gate }
4797c478bd9Sstevel@tonic-gate
4803bb2c156SToomas Soome static char pbuf[BUFSIZ]; /* static buf for TTYPROMPT */
4813bb2c156SToomas Soome static char hbuf[BUFSIZ]; /* static buf for HOME */
4827c478bd9Sstevel@tonic-gate static char tbuf[BUFSIZ]; /* static buf for TERM */
4837c478bd9Sstevel@tonic-gate
4847c478bd9Sstevel@tonic-gate /*
4857c478bd9Sstevel@tonic-gate * void invoke_service - invoke the service
4867c478bd9Sstevel@tonic-gate */
4877c478bd9Sstevel@tonic-gate
4887c478bd9Sstevel@tonic-gate static void
invoke_service(struct pmtab * pmtab)4893bb2c156SToomas Soome invoke_service(struct pmtab *pmtab)
4907c478bd9Sstevel@tonic-gate {
4917c478bd9Sstevel@tonic-gate char *argvp[MAXARGS]; /* service cmd args */
4927c478bd9Sstevel@tonic-gate int cnt = 0; /* arg counter */
4933bb2c156SToomas Soome int i;
4947c478bd9Sstevel@tonic-gate struct sigaction sigact;
4957c478bd9Sstevel@tonic-gate
4963bb2c156SToomas Soome #ifdef DEBUG
4977c478bd9Sstevel@tonic-gate debug("in invoke_service");
4987c478bd9Sstevel@tonic-gate #endif
4997c478bd9Sstevel@tonic-gate
5007c478bd9Sstevel@tonic-gate if (tcgetsid(0) != getsid(getpid())) {
5017c478bd9Sstevel@tonic-gate cons_printf("Warning -- ttymon cannot allocate controlling "
5027c478bd9Sstevel@tonic-gate "tty on \"%s\",\n", pmtab->p_device);
5037c478bd9Sstevel@tonic-gate cons_printf("\tThere may be another session active on this "
5047c478bd9Sstevel@tonic-gate "port.\n");
5057c478bd9Sstevel@tonic-gate
5067c478bd9Sstevel@tonic-gate if (strcmp("/dev/console", pmtab->p_device) != 0) {
5077c478bd9Sstevel@tonic-gate /*
5087c478bd9Sstevel@tonic-gate * if not on console, write to stderr to warn the user
5097c478bd9Sstevel@tonic-gate * also.
5107c478bd9Sstevel@tonic-gate */
5117c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "Warning -- ttymon cannot "
5127c478bd9Sstevel@tonic-gate "allocate controlling tty on \"%s\",\n",
5137c478bd9Sstevel@tonic-gate pmtab->p_device);
514b77c815bSNobutomo Nakano (void) fprintf(stderr, "\tthere may be another session "
5157c478bd9Sstevel@tonic-gate "active on this port.\n");
5167c478bd9Sstevel@tonic-gate }
5177c478bd9Sstevel@tonic-gate }
5187c478bd9Sstevel@tonic-gate
5197c478bd9Sstevel@tonic-gate if (pmtab->p_status != GETTY) {
5207c478bd9Sstevel@tonic-gate inform_parent(PCpipe[1]);
5217c478bd9Sstevel@tonic-gate sigact.sa_flags = 0;
5227c478bd9Sstevel@tonic-gate sigact.sa_handler = SIG_DFL;
523b77c815bSNobutomo Nakano (void) sigemptyset(&sigact.sa_mask);
524b77c815bSNobutomo Nakano (void) sigaction(SIGPOLL, &sigact, NULL);
5257c478bd9Sstevel@tonic-gate }
5267c478bd9Sstevel@tonic-gate
5277c478bd9Sstevel@tonic-gate if (pmtab->p_flags & U_FLAG) {
5287c478bd9Sstevel@tonic-gate if (account(pmtab->p_device) != 0) {
5297c478bd9Sstevel@tonic-gate log("invoke_service: account failed");
5307c478bd9Sstevel@tonic-gate exit(1);
5317c478bd9Sstevel@tonic-gate }
5327c478bd9Sstevel@tonic-gate }
5337c478bd9Sstevel@tonic-gate
5347c478bd9Sstevel@tonic-gate /* parse command line */
535b77c815bSNobutomo Nakano mkargv(pmtab->p_server, &argvp[0], &cnt, MAXARGS-1);
5367c478bd9Sstevel@tonic-gate
5377c478bd9Sstevel@tonic-gate if (!(pmtab->p_ttyflags & C_FLAG)) {
5387c478bd9Sstevel@tonic-gate (void) sprintf(pbuf, "TTYPROMPT=%s", pmtab->p_prompt);
5397c478bd9Sstevel@tonic-gate if (putenv(pbuf)) {
5407c478bd9Sstevel@tonic-gate log("cannot expand service <%s> environment", argvp[0]);
5417c478bd9Sstevel@tonic-gate exit(1);
5427c478bd9Sstevel@tonic-gate }
5437c478bd9Sstevel@tonic-gate }
5447c478bd9Sstevel@tonic-gate if (pmtab->p_status != GETTY) {
5457c478bd9Sstevel@tonic-gate (void) sprintf(hbuf, "HOME=%s", pmtab->p_dir);
5467c478bd9Sstevel@tonic-gate if (putenv(hbuf)) {
547b77c815bSNobutomo Nakano log("cannot expand service <%s> environment", argvp[0]);
5487c478bd9Sstevel@tonic-gate exit(1);
5497c478bd9Sstevel@tonic-gate }
5507c478bd9Sstevel@tonic-gate #ifdef DEBUG
5517c478bd9Sstevel@tonic-gate debug("about to run config script");
5527c478bd9Sstevel@tonic-gate #endif
5537c478bd9Sstevel@tonic-gate if ((i = doconfig(0, pmtab->p_tag, 0)) != 0) {
5547c478bd9Sstevel@tonic-gate if (i < 0) {
5557c478bd9Sstevel@tonic-gate log("doconfig failed, system error");
5563bb2c156SToomas Soome } else {
5577c478bd9Sstevel@tonic-gate log("doconfig failed on line %d of script %s",
5587c478bd9Sstevel@tonic-gate i, pmtab->p_tag);
5597c478bd9Sstevel@tonic-gate }
5607c478bd9Sstevel@tonic-gate exit(1);
5617c478bd9Sstevel@tonic-gate }
5627c478bd9Sstevel@tonic-gate }
5637c478bd9Sstevel@tonic-gate
5647c478bd9Sstevel@tonic-gate if (setgid(pmtab->p_gid)) {
5657c478bd9Sstevel@tonic-gate log("cannot set group id to %ld: %s", pmtab->p_gid,
5667c478bd9Sstevel@tonic-gate strerror(errno));
5677c478bd9Sstevel@tonic-gate exit(1);
5687c478bd9Sstevel@tonic-gate }
5697c478bd9Sstevel@tonic-gate
5707c478bd9Sstevel@tonic-gate if (setuid(pmtab->p_uid)) {
5717c478bd9Sstevel@tonic-gate log("cannot set user id to %ld: %s", pmtab->p_uid,
5727c478bd9Sstevel@tonic-gate strerror(errno));
5737c478bd9Sstevel@tonic-gate exit(1);
5747c478bd9Sstevel@tonic-gate }
5757c478bd9Sstevel@tonic-gate
5767c478bd9Sstevel@tonic-gate if (chdir(pmtab->p_dir)) {
5777c478bd9Sstevel@tonic-gate log("cannot chdir to %s: %s", pmtab->p_dir, strerror(errno));
5787c478bd9Sstevel@tonic-gate exit(1);
5797c478bd9Sstevel@tonic-gate }
5807c478bd9Sstevel@tonic-gate
5817c478bd9Sstevel@tonic-gate if (pmtab->p_uid != ROOTUID) {
5827c478bd9Sstevel@tonic-gate /* change ownership and mode of device */
583b77c815bSNobutomo Nakano (void) fchown(0, pmtab->p_uid, Tty_gid);
584b77c815bSNobutomo Nakano (void) fchmod(0, 0620);
5857c478bd9Sstevel@tonic-gate }
5867c478bd9Sstevel@tonic-gate
5877c478bd9Sstevel@tonic-gate
5887c478bd9Sstevel@tonic-gate if (pmtab->p_status != GETTY) {
5897c478bd9Sstevel@tonic-gate sigact.sa_flags = 0;
5907c478bd9Sstevel@tonic-gate sigact.sa_handler = SIG_DFL;
591b77c815bSNobutomo Nakano (void) sigemptyset(&sigact.sa_mask);
592b77c815bSNobutomo Nakano (void) sigaction(SIGINT, &sigact, NULL);
5937c478bd9Sstevel@tonic-gate if (setrlimit(RLIMIT_NOFILE, &Rlimit) == -1) {
5947c478bd9Sstevel@tonic-gate log("setrlimit failed: %s", strerror(errno));
5957c478bd9Sstevel@tonic-gate exit(1);
5967c478bd9Sstevel@tonic-gate }
5977c478bd9Sstevel@tonic-gate /* invoke the service */
5987c478bd9Sstevel@tonic-gate log("Starting service (%s) on %s", argvp[0], pmtab->p_device);
5997c478bd9Sstevel@tonic-gate }
6007c478bd9Sstevel@tonic-gate
6017c478bd9Sstevel@tonic-gate if (pmtab->p_termtype != (char *)NULL) {
6027c478bd9Sstevel@tonic-gate (void) sprintf(tbuf, "TERM=%s", pmtab->p_termtype);
6037c478bd9Sstevel@tonic-gate if (putenv(tbuf)) {
6047c478bd9Sstevel@tonic-gate log("cannot expand service <%s> environment", argvp[0]);
6057c478bd9Sstevel@tonic-gate exit(1);
6067c478bd9Sstevel@tonic-gate }
6077c478bd9Sstevel@tonic-gate }
6087c478bd9Sstevel@tonic-gate /* restore signal handlers and mask */
609b77c815bSNobutomo Nakano (void) sigaction(SIGINT, &Sigint, NULL);
610b77c815bSNobutomo Nakano (void) sigaction(SIGALRM, &Sigalrm, NULL);
611b77c815bSNobutomo Nakano (void) sigaction(SIGPOLL, &Sigpoll, NULL);
612b77c815bSNobutomo Nakano (void) sigaction(SIGQUIT, &Sigquit, NULL);
613b77c815bSNobutomo Nakano (void) sigaction(SIGCLD, &Sigcld, NULL);
614b77c815bSNobutomo Nakano (void) sigaction(SIGTERM, &Sigterm, NULL);
6157c478bd9Sstevel@tonic-gate #ifdef DEBUG
616b77c815bSNobutomo Nakano (void) sigaction(SIGUSR1, &Sigusr1, NULL);
617b77c815bSNobutomo Nakano (void) sigaction(SIGUSR2, &Sigusr2, NULL);
6187c478bd9Sstevel@tonic-gate #endif
619b77c815bSNobutomo Nakano (void) sigprocmask(SIG_SETMASK, &Origmask, NULL);
6207c478bd9Sstevel@tonic-gate (void) execve(argvp[0], argvp, environ);
6217c478bd9Sstevel@tonic-gate
6227c478bd9Sstevel@tonic-gate /* exec returns only on failure! */
6237c478bd9Sstevel@tonic-gate log("tmchild: exec service failed: %s", strerror(errno));
6247c478bd9Sstevel@tonic-gate exit(1);
6257c478bd9Sstevel@tonic-gate }
6267c478bd9Sstevel@tonic-gate
6277c478bd9Sstevel@tonic-gate /*
6287c478bd9Sstevel@tonic-gate * check_hup(fd) - do a poll on fd to check if it is in hangup state
6297c478bd9Sstevel@tonic-gate * - return 1 if hangup, otherwise return 0
6307c478bd9Sstevel@tonic-gate */
6317c478bd9Sstevel@tonic-gate
6327c478bd9Sstevel@tonic-gate static int
check_hup(int fd)6333bb2c156SToomas Soome check_hup(int fd)
6347c478bd9Sstevel@tonic-gate {
6357c478bd9Sstevel@tonic-gate int ret;
6367c478bd9Sstevel@tonic-gate struct pollfd pfd[1];
6377c478bd9Sstevel@tonic-gate
6387c478bd9Sstevel@tonic-gate pfd[0].fd = fd;
6397c478bd9Sstevel@tonic-gate pfd[0].events = POLLHUP;
6407c478bd9Sstevel@tonic-gate for (;;) {
6417c478bd9Sstevel@tonic-gate ret = poll(pfd, 1, 0);
6427c478bd9Sstevel@tonic-gate if (ret < 0) {
643b77c815bSNobutomo Nakano if (errno == EINTR)
6447c478bd9Sstevel@tonic-gate continue;
6457c478bd9Sstevel@tonic-gate log("check_hup: poll failed: %s", strerror(errno));
6467c478bd9Sstevel@tonic-gate exit(1);
6473bb2c156SToomas Soome } else if (ret > 0) {
6487c478bd9Sstevel@tonic-gate if (pfd[0].revents & POLLHUP) {
649b77c815bSNobutomo Nakano return (1);
6507c478bd9Sstevel@tonic-gate }
6517c478bd9Sstevel@tonic-gate }
652b77c815bSNobutomo Nakano return (0);
6537c478bd9Sstevel@tonic-gate }
6547c478bd9Sstevel@tonic-gate }
6557c478bd9Sstevel@tonic-gate
6567c478bd9Sstevel@tonic-gate /*
6577c478bd9Sstevel@tonic-gate * sigpoll() - SIGPOLL handle for tmchild
6587c478bd9Sstevel@tonic-gate * - when SIGPOLL is received by tmchild,
6597c478bd9Sstevel@tonic-gate * the pipe between ttymon and tmchild is broken.
6607c478bd9Sstevel@tonic-gate * Something must happen to ttymon.
6617c478bd9Sstevel@tonic-gate */
6627c478bd9Sstevel@tonic-gate void
sigpoll(int s __unused)6633bb2c156SToomas Soome sigpoll(int s __unused)
6647c478bd9Sstevel@tonic-gate {
6657c478bd9Sstevel@tonic-gate #ifdef DEBUG
6667c478bd9Sstevel@tonic-gate debug("tmchild got SIGPOLL, exiting");
6677c478bd9Sstevel@tonic-gate #endif
6687c478bd9Sstevel@tonic-gate exit(1);
6697c478bd9Sstevel@tonic-gate }
670