tty_pty.c revision 1.19 1 1.1 cgd /*
2 1.1 cgd * Copyright (c) 1982, 1986, 1989 The Regents of the University of California.
3 1.1 cgd * All rights reserved.
4 1.1 cgd *
5 1.1 cgd * Redistribution and use in source and binary forms, with or without
6 1.1 cgd * modification, are permitted provided that the following conditions
7 1.1 cgd * are met:
8 1.1 cgd * 1. Redistributions of source code must retain the above copyright
9 1.1 cgd * notice, this list of conditions and the following disclaimer.
10 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer in the
12 1.1 cgd * documentation and/or other materials provided with the distribution.
13 1.1 cgd * 3. All advertising materials mentioning features or use of this software
14 1.1 cgd * must display the following acknowledgement:
15 1.1 cgd * This product includes software developed by the University of
16 1.1 cgd * California, Berkeley and its contributors.
17 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
18 1.1 cgd * may be used to endorse or promote products derived from this software
19 1.1 cgd * without specific prior written permission.
20 1.1 cgd *
21 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.1 cgd * SUCH DAMAGE.
32 1.1 cgd *
33 1.4 cgd * from: @(#)tty_pty.c 7.21 (Berkeley) 5/30/91
34 1.19 cgd * $Id: tty_pty.c,v 1.19 1994/04/25 05:48:41 cgd Exp $
35 1.1 cgd */
36 1.1 cgd
37 1.1 cgd /*
38 1.1 cgd * Pseudo-teletype Driver
39 1.1 cgd * (Actually two drivers, requiring two entries in 'cdevsw')
40 1.1 cgd */
41 1.1 cgd #include "pty.h"
42 1.1 cgd
43 1.1 cgd #if NPTY > 0
44 1.16 mycroft #include <sys/param.h>
45 1.16 mycroft #include <sys/systm.h>
46 1.16 mycroft #include <sys/ioctl.h>
47 1.16 mycroft #include <sys/select.h>
48 1.16 mycroft #include <sys/tty.h>
49 1.16 mycroft #include <sys/conf.h>
50 1.16 mycroft #include <sys/file.h>
51 1.16 mycroft #include <sys/proc.h>
52 1.16 mycroft #include <sys/uio.h>
53 1.16 mycroft #include <sys/kernel.h>
54 1.16 mycroft #include <sys/vnode.h>
55 1.1 cgd
56 1.1 cgd #if NPTY == 1
57 1.1 cgd #undef NPTY
58 1.1 cgd #define NPTY 32 /* crude XXX */
59 1.1 cgd #endif
60 1.1 cgd
61 1.1 cgd #define BUFSIZ 100 /* Chunk size iomoved to/from user */
62 1.1 cgd
63 1.1 cgd /*
64 1.1 cgd * pts == /dev/tty[pqrs]?
65 1.1 cgd * ptc == /dev/pty[pqrs]?
66 1.1 cgd */
67 1.5 deraadt struct tty *pt_tty[NPTY];
68 1.1 cgd struct pt_ioctl {
69 1.1 cgd int pt_flags;
70 1.4 cgd struct selinfo pt_selr, pt_selw;
71 1.1 cgd u_char pt_send;
72 1.1 cgd u_char pt_ucntl;
73 1.1 cgd } pt_ioctl[NPTY];
74 1.1 cgd int npty = NPTY; /* for pstat -t */
75 1.1 cgd
76 1.8 mycroft #define PF_COPEN 0x01 /* master open */
77 1.8 mycroft #define PF_SOPEN 0x02 /* slave open */
78 1.1 cgd #define PF_PKT 0x08 /* packet mode */
79 1.1 cgd #define PF_STOPPED 0x10 /* user told stopped */
80 1.1 cgd #define PF_REMOTE 0x20 /* remote and flow controlled input */
81 1.1 cgd #define PF_NOSTOP 0x40
82 1.1 cgd #define PF_UCNTL 0x80 /* user control mode */
83 1.1 cgd
84 1.7 andrew void ptcwakeup __P((struct tty *tp, int flag));
85 1.15 deraadt
86 1.15 deraadt void
87 1.15 deraadt ptyattach(n)
88 1.15 deraadt int n;
89 1.15 deraadt {
90 1.15 deraadt }
91 1.7 andrew
92 1.1 cgd /*ARGSUSED*/
93 1.7 andrew int
94 1.1 cgd ptsopen(dev, flag, devtype, p)
95 1.1 cgd dev_t dev;
96 1.7 andrew int flag, devtype;
97 1.1 cgd struct proc *p;
98 1.1 cgd {
99 1.1 cgd register struct tty *tp;
100 1.1 cgd int error;
101 1.1 cgd
102 1.1 cgd #ifdef lint
103 1.1 cgd npty = npty;
104 1.1 cgd #endif
105 1.1 cgd if (minor(dev) >= NPTY)
106 1.1 cgd return (ENXIO);
107 1.5 deraadt if(!pt_tty[minor(dev)]) {
108 1.8 mycroft tp = pt_tty[minor(dev)] = ttymalloc();
109 1.5 deraadt } else
110 1.5 deraadt tp = pt_tty[minor(dev)];
111 1.1 cgd if ((tp->t_state & TS_ISOPEN) == 0) {
112 1.1 cgd tp->t_state |= TS_WOPEN;
113 1.1 cgd ttychars(tp); /* Set up default chars */
114 1.1 cgd tp->t_iflag = TTYDEF_IFLAG;
115 1.1 cgd tp->t_oflag = TTYDEF_OFLAG;
116 1.1 cgd tp->t_lflag = TTYDEF_LFLAG;
117 1.1 cgd tp->t_cflag = TTYDEF_CFLAG;
118 1.1 cgd tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
119 1.1 cgd ttsetwater(tp); /* would be done in xxparam() */
120 1.1 cgd } else if (tp->t_state&TS_XCLUDE && p->p_ucred->cr_uid != 0)
121 1.1 cgd return (EBUSY);
122 1.1 cgd if (tp->t_oproc) /* Ctrlr still around. */
123 1.1 cgd tp->t_state |= TS_CARR_ON;
124 1.1 cgd while ((tp->t_state & TS_CARR_ON) == 0) {
125 1.1 cgd tp->t_state |= TS_WOPEN;
126 1.1 cgd if (flag&FNONBLOCK)
127 1.1 cgd break;
128 1.8 mycroft if (error = ttysleep(tp, (caddr_t)&tp->t_rawq, TTIPRI | PCATCH,
129 1.1 cgd ttopen, 0))
130 1.1 cgd return (error);
131 1.1 cgd }
132 1.8 mycroft if (error = (*linesw[tp->t_line].l_open)(dev, tp))
133 1.8 mycroft return (error);
134 1.8 mycroft pt_ioctl[minor(dev)].pt_flags |= PF_SOPEN;
135 1.1 cgd ptcwakeup(tp, FREAD|FWRITE);
136 1.8 mycroft return (0);
137 1.1 cgd }
138 1.1 cgd
139 1.7 andrew int
140 1.1 cgd ptsclose(dev, flag, mode, p)
141 1.1 cgd dev_t dev;
142 1.1 cgd int flag, mode;
143 1.1 cgd struct proc *p;
144 1.1 cgd {
145 1.1 cgd register struct tty *tp;
146 1.1 cgd
147 1.5 deraadt tp = pt_tty[minor(dev)];
148 1.1 cgd (*linesw[tp->t_line].l_close)(tp, flag);
149 1.1 cgd ttyclose(tp);
150 1.1 cgd ptcwakeup(tp, FREAD|FWRITE);
151 1.8 mycroft pt_ioctl[minor(dev)].pt_flags &= ~PF_SOPEN;
152 1.13 cgd #ifdef broken /* session holds a ref to the tty; can't deallocate */
153 1.8 mycroft if ((pt_ioctl[minor(dev)].pt_flags & PF_COPEN) == 0) {
154 1.8 mycroft ttyfree(tp);
155 1.8 mycroft pt_tty[minor(dev)] = (struct tty *)NULL;
156 1.8 mycroft }
157 1.13 cgd #endif
158 1.2 cgd return(0);
159 1.1 cgd }
160 1.1 cgd
161 1.7 andrew int
162 1.1 cgd ptsread(dev, uio, flag)
163 1.1 cgd dev_t dev;
164 1.1 cgd struct uio *uio;
165 1.7 andrew int flag;
166 1.1 cgd {
167 1.1 cgd struct proc *p = curproc;
168 1.5 deraadt register struct tty *tp = pt_tty[minor(dev)];
169 1.1 cgd register struct pt_ioctl *pti = &pt_ioctl[minor(dev)];
170 1.1 cgd int error = 0;
171 1.1 cgd
172 1.1 cgd again:
173 1.1 cgd if (pti->pt_flags & PF_REMOTE) {
174 1.1 cgd while (isbackground(p, tp)) {
175 1.1 cgd if ((p->p_sigignore & sigmask(SIGTTIN)) ||
176 1.1 cgd (p->p_sigmask & sigmask(SIGTTIN)) ||
177 1.1 cgd p->p_pgrp->pg_jobc == 0 ||
178 1.1 cgd p->p_flag&SPPWAIT)
179 1.1 cgd return (EIO);
180 1.1 cgd pgsignal(p->p_pgrp, SIGTTIN, 1);
181 1.1 cgd if (error = ttysleep(tp, (caddr_t)&lbolt,
182 1.1 cgd TTIPRI | PCATCH, ttybg, 0))
183 1.1 cgd return (error);
184 1.1 cgd }
185 1.8 mycroft if (tp->t_canq.c_cc == 0) {
186 1.1 cgd if (flag & IO_NDELAY)
187 1.1 cgd return (EWOULDBLOCK);
188 1.8 mycroft if (error = ttysleep(tp, (caddr_t)&tp->t_canq,
189 1.1 cgd TTIPRI | PCATCH, ttyin, 0))
190 1.1 cgd return (error);
191 1.1 cgd goto again;
192 1.1 cgd }
193 1.8 mycroft while (tp->t_canq.c_cc > 1 && uio->uio_resid > 0)
194 1.8 mycroft if (ureadc(getc(&tp->t_canq), uio) < 0) {
195 1.1 cgd error = EFAULT;
196 1.1 cgd break;
197 1.1 cgd }
198 1.8 mycroft if (tp->t_canq.c_cc == 1)
199 1.8 mycroft (void) getc(&tp->t_canq);
200 1.8 mycroft if (tp->t_canq.c_cc)
201 1.1 cgd return (error);
202 1.1 cgd } else
203 1.1 cgd if (tp->t_oproc)
204 1.1 cgd error = (*linesw[tp->t_line].l_read)(tp, uio, flag);
205 1.1 cgd ptcwakeup(tp, FWRITE);
206 1.1 cgd return (error);
207 1.1 cgd }
208 1.1 cgd
209 1.1 cgd /*
210 1.1 cgd * Write to pseudo-tty.
211 1.1 cgd * Wakeups of controlling tty will happen
212 1.1 cgd * indirectly, when tty driver calls ptsstart.
213 1.1 cgd */
214 1.7 andrew int
215 1.1 cgd ptswrite(dev, uio, flag)
216 1.1 cgd dev_t dev;
217 1.1 cgd struct uio *uio;
218 1.7 andrew int flag;
219 1.1 cgd {
220 1.1 cgd register struct tty *tp;
221 1.1 cgd
222 1.5 deraadt tp = pt_tty[minor(dev)];
223 1.1 cgd if (tp->t_oproc == 0)
224 1.1 cgd return (EIO);
225 1.1 cgd return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
226 1.1 cgd }
227 1.1 cgd
228 1.1 cgd /*
229 1.1 cgd * Start output on pseudo-tty.
230 1.1 cgd * Wake up process selecting or sleeping for input from controlling tty.
231 1.1 cgd */
232 1.12 deraadt void
233 1.1 cgd ptsstart(tp)
234 1.1 cgd struct tty *tp;
235 1.1 cgd {
236 1.1 cgd register struct pt_ioctl *pti = &pt_ioctl[minor(tp->t_dev)];
237 1.1 cgd
238 1.1 cgd if (tp->t_state & TS_TTSTOP)
239 1.12 deraadt return;
240 1.1 cgd if (pti->pt_flags & PF_STOPPED) {
241 1.1 cgd pti->pt_flags &= ~PF_STOPPED;
242 1.1 cgd pti->pt_send = TIOCPKT_START;
243 1.1 cgd }
244 1.1 cgd ptcwakeup(tp, FREAD);
245 1.12 deraadt return;
246 1.1 cgd }
247 1.1 cgd
248 1.7 andrew void
249 1.1 cgd ptcwakeup(tp, flag)
250 1.1 cgd struct tty *tp;
251 1.7 andrew int flag;
252 1.1 cgd {
253 1.1 cgd struct pt_ioctl *pti = &pt_ioctl[minor(tp->t_dev)];
254 1.1 cgd
255 1.1 cgd if (flag & FREAD) {
256 1.4 cgd selwakeup(&pti->pt_selr);
257 1.8 mycroft wakeup((caddr_t)&tp->t_outq.c_cl);
258 1.1 cgd }
259 1.1 cgd if (flag & FWRITE) {
260 1.4 cgd selwakeup(&pti->pt_selw);
261 1.8 mycroft wakeup((caddr_t)&tp->t_rawq.c_cf);
262 1.1 cgd }
263 1.1 cgd }
264 1.1 cgd
265 1.1 cgd /*ARGSUSED*/
266 1.1 cgd #ifdef __STDC__
267 1.7 andrew int
268 1.1 cgd ptcopen(dev_t dev, int flag, int devtype, struct proc *p)
269 1.1 cgd #else
270 1.7 andrew int
271 1.1 cgd ptcopen(dev, flag, devtype, p)
272 1.1 cgd dev_t dev;
273 1.1 cgd int flag, devtype;
274 1.1 cgd struct proc *p;
275 1.1 cgd #endif
276 1.1 cgd {
277 1.1 cgd register struct tty *tp;
278 1.1 cgd struct pt_ioctl *pti;
279 1.1 cgd
280 1.1 cgd if (minor(dev) >= NPTY)
281 1.1 cgd return (ENXIO);
282 1.5 deraadt if(!pt_tty[minor(dev)]) {
283 1.8 mycroft tp = pt_tty[minor(dev)] = ttymalloc();
284 1.5 deraadt } else
285 1.5 deraadt tp = pt_tty[minor(dev)];
286 1.1 cgd if (tp->t_oproc)
287 1.1 cgd return (EIO);
288 1.1 cgd tp->t_oproc = ptsstart;
289 1.1 cgd (void)(*linesw[tp->t_line].l_modem)(tp, 1);
290 1.1 cgd tp->t_lflag &= ~EXTPROC;
291 1.1 cgd pti = &pt_ioctl[minor(dev)];
292 1.8 mycroft pti->pt_flags &= PF_SOPEN;
293 1.8 mycroft pti->pt_flags |= PF_COPEN;
294 1.1 cgd pti->pt_send = 0;
295 1.1 cgd pti->pt_ucntl = 0;
296 1.1 cgd return (0);
297 1.1 cgd }
298 1.1 cgd
299 1.7 andrew int
300 1.1 cgd ptcclose(dev)
301 1.1 cgd dev_t dev;
302 1.1 cgd {
303 1.1 cgd register struct tty *tp;
304 1.1 cgd
305 1.5 deraadt tp = pt_tty[minor(dev)];
306 1.1 cgd (void)(*linesw[tp->t_line].l_modem)(tp, 0);
307 1.1 cgd tp->t_state &= ~TS_CARR_ON;
308 1.1 cgd tp->t_oproc = 0; /* mark closed */
309 1.2 cgd
310 1.8 mycroft pt_ioctl[minor(dev)].pt_flags &= ~PF_COPEN;
311 1.13 cgd #ifdef broken
312 1.8 mycroft if ((pt_ioctl[minor(dev)].pt_flags & PF_SOPEN) == 0) {
313 1.8 mycroft ttyfree(tp);
314 1.8 mycroft pt_tty[minor(dev)] = (struct tty *)NULL;
315 1.8 mycroft }
316 1.13 cgd #endif
317 1.2 cgd return (0);
318 1.1 cgd }
319 1.1 cgd
320 1.7 andrew int
321 1.1 cgd ptcread(dev, uio, flag)
322 1.1 cgd dev_t dev;
323 1.1 cgd struct uio *uio;
324 1.7 andrew int flag;
325 1.1 cgd {
326 1.5 deraadt register struct tty *tp = pt_tty[minor(dev)];
327 1.1 cgd struct pt_ioctl *pti = &pt_ioctl[minor(dev)];
328 1.8 mycroft u_char buf[BUFSIZ];
329 1.1 cgd int error = 0, cc;
330 1.1 cgd
331 1.1 cgd /*
332 1.1 cgd * We want to block until the slave
333 1.1 cgd * is open, and there's something to read;
334 1.1 cgd * but if we lost the slave or we're NBIO,
335 1.1 cgd * then return the appropriate error instead.
336 1.1 cgd */
337 1.1 cgd for (;;) {
338 1.1 cgd if (tp->t_state&TS_ISOPEN) {
339 1.1 cgd if (pti->pt_flags&PF_PKT && pti->pt_send) {
340 1.1 cgd error = ureadc((int)pti->pt_send, uio);
341 1.1 cgd if (error)
342 1.1 cgd return (error);
343 1.1 cgd if (pti->pt_send & TIOCPKT_IOCTL) {
344 1.1 cgd cc = MIN(uio->uio_resid,
345 1.1 cgd sizeof(tp->t_termios));
346 1.7 andrew uiomove((caddr_t)&tp->t_termios, cc,
347 1.7 andrew uio);
348 1.1 cgd }
349 1.1 cgd pti->pt_send = 0;
350 1.1 cgd return (0);
351 1.1 cgd }
352 1.1 cgd if (pti->pt_flags&PF_UCNTL && pti->pt_ucntl) {
353 1.1 cgd error = ureadc((int)pti->pt_ucntl, uio);
354 1.1 cgd if (error)
355 1.1 cgd return (error);
356 1.1 cgd pti->pt_ucntl = 0;
357 1.1 cgd return (0);
358 1.1 cgd }
359 1.8 mycroft if (tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0)
360 1.1 cgd break;
361 1.1 cgd }
362 1.1 cgd if ((tp->t_state&TS_CARR_ON) == 0)
363 1.1 cgd return (0); /* EOF */
364 1.1 cgd if (flag & IO_NDELAY)
365 1.1 cgd return (EWOULDBLOCK);
366 1.8 mycroft if (error = tsleep((caddr_t)&tp->t_outq.c_cl, TTIPRI | PCATCH,
367 1.1 cgd ttyin, 0))
368 1.1 cgd return (error);
369 1.1 cgd }
370 1.1 cgd if (pti->pt_flags & (PF_PKT|PF_UCNTL))
371 1.1 cgd error = ureadc(0, uio);
372 1.1 cgd while (uio->uio_resid > 0 && error == 0) {
373 1.1 cgd cc = q_to_b(&tp->t_outq, buf, MIN(uio->uio_resid, BUFSIZ));
374 1.1 cgd if (cc <= 0)
375 1.1 cgd break;
376 1.1 cgd error = uiomove(buf, cc, uio);
377 1.1 cgd }
378 1.8 mycroft if (tp->t_outq.c_cc <= tp->t_lowat) {
379 1.1 cgd if (tp->t_state&TS_ASLEEP) {
380 1.1 cgd tp->t_state &= ~TS_ASLEEP;
381 1.8 mycroft wakeup((caddr_t)&tp->t_outq);
382 1.1 cgd }
383 1.4 cgd selwakeup(&tp->t_wsel);
384 1.1 cgd }
385 1.1 cgd return (error);
386 1.1 cgd }
387 1.1 cgd
388 1.7 andrew void
389 1.1 cgd ptsstop(tp, flush)
390 1.1 cgd register struct tty *tp;
391 1.1 cgd int flush;
392 1.1 cgd {
393 1.1 cgd struct pt_ioctl *pti = &pt_ioctl[minor(tp->t_dev)];
394 1.1 cgd int flag;
395 1.1 cgd
396 1.1 cgd /* note: FLUSHREAD and FLUSHWRITE already ok */
397 1.1 cgd if (flush == 0) {
398 1.1 cgd flush = TIOCPKT_STOP;
399 1.1 cgd pti->pt_flags |= PF_STOPPED;
400 1.1 cgd } else
401 1.1 cgd pti->pt_flags &= ~PF_STOPPED;
402 1.1 cgd pti->pt_send |= flush;
403 1.1 cgd /* change of perspective */
404 1.1 cgd flag = 0;
405 1.1 cgd if (flush & FREAD)
406 1.1 cgd flag |= FWRITE;
407 1.1 cgd if (flush & FWRITE)
408 1.1 cgd flag |= FREAD;
409 1.1 cgd ptcwakeup(tp, flag);
410 1.1 cgd }
411 1.1 cgd
412 1.7 andrew int
413 1.1 cgd ptcselect(dev, rw, p)
414 1.1 cgd dev_t dev;
415 1.1 cgd int rw;
416 1.1 cgd struct proc *p;
417 1.1 cgd {
418 1.5 deraadt register struct tty *tp = pt_tty[minor(dev)];
419 1.1 cgd struct pt_ioctl *pti = &pt_ioctl[minor(dev)];
420 1.1 cgd int s;
421 1.1 cgd
422 1.1 cgd if ((tp->t_state&TS_CARR_ON) == 0)
423 1.1 cgd return (1);
424 1.1 cgd switch (rw) {
425 1.1 cgd
426 1.1 cgd case FREAD:
427 1.1 cgd /*
428 1.1 cgd * Need to block timeouts (ttrstart).
429 1.1 cgd */
430 1.1 cgd s = spltty();
431 1.1 cgd if ((tp->t_state&TS_ISOPEN) &&
432 1.8 mycroft tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0) {
433 1.1 cgd splx(s);
434 1.1 cgd return (1);
435 1.1 cgd }
436 1.1 cgd splx(s);
437 1.1 cgd /* FALLTHROUGH */
438 1.1 cgd
439 1.1 cgd case 0: /* exceptional */
440 1.1 cgd if ((tp->t_state&TS_ISOPEN) &&
441 1.1 cgd (pti->pt_flags&PF_PKT && pti->pt_send ||
442 1.1 cgd pti->pt_flags&PF_UCNTL && pti->pt_ucntl))
443 1.1 cgd return (1);
444 1.4 cgd selrecord(p, &pti->pt_selr);
445 1.1 cgd break;
446 1.1 cgd
447 1.1 cgd
448 1.1 cgd case FWRITE:
449 1.1 cgd if (tp->t_state&TS_ISOPEN) {
450 1.1 cgd if (pti->pt_flags & PF_REMOTE) {
451 1.8 mycroft if (tp->t_canq.c_cc == 0)
452 1.1 cgd return (1);
453 1.1 cgd } else {
454 1.8 mycroft if (tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG-2)
455 1.1 cgd return (1);
456 1.8 mycroft if (tp->t_canq.c_cc == 0 && (tp->t_iflag&ICANON))
457 1.1 cgd return (1);
458 1.1 cgd }
459 1.1 cgd }
460 1.4 cgd selrecord(p, &pti->pt_selw);
461 1.1 cgd break;
462 1.1 cgd
463 1.1 cgd }
464 1.1 cgd return (0);
465 1.1 cgd }
466 1.1 cgd
467 1.7 andrew int
468 1.1 cgd ptcwrite(dev, uio, flag)
469 1.1 cgd dev_t dev;
470 1.1 cgd register struct uio *uio;
471 1.7 andrew int flag;
472 1.1 cgd {
473 1.5 deraadt register struct tty *tp = pt_tty[minor(dev)];
474 1.1 cgd register u_char *cp;
475 1.1 cgd register int cc = 0;
476 1.1 cgd u_char locbuf[BUFSIZ];
477 1.1 cgd int cnt = 0;
478 1.1 cgd struct pt_ioctl *pti = &pt_ioctl[minor(dev)];
479 1.1 cgd int error = 0;
480 1.1 cgd
481 1.1 cgd again:
482 1.1 cgd if ((tp->t_state&TS_ISOPEN) == 0)
483 1.1 cgd goto block;
484 1.1 cgd if (pti->pt_flags & PF_REMOTE) {
485 1.8 mycroft if (tp->t_canq.c_cc)
486 1.1 cgd goto block;
487 1.8 mycroft while (uio->uio_resid > 0 && tp->t_canq.c_cc < TTYHOG - 1) {
488 1.1 cgd if (cc == 0) {
489 1.1 cgd cc = min(uio->uio_resid, BUFSIZ);
490 1.8 mycroft cc = min(cc, TTYHOG - 1 - tp->t_canq.c_cc);
491 1.1 cgd cp = locbuf;
492 1.1 cgd error = uiomove((caddr_t)cp, cc, uio);
493 1.1 cgd if (error)
494 1.1 cgd return (error);
495 1.1 cgd /* check again for safety */
496 1.1 cgd if ((tp->t_state&TS_ISOPEN) == 0)
497 1.1 cgd return (EIO);
498 1.1 cgd }
499 1.1 cgd if (cc)
500 1.8 mycroft (void) b_to_q(cp, cc, &tp->t_canq);
501 1.1 cgd cc = 0;
502 1.1 cgd }
503 1.8 mycroft (void) putc(0, &tp->t_canq);
504 1.1 cgd ttwakeup(tp);
505 1.8 mycroft wakeup((caddr_t)&tp->t_canq);
506 1.1 cgd return (0);
507 1.1 cgd }
508 1.1 cgd while (uio->uio_resid > 0) {
509 1.1 cgd if (cc == 0) {
510 1.1 cgd cc = min(uio->uio_resid, BUFSIZ);
511 1.1 cgd cp = locbuf;
512 1.1 cgd error = uiomove((caddr_t)cp, cc, uio);
513 1.1 cgd if (error)
514 1.1 cgd return (error);
515 1.1 cgd /* check again for safety */
516 1.1 cgd if ((tp->t_state&TS_ISOPEN) == 0)
517 1.1 cgd return (EIO);
518 1.1 cgd }
519 1.1 cgd while (cc > 0) {
520 1.8 mycroft if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 &&
521 1.8 mycroft (tp->t_canq.c_cc > 0 || !(tp->t_iflag&ICANON))) {
522 1.8 mycroft wakeup((caddr_t)&tp->t_rawq);
523 1.1 cgd goto block;
524 1.1 cgd }
525 1.1 cgd (*linesw[tp->t_line].l_rint)(*cp++, tp);
526 1.1 cgd cnt++;
527 1.1 cgd cc--;
528 1.1 cgd }
529 1.1 cgd cc = 0;
530 1.1 cgd }
531 1.1 cgd return (0);
532 1.1 cgd block:
533 1.1 cgd /*
534 1.1 cgd * Come here to wait for slave to open, for space
535 1.1 cgd * in outq, or space in rawq.
536 1.1 cgd */
537 1.1 cgd if ((tp->t_state&TS_CARR_ON) == 0)
538 1.1 cgd return (EIO);
539 1.1 cgd if (flag & IO_NDELAY) {
540 1.1 cgd /* adjust for data copied in but not written */
541 1.1 cgd uio->uio_resid += cc;
542 1.1 cgd if (cnt == 0)
543 1.1 cgd return (EWOULDBLOCK);
544 1.1 cgd return (0);
545 1.1 cgd }
546 1.8 mycroft if (error = tsleep((caddr_t)&tp->t_rawq.c_cf, TTOPRI | PCATCH,
547 1.1 cgd ttyout, 0)) {
548 1.1 cgd /* adjust for data copied in but not written */
549 1.1 cgd uio->uio_resid += cc;
550 1.1 cgd return (error);
551 1.1 cgd }
552 1.1 cgd goto again;
553 1.1 cgd }
554 1.1 cgd
555 1.1 cgd /*ARGSUSED*/
556 1.7 andrew int
557 1.18 mycroft ptyioctl(dev, cmd, data, flag, p)
558 1.18 mycroft dev_t dev;
559 1.18 mycroft int cmd;
560 1.1 cgd caddr_t data;
561 1.18 mycroft int flag;
562 1.18 mycroft struct proc *p;
563 1.1 cgd {
564 1.5 deraadt register struct tty *tp = pt_tty[minor(dev)];
565 1.1 cgd register struct pt_ioctl *pti = &pt_ioctl[minor(dev)];
566 1.1 cgd register u_char *cc = tp->t_cc;
567 1.1 cgd int stop, error;
568 1.1 cgd
569 1.1 cgd /*
570 1.1 cgd * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
571 1.1 cgd * ttywflush(tp) will hang if there are characters in the outq.
572 1.1 cgd */
573 1.1 cgd if (cmd == TIOCEXT) {
574 1.1 cgd /*
575 1.1 cgd * When the EXTPROC bit is being toggled, we need
576 1.1 cgd * to send an TIOCPKT_IOCTL if the packet driver
577 1.1 cgd * is turned on.
578 1.1 cgd */
579 1.1 cgd if (*(int *)data) {
580 1.1 cgd if (pti->pt_flags & PF_PKT) {
581 1.1 cgd pti->pt_send |= TIOCPKT_IOCTL;
582 1.1 cgd ptcwakeup(tp, FREAD);
583 1.1 cgd }
584 1.1 cgd tp->t_lflag |= EXTPROC;
585 1.1 cgd } else {
586 1.1 cgd if ((tp->t_state & EXTPROC) &&
587 1.1 cgd (pti->pt_flags & PF_PKT)) {
588 1.1 cgd pti->pt_send |= TIOCPKT_IOCTL;
589 1.1 cgd ptcwakeup(tp, FREAD);
590 1.1 cgd }
591 1.1 cgd tp->t_lflag &= ~EXTPROC;
592 1.1 cgd }
593 1.1 cgd return(0);
594 1.1 cgd } else
595 1.1 cgd if (cdevsw[major(dev)].d_open == ptcopen)
596 1.1 cgd switch (cmd) {
597 1.1 cgd
598 1.1 cgd case TIOCGPGRP:
599 1.1 cgd /*
600 1.1 cgd * We aviod calling ttioctl on the controller since,
601 1.1 cgd * in that case, tp must be the controlling terminal.
602 1.1 cgd */
603 1.1 cgd *(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
604 1.1 cgd return (0);
605 1.1 cgd
606 1.1 cgd case TIOCPKT:
607 1.1 cgd if (*(int *)data) {
608 1.1 cgd if (pti->pt_flags & PF_UCNTL)
609 1.1 cgd return (EINVAL);
610 1.1 cgd pti->pt_flags |= PF_PKT;
611 1.1 cgd } else
612 1.1 cgd pti->pt_flags &= ~PF_PKT;
613 1.1 cgd return (0);
614 1.1 cgd
615 1.1 cgd case TIOCUCNTL:
616 1.1 cgd if (*(int *)data) {
617 1.1 cgd if (pti->pt_flags & PF_PKT)
618 1.1 cgd return (EINVAL);
619 1.1 cgd pti->pt_flags |= PF_UCNTL;
620 1.1 cgd } else
621 1.1 cgd pti->pt_flags &= ~PF_UCNTL;
622 1.1 cgd return (0);
623 1.1 cgd
624 1.1 cgd case TIOCREMOTE:
625 1.1 cgd if (*(int *)data)
626 1.1 cgd pti->pt_flags |= PF_REMOTE;
627 1.1 cgd else
628 1.1 cgd pti->pt_flags &= ~PF_REMOTE;
629 1.1 cgd ttyflush(tp, FREAD|FWRITE);
630 1.1 cgd return (0);
631 1.1 cgd
632 1.2 cgd #ifdef COMPAT_43
633 1.2 cgd /* wkt */
634 1.1 cgd case TIOCSETP:
635 1.1 cgd case TIOCSETN:
636 1.2 cgd #endif
637 1.1 cgd case TIOCSETD:
638 1.1 cgd case TIOCSETA:
639 1.1 cgd case TIOCSETAW:
640 1.1 cgd case TIOCSETAF:
641 1.11 mycroft flushq(&tp->t_outq);
642 1.1 cgd break;
643 1.1 cgd
644 1.1 cgd case TIOCSIG:
645 1.1 cgd if (*(unsigned int *)data >= NSIG)
646 1.1 cgd return(EINVAL);
647 1.1 cgd if ((tp->t_lflag&NOFLSH) == 0)
648 1.1 cgd ttyflush(tp, FREAD|FWRITE);
649 1.1 cgd pgsignal(tp->t_pgrp, *(unsigned int *)data, 1);
650 1.1 cgd if ((*(unsigned int *)data == SIGINFO) &&
651 1.1 cgd ((tp->t_lflag&NOKERNINFO) == 0))
652 1.1 cgd ttyinfo(tp);
653 1.1 cgd return(0);
654 1.1 cgd }
655 1.18 mycroft error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
656 1.1 cgd if (error < 0)
657 1.18 mycroft error = ttioctl(tp, cmd, data, flag, p);
658 1.1 cgd if (error < 0) {
659 1.1 cgd if (pti->pt_flags & PF_UCNTL &&
660 1.1 cgd (cmd & ~0xff) == UIOCCMD(0)) {
661 1.1 cgd if (cmd & 0xff) {
662 1.1 cgd pti->pt_ucntl = (u_char)cmd;
663 1.1 cgd ptcwakeup(tp, FREAD);
664 1.1 cgd }
665 1.1 cgd return (0);
666 1.1 cgd }
667 1.1 cgd error = ENOTTY;
668 1.1 cgd }
669 1.1 cgd /*
670 1.1 cgd * If external processing and packet mode send ioctl packet.
671 1.1 cgd */
672 1.1 cgd if ((tp->t_lflag&EXTPROC) && (pti->pt_flags & PF_PKT)) {
673 1.1 cgd switch(cmd) {
674 1.1 cgd case TIOCSETA:
675 1.1 cgd case TIOCSETAW:
676 1.1 cgd case TIOCSETAF:
677 1.2 cgd #ifdef COMPAT_43
678 1.2 cgd /* wkt */
679 1.1 cgd case TIOCSETP:
680 1.1 cgd case TIOCSETN:
681 1.1 cgd case TIOCSETC:
682 1.1 cgd case TIOCSLTC:
683 1.1 cgd case TIOCLBIS:
684 1.1 cgd case TIOCLBIC:
685 1.1 cgd case TIOCLSET:
686 1.1 cgd #endif
687 1.1 cgd pti->pt_send |= TIOCPKT_IOCTL;
688 1.1 cgd default:
689 1.1 cgd break;
690 1.1 cgd }
691 1.1 cgd }
692 1.1 cgd stop = (tp->t_iflag & IXON) && CCEQ(cc[VSTOP], CTRL('s'))
693 1.1 cgd && CCEQ(cc[VSTART], CTRL('q'));
694 1.1 cgd if (pti->pt_flags & PF_NOSTOP) {
695 1.1 cgd if (stop) {
696 1.1 cgd pti->pt_send &= ~TIOCPKT_NOSTOP;
697 1.1 cgd pti->pt_send |= TIOCPKT_DOSTOP;
698 1.1 cgd pti->pt_flags &= ~PF_NOSTOP;
699 1.1 cgd ptcwakeup(tp, FREAD);
700 1.1 cgd }
701 1.1 cgd } else {
702 1.1 cgd if (!stop) {
703 1.1 cgd pti->pt_send &= ~TIOCPKT_DOSTOP;
704 1.1 cgd pti->pt_send |= TIOCPKT_NOSTOP;
705 1.1 cgd pti->pt_flags |= PF_NOSTOP;
706 1.1 cgd ptcwakeup(tp, FREAD);
707 1.1 cgd }
708 1.1 cgd }
709 1.1 cgd return (error);
710 1.1 cgd }
711 1.1 cgd #endif
712