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. 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.
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
35 * @(#)tty_pty.c 8.4 (Berkeley) 2/20/95
36 * $FreeBSD: src/sys/kern/tty_pty.c,v 1.74.2.4 2002/02/20 19:58:13 dillon Exp $
37 * $DragonFly: src/sys/kern/tty_pty.c,v 1.21 2008/08/13 10:29:38 swildner Exp $
42 * Most functions here could use a separate lock to deal with concurrent
43 * access to the 'pt's.
45 * Right now the tty_token must be held for all this.
49 * Pseudo-teletype Driver
50 * (Actually two drivers, requiring two dev_ops structures)
52 #include "use_pty.h" /* XXX */
53 #include "opt_compat.h"
55 #include <sys/param.h>
56 #include <sys/systm.h>
57 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
58 #include <sys/ioctl_compat.h>
64 #include <sys/fcntl.h>
65 #include <sys/kernel.h>
66 #include <sys/vnode.h>
67 #include <sys/signalvar.h>
68 #include <sys/malloc.h>
69 #include <sys/device.h>
70 #include <sys/thread2.h>
71 #include <sys/devfs.h>
73 #include <sys/sysctl.h>
77 MALLOC_DEFINE(M_PTY, "ptys", "pty data structures");
79 static void ptsstart (struct tty *tp);
80 static void ptsstop (struct tty *tp, int rw);
81 static void ptcwakeup (struct tty *tp, int flag);
82 static void ptyinit (int n);
83 static int filt_ptcread (struct knote *kn, long hint);
84 static void filt_ptcrdetach (struct knote *kn);
85 static int filt_ptcwrite (struct knote *kn, long hint);
86 static void filt_ptcwdetach (struct knote *kn);
88 static d_open_t ptsopen;
89 static d_close_t ptsclose;
90 static d_read_t ptsread;
91 static d_write_t ptswrite;
92 static d_ioctl_t ptyioctl;
93 static d_open_t ptcopen;
94 static d_close_t ptcclose;
95 static d_read_t ptcread;
96 static d_write_t ptcwrite;
97 static d_kqfilter_t ptckqfilter;
100 DEVFS_DECLARE_CLONE_BITMAP(pty);
102 static d_clone_t ptyclone;
104 static int pty_debug_level = 0;
106 static struct dev_ops pts98_ops = {
107 { "pts98", 0, D_TTY },
113 .d_kqfilter = ttykqfilter,
114 .d_revoke = ttyrevoke
117 static struct dev_ops ptc98_ops = {
118 { "ptc98", 0, D_TTY | D_MASTER },
124 .d_kqfilter = ptckqfilter,
125 .d_revoke = ttyrevoke
129 #define CDEV_MAJOR_S 5
130 static struct dev_ops pts_ops = {
131 { "pts", CDEV_MAJOR_S, D_TTY },
137 .d_kqfilter = ttykqfilter,
138 .d_revoke = ttyrevoke
141 #define CDEV_MAJOR_C 6
142 static struct dev_ops ptc_ops = {
143 { "ptc", CDEV_MAJOR_C, D_TTY | D_MASTER },
149 .d_kqfilter = ptckqfilter,
150 .d_revoke = ttyrevoke
153 #define BUFSIZ 100 /* Chunk size iomoved to/from user */
158 int pt_refs; /* Structural references interlock S/MOPEN */
160 struct kqinfo pt_kqr, pt_kqw;
165 struct prison *pt_prison;
168 #define PF_PKT 0x08 /* packet mode */
169 #define PF_STOPPED 0x10 /* user told stopped */
170 #define PF_REMOTE 0x20 /* remote and flow controlled input */
171 #define PF_NOSTOP 0x40
172 #define PF_UCNTL 0x80 /* user control mode */
174 #define PF_UNIX98 0x01
175 #define PF_SOPEN 0x02
176 #define PF_MOPEN 0x04
177 #define PF_TERMINATED 0x08
180 ptydebug(int level, char *fmt, ...)
185 if (level <= pty_debug_level)
193 * This function creates and initializes a pts/ptc pair
195 * pts == /dev/tty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
196 * ptc == /dev/pty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
198 * XXX: define and add mapping of upper minor bits to allow more
205 char *names = "pqrsPQRS";
208 /* For now we only map the lower 8 bits of the minor */
212 pt = kmalloc(sizeof(*pt), M_PTY, M_WAITOK | M_ZERO);
213 pt->devs = devs = make_dev(&pts_ops, n,
214 0, 0, 0666, "tty%c%r", names[n / 32], n % 32);
215 pt->devc = devc = make_dev(&ptc_ops, n,
216 0, 0, 0666, "pty%c%r", names[n / 32], n % 32);
218 pt->pt_tty.t_dev = devs;
220 devs->si_drv1 = devc->si_drv1 = pt;
221 devs->si_tty = devc->si_tty = &pt->pt_tty;
222 devs->si_flags |= SI_OVERRIDE; /* uid, gid, perms from dev */
223 devc->si_flags |= SI_OVERRIDE; /* uid, gid, perms from dev */
224 ttyregister(&pt->pt_tty);
229 ptyclone(struct dev_clone_args *ap)
235 * Limit the number of unix98 pty (slave) devices to 1000, as
236 * the utmp(5) format only allows for 8 bytes for the tty,
238 * If this limit is reached, we don't clone and return error
241 unit = devfs_clone_bitmap_get(&DEVFS_CLONE_BITMAP(pty), 1000);
248 pt = kmalloc(sizeof(*pt), M_PTY, M_WAITOK | M_ZERO);
250 pt->devc = make_only_dev(&ptc98_ops, unit,
252 0, 0600, "ptm/%d", unit);
253 pt->devs = make_dev(&pts98_ops, unit,
255 GID_TTY, 0620, "pts/%d", unit);
256 ap->a_dev = pt->devc;
258 pt->devs->si_flags |= SI_OVERRIDE; /* uid, gid, perms from dev */
259 pt->devc->si_flags |= SI_OVERRIDE; /* uid, gid, perms from dev */
261 pt->pt_tty.t_dev = pt->devs;
262 pt->pt_flags2 |= PF_UNIX98;
263 pt->pt_uminor = unit;
264 pt->devs->si_drv1 = pt->devc->si_drv1 = pt;
265 pt->devs->si_tty = pt->devc->si_tty = &pt->pt_tty;
267 ttyregister(&pt->pt_tty);
274 * pti_hold() prevents the pti from being destroyed due to a termination
275 * while a pt*open() is blocked.
277 * This function returns non-zero if we cannot hold due to a termination
280 * NOTE: Must be called with tty_token held
283 pti_hold(struct pt_ioctl *pti)
285 if (pti->pt_flags2 & PF_TERMINATED)
292 * pti_done() releases the reference and checks to see if both sides have
293 * been closed on a unix98 pty, allowing us to destroy the device and
296 * We do not release resources on non-unix98 ptys. Those are left
297 * statically allocated.
300 pti_done(struct pt_ioctl *pti)
302 lwkt_gettoken(&tty_token);
303 if (--pti->pt_refs == 0) {
309 * Only unix09 ptys are freed up
311 if ((pti->pt_flags2 & PF_UNIX98) == 0) {
312 lwkt_reltoken(&tty_token);
317 * Interlock open attempts against termination by setting
318 * PF_TERMINATED. This allows us to block while cleaning
319 * out the device infrastructure.
321 * Do not terminate the tty if it still has a session
322 * association (t_refs).
324 if ((pti->pt_flags2 & (PF_SOPEN|PF_MOPEN)) == 0 &&
325 pti->pt_tty.t_refs == 0) {
326 pti->pt_flags2 |= PF_TERMINATED;
327 uminor_no = pti->pt_uminor;
329 if ((dev = pti->devs) != NULL) {
334 if ((dev = pti->devc) != NULL) {
339 ttyunregister(&pti->pt_tty);
340 devfs_clone_bitmap_put(&DEVFS_CLONE_BITMAP(pty),
346 lwkt_reltoken(&tty_token);
351 ptsopen(struct dev_open_args *ap)
353 cdev_t dev = ap->a_head.a_dev;
356 struct pt_ioctl *pti;
359 * The pti will already be assigned by the clone code or
360 * pre-created if a non-unix 98 pty. If si_drv1 is NULL
361 * we are somehow racing a unix98 termination.
363 if (dev->si_drv1 == NULL)
367 lwkt_gettoken(&tty_token);
369 lwkt_reltoken(&tty_token);
373 if ((tp->t_state & TS_ISOPEN) == 0) {
374 ttychars(tp); /* Set up default chars */
375 tp->t_iflag = TTYDEF_IFLAG;
376 tp->t_oflag = TTYDEF_OFLAG;
377 tp->t_lflag = TTYDEF_LFLAG;
378 tp->t_cflag = TTYDEF_CFLAG;
379 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
380 } else if ((tp->t_state & TS_XCLUDE) && priv_check_cred(ap->a_cred, PRIV_ROOT, 0)) {
382 lwkt_reltoken(&tty_token);
384 } else if (pti->pt_prison != ap->a_cred->cr_prison) {
386 lwkt_reltoken(&tty_token);
389 if (tp->t_oproc) /* Ctrlr still around. */
390 (void)(*linesw[tp->t_line].l_modem)(tp, 1);
391 while ((tp->t_state & TS_CARR_ON) == 0) {
392 if (ap->a_oflags & FNONBLOCK)
394 error = ttysleep(tp, TSA_CARR_ON(tp), PCATCH, "ptsopn", 0);
397 lwkt_reltoken(&tty_token);
401 error = (*linesw[tp->t_line].l_open)(dev, tp);
403 ptcwakeup(tp, FREAD|FWRITE);
408 * On open of the slave, we set the corresponding flag in the common
411 ptydebug(1, "ptsopen=%s | unix98? %s\n", dev->si_name,
412 (pti->pt_flags2 & PF_UNIX98)?"yes":"no");
414 if (error == 0 && (pti->pt_flags2 & PF_UNIX98)) {
415 pti->pt_flags2 |= PF_SOPEN;
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");
436 err = (*linesw[tp->t_line].l_close)(tp, ap->a_fflag);
437 ptsstop(tp, FREAD|FWRITE);
438 (void) ttyclose(tp); /* clears t_state */
443 * On close of the slave, we unset the corresponding flag, and if the master
444 * isn't open anymore, we destroy the slave and unset the unit.
446 ptydebug(1, "ptsclose=%s | unix98? %s\n", dev->si_name,
447 (pti->pt_flags2 & PF_UNIX98)?"yes":"no");
449 if (pti->pt_flags2 & PF_UNIX98) {
450 pti->pt_flags2 &= ~PF_SOPEN;
451 KKASSERT((pti->pt_flags2 & PF_SOPEN) == 0);
452 ptydebug(1, "master open? %s\n",
453 (pti->pt_flags2 & PF_MOPEN)?"yes":"no");
457 lwkt_reltoken(&tty_token);
462 ptsread(struct dev_read_args *ap)
464 cdev_t dev = ap->a_head.a_dev;
465 struct proc *p = curproc;
466 struct tty *tp = dev->si_tty;
467 struct pt_ioctl *pti = dev->si_drv1;
472 lp = curthread->td_lwp;
474 lwkt_gettoken(&tty_token);
476 if (pti->pt_flags & PF_REMOTE) {
477 while (isbackground(p, tp)) {
478 if (SIGISMEMBER(p->p_sigignore, SIGTTIN) ||
479 SIGISMEMBER(lp->lwp_sigmask, SIGTTIN) ||
480 p->p_pgrp->pg_jobc == 0 || p->p_flag & P_PPWAIT) {
481 lwkt_reltoken(&tty_token);
484 pgsignal(p->p_pgrp, SIGTTIN, 1);
485 error = ttysleep(tp, &lbolt, PCATCH, "ptsbg", 0);
487 lwkt_reltoken(&tty_token);
491 if (tp->t_canq.c_cc == 0) {
492 if (ap->a_ioflag & IO_NDELAY) {
493 lwkt_reltoken(&tty_token);
494 return (EWOULDBLOCK);
496 error = ttysleep(tp, TSA_PTS_READ(tp), PCATCH,
499 lwkt_reltoken(&tty_token);
504 while (tp->t_canq.c_cc > 1 && ap->a_uio->uio_resid > 0)
505 if (ureadc(clist_getc(&tp->t_canq), ap->a_uio) < 0) {
509 if (tp->t_canq.c_cc == 1)
510 clist_getc(&tp->t_canq);
511 if (tp->t_canq.c_cc) {
512 lwkt_reltoken(&tty_token);
517 error = (*linesw[tp->t_line].l_read)(tp, ap->a_uio, ap->a_ioflag);
518 ptcwakeup(tp, FWRITE);
519 lwkt_reltoken(&tty_token);
524 * Write to pseudo-tty.
525 * Wakeups of controlling tty will happen
526 * indirectly, when tty driver calls ptsstart.
529 ptswrite(struct dev_write_args *ap)
531 cdev_t dev = ap->a_head.a_dev;
535 lwkt_gettoken(&tty_token);
537 if (tp->t_oproc == 0) {
538 lwkt_reltoken(&tty_token);
541 ret = ((*linesw[tp->t_line].l_write)(tp, ap->a_uio, ap->a_ioflag));
542 lwkt_reltoken(&tty_token);
547 * Start output on pseudo-tty.
548 * Wake up process selecting or sleeping for input from controlling tty.
551 ptsstart(struct tty *tp)
553 lwkt_gettoken(&tty_token);
554 struct pt_ioctl *pti = tp->t_dev->si_drv1;
556 if (tp->t_state & TS_TTSTOP)
559 if (pti->pt_flags & PF_STOPPED) {
560 pti->pt_flags &= ~PF_STOPPED;
561 pti->pt_send = TIOCPKT_START;
564 ptcwakeup(tp, FREAD);
565 lwkt_reltoken(&tty_token);
569 * NOTE: Must be called with tty_token held
572 ptcwakeup(struct tty *tp, int flag)
574 ASSERT_LWKT_TOKEN_HELD(&tty_token);
577 wakeup(TSA_PTC_READ(tp));
578 KNOTE(&tp->t_rkq.ki_note, 0);
581 wakeup(TSA_PTC_WRITE(tp));
582 KNOTE(&tp->t_wkq.ki_note, 0);
587 ptcopen(struct dev_open_args *ap)
589 cdev_t dev = ap->a_head.a_dev;
591 struct pt_ioctl *pti;
594 * The pti will already be assigned by the clone code or
595 * pre-created if a non-unix 98 pty. If si_drv1 is NULL
596 * we are somehow racing a unix98 termination.
598 if (dev->si_drv1 == NULL)
601 lwkt_gettoken(&tty_token);
604 lwkt_reltoken(&tty_token);
607 if (pti->pt_prison && pti->pt_prison != ap->a_cred->cr_prison) {
609 lwkt_reltoken(&tty_token);
615 lwkt_reltoken(&tty_token);
618 tp->t_oproc = ptsstart;
619 tp->t_stop = ptsstop;
620 (void)(*linesw[tp->t_line].l_modem)(tp, 1);
621 tp->t_lflag &= ~EXTPROC;
622 pti->pt_prison = ap->a_cred->cr_prison;
627 pti->devs->si_uid = ap->a_cred->cr_uid;
628 pti->devs->si_gid = 0;
629 pti->devs->si_perms = 0600;
630 pti->devc->si_uid = ap->a_cred->cr_uid;
631 pti->devc->si_gid = 0;
632 pti->devc->si_perms = 0600;
637 * On open of the master, we set the corresponding flag in the common
640 ptydebug(1, "ptcopen=%s (master) | unix98? %s\n", dev->si_name,
641 (pti->pt_flags2 & PF_UNIX98)?"yes":"no");
643 if (pti->pt_flags2 & PF_UNIX98) {
644 pti->pt_flags2 |= PF_MOPEN;
649 lwkt_reltoken(&tty_token);
654 ptcclose(struct dev_close_args *ap)
656 cdev_t dev = ap->a_head.a_dev;
658 struct pt_ioctl *pti = dev->si_drv1;
660 lwkt_gettoken(&tty_token);
662 panic("ptcclose on terminated pti");
665 (void)(*linesw[tp->t_line].l_modem)(tp, 0);
668 * XXX MDMBUF makes no sense for ptys but would inhibit the above
669 * l_modem(). CLOCAL makes sense but isn't supported. Special
670 * l_modem()s that ignore carrier drop make no sense for ptys but
671 * may be in use because other parts of the line discipline make
672 * sense for ptys. Recover by doing everything that a normal
673 * ttymodem() would have done except for sending a SIGHUP.
675 if (tp->t_state & TS_ISOPEN) {
676 tp->t_state &= ~(TS_CARR_ON | TS_CONNECTED);
677 tp->t_state |= TS_ZOMBIE;
678 ttyflush(tp, FREAD | FWRITE);
680 tp->t_oproc = 0; /* mark closed */
685 * On close of the master, we unset the corresponding flag in the common
688 pti->pt_flags2 &= ~PF_MOPEN;
691 pti->pt_prison = NULL;
692 pti->devs->si_uid = 0;
693 pti->devs->si_gid = 0;
694 pti->devs->si_perms = 0666;
695 pti->devc->si_uid = 0;
696 pti->devc->si_gid = 0;
697 pti->devc->si_perms = 0666;
701 lwkt_reltoken(&tty_token);
706 ptcread(struct dev_read_args *ap)
708 cdev_t dev = ap->a_head.a_dev;
709 struct tty *tp = dev->si_tty;
710 struct pt_ioctl *pti = dev->si_drv1;
714 lwkt_gettoken(&tty_token);
716 * We want to block until the slave
717 * is open, and there's something to read;
718 * but if we lost the slave or we're NBIO,
719 * then return the appropriate error instead.
722 if (tp->t_state&TS_ISOPEN) {
723 if (pti->pt_flags&PF_PKT && pti->pt_send) {
724 error = ureadc((int)pti->pt_send, ap->a_uio);
726 lwkt_reltoken(&tty_token);
729 if (pti->pt_send & TIOCPKT_IOCTL) {
730 cc = (int)szmin(ap->a_uio->uio_resid,
731 sizeof(tp->t_termios));
732 uiomove((caddr_t)&tp->t_termios, cc,
736 lwkt_reltoken(&tty_token);
739 if (pti->pt_flags&PF_UCNTL && pti->pt_ucntl) {
740 error = ureadc((int)pti->pt_ucntl, ap->a_uio);
742 lwkt_reltoken(&tty_token);
746 lwkt_reltoken(&tty_token);
749 if (tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0)
752 if ((tp->t_state & TS_CONNECTED) == 0) {
753 lwkt_reltoken(&tty_token);
754 return (0); /* EOF */
756 if (ap->a_ioflag & IO_NDELAY) {
757 lwkt_reltoken(&tty_token);
758 return (EWOULDBLOCK);
760 error = tsleep(TSA_PTC_READ(tp), PCATCH, "ptcin", 0);
762 lwkt_reltoken(&tty_token);
766 if (pti->pt_flags & (PF_PKT|PF_UCNTL))
767 error = ureadc(0, ap->a_uio);
768 while (ap->a_uio->uio_resid > 0 && error == 0) {
769 cc = q_to_b(&tp->t_outq, buf,
770 (int)szmin(ap->a_uio->uio_resid, BUFSIZ));
773 error = uiomove(buf, (size_t)cc, ap->a_uio);
776 lwkt_reltoken(&tty_token);
781 ptsstop(struct tty *tp, int flush)
783 struct pt_ioctl *pti = tp->t_dev->si_drv1;
786 lwkt_gettoken(&tty_token);
787 /* note: FLUSHREAD and FLUSHWRITE already ok */
790 flush = TIOCPKT_STOP;
791 pti->pt_flags |= PF_STOPPED;
793 pti->pt_flags &= ~PF_STOPPED;
795 pti->pt_send |= flush;
796 /* change of perspective */
805 lwkt_reltoken(&tty_token);
809 * kqueue ops for pseudo-terminals.
811 static struct filterops ptcread_filtops =
812 { FILTEROP_ISFD, NULL, filt_ptcrdetach, filt_ptcread };
813 static struct filterops ptcwrite_filtops =
814 { FILTEROP_ISFD, NULL, filt_ptcwdetach, filt_ptcwrite };
817 ptckqfilter(struct dev_kqfilter_args *ap)
819 cdev_t dev = ap->a_head.a_dev;
820 struct knote *kn = ap->a_kn;
821 struct tty *tp = dev->si_tty;
824 lwkt_gettoken(&tty_token);
826 switch (kn->kn_filter) {
828 klist = &tp->t_rkq.ki_note;
829 kn->kn_fop = &ptcread_filtops;
832 klist = &tp->t_wkq.ki_note;
833 kn->kn_fop = &ptcwrite_filtops;
836 ap->a_result = EOPNOTSUPP;
837 lwkt_reltoken(&tty_token);
841 kn->kn_hook = (caddr_t)dev;
842 knote_insert(klist, kn);
843 lwkt_reltoken(&tty_token);
848 filt_ptcread (struct knote *kn, long hint)
850 struct tty *tp = ((cdev_t)kn->kn_hook)->si_tty;
851 struct pt_ioctl *pti = ((cdev_t)kn->kn_hook)->si_drv1;
853 lwkt_gettoken(&tty_token);
854 if (tp->t_state & TS_ZOMBIE) {
855 kn->kn_flags |= EV_EOF;
856 lwkt_reltoken(&tty_token);
860 if ((tp->t_state & TS_ISOPEN) &&
861 ((tp->t_outq.c_cc && (tp->t_state & TS_TTSTOP) == 0) ||
862 ((pti->pt_flags & PF_PKT) && pti->pt_send) ||
863 ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl))) {
864 kn->kn_data = tp->t_outq.c_cc;
865 lwkt_reltoken(&tty_token);
868 lwkt_reltoken(&tty_token);
874 filt_ptcwrite (struct knote *kn, long hint)
876 struct tty *tp = ((cdev_t)kn->kn_hook)->si_tty;
877 struct pt_ioctl *pti = ((cdev_t)kn->kn_hook)->si_drv1;
879 lwkt_gettoken(&tty_token);
880 if (tp->t_state & TS_ZOMBIE) {
881 kn->kn_flags |= EV_EOF;
882 lwkt_reltoken(&tty_token);
886 if (tp->t_state & TS_ISOPEN &&
887 ((pti->pt_flags & PF_REMOTE) ?
888 (tp->t_canq.c_cc == 0) :
889 ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG - 2) ||
890 (tp->t_canq.c_cc == 0 && (tp->t_lflag & ICANON))))) {
891 kn->kn_data = tp->t_canq.c_cc + tp->t_rawq.c_cc;
892 lwkt_reltoken(&tty_token);
895 lwkt_reltoken(&tty_token);
902 filt_ptcrdetach (struct knote *kn)
904 struct tty *tp = ((cdev_t)kn->kn_hook)->si_tty;
906 knote_remove(&tp->t_rkq.ki_note, kn);
910 filt_ptcwdetach (struct knote *kn)
912 struct tty *tp = ((cdev_t)kn->kn_hook)->si_tty;
914 knote_remove(&tp->t_wkq.ki_note, kn);
921 ptcwrite(struct dev_write_args *ap)
923 cdev_t dev = ap->a_head.a_dev;
924 struct tty *tp = dev->si_tty;
927 u_char locbuf[BUFSIZ];
929 struct pt_ioctl *pti = dev->si_drv1;
932 lwkt_gettoken(&tty_token);
934 if ((tp->t_state&TS_ISOPEN) == 0)
936 if (pti->pt_flags & PF_REMOTE) {
939 while ((ap->a_uio->uio_resid > 0 || cc > 0) &&
940 tp->t_canq.c_cc < TTYHOG - 1) {
942 cc = (int)szmin(ap->a_uio->uio_resid, BUFSIZ);
943 cc = imin(cc, TTYHOG - 1 - tp->t_canq.c_cc);
945 error = uiomove(cp, (size_t)cc, ap->a_uio);
947 lwkt_reltoken(&tty_token);
950 /* check again for safety */
951 if ((tp->t_state & TS_ISOPEN) == 0) {
952 /* adjust as usual */
953 ap->a_uio->uio_resid += cc;
954 lwkt_reltoken(&tty_token);
959 cc = b_to_q((char *)cp, cc, &tp->t_canq);
961 * XXX we don't guarantee that the canq size
962 * is >= TTYHOG, so the above b_to_q() may
963 * leave some bytes uncopied. However, space
964 * is guaranteed for the null terminator if
965 * we don't fail here since (TTYHOG - 1) is
966 * not a multiple of CBSIZE.
972 /* adjust for data copied in but not written */
973 ap->a_uio->uio_resid += cc;
974 clist_putc(0, &tp->t_canq);
976 wakeup(TSA_PTS_READ(tp));
977 lwkt_reltoken(&tty_token);
980 while (ap->a_uio->uio_resid > 0 || cc > 0) {
982 cc = (int)szmin(ap->a_uio->uio_resid, BUFSIZ);
984 error = uiomove(cp, (size_t)cc, ap->a_uio);
986 lwkt_reltoken(&tty_token);
989 /* check again for safety */
990 if ((tp->t_state & TS_ISOPEN) == 0) {
991 /* adjust for data copied in but not written */
992 ap->a_uio->uio_resid += cc;
993 lwkt_reltoken(&tty_token);
998 if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 &&
999 (tp->t_canq.c_cc > 0 || !(tp->t_lflag&ICANON))) {
1000 wakeup(TSA_HUP_OR_INPUT(tp));
1003 (*linesw[tp->t_line].l_rint)(*cp++, tp);
1009 lwkt_reltoken(&tty_token);
1013 * Come here to wait for slave to open, for space
1014 * in outq, or space in rawq, or an empty canq.
1016 if ((tp->t_state & TS_CONNECTED) == 0) {
1017 /* adjust for data copied in but not written */
1018 ap->a_uio->uio_resid += cc;
1019 lwkt_reltoken(&tty_token);
1022 if (ap->a_ioflag & IO_NDELAY) {
1023 /* adjust for data copied in but not written */
1024 ap->a_uio->uio_resid += cc;
1026 lwkt_reltoken(&tty_token);
1027 return (EWOULDBLOCK);
1029 lwkt_reltoken(&tty_token);
1032 error = tsleep(TSA_PTC_WRITE(tp), PCATCH, "ptcout", 0);
1034 /* adjust for data copied in but not written */
1035 ap->a_uio->uio_resid += cc;
1036 lwkt_reltoken(&tty_token);
1044 ptyioctl(struct dev_ioctl_args *ap)
1046 cdev_t dev = ap->a_head.a_dev;
1047 struct tty *tp = dev->si_tty;
1048 struct pt_ioctl *pti = dev->si_drv1;
1049 u_char *cc = tp->t_cc;
1052 lwkt_gettoken(&tty_token);
1053 if (dev_dflags(dev) & D_MASTER) {
1054 switch (ap->a_cmd) {
1058 * We avoid calling ttioctl on the controller since,
1059 * in that case, tp must be the controlling terminal.
1061 *(int *)ap->a_data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
1062 lwkt_reltoken(&tty_token);
1066 if (*(int *)ap->a_data) {
1067 if (pti->pt_flags & PF_UCNTL) {
1068 lwkt_reltoken(&tty_token);
1071 pti->pt_flags |= PF_PKT;
1073 pti->pt_flags &= ~PF_PKT;
1074 lwkt_reltoken(&tty_token);
1078 if (*(int *)ap->a_data) {
1079 if (pti->pt_flags & PF_PKT) {
1080 lwkt_reltoken(&tty_token);
1083 pti->pt_flags |= PF_UCNTL;
1085 pti->pt_flags &= ~PF_UCNTL;
1086 lwkt_reltoken(&tty_token);
1090 if (*(int *)ap->a_data)
1091 pti->pt_flags |= PF_REMOTE;
1093 pti->pt_flags &= ~PF_REMOTE;
1094 ttyflush(tp, FREAD|FWRITE);
1095 lwkt_reltoken(&tty_token);
1099 case TIOCISPTMASTER:
1100 if ((pti->pt_flags2 & PF_UNIX98) && (pti->devc == dev)) {
1101 lwkt_reltoken(&tty_token);
1104 lwkt_reltoken(&tty_token);
1111 * The rest of the ioctls shouldn't be called until
1112 * the slave is open.
1114 if ((tp->t_state & TS_ISOPEN) == 0) {
1115 lwkt_reltoken(&tty_token);
1119 switch (ap->a_cmd) {
1129 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
1130 * ttywflush(tp) will hang if there are characters in
1133 ndflush(&tp->t_outq, tp->t_outq.c_cc);
1137 if (*(unsigned int *)ap->a_data >= NSIG ||
1138 *(unsigned int *)ap->a_data == 0) {
1139 lwkt_reltoken(&tty_token);
1142 if ((tp->t_lflag&NOFLSH) == 0)
1143 ttyflush(tp, FREAD|FWRITE);
1144 pgsignal(tp->t_pgrp, *(unsigned int *)ap->a_data, 1);
1145 if ((*(unsigned int *)ap->a_data == SIGINFO) &&
1146 ((tp->t_lflag&NOKERNINFO) == 0))
1148 lwkt_reltoken(&tty_token);
1152 if (ap->a_cmd == TIOCEXT) {
1154 * When the EXTPROC bit is being toggled, we need
1155 * to send an TIOCPKT_IOCTL if the packet driver
1158 if (*(int *)ap->a_data) {
1159 if (pti->pt_flags & PF_PKT) {
1160 pti->pt_send |= TIOCPKT_IOCTL;
1161 ptcwakeup(tp, FREAD);
1163 tp->t_lflag |= EXTPROC;
1165 if ((tp->t_lflag & EXTPROC) &&
1166 (pti->pt_flags & PF_PKT)) {
1167 pti->pt_send |= TIOCPKT_IOCTL;
1168 ptcwakeup(tp, FREAD);
1170 tp->t_lflag &= ~EXTPROC;
1172 lwkt_reltoken(&tty_token);
1175 error = (*linesw[tp->t_line].l_ioctl)(tp, ap->a_cmd, ap->a_data,
1176 ap->a_fflag, ap->a_cred);
1177 if (error == ENOIOCTL)
1178 error = ttioctl(tp, ap->a_cmd, ap->a_data, ap->a_fflag);
1179 if (error == ENOIOCTL) {
1180 if (pti->pt_flags & PF_UCNTL &&
1181 (ap->a_cmd & ~0xff) == UIOCCMD(0)) {
1182 if (ap->a_cmd & 0xff) {
1183 pti->pt_ucntl = (u_char)ap->a_cmd;
1184 ptcwakeup(tp, FREAD);
1186 lwkt_reltoken(&tty_token);
1192 * If external processing and packet mode send ioctl packet.
1194 if ((tp->t_lflag&EXTPROC) && (pti->pt_flags & PF_PKT)) {
1203 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1210 pti->pt_send |= TIOCPKT_IOCTL;
1211 ptcwakeup(tp, FREAD);
1216 stop = (tp->t_iflag & IXON) && CCEQ(cc[VSTOP], CTRL('s'))
1217 && CCEQ(cc[VSTART], CTRL('q'));
1218 if (pti->pt_flags & PF_NOSTOP) {
1220 pti->pt_send &= ~TIOCPKT_NOSTOP;
1221 pti->pt_send |= TIOCPKT_DOSTOP;
1222 pti->pt_flags &= ~PF_NOSTOP;
1223 ptcwakeup(tp, FREAD);
1227 pti->pt_send &= ~TIOCPKT_DOSTOP;
1228 pti->pt_send |= TIOCPKT_NOSTOP;
1229 pti->pt_flags |= PF_NOSTOP;
1230 ptcwakeup(tp, FREAD);
1233 lwkt_reltoken(&tty_token);
1238 static void ptc_drvinit (void *unused);
1241 SYSCTL_INT(_kern, OID_AUTO, pty_debug, CTLFLAG_RW, &pty_debug_level,
1242 0, "Change pty debug level");
1246 ptc_drvinit(void *unused)
1253 * Create the clonable base device.
1255 make_autoclone_dev(&ptc_ops, &DEVFS_CLONE_BITMAP(pty), ptyclone,
1256 0, 0, 0666, "ptmx");
1259 for (i = 0; i < 256; i++) {
1264 SYSINIT(ptcdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR_C,ptc_drvinit,NULL)