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 <sys/param.h>
49 #include <sys/systm.h>
53 #include <sys/ttydefaults.h> /* for TTYDEF_* */
55 #include <sys/fcntl.h>
56 #include <sys/kernel.h>
57 #include <sys/vnode.h>
58 #include <sys/signalvar.h>
59 #include <sys/malloc.h>
60 #include <sys/device.h>
61 #include <sys/thread2.h>
62 #include <sys/devfs.h>
64 #include <sys/sysctl.h>
66 MALLOC_DEFINE(M_PTY
, "ptys", "pty data structures");
68 static void ptsstart (struct tty
*tp
);
69 static void ptsstop (struct tty
*tp
, int rw
);
70 static void ptsunhold (struct tty
*tp
);
71 static void ptcwakeup (struct tty
*tp
, int flag
);
72 static void ptyinit (int n
);
73 static int filt_ptcread (struct knote
*kn
, long hint
);
74 static void filt_ptcrdetach (struct knote
*kn
);
75 static int filt_ptcwrite (struct knote
*kn
, long hint
);
76 static void filt_ptcwdetach (struct knote
*kn
);
78 static d_open_t ptsopen
;
79 static d_close_t ptsclose
;
80 static d_read_t ptsread
;
81 static d_write_t ptswrite
;
82 static d_ioctl_t ptyioctl
;
83 static d_open_t ptcopen
;
84 static d_close_t ptcclose
;
85 static d_read_t ptcread
;
86 static d_write_t ptcwrite
;
87 static d_kqfilter_t ptckqfilter
;
89 DEVFS_DEFINE_CLONE_BITMAP(pty
);
91 static d_clone_t ptyclone
;
93 static int pty_debug_level
= 0;
95 static struct dev_ops pts98_ops
= {
96 { "pts98", 0, D_TTY
| D_MPSAFE
},
102 .d_kqfilter
= ttykqfilter
,
103 .d_revoke
= ttyrevoke
106 static struct dev_ops ptc98_ops
= {
107 { "ptc98", 0, D_TTY
| D_MASTER
| D_MPSAFE
},
113 .d_kqfilter
= ptckqfilter
,
114 .d_revoke
= ttyrevoke
117 static struct dev_ops pts_ops
= {
118 { "pts", 0, D_TTY
| D_MPSAFE
},
124 .d_kqfilter
= ttykqfilter
,
125 .d_revoke
= ttyrevoke
128 #define CDEV_MAJOR_C 6
129 static struct dev_ops ptc_ops
= {
130 { "ptc", 0, D_TTY
| D_MASTER
| D_MPSAFE
},
136 .d_kqfilter
= ptckqfilter
,
137 .d_revoke
= ttyrevoke
140 #define BUFSIZ 100 /* Chunk size iomoved to/from user */
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";
193 /* For now we only map the lower 8 bits of the minor */
197 pt
= kmalloc(sizeof(*pt
), M_PTY
, M_WAITOK
| M_ZERO
);
198 pt
->devs
= devs
= make_dev(&pts_ops
, n
,
199 0, 0, 0666, "tty%c%c", names
[n
/ 32], hex2ascii(n
% 32));
200 pt
->devc
= devc
= make_dev(&ptc_ops
, n
,
201 0, 0, 0666, "pty%c%c", names
[n
/ 32], hex2ascii(n
% 32));
203 pt
->pt_tty
.t_dev
= devs
;
205 devs
->si_drv1
= devc
->si_drv1
= pt
;
206 devs
->si_tty
= devc
->si_tty
= &pt
->pt_tty
;
207 devs
->si_flags
|= SI_OVERRIDE
; /* uid, gid, perms from dev */
208 devc
->si_flags
|= SI_OVERRIDE
; /* uid, gid, perms from dev */
209 ttyregister(&pt
->pt_tty
);
213 ptyclone(struct dev_clone_args
*ap
)
219 * Limit the number of unix98 pty (slave) devices to 1000, as
220 * the utmp(5) format only allows for 8 bytes for the tty,
222 * If this limit is reached, we don't clone and return error
225 unit
= devfs_clone_bitmap_get(&DEVFS_CLONE_BITMAP(pty
), 1000);
232 pt
= kmalloc(sizeof(*pt
), M_PTY
, M_WAITOK
| M_ZERO
);
234 pt
->devc
= make_only_dev(&ptc98_ops
, unit
,
236 0, 0600, "ptm/%d", unit
);
237 pt
->devs
= make_dev(&pts98_ops
, unit
,
239 GID_TTY
, 0620, "pts/%d", unit
);
240 ap
->a_dev
= pt
->devc
;
242 pt
->devs
->si_flags
|= SI_OVERRIDE
; /* uid, gid, perms from dev */
243 pt
->devc
->si_flags
|= SI_OVERRIDE
; /* uid, gid, perms from dev */
245 pt
->pt_tty
.t_dev
= pt
->devs
;
246 pt
->pt_flags
|= PF_UNIX98
;
247 pt
->pt_uminor
= unit
;
248 pt
->devs
->si_drv1
= pt
->devc
->si_drv1
= pt
;
249 pt
->devs
->si_tty
= pt
->devc
->si_tty
= &pt
->pt_tty
;
251 ttyregister(&pt
->pt_tty
);
257 * pti_hold() prevents the pti from being destroyed due to a termination
258 * while a pt*open() is blocked.
260 * This function returns non-zero if we cannot hold due to a termination
263 * NOTE: Must be called with tty_token held
266 pti_hold(struct pt_ioctl
*pti
)
268 if (pti
->pt_flags
& PF_TERMINATED
)
275 * pti_done() releases the reference and checks to see if both sides have
276 * been closed on a unix98 pty, allowing us to destroy the device and
279 * We do not release resources on non-unix98 ptys. Those are left
280 * statically allocated.
283 pti_done(struct pt_ioctl
*pti
)
285 lwkt_gettoken(&tty_token
);
286 if (--pti
->pt_refs
== 0) {
291 * Only unix09 ptys are freed up
293 if ((pti
->pt_flags
& PF_UNIX98
) == 0) {
294 lwkt_reltoken(&tty_token
);
299 * Interlock open attempts against termination by setting
300 * PF_TERMINATED. This allows us to block while cleaning
301 * out the device infrastructure.
303 * Do not terminate the tty if it still has a session
304 * association (t_refs).
306 if ((pti
->pt_flags
& (PF_SOPEN
|PF_MOPEN
)) == 0 &&
307 pti
->pt_tty
.t_refs
== 0) {
308 pti
->pt_flags
|= PF_TERMINATED
;
309 uminor_no
= pti
->pt_uminor
;
311 if ((dev
= pti
->devs
) != NULL
) {
316 if ((dev
= pti
->devc
) != NULL
) {
321 ttyunregister(&pti
->pt_tty
);
322 devfs_clone_bitmap_put(&DEVFS_CLONE_BITMAP(pty
),
327 lwkt_reltoken(&tty_token
);
332 ptsopen(struct dev_open_args
*ap
)
334 cdev_t dev
= ap
->a_head
.a_dev
;
337 struct pt_ioctl
*pti
;
340 * The pti will already be assigned by the clone code or
341 * pre-created if a non-unix 98 pty. If si_drv1 is NULL
342 * we are somehow racing a unix98 termination.
344 if (dev
->si_drv1
== NULL
)
348 lwkt_gettoken(&tty_token
);
350 lwkt_reltoken(&tty_token
);
357 * Reinit most of the tty state if it isn't open. Handle
360 if ((tp
->t_state
& TS_ISOPEN
) == 0) {
361 ttychars(tp
); /* Set up default chars */
362 tp
->t_iflag
= TTYDEF_IFLAG
;
363 tp
->t_oflag
= TTYDEF_OFLAG
;
364 tp
->t_lflag
= TTYDEF_LFLAG
;
365 tp
->t_cflag
= TTYDEF_CFLAG
;
366 tp
->t_ispeed
= tp
->t_ospeed
= TTYDEF_SPEED
;
367 } else if ((tp
->t_state
& TS_XCLUDE
) &&
368 priv_check_cred(ap
->a_cred
, PRIV_ROOT
, 0)) {
370 lwkt_reltoken(&tty_token
);
372 } else if (pti
->pt_prison
!= ap
->a_cred
->cr_prison
) {
374 lwkt_reltoken(&tty_token
);
379 * If the ptc is already present this will connect us up. It
380 * is unclear if this is actually needed.
382 * If neither side is open be sure to clear any left over
383 * ZOMBIE state before continuing.
386 (void)(*linesw
[tp
->t_line
].l_modem
)(tp
, 1);
387 else if ((pti
->pt_flags
& PF_SOPEN
) == 0)
388 tp
->t_state
&= ~TS_ZOMBIE
;
391 * Wait for the carrier (ptc side)
393 while ((tp
->t_state
& TS_CARR_ON
) == 0) {
394 if (ap
->a_oflags
& FNONBLOCK
)
396 error
= ttysleep(tp
, TSA_CARR_ON(tp
), PCATCH
, "ptsopn", 0);
399 lwkt_reltoken(&tty_token
);
405 * Mark the tty open and mark the slave side as being open.
407 error
= (*linesw
[tp
->t_line
].l_open
)(dev
, tp
);
410 pti
->pt_flags
|= PF_SOPEN
;
411 pti
->pt_flags
&= ~PF_SCLOSED
;
412 ptcwakeup(tp
, FREAD
|FWRITE
);
416 lwkt_reltoken(&tty_token
);
421 ptsclose(struct dev_close_args
*ap
)
423 cdev_t dev
= ap
->a_head
.a_dev
;
425 struct pt_ioctl
*pti
= dev
->si_drv1
;
428 lwkt_gettoken(&tty_token
);
430 panic("ptsclose on terminated pti");
433 * Disconnect the slave side
436 err
= (*linesw
[tp
->t_line
].l_close
)(tp
, ap
->a_fflag
);
437 ptsstop(tp
, FREAD
|FWRITE
);
438 ttyclose(tp
); /* clears t_state */
441 * Mark the pts side closed and signal the ptc. Do not mark the
442 * tty a zombie... that is, allow the tty to be re-opened as long
443 * as the ptc is still open. The ptc will read() EOFs until the
444 * pts side is reopened or the ptc is closed.
446 * xterm() depends on this behavior as it will revoke() the pts
447 * and then reopen it after the (unnecessary old code) chmod.
449 pti
->pt_flags
&= ~PF_SOPEN
;
450 pti
->pt_flags
|= PF_SCLOSED
;
452 ptcwakeup(tp
, FREAD
);
454 lwkt_reltoken(&tty_token
);
459 ptsread(struct dev_read_args
*ap
)
461 cdev_t dev
= ap
->a_head
.a_dev
;
462 struct proc
*p
= curproc
;
463 struct tty
*tp
= dev
->si_tty
;
464 struct pt_ioctl
*pti
= dev
->si_drv1
;
469 lp
= curthread
->td_lwp
;
471 lwkt_gettoken(&tty_token
);
473 if (pti
->pt_flags
& PF_REMOTE
) {
474 while (isbackground(p
, tp
)) {
475 if (SIGISMEMBER(p
->p_sigignore
, SIGTTIN
) ||
476 SIGISMEMBER(lp
->lwp_sigmask
, SIGTTIN
) ||
477 p
->p_pgrp
->pg_jobc
== 0 ||
478 (p
->p_flags
& P_PPWAIT
)) {
479 lwkt_reltoken(&tty_token
);
482 pgsignal(p
->p_pgrp
, SIGTTIN
, 1);
483 error
= ttysleep(tp
, &lbolt
, PCATCH
, "ptsbg", 0);
485 lwkt_reltoken(&tty_token
);
489 if (tp
->t_canq
.c_cc
== 0) {
490 if (ap
->a_ioflag
& IO_NDELAY
) {
491 lwkt_reltoken(&tty_token
);
492 return (EWOULDBLOCK
);
494 error
= ttysleep(tp
, TSA_PTS_READ(tp
), PCATCH
,
497 lwkt_reltoken(&tty_token
);
502 while (tp
->t_canq
.c_cc
> 1 && ap
->a_uio
->uio_resid
> 0)
503 if (ureadc(clist_getc(&tp
->t_canq
), ap
->a_uio
) < 0) {
507 if (tp
->t_canq
.c_cc
== 1)
508 clist_getc(&tp
->t_canq
);
509 if (tp
->t_canq
.c_cc
) {
510 lwkt_reltoken(&tty_token
);
515 error
= (*linesw
[tp
->t_line
].l_read
)(tp
, ap
->a_uio
, ap
->a_ioflag
);
516 ptcwakeup(tp
, FWRITE
);
517 lwkt_reltoken(&tty_token
);
522 * Write to pseudo-tty.
523 * Wakeups of controlling tty will happen
524 * indirectly, when tty driver calls ptsstart.
527 ptswrite(struct dev_write_args
*ap
)
529 cdev_t dev
= ap
->a_head
.a_dev
;
533 lwkt_gettoken(&tty_token
);
535 if (tp
->t_oproc
== NULL
) {
536 lwkt_reltoken(&tty_token
);
539 ret
= ((*linesw
[tp
->t_line
].l_write
)(tp
, ap
->a_uio
, ap
->a_ioflag
));
540 lwkt_reltoken(&tty_token
);
545 * Start output on pseudo-tty.
546 * Wake up process selecting or sleeping for input from controlling tty.
549 ptsstart(struct tty
*tp
)
551 lwkt_gettoken(&tty_token
);
552 struct pt_ioctl
*pti
= tp
->t_dev
->si_drv1
;
554 if (tp
->t_state
& TS_TTSTOP
) {
555 lwkt_reltoken(&tty_token
);
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
);
620 * If the slave side is not yet open clear any left over zombie
621 * state before doing our modem control.
623 if ((pti
->pt_flags
& PF_SOPEN
) == 0)
624 tp
->t_state
&= ~TS_ZOMBIE
;
626 tp
->t_oproc
= ptsstart
;
627 tp
->t_stop
= ptsstop
;
628 tp
->t_unhold
= ptsunhold
;
633 (void)(*linesw
[tp
->t_line
].l_modem
)(tp
, 1);
635 tp
->t_lflag
&= ~EXTPROC
;
636 pti
->pt_prison
= ap
->a_cred
->cr_prison
;
637 pti
->pt_flags
&= ~PF_PTCSTATEMASK
;
641 pti
->devs
->si_uid
= ap
->a_cred
->cr_uid
;
642 pti
->devs
->si_gid
= 0;
643 pti
->devs
->si_perms
= 0600;
644 pti
->devc
->si_uid
= ap
->a_cred
->cr_uid
;
645 pti
->devc
->si_gid
= 0;
646 pti
->devc
->si_perms
= 0600;
649 * Mark master side open. This does not cause any events
652 pti
->pt_flags
|= PF_MOPEN
;
655 lwkt_reltoken(&tty_token
);
660 ptcclose(struct dev_close_args
*ap
)
662 cdev_t dev
= ap
->a_head
.a_dev
;
664 struct pt_ioctl
*pti
= dev
->si_drv1
;
666 lwkt_gettoken(&tty_token
);
668 panic("ptcclose on terminated pti");
671 (void)(*linesw
[tp
->t_line
].l_modem
)(tp
, 0);
674 * Mark the master side closed. If the slave is still open
675 * mark the tty ZOMBIE, preventing any new action until both
678 * NOTE: The ttyflush() will wake up the slave once we've
679 * set appropriate flags. The ZOMBIE flag will be
680 * cleared when the slave side is closed.
682 pti
->pt_flags
&= ~PF_MOPEN
;
683 if (pti
->pt_flags
& PF_SOPEN
)
684 tp
->t_state
|= TS_ZOMBIE
;
687 * Turn off the carrier and disconnect. This will notify the slave
690 if (tp
->t_state
& TS_ISOPEN
) {
691 tp
->t_state
&= ~(TS_CARR_ON
| TS_CONNECTED
);
692 ttyflush(tp
, FREAD
| FWRITE
);
694 tp
->t_oproc
= NULL
; /* mark closed */
696 pti
->pt_prison
= NULL
;
697 pti
->devs
->si_uid
= 0;
698 pti
->devs
->si_gid
= 0;
699 pti
->devs
->si_perms
= 0666;
700 pti
->devc
->si_uid
= 0;
701 pti
->devc
->si_gid
= 0;
702 pti
->devc
->si_perms
= 0666;
706 lwkt_reltoken(&tty_token
);
711 ptcread(struct dev_read_args
*ap
)
713 cdev_t dev
= ap
->a_head
.a_dev
;
714 struct tty
*tp
= dev
->si_tty
;
715 struct pt_ioctl
*pti
= dev
->si_drv1
;
719 lwkt_gettoken(&tty_token
);
721 * We want to block until the slave
722 * is open, and there's something to read;
723 * but if we lost the slave or we're NBIO,
724 * then return the appropriate error instead.
727 if (tp
->t_state
&TS_ISOPEN
) {
728 if ((pti
->pt_flags
& PF_PKT
) && pti
->pt_send
) {
729 error
= ureadc((int)pti
->pt_send
, ap
->a_uio
);
731 lwkt_reltoken(&tty_token
);
734 if (pti
->pt_send
& TIOCPKT_IOCTL
) {
735 cc
= (int)szmin(ap
->a_uio
->uio_resid
,
736 sizeof(tp
->t_termios
));
737 uiomove((caddr_t
)&tp
->t_termios
, cc
,
741 lwkt_reltoken(&tty_token
);
744 if ((pti
->pt_flags
& PF_UCNTL
) && pti
->pt_ucntl
) {
745 error
= ureadc((int)pti
->pt_ucntl
, ap
->a_uio
);
747 lwkt_reltoken(&tty_token
);
751 lwkt_reltoken(&tty_token
);
754 if (tp
->t_outq
.c_cc
&& (tp
->t_state
&TS_TTSTOP
) == 0)
757 if ((tp
->t_state
& TS_CONNECTED
) == 0) {
758 lwkt_reltoken(&tty_token
);
759 return (0); /* EOF */
761 if (ap
->a_ioflag
& IO_NDELAY
) {
762 lwkt_reltoken(&tty_token
);
763 return (EWOULDBLOCK
);
765 error
= tsleep(TSA_PTC_READ(tp
), PCATCH
, "ptcin", 0);
767 lwkt_reltoken(&tty_token
);
771 if (pti
->pt_flags
& (PF_PKT
|PF_UCNTL
))
772 error
= ureadc(0, ap
->a_uio
);
773 while (ap
->a_uio
->uio_resid
> 0 && error
== 0) {
774 cc
= q_to_b(&tp
->t_outq
, buf
,
775 (int)szmin(ap
->a_uio
->uio_resid
, BUFSIZ
));
778 error
= uiomove(buf
, (size_t)cc
, ap
->a_uio
);
781 lwkt_reltoken(&tty_token
);
786 ptsstop(struct tty
*tp
, int flush
)
788 struct pt_ioctl
*pti
= tp
->t_dev
->si_drv1
;
791 lwkt_gettoken(&tty_token
);
792 /* note: FLUSHREAD and FLUSHWRITE already ok */
795 flush
= TIOCPKT_STOP
;
796 pti
->pt_flags
|= PF_STOPPED
;
798 pti
->pt_flags
&= ~PF_STOPPED
;
800 pti
->pt_send
|= flush
;
801 /* change of perspective */
810 lwkt_reltoken(&tty_token
);
814 * ttyunhold() calls us instead of just decrementing tp->t_refs. This
815 * is needed because a session can hold onto a pts (half closed state)
816 * even if there are no live file descriptors. Without the callback
820 ptsunhold(struct tty
*tp
)
822 struct pt_ioctl
*pti
= tp
->t_dev
->si_drv1
;
824 lwkt_gettoken(&tty_token
);
828 lwkt_reltoken(&tty_token
);
832 * kqueue ops for pseudo-terminals.
834 static struct filterops ptcread_filtops
=
835 { FILTEROP_ISFD
|FILTEROP_MPSAFE
, NULL
, filt_ptcrdetach
, filt_ptcread
};
836 static struct filterops ptcwrite_filtops
=
837 { FILTEROP_ISFD
|FILTEROP_MPSAFE
, NULL
, filt_ptcwdetach
, filt_ptcwrite
};
840 ptckqfilter(struct dev_kqfilter_args
*ap
)
842 cdev_t dev
= ap
->a_head
.a_dev
;
843 struct knote
*kn
= ap
->a_kn
;
844 struct tty
*tp
= dev
->si_tty
;
848 switch (kn
->kn_filter
) {
850 klist
= &tp
->t_rkq
.ki_note
;
851 kn
->kn_fop
= &ptcread_filtops
;
854 klist
= &tp
->t_wkq
.ki_note
;
855 kn
->kn_fop
= &ptcwrite_filtops
;
858 ap
->a_result
= EOPNOTSUPP
;
862 kn
->kn_hook
= (caddr_t
)dev
;
863 knote_insert(klist
, kn
);
868 filt_ptcread (struct knote
*kn
, long hint
)
870 struct tty
*tp
= ((cdev_t
)kn
->kn_hook
)->si_tty
;
871 struct pt_ioctl
*pti
= ((cdev_t
)kn
->kn_hook
)->si_drv1
;
873 lwkt_gettoken(&tty_token
);
874 if ((tp
->t_state
& TS_ZOMBIE
) || (pti
->pt_flags
& PF_SCLOSED
)) {
875 lwkt_reltoken(&tty_token
);
876 kn
->kn_flags
|= (EV_EOF
| EV_NODATA
);
880 if ((tp
->t_state
& TS_ISOPEN
) &&
881 ((tp
->t_outq
.c_cc
&& (tp
->t_state
& TS_TTSTOP
) == 0) ||
882 ((pti
->pt_flags
& PF_PKT
) && pti
->pt_send
) ||
883 ((pti
->pt_flags
& PF_UCNTL
) && pti
->pt_ucntl
))) {
884 kn
->kn_data
= tp
->t_outq
.c_cc
;
885 lwkt_reltoken(&tty_token
);
888 lwkt_reltoken(&tty_token
);
894 filt_ptcwrite (struct knote
*kn
, long hint
)
896 struct tty
*tp
= ((cdev_t
)kn
->kn_hook
)->si_tty
;
897 struct pt_ioctl
*pti
= ((cdev_t
)kn
->kn_hook
)->si_drv1
;
899 lwkt_gettoken(&tty_token
);
900 if (tp
->t_state
& TS_ZOMBIE
) {
901 lwkt_reltoken(&tty_token
);
902 kn
->kn_flags
|= (EV_EOF
| EV_NODATA
);
906 if (tp
->t_state
& TS_ISOPEN
&&
907 ((pti
->pt_flags
& PF_REMOTE
) ?
908 (tp
->t_canq
.c_cc
== 0) :
909 ((tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
< TTYHOG
- 2) ||
910 (tp
->t_canq
.c_cc
== 0 && (tp
->t_lflag
& ICANON
))))) {
911 kn
->kn_data
= tp
->t_canq
.c_cc
+ tp
->t_rawq
.c_cc
;
912 lwkt_reltoken(&tty_token
);
915 lwkt_reltoken(&tty_token
);
922 filt_ptcrdetach (struct knote
*kn
)
924 struct tty
*tp
= ((cdev_t
)kn
->kn_hook
)->si_tty
;
926 knote_remove(&tp
->t_rkq
.ki_note
, kn
);
930 filt_ptcwdetach (struct knote
*kn
)
932 struct tty
*tp
= ((cdev_t
)kn
->kn_hook
)->si_tty
;
934 knote_remove(&tp
->t_wkq
.ki_note
, kn
);
941 ptcwrite(struct dev_write_args
*ap
)
943 cdev_t dev
= ap
->a_head
.a_dev
;
944 struct tty
*tp
= dev
->si_tty
;
947 u_char locbuf
[BUFSIZ
];
949 struct pt_ioctl
*pti
= dev
->si_drv1
;
952 lwkt_gettoken(&tty_token
);
954 if ((tp
->t_state
&TS_ISOPEN
) == 0)
956 if (pti
->pt_flags
& PF_REMOTE
) {
959 while ((ap
->a_uio
->uio_resid
> 0 || cc
> 0) &&
960 tp
->t_canq
.c_cc
< TTYHOG
- 1) {
962 cc
= (int)szmin(ap
->a_uio
->uio_resid
, BUFSIZ
);
963 cc
= imin(cc
, TTYHOG
- 1 - tp
->t_canq
.c_cc
);
965 error
= uiomove(cp
, (size_t)cc
, ap
->a_uio
);
967 lwkt_reltoken(&tty_token
);
970 /* check again for safety */
971 if ((tp
->t_state
& TS_ISOPEN
) == 0) {
972 /* adjust as usual */
973 ap
->a_uio
->uio_resid
+= cc
;
974 lwkt_reltoken(&tty_token
);
979 cc
= b_to_q((char *)cp
, cc
, &tp
->t_canq
);
981 * XXX we don't guarantee that the canq size
982 * is >= TTYHOG, so the above b_to_q() may
983 * leave some bytes uncopied. However, space
984 * is guaranteed for the null terminator if
985 * we don't fail here since (TTYHOG - 1) is
986 * not a multiple of CBSIZE.
992 /* adjust for data copied in but not written */
993 ap
->a_uio
->uio_resid
+= cc
;
994 clist_putc(0, &tp
->t_canq
);
996 wakeup(TSA_PTS_READ(tp
));
997 lwkt_reltoken(&tty_token
);
1000 while (ap
->a_uio
->uio_resid
> 0 || cc
> 0) {
1002 cc
= (int)szmin(ap
->a_uio
->uio_resid
, BUFSIZ
);
1004 error
= uiomove(cp
, (size_t)cc
, ap
->a_uio
);
1006 lwkt_reltoken(&tty_token
);
1009 /* check again for safety */
1010 if ((tp
->t_state
& TS_ISOPEN
) == 0) {
1011 /* adjust for data copied in but not written */
1012 ap
->a_uio
->uio_resid
+= cc
;
1013 lwkt_reltoken(&tty_token
);
1018 if ((tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
) >= TTYHOG
- 2 &&
1019 (tp
->t_canq
.c_cc
> 0 || !(tp
->t_lflag
&ICANON
))) {
1020 wakeup(TSA_HUP_OR_INPUT(tp
));
1023 (*linesw
[tp
->t_line
].l_rint
)(*cp
++, tp
);
1029 lwkt_reltoken(&tty_token
);
1033 * Come here to wait for slave to open, for space
1034 * in outq, or space in rawq, or an empty canq.
1036 if ((tp
->t_state
& TS_CONNECTED
) == 0) {
1037 /* adjust for data copied in but not written */
1038 ap
->a_uio
->uio_resid
+= cc
;
1039 lwkt_reltoken(&tty_token
);
1042 if (ap
->a_ioflag
& IO_NDELAY
) {
1043 /* adjust for data copied in but not written */
1044 ap
->a_uio
->uio_resid
+= cc
;
1046 lwkt_reltoken(&tty_token
);
1047 return (EWOULDBLOCK
);
1049 lwkt_reltoken(&tty_token
);
1052 error
= tsleep(TSA_PTC_WRITE(tp
), PCATCH
, "ptcout", 0);
1054 /* adjust for data copied in but not written */
1055 ap
->a_uio
->uio_resid
+= cc
;
1056 lwkt_reltoken(&tty_token
);
1064 ptyioctl(struct dev_ioctl_args
*ap
)
1066 cdev_t dev
= ap
->a_head
.a_dev
;
1067 struct tty
*tp
= dev
->si_tty
;
1068 struct pt_ioctl
*pti
= dev
->si_drv1
;
1069 u_char
*cc
= tp
->t_cc
;
1072 lwkt_gettoken(&tty_token
);
1073 if (dev_dflags(dev
) & D_MASTER
) {
1074 switch (ap
->a_cmd
) {
1078 * We avoid calling ttioctl on the controller since,
1079 * in that case, tp must be the controlling terminal.
1081 *(int *)ap
->a_data
= tp
->t_pgrp
? tp
->t_pgrp
->pg_id
: 0;
1082 lwkt_reltoken(&tty_token
);
1086 if (*(int *)ap
->a_data
) {
1087 if (pti
->pt_flags
& PF_UCNTL
) {
1088 lwkt_reltoken(&tty_token
);
1091 pti
->pt_flags
|= PF_PKT
;
1093 pti
->pt_flags
&= ~PF_PKT
;
1095 lwkt_reltoken(&tty_token
);
1099 if (*(int *)ap
->a_data
) {
1100 if (pti
->pt_flags
& PF_PKT
) {
1101 lwkt_reltoken(&tty_token
);
1104 pti
->pt_flags
|= PF_UCNTL
;
1106 pti
->pt_flags
&= ~PF_UCNTL
;
1108 lwkt_reltoken(&tty_token
);
1112 if (*(int *)ap
->a_data
)
1113 pti
->pt_flags
|= PF_REMOTE
;
1115 pti
->pt_flags
&= ~PF_REMOTE
;
1116 ttyflush(tp
, FREAD
|FWRITE
);
1117 lwkt_reltoken(&tty_token
);
1120 case TIOCISPTMASTER
:
1121 if ((pti
->pt_flags
& PF_UNIX98
) &&
1122 (pti
->devc
== dev
)) {
1123 lwkt_reltoken(&tty_token
);
1126 lwkt_reltoken(&tty_token
);
1132 * The rest of the ioctls shouldn't be called until
1133 * the slave is open.
1135 if ((tp
->t_state
& TS_ISOPEN
) == 0) {
1136 lwkt_reltoken(&tty_token
);
1140 switch (ap
->a_cmd
) {
1146 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
1147 * ttywflush(tp) will hang if there are characters in
1150 ndflush(&tp
->t_outq
, tp
->t_outq
.c_cc
);
1154 if (*(unsigned int *)ap
->a_data
>= NSIG
||
1155 *(unsigned int *)ap
->a_data
== 0) {
1156 lwkt_reltoken(&tty_token
);
1159 if ((tp
->t_lflag
&NOFLSH
) == 0)
1160 ttyflush(tp
, FREAD
|FWRITE
);
1161 pgsignal(tp
->t_pgrp
, *(unsigned int *)ap
->a_data
, 1);
1162 if ((*(unsigned int *)ap
->a_data
== SIGINFO
) &&
1163 ((tp
->t_lflag
&NOKERNINFO
) == 0))
1165 lwkt_reltoken(&tty_token
);
1169 if (ap
->a_cmd
== TIOCEXT
) {
1171 * When the EXTPROC bit is being toggled, we need
1172 * to send an TIOCPKT_IOCTL if the packet driver
1175 if (*(int *)ap
->a_data
) {
1176 if (pti
->pt_flags
& PF_PKT
) {
1177 pti
->pt_send
|= TIOCPKT_IOCTL
;
1178 ptcwakeup(tp
, FREAD
);
1180 tp
->t_lflag
|= EXTPROC
;
1182 if ((tp
->t_lflag
& EXTPROC
) &&
1183 (pti
->pt_flags
& PF_PKT
)) {
1184 pti
->pt_send
|= TIOCPKT_IOCTL
;
1185 ptcwakeup(tp
, FREAD
);
1187 tp
->t_lflag
&= ~EXTPROC
;
1189 lwkt_reltoken(&tty_token
);
1192 error
= (*linesw
[tp
->t_line
].l_ioctl
)(tp
, ap
->a_cmd
, ap
->a_data
,
1193 ap
->a_fflag
, ap
->a_cred
);
1194 if (error
== ENOIOCTL
)
1195 error
= ttioctl(tp
, ap
->a_cmd
, ap
->a_data
, ap
->a_fflag
);
1196 if (error
== ENOIOCTL
) {
1197 if (pti
->pt_flags
& PF_UCNTL
&&
1198 (ap
->a_cmd
& ~0xff) == UIOCCMD(0)) {
1199 if (ap
->a_cmd
& 0xff) {
1200 pti
->pt_ucntl
= (u_char
)ap
->a_cmd
;
1201 ptcwakeup(tp
, FREAD
);
1203 lwkt_reltoken(&tty_token
);
1209 * If external processing and packet mode send ioctl packet.
1211 if ((tp
->t_lflag
&EXTPROC
) && (pti
->pt_flags
& PF_PKT
)) {
1216 pti
->pt_send
|= TIOCPKT_IOCTL
;
1217 ptcwakeup(tp
, FREAD
);
1222 stop
= (tp
->t_iflag
& IXON
) && CCEQ(cc
[VSTOP
], CTRL('s'))
1223 && CCEQ(cc
[VSTART
], CTRL('q'));
1224 if (pti
->pt_flags
& PF_NOSTOP
) {
1226 pti
->pt_send
&= ~TIOCPKT_NOSTOP
;
1227 pti
->pt_send
|= TIOCPKT_DOSTOP
;
1228 pti
->pt_flags
&= ~PF_NOSTOP
;
1229 ptcwakeup(tp
, FREAD
);
1233 pti
->pt_send
&= ~TIOCPKT_DOSTOP
;
1234 pti
->pt_send
|= TIOCPKT_NOSTOP
;
1235 pti
->pt_flags
|= PF_NOSTOP
;
1236 ptcwakeup(tp
, FREAD
);
1239 lwkt_reltoken(&tty_token
);
1244 static void ptc_drvinit (void *unused
);
1246 SYSCTL_INT(_kern
, OID_AUTO
, pty_debug
, CTLFLAG_RW
, &pty_debug_level
,
1247 0, "Change pty debug level");
1250 ptc_drvinit(void *unused
)
1256 * Create the clonable base device.
1258 make_autoclone_dev(&ptc_ops
, &DEVFS_CLONE_BITMAP(pty
), ptyclone
,
1259 0, 0, 0666, "ptmx");
1261 for (i
= 0; i
< 256; i
++) {
1266 SYSINIT(ptcdev
, SI_SUB_DRIVERS
, SI_ORDER_MIDDLE
+ CDEV_MAJOR_C
, ptc_drvinit
,