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