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
57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate * with the License.
87c478bd9Sstevel@tonic-gate *
97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate * and limitations under the License.
137c478bd9Sstevel@tonic-gate *
147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate *
207c478bd9Sstevel@tonic-gate * CDDL HEADER END
217c478bd9Sstevel@tonic-gate */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
247c478bd9Sstevel@tonic-gate * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
283bb2c156SToomas Soome /* All Rights Reserved */
297c478bd9Sstevel@tonic-gate
307c478bd9Sstevel@tonic-gate #include <stdlib.h>
317c478bd9Sstevel@tonic-gate #include <stdio.h>
327c478bd9Sstevel@tonic-gate #include <errno.h>
337c478bd9Sstevel@tonic-gate #include <termio.h>
347c478bd9Sstevel@tonic-gate #include <sys/stermio.h>
357c478bd9Sstevel@tonic-gate #include <sys/termiox.h>
367c478bd9Sstevel@tonic-gate #include <string.h>
377c478bd9Sstevel@tonic-gate #include <ctype.h>
387c478bd9Sstevel@tonic-gate #include <unistd.h>
397c478bd9Sstevel@tonic-gate #include <sys/types.h>
407c478bd9Sstevel@tonic-gate #include "sys/stropts.h"
417c478bd9Sstevel@tonic-gate #include "sys/signal.h"
423bb2c156SToomas Soome #include "ttymon.h"
433bb2c156SToomas Soome #include "tmstruct.h"
443bb2c156SToomas Soome #include "tmextern.h"
457c478bd9Sstevel@tonic-gate
467c478bd9Sstevel@tonic-gate /*
473bb2c156SToomas Soome * set_termio - set termio on device
487c478bd9Sstevel@tonic-gate * fd - fd for the device
493bb2c156SToomas Soome * options - stty termio options
503bb2c156SToomas Soome * aspeed - autobaud speed
517c478bd9Sstevel@tonic-gate * clear - if TRUE, current flags will be set to some defaults
523bb2c156SToomas Soome * before applying the options
533bb2c156SToomas Soome * - if FALSE, current flags will not be cleared
547c478bd9Sstevel@tonic-gate * mode - terminal mode, CANON, RAW
557c478bd9Sstevel@tonic-gate */
567c478bd9Sstevel@tonic-gate
577c478bd9Sstevel@tonic-gate
587c478bd9Sstevel@tonic-gate
597c478bd9Sstevel@tonic-gate int
set_termio(int fd,char * options,char * aspeed,int clear,long mode)603bb2c156SToomas Soome set_termio(int fd, char *options, char *aspeed, int clear, long mode)
617c478bd9Sstevel@tonic-gate {
623bb2c156SToomas Soome struct termio termio;
633bb2c156SToomas Soome struct termios termios;
643bb2c156SToomas Soome struct stio stermio;
653bb2c156SToomas Soome struct termiox termiox;
663bb2c156SToomas Soome struct winsize winsize;
673bb2c156SToomas Soome struct winsize owinsize;
687c478bd9Sstevel@tonic-gate int term;
697c478bd9Sstevel@tonic-gate int cnt = 1;
703bb2c156SToomas Soome char *uarg;
717c478bd9Sstevel@tonic-gate char *argvp[MAXARGS]; /* stty args */
727c478bd9Sstevel@tonic-gate static char *binstty = "/usr/bin/stty";
737c478bd9Sstevel@tonic-gate static char buf[BUFSIZ];
747c478bd9Sstevel@tonic-gate
757c478bd9Sstevel@tonic-gate #ifdef DEBUG
767c478bd9Sstevel@tonic-gate debug("in set_termio");
777c478bd9Sstevel@tonic-gate #endif
787c478bd9Sstevel@tonic-gate
793bb2c156SToomas Soome if ((term = get_ttymode(fd, &termio, &termios, &stermio,
803bb2c156SToomas Soome &termiox, &winsize)) < 0) {
817c478bd9Sstevel@tonic-gate log("set_termio: get_ttymode failed: %s", strerror(errno));
823bb2c156SToomas Soome return (-1);
837c478bd9Sstevel@tonic-gate }
847c478bd9Sstevel@tonic-gate owinsize = winsize;
857c478bd9Sstevel@tonic-gate if (clear) {
867c478bd9Sstevel@tonic-gate if (mode & CANON) {
877c478bd9Sstevel@tonic-gate /* could have removed these too - rely on defaults */
887c478bd9Sstevel@tonic-gate termios.c_cc[VEOF] = CEOF;
897c478bd9Sstevel@tonic-gate termios.c_cc[VEOL] = CNUL;
903bb2c156SToomas Soome } else {
917c478bd9Sstevel@tonic-gate termios.c_lflag &= ECHO;
927c478bd9Sstevel@tonic-gate termios.c_cc[VMIN] = 1;
937c478bd9Sstevel@tonic-gate termios.c_cc[VTIME] = 0;
947c478bd9Sstevel@tonic-gate }
957c478bd9Sstevel@tonic-gate
967c478bd9Sstevel@tonic-gate }
977c478bd9Sstevel@tonic-gate
987c478bd9Sstevel@tonic-gate if (options != NULL && *options != '\0') {
997c478bd9Sstevel@tonic-gate /* just a place holder to make it look like invoking stty */
1007c478bd9Sstevel@tonic-gate argvp[0] = binstty;
1013bb2c156SToomas Soome (void) strcpy(buf, options);
1023bb2c156SToomas Soome mkargv(buf, &argvp[1], &cnt, MAXARGS - 1);
1037c478bd9Sstevel@tonic-gate if (aspeed != NULL && *aspeed != '\0') {
1047c478bd9Sstevel@tonic-gate argvp[cnt++] = aspeed;
1057c478bd9Sstevel@tonic-gate }
1067c478bd9Sstevel@tonic-gate argvp[cnt] = (char *)0;
1073bb2c156SToomas Soome if ((uarg = sttyparse(cnt, argvp, term, &termio, &termios,
1083bb2c156SToomas Soome &termiox, &winsize)) != NULL) {
1097c478bd9Sstevel@tonic-gate log("sttyparse unknown mode: %s", uarg);
1103bb2c156SToomas Soome return (-1);
1117c478bd9Sstevel@tonic-gate }
1127c478bd9Sstevel@tonic-gate }
1137c478bd9Sstevel@tonic-gate
1147c478bd9Sstevel@tonic-gate
1153bb2c156SToomas Soome if (set_ttymode(fd, term, &termio, &termios, &stermio,
1163bb2c156SToomas Soome &termiox, &winsize, &owinsize) != 0) {
1177c478bd9Sstevel@tonic-gate log("set_termio: set_ttymode failed", strerror(errno));
1183bb2c156SToomas Soome return (-1);
1197c478bd9Sstevel@tonic-gate }
1207c478bd9Sstevel@tonic-gate
1213bb2c156SToomas Soome return (0);
1227c478bd9Sstevel@tonic-gate }
1237c478bd9Sstevel@tonic-gate
1247c478bd9Sstevel@tonic-gate #ifdef NOT_USE
1257c478bd9Sstevel@tonic-gate /*
1267c478bd9Sstevel@tonic-gate * turnon_canon - turn on canonical processing
1277c478bd9Sstevel@tonic-gate * - return 0 if succeeds, -1 if fails
1287c478bd9Sstevel@tonic-gate */
turnon_canon(int fd)1293bb2c156SToomas Soome turnon_canon(int fd)
1307c478bd9Sstevel@tonic-gate {
1317c478bd9Sstevel@tonic-gate struct termio termio;
1327c478bd9Sstevel@tonic-gate
1337c478bd9Sstevel@tonic-gate #ifdef DEBUG
1347c478bd9Sstevel@tonic-gate debug("in turnon_canon");
1357c478bd9Sstevel@tonic-gate #endif
1367c478bd9Sstevel@tonic-gate if (ioctl(fd, TCGETA, &termio) != 0) {
1377c478bd9Sstevel@tonic-gate log("turnon_canon: TCGETA failed, fd = %d: %s", fd,
1387c478bd9Sstevel@tonic-gate strerror(errno));
1393bb2c156SToomas Soome return (-1);
1407c478bd9Sstevel@tonic-gate }
1413bb2c156SToomas Soome termio.c_lflag |= (ISIG|ICANON|ECHO|ECHOE|ECHOK);
1427c478bd9Sstevel@tonic-gate termio.c_cc[VEOF] = CEOF;
1437c478bd9Sstevel@tonic-gate termio.c_cc[VEOL] = CNUL;
1447c478bd9Sstevel@tonic-gate if (ioctl(fd, TCSETA, &termio) != 0) {
1457c478bd9Sstevel@tonic-gate log("turnon_canon: TCSETA failed, fd = %d: %s", fd,
1467c478bd9Sstevel@tonic-gate strerror(errno));
1473bb2c156SToomas Soome return (-1);
1487c478bd9Sstevel@tonic-gate }
1493bb2c156SToomas Soome return (0);
1507c478bd9Sstevel@tonic-gate }
1517c478bd9Sstevel@tonic-gate #endif
1527c478bd9Sstevel@tonic-gate
1537c478bd9Sstevel@tonic-gate /*
1547c478bd9Sstevel@tonic-gate * flush_input - flush the input queue
1557c478bd9Sstevel@tonic-gate */
1567c478bd9Sstevel@tonic-gate void
flush_input(int fd)1573bb2c156SToomas Soome flush_input(int fd)
1587c478bd9Sstevel@tonic-gate {
1597c478bd9Sstevel@tonic-gate if (ioctl(fd, I_FLUSH, FLUSHR) == -1)
1607c478bd9Sstevel@tonic-gate log("flush_input failed, fd = %d: %s", fd, strerror(errno));
1617c478bd9Sstevel@tonic-gate
1627c478bd9Sstevel@tonic-gate if (ioctl(fd, TCSBRK, 1) == -1)
1637c478bd9Sstevel@tonic-gate log("drain of ouput failed, fd = %d: %s", fd, strerror(errno));
1647c478bd9Sstevel@tonic-gate }
1657c478bd9Sstevel@tonic-gate
1667c478bd9Sstevel@tonic-gate /*
1677c478bd9Sstevel@tonic-gate * push_linedisc - if modules is not NULL, pop everything
1687c478bd9Sstevel@tonic-gate * - then push modules specified by "modules"
1697c478bd9Sstevel@tonic-gate */
1707c478bd9Sstevel@tonic-gate int
push_linedisc(int fd,char * modules,char * device)1717c478bd9Sstevel@tonic-gate push_linedisc(
1727c478bd9Sstevel@tonic-gate int fd, /* fd to push modules on */
1737c478bd9Sstevel@tonic-gate char *modules, /* ptr to a list of comma separated module names */
1747c478bd9Sstevel@tonic-gate char *device) /* device name for printing msg */
1757c478bd9Sstevel@tonic-gate {
1767c478bd9Sstevel@tonic-gate char *p, *tp;
1777c478bd9Sstevel@tonic-gate char buf[BUFSIZ];
1787c478bd9Sstevel@tonic-gate
1797c478bd9Sstevel@tonic-gate #ifdef DEBUG
1807c478bd9Sstevel@tonic-gate debug("in push_linedisc");
1817c478bd9Sstevel@tonic-gate #endif
1827c478bd9Sstevel@tonic-gate /*
1837c478bd9Sstevel@tonic-gate * copy modules into buf so we won't mess up the original buffer
1847c478bd9Sstevel@tonic-gate * because strtok will chop the string
1857c478bd9Sstevel@tonic-gate */
1863bb2c156SToomas Soome p = strcpy(buf, modules);
1877c478bd9Sstevel@tonic-gate
1883bb2c156SToomas Soome while (ioctl(fd, I_POP, 0) >= 0) /* pop everything */
1897c478bd9Sstevel@tonic-gate ;
1903bb2c156SToomas Soome for (p = strtok(p, ","); p != NULL; p = strtok(NULL, ",")) {
1917c478bd9Sstevel@tonic-gate for (tp = p + strlen(p) - 1; tp >= p && isspace(*tp); --tp)
1927c478bd9Sstevel@tonic-gate *tp = '\0';
1937c478bd9Sstevel@tonic-gate if (ioctl(fd, I_PUSH, p) == -1) {
1947c478bd9Sstevel@tonic-gate log("push (%s) on %s failed: %s", p, device,
1957c478bd9Sstevel@tonic-gate strerror(errno));
1963bb2c156SToomas Soome return (-1);
1973bb2c156SToomas Soome }
1987c478bd9Sstevel@tonic-gate }
1993bb2c156SToomas Soome return (0);
2007c478bd9Sstevel@tonic-gate }
2017c478bd9Sstevel@tonic-gate
2027c478bd9Sstevel@tonic-gate /*
2037c478bd9Sstevel@tonic-gate * hang_up_line - set speed to B0. This will drop DTR
2047c478bd9Sstevel@tonic-gate */
2057c478bd9Sstevel@tonic-gate int
hang_up_line(int fd)2067c478bd9Sstevel@tonic-gate hang_up_line(int fd)
2077c478bd9Sstevel@tonic-gate {
2087c478bd9Sstevel@tonic-gate struct termio termio;
2097c478bd9Sstevel@tonic-gate struct termios termios;
2107c478bd9Sstevel@tonic-gate
2117c478bd9Sstevel@tonic-gate #ifdef DEBUG
2127c478bd9Sstevel@tonic-gate debug("in hang_up_line");
2137c478bd9Sstevel@tonic-gate #endif
2143bb2c156SToomas Soome if (ioctl(fd, TCGETS, &termios) < 0) {
2153bb2c156SToomas Soome if (ioctl(fd, TCGETA, &termio) < 0) {
2163bb2c156SToomas Soome log("hang_up_line: TCGETA failed: %s", strerror(errno));
2173bb2c156SToomas Soome return (-1);
2183bb2c156SToomas Soome }
2193bb2c156SToomas Soome termio.c_cflag &= ~CBAUD;
2203bb2c156SToomas Soome termio.c_cflag |= B0;
2217c478bd9Sstevel@tonic-gate
2223bb2c156SToomas Soome if (ioctl(fd, TCSETA, &termio) < 0) {
2233bb2c156SToomas Soome log("hang_up_line: TCSETA failed: %s", strerror(errno));
2243bb2c156SToomas Soome return (-1);
2253bb2c156SToomas Soome }
2267c478bd9Sstevel@tonic-gate } else {
2273bb2c156SToomas Soome (void) cfsetospeed(&termios, B0);
2287c478bd9Sstevel@tonic-gate
2293bb2c156SToomas Soome if (ioctl(fd, TCSETS, &termios) < 0) {
2303bb2c156SToomas Soome log("hang_up_line: TCSETS failed: %s", strerror(errno));
2313bb2c156SToomas Soome return (-1);
2323bb2c156SToomas Soome }
2337c478bd9Sstevel@tonic-gate }
2343bb2c156SToomas Soome return (0);
2357c478bd9Sstevel@tonic-gate }
2367c478bd9Sstevel@tonic-gate
2377c478bd9Sstevel@tonic-gate /*
2387c478bd9Sstevel@tonic-gate * initial_termio - set initial termios
2397c478bd9Sstevel@tonic-gate * - return 0 if successful, -1 if failed.
2407c478bd9Sstevel@tonic-gate */
2417c478bd9Sstevel@tonic-gate int
initial_termio(int fd,struct pmtab * pmptr)2423bb2c156SToomas Soome initial_termio(int fd, struct pmtab *pmptr)
2437c478bd9Sstevel@tonic-gate {
2447c478bd9Sstevel@tonic-gate int ret;
2457c478bd9Sstevel@tonic-gate struct Gdef *speedef;
2467c478bd9Sstevel@tonic-gate
247*902bba37SToomas Soome speedef = get_speed(pmptr);
2487c478bd9Sstevel@tonic-gate if (speedef->g_autobaud & A_FLAG) {
2497c478bd9Sstevel@tonic-gate pmptr->p_ttyflags |= A_FLAG;
2507c478bd9Sstevel@tonic-gate if (auto_termio(fd) == -1) {
2513bb2c156SToomas Soome (void) close(fd);
2523bb2c156SToomas Soome return (-1);
2537c478bd9Sstevel@tonic-gate }
2543bb2c156SToomas Soome } else {
2557c478bd9Sstevel@tonic-gate if (pmptr->p_ttyflags & R_FLAG)
2563bb2c156SToomas Soome ret = set_termio(fd, speedef->g_iflags,
2573bb2c156SToomas Soome NULL, TRUE, (long)RAW);
2583bb2c156SToomas Soome else
2593bb2c156SToomas Soome ret = set_termio(fd, speedef->g_iflags,
2603bb2c156SToomas Soome NULL, TRUE, (long)CANON);
2617c478bd9Sstevel@tonic-gate if (ret == -1) {
2627c478bd9Sstevel@tonic-gate log("initial termio on (%s) failed", pmptr->p_device);
2633bb2c156SToomas Soome (void) close(fd);
2643bb2c156SToomas Soome return (-1);
2657c478bd9Sstevel@tonic-gate }
2667c478bd9Sstevel@tonic-gate }
2673bb2c156SToomas Soome return (0);
2687c478bd9Sstevel@tonic-gate }
269