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 $
36 * Most functions here could use a separate lock to deal with concurrent
37 * access to the 'pt's.
41 * Pseudo-teletype Driver
42 * (Actually two drivers, requiring two dev_ops structures)
45 #include <sys/param.h>
46 #include <sys/systm.h>
50 #include <sys/ttydefaults.h> /* for TTYDEF_* */
52 #include <sys/fcntl.h>
53 #include <sys/kernel.h>
54 #include <sys/vnode.h>
55 #include <sys/signalvar.h>
56 #include <sys/malloc.h>
57 #include <sys/device.h>
58 #include <sys/devfs.h>
60 #include <sys/sysctl.h>
62 MALLOC_DEFINE(M_PTY, "ptys", "pty data structures");
64 static void ptsstart (struct tty *tp);
65 static void ptsstop (struct tty *tp, int rw);
66 static void ptsunhold (struct tty *tp);
67 static void ptcwakeup (struct tty *tp, int flag);
68 static void ptyinit (int n);
69 static int filt_ptcread (struct knote *kn, long hint);
70 static void filt_ptcrdetach (struct knote *kn);
71 static int filt_ptcwrite (struct knote *kn, long hint);
72 static void filt_ptcwdetach (struct knote *kn);
74 static d_open_t ptsopen;
75 static d_close_t ptsclose;
76 static d_read_t ptsread;
77 static d_write_t ptswrite;
78 static d_ioctl_t ptyioctl;
79 static d_open_t ptcopen;
80 static d_close_t ptcclose;
81 static d_read_t ptcread;
82 static d_write_t ptcwrite;
83 static d_kqfilter_t ptckqfilter;
85 DEVFS_DEFINE_CLONE_BITMAP(pty);
86 static struct pt_ioctl **ptis; /* keep pti's intact */
88 static d_clone_t ptyclone;
90 static int pty_debug_level = 0;
92 static struct dev_ops pts98_ops = {
93 { "pts98", 0, D_TTY | D_MPSAFE },
99 .d_kqfilter = ttykqfilter,
100 .d_revoke = ttyrevoke
103 static struct dev_ops ptc98_ops = {
104 { "ptc98", 0, D_TTY | D_MASTER | D_MPSAFE },
110 .d_kqfilter = ptckqfilter,
111 .d_revoke = ttyrevoke
114 static struct dev_ops pts_ops = {
115 { "pts", 0, D_TTY | D_MPSAFE },
121 .d_kqfilter = ttykqfilter,
122 .d_revoke = ttyrevoke
125 #define CDEV_MAJOR_C 6
126 static struct dev_ops ptc_ops = {
127 { "ptc", 0, D_TTY | D_MASTER | D_MPSAFE },
133 .d_kqfilter = ptckqfilter,
134 .d_revoke = ttyrevoke
137 #define BUFSIZ 100 /* Chunk size iomoved to/from user */
139 #define MAXPTYS 1000 /* Maximum cloneable ptys */
143 int pt_refs; /* Structural references interlock S/MOPEN */
145 struct kqinfo pt_kqr, pt_kqw;
150 struct prison *pt_prison;
156 #define PF_PKT 0x0008 /* packet mode */
157 #define PF_STOPPED 0x0010 /* user told stopped */
158 #define PF_REMOTE 0x0020 /* remote and flow controlled input */
159 #define PF_NOSTOP 0x0040
160 #define PF_UCNTL 0x0080 /* user control mode */
162 #define PF_PTCSTATEMASK 0x00FF
165 * pt_flags open state. Note that PF_SCLOSED is used to activate
166 * read EOF on the ptc so it is only set after the slave has been
167 * opened and then closed, and cleared again if the slave is opened
170 #define PF_UNIX98 0x0100
171 #define PF_SOPEN 0x0200
172 #define PF_MOPEN 0x0400
173 #define PF_SCLOSED 0x0800
174 #define PF_TERMINATED 0x8000
177 * This function creates and initializes a pts/ptc pair
179 * pts == /dev/tty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
180 * ptc == /dev/pty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
182 * XXX: define and add mapping of upper minor bits to allow more
189 char *names = "pqrsPQRS";
190 struct pt_ioctl *pti;
192 /* For now we only map the lower 8 bits of the minor */
196 pti = kmalloc(sizeof(*pti), M_PTY, M_WAITOK | M_ZERO);
197 pti->devs = devs = make_dev(&pts_ops, n, 0, 0, 0666,
199 names[n / 32], hex2ascii(n % 32));
200 pti->devc = devc = make_dev(&ptc_ops, n, 0, 0, 0666,
202 names[n / 32], hex2ascii(n % 32));
204 pti->pt_tty.t_dev = devs;
206 devs->si_drv1 = devc->si_drv1 = pti;
207 devs->si_tty = devc->si_tty = &pti->pt_tty;
208 devs->si_flags |= SI_OVERRIDE; /* uid, gid, perms from dev */
209 devc->si_flags |= SI_OVERRIDE; /* uid, gid, perms from dev */
210 ttyinit(&pti->pt_tty);
211 ttyregister(&pti->pt_tty);
215 ptyclone(struct dev_clone_args *ap)
218 struct pt_ioctl *pti;
221 * Limit the number of unix98 pty (slave) devices to 1000 for now.
223 * If this limit is reached, we don't clone and return an error
226 unit = devfs_clone_bitmap_get(&DEVFS_CLONE_BITMAP(pty), MAXPTYS);
234 * pti structures must be persistent once allocated.
236 if ((pti = ptis[unit]) == NULL) {
237 lwkt_gettoken(&tty_token);
238 pti = kmalloc(sizeof(*pti), M_PTY, M_WAITOK | M_ZERO);
239 if (ptis[unit] == NULL) {
241 ttyinit(&pti->pt_tty);
245 lwkt_reltoken(&tty_token);
249 * The cloning bitmap should guarantee isolation during
252 pti->devc = make_only_dev(&ptc98_ops, unit,
254 0, 0600, "ptm/%d", unit);
255 pti->devs = make_dev(&pts98_ops, unit,
257 GID_TTY, 0620, "pts/%d", unit);
258 ap->a_dev = pti->devc;
260 pti->devs->si_flags |= SI_OVERRIDE; /* uid, gid, perms from dev */
261 pti->devc->si_flags |= SI_OVERRIDE; /* uid, gid, perms from dev */
263 pti->pt_tty.t_dev = pti->devs;
264 pti->pt_flags = PF_UNIX98;
265 pti->pt_uminor = unit;
266 pti->devs->si_drv1 = pti->devc->si_drv1 = pti;
267 pti->devs->si_tty = pti->devc->si_tty = &pti->pt_tty;
268 ttyregister(&pti->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
281 pti_hold(struct pt_ioctl *pti)
283 if (pti->pt_flags & PF_TERMINATED)
291 * pti_done() releases the reference and checks to see if both sides have
292 * been closed on a unix98 pty, allowing us to destroy the device and
295 * We do not release resources on non-unix98 ptys. Those are left
296 * statically allocated.
299 pti_done(struct pt_ioctl *pti)
301 lwkt_gettoken(&pti->pt_tty.t_token);
302 if (--pti->pt_refs == 0) {
307 * Only unix09 ptys are freed up (the pti structure itself
308 * is never freed, regardless).
310 if ((pti->pt_flags & PF_UNIX98) == 0) {
311 lwkt_reltoken(&pti->pt_tty.t_token);
316 * Interlock open attempts against termination by setting
317 * PF_TERMINATED. This allows us to block while cleaning
318 * out the device infrastructure.
320 * Do not terminate the tty if it still has a session
321 * association (t_refs).
323 if ((pti->pt_flags & (PF_SOPEN|PF_MOPEN)) == 0 &&
324 pti->pt_tty.t_refs == 0) {
325 pti->pt_flags |= PF_TERMINATED;
326 uminor_no = pti->pt_uminor;
328 if ((dev = pti->devs) != NULL) {
333 if ((dev = pti->devc) != NULL) {
338 ttyunregister(&pti->pt_tty);
339 pti->pt_tty.t_dev = NULL;
341 devfs_clone_bitmap_put(&DEVFS_CLONE_BITMAP(pty),
343 /* pti structure remains intact */
346 lwkt_reltoken(&pti->pt_tty.t_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(&pti->pt_tty.t_token);
369 lwkt_reltoken(&pti->pt_tty.t_token);
376 * Reinit most of the tty state if it isn't open. Handle
379 if ((tp->t_state & TS_ISOPEN) == 0) {
380 ttychars(tp); /* Set up default chars */
381 tp->t_iflag = TTYDEF_IFLAG;
382 tp->t_oflag = TTYDEF_OFLAG;
383 tp->t_lflag = TTYDEF_LFLAG;
384 tp->t_cflag = TTYDEF_CFLAG;
385 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
386 } else if ((tp->t_state & TS_XCLUDE) &&
387 priv_check_cred(ap->a_cred, PRIV_ROOT, 0)) {
389 lwkt_reltoken(&pti->pt_tty.t_token);
391 } else if (pti->pt_prison != ap->a_cred->cr_prison) {
393 lwkt_reltoken(&pti->pt_tty.t_token);
398 * If the ptc is already present this will connect us up. It
399 * is unclear if this is actually needed.
401 * If neither side is open be sure to clear any left over
402 * ZOMBIE state before continuing.
405 (void)(*linesw[tp->t_line].l_modem)(tp, 1);
406 else if ((pti->pt_flags & PF_SOPEN) == 0)
407 tp->t_state &= ~TS_ZOMBIE;
410 * Wait for the carrier (ptc side)
412 while ((tp->t_state & TS_CARR_ON) == 0) {
413 if (ap->a_oflags & FNONBLOCK)
415 error = ttysleep(tp, TSA_CARR_ON(tp), PCATCH, "ptsopn", 0);
418 lwkt_reltoken(&pti->pt_tty.t_token);
424 * Mark the tty open and mark the slave side as being open.
426 error = (*linesw[tp->t_line].l_open)(dev, tp);
429 pti->pt_flags |= PF_SOPEN;
430 pti->pt_flags &= ~PF_SCLOSED;
431 ptcwakeup(tp, FREAD|FWRITE);
434 lwkt_reltoken(&pti->pt_tty.t_token);
440 ptsclose(struct dev_close_args *ap)
442 cdev_t dev = ap->a_head.a_dev;
444 struct pt_ioctl *pti = dev->si_drv1;
447 lwkt_gettoken(&pti->pt_tty.t_token);
449 panic("ptsclose on terminated pti");
452 * Disconnect the slave side
455 err = (*linesw[tp->t_line].l_close)(tp, ap->a_fflag);
456 ptsstop(tp, FREAD|FWRITE);
457 ttyclose(tp); /* clears t_state */
460 * Mark the pts side closed and signal the ptc. Do not mark the
461 * tty a zombie... that is, allow the tty to be re-opened as long
462 * as the ptc is still open. The ptc will read() EOFs until the
463 * pts side is reopened or the ptc is closed.
465 * xterm() depends on this behavior as it will revoke() the pts
466 * and then reopen it after the (unnecessary old code) chmod.
468 pti->pt_flags &= ~PF_SOPEN;
469 pti->pt_flags |= PF_SCLOSED;
471 ptcwakeup(tp, FREAD);
473 lwkt_reltoken(&pti->pt_tty.t_token);
478 ptsread(struct dev_read_args *ap)
480 cdev_t dev = ap->a_head.a_dev;
481 struct proc *p = curproc;
482 struct tty *tp = dev->si_tty;
483 struct pt_ioctl *pti = dev->si_drv1;
488 lp = curthread->td_lwp;
490 lwkt_gettoken(&pti->pt_tty.t_token);
492 if (pti->pt_flags & PF_REMOTE) {
493 while (isbackground(p, tp)) {
494 if (SIGISMEMBER(p->p_sigignore, SIGTTIN) ||
495 SIGISMEMBER(lp->lwp_sigmask, SIGTTIN) ||
496 p->p_pgrp->pg_jobc == 0 ||
497 (p->p_flags & P_PPWAIT)) {
498 lwkt_reltoken(&pti->pt_tty.t_token);
501 pgsignal(p->p_pgrp, SIGTTIN, 1);
502 error = ttysleep(tp, &lbolt, PCATCH, "ptsbg", 0);
504 lwkt_reltoken(&pti->pt_tty.t_token);
508 if (tp->t_canq.c_cc == 0) {
509 if (ap->a_ioflag & IO_NDELAY) {
510 lwkt_reltoken(&pti->pt_tty.t_token);
511 return (EWOULDBLOCK);
513 error = ttysleep(tp, TSA_PTS_READ(tp), PCATCH,
516 lwkt_reltoken(&pti->pt_tty.t_token);
521 while (tp->t_canq.c_cc > 1 && ap->a_uio->uio_resid > 0)
522 if (ureadc(clist_getc(&tp->t_canq), ap->a_uio) < 0) {
526 if (tp->t_canq.c_cc == 1)
527 clist_getc(&tp->t_canq);
528 if (tp->t_canq.c_cc) {
529 lwkt_reltoken(&pti->pt_tty.t_token);
534 error = (*linesw[tp->t_line].l_read)(tp, ap->a_uio, ap->a_ioflag);
535 ptcwakeup(tp, FWRITE);
536 lwkt_reltoken(&pti->pt_tty.t_token);
542 * Write to pseudo-tty.
543 * Wakeups of controlling tty will happen
544 * indirectly, when tty driver calls ptsstart.
547 ptswrite(struct dev_write_args *ap)
549 cdev_t dev = ap->a_head.a_dev;
554 lwkt_gettoken(&tp->t_token);
555 if (tp->t_oproc == NULL) {
556 lwkt_reltoken(&tp->t_token);
559 ret = ((*linesw[tp->t_line].l_write)(tp, ap->a_uio, ap->a_ioflag));
560 lwkt_reltoken(&tp->t_token);
566 * Start output on pseudo-tty.
567 * Wake up process selecting or sleeping for input from controlling tty.
570 ptsstart(struct tty *tp)
572 struct pt_ioctl *pti = tp->t_dev->si_drv1;
574 lwkt_gettoken(&pti->pt_tty.t_token);
575 lwkt_gettoken(&tp->t_token);
576 if (tp->t_state & TS_TTSTOP) {
577 lwkt_reltoken(&tp->t_token);
578 lwkt_reltoken(&pti->pt_tty.t_token);
582 if (pti->pt_flags & PF_STOPPED) {
583 pti->pt_flags &= ~PF_STOPPED;
584 pti->pt_send = TIOCPKT_START;
587 ptcwakeup(tp, FREAD);
588 lwkt_reltoken(&tp->t_token);
589 lwkt_reltoken(&pti->pt_tty.t_token);
593 * NOTE: Must be called with tp->t_token held
596 ptcwakeup(struct tty *tp, int flag)
599 wakeup(TSA_PTC_READ(tp));
600 KNOTE(&tp->t_rkq.ki_note, 0);
603 wakeup(TSA_PTC_WRITE(tp));
604 KNOTE(&tp->t_wkq.ki_note, 0);
609 ptcopen(struct dev_open_args *ap)
611 cdev_t dev = ap->a_head.a_dev;
613 struct pt_ioctl *pti;
616 * The pti will already be assigned by the clone code or
617 * pre-created if a non-unix 98 pty. If si_drv1 is NULL
618 * we are somehow racing a unix98 termination.
624 lwkt_gettoken(&pti->pt_tty.t_token);
626 lwkt_reltoken(&pti->pt_tty.t_token);
629 if (pti->pt_prison && pti->pt_prison != ap->a_cred->cr_prison) {
631 lwkt_reltoken(&pti->pt_tty.t_token);
635 lwkt_gettoken(&tp->t_token);
638 lwkt_reltoken(&tp->t_token);
639 lwkt_reltoken(&pti->pt_tty.t_token);
644 * If the slave side is not yet open clear any left over zombie
645 * state before doing our modem control.
647 if ((pti->pt_flags & PF_SOPEN) == 0)
648 tp->t_state &= ~TS_ZOMBIE;
650 tp->t_oproc = ptsstart;
651 tp->t_stop = ptsstop;
652 tp->t_unhold = ptsunhold;
657 (void)(*linesw[tp->t_line].l_modem)(tp, 1);
659 tp->t_lflag &= ~EXTPROC;
660 pti->pt_prison = ap->a_cred->cr_prison;
661 pti->pt_flags &= ~PF_PTCSTATEMASK;
665 pti->devs->si_uid = ap->a_cred->cr_uid;
666 pti->devs->si_gid = ap->a_cred->cr_uid ? GID_TTY : 0;
667 pti->devs->si_perms = 0600;
668 pti->devc->si_uid = ap->a_cred->cr_uid;
669 pti->devc->si_gid = 0;
670 pti->devc->si_perms = 0600;
673 * Mark master side open. This does not cause any events
676 pti->pt_flags |= PF_MOPEN;
679 lwkt_reltoken(&tp->t_token);
680 lwkt_reltoken(&pti->pt_tty.t_token);
686 ptcclose(struct dev_close_args *ap)
688 cdev_t dev = ap->a_head.a_dev;
690 struct pt_ioctl *pti = dev->si_drv1;
692 lwkt_gettoken(&pti->pt_tty.t_token);
694 lwkt_reltoken(&pti->pt_tty.t_token);
695 panic("ptcclose on terminated pti");
698 lwkt_gettoken(&tp->t_token);
700 (void)(*linesw[tp->t_line].l_modem)(tp, 0);
703 * Mark the master side closed. If the slave is still open
704 * mark the tty ZOMBIE, preventing any new action until both
707 * NOTE: The ttyflush() will wake up the slave once we've
708 * set appropriate flags. The ZOMBIE flag will be
709 * cleared when the slave side is closed.
711 pti->pt_flags &= ~PF_MOPEN;
712 if (pti->pt_flags & PF_SOPEN)
713 tp->t_state |= TS_ZOMBIE;
716 * Turn off the carrier and disconnect. This will notify the slave
719 if (tp->t_state & TS_ISOPEN) {
720 tp->t_state &= ~(TS_CARR_ON | TS_CONNECTED);
721 ttyflush(tp, FREAD | FWRITE);
723 tp->t_oproc = NULL; /* mark closed */
725 pti->pt_prison = NULL;
726 pti->devs->si_uid = 0;
727 pti->devs->si_gid = 0;
728 pti->devs->si_perms = 0666;
729 pti->devc->si_uid = 0;
730 pti->devc->si_gid = 0;
731 pti->devc->si_perms = 0666;
734 lwkt_reltoken(&tp->t_token);
735 lwkt_reltoken(&pti->pt_tty.t_token);
741 ptcread(struct dev_read_args *ap)
743 cdev_t dev = ap->a_head.a_dev;
744 struct tty *tp = dev->si_tty;
745 struct pt_ioctl *pti = dev->si_drv1;
749 lwkt_gettoken(&pti->pt_tty.t_token);
750 lwkt_gettoken(&tp->t_token);
753 * We want to block until the slave
754 * is open, and there's something to read;
755 * but if we lost the slave or we're NBIO,
756 * then return the appropriate error instead.
759 if (tp->t_state&TS_ISOPEN) {
760 if ((pti->pt_flags & PF_PKT) && pti->pt_send) {
761 error = ureadc((int)pti->pt_send, ap->a_uio);
763 lwkt_reltoken(&tp->t_token);
764 lwkt_reltoken(&pti->pt_tty.t_token);
767 if (pti->pt_send & TIOCPKT_IOCTL) {
768 cc = (int)szmin(ap->a_uio->uio_resid,
769 sizeof(tp->t_termios));
770 uiomove((caddr_t)&tp->t_termios, cc,
774 lwkt_reltoken(&tp->t_token);
775 lwkt_reltoken(&pti->pt_tty.t_token);
779 if ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl) {
780 error = ureadc((int)pti->pt_ucntl, ap->a_uio);
782 lwkt_reltoken(&tp->t_token);
783 lwkt_reltoken(&pti->pt_tty.t_token);
787 lwkt_reltoken(&tp->t_token);
788 lwkt_reltoken(&pti->pt_tty.t_token);
792 if (tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0)
795 if ((tp->t_state & TS_CONNECTED) == 0) {
796 lwkt_reltoken(&tp->t_token);
797 lwkt_reltoken(&pti->pt_tty.t_token);
798 return (0); /* EOF */
800 if (ap->a_ioflag & IO_NDELAY) {
801 lwkt_reltoken(&tp->t_token);
802 lwkt_reltoken(&pti->pt_tty.t_token);
803 return (EWOULDBLOCK);
805 error = tsleep(TSA_PTC_READ(tp), PCATCH, "ptcin", 0);
807 lwkt_reltoken(&tp->t_token);
808 lwkt_reltoken(&pti->pt_tty.t_token);
812 if (pti->pt_flags & (PF_PKT|PF_UCNTL))
813 error = ureadc(0, ap->a_uio);
814 while (ap->a_uio->uio_resid > 0 && error == 0) {
815 cc = clist_qtob(&tp->t_outq, buf,
816 (int)szmin(ap->a_uio->uio_resid, BUFSIZ));
819 error = uiomove(buf, (size_t)cc, ap->a_uio);
822 lwkt_reltoken(&tp->t_token);
823 lwkt_reltoken(&pti->pt_tty.t_token);
829 ptsstop(struct tty *tp, int flush)
831 struct pt_ioctl *pti = tp->t_dev->si_drv1;
834 lwkt_gettoken(&pti->pt_tty.t_token);
835 /* note: FLUSHREAD and FLUSHWRITE already ok */
838 flush = TIOCPKT_STOP;
839 pti->pt_flags |= PF_STOPPED;
841 pti->pt_flags &= ~PF_STOPPED;
843 pti->pt_send |= flush;
844 /* change of perspective */
853 lwkt_reltoken(&pti->pt_tty.t_token);
857 * ttyunhold() calls us instead of just decrementing tp->t_refs. This
858 * is needed because a session can hold onto a pts (half closed state)
859 * even if there are no live file descriptors. Without the callback
863 ptsunhold(struct tty *tp)
865 struct pt_ioctl *pti = tp->t_dev->si_drv1;
867 lwkt_gettoken(&pti->pt_tty.t_token);
868 lwkt_gettoken(&tp->t_token);
872 lwkt_reltoken(&tp->t_token);
873 lwkt_reltoken(&pti->pt_tty.t_token);
877 * kqueue ops for pseudo-terminals.
879 static struct filterops ptcread_filtops =
880 { FILTEROP_ISFD|FILTEROP_MPSAFE, NULL, filt_ptcrdetach, filt_ptcread };
881 static struct filterops ptcwrite_filtops =
882 { FILTEROP_ISFD|FILTEROP_MPSAFE, NULL, filt_ptcwdetach, filt_ptcwrite };
885 ptckqfilter(struct dev_kqfilter_args *ap)
887 cdev_t dev = ap->a_head.a_dev;
888 struct knote *kn = ap->a_kn;
889 struct tty *tp = dev->si_tty;
893 switch (kn->kn_filter) {
895 klist = &tp->t_rkq.ki_note;
896 kn->kn_fop = &ptcread_filtops;
899 klist = &tp->t_wkq.ki_note;
900 kn->kn_fop = &ptcwrite_filtops;
903 ap->a_result = EOPNOTSUPP;
907 kn->kn_hook = (caddr_t)dev;
908 knote_insert(klist, kn);
913 filt_ptcread (struct knote *kn, long hint)
915 struct tty *tp = ((cdev_t)kn->kn_hook)->si_tty;
916 struct pt_ioctl *pti = ((cdev_t)kn->kn_hook)->si_drv1;
918 lwkt_gettoken(&pti->pt_tty.t_token);
919 lwkt_gettoken(&tp->t_token);
921 if ((tp->t_state & TS_ZOMBIE) || (pti->pt_flags & PF_SCLOSED)) {
922 kn->kn_flags |= (EV_EOF | EV_NODATA);
923 lwkt_reltoken(&tp->t_token);
924 lwkt_reltoken(&pti->pt_tty.t_token);
928 if ((tp->t_state & TS_ISOPEN) &&
929 ((tp->t_outq.c_cc && (tp->t_state & TS_TTSTOP) == 0) ||
930 ((pti->pt_flags & PF_PKT) && pti->pt_send) ||
931 ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl))) {
932 kn->kn_data = tp->t_outq.c_cc;
933 lwkt_reltoken(&tp->t_token);
934 lwkt_reltoken(&pti->pt_tty.t_token);
937 lwkt_reltoken(&tp->t_token);
938 lwkt_reltoken(&pti->pt_tty.t_token);
944 filt_ptcwrite (struct knote *kn, long hint)
946 struct tty *tp = ((cdev_t)kn->kn_hook)->si_tty;
947 struct pt_ioctl *pti = ((cdev_t)kn->kn_hook)->si_drv1;
949 lwkt_gettoken(&pti->pt_tty.t_token);
950 lwkt_gettoken(&tp->t_token);
951 if (tp->t_state & TS_ZOMBIE) {
952 lwkt_reltoken(&tp->t_token);
953 lwkt_reltoken(&pti->pt_tty.t_token);
954 kn->kn_flags |= (EV_EOF | EV_NODATA);
958 if (tp->t_state & TS_ISOPEN &&
959 ((pti->pt_flags & PF_REMOTE) ?
960 (tp->t_canq.c_cc == 0) :
961 ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG - 2) ||
962 (tp->t_canq.c_cc == 0 && (tp->t_lflag & ICANON))))) {
963 kn->kn_data = tp->t_canq.c_cc + tp->t_rawq.c_cc;
964 lwkt_reltoken(&tp->t_token);
965 lwkt_reltoken(&pti->pt_tty.t_token);
968 lwkt_reltoken(&tp->t_token);
969 lwkt_reltoken(&pti->pt_tty.t_token);
976 filt_ptcrdetach (struct knote *kn)
978 struct tty *tp = ((cdev_t)kn->kn_hook)->si_tty;
980 knote_remove(&tp->t_rkq.ki_note, kn);
984 filt_ptcwdetach (struct knote *kn)
986 struct tty *tp = ((cdev_t)kn->kn_hook)->si_tty;
988 knote_remove(&tp->t_wkq.ki_note, kn);
995 ptcwrite(struct dev_write_args *ap)
997 cdev_t dev = ap->a_head.a_dev;
998 struct tty *tp = dev->si_tty;
1001 u_char locbuf[BUFSIZ];
1003 struct pt_ioctl *pti = dev->si_drv1;
1006 lwkt_gettoken(&pti->pt_tty.t_token);
1007 lwkt_gettoken(&tp->t_token);
1009 if ((tp->t_state&TS_ISOPEN) == 0)
1011 if (pti->pt_flags & PF_REMOTE) {
1012 if (tp->t_canq.c_cc)
1014 while ((ap->a_uio->uio_resid > 0 || cc > 0) &&
1015 tp->t_canq.c_cc < TTYHOG - 1) {
1017 cc = (int)szmin(ap->a_uio->uio_resid, BUFSIZ);
1018 cc = imin(cc, TTYHOG - 1 - tp->t_canq.c_cc);
1020 error = uiomove(cp, (size_t)cc, ap->a_uio);
1022 lwkt_reltoken(&tp->t_token);
1023 lwkt_reltoken(&pti->pt_tty.t_token);
1026 /* check again for safety */
1027 if ((tp->t_state & TS_ISOPEN) == 0) {
1028 /* adjust as usual */
1029 ap->a_uio->uio_resid += cc;
1030 lwkt_reltoken(&tp->t_token);
1031 lwkt_reltoken(&pti->pt_tty.t_token);
1036 cc = clist_btoq((char *)cp, cc, &tp->t_canq);
1038 * XXX we don't guarantee that the canq size
1039 * is >= TTYHOG, so the above btoq() may
1040 * leave some bytes uncopied. However, space
1041 * is guaranteed for the null terminator if
1042 * we don't fail here since (TTYHOG - 1) is
1043 * not a multiple of CBSIZE.
1049 /* adjust for data copied in but not written */
1050 ap->a_uio->uio_resid += cc;
1051 clist_putc(0, &tp->t_canq);
1053 wakeup(TSA_PTS_READ(tp));
1054 lwkt_reltoken(&tp->t_token);
1055 lwkt_reltoken(&pti->pt_tty.t_token);
1059 while (ap->a_uio->uio_resid > 0 || cc > 0) {
1061 cc = (int)szmin(ap->a_uio->uio_resid, BUFSIZ);
1063 error = uiomove(cp, (size_t)cc, ap->a_uio);
1065 lwkt_reltoken(&tp->t_token);
1066 lwkt_reltoken(&pti->pt_tty.t_token);
1069 /* check again for safety */
1070 if ((tp->t_state & TS_ISOPEN) == 0) {
1071 /* adjust for data copied in but not written */
1072 ap->a_uio->uio_resid += cc;
1073 lwkt_reltoken(&tp->t_token);
1074 lwkt_reltoken(&pti->pt_tty.t_token);
1079 if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 &&
1080 (tp->t_canq.c_cc > 0 || !(tp->t_lflag&ICANON))) {
1081 wakeup(TSA_HUP_OR_INPUT(tp));
1084 (*linesw[tp->t_line].l_rint)(*cp++, tp);
1090 lwkt_reltoken(&tp->t_token);
1091 lwkt_reltoken(&pti->pt_tty.t_token);
1095 * Come here to wait for slave to open, for space
1096 * in outq, or space in rawq, or an empty canq.
1098 if ((tp->t_state & TS_CONNECTED) == 0) {
1099 /* adjust for data copied in but not written */
1100 ap->a_uio->uio_resid += cc;
1101 lwkt_reltoken(&tp->t_token);
1102 lwkt_reltoken(&pti->pt_tty.t_token);
1105 if (ap->a_ioflag & IO_NDELAY) {
1106 /* adjust for data copied in but not written */
1107 ap->a_uio->uio_resid += cc;
1109 lwkt_reltoken(&tp->t_token);
1110 lwkt_reltoken(&pti->pt_tty.t_token);
1111 return (EWOULDBLOCK);
1113 lwkt_reltoken(&tp->t_token);
1114 lwkt_reltoken(&pti->pt_tty.t_token);
1117 error = tsleep(TSA_PTC_WRITE(tp), PCATCH, "ptcout", 0);
1119 /* adjust for data copied in but not written */
1120 ap->a_uio->uio_resid += cc;
1121 lwkt_reltoken(&tp->t_token);
1122 lwkt_reltoken(&pti->pt_tty.t_token);
1130 ptyioctl(struct dev_ioctl_args *ap)
1132 cdev_t dev = ap->a_head.a_dev;
1133 struct tty *tp = dev->si_tty;
1134 struct pt_ioctl *pti = dev->si_drv1;
1135 u_char *cc = tp->t_cc;
1138 lwkt_gettoken(&pti->pt_tty.t_token);
1139 lwkt_gettoken(&tp->t_token);
1141 if (dev_dflags(dev) & D_MASTER) {
1142 switch (ap->a_cmd) {
1146 * We avoid calling ttioctl on the controller since,
1147 * in that case, tp must be the controlling terminal.
1149 *(int *)ap->a_data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
1150 lwkt_reltoken(&tp->t_token);
1151 lwkt_reltoken(&pti->pt_tty.t_token);
1155 if (*(int *)ap->a_data) {
1156 if (pti->pt_flags & PF_UCNTL) {
1157 lwkt_reltoken(&tp->t_token);
1158 lwkt_reltoken(&pti->pt_tty.t_token);
1161 pti->pt_flags |= PF_PKT;
1163 pti->pt_flags &= ~PF_PKT;
1165 lwkt_reltoken(&tp->t_token);
1166 lwkt_reltoken(&pti->pt_tty.t_token);
1170 if (*(int *)ap->a_data) {
1171 if (pti->pt_flags & PF_PKT) {
1172 lwkt_reltoken(&tp->t_token);
1173 lwkt_reltoken(&pti->pt_tty.t_token);
1176 pti->pt_flags |= PF_UCNTL;
1178 pti->pt_flags &= ~PF_UCNTL;
1180 lwkt_reltoken(&tp->t_token);
1181 lwkt_reltoken(&pti->pt_tty.t_token);
1185 if (*(int *)ap->a_data)
1186 pti->pt_flags |= PF_REMOTE;
1188 pti->pt_flags &= ~PF_REMOTE;
1189 ttyflush(tp, FREAD|FWRITE);
1190 lwkt_reltoken(&tp->t_token);
1191 lwkt_reltoken(&pti->pt_tty.t_token);
1194 case TIOCISPTMASTER:
1195 if ((pti->pt_flags & PF_UNIX98) &&
1196 (pti->devc == dev)) {
1197 lwkt_reltoken(&tp->t_token);
1198 lwkt_reltoken(&pti->pt_tty.t_token);
1201 lwkt_reltoken(&tp->t_token);
1202 lwkt_reltoken(&pti->pt_tty.t_token);
1208 * The rest of the ioctls shouldn't be called until
1209 * the slave is open.
1211 if ((tp->t_state & TS_ISOPEN) == 0) {
1212 lwkt_reltoken(&tp->t_token);
1213 lwkt_reltoken(&pti->pt_tty.t_token);
1217 switch (ap->a_cmd) {
1223 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
1224 * ttywflush(tp) will hang if there are characters in
1227 ndflush(&tp->t_outq, tp->t_outq.c_cc);
1231 if (*(unsigned int *)ap->a_data >= NSIG ||
1232 *(unsigned int *)ap->a_data == 0) {
1233 lwkt_reltoken(&tp->t_token);
1234 lwkt_reltoken(&pti->pt_tty.t_token);
1237 if ((tp->t_lflag&NOFLSH) == 0)
1238 ttyflush(tp, FREAD|FWRITE);
1239 pgsignal(tp->t_pgrp, *(unsigned int *)ap->a_data, 1);
1240 if ((*(unsigned int *)ap->a_data == SIGINFO) &&
1241 ((tp->t_lflag&NOKERNINFO) == 0))
1243 lwkt_reltoken(&tp->t_token);
1244 lwkt_reltoken(&pti->pt_tty.t_token);
1248 if (ap->a_cmd == TIOCEXT) {
1250 * When the EXTPROC bit is being toggled, we need
1251 * to send an TIOCPKT_IOCTL if the packet driver
1254 if (*(int *)ap->a_data) {
1255 if (pti->pt_flags & PF_PKT) {
1256 pti->pt_send |= TIOCPKT_IOCTL;
1257 ptcwakeup(tp, FREAD);
1259 tp->t_lflag |= EXTPROC;
1261 if ((tp->t_lflag & EXTPROC) &&
1262 (pti->pt_flags & PF_PKT)) {
1263 pti->pt_send |= TIOCPKT_IOCTL;
1264 ptcwakeup(tp, FREAD);
1266 tp->t_lflag &= ~EXTPROC;
1268 lwkt_reltoken(&tp->t_token);
1269 lwkt_reltoken(&pti->pt_tty.t_token);
1272 error = (*linesw[tp->t_line].l_ioctl)(tp, ap->a_cmd, ap->a_data,
1273 ap->a_fflag, ap->a_cred);
1274 if (error == ENOIOCTL)
1275 error = ttioctl(tp, ap->a_cmd, ap->a_data, ap->a_fflag);
1276 if (error == ENOIOCTL) {
1277 if (pti->pt_flags & PF_UCNTL &&
1278 (ap->a_cmd & ~0xff) == UIOCCMD(0)) {
1279 if (ap->a_cmd & 0xff) {
1280 pti->pt_ucntl = (u_char)ap->a_cmd;
1281 ptcwakeup(tp, FREAD);
1283 lwkt_reltoken(&tp->t_token);
1284 lwkt_reltoken(&pti->pt_tty.t_token);
1290 * If external processing and packet mode send ioctl packet.
1292 if ((tp->t_lflag&EXTPROC) && (pti->pt_flags & PF_PKT)) {
1297 pti->pt_send |= TIOCPKT_IOCTL;
1298 ptcwakeup(tp, FREAD);
1303 stop = (tp->t_iflag & IXON) && CCEQ(cc[VSTOP], CTRL('s'))
1304 && CCEQ(cc[VSTART], CTRL('q'));
1305 if (pti->pt_flags & PF_NOSTOP) {
1307 pti->pt_send &= ~TIOCPKT_NOSTOP;
1308 pti->pt_send |= TIOCPKT_DOSTOP;
1309 pti->pt_flags &= ~PF_NOSTOP;
1310 ptcwakeup(tp, FREAD);
1314 pti->pt_send &= ~TIOCPKT_DOSTOP;
1315 pti->pt_send |= TIOCPKT_NOSTOP;
1316 pti->pt_flags |= PF_NOSTOP;
1317 ptcwakeup(tp, FREAD);
1320 lwkt_reltoken(&tp->t_token);
1321 lwkt_reltoken(&pti->pt_tty.t_token);
1327 static void ptc_drvinit (void *unused);
1329 SYSCTL_INT(_kern, OID_AUTO, pty_debug, CTLFLAG_RW, &pty_debug_level,
1330 0, "Change pty debug level");
1333 ptc_drvinit(void *unused)
1339 * Create the clonable base device.
1341 make_autoclone_dev(&ptc_ops, &DEVFS_CLONE_BITMAP(pty), ptyclone,
1342 0, 0, 0666, "ptmx");
1343 ptis = kmalloc(sizeof(struct pt_ioctl *) * MAXPTYS, M_PTY,
1346 for (i = 0; i < 256; i++) {
1351 SYSINIT(ptcdev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE + CDEV_MAJOR_C,