tty_pty.c revision 1.56.2.6 1 /* $NetBSD: tty_pty.c,v 1.56.2.6 2002/09/24 10:45:23 jdolecek 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.56.2.6 2002/09/24 10:45:23 jdolecek 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/proc.h>
52 #include <sys/tty.h>
53 #include <sys/file.h>
54 #include <sys/uio.h>
55 #include <sys/kernel.h>
56 #include <sys/vnode.h>
57 #include <sys/signalvar.h>
58 #include <sys/uio.h>
59 #include <sys/conf.h>
60 #include <sys/poll.h>
61 #include <sys/malloc.h>
62
63 #define DEFAULT_NPTYS 16 /* default number of initial ptys */
64 #define DEFAULT_MAXPTYS 992 /* default maximum number of ptys */
65
66 /* Macros to clear/set/test flags. */
67 #define SET(t, f) (t) |= (f)
68 #define CLR(t, f) (t) &= ~((unsigned)(f))
69 #define ISSET(t, f) ((t) & (f))
70
71 #define BUFSIZ 100 /* Chunk size iomoved to/from user */
72
73 /*
74 * pts == /dev/tty[pqrs]?
75 * ptc == /dev/pty[pqrs]?
76 */
77 struct pt_softc {
78 struct tty *pt_tty;
79 int pt_flags;
80 struct selinfo pt_selr, pt_selw;
81 u_char pt_send;
82 u_char pt_ucntl;
83 };
84
85 static struct pt_softc **pt_softc = NULL; /* pty array */
86 static int npty = 0; /* for pstat -t */
87 static int maxptys = DEFAULT_MAXPTYS; /* maximum number of ptys (sysctable) */
88 static struct simplelock pt_softc_mutex = SIMPLELOCK_INITIALIZER;
89
90 #define PF_PKT 0x08 /* packet mode */
91 #define PF_STOPPED 0x10 /* user told stopped */
92 #define PF_REMOTE 0x20 /* remote and flow controlled input */
93 #define PF_NOSTOP 0x40
94 #define PF_UCNTL 0x80 /* user control mode */
95
96 void ptyattach __P((int));
97 void ptcwakeup __P((struct tty *, int));
98 int ptcopen __P((dev_t, int, int, struct proc *));
99 struct tty *ptytty __P((dev_t));
100 void ptsstart __P((struct tty *));
101 int pty_maxptys __P((int, int));
102
103 static struct pt_softc **ptyarralloc __P((int));
104 static int check_pty __P((dev_t));
105
106 /*
107 * Allocate and zero array of nelem elements.
108 */
109 static struct pt_softc **
110 ptyarralloc(nelem)
111 int nelem;
112 {
113 struct pt_softc **pt;
114 nelem += 10;
115 pt = malloc(nelem * sizeof(struct pt_softc *), M_DEVBUF, M_WAITOK);
116 memset(pt, '\0', nelem * sizeof(struct pt_softc *));
117 return pt;
118 }
119
120 /*
121 * Check if the minor is correct and ensure necessary structures
122 * are properly allocated.
123 */
124 static int
125 check_pty(dev)
126 dev_t dev;
127 {
128 struct pt_softc *pti;
129
130 if (minor(dev) >= npty) {
131 struct pt_softc **newpt;
132 int newnpty;
133
134 /* check if the requested pty can be granted */
135 if (minor(dev) >= maxptys) {
136 limit_reached:
137 tablefull("pty", "increase kern.maxptys");
138 return (ENXIO);
139 }
140
141 /*
142 * Now grab the pty array mutex - we need to ensure
143 * that the pty array is consistent while copying it's
144 * content to newly allocated, larger space; we also
145 * need to be safe against pty_maxptys().
146 */
147 simple_lock(&pt_softc_mutex);
148
149 do {
150 for(newnpty = npty; newnpty <= minor(dev);
151 newnpty *= 2);
152
153 if (newnpty > maxptys)
154 newnpty = maxptys;
155
156 simple_unlock(&pt_softc_mutex);
157 newpt = ptyarralloc(newnpty);
158 simple_lock(&pt_softc_mutex);
159
160 if (maxptys == npty) {
161 simple_unlock(&pt_softc_mutex);
162 goto limit_reached;
163 }
164 } while(newnpty > maxptys);
165
166 /*
167 * If the pty array was not enlarged while we were waiting
168 * for mutex, copy current contents of pt_softc[] to newly
169 * allocated array and start using the new bigger array.
170 */
171 if (minor(dev) >= npty) {
172 memcpy(newpt, pt_softc, npty*sizeof(struct pt_softc *));
173 free(pt_softc, M_DEVBUF);
174
175 pt_softc = newpt;
176 npty = newnpty;
177 } else {
178 /* was enlarged when waited fot lock, free new space */
179 free(newpt, M_DEVBUF);
180 }
181
182 simple_unlock(&pt_softc_mutex);
183 }
184
185 /*
186 * If the entry is not yet allocated, allocate one. The mutex is
187 * needed so that the state of pt_softc[] array is consistant
188 * in case it has been longened above.
189 */
190 if (!pt_softc[minor(dev)]) {
191 MALLOC(pti, struct pt_softc *, sizeof(struct pt_softc),
192 M_DEVBUF, M_WAITOK);
193 memset(pti, 0, sizeof(struct pt_softc));
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;
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 selnotify(&pti->pt_selr, 0);
463 wakeup((caddr_t)&tp->t_outq.c_cf);
464 }
465 if (flag & FWRITE) {
466 selnotify(&pti->pt_selw, 0);
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 selnotify(&tp->t_wsel, 0);
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 static void
717 filt_ptcrdetach(struct knote *kn)
718 {
719 struct pt_softc *pti;
720 int s;
721
722 pti = (void *) kn->kn_hook;
723 s = spltty();
724 SLIST_REMOVE(&pti->pt_selr.si_klist, kn, knote, kn_selnext);
725 splx(s);
726 }
727
728 static int
729 filt_ptcread(struct knote *kn, long hint)
730 {
731 struct pt_softc *pti;
732 struct tty *tp;
733 int canread;
734
735 pti = (void *) kn->kn_hook;
736 tp = pti->pt_tty;
737
738 canread = (ISSET(tp->t_state, TS_ISOPEN) &&
739 ((tp->t_outq.c_cc > 0 && !ISSET(tp->t_state, TS_TTSTOP)) ||
740 ((pti->pt_flags & PF_PKT) && pti->pt_send) ||
741 ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl)));
742
743 if (canread) {
744 /*
745 * XXXLUKEM - the below c_cc value is one more than
746 * the actual amount of data in tty queue - why?
747 */
748 kn->kn_data = tp->t_outq.c_cc;
749 if (((pti->pt_flags & PF_PKT) && pti->pt_send) ||
750 ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl))
751 kn->kn_data++;
752 }
753
754 return (canread);
755 }
756
757 static void
758 filt_ptcwdetach(struct knote *kn)
759 {
760 struct pt_softc *pti;
761 int s;
762
763 pti = (void *) kn->kn_hook;
764 s = spltty();
765 SLIST_REMOVE(&pti->pt_selw.si_klist, kn, knote, kn_selnext);
766 splx(s);
767 }
768
769 static int
770 filt_ptcwrite(struct knote *kn, long hint)
771 {
772 struct pt_softc *pti;
773 struct tty *tp;
774 int canwrite;
775 int nwrite;
776
777 pti = (void *) kn->kn_hook;
778 tp = pti->pt_tty;
779
780 canwrite = (ISSET(tp->t_state, TS_ISOPEN) &&
781 ((pti->pt_flags & PF_REMOTE) ?
782 (tp->t_canq.c_cc == 0) :
783 ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG-2) ||
784 (tp->t_canq.c_cc == 0 && ISSET(tp->t_lflag, ICANON)))));
785
786 if (canwrite) {
787 if (pti->pt_flags & PF_REMOTE)
788 nwrite = tp->t_canq.c_cn;
789 else {
790 /* this is guaranteed to be > 0 due to above check */
791 nwrite = tp->t_canq.c_cn
792 - (tp->t_rawq.c_cc + tp->t_canq.c_cc);
793 }
794 kn->kn_data = nwrite;
795 }
796
797 return (canwrite);
798 }
799
800 static const struct filterops ptcread_filtops =
801 { 1, NULL, filt_ptcrdetach, filt_ptcread };
802 static const struct filterops ptcwrite_filtops =
803 { 1, NULL, filt_ptcwdetach, filt_ptcwrite };
804
805 int
806 ptckqfilter(dev_t dev, struct knote *kn)
807 {
808 struct pt_softc *pti = pt_softc[minor(dev)];
809 struct klist *klist;
810 int s;
811
812 switch (kn->kn_filter) {
813 case EVFILT_READ:
814 klist = &pti->pt_selr.si_klist;
815 kn->kn_fop = &ptcread_filtops;
816 break;
817 case EVFILT_WRITE:
818 klist = &pti->pt_selw.si_klist;
819 kn->kn_fop = &ptcwrite_filtops;
820 break;
821 default:
822 return (1);
823 }
824
825 kn->kn_hook = (void *) pti;
826
827 s = spltty();
828 SLIST_INSERT_HEAD(klist, kn, kn_selnext);
829 splx(s);
830
831 return (0);
832 }
833
834 struct tty *
835 ptytty(dev)
836 dev_t dev;
837 {
838 struct pt_softc *pti = pt_softc[minor(dev)];
839 struct tty *tp = pti->pt_tty;
840
841 return (tp);
842 }
843
844 /*ARGSUSED*/
845 int
846 ptyioctl(dev, cmd, data, flag, p)
847 dev_t dev;
848 u_long cmd;
849 caddr_t data;
850 int flag;
851 struct proc *p;
852 {
853 struct pt_softc *pti = pt_softc[minor(dev)];
854 struct tty *tp = pti->pt_tty;
855 u_char *cc = tp->t_cc;
856 int stop, error, sig;
857
858 /*
859 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
860 * ttywflush(tp) will hang if there are characters in the outq.
861 */
862 if (cmd == TIOCEXT) {
863 /*
864 * When the EXTPROC bit is being toggled, we need
865 * to send an TIOCPKT_IOCTL if the packet driver
866 * is turned on.
867 */
868 if (*(int *)data) {
869 if (pti->pt_flags & PF_PKT) {
870 pti->pt_send |= TIOCPKT_IOCTL;
871 ptcwakeup(tp, FREAD);
872 }
873 SET(tp->t_lflag, EXTPROC);
874 } else {
875 if (ISSET(tp->t_lflag, EXTPROC) &&
876 (pti->pt_flags & PF_PKT)) {
877 pti->pt_send |= TIOCPKT_IOCTL;
878 ptcwakeup(tp, FREAD);
879 }
880 CLR(tp->t_lflag, EXTPROC);
881 }
882 return(0);
883 } else
884 if (cdevsw[major(dev)].d_open == ptcopen)
885 switch (cmd) {
886
887 case TIOCGPGRP:
888 /*
889 * We avoid calling ttioctl on the controller since,
890 * in that case, tp must be the controlling terminal.
891 */
892 *(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
893 return (0);
894
895 case TIOCPKT:
896 if (*(int *)data) {
897 if (pti->pt_flags & PF_UCNTL)
898 return (EINVAL);
899 pti->pt_flags |= PF_PKT;
900 } else
901 pti->pt_flags &= ~PF_PKT;
902 return (0);
903
904 case TIOCUCNTL:
905 if (*(int *)data) {
906 if (pti->pt_flags & PF_PKT)
907 return (EINVAL);
908 pti->pt_flags |= PF_UCNTL;
909 } else
910 pti->pt_flags &= ~PF_UCNTL;
911 return (0);
912
913 case TIOCREMOTE:
914 if (*(int *)data)
915 pti->pt_flags |= PF_REMOTE;
916 else
917 pti->pt_flags &= ~PF_REMOTE;
918 ttyflush(tp, FREAD|FWRITE);
919 return (0);
920
921 #ifdef COMPAT_OLDTTY
922 case TIOCSETP:
923 case TIOCSETN:
924 #endif
925 case TIOCSETD:
926 case TIOCSETA:
927 case TIOCSETAW:
928 case TIOCSETAF:
929 ndflush(&tp->t_outq, tp->t_outq.c_cc);
930 break;
931
932 case TIOCSIG:
933 sig = (int)(long)*(caddr_t *)data;
934 if (sig <= 0 || sig >= NSIG)
935 return (EINVAL);
936 if (!ISSET(tp->t_lflag, NOFLSH))
937 ttyflush(tp, FREAD|FWRITE);
938 if ((sig == SIGINFO) &&
939 (!ISSET(tp->t_lflag, NOKERNINFO)))
940 ttyinfo(tp);
941 pgsignal(tp->t_pgrp, sig, 1);
942 return(0);
943 }
944 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, p);
945 if (error == EPASSTHROUGH)
946 error = ttioctl(tp, cmd, data, flag, p);
947 if (error == EPASSTHROUGH) {
948 if (pti->pt_flags & PF_UCNTL &&
949 (cmd & ~0xff) == UIOCCMD(0)) {
950 if (cmd & 0xff) {
951 pti->pt_ucntl = (u_char)cmd;
952 ptcwakeup(tp, FREAD);
953 }
954 return (0);
955 }
956 }
957 /*
958 * If external processing and packet mode send ioctl packet.
959 */
960 if (ISSET(tp->t_lflag, EXTPROC) && (pti->pt_flags & PF_PKT)) {
961 switch(cmd) {
962 case TIOCSETA:
963 case TIOCSETAW:
964 case TIOCSETAF:
965 #ifdef COMPAT_OLDTTY
966 case TIOCSETP:
967 case TIOCSETN:
968 case TIOCSETC:
969 case TIOCSLTC:
970 case TIOCLBIS:
971 case TIOCLBIC:
972 case TIOCLSET:
973 #endif
974 pti->pt_send |= TIOCPKT_IOCTL;
975 ptcwakeup(tp, FREAD);
976 default:
977 break;
978 }
979 }
980 stop = ISSET(tp->t_iflag, IXON) && CCEQ(cc[VSTOP], CTRL('s'))
981 && CCEQ(cc[VSTART], CTRL('q'));
982 if (pti->pt_flags & PF_NOSTOP) {
983 if (stop) {
984 pti->pt_send &= ~TIOCPKT_NOSTOP;
985 pti->pt_send |= TIOCPKT_DOSTOP;
986 pti->pt_flags &= ~PF_NOSTOP;
987 ptcwakeup(tp, FREAD);
988 }
989 } else {
990 if (!stop) {
991 pti->pt_send &= ~TIOCPKT_DOSTOP;
992 pti->pt_send |= TIOCPKT_NOSTOP;
993 pti->pt_flags |= PF_NOSTOP;
994 ptcwakeup(tp, FREAD);
995 }
996 }
997 return (error);
998 }
999