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
34*0Sstevel@tonic-gate #include <stdlib.h>
35*0Sstevel@tonic-gate #include <stdio.h>
36*0Sstevel@tonic-gate #include <errno.h>
37*0Sstevel@tonic-gate #include <termio.h>
38*0Sstevel@tonic-gate #include <sys/stermio.h>
39*0Sstevel@tonic-gate #include <sys/termiox.h>
40*0Sstevel@tonic-gate #include <string.h>
41*0Sstevel@tonic-gate #include <ctype.h>
42*0Sstevel@tonic-gate #include <unistd.h>
43*0Sstevel@tonic-gate #include <sys/types.h>
44*0Sstevel@tonic-gate #include "sys/stropts.h"
45*0Sstevel@tonic-gate #include "sys/signal.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
50*0Sstevel@tonic-gate extern void mkargv();
51*0Sstevel@tonic-gate
52*0Sstevel@tonic-gate /*
53*0Sstevel@tonic-gate * set_termio - set termio on device
54*0Sstevel@tonic-gate * fd - fd for the device
55*0Sstevel@tonic-gate * options - stty termio options
56*0Sstevel@tonic-gate * aspeed - autobaud speed
57*0Sstevel@tonic-gate * clear - if TRUE, current flags will be set to some defaults
58*0Sstevel@tonic-gate * before applying the options
59*0Sstevel@tonic-gate * - if FALSE, current flags will not be cleared
60*0Sstevel@tonic-gate * mode - terminal mode, CANON, RAW
61*0Sstevel@tonic-gate */
62*0Sstevel@tonic-gate
63*0Sstevel@tonic-gate
64*0Sstevel@tonic-gate
65*0Sstevel@tonic-gate int
set_termio(fd,options,aspeed,clear,mode)66*0Sstevel@tonic-gate set_termio(fd,options,aspeed,clear,mode)
67*0Sstevel@tonic-gate int fd;
68*0Sstevel@tonic-gate char *options;
69*0Sstevel@tonic-gate char *aspeed;
70*0Sstevel@tonic-gate int clear;
71*0Sstevel@tonic-gate long mode;
72*0Sstevel@tonic-gate {
73*0Sstevel@tonic-gate struct termio termio;
74*0Sstevel@tonic-gate struct termios termios;
75*0Sstevel@tonic-gate struct stio stermio;
76*0Sstevel@tonic-gate struct termiox termiox;
77*0Sstevel@tonic-gate struct winsize winsize;
78*0Sstevel@tonic-gate struct winsize owinsize;
79*0Sstevel@tonic-gate int term;
80*0Sstevel@tonic-gate int cnt = 1;
81*0Sstevel@tonic-gate char *uarg;
82*0Sstevel@tonic-gate char *argvp[MAXARGS]; /* stty args */
83*0Sstevel@tonic-gate static char *binstty = "/usr/bin/stty";
84*0Sstevel@tonic-gate static char buf[BUFSIZ];
85*0Sstevel@tonic-gate extern int get_ttymode(), set_ttymode();
86*0Sstevel@tonic-gate extern char *sttyparse();
87*0Sstevel@tonic-gate
88*0Sstevel@tonic-gate #ifdef DEBUG
89*0Sstevel@tonic-gate debug("in set_termio");
90*0Sstevel@tonic-gate #endif
91*0Sstevel@tonic-gate
92*0Sstevel@tonic-gate if ((term = get_ttymode(fd, &termio, &termios, &stermio,
93*0Sstevel@tonic-gate &termiox, &winsize)) < 0) {
94*0Sstevel@tonic-gate log("set_termio: get_ttymode failed: %s", strerror(errno));
95*0Sstevel@tonic-gate return(-1);
96*0Sstevel@tonic-gate }
97*0Sstevel@tonic-gate owinsize = winsize;
98*0Sstevel@tonic-gate if (clear) {
99*0Sstevel@tonic-gate if (mode & CANON) {
100*0Sstevel@tonic-gate /* could have removed these too - rely on defaults */
101*0Sstevel@tonic-gate termios.c_cc[VEOF] = CEOF;
102*0Sstevel@tonic-gate termios.c_cc[VEOL] = CNUL;
103*0Sstevel@tonic-gate }
104*0Sstevel@tonic-gate else {
105*0Sstevel@tonic-gate termios.c_lflag &= ECHO;
106*0Sstevel@tonic-gate termios.c_cc[VMIN] = 1;
107*0Sstevel@tonic-gate termios.c_cc[VTIME] = 0;
108*0Sstevel@tonic-gate }
109*0Sstevel@tonic-gate
110*0Sstevel@tonic-gate }
111*0Sstevel@tonic-gate
112*0Sstevel@tonic-gate if (options != NULL && *options != '\0') {
113*0Sstevel@tonic-gate /* just a place holder to make it look like invoking stty */
114*0Sstevel@tonic-gate argvp[0] = binstty;
115*0Sstevel@tonic-gate (void)strcpy(buf,options);
116*0Sstevel@tonic-gate mkargv(buf,&argvp[1],&cnt,MAXARGS-1);
117*0Sstevel@tonic-gate if (aspeed != NULL && *aspeed != '\0') {
118*0Sstevel@tonic-gate argvp[cnt++] = aspeed;
119*0Sstevel@tonic-gate }
120*0Sstevel@tonic-gate argvp[cnt] = (char *)0;
121*0Sstevel@tonic-gate if ((uarg = sttyparse(cnt, argvp, term, &termio, &termios,
122*0Sstevel@tonic-gate &termiox, &winsize)) != NULL) {
123*0Sstevel@tonic-gate log("sttyparse unknown mode: %s", uarg);
124*0Sstevel@tonic-gate return(-1);
125*0Sstevel@tonic-gate }
126*0Sstevel@tonic-gate }
127*0Sstevel@tonic-gate
128*0Sstevel@tonic-gate
129*0Sstevel@tonic-gate if (set_ttymode(fd, term, &termio, &termios, &stermio,
130*0Sstevel@tonic-gate &termiox, &winsize, &owinsize) != 0) {
131*0Sstevel@tonic-gate log("set_termio: set_ttymode failed", strerror(errno));
132*0Sstevel@tonic-gate return(-1);
133*0Sstevel@tonic-gate }
134*0Sstevel@tonic-gate
135*0Sstevel@tonic-gate return(0);
136*0Sstevel@tonic-gate }
137*0Sstevel@tonic-gate
138*0Sstevel@tonic-gate #ifdef NOT_USE
139*0Sstevel@tonic-gate /*
140*0Sstevel@tonic-gate * turnon_canon - turn on canonical processing
141*0Sstevel@tonic-gate * - return 0 if succeeds, -1 if fails
142*0Sstevel@tonic-gate */
turnon_canon(fd)143*0Sstevel@tonic-gate turnon_canon(fd)
144*0Sstevel@tonic-gate int fd;
145*0Sstevel@tonic-gate {
146*0Sstevel@tonic-gate struct termio termio;
147*0Sstevel@tonic-gate
148*0Sstevel@tonic-gate #ifdef DEBUG
149*0Sstevel@tonic-gate debug("in turnon_canon");
150*0Sstevel@tonic-gate #endif
151*0Sstevel@tonic-gate if (ioctl(fd, TCGETA, &termio) != 0) {
152*0Sstevel@tonic-gate log("turnon_canon: TCGETA failed, fd = %d: %s", fd,
153*0Sstevel@tonic-gate strerror(errno));
154*0Sstevel@tonic-gate return(-1);
155*0Sstevel@tonic-gate }
156*0Sstevel@tonic-gate termio.c_lflag |= (ISIG|ICANON|ECHO|ECHOE|ECHOK);
157*0Sstevel@tonic-gate termio.c_cc[VEOF] = CEOF;
158*0Sstevel@tonic-gate termio.c_cc[VEOL] = CNUL;
159*0Sstevel@tonic-gate if (ioctl(fd, TCSETA, &termio) != 0) {
160*0Sstevel@tonic-gate log("turnon_canon: TCSETA failed, fd = %d: %s", fd,
161*0Sstevel@tonic-gate strerror(errno));
162*0Sstevel@tonic-gate return(-1);
163*0Sstevel@tonic-gate }
164*0Sstevel@tonic-gate return(0);
165*0Sstevel@tonic-gate }
166*0Sstevel@tonic-gate #endif
167*0Sstevel@tonic-gate
168*0Sstevel@tonic-gate /*
169*0Sstevel@tonic-gate * flush_input - flush the input queue
170*0Sstevel@tonic-gate */
171*0Sstevel@tonic-gate void
flush_input(fd)172*0Sstevel@tonic-gate flush_input(fd)
173*0Sstevel@tonic-gate int fd;
174*0Sstevel@tonic-gate {
175*0Sstevel@tonic-gate if (ioctl(fd, I_FLUSH, FLUSHR) == -1)
176*0Sstevel@tonic-gate log("flush_input failed, fd = %d: %s", fd, strerror(errno));
177*0Sstevel@tonic-gate
178*0Sstevel@tonic-gate if (ioctl(fd, TCSBRK, 1) == -1)
179*0Sstevel@tonic-gate log("drain of ouput failed, fd = %d: %s", fd, strerror(errno));
180*0Sstevel@tonic-gate
181*0Sstevel@tonic-gate return;
182*0Sstevel@tonic-gate }
183*0Sstevel@tonic-gate
184*0Sstevel@tonic-gate /*
185*0Sstevel@tonic-gate * push_linedisc - if modules is not NULL, pop everything
186*0Sstevel@tonic-gate * - then push modules specified by "modules"
187*0Sstevel@tonic-gate */
188*0Sstevel@tonic-gate int
push_linedisc(int fd,char * modules,char * device)189*0Sstevel@tonic-gate push_linedisc(
190*0Sstevel@tonic-gate int fd, /* fd to push modules on */
191*0Sstevel@tonic-gate char *modules, /* ptr to a list of comma separated module names */
192*0Sstevel@tonic-gate char *device) /* device name for printing msg */
193*0Sstevel@tonic-gate {
194*0Sstevel@tonic-gate char *p, *tp;
195*0Sstevel@tonic-gate char buf[BUFSIZ];
196*0Sstevel@tonic-gate
197*0Sstevel@tonic-gate #ifdef DEBUG
198*0Sstevel@tonic-gate debug("in push_linedisc");
199*0Sstevel@tonic-gate #endif
200*0Sstevel@tonic-gate /*
201*0Sstevel@tonic-gate * copy modules into buf so we won't mess up the original buffer
202*0Sstevel@tonic-gate * because strtok will chop the string
203*0Sstevel@tonic-gate */
204*0Sstevel@tonic-gate p = strcpy(buf,modules);
205*0Sstevel@tonic-gate
206*0Sstevel@tonic-gate while(ioctl(fd, I_POP, 0) >= 0) /* pop everything */
207*0Sstevel@tonic-gate ;
208*0Sstevel@tonic-gate for (p=(char *)strtok(p,","); p!=(char *)NULL;
209*0Sstevel@tonic-gate p=(char *)strtok(NULL,",")) {
210*0Sstevel@tonic-gate for (tp = p + strlen(p) - 1; tp >= p && isspace(*tp); --tp)
211*0Sstevel@tonic-gate *tp = '\0';
212*0Sstevel@tonic-gate if (ioctl(fd, I_PUSH, p) == -1) {
213*0Sstevel@tonic-gate log("push (%s) on %s failed: %s", p, device,
214*0Sstevel@tonic-gate strerror(errno));
215*0Sstevel@tonic-gate return(-1);
216*0Sstevel@tonic-gate }
217*0Sstevel@tonic-gate }
218*0Sstevel@tonic-gate return(0);
219*0Sstevel@tonic-gate }
220*0Sstevel@tonic-gate
221*0Sstevel@tonic-gate /*
222*0Sstevel@tonic-gate * hang_up_line - set speed to B0. This will drop DTR
223*0Sstevel@tonic-gate */
224*0Sstevel@tonic-gate int
hang_up_line(int fd)225*0Sstevel@tonic-gate hang_up_line(int fd)
226*0Sstevel@tonic-gate {
227*0Sstevel@tonic-gate struct termio termio;
228*0Sstevel@tonic-gate struct termios termios;
229*0Sstevel@tonic-gate
230*0Sstevel@tonic-gate #ifdef DEBUG
231*0Sstevel@tonic-gate debug("in hang_up_line");
232*0Sstevel@tonic-gate #endif
233*0Sstevel@tonic-gate if (ioctl(fd,TCGETS,&termios) < 0) {
234*0Sstevel@tonic-gate if (ioctl(fd,TCGETA,&termio) < 0) {
235*0Sstevel@tonic-gate log("hang_up_line: TCGETA failed: %s", strerror(errno));
236*0Sstevel@tonic-gate return(-1);
237*0Sstevel@tonic-gate }
238*0Sstevel@tonic-gate termio.c_cflag &= ~CBAUD;
239*0Sstevel@tonic-gate termio.c_cflag |= B0;
240*0Sstevel@tonic-gate
241*0Sstevel@tonic-gate if (ioctl(fd,TCSETA,&termio) < 0) {
242*0Sstevel@tonic-gate log("hang_up_line: TCSETA failed: %s", strerror(errno));
243*0Sstevel@tonic-gate return(-1);
244*0Sstevel@tonic-gate }
245*0Sstevel@tonic-gate } else {
246*0Sstevel@tonic-gate cfsetospeed(&termios, B0);
247*0Sstevel@tonic-gate
248*0Sstevel@tonic-gate if (ioctl(fd,TCSETS,&termios) < 0) {
249*0Sstevel@tonic-gate log("hang_up_line: TCSETS failed: %s", strerror(errno));
250*0Sstevel@tonic-gate return(-1);
251*0Sstevel@tonic-gate }
252*0Sstevel@tonic-gate }
253*0Sstevel@tonic-gate return(0);
254*0Sstevel@tonic-gate }
255*0Sstevel@tonic-gate
256*0Sstevel@tonic-gate /*
257*0Sstevel@tonic-gate * initial_termio - set initial termios
258*0Sstevel@tonic-gate * - return 0 if successful, -1 if failed.
259*0Sstevel@tonic-gate */
260*0Sstevel@tonic-gate int
initial_termio(fd,pmptr)261*0Sstevel@tonic-gate initial_termio(fd,pmptr)
262*0Sstevel@tonic-gate int fd;
263*0Sstevel@tonic-gate struct pmtab *pmptr;
264*0Sstevel@tonic-gate {
265*0Sstevel@tonic-gate int ret;
266*0Sstevel@tonic-gate struct Gdef *speedef;
267*0Sstevel@tonic-gate struct Gdef *get_speed();
268*0Sstevel@tonic-gate extern int auto_termio();
269*0Sstevel@tonic-gate
270*0Sstevel@tonic-gate speedef = get_speed(pmptr->p_ttylabel);
271*0Sstevel@tonic-gate if (speedef->g_autobaud & A_FLAG) {
272*0Sstevel@tonic-gate pmptr->p_ttyflags |= A_FLAG;
273*0Sstevel@tonic-gate if (auto_termio(fd) == -1) {
274*0Sstevel@tonic-gate (void)close(fd);
275*0Sstevel@tonic-gate return(-1);
276*0Sstevel@tonic-gate }
277*0Sstevel@tonic-gate }
278*0Sstevel@tonic-gate else {
279*0Sstevel@tonic-gate if (pmptr->p_ttyflags & R_FLAG)
280*0Sstevel@tonic-gate ret = set_termio(fd,speedef->g_iflags,
281*0Sstevel@tonic-gate (char *)NULL, TRUE, (long)RAW);
282*0Sstevel@tonic-gate else
283*0Sstevel@tonic-gate ret = set_termio(fd,speedef->g_iflags,
284*0Sstevel@tonic-gate (char *)NULL, TRUE, (long)CANON);
285*0Sstevel@tonic-gate if (ret == -1) {
286*0Sstevel@tonic-gate log("initial termio on (%s) failed", pmptr->p_device);
287*0Sstevel@tonic-gate (void)close(fd);
288*0Sstevel@tonic-gate return(-1);
289*0Sstevel@tonic-gate }
290*0Sstevel@tonic-gate }
291*0Sstevel@tonic-gate return(0);
292*0Sstevel@tonic-gate }
293