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>
54 #include <sys/fcntl.h>
55 #include <sys/kernel.h>
56 #include <sys/vnode.h>
57 #include <sys/signalvar.h>
58 #include <sys/malloc.h>
59 #include <sys/device.h>
60 #include <sys/thread2.h>
61 #include <sys/devfs.h>
63 #include <sys/sysctl.h>
65 MALLOC_DEFINE(M_PTY
, "ptys", "pty data structures");
67 static void ptsstart (struct tty
*tp
);
68 static void ptsstop (struct tty
*tp
, int rw
);
69 static void ptsunhold (struct tty
*tp
);
70 static void ptcwakeup (struct tty
*tp
, int flag
);
71 static void ptyinit (int n
);
72 static int filt_ptcread (struct knote
*kn
, long hint
);
73 static void filt_ptcrdetach (struct knote
*kn
);
74 static int filt_ptcwrite (struct knote
*kn
, long hint
);
75 static void filt_ptcwdetach (struct knote
*kn
);
77 static d_open_t ptsopen
;
78 static d_close_t ptsclose
;
79 static d_read_t ptsread
;
80 static d_write_t ptswrite
;
81 static d_ioctl_t ptyioctl
;
82 static d_open_t ptcopen
;
83 static d_close_t ptcclose
;
84 static d_read_t ptcread
;
85 static d_write_t ptcwrite
;
86 static d_kqfilter_t ptckqfilter
;
88 DEVFS_DEFINE_CLONE_BITMAP(pty
);
90 static d_clone_t ptyclone
;
92 static int pty_debug_level
= 0;
94 static struct dev_ops pts98_ops
= {
95 { "pts98", 0, D_TTY
| D_MPSAFE
},
101 .d_kqfilter
= ttykqfilter
,
102 .d_revoke
= ttyrevoke
105 static struct dev_ops ptc98_ops
= {
106 { "ptc98", 0, D_TTY
| D_MASTER
| D_MPSAFE
},
112 .d_kqfilter
= ptckqfilter
,
113 .d_revoke
= ttyrevoke
116 static struct dev_ops pts_ops
= {
117 { "pts", 0, D_TTY
| D_MPSAFE
},
123 .d_kqfilter
= ttykqfilter
,
124 .d_revoke
= ttyrevoke
127 #define CDEV_MAJOR_C 6
128 static struct dev_ops ptc_ops
= {
129 { "ptc", 0, D_TTY
| D_MASTER
| D_MPSAFE
},
135 .d_kqfilter
= ptckqfilter
,
136 .d_revoke
= ttyrevoke
139 #define BUFSIZ 100 /* Chunk size iomoved to/from user */
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";
192 /* For now we only map the lower 8 bits of the minor */
196 pt
= kmalloc(sizeof(*pt
), M_PTY
, M_WAITOK
| M_ZERO
);
197 pt
->devs
= devs
= make_dev(&pts_ops
, n
,
198 0, 0, 0666, "tty%c%r", names
[n
/ 32], n
% 32);
199 pt
->devc
= devc
= make_dev(&ptc_ops
, n
,
200 0, 0, 0666, "pty%c%r", names
[n
/ 32], n
% 32);
202 pt
->pt_tty
.t_dev
= devs
;
204 devs
->si_drv1
= devc
->si_drv1
= pt
;
205 devs
->si_tty
= devc
->si_tty
= &pt
->pt_tty
;
206 devs
->si_flags
|= SI_OVERRIDE
; /* uid, gid, perms from dev */
207 devc
->si_flags
|= SI_OVERRIDE
; /* uid, gid, perms from dev */
208 ttyregister(&pt
->pt_tty
);
212 ptyclone(struct dev_clone_args
*ap
)
218 * Limit the number of unix98 pty (slave) devices to 1000, as
219 * the utmp(5) format only allows for 8 bytes for the tty,
221 * If this limit is reached, we don't clone and return error
224 unit
= devfs_clone_bitmap_get(&DEVFS_CLONE_BITMAP(pty
), 1000);
231 pt
= kmalloc(sizeof(*pt
), M_PTY
, M_WAITOK
| M_ZERO
);
233 pt
->devc
= make_only_dev(&ptc98_ops
, unit
,
235 0, 0600, "ptm/%d", unit
);
236 pt
->devs
= make_dev(&pts98_ops
, unit
,
238 GID_TTY
, 0620, "pts/%d", unit
);
239 ap
->a_dev
= pt
->devc
;
241 pt
->devs
->si_flags
|= SI_OVERRIDE
; /* uid, gid, perms from dev */
242 pt
->devc
->si_flags
|= SI_OVERRIDE
; /* uid, gid, perms from dev */
244 pt
->pt_tty
.t_dev
= pt
->devs
;
245 pt
->pt_flags
|= PF_UNIX98
;
246 pt
->pt_uminor
= unit
;
247 pt
->devs
->si_drv1
= pt
->devc
->si_drv1
= pt
;
248 pt
->devs
->si_tty
= pt
->devc
->si_tty
= &pt
->pt_tty
;
250 ttyregister(&pt
->pt_tty
);
256 * pti_hold() prevents the pti from being destroyed due to a termination
257 * while a pt*open() is blocked.
259 * This function returns non-zero if we cannot hold due to a termination
262 * NOTE: Must be called with tty_token held
265 pti_hold(struct pt_ioctl
*pti
)
267 if (pti
->pt_flags
& PF_TERMINATED
)
274 * pti_done() releases the reference and checks to see if both sides have
275 * been closed on a unix98 pty, allowing us to destroy the device and
278 * We do not release resources on non-unix98 ptys. Those are left
279 * statically allocated.
282 pti_done(struct pt_ioctl
*pti
)
284 lwkt_gettoken(&tty_token
);
285 if (--pti
->pt_refs
== 0) {
290 * Only unix09 ptys are freed up
292 if ((pti
->pt_flags
& PF_UNIX98
) == 0) {
293 lwkt_reltoken(&tty_token
);
298 * Interlock open attempts against termination by setting
299 * PF_TERMINATED. This allows us to block while cleaning
300 * out the device infrastructure.
302 * Do not terminate the tty if it still has a session
303 * association (t_refs).
305 if ((pti
->pt_flags
& (PF_SOPEN
|PF_MOPEN
)) == 0 &&
306 pti
->pt_tty
.t_refs
== 0) {
307 pti
->pt_flags
|= PF_TERMINATED
;
308 uminor_no
= pti
->pt_uminor
;
310 if ((dev
= pti
->devs
) != NULL
) {
315 if ((dev
= pti
->devc
) != NULL
) {
320 ttyunregister(&pti
->pt_tty
);
321 devfs_clone_bitmap_put(&DEVFS_CLONE_BITMAP(pty
),
326 lwkt_reltoken(&tty_token
);
331 ptsopen(struct dev_open_args
*ap
)
333 cdev_t dev
= ap
->a_head
.a_dev
;
336 struct pt_ioctl
*pti
;
339 * The pti will already be assigned by the clone code or
340 * pre-created if a non-unix 98 pty. If si_drv1 is NULL
341 * we are somehow racing a unix98 termination.
343 if (dev
->si_drv1
== NULL
)
347 lwkt_gettoken(&tty_token
);
349 lwkt_reltoken(&tty_token
);
356 * Reinit most of the tty state if it isn't open. Handle
359 if ((tp
->t_state
& TS_ISOPEN
) == 0) {
360 ttychars(tp
); /* Set up default chars */
361 tp
->t_iflag
= TTYDEF_IFLAG
;
362 tp
->t_oflag
= TTYDEF_OFLAG
;
363 tp
->t_lflag
= TTYDEF_LFLAG
;
364 tp
->t_cflag
= TTYDEF_CFLAG
;
365 tp
->t_ispeed
= tp
->t_ospeed
= TTYDEF_SPEED
;
366 } else if ((tp
->t_state
& TS_XCLUDE
) &&
367 priv_check_cred(ap
->a_cred
, PRIV_ROOT
, 0)) {
369 lwkt_reltoken(&tty_token
);
371 } else if (pti
->pt_prison
!= ap
->a_cred
->cr_prison
) {
373 lwkt_reltoken(&tty_token
);
378 * If the ptc is already present this will connect us up. It
379 * is unclear if this is actually needed.
381 * If neither side is open be sure to clear any left over
382 * ZOMBIE state before continuing.
385 (void)(*linesw
[tp
->t_line
].l_modem
)(tp
, 1);
386 else if ((pti
->pt_flags
& PF_SOPEN
) == 0)
387 tp
->t_state
&= ~TS_ZOMBIE
;
390 * Wait for the carrier (ptc side)
392 while ((tp
->t_state
& TS_CARR_ON
) == 0) {
393 if (ap
->a_oflags
& FNONBLOCK
)
395 error
= ttysleep(tp
, TSA_CARR_ON(tp
), PCATCH
, "ptsopn", 0);
398 lwkt_reltoken(&tty_token
);
404 * Mark the tty open and mark the slave side as being open.
406 error
= (*linesw
[tp
->t_line
].l_open
)(dev
, tp
);
409 pti
->pt_flags
|= PF_SOPEN
;
410 pti
->pt_flags
&= ~PF_SCLOSED
;
411 ptcwakeup(tp
, FREAD
|FWRITE
);
415 lwkt_reltoken(&tty_token
);
420 ptsclose(struct dev_close_args
*ap
)
422 cdev_t dev
= ap
->a_head
.a_dev
;
424 struct pt_ioctl
*pti
= dev
->si_drv1
;
427 lwkt_gettoken(&tty_token
);
429 panic("ptsclose on terminated pti");
432 * Disconnect the slave side
435 err
= (*linesw
[tp
->t_line
].l_close
)(tp
, ap
->a_fflag
);
436 ptsstop(tp
, FREAD
|FWRITE
);
437 ttyclose(tp
); /* clears t_state */
440 * Mark the pts side closed and signal the ptc. Do not mark the
441 * tty a zombie... that is, allow the tty to be re-opened as long
442 * as the ptc is still open. The ptc will read() EOFs until the
443 * pts side is reopened or the ptc is closed.
445 * xterm() depends on this behavior as it will revoke() the pts
446 * and then reopen it after the (unnecessary old code) chmod.
448 pti
->pt_flags
&= ~PF_SOPEN
;
449 pti
->pt_flags
|= PF_SCLOSED
;
451 ptcwakeup(tp
, FREAD
);
453 lwkt_reltoken(&tty_token
);
458 ptsread(struct dev_read_args
*ap
)
460 cdev_t dev
= ap
->a_head
.a_dev
;
461 struct proc
*p
= curproc
;
462 struct tty
*tp
= dev
->si_tty
;
463 struct pt_ioctl
*pti
= dev
->si_drv1
;
468 lp
= curthread
->td_lwp
;
470 lwkt_gettoken(&tty_token
);
472 if (pti
->pt_flags
& PF_REMOTE
) {
473 while (isbackground(p
, tp
)) {
474 if (SIGISMEMBER(p
->p_sigignore
, SIGTTIN
) ||
475 SIGISMEMBER(lp
->lwp_sigmask
, SIGTTIN
) ||
476 p
->p_pgrp
->pg_jobc
== 0 ||
477 (p
->p_flags
& P_PPWAIT
)) {
478 lwkt_reltoken(&tty_token
);
481 pgsignal(p
->p_pgrp
, SIGTTIN
, 1);
482 error
= ttysleep(tp
, &lbolt
, PCATCH
, "ptsbg", 0);
484 lwkt_reltoken(&tty_token
);
488 if (tp
->t_canq
.c_cc
== 0) {
489 if (ap
->a_ioflag
& IO_NDELAY
) {
490 lwkt_reltoken(&tty_token
);
491 return (EWOULDBLOCK
);
493 error
= ttysleep(tp
, TSA_PTS_READ(tp
), PCATCH
,
496 lwkt_reltoken(&tty_token
);
501 while (tp
->t_canq
.c_cc
> 1 && ap
->a_uio
->uio_resid
> 0)
502 if (ureadc(clist_getc(&tp
->t_canq
), ap
->a_uio
) < 0) {
506 if (tp
->t_canq
.c_cc
== 1)
507 clist_getc(&tp
->t_canq
);
508 if (tp
->t_canq
.c_cc
) {
509 lwkt_reltoken(&tty_token
);
514 error
= (*linesw
[tp
->t_line
].l_read
)(tp
, ap
->a_uio
, ap
->a_ioflag
);
515 ptcwakeup(tp
, FWRITE
);
516 lwkt_reltoken(&tty_token
);
521 * Write to pseudo-tty.
522 * Wakeups of controlling tty will happen
523 * indirectly, when tty driver calls ptsstart.
526 ptswrite(struct dev_write_args
*ap
)
528 cdev_t dev
= ap
->a_head
.a_dev
;
532 lwkt_gettoken(&tty_token
);
534 if (tp
->t_oproc
== NULL
) {
535 lwkt_reltoken(&tty_token
);
538 ret
= ((*linesw
[tp
->t_line
].l_write
)(tp
, ap
->a_uio
, ap
->a_ioflag
));
539 lwkt_reltoken(&tty_token
);
544 * Start output on pseudo-tty.
545 * Wake up process selecting or sleeping for input from controlling tty.
548 ptsstart(struct tty
*tp
)
550 lwkt_gettoken(&tty_token
);
551 struct pt_ioctl
*pti
= tp
->t_dev
->si_drv1
;
553 if (tp
->t_state
& TS_TTSTOP
) {
554 lwkt_reltoken(&tty_token
);
558 if (pti
->pt_flags
& PF_STOPPED
) {
559 pti
->pt_flags
&= ~PF_STOPPED
;
560 pti
->pt_send
= TIOCPKT_START
;
563 ptcwakeup(tp
, FREAD
);
564 lwkt_reltoken(&tty_token
);
568 * NOTE: Must be called with tty_token held
571 ptcwakeup(struct tty
*tp
, int flag
)
573 ASSERT_LWKT_TOKEN_HELD(&tty_token
);
576 wakeup(TSA_PTC_READ(tp
));
577 KNOTE(&tp
->t_rkq
.ki_note
, 0);
580 wakeup(TSA_PTC_WRITE(tp
));
581 KNOTE(&tp
->t_wkq
.ki_note
, 0);
586 ptcopen(struct dev_open_args
*ap
)
588 cdev_t dev
= ap
->a_head
.a_dev
;
590 struct pt_ioctl
*pti
;
593 * The pti will already be assigned by the clone code or
594 * pre-created if a non-unix 98 pty. If si_drv1 is NULL
595 * we are somehow racing a unix98 termination.
597 if (dev
->si_drv1
== NULL
)
600 lwkt_gettoken(&tty_token
);
603 lwkt_reltoken(&tty_token
);
606 if (pti
->pt_prison
&& pti
->pt_prison
!= ap
->a_cred
->cr_prison
) {
608 lwkt_reltoken(&tty_token
);
614 lwkt_reltoken(&tty_token
);
619 * If the slave side is not yet open clear any left over zombie
620 * state before doing our modem control.
622 if ((pti
->pt_flags
& PF_SOPEN
) == 0)
623 tp
->t_state
&= ~TS_ZOMBIE
;
625 tp
->t_oproc
= ptsstart
;
626 tp
->t_stop
= ptsstop
;
627 tp
->t_unhold
= ptsunhold
;
632 (void)(*linesw
[tp
->t_line
].l_modem
)(tp
, 1);
634 tp
->t_lflag
&= ~EXTPROC
;
635 pti
->pt_prison
= ap
->a_cred
->cr_prison
;
636 pti
->pt_flags
&= ~PF_PTCSTATEMASK
;
640 pti
->devs
->si_uid
= ap
->a_cred
->cr_uid
;
641 pti
->devs
->si_gid
= 0;
642 pti
->devs
->si_perms
= 0600;
643 pti
->devc
->si_uid
= ap
->a_cred
->cr_uid
;
644 pti
->devc
->si_gid
= 0;
645 pti
->devc
->si_perms
= 0600;
648 * Mark master side open. This does not cause any events
651 pti
->pt_flags
|= PF_MOPEN
;
654 lwkt_reltoken(&tty_token
);
659 ptcclose(struct dev_close_args
*ap
)
661 cdev_t dev
= ap
->a_head
.a_dev
;
663 struct pt_ioctl
*pti
= dev
->si_drv1
;
665 lwkt_gettoken(&tty_token
);
667 panic("ptcclose on terminated pti");
670 (void)(*linesw
[tp
->t_line
].l_modem
)(tp
, 0);
673 * Mark the master side closed. If the slave is still open
674 * mark the tty ZOMBIE, preventing any new action until both
677 * NOTE: The ttyflush() will wake up the slave once we've
678 * set appropriate flags. The ZOMBIE flag will be
679 * cleared when the slave side is closed.
681 pti
->pt_flags
&= ~PF_MOPEN
;
682 if (pti
->pt_flags
& PF_SOPEN
)
683 tp
->t_state
|= TS_ZOMBIE
;
686 * Turn off the carrier and disconnect. This will notify the slave
689 if (tp
->t_state
& TS_ISOPEN
) {
690 tp
->t_state
&= ~(TS_CARR_ON
| TS_CONNECTED
);
691 ttyflush(tp
, FREAD
| FWRITE
);
693 tp
->t_oproc
= NULL
; /* mark closed */
695 pti
->pt_prison
= NULL
;
696 pti
->devs
->si_uid
= 0;
697 pti
->devs
->si_gid
= 0;
698 pti
->devs
->si_perms
= 0666;
699 pti
->devc
->si_uid
= 0;
700 pti
->devc
->si_gid
= 0;
701 pti
->devc
->si_perms
= 0666;
705 lwkt_reltoken(&tty_token
);
710 ptcread(struct dev_read_args
*ap
)
712 cdev_t dev
= ap
->a_head
.a_dev
;
713 struct tty
*tp
= dev
->si_tty
;
714 struct pt_ioctl
*pti
= dev
->si_drv1
;
718 lwkt_gettoken(&tty_token
);
720 * We want to block until the slave
721 * is open, and there's something to read;
722 * but if we lost the slave or we're NBIO,
723 * then return the appropriate error instead.
726 if (tp
->t_state
&TS_ISOPEN
) {
727 if ((pti
->pt_flags
& PF_PKT
) && pti
->pt_send
) {
728 error
= ureadc((int)pti
->pt_send
, ap
->a_uio
);
730 lwkt_reltoken(&tty_token
);
733 if (pti
->pt_send
& TIOCPKT_IOCTL
) {
734 cc
= (int)szmin(ap
->a_uio
->uio_resid
,
735 sizeof(tp
->t_termios
));
736 uiomove((caddr_t
)&tp
->t_termios
, cc
,
740 lwkt_reltoken(&tty_token
);
743 if ((pti
->pt_flags
& PF_UCNTL
) && pti
->pt_ucntl
) {
744 error
= ureadc((int)pti
->pt_ucntl
, ap
->a_uio
);
746 lwkt_reltoken(&tty_token
);
750 lwkt_reltoken(&tty_token
);
753 if (tp
->t_outq
.c_cc
&& (tp
->t_state
&TS_TTSTOP
) == 0)
756 if ((tp
->t_state
& TS_CONNECTED
) == 0) {
757 lwkt_reltoken(&tty_token
);
758 return (0); /* EOF */
760 if (ap
->a_ioflag
& IO_NDELAY
) {
761 lwkt_reltoken(&tty_token
);
762 return (EWOULDBLOCK
);
764 error
= tsleep(TSA_PTC_READ(tp
), PCATCH
, "ptcin", 0);
766 lwkt_reltoken(&tty_token
);
770 if (pti
->pt_flags
& (PF_PKT
|PF_UCNTL
))
771 error
= ureadc(0, ap
->a_uio
);
772 while (ap
->a_uio
->uio_resid
> 0 && error
== 0) {
773 cc
= q_to_b(&tp
->t_outq
, buf
,
774 (int)szmin(ap
->a_uio
->uio_resid
, BUFSIZ
));
777 error
= uiomove(buf
, (size_t)cc
, ap
->a_uio
);
780 lwkt_reltoken(&tty_token
);
785 ptsstop(struct tty
*tp
, int flush
)
787 struct pt_ioctl
*pti
= tp
->t_dev
->si_drv1
;
790 lwkt_gettoken(&tty_token
);
791 /* note: FLUSHREAD and FLUSHWRITE already ok */
794 flush
= TIOCPKT_STOP
;
795 pti
->pt_flags
|= PF_STOPPED
;
797 pti
->pt_flags
&= ~PF_STOPPED
;
799 pti
->pt_send
|= flush
;
800 /* change of perspective */
809 lwkt_reltoken(&tty_token
);
813 * ttyunhold() calls us instead of just decrementing tp->t_refs. This
814 * is needed because a session can hold onto a pts (half closed state)
815 * even if there are no live file descriptors. Without the callback
819 ptsunhold(struct tty
*tp
)
821 struct pt_ioctl
*pti
= tp
->t_dev
->si_drv1
;
823 lwkt_gettoken(&tty_token
);
827 lwkt_reltoken(&tty_token
);
831 * kqueue ops for pseudo-terminals.
833 static struct filterops ptcread_filtops
=
834 { FILTEROP_ISFD
|FILTEROP_MPSAFE
, NULL
, filt_ptcrdetach
, filt_ptcread
};
835 static struct filterops ptcwrite_filtops
=
836 { FILTEROP_ISFD
|FILTEROP_MPSAFE
, NULL
, filt_ptcwdetach
, filt_ptcwrite
};
839 ptckqfilter(struct dev_kqfilter_args
*ap
)
841 cdev_t dev
= ap
->a_head
.a_dev
;
842 struct knote
*kn
= ap
->a_kn
;
843 struct tty
*tp
= dev
->si_tty
;
847 switch (kn
->kn_filter
) {
849 klist
= &tp
->t_rkq
.ki_note
;
850 kn
->kn_fop
= &ptcread_filtops
;
853 klist
= &tp
->t_wkq
.ki_note
;
854 kn
->kn_fop
= &ptcwrite_filtops
;
857 ap
->a_result
= EOPNOTSUPP
;
861 kn
->kn_hook
= (caddr_t
)dev
;
862 knote_insert(klist
, kn
);
867 filt_ptcread (struct knote
*kn
, long hint
)
869 struct tty
*tp
= ((cdev_t
)kn
->kn_hook
)->si_tty
;
870 struct pt_ioctl
*pti
= ((cdev_t
)kn
->kn_hook
)->si_drv1
;
872 lwkt_gettoken(&tty_token
);
873 if ((tp
->t_state
& TS_ZOMBIE
) || (pti
->pt_flags
& PF_SCLOSED
)) {
874 lwkt_reltoken(&tty_token
);
875 kn
->kn_flags
|= (EV_EOF
| EV_NODATA
);
879 if ((tp
->t_state
& TS_ISOPEN
) &&
880 ((tp
->t_outq
.c_cc
&& (tp
->t_state
& TS_TTSTOP
) == 0) ||
881 ((pti
->pt_flags
& PF_PKT
) && pti
->pt_send
) ||
882 ((pti
->pt_flags
& PF_UCNTL
) && pti
->pt_ucntl
))) {
883 kn
->kn_data
= tp
->t_outq
.c_cc
;
884 lwkt_reltoken(&tty_token
);
887 lwkt_reltoken(&tty_token
);
893 filt_ptcwrite (struct knote
*kn
, long hint
)
895 struct tty
*tp
= ((cdev_t
)kn
->kn_hook
)->si_tty
;
896 struct pt_ioctl
*pti
= ((cdev_t
)kn
->kn_hook
)->si_drv1
;
898 lwkt_gettoken(&tty_token
);
899 if (tp
->t_state
& TS_ZOMBIE
) {
900 lwkt_reltoken(&tty_token
);
901 kn
->kn_flags
|= (EV_EOF
| EV_NODATA
);
905 if (tp
->t_state
& TS_ISOPEN
&&
906 ((pti
->pt_flags
& PF_REMOTE
) ?
907 (tp
->t_canq
.c_cc
== 0) :
908 ((tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
< TTYHOG
- 2) ||
909 (tp
->t_canq
.c_cc
== 0 && (tp
->t_lflag
& ICANON
))))) {
910 kn
->kn_data
= tp
->t_canq
.c_cc
+ tp
->t_rawq
.c_cc
;
911 lwkt_reltoken(&tty_token
);
914 lwkt_reltoken(&tty_token
);
921 filt_ptcrdetach (struct knote
*kn
)
923 struct tty
*tp
= ((cdev_t
)kn
->kn_hook
)->si_tty
;
925 knote_remove(&tp
->t_rkq
.ki_note
, kn
);
929 filt_ptcwdetach (struct knote
*kn
)
931 struct tty
*tp
= ((cdev_t
)kn
->kn_hook
)->si_tty
;
933 knote_remove(&tp
->t_wkq
.ki_note
, kn
);
940 ptcwrite(struct dev_write_args
*ap
)
942 cdev_t dev
= ap
->a_head
.a_dev
;
943 struct tty
*tp
= dev
->si_tty
;
946 u_char locbuf
[BUFSIZ
];
948 struct pt_ioctl
*pti
= dev
->si_drv1
;
951 lwkt_gettoken(&tty_token
);
953 if ((tp
->t_state
&TS_ISOPEN
) == 0)
955 if (pti
->pt_flags
& PF_REMOTE
) {
958 while ((ap
->a_uio
->uio_resid
> 0 || cc
> 0) &&
959 tp
->t_canq
.c_cc
< TTYHOG
- 1) {
961 cc
= (int)szmin(ap
->a_uio
->uio_resid
, BUFSIZ
);
962 cc
= imin(cc
, TTYHOG
- 1 - tp
->t_canq
.c_cc
);
964 error
= uiomove(cp
, (size_t)cc
, ap
->a_uio
);
966 lwkt_reltoken(&tty_token
);
969 /* check again for safety */
970 if ((tp
->t_state
& TS_ISOPEN
) == 0) {
971 /* adjust as usual */
972 ap
->a_uio
->uio_resid
+= cc
;
973 lwkt_reltoken(&tty_token
);
978 cc
= b_to_q((char *)cp
, cc
, &tp
->t_canq
);
980 * XXX we don't guarantee that the canq size
981 * is >= TTYHOG, so the above b_to_q() may
982 * leave some bytes uncopied. However, space
983 * is guaranteed for the null terminator if
984 * we don't fail here since (TTYHOG - 1) is
985 * not a multiple of CBSIZE.
991 /* adjust for data copied in but not written */
992 ap
->a_uio
->uio_resid
+= cc
;
993 clist_putc(0, &tp
->t_canq
);
995 wakeup(TSA_PTS_READ(tp
));
996 lwkt_reltoken(&tty_token
);
999 while (ap
->a_uio
->uio_resid
> 0 || cc
> 0) {
1001 cc
= (int)szmin(ap
->a_uio
->uio_resid
, BUFSIZ
);
1003 error
= uiomove(cp
, (size_t)cc
, ap
->a_uio
);
1005 lwkt_reltoken(&tty_token
);
1008 /* check again for safety */
1009 if ((tp
->t_state
& TS_ISOPEN
) == 0) {
1010 /* adjust for data copied in but not written */
1011 ap
->a_uio
->uio_resid
+= cc
;
1012 lwkt_reltoken(&tty_token
);
1017 if ((tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
) >= TTYHOG
- 2 &&
1018 (tp
->t_canq
.c_cc
> 0 || !(tp
->t_lflag
&ICANON
))) {
1019 wakeup(TSA_HUP_OR_INPUT(tp
));
1022 (*linesw
[tp
->t_line
].l_rint
)(*cp
++, tp
);
1028 lwkt_reltoken(&tty_token
);
1032 * Come here to wait for slave to open, for space
1033 * in outq, or space in rawq, or an empty canq.
1035 if ((tp
->t_state
& TS_CONNECTED
) == 0) {
1036 /* adjust for data copied in but not written */
1037 ap
->a_uio
->uio_resid
+= cc
;
1038 lwkt_reltoken(&tty_token
);
1041 if (ap
->a_ioflag
& IO_NDELAY
) {
1042 /* adjust for data copied in but not written */
1043 ap
->a_uio
->uio_resid
+= cc
;
1045 lwkt_reltoken(&tty_token
);
1046 return (EWOULDBLOCK
);
1048 lwkt_reltoken(&tty_token
);
1051 error
= tsleep(TSA_PTC_WRITE(tp
), PCATCH
, "ptcout", 0);
1053 /* adjust for data copied in but not written */
1054 ap
->a_uio
->uio_resid
+= cc
;
1055 lwkt_reltoken(&tty_token
);
1063 ptyioctl(struct dev_ioctl_args
*ap
)
1065 cdev_t dev
= ap
->a_head
.a_dev
;
1066 struct tty
*tp
= dev
->si_tty
;
1067 struct pt_ioctl
*pti
= dev
->si_drv1
;
1068 u_char
*cc
= tp
->t_cc
;
1071 lwkt_gettoken(&tty_token
);
1072 if (dev_dflags(dev
) & D_MASTER
) {
1073 switch (ap
->a_cmd
) {
1077 * We avoid calling ttioctl on the controller since,
1078 * in that case, tp must be the controlling terminal.
1080 *(int *)ap
->a_data
= tp
->t_pgrp
? tp
->t_pgrp
->pg_id
: 0;
1081 lwkt_reltoken(&tty_token
);
1085 if (*(int *)ap
->a_data
) {
1086 if (pti
->pt_flags
& PF_UCNTL
) {
1087 lwkt_reltoken(&tty_token
);
1090 pti
->pt_flags
|= PF_PKT
;
1092 pti
->pt_flags
&= ~PF_PKT
;
1094 lwkt_reltoken(&tty_token
);
1098 if (*(int *)ap
->a_data
) {
1099 if (pti
->pt_flags
& PF_PKT
) {
1100 lwkt_reltoken(&tty_token
);
1103 pti
->pt_flags
|= PF_UCNTL
;
1105 pti
->pt_flags
&= ~PF_UCNTL
;
1107 lwkt_reltoken(&tty_token
);
1111 if (*(int *)ap
->a_data
)
1112 pti
->pt_flags
|= PF_REMOTE
;
1114 pti
->pt_flags
&= ~PF_REMOTE
;
1115 ttyflush(tp
, FREAD
|FWRITE
);
1116 lwkt_reltoken(&tty_token
);
1119 case TIOCISPTMASTER
:
1120 if ((pti
->pt_flags
& PF_UNIX98
) &&
1121 (pti
->devc
== dev
)) {
1122 lwkt_reltoken(&tty_token
);
1125 lwkt_reltoken(&tty_token
);
1131 * The rest of the ioctls shouldn't be called until
1132 * the slave is open.
1134 if ((tp
->t_state
& TS_ISOPEN
) == 0) {
1135 lwkt_reltoken(&tty_token
);
1139 switch (ap
->a_cmd
) {
1145 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
1146 * ttywflush(tp) will hang if there are characters in
1149 ndflush(&tp
->t_outq
, tp
->t_outq
.c_cc
);
1153 if (*(unsigned int *)ap
->a_data
>= NSIG
||
1154 *(unsigned int *)ap
->a_data
== 0) {
1155 lwkt_reltoken(&tty_token
);
1158 if ((tp
->t_lflag
&NOFLSH
) == 0)
1159 ttyflush(tp
, FREAD
|FWRITE
);
1160 pgsignal(tp
->t_pgrp
, *(unsigned int *)ap
->a_data
, 1);
1161 if ((*(unsigned int *)ap
->a_data
== SIGINFO
) &&
1162 ((tp
->t_lflag
&NOKERNINFO
) == 0))
1164 lwkt_reltoken(&tty_token
);
1168 if (ap
->a_cmd
== TIOCEXT
) {
1170 * When the EXTPROC bit is being toggled, we need
1171 * to send an TIOCPKT_IOCTL if the packet driver
1174 if (*(int *)ap
->a_data
) {
1175 if (pti
->pt_flags
& PF_PKT
) {
1176 pti
->pt_send
|= TIOCPKT_IOCTL
;
1177 ptcwakeup(tp
, FREAD
);
1179 tp
->t_lflag
|= EXTPROC
;
1181 if ((tp
->t_lflag
& EXTPROC
) &&
1182 (pti
->pt_flags
& PF_PKT
)) {
1183 pti
->pt_send
|= TIOCPKT_IOCTL
;
1184 ptcwakeup(tp
, FREAD
);
1186 tp
->t_lflag
&= ~EXTPROC
;
1188 lwkt_reltoken(&tty_token
);
1191 error
= (*linesw
[tp
->t_line
].l_ioctl
)(tp
, ap
->a_cmd
, ap
->a_data
,
1192 ap
->a_fflag
, ap
->a_cred
);
1193 if (error
== ENOIOCTL
)
1194 error
= ttioctl(tp
, ap
->a_cmd
, ap
->a_data
, ap
->a_fflag
);
1195 if (error
== ENOIOCTL
) {
1196 if (pti
->pt_flags
& PF_UCNTL
&&
1197 (ap
->a_cmd
& ~0xff) == UIOCCMD(0)) {
1198 if (ap
->a_cmd
& 0xff) {
1199 pti
->pt_ucntl
= (u_char
)ap
->a_cmd
;
1200 ptcwakeup(tp
, FREAD
);
1202 lwkt_reltoken(&tty_token
);
1208 * If external processing and packet mode send ioctl packet.
1210 if ((tp
->t_lflag
&EXTPROC
) && (pti
->pt_flags
& PF_PKT
)) {
1215 pti
->pt_send
|= TIOCPKT_IOCTL
;
1216 ptcwakeup(tp
, FREAD
);
1221 stop
= (tp
->t_iflag
& IXON
) && CCEQ(cc
[VSTOP
], CTRL('s'))
1222 && CCEQ(cc
[VSTART
], CTRL('q'));
1223 if (pti
->pt_flags
& PF_NOSTOP
) {
1225 pti
->pt_send
&= ~TIOCPKT_NOSTOP
;
1226 pti
->pt_send
|= TIOCPKT_DOSTOP
;
1227 pti
->pt_flags
&= ~PF_NOSTOP
;
1228 ptcwakeup(tp
, FREAD
);
1232 pti
->pt_send
&= ~TIOCPKT_DOSTOP
;
1233 pti
->pt_send
|= TIOCPKT_NOSTOP
;
1234 pti
->pt_flags
|= PF_NOSTOP
;
1235 ptcwakeup(tp
, FREAD
);
1238 lwkt_reltoken(&tty_token
);
1243 static void ptc_drvinit (void *unused
);
1245 SYSCTL_INT(_kern
, OID_AUTO
, pty_debug
, CTLFLAG_RW
, &pty_debug_level
,
1246 0, "Change pty debug level");
1249 ptc_drvinit(void *unused
)
1255 * Create the clonable base device.
1257 make_autoclone_dev(&ptc_ops
, &DEVFS_CLONE_BITMAP(pty
), ptyclone
,
1258 0, 0, 0666, "ptmx");
1260 for (i
= 0; i
< 256; i
++) {
1265 SYSINIT(ptcdev
, SI_SUB_DRIVERS
, SI_ORDER_MIDDLE
+ CDEV_MAJOR_C
, ptc_drvinit
,