tty_pty.c revision 1.55.2.5 1 /* $NetBSD: tty_pty.c,v 1.55.2.5 2002/04/01 07:48:00 nathanw Exp $ */
2
3 /*
4 * Copyright (c) 1982, 1986, 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * @(#)tty_pty.c 8.4 (Berkeley) 2/20/95
36 */
37
38 /*
39 * Pseudo-teletype Driver
40 * (Actually two drivers, requiring two entries in 'cdevsw')
41 */
42
43 #include <sys/cdefs.h>
44 __KERNEL_RCSID(0, "$NetBSD: tty_pty.c,v 1.55.2.5 2002/04/01 07:48:00 nathanw Exp $");
45
46 #include "opt_compat_sunos.h"
47
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/ioctl.h>
51 #include <sys/lwp.h>
52 #include <sys/proc.h>
53 #include <sys/tty.h>
54 #include <sys/file.h>
55 #include <sys/uio.h>
56 #include <sys/kernel.h>
57 #include <sys/vnode.h>
58 #include <sys/signalvar.h>
59 #include <sys/uio.h>
60 #include <sys/conf.h>
61 #include <sys/poll.h>
62 #include <sys/malloc.h>
63
64 #define DEFAULT_NPTYS 16 /* default number of initial ptys */
65 #define DEFAULT_MAXPTYS 992 /* default maximum number of ptys */
66
67 /* Macros to clear/set/test flags. */
68 #define SET(t, f) (t) |= (f)
69 #define CLR(t, f) (t) &= ~((unsigned)(f))
70 #define ISSET(t, f) ((t) & (f))
71
72 #define BUFSIZ 100 /* Chunk size iomoved to/from user */
73
74 /*
75 * pts == /dev/tty[pqrs]?
76 * ptc == /dev/pty[pqrs]?
77 */
78 struct pt_softc {
79 struct tty *pt_tty;
80 int pt_flags;
81 struct selinfo pt_selr, pt_selw;
82 u_char pt_send;
83 u_char pt_ucntl;
84 };
85
86 static struct pt_softc **pt_softc = NULL; /* pty array */
87 static int npty = 0; /* for pstat -t */
88 static int maxptys = DEFAULT_MAXPTYS; /* maximum number of ptys (sysctable) */
89 static struct simplelock pt_softc_mutex = SIMPLELOCK_INITIALIZER;
90
91 #define PF_PKT 0x08 /* packet mode */
92 #define PF_STOPPED 0x10 /* user told stopped */
93 #define PF_REMOTE 0x20 /* remote and flow controlled input */
94 #define PF_NOSTOP 0x40
95 #define PF_UCNTL 0x80 /* user control mode */
96
97 void ptyattach __P((int));
98 void ptcwakeup __P((struct tty *, int));
99 int ptcopen __P((dev_t, int, int, struct proc *));
100 struct tty *ptytty __P((dev_t));
101 void ptsstart __P((struct tty *));
102 int pty_maxptys __P((int, int));
103
104 static struct pt_softc **ptyarralloc __P((int));
105 static int check_pty __P((dev_t));
106
107 /*
108 * Allocate and zero array of nelem elements.
109 */
110 static struct pt_softc **
111 ptyarralloc(nelem)
112 int nelem;
113 {
114 struct pt_softc **pt;
115 nelem += 10;
116 pt = malloc(nelem * sizeof(struct pt_softc *), M_DEVBUF, M_WAITOK);
117 memset(pt, '\0', nelem * sizeof(struct pt_softc *));
118 return pt;
119 }
120
121 /*
122 * Check if the minor is correct and ensure necessary structures
123 * are properly allocated.
124 */
125 static int
126 check_pty(dev)
127 dev_t dev;
128 {
129 struct pt_softc *pti;
130
131 if (minor(dev) >= npty) {
132 struct pt_softc **newpt;
133 int newnpty;
134
135 /* check if the requested pty can be granted */
136 if (minor(dev) >= maxptys) {
137 limit_reached:
138 tablefull("pty", "increase kern.maxptys");
139 return (ENXIO);
140 }
141
142 /*
143 * Now grab the pty array mutex - we need to ensure
144 * that the pty array is consistent while copying it's
145 * content to newly allocated, larger space; we also
146 * need to be safe against pty_maxptys().
147 */
148 simple_lock(&pt_softc_mutex);
149
150 do {
151 for(newnpty = npty; newnpty <= minor(dev);
152 newnpty *= 2);
153
154 if (newnpty > maxptys)
155 newnpty = maxptys;
156
157 simple_unlock(&pt_softc_mutex);
158 newpt = ptyarralloc(newnpty);
159 simple_lock(&pt_softc_mutex);
160
161 if (maxptys == npty) {
162 simple_unlock(&pt_softc_mutex);
163 goto limit_reached;
164 }
165 } while(newnpty > maxptys);
166
167 /*
168 * If the pty array was not enlarged while we were waiting
169 * for mutex, copy current contents of pt_softc[] to newly
170 * allocated array and start using the new bigger array.
171 */
172 if (minor(dev) >= npty) {
173 memcpy(newpt, pt_softc, npty*sizeof(struct pt_softc *));
174 free(pt_softc, M_DEVBUF);
175
176 pt_softc = newpt;
177 npty = newnpty;
178 } else {
179 /* was enlarged when waited fot lock, free new space */
180 free(newpt, M_DEVBUF);
181 }
182
183 simple_unlock(&pt_softc_mutex);
184 }
185
186 /*
187 * If the entry is not yet allocated, allocate one. The mutex is
188 * needed so that the state of pt_softc[] array is consistant
189 * in case it has been longened above.
190 */
191 if (!pt_softc[minor(dev)]) {
192 MALLOC(pti, struct pt_softc *, sizeof(struct pt_softc),
193 M_DEVBUF, M_WAITOK);
194
195 pti->pt_tty = ttymalloc();
196
197 simple_lock(&pt_softc_mutex);
198
199 /*
200 * Check the entry again - it might have been
201 * added while we were waiting for mutex.
202 */
203 if (!pt_softc[minor(dev)]) {
204 tty_attach(pti->pt_tty);
205 pt_softc[minor(dev)] = pti;
206 } else {
207 ttyfree(pti->pt_tty);
208 FREE(pti, M_DEVBUF);
209 }
210
211 simple_unlock(&pt_softc_mutex);
212 }
213
214 return (0);
215 }
216
217 /*
218 * Set maxpty in thread-safe way. Returns 0 in case of error, otherwise
219 * new value of maxptys.
220 */
221 int
222 pty_maxptys(newmax, set)
223 int newmax, set;
224 {
225 if (!set)
226 return (maxptys);
227
228 /* the value cannot be set to value lower than current number of ptys */
229 if (newmax < npty)
230 return (0);
231
232 /* can proceed immediatelly if bigger than current maximum */
233 if (newmax > maxptys) {
234 maxptys = newmax;
235 return (maxptys);
236 }
237
238 /*
239 * We have to grab the pt_softc lock, so that we would pick correct
240 * value of npty (might be modified in check_pty()).
241 */
242 simple_lock(&pt_softc_mutex);
243
244 if (newmax > npty)
245 maxptys = newmax;
246
247 simple_unlock(&pt_softc_mutex);
248
249 return (maxptys);
250 }
251
252 /*
253 * Establish n (or default if n is 1) ptys in the system.
254 */
255 void
256 ptyattach(n)
257 int n;
258 {
259 /* maybe should allow 0 => none? */
260 if (n <= 1)
261 n = DEFAULT_NPTYS;
262 pt_softc = ptyarralloc(n);
263 npty = n;
264 }
265
266 /*ARGSUSED*/
267 int
268 ptsopen(dev, flag, devtype, p)
269 dev_t dev;
270 int flag, devtype;
271 struct proc *p;
272 {
273 struct pt_softc *pti;
274 struct tty *tp;
275 int error;
276
277 if ((error = check_pty(dev)))
278 return (error);
279
280 pti = pt_softc[minor(dev)];
281 tp = pti->pt_tty;
282
283 if (!ISSET(tp->t_state, TS_ISOPEN)) {
284 ttychars(tp); /* Set up default chars */
285 tp->t_iflag = TTYDEF_IFLAG;
286 tp->t_oflag = TTYDEF_OFLAG;
287 tp->t_lflag = TTYDEF_LFLAG;
288 tp->t_cflag = TTYDEF_CFLAG;
289 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
290 ttsetwater(tp); /* would be done in xxparam() */
291 } else if (ISSET(tp->t_state, TS_XCLUDE) && p->p_ucred->cr_uid != 0)
292 return (EBUSY);
293 if (tp->t_oproc) /* Ctrlr still around. */
294 SET(tp->t_state, TS_CARR_ON);
295 if (!ISSET(flag, O_NONBLOCK))
296 while (!ISSET(tp->t_state, TS_CARR_ON)) {
297 tp->t_wopen++;
298 error = ttysleep(tp, &tp->t_rawq, TTIPRI | PCATCH,
299 ttopen, 0);
300 tp->t_wopen--;
301 if (error)
302 return (error);
303 }
304 error = (*tp->t_linesw->l_open)(dev, tp);
305 ptcwakeup(tp, FREAD|FWRITE);
306 return (error);
307 }
308
309 int
310 ptsclose(dev, flag, mode, p)
311 dev_t dev;
312 int flag, mode;
313 struct proc *p;
314 {
315 struct pt_softc *pti = pt_softc[minor(dev)];
316 struct tty *tp = pti->pt_tty;
317 int error;
318
319 error = (*tp->t_linesw->l_close)(tp, flag);
320 error |= ttyclose(tp);
321 ptcwakeup(tp, FREAD|FWRITE);
322 return (error);
323 }
324
325 int
326 ptsread(dev, uio, flag)
327 dev_t dev;
328 struct uio *uio;
329 int flag;
330 {
331 struct proc *p = curproc->l_proc;
332 struct pt_softc *pti = pt_softc[minor(dev)];
333 struct tty *tp = pti->pt_tty;
334 int error = 0;
335
336 again:
337 if (pti->pt_flags & PF_REMOTE) {
338 while (isbackground(p, tp)) {
339 if (sigismasked(p, SIGTTIN) ||
340 p->p_pgrp->pg_jobc == 0 ||
341 p->p_flag & P_PPWAIT)
342 return (EIO);
343 pgsignal(p->p_pgrp, SIGTTIN, 1);
344 error = ttysleep(tp, (caddr_t)&lbolt,
345 TTIPRI | PCATCH, ttybg, 0);
346 if (error)
347 return (error);
348 }
349 if (tp->t_canq.c_cc == 0) {
350 if (flag & IO_NDELAY)
351 return (EWOULDBLOCK);
352 error = ttysleep(tp, (caddr_t)&tp->t_canq,
353 TTIPRI | PCATCH, ttyin, 0);
354 if (error)
355 return (error);
356 goto again;
357 }
358 while (tp->t_canq.c_cc > 1 && uio->uio_resid > 0)
359 if (ureadc(getc(&tp->t_canq), uio) < 0) {
360 error = EFAULT;
361 break;
362 }
363 if (tp->t_canq.c_cc == 1)
364 (void) getc(&tp->t_canq);
365 if (tp->t_canq.c_cc)
366 return (error);
367 } else
368 if (tp->t_oproc)
369 error = (*tp->t_linesw->l_read)(tp, uio, flag);
370 ptcwakeup(tp, FWRITE);
371 return (error);
372 }
373
374 /*
375 * Write to pseudo-tty.
376 * Wakeups of controlling tty will happen
377 * indirectly, when tty driver calls ptsstart.
378 */
379 int
380 ptswrite(dev, uio, flag)
381 dev_t dev;
382 struct uio *uio;
383 int flag;
384 {
385 struct pt_softc *pti = pt_softc[minor(dev)];
386 struct tty *tp = pti->pt_tty;
387
388 if (tp->t_oproc == 0)
389 return (EIO);
390 return ((*tp->t_linesw->l_write)(tp, uio, flag));
391 }
392
393 /*
394 * Poll pseudo-tty.
395 */
396 int
397 ptspoll(dev, events, p)
398 dev_t dev;
399 int events;
400 struct proc *p;
401 {
402 struct pt_softc *pti = pt_softc[minor(dev)];
403 struct tty *tp = pti->pt_tty;
404
405 if (tp->t_oproc == 0)
406 return (EIO);
407
408 return ((*tp->t_linesw->l_poll)(tp, events, p));
409 }
410
411 /*
412 * Start output on pseudo-tty.
413 * Wake up process polling or sleeping for input from controlling tty.
414 */
415 void
416 ptsstart(tp)
417 struct tty *tp;
418 {
419 struct pt_softc *pti = pt_softc[minor(tp->t_dev)];
420
421 if (ISSET(tp->t_state, TS_TTSTOP))
422 return;
423 if (pti->pt_flags & PF_STOPPED) {
424 pti->pt_flags &= ~PF_STOPPED;
425 pti->pt_send = TIOCPKT_START;
426 }
427 ptcwakeup(tp, FREAD);
428 }
429
430 void
431 ptsstop(tp, flush)
432 struct tty *tp;
433 int flush;
434 {
435 struct pt_softc *pti = pt_softc[minor(tp->t_dev)];
436 int flag;
437
438 /* note: FLUSHREAD and FLUSHWRITE already ok */
439 if (flush == 0) {
440 flush = TIOCPKT_STOP;
441 pti->pt_flags |= PF_STOPPED;
442 } else
443 pti->pt_flags &= ~PF_STOPPED;
444 pti->pt_send |= flush;
445 /* change of perspective */
446 flag = 0;
447 if (flush & FREAD)
448 flag |= FWRITE;
449 if (flush & FWRITE)
450 flag |= FREAD;
451 ptcwakeup(tp, flag);
452 }
453
454 void
455 ptcwakeup(tp, flag)
456 struct tty *tp;
457 int flag;
458 {
459 struct pt_softc *pti = pt_softc[minor(tp->t_dev)];
460
461 if (flag & FREAD) {
462 selwakeup(&pti->pt_selr);
463 wakeup((caddr_t)&tp->t_outq.c_cf);
464 }
465 if (flag & FWRITE) {
466 selwakeup(&pti->pt_selw);
467 wakeup((caddr_t)&tp->t_rawq.c_cf);
468 }
469 }
470
471 /*ARGSUSED*/
472 int
473 ptcopen(dev, flag, devtype, p)
474 dev_t dev;
475 int flag, devtype;
476 struct proc *p;
477 {
478 struct pt_softc *pti;
479 struct tty *tp;
480 int error;
481
482 if ((error = check_pty(dev)))
483 return (error);
484
485 pti = pt_softc[minor(dev)];
486 tp = pti->pt_tty;
487
488 if (tp->t_oproc)
489 return (EIO);
490 tp->t_oproc = ptsstart;
491 (void)(*tp->t_linesw->l_modem)(tp, 1);
492 CLR(tp->t_lflag, EXTPROC);
493 pti->pt_flags = 0;
494 pti->pt_send = 0;
495 pti->pt_ucntl = 0;
496 return (0);
497 }
498
499 /*ARGSUSED*/
500 int
501 ptcclose(dev, flag, devtype, p)
502 dev_t dev;
503 int flag, devtype;
504 struct proc *p;
505 {
506 struct pt_softc *pti = pt_softc[minor(dev)];
507 struct tty *tp = pti->pt_tty;
508
509 (void)(*tp->t_linesw->l_modem)(tp, 0);
510 CLR(tp->t_state, TS_CARR_ON);
511 tp->t_oproc = 0; /* mark closed */
512 return (0);
513 }
514
515 int
516 ptcread(dev, uio, flag)
517 dev_t dev;
518 struct uio *uio;
519 int flag;
520 {
521 struct pt_softc *pti = pt_softc[minor(dev)];
522 struct tty *tp = pti->pt_tty;
523 char buf[BUFSIZ];
524 int error = 0, cc;
525
526 /*
527 * We want to block until the slave
528 * is open, and there's something to read;
529 * but if we lost the slave or we're NBIO,
530 * then return the appropriate error instead.
531 */
532 for (;;) {
533 if (ISSET(tp->t_state, TS_ISOPEN)) {
534 if (pti->pt_flags&PF_PKT && pti->pt_send) {
535 error = ureadc((int)pti->pt_send, uio);
536 if (error)
537 return (error);
538 if (pti->pt_send & TIOCPKT_IOCTL) {
539 cc = min(uio->uio_resid,
540 sizeof(tp->t_termios));
541 uiomove((caddr_t) &tp->t_termios,
542 cc, uio);
543 }
544 pti->pt_send = 0;
545 return (0);
546 }
547 if (pti->pt_flags&PF_UCNTL && pti->pt_ucntl) {
548 error = ureadc((int)pti->pt_ucntl, uio);
549 if (error)
550 return (error);
551 pti->pt_ucntl = 0;
552 return (0);
553 }
554 if (tp->t_outq.c_cc && !ISSET(tp->t_state, TS_TTSTOP))
555 break;
556 }
557 if (!ISSET(tp->t_state, TS_CARR_ON))
558 return (0); /* EOF */
559 if (flag & IO_NDELAY)
560 return (EWOULDBLOCK);
561 error = tsleep((caddr_t)&tp->t_outq.c_cf, TTIPRI | PCATCH,
562 ttyin, 0);
563 if (error)
564 return (error);
565 }
566 if (pti->pt_flags & (PF_PKT|PF_UCNTL))
567 error = ureadc(0, uio);
568 while (uio->uio_resid > 0 && error == 0) {
569 cc = q_to_b(&tp->t_outq, buf, min(uio->uio_resid, BUFSIZ));
570 if (cc <= 0)
571 break;
572 error = uiomove(buf, cc, uio);
573 }
574 if (tp->t_outq.c_cc <= tp->t_lowat) {
575 if (ISSET(tp->t_state, TS_ASLEEP)) {
576 CLR(tp->t_state, TS_ASLEEP);
577 wakeup((caddr_t)&tp->t_outq);
578 }
579 selwakeup(&tp->t_wsel);
580 }
581 return (error);
582 }
583
584
585 int
586 ptcwrite(dev, uio, flag)
587 dev_t dev;
588 struct uio *uio;
589 int flag;
590 {
591 struct pt_softc *pti = pt_softc[minor(dev)];
592 struct tty *tp = pti->pt_tty;
593 u_char *cp = NULL;
594 int cc = 0;
595 u_char locbuf[BUFSIZ];
596 int cnt = 0;
597 int error = 0;
598
599 again:
600 if (!ISSET(tp->t_state, TS_ISOPEN))
601 goto block;
602 if (pti->pt_flags & PF_REMOTE) {
603 if (tp->t_canq.c_cc)
604 goto block;
605 while (uio->uio_resid > 0 && tp->t_canq.c_cc < TTYHOG - 1) {
606 if (cc == 0) {
607 cc = min(uio->uio_resid, BUFSIZ);
608 cc = min(cc, TTYHOG - 1 - tp->t_canq.c_cc);
609 cp = locbuf;
610 error = uiomove((caddr_t)cp, cc, uio);
611 if (error)
612 return (error);
613 /* check again for safety */
614 if (!ISSET(tp->t_state, TS_ISOPEN))
615 return (EIO);
616 }
617 if (cc)
618 (void) b_to_q((char *)cp, cc, &tp->t_canq);
619 cc = 0;
620 }
621 (void) putc(0, &tp->t_canq);
622 ttwakeup(tp);
623 wakeup((caddr_t)&tp->t_canq);
624 return (0);
625 }
626 while (uio->uio_resid > 0) {
627 if (cc == 0) {
628 cc = min(uio->uio_resid, BUFSIZ);
629 cp = locbuf;
630 error = uiomove((caddr_t)cp, cc, uio);
631 if (error)
632 return (error);
633 /* check again for safety */
634 if (!ISSET(tp->t_state, TS_ISOPEN))
635 return (EIO);
636 }
637 while (cc > 0) {
638 if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 &&
639 (tp->t_canq.c_cc > 0 || !ISSET(tp->t_lflag, ICANON))) {
640 wakeup((caddr_t)&tp->t_rawq);
641 goto block;
642 }
643 (*tp->t_linesw->l_rint)(*cp++, tp);
644 cnt++;
645 cc--;
646 }
647 cc = 0;
648 }
649 return (0);
650 block:
651 /*
652 * Come here to wait for slave to open, for space
653 * in outq, or space in rawq.
654 */
655 if (!ISSET(tp->t_state, TS_CARR_ON))
656 return (EIO);
657 if (flag & IO_NDELAY) {
658 /* adjust for data copied in but not written */
659 uio->uio_resid += cc;
660 if (cnt == 0)
661 return (EWOULDBLOCK);
662 return (0);
663 }
664 error = tsleep((caddr_t)&tp->t_rawq.c_cf, TTOPRI | PCATCH,
665 ttyout, 0);
666 if (error) {
667 /* adjust for data copied in but not written */
668 uio->uio_resid += cc;
669 return (error);
670 }
671 goto again;
672 }
673
674 int
675 ptcpoll(dev, events, p)
676 dev_t dev;
677 int events;
678 struct proc *p;
679 {
680 struct pt_softc *pti = pt_softc[minor(dev)];
681 struct tty *tp = pti->pt_tty;
682 int revents = 0;
683 int s = splsoftclock();
684
685 if (events & (POLLIN | POLLRDNORM))
686 if (ISSET(tp->t_state, TS_ISOPEN) &&
687 ((tp->t_outq.c_cc > 0 && !ISSET(tp->t_state, TS_TTSTOP)) ||
688 ((pti->pt_flags & PF_PKT) && pti->pt_send) ||
689 ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl)))
690 revents |= events & (POLLIN | POLLRDNORM);
691
692 if (events & (POLLOUT | POLLWRNORM))
693 if (ISSET(tp->t_state, TS_ISOPEN) &&
694 ((pti->pt_flags & PF_REMOTE) ?
695 (tp->t_canq.c_cc == 0) :
696 ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG-2) ||
697 (tp->t_canq.c_cc == 0 && ISSET(tp->t_lflag, ICANON)))))
698 revents |= events & (POLLOUT | POLLWRNORM);
699
700 if (events & POLLHUP)
701 if (!ISSET(tp->t_state, TS_CARR_ON))
702 revents |= POLLHUP;
703
704 if (revents == 0) {
705 if (events & (POLLIN | POLLHUP | POLLRDNORM))
706 selrecord(p, &pti->pt_selr);
707
708 if (events & (POLLOUT | POLLWRNORM))
709 selrecord(p, &pti->pt_selw);
710 }
711
712 splx(s);
713 return (revents);
714 }
715
716
717 struct tty *
718 ptytty(dev)
719 dev_t dev;
720 {
721 struct pt_softc *pti = pt_softc[minor(dev)];
722 struct tty *tp = pti->pt_tty;
723
724 return (tp);
725 }
726
727 /*ARGSUSED*/
728 int
729 ptyioctl(dev, cmd, data, flag, p)
730 dev_t dev;
731 u_long cmd;
732 caddr_t data;
733 int flag;
734 struct proc *p;
735 {
736 struct pt_softc *pti = pt_softc[minor(dev)];
737 struct tty *tp = pti->pt_tty;
738 u_char *cc = tp->t_cc;
739 int stop, error, sig;
740
741 /*
742 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
743 * ttywflush(tp) will hang if there are characters in the outq.
744 */
745 if (cmd == TIOCEXT) {
746 /*
747 * When the EXTPROC bit is being toggled, we need
748 * to send an TIOCPKT_IOCTL if the packet driver
749 * is turned on.
750 */
751 if (*(int *)data) {
752 if (pti->pt_flags & PF_PKT) {
753 pti->pt_send |= TIOCPKT_IOCTL;
754 ptcwakeup(tp, FREAD);
755 }
756 SET(tp->t_lflag, EXTPROC);
757 } else {
758 if (ISSET(tp->t_lflag, EXTPROC) &&
759 (pti->pt_flags & PF_PKT)) {
760 pti->pt_send |= TIOCPKT_IOCTL;
761 ptcwakeup(tp, FREAD);
762 }
763 CLR(tp->t_lflag, EXTPROC);
764 }
765 return(0);
766 } else
767 if (cdevsw[major(dev)].d_open == ptcopen)
768 switch (cmd) {
769
770 case TIOCGPGRP:
771 /*
772 * We avoid calling ttioctl on the controller since,
773 * in that case, tp must be the controlling terminal.
774 */
775 *(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
776 return (0);
777
778 case TIOCPKT:
779 if (*(int *)data) {
780 if (pti->pt_flags & PF_UCNTL)
781 return (EINVAL);
782 pti->pt_flags |= PF_PKT;
783 } else
784 pti->pt_flags &= ~PF_PKT;
785 return (0);
786
787 case TIOCUCNTL:
788 if (*(int *)data) {
789 if (pti->pt_flags & PF_PKT)
790 return (EINVAL);
791 pti->pt_flags |= PF_UCNTL;
792 } else
793 pti->pt_flags &= ~PF_UCNTL;
794 return (0);
795
796 case TIOCREMOTE:
797 if (*(int *)data)
798 pti->pt_flags |= PF_REMOTE;
799 else
800 pti->pt_flags &= ~PF_REMOTE;
801 ttyflush(tp, FREAD|FWRITE);
802 return (0);
803
804 #ifdef COMPAT_OLDTTY
805 case TIOCSETP:
806 case TIOCSETN:
807 #endif
808 case TIOCSETD:
809 case TIOCSETA:
810 case TIOCSETAW:
811 case TIOCSETAF:
812 ndflush(&tp->t_outq, tp->t_outq.c_cc);
813 break;
814
815 case TIOCSIG:
816 sig = (int)(long)*(caddr_t *)data;
817 if (sig <= 0 || sig >= NSIG)
818 return (EINVAL);
819 if (!ISSET(tp->t_lflag, NOFLSH))
820 ttyflush(tp, FREAD|FWRITE);
821 if ((sig == SIGINFO) &&
822 (!ISSET(tp->t_lflag, NOKERNINFO)))
823 ttyinfo(tp);
824 pgsignal(tp->t_pgrp, sig, 1);
825 return(0);
826 }
827 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, p);
828 if (error == EPASSTHROUGH)
829 error = ttioctl(tp, cmd, data, flag, p);
830 if (error == EPASSTHROUGH) {
831 if (pti->pt_flags & PF_UCNTL &&
832 (cmd & ~0xff) == UIOCCMD(0)) {
833 if (cmd & 0xff) {
834 pti->pt_ucntl = (u_char)cmd;
835 ptcwakeup(tp, FREAD);
836 }
837 return (0);
838 }
839 }
840 /*
841 * If external processing and packet mode send ioctl packet.
842 */
843 if (ISSET(tp->t_lflag, EXTPROC) && (pti->pt_flags & PF_PKT)) {
844 switch(cmd) {
845 case TIOCSETA:
846 case TIOCSETAW:
847 case TIOCSETAF:
848 #ifdef COMPAT_OLDTTY
849 case TIOCSETP:
850 case TIOCSETN:
851 case TIOCSETC:
852 case TIOCSLTC:
853 case TIOCLBIS:
854 case TIOCLBIC:
855 case TIOCLSET:
856 #endif
857 pti->pt_send |= TIOCPKT_IOCTL;
858 ptcwakeup(tp, FREAD);
859 default:
860 break;
861 }
862 }
863 stop = ISSET(tp->t_iflag, IXON) && CCEQ(cc[VSTOP], CTRL('s'))
864 && CCEQ(cc[VSTART], CTRL('q'));
865 if (pti->pt_flags & PF_NOSTOP) {
866 if (stop) {
867 pti->pt_send &= ~TIOCPKT_NOSTOP;
868 pti->pt_send |= TIOCPKT_DOSTOP;
869 pti->pt_flags &= ~PF_NOSTOP;
870 ptcwakeup(tp, FREAD);
871 }
872 } else {
873 if (!stop) {
874 pti->pt_send &= ~TIOCPKT_DOSTOP;
875 pti->pt_send |= TIOCPKT_NOSTOP;
876 pti->pt_flags |= PF_NOSTOP;
877 ptcwakeup(tp, FREAD);
878 }
879 }
880 return (error);
881 }
882