4 * Copyright (c) 1982, 1986, 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * @(#)tty_pty.c 8.4 (Berkeley) 2/20/95
32 * $FreeBSD: src/sys/kern/tty_pty.c,v 1.74.2.4 2002/02/20 19:58:13 dillon Exp $
37 * Most functions here could use a separate lock to deal with concurrent
38 * access to the 'pt's.
40 * Right now the tty_token must be held for all this.
44 * Pseudo-teletype Driver
45 * (Actually two drivers, requiring two dev_ops structures)
48 #include "opt_compat.h"
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #if defined(COMPAT_43)
53 #include <sys/ioctl_compat.h>
59 #include <sys/fcntl.h>
60 #include <sys/kernel.h>
61 #include <sys/vnode.h>
62 #include <sys/signalvar.h>
63 #include <sys/malloc.h>
64 #include <sys/device.h>
65 #include <sys/thread2.h>
66 #include <sys/devfs.h>
68 #include <sys/sysctl.h>
70 MALLOC_DEFINE(M_PTY, "ptys", "pty data structures");
72 static void ptsstart (struct tty *tp);
73 static void ptsstop (struct tty *tp, int rw);
74 static void ptsunhold (struct tty *tp);
75 static void ptcwakeup (struct tty *tp, int flag);
76 static void ptyinit (int n);
77 static int filt_ptcread (struct knote *kn, long hint);
78 static void filt_ptcrdetach (struct knote *kn);
79 static int filt_ptcwrite (struct knote *kn, long hint);
80 static void filt_ptcwdetach (struct knote *kn);
82 static d_open_t ptsopen;
83 static d_close_t ptsclose;
84 static d_read_t ptsread;
85 static d_write_t ptswrite;
86 static d_ioctl_t ptyioctl;
87 static d_open_t ptcopen;
88 static d_close_t ptcclose;
89 static d_read_t ptcread;
90 static d_write_t ptcwrite;
91 static d_kqfilter_t ptckqfilter;
93 DEVFS_DECLARE_CLONE_BITMAP(pty);
95 static d_clone_t ptyclone;
97 static int pty_debug_level = 0;
99 static struct dev_ops pts98_ops = {
100 { "pts98", 0, D_TTY | D_MPSAFE },
106 .d_kqfilter = ttykqfilter,
107 .d_revoke = ttyrevoke
110 static struct dev_ops ptc98_ops = {
111 { "ptc98", 0, D_TTY | D_MASTER | D_MPSAFE },
117 .d_kqfilter = ptckqfilter,
118 .d_revoke = ttyrevoke
121 static struct dev_ops pts_ops = {
122 { "pts", 0, D_TTY | D_MPSAFE },
128 .d_kqfilter = ttykqfilter,
129 .d_revoke = ttyrevoke
132 #define CDEV_MAJOR_C 6
133 static struct dev_ops ptc_ops = {
134 { "ptc", 0, D_TTY | D_MASTER | D_MPSAFE },
140 .d_kqfilter = ptckqfilter,
141 .d_revoke = ttyrevoke
144 #define BUFSIZ 100 /* Chunk size iomoved to/from user */
148 int pt_refs; /* Structural references interlock S/MOPEN */
150 struct kqinfo pt_kqr, pt_kqw;
155 struct prison *pt_prison;
161 #define PF_PKT 0x0008 /* packet mode */
162 #define PF_STOPPED 0x0010 /* user told stopped */
163 #define PF_REMOTE 0x0020 /* remote and flow controlled input */
164 #define PF_NOSTOP 0x0040
165 #define PF_UCNTL 0x0080 /* user control mode */
167 #define PF_PTCSTATEMASK 0x00FF
170 * pt_flags open state. Note that PF_SCLOSED is used to activate
171 * read EOF on the ptc so it is only set after the slave has been
172 * opened and then closed, and cleared again if the slave is opened
175 #define PF_UNIX98 0x0100
176 #define PF_SOPEN 0x0200
177 #define PF_MOPEN 0x0400
178 #define PF_SCLOSED 0x0800
179 #define PF_TERMINATED 0x8000
182 * This function creates and initializes a pts/ptc pair
184 * pts == /dev/tty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
185 * ptc == /dev/pty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
187 * XXX: define and add mapping of upper minor bits to allow more
194 char *names = "pqrsPQRS";
197 /* For now we only map the lower 8 bits of the minor */
201 pt = kmalloc(sizeof(*pt), M_PTY, M_WAITOK | M_ZERO);
202 pt->devs = devs = make_dev(&pts_ops, n,
203 0, 0, 0666, "tty%c%r", names[n / 32], n % 32);
204 pt->devc = devc = make_dev(&ptc_ops, n,
205 0, 0, 0666, "pty%c%r", names[n / 32], n % 32);
207 pt->pt_tty.t_dev = devs;
209 devs->si_drv1 = devc->si_drv1 = pt;
210 devs->si_tty = devc->si_tty = &pt->pt_tty;
211 devs->si_flags |= SI_OVERRIDE; /* uid, gid, perms from dev */
212 devc->si_flags |= SI_OVERRIDE; /* uid, gid, perms from dev */
213 ttyregister(&pt->pt_tty);
217 ptyclone(struct dev_clone_args *ap)
223 * Limit the number of unix98 pty (slave) devices to 1000, as
224 * the utmp(5) format only allows for 8 bytes for the tty,
226 * If this limit is reached, we don't clone and return error
229 unit = devfs_clone_bitmap_get(&DEVFS_CLONE_BITMAP(pty), 1000);
236 pt = kmalloc(sizeof(*pt), M_PTY, M_WAITOK | M_ZERO);
238 pt->devc = make_only_dev(&ptc98_ops, unit,
240 0, 0600, "ptm/%d", unit);
241 pt->devs = make_dev(&pts98_ops, unit,
243 GID_TTY, 0620, "pts/%d", unit);
244 ap->a_dev = pt->devc;
246 pt->devs->si_flags |= SI_OVERRIDE; /* uid, gid, perms from dev */
247 pt->devc->si_flags |= SI_OVERRIDE; /* uid, gid, perms from dev */
249 pt->pt_tty.t_dev = pt->devs;
250 pt->pt_flags |= PF_UNIX98;
251 pt->pt_uminor = unit;
252 pt->devs->si_drv1 = pt->devc->si_drv1 = pt;
253 pt->devs->si_tty = pt->devc->si_tty = &pt->pt_tty;
255 ttyregister(&pt->pt_tty);
261 * pti_hold() prevents the pti from being destroyed due to a termination
262 * while a pt*open() is blocked.
264 * This function returns non-zero if we cannot hold due to a termination
267 * NOTE: Must be called with tty_token held
270 pti_hold(struct pt_ioctl *pti)
272 if (pti->pt_flags & PF_TERMINATED)
279 * pti_done() releases the reference and checks to see if both sides have
280 * been closed on a unix98 pty, allowing us to destroy the device and
283 * We do not release resources on non-unix98 ptys. Those are left
284 * statically allocated.
287 pti_done(struct pt_ioctl *pti)
289 lwkt_gettoken(&tty_token);
290 if (--pti->pt_refs == 0) {
295 * Only unix09 ptys are freed up
297 if ((pti->pt_flags & PF_UNIX98) == 0) {
298 lwkt_reltoken(&tty_token);
303 * Interlock open attempts against termination by setting
304 * PF_TERMINATED. This allows us to block while cleaning
305 * out the device infrastructure.
307 * Do not terminate the tty if it still has a session
308 * association (t_refs).
310 if ((pti->pt_flags & (PF_SOPEN|PF_MOPEN)) == 0 &&
311 pti->pt_tty.t_refs == 0) {
312 pti->pt_flags |= PF_TERMINATED;
313 uminor_no = pti->pt_uminor;
315 if ((dev = pti->devs) != NULL) {
320 if ((dev = pti->devc) != NULL) {
325 ttyunregister(&pti->pt_tty);
326 devfs_clone_bitmap_put(&DEVFS_CLONE_BITMAP(pty),
331 lwkt_reltoken(&tty_token);
336 ptsopen(struct dev_open_args *ap)
338 cdev_t dev = ap->a_head.a_dev;
341 struct pt_ioctl *pti;
344 * The pti will already be assigned by the clone code or
345 * pre-created if a non-unix 98 pty. If si_drv1 is NULL
346 * we are somehow racing a unix98 termination.
348 if (dev->si_drv1 == NULL)
352 lwkt_gettoken(&tty_token);
354 lwkt_reltoken(&tty_token);
361 * Reinit most of the tty state if it isn't open. Handle
364 if ((tp->t_state & TS_ISOPEN) == 0) {
365 ttychars(tp); /* Set up default chars */
366 tp->t_iflag = TTYDEF_IFLAG;
367 tp->t_oflag = TTYDEF_OFLAG;
368 tp->t_lflag = TTYDEF_LFLAG;
369 tp->t_cflag = TTYDEF_CFLAG;
370 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
371 } else if ((tp->t_state & TS_XCLUDE) &&
372 priv_check_cred(ap->a_cred, PRIV_ROOT, 0)) {
374 lwkt_reltoken(&tty_token);
376 } else if (pti->pt_prison != ap->a_cred->cr_prison) {
378 lwkt_reltoken(&tty_token);
383 * If the ptc is already present this will connect us up. It
384 * is unclear if this is actually needed.
386 * If neither side is open be sure to clear any left over
387 * ZOMBIE state before continuing.
390 (void)(*linesw[tp->t_line].l_modem)(tp, 1);
391 else if ((pti->pt_flags & PF_SOPEN) == 0)
392 tp->t_state &= ~TS_ZOMBIE;
395 * Wait for the carrier (ptc side)
397 while ((tp->t_state & TS_CARR_ON) == 0) {
398 if (ap->a_oflags & FNONBLOCK)
400 error = ttysleep(tp, TSA_CARR_ON(tp), PCATCH, "ptsopn", 0);
403 lwkt_reltoken(&tty_token);
409 * Mark the tty open and mark the slave side as being open.
411 error = (*linesw[tp->t_line].l_open)(dev, tp);
414 pti->pt_flags |= PF_SOPEN;
415 pti->pt_flags &= ~PF_SCLOSED;
416 ptcwakeup(tp, FREAD|FWRITE);
420 lwkt_reltoken(&tty_token);
425 ptsclose(struct dev_close_args *ap)
427 cdev_t dev = ap->a_head.a_dev;
429 struct pt_ioctl *pti = dev->si_drv1;
432 lwkt_gettoken(&tty_token);
434 panic("ptsclose on terminated pti");
437 * Disconnect the slave side
440 err = (*linesw[tp->t_line].l_close)(tp, ap->a_fflag);
441 ptsstop(tp, FREAD|FWRITE);
442 ttyclose(tp); /* clears t_state */
445 * Mark the pts side closed and signal the ptc. Do not mark the
446 * tty a zombie... that is, allow the tty to be re-opened as long
447 * as the ptc is still open. The ptc will read() EOFs until the
448 * pts side is reopened or the ptc is closed.
450 * xterm() depends on this behavior as it will revoke() the pts
451 * and then reopen it after the (unnecessary old code) chmod.
453 pti->pt_flags &= ~PF_SOPEN;
454 pti->pt_flags |= PF_SCLOSED;
456 ptcwakeup(tp, FREAD);
458 lwkt_reltoken(&tty_token);
463 ptsread(struct dev_read_args *ap)
465 cdev_t dev = ap->a_head.a_dev;
466 struct proc *p = curproc;
467 struct tty *tp = dev->si_tty;
468 struct pt_ioctl *pti = dev->si_drv1;
473 lp = curthread->td_lwp;
475 lwkt_gettoken(&tty_token);
477 if (pti->pt_flags & PF_REMOTE) {
478 while (isbackground(p, tp)) {
479 if (SIGISMEMBER(p->p_sigignore, SIGTTIN) ||
480 SIGISMEMBER(lp->lwp_sigmask, SIGTTIN) ||
481 p->p_pgrp->pg_jobc == 0 ||
482 (p->p_flags & P_PPWAIT)) {
483 lwkt_reltoken(&tty_token);
486 pgsignal(p->p_pgrp, SIGTTIN, 1);
487 error = ttysleep(tp, &lbolt, PCATCH, "ptsbg", 0);
489 lwkt_reltoken(&tty_token);
493 if (tp->t_canq.c_cc == 0) {
494 if (ap->a_ioflag & IO_NDELAY) {
495 lwkt_reltoken(&tty_token);
496 return (EWOULDBLOCK);
498 error = ttysleep(tp, TSA_PTS_READ(tp), PCATCH,
501 lwkt_reltoken(&tty_token);
506 while (tp->t_canq.c_cc > 1 && ap->a_uio->uio_resid > 0)
507 if (ureadc(clist_getc(&tp->t_canq), ap->a_uio) < 0) {
511 if (tp->t_canq.c_cc == 1)
512 clist_getc(&tp->t_canq);
513 if (tp->t_canq.c_cc) {
514 lwkt_reltoken(&tty_token);
519 error = (*linesw[tp->t_line].l_read)(tp, ap->a_uio, ap->a_ioflag);
520 ptcwakeup(tp, FWRITE);
521 lwkt_reltoken(&tty_token);
526 * Write to pseudo-tty.
527 * Wakeups of controlling tty will happen
528 * indirectly, when tty driver calls ptsstart.
531 ptswrite(struct dev_write_args *ap)
533 cdev_t dev = ap->a_head.a_dev;
537 lwkt_gettoken(&tty_token);
539 if (tp->t_oproc == NULL) {
540 lwkt_reltoken(&tty_token);
543 ret = ((*linesw[tp->t_line].l_write)(tp, ap->a_uio, ap->a_ioflag));
544 lwkt_reltoken(&tty_token);
549 * Start output on pseudo-tty.
550 * Wake up process selecting or sleeping for input from controlling tty.
553 ptsstart(struct tty *tp)
555 lwkt_gettoken(&tty_token);
556 struct pt_ioctl *pti = tp->t_dev->si_drv1;
558 if (tp->t_state & TS_TTSTOP) {
559 lwkt_reltoken(&tty_token);
563 if (pti->pt_flags & PF_STOPPED) {
564 pti->pt_flags &= ~PF_STOPPED;
565 pti->pt_send = TIOCPKT_START;
568 ptcwakeup(tp, FREAD);
569 lwkt_reltoken(&tty_token);
573 * NOTE: Must be called with tty_token held
576 ptcwakeup(struct tty *tp, int flag)
578 ASSERT_LWKT_TOKEN_HELD(&tty_token);
581 wakeup(TSA_PTC_READ(tp));
582 KNOTE(&tp->t_rkq.ki_note, 0);
585 wakeup(TSA_PTC_WRITE(tp));
586 KNOTE(&tp->t_wkq.ki_note, 0);
591 ptcopen(struct dev_open_args *ap)
593 cdev_t dev = ap->a_head.a_dev;
595 struct pt_ioctl *pti;
598 * The pti will already be assigned by the clone code or
599 * pre-created if a non-unix 98 pty. If si_drv1 is NULL
600 * we are somehow racing a unix98 termination.
602 if (dev->si_drv1 == NULL)
605 lwkt_gettoken(&tty_token);
608 lwkt_reltoken(&tty_token);
611 if (pti->pt_prison && pti->pt_prison != ap->a_cred->cr_prison) {
613 lwkt_reltoken(&tty_token);
619 lwkt_reltoken(&tty_token);
624 * If the slave side is not yet open clear any left over zombie
625 * state before doing our modem control.
627 if ((pti->pt_flags & PF_SOPEN) == 0)
628 tp->t_state &= ~TS_ZOMBIE;
630 tp->t_oproc = ptsstart;
631 tp->t_stop = ptsstop;
632 tp->t_unhold = ptsunhold;
637 (void)(*linesw[tp->t_line].l_modem)(tp, 1);
639 tp->t_lflag &= ~EXTPROC;
640 pti->pt_prison = ap->a_cred->cr_prison;
641 pti->pt_flags &= ~PF_PTCSTATEMASK;
645 pti->devs->si_uid = ap->a_cred->cr_uid;
646 pti->devs->si_gid = 0;
647 pti->devs->si_perms = 0600;
648 pti->devc->si_uid = ap->a_cred->cr_uid;
649 pti->devc->si_gid = 0;
650 pti->devc->si_perms = 0600;
653 * Mark master side open. This does not cause any events
656 pti->pt_flags |= PF_MOPEN;
659 lwkt_reltoken(&tty_token);
664 ptcclose(struct dev_close_args *ap)
666 cdev_t dev = ap->a_head.a_dev;
668 struct pt_ioctl *pti = dev->si_drv1;
670 lwkt_gettoken(&tty_token);
672 panic("ptcclose on terminated pti");
675 (void)(*linesw[tp->t_line].l_modem)(tp, 0);
678 * Mark the master side closed. If the slave is still open
679 * mark the tty ZOMBIE, preventing any new action until both
682 * NOTE: The ttyflush() will wake up the slave once we've
683 * set appropriate flags. The ZOMBIE flag will be
684 * cleared when the slave side is closed.
686 pti->pt_flags &= ~PF_MOPEN;
687 if (pti->pt_flags & PF_SOPEN)
688 tp->t_state |= TS_ZOMBIE;
691 * Turn off the carrier and disconnect. This will notify the slave
694 if (tp->t_state & TS_ISOPEN) {
695 tp->t_state &= ~(TS_CARR_ON | TS_CONNECTED);
696 ttyflush(tp, FREAD | FWRITE);
698 tp->t_oproc = NULL; /* mark closed */
700 pti->pt_prison = NULL;
701 pti->devs->si_uid = 0;
702 pti->devs->si_gid = 0;
703 pti->devs->si_perms = 0666;
704 pti->devc->si_uid = 0;
705 pti->devc->si_gid = 0;
706 pti->devc->si_perms = 0666;
710 lwkt_reltoken(&tty_token);
715 ptcread(struct dev_read_args *ap)
717 cdev_t dev = ap->a_head.a_dev;
718 struct tty *tp = dev->si_tty;
719 struct pt_ioctl *pti = dev->si_drv1;
723 lwkt_gettoken(&tty_token);
725 * We want to block until the slave
726 * is open, and there's something to read;
727 * but if we lost the slave or we're NBIO,
728 * then return the appropriate error instead.
731 if (tp->t_state&TS_ISOPEN) {
732 if ((pti->pt_flags & PF_PKT) && pti->pt_send) {
733 error = ureadc((int)pti->pt_send, ap->a_uio);
735 lwkt_reltoken(&tty_token);
738 if (pti->pt_send & TIOCPKT_IOCTL) {
739 cc = (int)szmin(ap->a_uio->uio_resid,
740 sizeof(tp->t_termios));
741 uiomove((caddr_t)&tp->t_termios, cc,
745 lwkt_reltoken(&tty_token);
748 if ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl) {
749 error = ureadc((int)pti->pt_ucntl, ap->a_uio);
751 lwkt_reltoken(&tty_token);
755 lwkt_reltoken(&tty_token);
758 if (tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0)
761 if ((tp->t_state & TS_CONNECTED) == 0) {
762 lwkt_reltoken(&tty_token);
763 return (0); /* EOF */
765 if (ap->a_ioflag & IO_NDELAY) {
766 lwkt_reltoken(&tty_token);
767 return (EWOULDBLOCK);
769 error = tsleep(TSA_PTC_READ(tp), PCATCH, "ptcin", 0);
771 lwkt_reltoken(&tty_token);
775 if (pti->pt_flags & (PF_PKT|PF_UCNTL))
776 error = ureadc(0, ap->a_uio);
777 while (ap->a_uio->uio_resid > 0 && error == 0) {
778 cc = q_to_b(&tp->t_outq, buf,
779 (int)szmin(ap->a_uio->uio_resid, BUFSIZ));
782 error = uiomove(buf, (size_t)cc, ap->a_uio);
785 lwkt_reltoken(&tty_token);
790 ptsstop(struct tty *tp, int flush)
792 struct pt_ioctl *pti = tp->t_dev->si_drv1;
795 lwkt_gettoken(&tty_token);
796 /* note: FLUSHREAD and FLUSHWRITE already ok */
799 flush = TIOCPKT_STOP;
800 pti->pt_flags |= PF_STOPPED;
802 pti->pt_flags &= ~PF_STOPPED;
804 pti->pt_send |= flush;
805 /* change of perspective */
814 lwkt_reltoken(&tty_token);
818 * ttyunhold() calls us instead of just decrementing tp->t_refs. This
819 * is needed because a session can hold onto a pts (half closed state)
820 * even if there are no live file descriptors. Without the callback
824 ptsunhold(struct tty *tp)
826 struct pt_ioctl *pti = tp->t_dev->si_drv1;
828 lwkt_gettoken(&tty_token);
832 lwkt_reltoken(&tty_token);
836 * kqueue ops for pseudo-terminals.
838 static struct filterops ptcread_filtops =
839 { FILTEROP_ISFD|FILTEROP_MPSAFE, NULL, filt_ptcrdetach, filt_ptcread };
840 static struct filterops ptcwrite_filtops =
841 { FILTEROP_ISFD|FILTEROP_MPSAFE, NULL, filt_ptcwdetach, filt_ptcwrite };
844 ptckqfilter(struct dev_kqfilter_args *ap)
846 cdev_t dev = ap->a_head.a_dev;
847 struct knote *kn = ap->a_kn;
848 struct tty *tp = dev->si_tty;
851 lwkt_gettoken(&tty_token);
853 switch (kn->kn_filter) {
855 klist = &tp->t_rkq.ki_note;
856 kn->kn_fop = &ptcread_filtops;
859 klist = &tp->t_wkq.ki_note;
860 kn->kn_fop = &ptcwrite_filtops;
863 ap->a_result = EOPNOTSUPP;
864 lwkt_reltoken(&tty_token);
868 kn->kn_hook = (caddr_t)dev;
869 knote_insert(klist, kn);
870 lwkt_reltoken(&tty_token);
875 filt_ptcread (struct knote *kn, long hint)
877 struct tty *tp = ((cdev_t)kn->kn_hook)->si_tty;
878 struct pt_ioctl *pti = ((cdev_t)kn->kn_hook)->si_drv1;
880 lwkt_gettoken(&tty_token);
881 if ((tp->t_state & TS_ZOMBIE) || (pti->pt_flags & PF_SCLOSED)) {
882 kn->kn_flags |= (EV_EOF | EV_NODATA);
883 lwkt_reltoken(&tty_token);
887 if ((tp->t_state & TS_ISOPEN) &&
888 ((tp->t_outq.c_cc && (tp->t_state & TS_TTSTOP) == 0) ||
889 ((pti->pt_flags & PF_PKT) && pti->pt_send) ||
890 ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl))) {
891 kn->kn_data = tp->t_outq.c_cc;
892 lwkt_reltoken(&tty_token);
895 lwkt_reltoken(&tty_token);
901 filt_ptcwrite (struct knote *kn, long hint)
903 struct tty *tp = ((cdev_t)kn->kn_hook)->si_tty;
904 struct pt_ioctl *pti = ((cdev_t)kn->kn_hook)->si_drv1;
906 lwkt_gettoken(&tty_token);
907 if (tp->t_state & TS_ZOMBIE) {
908 kn->kn_flags |= (EV_EOF | EV_NODATA);
909 lwkt_reltoken(&tty_token);
913 if (tp->t_state & TS_ISOPEN &&
914 ((pti->pt_flags & PF_REMOTE) ?
915 (tp->t_canq.c_cc == 0) :
916 ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG - 2) ||
917 (tp->t_canq.c_cc == 0 && (tp->t_lflag & ICANON))))) {
918 kn->kn_data = tp->t_canq.c_cc + tp->t_rawq.c_cc;
919 lwkt_reltoken(&tty_token);
922 lwkt_reltoken(&tty_token);
929 filt_ptcrdetach (struct knote *kn)
931 struct tty *tp = ((cdev_t)kn->kn_hook)->si_tty;
933 knote_remove(&tp->t_rkq.ki_note, kn);
937 filt_ptcwdetach (struct knote *kn)
939 struct tty *tp = ((cdev_t)kn->kn_hook)->si_tty;
941 knote_remove(&tp->t_wkq.ki_note, kn);
948 ptcwrite(struct dev_write_args *ap)
950 cdev_t dev = ap->a_head.a_dev;
951 struct tty *tp = dev->si_tty;
954 u_char locbuf[BUFSIZ];
956 struct pt_ioctl *pti = dev->si_drv1;
959 lwkt_gettoken(&tty_token);
961 if ((tp->t_state&TS_ISOPEN) == 0)
963 if (pti->pt_flags & PF_REMOTE) {
966 while ((ap->a_uio->uio_resid > 0 || cc > 0) &&
967 tp->t_canq.c_cc < TTYHOG - 1) {
969 cc = (int)szmin(ap->a_uio->uio_resid, BUFSIZ);
970 cc = imin(cc, TTYHOG - 1 - tp->t_canq.c_cc);
972 error = uiomove(cp, (size_t)cc, ap->a_uio);
974 lwkt_reltoken(&tty_token);
977 /* check again for safety */
978 if ((tp->t_state & TS_ISOPEN) == 0) {
979 /* adjust as usual */
980 ap->a_uio->uio_resid += cc;
981 lwkt_reltoken(&tty_token);
986 cc = b_to_q((char *)cp, cc, &tp->t_canq);
988 * XXX we don't guarantee that the canq size
989 * is >= TTYHOG, so the above b_to_q() may
990 * leave some bytes uncopied. However, space
991 * is guaranteed for the null terminator if
992 * we don't fail here since (TTYHOG - 1) is
993 * not a multiple of CBSIZE.
999 /* adjust for data copied in but not written */
1000 ap->a_uio->uio_resid += cc;
1001 clist_putc(0, &tp->t_canq);
1003 wakeup(TSA_PTS_READ(tp));
1004 lwkt_reltoken(&tty_token);
1007 while (ap->a_uio->uio_resid > 0 || cc > 0) {
1009 cc = (int)szmin(ap->a_uio->uio_resid, BUFSIZ);
1011 error = uiomove(cp, (size_t)cc, ap->a_uio);
1013 lwkt_reltoken(&tty_token);
1016 /* check again for safety */
1017 if ((tp->t_state & TS_ISOPEN) == 0) {
1018 /* adjust for data copied in but not written */
1019 ap->a_uio->uio_resid += cc;
1020 lwkt_reltoken(&tty_token);
1025 if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 &&
1026 (tp->t_canq.c_cc > 0 || !(tp->t_lflag&ICANON))) {
1027 wakeup(TSA_HUP_OR_INPUT(tp));
1030 (*linesw[tp->t_line].l_rint)(*cp++, tp);
1036 lwkt_reltoken(&tty_token);
1040 * Come here to wait for slave to open, for space
1041 * in outq, or space in rawq, or an empty canq.
1043 if ((tp->t_state & TS_CONNECTED) == 0) {
1044 /* adjust for data copied in but not written */
1045 ap->a_uio->uio_resid += cc;
1046 lwkt_reltoken(&tty_token);
1049 if (ap->a_ioflag & IO_NDELAY) {
1050 /* adjust for data copied in but not written */
1051 ap->a_uio->uio_resid += cc;
1053 lwkt_reltoken(&tty_token);
1054 return (EWOULDBLOCK);
1056 lwkt_reltoken(&tty_token);
1059 error = tsleep(TSA_PTC_WRITE(tp), PCATCH, "ptcout", 0);
1061 /* adjust for data copied in but not written */
1062 ap->a_uio->uio_resid += cc;
1063 lwkt_reltoken(&tty_token);
1071 ptyioctl(struct dev_ioctl_args *ap)
1073 cdev_t dev = ap->a_head.a_dev;
1074 struct tty *tp = dev->si_tty;
1075 struct pt_ioctl *pti = dev->si_drv1;
1076 u_char *cc = tp->t_cc;
1079 lwkt_gettoken(&tty_token);
1080 if (dev_dflags(dev) & D_MASTER) {
1081 switch (ap->a_cmd) {
1085 * We avoid calling ttioctl on the controller since,
1086 * in that case, tp must be the controlling terminal.
1088 *(int *)ap->a_data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
1089 lwkt_reltoken(&tty_token);
1093 if (*(int *)ap->a_data) {
1094 if (pti->pt_flags & PF_UCNTL) {
1095 lwkt_reltoken(&tty_token);
1098 pti->pt_flags |= PF_PKT;
1100 pti->pt_flags &= ~PF_PKT;
1102 lwkt_reltoken(&tty_token);
1106 if (*(int *)ap->a_data) {
1107 if (pti->pt_flags & PF_PKT) {
1108 lwkt_reltoken(&tty_token);
1111 pti->pt_flags |= PF_UCNTL;
1113 pti->pt_flags &= ~PF_UCNTL;
1115 lwkt_reltoken(&tty_token);
1119 if (*(int *)ap->a_data)
1120 pti->pt_flags |= PF_REMOTE;
1122 pti->pt_flags &= ~PF_REMOTE;
1123 ttyflush(tp, FREAD|FWRITE);
1124 lwkt_reltoken(&tty_token);
1127 case TIOCISPTMASTER:
1128 if ((pti->pt_flags & PF_UNIX98) &&
1129 (pti->devc == dev)) {
1130 lwkt_reltoken(&tty_token);
1133 lwkt_reltoken(&tty_token);
1139 * The rest of the ioctls shouldn't be called until
1140 * the slave is open.
1142 if ((tp->t_state & TS_ISOPEN) == 0) {
1143 lwkt_reltoken(&tty_token);
1147 switch (ap->a_cmd) {
1157 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
1158 * ttywflush(tp) will hang if there are characters in
1161 ndflush(&tp->t_outq, tp->t_outq.c_cc);
1165 if (*(unsigned int *)ap->a_data >= NSIG ||
1166 *(unsigned int *)ap->a_data == 0) {
1167 lwkt_reltoken(&tty_token);
1170 if ((tp->t_lflag&NOFLSH) == 0)
1171 ttyflush(tp, FREAD|FWRITE);
1172 pgsignal(tp->t_pgrp, *(unsigned int *)ap->a_data, 1);
1173 if ((*(unsigned int *)ap->a_data == SIGINFO) &&
1174 ((tp->t_lflag&NOKERNINFO) == 0))
1176 lwkt_reltoken(&tty_token);
1180 if (ap->a_cmd == TIOCEXT) {
1182 * When the EXTPROC bit is being toggled, we need
1183 * to send an TIOCPKT_IOCTL if the packet driver
1186 if (*(int *)ap->a_data) {
1187 if (pti->pt_flags & PF_PKT) {
1188 pti->pt_send |= TIOCPKT_IOCTL;
1189 ptcwakeup(tp, FREAD);
1191 tp->t_lflag |= EXTPROC;
1193 if ((tp->t_lflag & EXTPROC) &&
1194 (pti->pt_flags & PF_PKT)) {
1195 pti->pt_send |= TIOCPKT_IOCTL;
1196 ptcwakeup(tp, FREAD);
1198 tp->t_lflag &= ~EXTPROC;
1200 lwkt_reltoken(&tty_token);
1203 error = (*linesw[tp->t_line].l_ioctl)(tp, ap->a_cmd, ap->a_data,
1204 ap->a_fflag, ap->a_cred);
1205 if (error == ENOIOCTL)
1206 error = ttioctl(tp, ap->a_cmd, ap->a_data, ap->a_fflag);
1207 if (error == ENOIOCTL) {
1208 if (pti->pt_flags & PF_UCNTL &&
1209 (ap->a_cmd & ~0xff) == UIOCCMD(0)) {
1210 if (ap->a_cmd & 0xff) {
1211 pti->pt_ucntl = (u_char)ap->a_cmd;
1212 ptcwakeup(tp, FREAD);
1214 lwkt_reltoken(&tty_token);
1220 * If external processing and packet mode send ioctl packet.
1222 if ((tp->t_lflag&EXTPROC) && (pti->pt_flags & PF_PKT)) {
1236 pti->pt_send |= TIOCPKT_IOCTL;
1237 ptcwakeup(tp, FREAD);
1242 stop = (tp->t_iflag & IXON) && CCEQ(cc[VSTOP], CTRL('s'))
1243 && CCEQ(cc[VSTART], CTRL('q'));
1244 if (pti->pt_flags & PF_NOSTOP) {
1246 pti->pt_send &= ~TIOCPKT_NOSTOP;
1247 pti->pt_send |= TIOCPKT_DOSTOP;
1248 pti->pt_flags &= ~PF_NOSTOP;
1249 ptcwakeup(tp, FREAD);
1253 pti->pt_send &= ~TIOCPKT_DOSTOP;
1254 pti->pt_send |= TIOCPKT_NOSTOP;
1255 pti->pt_flags |= PF_NOSTOP;
1256 ptcwakeup(tp, FREAD);
1259 lwkt_reltoken(&tty_token);
1264 static void ptc_drvinit (void *unused);
1266 SYSCTL_INT(_kern, OID_AUTO, pty_debug, CTLFLAG_RW, &pty_debug_level,
1267 0, "Change pty debug level");
1270 ptc_drvinit(void *unused)
1276 * Create the clonable base device.
1278 make_autoclone_dev(&ptc_ops, &DEVFS_CLONE_BITMAP(pty), ptyclone,
1279 0, 0, 0666, "ptmx");
1281 for (i = 0; i < 256; i++) {
1286 SYSINIT(ptcdev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE + CDEV_MAJOR_C, ptc_drvinit,