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>
51 #include <sys/ttydefaults.h> /* for TTYDEF_* */
53 #include <sys/fcntl.h>
54 #include <sys/kernel.h>
55 #include <sys/vnode.h>
56 #include <sys/signalvar.h>
57 #include <sys/malloc.h>
58 #include <sys/device.h>
59 #include <sys/devfs.h>
61 #include <sys/sysctl.h>
63 MALLOC_DEFINE(M_PTY
, "ptys", "pty data structures");
65 static void ptsstart (struct tty
*tp
);
66 static void ptsstop (struct tty
*tp
, int rw
);
67 static void ptsunhold (struct tty
*tp
);
68 static void ptcwakeup (struct tty
*tp
, int flag
);
69 static void ptyinit (int n
);
70 static int filt_ptcread (struct knote
*kn
, long hint
);
71 static void filt_ptcrdetach (struct knote
*kn
);
72 static int filt_ptcwrite (struct knote
*kn
, long hint
);
73 static void filt_ptcwdetach (struct knote
*kn
);
75 static d_open_t ptsopen
;
76 static d_close_t ptsclose
;
77 static d_read_t ptsread
;
78 static d_write_t ptswrite
;
79 static d_ioctl_t ptyioctl
;
80 static d_open_t ptcopen
;
81 static d_close_t ptcclose
;
82 static d_read_t ptcread
;
83 static d_write_t ptcwrite
;
84 static d_kqfilter_t ptckqfilter
;
86 DEVFS_DEFINE_CLONE_BITMAP(pty
);
87 static struct pt_ioctl
**ptis
; /* keep pti's intact */
89 static d_clone_t ptyclone
;
91 static int pty_debug_level
= 0;
93 static struct dev_ops pts98_ops
= {
94 { "pts98", 0, D_TTY
| D_MPSAFE
},
100 .d_kqfilter
= ttykqfilter
,
101 .d_revoke
= ttyrevoke
104 static struct dev_ops ptc98_ops
= {
105 { "ptc98", 0, D_TTY
| D_MASTER
| D_MPSAFE
},
111 .d_kqfilter
= ptckqfilter
,
112 .d_revoke
= ttyrevoke
115 static struct dev_ops pts_ops
= {
116 { "pts", 0, D_TTY
| D_MPSAFE
},
122 .d_kqfilter
= ttykqfilter
,
123 .d_revoke
= ttyrevoke
126 #define CDEV_MAJOR_C 6
127 static struct dev_ops ptc_ops
= {
128 { "ptc", 0, D_TTY
| D_MASTER
| D_MPSAFE
},
134 .d_kqfilter
= ptckqfilter
,
135 .d_revoke
= ttyrevoke
138 #define BUFSIZ 100 /* Chunk size iomoved to/from user */
140 #define MAXPTYS 1000 /* Maximum cloneable ptys */
144 int pt_refs
; /* Structural references interlock S/MOPEN */
146 struct kqinfo pt_kqr
, pt_kqw
;
151 struct prison
*pt_prison
;
157 #define PF_PKT 0x0008 /* packet mode */
158 #define PF_STOPPED 0x0010 /* user told stopped */
159 #define PF_REMOTE 0x0020 /* remote and flow controlled input */
160 #define PF_NOSTOP 0x0040
161 #define PF_UCNTL 0x0080 /* user control mode */
163 #define PF_PTCSTATEMASK 0x00FF
166 * pt_flags open state. Note that PF_SCLOSED is used to activate
167 * read EOF on the ptc so it is only set after the slave has been
168 * opened and then closed, and cleared again if the slave is opened
171 #define PF_UNIX98 0x0100
172 #define PF_SOPEN 0x0200
173 #define PF_MOPEN 0x0400
174 #define PF_SCLOSED 0x0800
175 #define PF_TERMINATED 0x8000
178 * This function creates and initializes a pts/ptc pair
180 * pts == /dev/tty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
181 * ptc == /dev/pty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
183 * XXX: define and add mapping of upper minor bits to allow more
190 char *names
= "pqrsPQRS";
191 struct pt_ioctl
*pti
;
193 /* For now we only map the lower 8 bits of the minor */
197 pti
= kmalloc(sizeof(*pti
), M_PTY
, M_WAITOK
| M_ZERO
);
198 pti
->devs
= devs
= make_dev(&pts_ops
, n
, 0, 0, 0666,
200 names
[n
/ 32], hex2ascii(n
% 32));
201 pti
->devc
= devc
= make_dev(&ptc_ops
, n
, 0, 0, 0666,
203 names
[n
/ 32], hex2ascii(n
% 32));
205 pti
->pt_tty
.t_dev
= devs
;
207 devs
->si_drv1
= devc
->si_drv1
= pti
;
208 devs
->si_tty
= devc
->si_tty
= &pti
->pt_tty
;
209 devs
->si_flags
|= SI_OVERRIDE
; /* uid, gid, perms from dev */
210 devc
->si_flags
|= SI_OVERRIDE
; /* uid, gid, perms from dev */
211 ttyinit(&pti
->pt_tty
);
212 ttyregister(&pti
->pt_tty
);
216 ptyclone(struct dev_clone_args
*ap
)
219 struct pt_ioctl
*pti
;
222 * Limit the number of unix98 pty (slave) devices to 1000 for now.
224 * If this limit is reached, we don't clone and return an error
227 unit
= devfs_clone_bitmap_get(&DEVFS_CLONE_BITMAP(pty
), MAXPTYS
);
235 * pti structures must be persistent once allocated.
237 if ((pti
= ptis
[unit
]) == NULL
) {
238 lwkt_gettoken(&tty_token
);
239 pti
= kmalloc(sizeof(*pti
), M_PTY
, M_WAITOK
| M_ZERO
);
240 if (ptis
[unit
] == NULL
) {
242 ttyinit(&pti
->pt_tty
);
246 lwkt_reltoken(&tty_token
);
250 * The cloning bitmap should guarantee isolation during
253 pti
->devc
= make_only_dev(&ptc98_ops
, unit
,
255 0, 0600, "ptm/%d", unit
);
256 pti
->devs
= make_dev(&pts98_ops
, unit
,
258 GID_TTY
, 0620, "pts/%d", unit
);
259 ap
->a_dev
= pti
->devc
;
261 pti
->devs
->si_flags
|= SI_OVERRIDE
; /* uid, gid, perms from dev */
262 pti
->devc
->si_flags
|= SI_OVERRIDE
; /* uid, gid, perms from dev */
264 pti
->pt_tty
.t_dev
= pti
->devs
;
265 pti
->pt_flags
= PF_UNIX98
;
266 pti
->pt_uminor
= unit
;
267 pti
->devs
->si_drv1
= pti
->devc
->si_drv1
= pti
;
268 pti
->devs
->si_tty
= pti
->devc
->si_tty
= &pti
->pt_tty
;
269 ttyregister(&pti
->pt_tty
);
275 * pti_hold() prevents the pti from being destroyed due to a termination
276 * while a pt*open() is blocked.
278 * This function returns non-zero if we cannot hold due to a termination
282 pti_hold(struct pt_ioctl
*pti
)
284 if (pti
->pt_flags
& 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(&pti
->pt_tty
.t_token
);
303 if (--pti
->pt_refs
== 0) {
308 * Only unix09 ptys are freed up (the pti structure itself
309 * is never freed, regardless).
311 if ((pti
->pt_flags
& PF_UNIX98
) == 0) {
312 lwkt_reltoken(&pti
->pt_tty
.t_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_flags
& (PF_SOPEN
|PF_MOPEN
)) == 0 &&
325 pti
->pt_tty
.t_refs
== 0) {
326 pti
->pt_flags
|= 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 pti
->pt_tty
.t_dev
= NULL
;
342 devfs_clone_bitmap_put(&DEVFS_CLONE_BITMAP(pty
),
344 /* pti structure remains intact */
347 lwkt_reltoken(&pti
->pt_tty
.t_token
);
352 ptsopen(struct dev_open_args
*ap
)
354 cdev_t dev
= ap
->a_head
.a_dev
;
357 struct pt_ioctl
*pti
;
360 * The pti will already be assigned by the clone code or
361 * pre-created if a non-unix 98 pty. If si_drv1 is NULL
362 * we are somehow racing a unix98 termination.
364 if (dev
->si_drv1
== NULL
)
368 lwkt_gettoken(&pti
->pt_tty
.t_token
);
370 lwkt_reltoken(&pti
->pt_tty
.t_token
);
377 * Reinit most of the tty state if it isn't open. Handle
380 if ((tp
->t_state
& TS_ISOPEN
) == 0) {
381 ttychars(tp
); /* Set up default chars */
382 tp
->t_iflag
= TTYDEF_IFLAG
;
383 tp
->t_oflag
= TTYDEF_OFLAG
;
384 tp
->t_lflag
= TTYDEF_LFLAG
;
385 tp
->t_cflag
= TTYDEF_CFLAG
;
386 tp
->t_ispeed
= tp
->t_ospeed
= TTYDEF_SPEED
;
387 } else if ((tp
->t_state
& TS_XCLUDE
) &&
388 caps_priv_check(ap
->a_cred
, SYSCAP_RESTRICTEDROOT
))
391 lwkt_reltoken(&pti
->pt_tty
.t_token
);
393 } else if (pti
->pt_prison
!= ap
->a_cred
->cr_prison
) {
395 lwkt_reltoken(&pti
->pt_tty
.t_token
);
400 * If the ptc is already present this will connect us up. It
401 * is unclear if this is actually needed.
403 * If neither side is open be sure to clear any left over
404 * ZOMBIE state before continuing.
407 (void)(*linesw
[tp
->t_line
].l_modem
)(tp
, 1);
408 else if ((pti
->pt_flags
& PF_SOPEN
) == 0)
409 tp
->t_state
&= ~TS_ZOMBIE
;
412 * Wait for the carrier (ptc side)
414 while ((tp
->t_state
& TS_CARR_ON
) == 0) {
415 if (ap
->a_oflags
& FNONBLOCK
)
417 error
= ttysleep(tp
, TSA_CARR_ON(tp
), PCATCH
, "ptsopn", 0);
420 lwkt_reltoken(&pti
->pt_tty
.t_token
);
426 * Mark the tty open and mark the slave side as being open.
428 error
= (*linesw
[tp
->t_line
].l_open
)(dev
, tp
);
431 pti
->pt_flags
|= PF_SOPEN
;
432 pti
->pt_flags
&= ~PF_SCLOSED
;
433 ptcwakeup(tp
, FREAD
|FWRITE
);
436 lwkt_reltoken(&pti
->pt_tty
.t_token
);
442 ptsclose(struct dev_close_args
*ap
)
444 cdev_t dev
= ap
->a_head
.a_dev
;
446 struct pt_ioctl
*pti
= dev
->si_drv1
;
449 lwkt_gettoken(&pti
->pt_tty
.t_token
);
451 panic("ptsclose on terminated pti");
454 * Disconnect the slave side
457 err
= (*linesw
[tp
->t_line
].l_close
)(tp
, ap
->a_fflag
);
458 ptsstop(tp
, FREAD
|FWRITE
);
459 ttyclose(tp
); /* clears t_state */
462 * Mark the pts side closed and signal the ptc. Do not mark the
463 * tty a zombie... that is, allow the tty to be re-opened as long
464 * as the ptc is still open. The ptc will read() EOFs until the
465 * pts side is reopened or the ptc is closed.
467 * xterm() depends on this behavior as it will revoke() the pts
468 * and then reopen it after the (unnecessary old code) chmod.
470 pti
->pt_flags
&= ~PF_SOPEN
;
471 pti
->pt_flags
|= PF_SCLOSED
;
473 ptcwakeup(tp
, FREAD
);
475 lwkt_reltoken(&pti
->pt_tty
.t_token
);
480 ptsread(struct dev_read_args
*ap
)
482 cdev_t dev
= ap
->a_head
.a_dev
;
483 struct proc
*p
= curproc
;
484 struct tty
*tp
= dev
->si_tty
;
485 struct pt_ioctl
*pti
= dev
->si_drv1
;
490 lp
= curthread
->td_lwp
;
492 lwkt_gettoken(&pti
->pt_tty
.t_token
);
494 if (pti
->pt_flags
& PF_REMOTE
) {
495 while (isbackground(p
, tp
)) {
496 if (SIGISMEMBER(p
->p_sigignore
, SIGTTIN
) ||
497 SIGISMEMBER(lp
->lwp_sigmask
, SIGTTIN
) ||
498 p
->p_pgrp
->pg_jobc
== 0 ||
499 (p
->p_flags
& P_PPWAIT
)) {
500 lwkt_reltoken(&pti
->pt_tty
.t_token
);
503 pgsignal(p
->p_pgrp
, SIGTTIN
, 1);
504 error
= ttysleep(tp
, &lbolt
, PCATCH
, "ptsbg", 0);
506 lwkt_reltoken(&pti
->pt_tty
.t_token
);
510 if (tp
->t_canq
.c_cc
== 0) {
511 if (ap
->a_ioflag
& IO_NDELAY
) {
512 lwkt_reltoken(&pti
->pt_tty
.t_token
);
513 return (EWOULDBLOCK
);
515 error
= ttysleep(tp
, TSA_PTS_READ(tp
), PCATCH
,
518 lwkt_reltoken(&pti
->pt_tty
.t_token
);
523 while (tp
->t_canq
.c_cc
> 1 && ap
->a_uio
->uio_resid
> 0)
524 if (ureadc(clist_getc(&tp
->t_canq
), ap
->a_uio
) < 0) {
528 if (tp
->t_canq
.c_cc
== 1)
529 clist_getc(&tp
->t_canq
);
530 if (tp
->t_canq
.c_cc
) {
531 lwkt_reltoken(&pti
->pt_tty
.t_token
);
536 error
= (*linesw
[tp
->t_line
].l_read
)(tp
, ap
->a_uio
, ap
->a_ioflag
);
537 ptcwakeup(tp
, FWRITE
);
538 lwkt_reltoken(&pti
->pt_tty
.t_token
);
544 * Write to pseudo-tty.
545 * Wakeups of controlling tty will happen
546 * indirectly, when tty driver calls ptsstart.
549 ptswrite(struct dev_write_args
*ap
)
551 cdev_t dev
= ap
->a_head
.a_dev
;
556 lwkt_gettoken(&tp
->t_token
);
557 if (tp
->t_oproc
== NULL
) {
558 lwkt_reltoken(&tp
->t_token
);
561 ret
= ((*linesw
[tp
->t_line
].l_write
)(tp
, ap
->a_uio
, ap
->a_ioflag
));
562 lwkt_reltoken(&tp
->t_token
);
568 * Start output on pseudo-tty.
569 * Wake up process selecting or sleeping for input from controlling tty.
572 ptsstart(struct tty
*tp
)
574 struct pt_ioctl
*pti
= tp
->t_dev
->si_drv1
;
576 lwkt_gettoken(&pti
->pt_tty
.t_token
);
577 lwkt_gettoken(&tp
->t_token
);
578 if (tp
->t_state
& TS_TTSTOP
) {
579 lwkt_reltoken(&tp
->t_token
);
580 lwkt_reltoken(&pti
->pt_tty
.t_token
);
584 if (pti
->pt_flags
& PF_STOPPED
) {
585 pti
->pt_flags
&= ~PF_STOPPED
;
586 pti
->pt_send
= TIOCPKT_START
;
589 ptcwakeup(tp
, FREAD
);
590 lwkt_reltoken(&tp
->t_token
);
591 lwkt_reltoken(&pti
->pt_tty
.t_token
);
595 * NOTE: Must be called with tp->t_token held
598 ptcwakeup(struct tty
*tp
, int flag
)
601 wakeup(TSA_PTC_READ(tp
));
602 KNOTE(&tp
->t_rkq
.ki_note
, 0);
605 wakeup(TSA_PTC_WRITE(tp
));
606 KNOTE(&tp
->t_wkq
.ki_note
, 0);
611 ptcopen(struct dev_open_args
*ap
)
613 cdev_t dev
= ap
->a_head
.a_dev
;
615 struct pt_ioctl
*pti
;
618 * The pti will already be assigned by the clone code or
619 * pre-created if a non-unix 98 pty. If si_drv1 is NULL
620 * we are somehow racing a unix98 termination.
626 lwkt_gettoken(&pti
->pt_tty
.t_token
);
628 lwkt_reltoken(&pti
->pt_tty
.t_token
);
631 if (pti
->pt_prison
&& pti
->pt_prison
!= ap
->a_cred
->cr_prison
) {
633 lwkt_reltoken(&pti
->pt_tty
.t_token
);
637 lwkt_gettoken(&tp
->t_token
);
640 lwkt_reltoken(&tp
->t_token
);
641 lwkt_reltoken(&pti
->pt_tty
.t_token
);
646 * If the slave side is not yet open clear any left over zombie
647 * state before doing our modem control.
649 if ((pti
->pt_flags
& PF_SOPEN
) == 0)
650 tp
->t_state
&= ~TS_ZOMBIE
;
652 tp
->t_oproc
= ptsstart
;
653 tp
->t_stop
= ptsstop
;
654 tp
->t_unhold
= ptsunhold
;
659 (void)(*linesw
[tp
->t_line
].l_modem
)(tp
, 1);
661 tp
->t_lflag
&= ~EXTPROC
;
662 pti
->pt_prison
= ap
->a_cred
->cr_prison
;
663 pti
->pt_flags
&= ~PF_PTCSTATEMASK
;
667 pti
->devs
->si_uid
= ap
->a_cred
->cr_uid
;
668 pti
->devs
->si_gid
= ap
->a_cred
->cr_uid
? GID_TTY
: 0;
669 pti
->devs
->si_perms
= 0600;
670 pti
->devc
->si_uid
= ap
->a_cred
->cr_uid
;
671 pti
->devc
->si_gid
= 0;
672 pti
->devc
->si_perms
= 0600;
675 * Mark master side open. This does not cause any events
678 pti
->pt_flags
|= PF_MOPEN
;
681 lwkt_reltoken(&tp
->t_token
);
682 lwkt_reltoken(&pti
->pt_tty
.t_token
);
688 ptcclose(struct dev_close_args
*ap
)
690 cdev_t dev
= ap
->a_head
.a_dev
;
692 struct pt_ioctl
*pti
= dev
->si_drv1
;
694 lwkt_gettoken(&pti
->pt_tty
.t_token
);
696 lwkt_reltoken(&pti
->pt_tty
.t_token
);
697 panic("ptcclose on terminated pti");
700 lwkt_gettoken(&tp
->t_token
);
702 (void)(*linesw
[tp
->t_line
].l_modem
)(tp
, 0);
705 * Mark the master side closed. If the slave is still open
706 * mark the tty ZOMBIE, preventing any new action until both
709 * NOTE: The ttyflush() will wake up the slave once we've
710 * set appropriate flags. The ZOMBIE flag will be
711 * cleared when the slave side is closed.
713 pti
->pt_flags
&= ~PF_MOPEN
;
714 if (pti
->pt_flags
& PF_SOPEN
)
715 tp
->t_state
|= TS_ZOMBIE
;
718 * Turn off the carrier and disconnect. This will notify the slave
721 if (tp
->t_state
& TS_ISOPEN
) {
722 tp
->t_state
&= ~(TS_CARR_ON
| TS_CONNECTED
);
723 ttyflush(tp
, FREAD
| FWRITE
);
725 tp
->t_oproc
= NULL
; /* mark closed */
727 pti
->pt_prison
= NULL
;
728 pti
->devs
->si_uid
= 0;
729 pti
->devs
->si_gid
= 0;
730 pti
->devs
->si_perms
= 0666;
731 pti
->devc
->si_uid
= 0;
732 pti
->devc
->si_gid
= 0;
733 pti
->devc
->si_perms
= 0666;
736 lwkt_reltoken(&tp
->t_token
);
737 lwkt_reltoken(&pti
->pt_tty
.t_token
);
743 ptcread(struct dev_read_args
*ap
)
745 cdev_t dev
= ap
->a_head
.a_dev
;
746 struct tty
*tp
= dev
->si_tty
;
747 struct pt_ioctl
*pti
= dev
->si_drv1
;
751 lwkt_gettoken(&pti
->pt_tty
.t_token
);
752 lwkt_gettoken(&tp
->t_token
);
755 * We want to block until the slave
756 * is open, and there's something to read;
757 * but if we lost the slave or we're NBIO,
758 * then return the appropriate error instead.
761 if (tp
->t_state
&TS_ISOPEN
) {
762 if ((pti
->pt_flags
& PF_PKT
) && pti
->pt_send
) {
763 error
= ureadc((int)pti
->pt_send
, ap
->a_uio
);
765 lwkt_reltoken(&tp
->t_token
);
766 lwkt_reltoken(&pti
->pt_tty
.t_token
);
769 if (pti
->pt_send
& TIOCPKT_IOCTL
) {
770 cc
= (int)szmin(ap
->a_uio
->uio_resid
,
771 sizeof(tp
->t_termios
));
772 uiomove((caddr_t
)&tp
->t_termios
, cc
,
776 lwkt_reltoken(&tp
->t_token
);
777 lwkt_reltoken(&pti
->pt_tty
.t_token
);
781 if ((pti
->pt_flags
& PF_UCNTL
) && pti
->pt_ucntl
) {
782 error
= ureadc((int)pti
->pt_ucntl
, ap
->a_uio
);
784 lwkt_reltoken(&tp
->t_token
);
785 lwkt_reltoken(&pti
->pt_tty
.t_token
);
789 lwkt_reltoken(&tp
->t_token
);
790 lwkt_reltoken(&pti
->pt_tty
.t_token
);
794 if (tp
->t_outq
.c_cc
&& (tp
->t_state
&TS_TTSTOP
) == 0)
797 if ((tp
->t_state
& TS_CONNECTED
) == 0) {
798 lwkt_reltoken(&tp
->t_token
);
799 lwkt_reltoken(&pti
->pt_tty
.t_token
);
800 return (0); /* EOF */
802 if (ap
->a_ioflag
& IO_NDELAY
) {
803 lwkt_reltoken(&tp
->t_token
);
804 lwkt_reltoken(&pti
->pt_tty
.t_token
);
805 return (EWOULDBLOCK
);
807 error
= tsleep(TSA_PTC_READ(tp
), PCATCH
, "ptcin", 0);
809 lwkt_reltoken(&tp
->t_token
);
810 lwkt_reltoken(&pti
->pt_tty
.t_token
);
814 if (pti
->pt_flags
& (PF_PKT
|PF_UCNTL
))
815 error
= ureadc(0, ap
->a_uio
);
816 while (ap
->a_uio
->uio_resid
> 0 && error
== 0) {
817 cc
= clist_qtob(&tp
->t_outq
, buf
,
818 (int)szmin(ap
->a_uio
->uio_resid
, BUFSIZ
));
821 error
= uiomove(buf
, (size_t)cc
, ap
->a_uio
);
824 lwkt_reltoken(&tp
->t_token
);
825 lwkt_reltoken(&pti
->pt_tty
.t_token
);
831 ptsstop(struct tty
*tp
, int flush
)
833 struct pt_ioctl
*pti
= tp
->t_dev
->si_drv1
;
836 lwkt_gettoken(&pti
->pt_tty
.t_token
);
837 /* note: FLUSHREAD and FLUSHWRITE already ok */
840 flush
= TIOCPKT_STOP
;
841 pti
->pt_flags
|= PF_STOPPED
;
843 pti
->pt_flags
&= ~PF_STOPPED
;
845 pti
->pt_send
|= flush
;
846 /* change of perspective */
855 lwkt_reltoken(&pti
->pt_tty
.t_token
);
859 * ttyunhold() calls us instead of just decrementing tp->t_refs. This
860 * is needed because a session can hold onto a pts (half closed state)
861 * even if there are no live file descriptors. Without the callback
865 ptsunhold(struct tty
*tp
)
867 struct pt_ioctl
*pti
= tp
->t_dev
->si_drv1
;
869 lwkt_gettoken(&pti
->pt_tty
.t_token
);
870 lwkt_gettoken(&tp
->t_token
);
874 lwkt_reltoken(&tp
->t_token
);
875 lwkt_reltoken(&pti
->pt_tty
.t_token
);
879 * kqueue ops for pseudo-terminals.
881 static struct filterops ptcread_filtops
=
882 { FILTEROP_ISFD
|FILTEROP_MPSAFE
, NULL
, filt_ptcrdetach
, filt_ptcread
};
883 static struct filterops ptcwrite_filtops
=
884 { FILTEROP_ISFD
|FILTEROP_MPSAFE
, NULL
, filt_ptcwdetach
, filt_ptcwrite
};
887 ptckqfilter(struct dev_kqfilter_args
*ap
)
889 cdev_t dev
= ap
->a_head
.a_dev
;
890 struct knote
*kn
= ap
->a_kn
;
891 struct tty
*tp
= dev
->si_tty
;
895 switch (kn
->kn_filter
) {
897 klist
= &tp
->t_rkq
.ki_note
;
898 kn
->kn_fop
= &ptcread_filtops
;
901 klist
= &tp
->t_wkq
.ki_note
;
902 kn
->kn_fop
= &ptcwrite_filtops
;
905 ap
->a_result
= EOPNOTSUPP
;
909 kn
->kn_hook
= (caddr_t
)dev
;
910 knote_insert(klist
, kn
);
915 filt_ptcread (struct knote
*kn
, long hint
)
917 struct tty
*tp
= ((cdev_t
)kn
->kn_hook
)->si_tty
;
918 struct pt_ioctl
*pti
= ((cdev_t
)kn
->kn_hook
)->si_drv1
;
920 lwkt_gettoken(&pti
->pt_tty
.t_token
);
921 lwkt_gettoken(&tp
->t_token
);
923 if ((tp
->t_state
& TS_ZOMBIE
) || (pti
->pt_flags
& PF_SCLOSED
)) {
924 kn
->kn_flags
|= (EV_EOF
| EV_NODATA
);
925 lwkt_reltoken(&tp
->t_token
);
926 lwkt_reltoken(&pti
->pt_tty
.t_token
);
930 if ((tp
->t_state
& TS_ISOPEN
) &&
931 ((tp
->t_outq
.c_cc
&& (tp
->t_state
& TS_TTSTOP
) == 0) ||
932 ((pti
->pt_flags
& PF_PKT
) && pti
->pt_send
) ||
933 ((pti
->pt_flags
& PF_UCNTL
) && pti
->pt_ucntl
))) {
934 kn
->kn_data
= tp
->t_outq
.c_cc
;
935 lwkt_reltoken(&tp
->t_token
);
936 lwkt_reltoken(&pti
->pt_tty
.t_token
);
939 lwkt_reltoken(&tp
->t_token
);
940 lwkt_reltoken(&pti
->pt_tty
.t_token
);
946 filt_ptcwrite (struct knote
*kn
, long hint
)
948 struct tty
*tp
= ((cdev_t
)kn
->kn_hook
)->si_tty
;
949 struct pt_ioctl
*pti
= ((cdev_t
)kn
->kn_hook
)->si_drv1
;
951 lwkt_gettoken(&pti
->pt_tty
.t_token
);
952 lwkt_gettoken(&tp
->t_token
);
953 if (tp
->t_state
& TS_ZOMBIE
) {
954 lwkt_reltoken(&tp
->t_token
);
955 lwkt_reltoken(&pti
->pt_tty
.t_token
);
956 kn
->kn_flags
|= (EV_EOF
| EV_NODATA
);
960 if (tp
->t_state
& TS_ISOPEN
&&
961 ((pti
->pt_flags
& PF_REMOTE
) ?
962 (tp
->t_canq
.c_cc
== 0) :
963 ((tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
< TTYHOG
- 2) ||
964 (tp
->t_canq
.c_cc
== 0 && (tp
->t_lflag
& ICANON
))))) {
965 kn
->kn_data
= tp
->t_canq
.c_cc
+ tp
->t_rawq
.c_cc
;
966 lwkt_reltoken(&tp
->t_token
);
967 lwkt_reltoken(&pti
->pt_tty
.t_token
);
970 lwkt_reltoken(&tp
->t_token
);
971 lwkt_reltoken(&pti
->pt_tty
.t_token
);
978 filt_ptcrdetach (struct knote
*kn
)
980 struct tty
*tp
= ((cdev_t
)kn
->kn_hook
)->si_tty
;
982 knote_remove(&tp
->t_rkq
.ki_note
, kn
);
986 filt_ptcwdetach (struct knote
*kn
)
988 struct tty
*tp
= ((cdev_t
)kn
->kn_hook
)->si_tty
;
990 knote_remove(&tp
->t_wkq
.ki_note
, kn
);
997 ptcwrite(struct dev_write_args
*ap
)
999 cdev_t dev
= ap
->a_head
.a_dev
;
1000 struct tty
*tp
= dev
->si_tty
;
1003 u_char locbuf
[BUFSIZ
];
1005 struct pt_ioctl
*pti
= dev
->si_drv1
;
1008 lwkt_gettoken(&pti
->pt_tty
.t_token
);
1009 lwkt_gettoken(&tp
->t_token
);
1011 if ((tp
->t_state
&TS_ISOPEN
) == 0)
1013 if (pti
->pt_flags
& PF_REMOTE
) {
1014 if (tp
->t_canq
.c_cc
)
1016 while ((ap
->a_uio
->uio_resid
> 0 || cc
> 0) &&
1017 tp
->t_canq
.c_cc
< TTYHOG
- 1) {
1019 cc
= (int)szmin(ap
->a_uio
->uio_resid
, BUFSIZ
);
1020 cc
= imin(cc
, TTYHOG
- 1 - tp
->t_canq
.c_cc
);
1022 error
= uiomove(cp
, (size_t)cc
, ap
->a_uio
);
1024 lwkt_reltoken(&tp
->t_token
);
1025 lwkt_reltoken(&pti
->pt_tty
.t_token
);
1028 /* check again for safety */
1029 if ((tp
->t_state
& TS_ISOPEN
) == 0) {
1030 /* adjust as usual */
1031 ap
->a_uio
->uio_resid
+= cc
;
1032 lwkt_reltoken(&tp
->t_token
);
1033 lwkt_reltoken(&pti
->pt_tty
.t_token
);
1038 cc
= clist_btoq((char *)cp
, cc
, &tp
->t_canq
);
1040 * XXX we don't guarantee that the canq size
1041 * is >= TTYHOG, so the above btoq() may
1042 * leave some bytes uncopied. However, space
1043 * is guaranteed for the null terminator if
1044 * we don't fail here since (TTYHOG - 1) is
1045 * not a multiple of CBSIZE.
1051 /* adjust for data copied in but not written */
1052 ap
->a_uio
->uio_resid
+= cc
;
1053 clist_putc(0, &tp
->t_canq
);
1055 wakeup(TSA_PTS_READ(tp
));
1056 lwkt_reltoken(&tp
->t_token
);
1057 lwkt_reltoken(&pti
->pt_tty
.t_token
);
1061 while (ap
->a_uio
->uio_resid
> 0 || cc
> 0) {
1063 cc
= (int)szmin(ap
->a_uio
->uio_resid
, BUFSIZ
);
1065 error
= uiomove(cp
, (size_t)cc
, ap
->a_uio
);
1067 lwkt_reltoken(&tp
->t_token
);
1068 lwkt_reltoken(&pti
->pt_tty
.t_token
);
1071 /* check again for safety */
1072 if ((tp
->t_state
& TS_ISOPEN
) == 0) {
1073 /* adjust for data copied in but not written */
1074 ap
->a_uio
->uio_resid
+= cc
;
1075 lwkt_reltoken(&tp
->t_token
);
1076 lwkt_reltoken(&pti
->pt_tty
.t_token
);
1081 if ((tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
) >= TTYHOG
- 2 &&
1082 (tp
->t_canq
.c_cc
> 0 || !(tp
->t_lflag
&ICANON
))) {
1083 wakeup(TSA_HUP_OR_INPUT(tp
));
1086 (*linesw
[tp
->t_line
].l_rint
)(*cp
++, tp
);
1092 lwkt_reltoken(&tp
->t_token
);
1093 lwkt_reltoken(&pti
->pt_tty
.t_token
);
1097 * Come here to wait for slave to open, for space
1098 * in outq, or space in rawq, or an empty canq.
1100 if ((tp
->t_state
& TS_CONNECTED
) == 0) {
1101 /* adjust for data copied in but not written */
1102 ap
->a_uio
->uio_resid
+= cc
;
1103 lwkt_reltoken(&tp
->t_token
);
1104 lwkt_reltoken(&pti
->pt_tty
.t_token
);
1107 if (ap
->a_ioflag
& IO_NDELAY
) {
1108 /* adjust for data copied in but not written */
1109 ap
->a_uio
->uio_resid
+= cc
;
1111 lwkt_reltoken(&tp
->t_token
);
1112 lwkt_reltoken(&pti
->pt_tty
.t_token
);
1113 return (EWOULDBLOCK
);
1115 lwkt_reltoken(&tp
->t_token
);
1116 lwkt_reltoken(&pti
->pt_tty
.t_token
);
1119 error
= tsleep(TSA_PTC_WRITE(tp
), PCATCH
, "ptcout", 0);
1121 /* adjust for data copied in but not written */
1122 ap
->a_uio
->uio_resid
+= cc
;
1123 lwkt_reltoken(&tp
->t_token
);
1124 lwkt_reltoken(&pti
->pt_tty
.t_token
);
1132 ptyioctl(struct dev_ioctl_args
*ap
)
1134 cdev_t dev
= ap
->a_head
.a_dev
;
1135 struct tty
*tp
= dev
->si_tty
;
1136 struct pt_ioctl
*pti
= dev
->si_drv1
;
1137 u_char
*cc
= tp
->t_cc
;
1140 lwkt_gettoken(&pti
->pt_tty
.t_token
);
1141 lwkt_gettoken(&tp
->t_token
);
1143 if (dev_dflags(dev
) & D_MASTER
) {
1144 switch (ap
->a_cmd
) {
1148 * We avoid calling ttioctl on the controller since,
1149 * in that case, tp must be the controlling terminal.
1151 *(int *)ap
->a_data
= tp
->t_pgrp
? tp
->t_pgrp
->pg_id
: 0;
1152 lwkt_reltoken(&tp
->t_token
);
1153 lwkt_reltoken(&pti
->pt_tty
.t_token
);
1157 if (*(int *)ap
->a_data
) {
1158 if (pti
->pt_flags
& PF_UCNTL
) {
1159 lwkt_reltoken(&tp
->t_token
);
1160 lwkt_reltoken(&pti
->pt_tty
.t_token
);
1163 pti
->pt_flags
|= PF_PKT
;
1165 pti
->pt_flags
&= ~PF_PKT
;
1167 lwkt_reltoken(&tp
->t_token
);
1168 lwkt_reltoken(&pti
->pt_tty
.t_token
);
1172 if (*(int *)ap
->a_data
) {
1173 if (pti
->pt_flags
& PF_PKT
) {
1174 lwkt_reltoken(&tp
->t_token
);
1175 lwkt_reltoken(&pti
->pt_tty
.t_token
);
1178 pti
->pt_flags
|= PF_UCNTL
;
1180 pti
->pt_flags
&= ~PF_UCNTL
;
1182 lwkt_reltoken(&tp
->t_token
);
1183 lwkt_reltoken(&pti
->pt_tty
.t_token
);
1187 if (*(int *)ap
->a_data
)
1188 pti
->pt_flags
|= PF_REMOTE
;
1190 pti
->pt_flags
&= ~PF_REMOTE
;
1191 ttyflush(tp
, FREAD
|FWRITE
);
1192 lwkt_reltoken(&tp
->t_token
);
1193 lwkt_reltoken(&pti
->pt_tty
.t_token
);
1196 case TIOCISPTMASTER
:
1197 if ((pti
->pt_flags
& PF_UNIX98
) &&
1198 (pti
->devc
== dev
)) {
1199 lwkt_reltoken(&tp
->t_token
);
1200 lwkt_reltoken(&pti
->pt_tty
.t_token
);
1203 lwkt_reltoken(&tp
->t_token
);
1204 lwkt_reltoken(&pti
->pt_tty
.t_token
);
1210 * The rest of the ioctls shouldn't be called until
1211 * the slave is open.
1213 if ((tp
->t_state
& TS_ISOPEN
) == 0) {
1214 lwkt_reltoken(&tp
->t_token
);
1215 lwkt_reltoken(&pti
->pt_tty
.t_token
);
1219 switch (ap
->a_cmd
) {
1225 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
1226 * ttywflush(tp) will hang if there are characters in
1229 ndflush(&tp
->t_outq
, tp
->t_outq
.c_cc
);
1233 if (*(unsigned int *)ap
->a_data
>= NSIG
||
1234 *(unsigned int *)ap
->a_data
== 0) {
1235 lwkt_reltoken(&tp
->t_token
);
1236 lwkt_reltoken(&pti
->pt_tty
.t_token
);
1239 if ((tp
->t_lflag
&NOFLSH
) == 0)
1240 ttyflush(tp
, FREAD
|FWRITE
);
1241 pgsignal(tp
->t_pgrp
, *(unsigned int *)ap
->a_data
, 1);
1242 if ((*(unsigned int *)ap
->a_data
== SIGINFO
) &&
1243 ((tp
->t_lflag
&NOKERNINFO
) == 0))
1245 lwkt_reltoken(&tp
->t_token
);
1246 lwkt_reltoken(&pti
->pt_tty
.t_token
);
1250 if (ap
->a_cmd
== TIOCEXT
) {
1252 * When the EXTPROC bit is being toggled, we need
1253 * to send an TIOCPKT_IOCTL if the packet driver
1256 if (*(int *)ap
->a_data
) {
1257 if (pti
->pt_flags
& PF_PKT
) {
1258 pti
->pt_send
|= TIOCPKT_IOCTL
;
1259 ptcwakeup(tp
, FREAD
);
1261 tp
->t_lflag
|= EXTPROC
;
1263 if ((tp
->t_lflag
& EXTPROC
) &&
1264 (pti
->pt_flags
& PF_PKT
)) {
1265 pti
->pt_send
|= TIOCPKT_IOCTL
;
1266 ptcwakeup(tp
, FREAD
);
1268 tp
->t_lflag
&= ~EXTPROC
;
1270 lwkt_reltoken(&tp
->t_token
);
1271 lwkt_reltoken(&pti
->pt_tty
.t_token
);
1274 error
= (*linesw
[tp
->t_line
].l_ioctl
)(tp
, ap
->a_cmd
, ap
->a_data
,
1275 ap
->a_fflag
, ap
->a_cred
);
1276 if (error
== ENOIOCTL
)
1277 error
= ttioctl(tp
, ap
->a_cmd
, ap
->a_data
, ap
->a_fflag
);
1278 if (error
== ENOIOCTL
) {
1279 if (pti
->pt_flags
& PF_UCNTL
&&
1280 (ap
->a_cmd
& ~0xff) == UIOCCMD(0)) {
1281 if (ap
->a_cmd
& 0xff) {
1282 pti
->pt_ucntl
= (u_char
)ap
->a_cmd
;
1283 ptcwakeup(tp
, FREAD
);
1285 lwkt_reltoken(&tp
->t_token
);
1286 lwkt_reltoken(&pti
->pt_tty
.t_token
);
1292 * If external processing and packet mode send ioctl packet.
1294 if ((tp
->t_lflag
&EXTPROC
) && (pti
->pt_flags
& PF_PKT
)) {
1299 pti
->pt_send
|= TIOCPKT_IOCTL
;
1300 ptcwakeup(tp
, FREAD
);
1305 stop
= (tp
->t_iflag
& IXON
) && CCEQ(cc
[VSTOP
], CTRL('s'))
1306 && CCEQ(cc
[VSTART
], CTRL('q'));
1307 if (pti
->pt_flags
& PF_NOSTOP
) {
1309 pti
->pt_send
&= ~TIOCPKT_NOSTOP
;
1310 pti
->pt_send
|= TIOCPKT_DOSTOP
;
1311 pti
->pt_flags
&= ~PF_NOSTOP
;
1312 ptcwakeup(tp
, FREAD
);
1316 pti
->pt_send
&= ~TIOCPKT_DOSTOP
;
1317 pti
->pt_send
|= TIOCPKT_NOSTOP
;
1318 pti
->pt_flags
|= PF_NOSTOP
;
1319 ptcwakeup(tp
, FREAD
);
1322 lwkt_reltoken(&tp
->t_token
);
1323 lwkt_reltoken(&pti
->pt_tty
.t_token
);
1329 static void ptc_drvinit (void *unused
);
1331 SYSCTL_INT(_kern
, OID_AUTO
, pty_debug
, CTLFLAG_RW
, &pty_debug_level
,
1332 0, "Change pty debug level");
1335 ptc_drvinit(void *unused
)
1341 * Create the clonable base device.
1343 make_autoclone_dev(&ptc_ops
, &DEVFS_CLONE_BITMAP(pty
), ptyclone
,
1344 0, 0, 0666, "ptmx");
1345 ptis
= kmalloc(sizeof(struct pt_ioctl
*) * MAXPTYS
, M_PTY
,
1348 for (i
= 0; i
< 256; i
++) {
1353 SYSINIT(ptcdev
, SI_SUB_DRIVERS
, SI_ORDER_MIDDLE
+ CDEV_MAJOR_C
,