tty_pty.c revision 1.56.4.3 1 /* $NetBSD: tty_pty.c,v 1.56.4.3 2001/10/13 17:42:52 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 = pt_softc[minor(tp->t_dev)];
434
435
436 if (ISSET(tp->t_state, TS_TTSTOP))
437 return;
438 if (pti->pt_flags & PF_STOPPED) {
439 pti->pt_flags &= ~PF_STOPPED;
440 pti->pt_send = TIOCPKT_START;
441 }
442 ptcwakeup(tp, FREAD);
443 }
444
445 void
446 ptsstop(tp, flush)
447 struct tty *tp;
448 int flush;
449 {
450 struct pt_softc *pti;
451 int flag;
452
453 pti = pt_softc[minor(tp->t_dev)];
454
455 /* note: FLUSHREAD and FLUSHWRITE already ok */
456 if (flush == 0) {
457 flush = TIOCPKT_STOP;
458 pti->pt_flags |= PF_STOPPED;
459 } else
460 pti->pt_flags &= ~PF_STOPPED;
461 pti->pt_send |= flush;
462 /* change of perspective */
463 flag = 0;
464 if (flush & FREAD)
465 flag |= FWRITE;
466 if (flush & FWRITE)
467 flag |= FREAD;
468 ptcwakeup(tp, flag);
469 }
470
471 void
472 ptcwakeup(tp, flag)
473 struct tty *tp;
474 int flag;
475 {
476 struct pt_softc *pti;
477
478 pti = pt_softc[minor(tp->t_dev)];
479
480 if (flag & FREAD) {
481 selwakeup(&pti->pt_selr);
482 wakeup((caddr_t)&tp->t_outq.c_cf);
483 }
484 if (flag & FWRITE) {
485 selwakeup(&pti->pt_selw);
486 wakeup((caddr_t)&tp->t_rawq.c_cf);
487 }
488 }
489
490 /*ARGSUSED*/
491 int
492 ptcopen(devvp, flag, devtype, p)
493 struct vnode *devvp;
494 int flag, devtype;
495 struct proc *p;
496 {
497 struct pt_softc *pti;
498 struct tty *tp;
499 int error;
500 dev_t rdev;
501
502 rdev = vdev_rdev(devvp);
503
504 if ((error = check_pty(rdev)))
505 return (error);
506
507 pti = pt_softc[minor(rdev)];
508 tp = pti->pt_tty;
509 vdev_setprivdata(devvp, pti);
510
511 if (tp->t_oproc)
512 return (EIO);
513 tp->t_oproc = ptsstart;
514 (void)(*tp->t_linesw->l_modem)(tp, 1);
515 CLR(tp->t_lflag, EXTPROC);
516 pti->pt_flags = 0;
517 pti->pt_send = 0;
518 pti->pt_ucntl = 0;
519 return (0);
520 }
521
522 /*ARGSUSED*/
523 int
524 ptcclose(devvp, flag, devtype, p)
525 struct vnode *devvp;
526 int flag, devtype;
527 struct proc *p;
528 {
529 struct pt_softc *pti;
530 struct tty *tp;
531
532 pti = vdev_privdata(devvp);
533 tp = pti->pt_tty;
534
535 (void)(*tp->t_linesw->l_modem)(tp, 0);
536 CLR(tp->t_state, TS_CARR_ON);
537 tp->t_oproc = 0; /* mark closed */
538 return (0);
539 }
540
541 int
542 ptcread(devvp, uio, flag)
543 struct vnode *devvp;
544 struct uio *uio;
545 int flag;
546 {
547 struct pt_softc *pti;
548 struct tty *tp;
549 char buf[BUFSIZ];
550 int error = 0, cc;
551
552 pti = vdev_privdata(devvp);
553 tp = pti->pt_tty;
554
555 /*
556 * We want to block until the slave
557 * is open, and there's something to read;
558 * but if we lost the slave or we're NBIO,
559 * then return the appropriate error instead.
560 */
561 for (;;) {
562 if (ISSET(tp->t_state, TS_ISOPEN)) {
563 if (pti->pt_flags&PF_PKT && pti->pt_send) {
564 error = ureadc((int)pti->pt_send, uio);
565 if (error)
566 return (error);
567 if (pti->pt_send & TIOCPKT_IOCTL) {
568 cc = min(uio->uio_resid,
569 sizeof(tp->t_termios));
570 uiomove((caddr_t) &tp->t_termios,
571 cc, uio);
572 }
573 pti->pt_send = 0;
574 return (0);
575 }
576 if (pti->pt_flags&PF_UCNTL && pti->pt_ucntl) {
577 error = ureadc((int)pti->pt_ucntl, uio);
578 if (error)
579 return (error);
580 pti->pt_ucntl = 0;
581 return (0);
582 }
583 if (tp->t_outq.c_cc && !ISSET(tp->t_state, TS_TTSTOP))
584 break;
585 }
586 if (!ISSET(tp->t_state, TS_CARR_ON))
587 return (0); /* EOF */
588 if (flag & IO_NDELAY)
589 return (EWOULDBLOCK);
590 error = tsleep((caddr_t)&tp->t_outq.c_cf, TTIPRI | PCATCH,
591 ttyin, 0);
592 if (error)
593 return (error);
594 }
595 if (pti->pt_flags & (PF_PKT|PF_UCNTL))
596 error = ureadc(0, uio);
597 while (uio->uio_resid > 0 && error == 0) {
598 cc = q_to_b(&tp->t_outq, buf, min(uio->uio_resid, BUFSIZ));
599 if (cc <= 0)
600 break;
601 error = uiomove(buf, cc, uio);
602 }
603 if (tp->t_outq.c_cc <= tp->t_lowat) {
604 if (ISSET(tp->t_state, TS_ASLEEP)) {
605 CLR(tp->t_state, TS_ASLEEP);
606 wakeup((caddr_t)&tp->t_outq);
607 }
608 selwakeup(&tp->t_wsel);
609 }
610 return (error);
611 }
612
613
614 int
615 ptcwrite(devvp, uio, flag)
616 struct vnode *devvp;
617 struct uio *uio;
618 int flag;
619 {
620 struct pt_softc *pti;
621 struct tty *tp;
622 u_char *cp = NULL;
623 int cc = 0;
624 u_char locbuf[BUFSIZ];
625 int cnt = 0;
626 int error = 0;
627
628 pti = vdev_privdata(devvp);
629 tp = pti->pt_tty;
630
631 again:
632 if (!ISSET(tp->t_state, TS_ISOPEN))
633 goto block;
634 if (pti->pt_flags & PF_REMOTE) {
635 if (tp->t_canq.c_cc)
636 goto block;
637 while (uio->uio_resid > 0 && tp->t_canq.c_cc < TTYHOG - 1) {
638 if (cc == 0) {
639 cc = min(uio->uio_resid, BUFSIZ);
640 cc = min(cc, TTYHOG - 1 - tp->t_canq.c_cc);
641 cp = locbuf;
642 error = uiomove((caddr_t)cp, cc, uio);
643 if (error)
644 return (error);
645 /* check again for safety */
646 if (!ISSET(tp->t_state, TS_ISOPEN))
647 return (EIO);
648 }
649 if (cc)
650 (void) b_to_q((char *)cp, cc, &tp->t_canq);
651 cc = 0;
652 }
653 (void) putc(0, &tp->t_canq);
654 ttwakeup(tp);
655 wakeup((caddr_t)&tp->t_canq);
656 return (0);
657 }
658 while (uio->uio_resid > 0) {
659 if (cc == 0) {
660 cc = min(uio->uio_resid, BUFSIZ);
661 cp = locbuf;
662 error = uiomove((caddr_t)cp, cc, uio);
663 if (error)
664 return (error);
665 /* check again for safety */
666 if (!ISSET(tp->t_state, TS_ISOPEN))
667 return (EIO);
668 }
669 while (cc > 0) {
670 if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 &&
671 (tp->t_canq.c_cc > 0 || !ISSET(tp->t_iflag, ICANON))) {
672 wakeup((caddr_t)&tp->t_rawq);
673 goto block;
674 }
675 (*tp->t_linesw->l_rint)(*cp++, tp);
676 cnt++;
677 cc--;
678 }
679 cc = 0;
680 }
681 return (0);
682 block:
683 /*
684 * Come here to wait for slave to open, for space
685 * in outq, or space in rawq.
686 */
687 if (!ISSET(tp->t_state, TS_CARR_ON))
688 return (EIO);
689 if (flag & IO_NDELAY) {
690 /* adjust for data copied in but not written */
691 uio->uio_resid += cc;
692 if (cnt == 0)
693 return (EWOULDBLOCK);
694 return (0);
695 }
696 error = tsleep((caddr_t)&tp->t_rawq.c_cf, TTOPRI | PCATCH,
697 ttyout, 0);
698 if (error) {
699 /* adjust for data copied in but not written */
700 uio->uio_resid += cc;
701 return (error);
702 }
703 goto again;
704 }
705
706 int
707 ptcpoll(devvp, events, p)
708 struct vnode *devvp;
709 int events;
710 struct proc *p;
711 {
712 struct pt_softc *pti;
713 struct tty *tp;
714 int revents;
715 int s;
716
717 pti = vdev_privdata(devvp);
718 tp = pti->pt_tty;
719 revents = 0;
720
721 s = splsoftclock();
722
723 if (events & (POLLIN | POLLRDNORM))
724 if (ISSET(tp->t_state, TS_ISOPEN) &&
725 ((tp->t_outq.c_cc > 0 && !ISSET(tp->t_state, TS_TTSTOP)) ||
726 ((pti->pt_flags & PF_PKT) && pti->pt_send) ||
727 ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl)))
728 revents |= events & (POLLIN | POLLRDNORM);
729
730 if (events & (POLLOUT | POLLWRNORM))
731 if (ISSET(tp->t_state, TS_ISOPEN) &&
732 ((pti->pt_flags & PF_REMOTE) ?
733 (tp->t_canq.c_cc == 0) :
734 ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG-2) ||
735 (tp->t_canq.c_cc == 0 && ISSET(tp->t_iflag, ICANON)))))
736 revents |= events & (POLLOUT | POLLWRNORM);
737
738 if (events & POLLHUP)
739 if (!ISSET(tp->t_state, TS_CARR_ON))
740 revents |= POLLHUP;
741
742 if (revents == 0) {
743 if (events & (POLLIN | POLLHUP | POLLRDNORM))
744 selrecord(p, &pti->pt_selr);
745
746 if (events & (POLLOUT | POLLWRNORM))
747 selrecord(p, &pti->pt_selw);
748 }
749
750 splx(s);
751 return (revents);
752 }
753
754
755 struct tty *
756 ptytty(devvp)
757 struct vnode *devvp;
758 {
759 struct pt_softc *pti;
760
761 pti = vdev_privdata(devvp);
762 return pti->pt_tty;
763 }
764
765 /*ARGSUSED*/
766 int
767 ptyioctl(devvp, cmd, data, flag, p)
768 struct vnode *devvp;
769 u_long cmd;
770 caddr_t data;
771 int flag;
772 struct proc *p;
773 {
774 struct pt_softc *pti;
775 struct tty *tp;
776 u_char *cc;
777 int stop, error, sig;
778
779 pti = vdev_privdata(devvp);
780 tp = pti->pt_tty;
781 cc = tp->t_cc;
782
783 /*
784 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
785 * ttywflush(tp) will hang if there are characters in the outq.
786 */
787 if (cmd == TIOCEXT) {
788 /*
789 * When the EXTPROC bit is being toggled, we need
790 * to send an TIOCPKT_IOCTL if the packet driver
791 * is turned on.
792 */
793 if (*(int *)data) {
794 if (pti->pt_flags & PF_PKT) {
795 pti->pt_send |= TIOCPKT_IOCTL;
796 ptcwakeup(tp, FREAD);
797 }
798 SET(tp->t_lflag, EXTPROC);
799 } else {
800 if (ISSET(tp->t_lflag, EXTPROC) &&
801 (pti->pt_flags & PF_PKT)) {
802 pti->pt_send |= TIOCPKT_IOCTL;
803 ptcwakeup(tp, FREAD);
804 }
805 CLR(tp->t_lflag, EXTPROC);
806 }
807 return(0);
808 } else
809 if (cdevsw[major(vdev_rdev(devvp))].d_open == ptcopen)
810 switch (cmd) {
811
812 case TIOCGPGRP:
813 #ifdef COMPAT_SUNOS
814 {
815 /*
816 * I'm not sure about SunOS TIOCGPGRP semantics
817 * on PTYs, but it's something like this:
818 */
819 extern struct emul emul_sunos;
820 if (p->p_emul == &emul_sunos && tp->t_pgrp == 0)
821 return (EIO);
822 *(int *)data = tp->t_pgrp->pg_id;
823 return (0);
824 }
825 #endif
826 /*
827 * We avoid calling ttioctl on the controller since,
828 * in that case, tp must be the controlling terminal.
829 */
830 *(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
831 return (0);
832
833 case TIOCPKT:
834 if (*(int *)data) {
835 if (pti->pt_flags & PF_UCNTL)
836 return (EINVAL);
837 pti->pt_flags |= PF_PKT;
838 } else
839 pti->pt_flags &= ~PF_PKT;
840 return (0);
841
842 case TIOCUCNTL:
843 if (*(int *)data) {
844 if (pti->pt_flags & PF_PKT)
845 return (EINVAL);
846 pti->pt_flags |= PF_UCNTL;
847 } else
848 pti->pt_flags &= ~PF_UCNTL;
849 return (0);
850
851 case TIOCREMOTE:
852 if (*(int *)data)
853 pti->pt_flags |= PF_REMOTE;
854 else
855 pti->pt_flags &= ~PF_REMOTE;
856 ttyflush(tp, FREAD|FWRITE);
857 return (0);
858
859 #ifdef COMPAT_OLDTTY
860 case TIOCSETP:
861 case TIOCSETN:
862 #endif
863 case TIOCSETD:
864 case TIOCSETA:
865 case TIOCSETAW:
866 case TIOCSETAF:
867 ndflush(&tp->t_outq, tp->t_outq.c_cc);
868 break;
869
870 case TIOCSIG:
871 sig = (int)(long)*(caddr_t *)data;
872 if (sig <= 0 || sig >= NSIG)
873 return (EINVAL);
874 if (!ISSET(tp->t_lflag, NOFLSH))
875 ttyflush(tp, FREAD|FWRITE);
876 pgsignal(tp->t_pgrp, sig, 1);
877 if ((sig == SIGINFO) &&
878 (!ISSET(tp->t_lflag, NOKERNINFO)))
879 ttyinfo(tp);
880 return(0);
881 }
882 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, p);
883 if (error < 0)
884 error = ttioctl(tp, devvp, cmd, data, flag, p);
885 if (error < 0) {
886 if (pti->pt_flags & PF_UCNTL &&
887 (cmd & ~0xff) == UIOCCMD(0)) {
888 if (cmd & 0xff) {
889 pti->pt_ucntl = (u_char)cmd;
890 ptcwakeup(tp, FREAD);
891 }
892 return (0);
893 }
894 error = ENOTTY;
895 }
896 /*
897 * If external processing and packet mode send ioctl packet.
898 */
899 if (ISSET(tp->t_lflag, EXTPROC) && (pti->pt_flags & PF_PKT)) {
900 switch(cmd) {
901 case TIOCSETA:
902 case TIOCSETAW:
903 case TIOCSETAF:
904 #ifdef COMPAT_OLDTTY
905 case TIOCSETP:
906 case TIOCSETN:
907 case TIOCSETC:
908 case TIOCSLTC:
909 case TIOCLBIS:
910 case TIOCLBIC:
911 case TIOCLSET:
912 #endif
913 pti->pt_send |= TIOCPKT_IOCTL;
914 ptcwakeup(tp, FREAD);
915 default:
916 break;
917 }
918 }
919 stop = ISSET(tp->t_iflag, IXON) && CCEQ(cc[VSTOP], CTRL('s'))
920 && CCEQ(cc[VSTART], CTRL('q'));
921 if (pti->pt_flags & PF_NOSTOP) {
922 if (stop) {
923 pti->pt_send &= ~TIOCPKT_NOSTOP;
924 pti->pt_send |= TIOCPKT_DOSTOP;
925 pti->pt_flags &= ~PF_NOSTOP;
926 ptcwakeup(tp, FREAD);
927 }
928 } else {
929 if (!stop) {
930 pti->pt_send &= ~TIOCPKT_DOSTOP;
931 pti->pt_send |= TIOCPKT_NOSTOP;
932 pti->pt_flags |= PF_NOSTOP;
933 ptcwakeup(tp, FREAD);
934 }
935 }
936 return (error);
937 }
938