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_DEFINE_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
;
852 switch (kn
->kn_filter
) {
854 klist
= &tp
->t_rkq
.ki_note
;
855 kn
->kn_fop
= &ptcread_filtops
;
858 klist
= &tp
->t_wkq
.ki_note
;
859 kn
->kn_fop
= &ptcwrite_filtops
;
862 ap
->a_result
= EOPNOTSUPP
;
866 kn
->kn_hook
= (caddr_t
)dev
;
867 knote_insert(klist
, kn
);
872 filt_ptcread (struct knote
*kn
, long hint
)
874 struct tty
*tp
= ((cdev_t
)kn
->kn_hook
)->si_tty
;
875 struct pt_ioctl
*pti
= ((cdev_t
)kn
->kn_hook
)->si_drv1
;
877 lwkt_gettoken(&tty_token
);
878 if ((tp
->t_state
& TS_ZOMBIE
) || (pti
->pt_flags
& PF_SCLOSED
)) {
879 lwkt_reltoken(&tty_token
);
880 kn
->kn_flags
|= (EV_EOF
| EV_NODATA
);
884 if ((tp
->t_state
& TS_ISOPEN
) &&
885 ((tp
->t_outq
.c_cc
&& (tp
->t_state
& TS_TTSTOP
) == 0) ||
886 ((pti
->pt_flags
& PF_PKT
) && pti
->pt_send
) ||
887 ((pti
->pt_flags
& PF_UCNTL
) && pti
->pt_ucntl
))) {
888 kn
->kn_data
= tp
->t_outq
.c_cc
;
889 lwkt_reltoken(&tty_token
);
892 lwkt_reltoken(&tty_token
);
898 filt_ptcwrite (struct knote
*kn
, long hint
)
900 struct tty
*tp
= ((cdev_t
)kn
->kn_hook
)->si_tty
;
901 struct pt_ioctl
*pti
= ((cdev_t
)kn
->kn_hook
)->si_drv1
;
903 lwkt_gettoken(&tty_token
);
904 if (tp
->t_state
& TS_ZOMBIE
) {
905 lwkt_reltoken(&tty_token
);
906 kn
->kn_flags
|= (EV_EOF
| EV_NODATA
);
910 if (tp
->t_state
& TS_ISOPEN
&&
911 ((pti
->pt_flags
& PF_REMOTE
) ?
912 (tp
->t_canq
.c_cc
== 0) :
913 ((tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
< TTYHOG
- 2) ||
914 (tp
->t_canq
.c_cc
== 0 && (tp
->t_lflag
& ICANON
))))) {
915 kn
->kn_data
= tp
->t_canq
.c_cc
+ tp
->t_rawq
.c_cc
;
916 lwkt_reltoken(&tty_token
);
919 lwkt_reltoken(&tty_token
);
926 filt_ptcrdetach (struct knote
*kn
)
928 struct tty
*tp
= ((cdev_t
)kn
->kn_hook
)->si_tty
;
930 knote_remove(&tp
->t_rkq
.ki_note
, kn
);
934 filt_ptcwdetach (struct knote
*kn
)
936 struct tty
*tp
= ((cdev_t
)kn
->kn_hook
)->si_tty
;
938 knote_remove(&tp
->t_wkq
.ki_note
, kn
);
945 ptcwrite(struct dev_write_args
*ap
)
947 cdev_t dev
= ap
->a_head
.a_dev
;
948 struct tty
*tp
= dev
->si_tty
;
951 u_char locbuf
[BUFSIZ
];
953 struct pt_ioctl
*pti
= dev
->si_drv1
;
956 lwkt_gettoken(&tty_token
);
958 if ((tp
->t_state
&TS_ISOPEN
) == 0)
960 if (pti
->pt_flags
& PF_REMOTE
) {
963 while ((ap
->a_uio
->uio_resid
> 0 || cc
> 0) &&
964 tp
->t_canq
.c_cc
< TTYHOG
- 1) {
966 cc
= (int)szmin(ap
->a_uio
->uio_resid
, BUFSIZ
);
967 cc
= imin(cc
, TTYHOG
- 1 - tp
->t_canq
.c_cc
);
969 error
= uiomove(cp
, (size_t)cc
, ap
->a_uio
);
971 lwkt_reltoken(&tty_token
);
974 /* check again for safety */
975 if ((tp
->t_state
& TS_ISOPEN
) == 0) {
976 /* adjust as usual */
977 ap
->a_uio
->uio_resid
+= cc
;
978 lwkt_reltoken(&tty_token
);
983 cc
= b_to_q((char *)cp
, cc
, &tp
->t_canq
);
985 * XXX we don't guarantee that the canq size
986 * is >= TTYHOG, so the above b_to_q() may
987 * leave some bytes uncopied. However, space
988 * is guaranteed for the null terminator if
989 * we don't fail here since (TTYHOG - 1) is
990 * not a multiple of CBSIZE.
996 /* adjust for data copied in but not written */
997 ap
->a_uio
->uio_resid
+= cc
;
998 clist_putc(0, &tp
->t_canq
);
1000 wakeup(TSA_PTS_READ(tp
));
1001 lwkt_reltoken(&tty_token
);
1004 while (ap
->a_uio
->uio_resid
> 0 || cc
> 0) {
1006 cc
= (int)szmin(ap
->a_uio
->uio_resid
, BUFSIZ
);
1008 error
= uiomove(cp
, (size_t)cc
, ap
->a_uio
);
1010 lwkt_reltoken(&tty_token
);
1013 /* check again for safety */
1014 if ((tp
->t_state
& TS_ISOPEN
) == 0) {
1015 /* adjust for data copied in but not written */
1016 ap
->a_uio
->uio_resid
+= cc
;
1017 lwkt_reltoken(&tty_token
);
1022 if ((tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
) >= TTYHOG
- 2 &&
1023 (tp
->t_canq
.c_cc
> 0 || !(tp
->t_lflag
&ICANON
))) {
1024 wakeup(TSA_HUP_OR_INPUT(tp
));
1027 (*linesw
[tp
->t_line
].l_rint
)(*cp
++, tp
);
1033 lwkt_reltoken(&tty_token
);
1037 * Come here to wait for slave to open, for space
1038 * in outq, or space in rawq, or an empty canq.
1040 if ((tp
->t_state
& TS_CONNECTED
) == 0) {
1041 /* adjust for data copied in but not written */
1042 ap
->a_uio
->uio_resid
+= cc
;
1043 lwkt_reltoken(&tty_token
);
1046 if (ap
->a_ioflag
& IO_NDELAY
) {
1047 /* adjust for data copied in but not written */
1048 ap
->a_uio
->uio_resid
+= cc
;
1050 lwkt_reltoken(&tty_token
);
1051 return (EWOULDBLOCK
);
1053 lwkt_reltoken(&tty_token
);
1056 error
= tsleep(TSA_PTC_WRITE(tp
), PCATCH
, "ptcout", 0);
1058 /* adjust for data copied in but not written */
1059 ap
->a_uio
->uio_resid
+= cc
;
1060 lwkt_reltoken(&tty_token
);
1068 ptyioctl(struct dev_ioctl_args
*ap
)
1070 cdev_t dev
= ap
->a_head
.a_dev
;
1071 struct tty
*tp
= dev
->si_tty
;
1072 struct pt_ioctl
*pti
= dev
->si_drv1
;
1073 u_char
*cc
= tp
->t_cc
;
1076 lwkt_gettoken(&tty_token
);
1077 if (dev_dflags(dev
) & D_MASTER
) {
1078 switch (ap
->a_cmd
) {
1082 * We avoid calling ttioctl on the controller since,
1083 * in that case, tp must be the controlling terminal.
1085 *(int *)ap
->a_data
= tp
->t_pgrp
? tp
->t_pgrp
->pg_id
: 0;
1086 lwkt_reltoken(&tty_token
);
1090 if (*(int *)ap
->a_data
) {
1091 if (pti
->pt_flags
& PF_UCNTL
) {
1092 lwkt_reltoken(&tty_token
);
1095 pti
->pt_flags
|= PF_PKT
;
1097 pti
->pt_flags
&= ~PF_PKT
;
1099 lwkt_reltoken(&tty_token
);
1103 if (*(int *)ap
->a_data
) {
1104 if (pti
->pt_flags
& PF_PKT
) {
1105 lwkt_reltoken(&tty_token
);
1108 pti
->pt_flags
|= PF_UCNTL
;
1110 pti
->pt_flags
&= ~PF_UCNTL
;
1112 lwkt_reltoken(&tty_token
);
1116 if (*(int *)ap
->a_data
)
1117 pti
->pt_flags
|= PF_REMOTE
;
1119 pti
->pt_flags
&= ~PF_REMOTE
;
1120 ttyflush(tp
, FREAD
|FWRITE
);
1121 lwkt_reltoken(&tty_token
);
1124 case TIOCISPTMASTER
:
1125 if ((pti
->pt_flags
& PF_UNIX98
) &&
1126 (pti
->devc
== dev
)) {
1127 lwkt_reltoken(&tty_token
);
1130 lwkt_reltoken(&tty_token
);
1136 * The rest of the ioctls shouldn't be called until
1137 * the slave is open.
1139 if ((tp
->t_state
& TS_ISOPEN
) == 0) {
1140 lwkt_reltoken(&tty_token
);
1144 switch (ap
->a_cmd
) {
1154 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
1155 * ttywflush(tp) will hang if there are characters in
1158 ndflush(&tp
->t_outq
, tp
->t_outq
.c_cc
);
1162 if (*(unsigned int *)ap
->a_data
>= NSIG
||
1163 *(unsigned int *)ap
->a_data
== 0) {
1164 lwkt_reltoken(&tty_token
);
1167 if ((tp
->t_lflag
&NOFLSH
) == 0)
1168 ttyflush(tp
, FREAD
|FWRITE
);
1169 pgsignal(tp
->t_pgrp
, *(unsigned int *)ap
->a_data
, 1);
1170 if ((*(unsigned int *)ap
->a_data
== SIGINFO
) &&
1171 ((tp
->t_lflag
&NOKERNINFO
) == 0))
1173 lwkt_reltoken(&tty_token
);
1177 if (ap
->a_cmd
== TIOCEXT
) {
1179 * When the EXTPROC bit is being toggled, we need
1180 * to send an TIOCPKT_IOCTL if the packet driver
1183 if (*(int *)ap
->a_data
) {
1184 if (pti
->pt_flags
& PF_PKT
) {
1185 pti
->pt_send
|= TIOCPKT_IOCTL
;
1186 ptcwakeup(tp
, FREAD
);
1188 tp
->t_lflag
|= EXTPROC
;
1190 if ((tp
->t_lflag
& EXTPROC
) &&
1191 (pti
->pt_flags
& PF_PKT
)) {
1192 pti
->pt_send
|= TIOCPKT_IOCTL
;
1193 ptcwakeup(tp
, FREAD
);
1195 tp
->t_lflag
&= ~EXTPROC
;
1197 lwkt_reltoken(&tty_token
);
1200 error
= (*linesw
[tp
->t_line
].l_ioctl
)(tp
, ap
->a_cmd
, ap
->a_data
,
1201 ap
->a_fflag
, ap
->a_cred
);
1202 if (error
== ENOIOCTL
)
1203 error
= ttioctl(tp
, ap
->a_cmd
, ap
->a_data
, ap
->a_fflag
);
1204 if (error
== ENOIOCTL
) {
1205 if (pti
->pt_flags
& PF_UCNTL
&&
1206 (ap
->a_cmd
& ~0xff) == UIOCCMD(0)) {
1207 if (ap
->a_cmd
& 0xff) {
1208 pti
->pt_ucntl
= (u_char
)ap
->a_cmd
;
1209 ptcwakeup(tp
, FREAD
);
1211 lwkt_reltoken(&tty_token
);
1217 * If external processing and packet mode send ioctl packet.
1219 if ((tp
->t_lflag
&EXTPROC
) && (pti
->pt_flags
& PF_PKT
)) {
1233 pti
->pt_send
|= TIOCPKT_IOCTL
;
1234 ptcwakeup(tp
, FREAD
);
1239 stop
= (tp
->t_iflag
& IXON
) && CCEQ(cc
[VSTOP
], CTRL('s'))
1240 && CCEQ(cc
[VSTART
], CTRL('q'));
1241 if (pti
->pt_flags
& PF_NOSTOP
) {
1243 pti
->pt_send
&= ~TIOCPKT_NOSTOP
;
1244 pti
->pt_send
|= TIOCPKT_DOSTOP
;
1245 pti
->pt_flags
&= ~PF_NOSTOP
;
1246 ptcwakeup(tp
, FREAD
);
1250 pti
->pt_send
&= ~TIOCPKT_DOSTOP
;
1251 pti
->pt_send
|= TIOCPKT_NOSTOP
;
1252 pti
->pt_flags
|= PF_NOSTOP
;
1253 ptcwakeup(tp
, FREAD
);
1256 lwkt_reltoken(&tty_token
);
1261 static void ptc_drvinit (void *unused
);
1263 SYSCTL_INT(_kern
, OID_AUTO
, pty_debug
, CTLFLAG_RW
, &pty_debug_level
,
1264 0, "Change pty debug level");
1267 ptc_drvinit(void *unused
)
1273 * Create the clonable base device.
1275 make_autoclone_dev(&ptc_ops
, &DEVFS_CLONE_BITMAP(pty
), ptyclone
,
1276 0, 0, 0666, "ptmx");
1278 for (i
= 0; i
< 256; i
++) {
1283 SYSINIT(ptcdev
, SI_SUB_DRIVERS
, SI_ORDER_MIDDLE
+ CDEV_MAJOR_C
, ptc_drvinit
,