2 * Copyright (c) 1982, 1986, 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)tty_pty.c 8.4 (Berkeley) 2/20/95
34 * $FreeBSD: src/sys/kern/tty_pty.c,v 1.74.2.4 2002/02/20 19:58:13 dillon Exp $
35 * $DragonFly: src/sys/kern/tty_pty.c,v 1.12 2004/05/19 22:52:58 dillon Exp $
39 * Pseudo-teletype Driver
40 * (Actually two drivers, requiring two entries in 'cdevsw')
42 #include "use_pty.h" /* XXX */
43 #include "opt_compat.h"
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
48 #include <sys/ioctl_compat.h>
53 #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>
61 MALLOC_DEFINE(M_PTY
, "ptys", "pty data structures");
63 static void ptsstart (struct tty
*tp
);
64 static void ptsstop (struct tty
*tp
, int rw
);
65 static void ptcwakeup (struct tty
*tp
, int flag
);
66 static void ptyinit (int n
);
68 static d_open_t ptsopen
;
69 static d_close_t ptsclose
;
70 static d_read_t ptsread
;
71 static d_write_t ptswrite
;
72 static d_ioctl_t ptyioctl
;
73 static d_open_t ptcopen
;
74 static d_close_t ptcclose
;
75 static d_read_t ptcread
;
76 static d_write_t ptcwrite
;
77 static d_poll_t ptcpoll
;
79 #define CDEV_MAJOR_S 5
80 static struct cdevsw pts_cdevsw
= {
82 /* maj */ CDEV_MAJOR_S
,
83 /* flags */ D_TTY
| D_KQFILTER
,
94 /* strategy */ nostrategy
,
97 /* kqfilter */ ttykqfilter
100 #define CDEV_MAJOR_C 6
101 static struct cdevsw ptc_cdevsw
= {
103 /* maj */ CDEV_MAJOR_C
,
104 /* flags */ D_TTY
| D_KQFILTER
| D_MASTER
,
109 /* close */ ptcclose
,
111 /* write */ ptcwrite
,
112 /* ioctl */ ptyioctl
,
115 /* strategy */ nostrategy
,
118 /* kqfilter */ ttykqfilter
121 #define BUFSIZ 100 /* Chunk size iomoved to/from user */
125 struct selinfo pt_selr
, pt_selw
;
130 struct prison
*pt_prison
;
133 #define PF_PKT 0x08 /* packet mode */
134 #define PF_STOPPED 0x10 /* user told stopped */
135 #define PF_REMOTE 0x20 /* remote and flow controlled input */
136 #define PF_NOSTOP 0x40
137 #define PF_UCNTL 0x80 /* user control mode */
140 * This function creates and initializes a pts/ptc pair
142 * pts == /dev/tty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
143 * ptc == /dev/pty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
145 * XXX: define and add mapping of upper minor bits to allow more
153 char *names
= "pqrsPQRS";
156 /* For now we only map the lower 8 bits of the minor */
160 pt
= malloc(sizeof(*pt
), M_PTY
, M_WAITOK
);
161 bzero(pt
, sizeof(*pt
));
162 pt
->devs
= devs
= make_dev(&pts_cdevsw
, n
,
163 0, 0, 0666, "tty%c%r", names
[n
/ 32], n
% 32);
164 pt
->devc
= devc
= make_dev(&ptc_cdevsw
, n
,
165 0, 0, 0666, "pty%c%r", names
[n
/ 32], n
% 32);
167 devs
->si_drv1
= devc
->si_drv1
= pt
;
168 devs
->si_tty
= devc
->si_tty
= &pt
->pt_tty
;
169 pt
->pt_tty
.t_dev
= devs
;
170 ttyregister(&pt
->pt_tty
);
175 ptsopen(dev_t dev
, int flag
, int devtype
, struct thread
*td
)
183 struct pt_ioctl
*pti
;
192 * REMOVED gross hack for devfs. also note that makedev()
193 * no longer exists in this form and reference counting makes
194 * this a problem if we don't clean it out later.
197 nextdev
= make_sub_dev(dev
, minr
+ 1);
198 if (!nextdev
->si_drv1
) {
209 if ((tp
->t_state
& TS_ISOPEN
) == 0) {
210 ttychars(tp
); /* Set up default chars */
211 tp
->t_iflag
= TTYDEF_IFLAG
;
212 tp
->t_oflag
= TTYDEF_OFLAG
;
213 tp
->t_lflag
= TTYDEF_LFLAG
;
214 tp
->t_cflag
= TTYDEF_CFLAG
;
215 tp
->t_ispeed
= tp
->t_ospeed
= TTYDEF_SPEED
;
216 } else if (tp
->t_state
& TS_XCLUDE
&& suser(td
)) {
218 } else if (pti
->pt_prison
!= p
->p_ucred
->cr_prison
) {
221 if (tp
->t_oproc
) /* Ctrlr still around. */
222 (void)(*linesw
[tp
->t_line
].l_modem
)(tp
, 1);
223 while ((tp
->t_state
& TS_CARR_ON
) == 0) {
226 error
= ttysleep(tp
, TSA_CARR_ON(tp
), PCATCH
, "ptsopn", 0);
230 error
= (*linesw
[tp
->t_line
].l_open
)(dev
, tp
);
232 ptcwakeup(tp
, FREAD
|FWRITE
);
237 ptsclose(dev
, flag
, mode
, td
)
246 err
= (*linesw
[tp
->t_line
].l_close
)(tp
, flag
);
247 ptsstop(tp
, FREAD
|FWRITE
);
253 ptsread(dev
, uio
, flag
)
258 struct proc
*p
= curproc
;
259 struct tty
*tp
= dev
->si_tty
;
260 struct pt_ioctl
*pti
= dev
->si_drv1
;
264 if (pti
->pt_flags
& PF_REMOTE
) {
265 while (isbackground(p
, tp
)) {
266 if (SIGISMEMBER(p
->p_sigignore
, SIGTTIN
) ||
267 SIGISMEMBER(p
->p_sigmask
, SIGTTIN
) ||
268 p
->p_pgrp
->pg_jobc
== 0 || p
->p_flag
& P_PPWAIT
)
270 pgsignal(p
->p_pgrp
, SIGTTIN
, 1);
271 error
= ttysleep(tp
, &lbolt
, PCATCH
, "ptsbg", 0);
275 if (tp
->t_canq
.c_cc
== 0) {
276 if (flag
& IO_NDELAY
)
277 return (EWOULDBLOCK
);
278 error
= ttysleep(tp
, TSA_PTS_READ(tp
), PCATCH
,
284 while (tp
->t_canq
.c_cc
> 1 && uio
->uio_resid
> 0)
285 if (ureadc(getc(&tp
->t_canq
), uio
) < 0) {
289 if (tp
->t_canq
.c_cc
== 1)
290 (void) getc(&tp
->t_canq
);
295 error
= (*linesw
[tp
->t_line
].l_read
)(tp
, uio
, flag
);
296 ptcwakeup(tp
, FWRITE
);
301 * Write to pseudo-tty.
302 * Wakeups of controlling tty will happen
303 * indirectly, when tty driver calls ptsstart.
306 ptswrite(dev
, uio
, flag
)
314 if (tp
->t_oproc
== 0)
316 return ((*linesw
[tp
->t_line
].l_write
)(tp
, uio
, flag
));
320 * Start output on pseudo-tty.
321 * Wake up process selecting or sleeping for input from controlling tty.
327 struct pt_ioctl
*pti
= tp
->t_dev
->si_drv1
;
329 if (tp
->t_state
& TS_TTSTOP
)
331 if (pti
->pt_flags
& PF_STOPPED
) {
332 pti
->pt_flags
&= ~PF_STOPPED
;
333 pti
->pt_send
= TIOCPKT_START
;
335 ptcwakeup(tp
, FREAD
);
343 struct pt_ioctl
*pti
= tp
->t_dev
->si_drv1
;
346 selwakeup(&pti
->pt_selr
);
347 wakeup(TSA_PTC_READ(tp
));
350 selwakeup(&pti
->pt_selw
);
351 wakeup(TSA_PTC_WRITE(tp
));
356 ptcopen(dev
, flag
, devtype
, td
)
362 struct pt_ioctl
*pti
;
371 KKASSERT(td
->td_proc
!= NULL
);
372 tp
->t_oproc
= ptsstart
;
373 tp
->t_stop
= ptsstop
;
374 (void)(*linesw
[tp
->t_line
].l_modem
)(tp
, 1);
375 tp
->t_lflag
&= ~EXTPROC
;
377 pti
->pt_prison
= td
->td_proc
->p_ucred
->cr_prison
;
385 ptcclose(dev
, flags
, fmt
, td
)
394 (void)(*linesw
[tp
->t_line
].l_modem
)(tp
, 0);
397 * XXX MDMBUF makes no sense for ptys but would inhibit the above
398 * l_modem(). CLOCAL makes sense but isn't supported. Special
399 * l_modem()s that ignore carrier drop make no sense for ptys but
400 * may be in use because other parts of the line discipline make
401 * sense for ptys. Recover by doing everything that a normal
402 * ttymodem() would have done except for sending a SIGHUP.
404 if (tp
->t_state
& TS_ISOPEN
) {
405 tp
->t_state
&= ~(TS_CARR_ON
| TS_CONNECTED
);
406 tp
->t_state
|= TS_ZOMBIE
;
407 ttyflush(tp
, FREAD
| FWRITE
);
410 tp
->t_oproc
= 0; /* mark closed */
415 ptcread(dev
, uio
, flag
)
420 struct tty
*tp
= dev
->si_tty
;
421 struct pt_ioctl
*pti
= dev
->si_drv1
;
426 * We want to block until the slave
427 * is open, and there's something to read;
428 * but if we lost the slave or we're NBIO,
429 * then return the appropriate error instead.
432 if (tp
->t_state
&TS_ISOPEN
) {
433 if (pti
->pt_flags
&PF_PKT
&& pti
->pt_send
) {
434 error
= ureadc((int)pti
->pt_send
, uio
);
437 if (pti
->pt_send
& TIOCPKT_IOCTL
) {
438 cc
= min(uio
->uio_resid
,
439 sizeof(tp
->t_termios
));
440 uiomove((caddr_t
)&tp
->t_termios
, cc
,
446 if (pti
->pt_flags
&PF_UCNTL
&& pti
->pt_ucntl
) {
447 error
= ureadc((int)pti
->pt_ucntl
, uio
);
453 if (tp
->t_outq
.c_cc
&& (tp
->t_state
&TS_TTSTOP
) == 0)
456 if ((tp
->t_state
& TS_CONNECTED
) == 0)
457 return (0); /* EOF */
458 if (flag
& IO_NDELAY
)
459 return (EWOULDBLOCK
);
460 error
= tsleep(TSA_PTC_READ(tp
), PCATCH
, "ptcin", 0);
464 if (pti
->pt_flags
& (PF_PKT
|PF_UCNTL
))
465 error
= ureadc(0, uio
);
466 while (uio
->uio_resid
> 0 && error
== 0) {
467 cc
= q_to_b(&tp
->t_outq
, buf
, min(uio
->uio_resid
, BUFSIZ
));
470 error
= uiomove(buf
, cc
, uio
);
481 struct pt_ioctl
*pti
= tp
->t_dev
->si_drv1
;
484 /* note: FLUSHREAD and FLUSHWRITE already ok */
486 flush
= TIOCPKT_STOP
;
487 pti
->pt_flags
|= PF_STOPPED
;
489 pti
->pt_flags
&= ~PF_STOPPED
;
490 pti
->pt_send
|= flush
;
491 /* change of perspective */
501 ptcpoll(dev
, events
, td
)
506 struct tty
*tp
= dev
->si_tty
;
507 struct pt_ioctl
*pti
= dev
->si_drv1
;
511 if ((tp
->t_state
& TS_CONNECTED
) == 0)
512 return (seltrue(dev
, events
, td
) | POLLHUP
);
515 * Need to block timeouts (ttrstart).
519 if (events
& (POLLIN
| POLLRDNORM
))
520 if ((tp
->t_state
& TS_ISOPEN
) &&
521 ((tp
->t_outq
.c_cc
&& (tp
->t_state
& TS_TTSTOP
) == 0) ||
522 ((pti
->pt_flags
& PF_PKT
) && pti
->pt_send
) ||
523 ((pti
->pt_flags
& PF_UCNTL
) && pti
->pt_ucntl
)))
524 revents
|= events
& (POLLIN
| POLLRDNORM
);
526 if (events
& (POLLOUT
| POLLWRNORM
))
527 if (tp
->t_state
& TS_ISOPEN
&&
528 ((pti
->pt_flags
& PF_REMOTE
) ?
529 (tp
->t_canq
.c_cc
== 0) :
530 ((tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
< TTYHOG
- 2) ||
531 (tp
->t_canq
.c_cc
== 0 && (tp
->t_lflag
& ICANON
)))))
532 revents
|= events
& (POLLOUT
| POLLWRNORM
);
534 if (events
& POLLHUP
)
535 if ((tp
->t_state
& TS_CARR_ON
) == 0)
539 if (events
& (POLLIN
| POLLRDNORM
))
540 selrecord(td
, &pti
->pt_selr
);
542 if (events
& (POLLOUT
| POLLWRNORM
))
543 selrecord(td
, &pti
->pt_selw
);
551 ptcwrite(dev
, uio
, flag
)
556 struct tty
*tp
= dev
->si_tty
;
559 u_char locbuf
[BUFSIZ
];
561 struct pt_ioctl
*pti
= dev
->si_drv1
;
565 if ((tp
->t_state
&TS_ISOPEN
) == 0)
567 if (pti
->pt_flags
& PF_REMOTE
) {
570 while ((uio
->uio_resid
> 0 || cc
> 0) &&
571 tp
->t_canq
.c_cc
< TTYHOG
- 1) {
573 cc
= min(uio
->uio_resid
, BUFSIZ
);
574 cc
= min(cc
, TTYHOG
- 1 - tp
->t_canq
.c_cc
);
576 error
= uiomove((caddr_t
)cp
, cc
, uio
);
579 /* check again for safety */
580 if ((tp
->t_state
& TS_ISOPEN
) == 0) {
581 /* adjust as usual */
582 uio
->uio_resid
+= cc
;
587 cc
= b_to_q((char *)cp
, cc
, &tp
->t_canq
);
589 * XXX we don't guarantee that the canq size
590 * is >= TTYHOG, so the above b_to_q() may
591 * leave some bytes uncopied. However, space
592 * is guaranteed for the null terminator if
593 * we don't fail here since (TTYHOG - 1) is
594 * not a multiple of CBSIZE.
600 /* adjust for data copied in but not written */
601 uio
->uio_resid
+= cc
;
602 (void) putc(0, &tp
->t_canq
);
604 wakeup(TSA_PTS_READ(tp
));
607 while (uio
->uio_resid
> 0 || cc
> 0) {
609 cc
= min(uio
->uio_resid
, BUFSIZ
);
611 error
= uiomove((caddr_t
)cp
, cc
, uio
);
614 /* check again for safety */
615 if ((tp
->t_state
& TS_ISOPEN
) == 0) {
616 /* adjust for data copied in but not written */
617 uio
->uio_resid
+= cc
;
622 if ((tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
) >= TTYHOG
- 2 &&
623 (tp
->t_canq
.c_cc
> 0 || !(tp
->t_lflag
&ICANON
))) {
624 wakeup(TSA_HUP_OR_INPUT(tp
));
627 (*linesw
[tp
->t_line
].l_rint
)(*cp
++, tp
);
636 * Come here to wait for slave to open, for space
637 * in outq, or space in rawq, or an empty canq.
639 if ((tp
->t_state
& TS_CONNECTED
) == 0) {
640 /* adjust for data copied in but not written */
641 uio
->uio_resid
+= cc
;
644 if (flag
& IO_NDELAY
) {
645 /* adjust for data copied in but not written */
646 uio
->uio_resid
+= cc
;
648 return (EWOULDBLOCK
);
651 error
= tsleep(TSA_PTC_WRITE(tp
), PCATCH
, "ptcout", 0);
653 /* adjust for data copied in but not written */
654 uio
->uio_resid
+= cc
;
662 ptyioctl(dev
, cmd
, data
, flag
, td
)
669 struct tty
*tp
= dev
->si_tty
;
670 struct pt_ioctl
*pti
= dev
->si_drv1
;
671 u_char
*cc
= tp
->t_cc
;
674 if (dev_dflags(dev
) & D_MASTER
) {
679 * We avoid calling ttioctl on the controller since,
680 * in that case, tp must be the controlling terminal.
682 *(int *)data
= tp
->t_pgrp
? tp
->t_pgrp
->pg_id
: 0;
687 if (pti
->pt_flags
& PF_UCNTL
)
689 pti
->pt_flags
|= PF_PKT
;
691 pti
->pt_flags
&= ~PF_PKT
;
696 if (pti
->pt_flags
& PF_PKT
)
698 pti
->pt_flags
|= PF_UCNTL
;
700 pti
->pt_flags
&= ~PF_UCNTL
;
705 pti
->pt_flags
|= PF_REMOTE
;
707 pti
->pt_flags
&= ~PF_REMOTE
;
708 ttyflush(tp
, FREAD
|FWRITE
);
713 * The rest of the ioctls shouldn't be called until
716 if ((tp
->t_state
& TS_ISOPEN
) == 0)
729 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
730 * ttywflush(tp) will hang if there are characters in
733 ndflush(&tp
->t_outq
, tp
->t_outq
.c_cc
);
737 if (*(unsigned int *)data
>= NSIG
||
738 *(unsigned int *)data
== 0)
740 if ((tp
->t_lflag
&NOFLSH
) == 0)
741 ttyflush(tp
, FREAD
|FWRITE
);
742 pgsignal(tp
->t_pgrp
, *(unsigned int *)data
, 1);
743 if ((*(unsigned int *)data
== SIGINFO
) &&
744 ((tp
->t_lflag
&NOKERNINFO
) == 0))
749 if (cmd
== TIOCEXT
) {
751 * When the EXTPROC bit is being toggled, we need
752 * to send an TIOCPKT_IOCTL if the packet driver
756 if (pti
->pt_flags
& PF_PKT
) {
757 pti
->pt_send
|= TIOCPKT_IOCTL
;
758 ptcwakeup(tp
, FREAD
);
760 tp
->t_lflag
|= EXTPROC
;
762 if ((tp
->t_lflag
& EXTPROC
) &&
763 (pti
->pt_flags
& PF_PKT
)) {
764 pti
->pt_send
|= TIOCPKT_IOCTL
;
765 ptcwakeup(tp
, FREAD
);
767 tp
->t_lflag
&= ~EXTPROC
;
771 error
= (*linesw
[tp
->t_line
].l_ioctl
)(tp
, cmd
, data
, flag
, td
);
772 if (error
== ENOIOCTL
)
773 error
= ttioctl(tp
, cmd
, data
, flag
);
774 if (error
== ENOIOCTL
) {
775 if (pti
->pt_flags
& PF_UCNTL
&&
776 (cmd
& ~0xff) == UIOCCMD(0)) {
778 pti
->pt_ucntl
= (u_char
)cmd
;
779 ptcwakeup(tp
, FREAD
);
786 * If external processing and packet mode send ioctl packet.
788 if ((tp
->t_lflag
&EXTPROC
) && (pti
->pt_flags
& PF_PKT
)) {
797 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
804 pti
->pt_send
|= TIOCPKT_IOCTL
;
805 ptcwakeup(tp
, FREAD
);
810 stop
= (tp
->t_iflag
& IXON
) && CCEQ(cc
[VSTOP
], CTRL('s'))
811 && CCEQ(cc
[VSTART
], CTRL('q'));
812 if (pti
->pt_flags
& PF_NOSTOP
) {
814 pti
->pt_send
&= ~TIOCPKT_NOSTOP
;
815 pti
->pt_send
|= TIOCPKT_DOSTOP
;
816 pti
->pt_flags
&= ~PF_NOSTOP
;
817 ptcwakeup(tp
, FREAD
);
821 pti
->pt_send
&= ~TIOCPKT_DOSTOP
;
822 pti
->pt_send
|= TIOCPKT_NOSTOP
;
823 pti
->pt_flags
|= PF_NOSTOP
;
824 ptcwakeup(tp
, FREAD
);
831 static void ptc_drvinit (void *unused
);
837 cdevsw_add(&pts_cdevsw
, 0, 0);
838 cdevsw_add(&ptc_cdevsw
, 0, 0);
839 /* XXX: Gross hack for DEVFS */
840 /* XXX: DEVFS is no more, should this be removed? */
844 SYSINIT(ptcdev
,SI_SUB_DRIVERS
,SI_ORDER_MIDDLE
+CDEV_MAJOR_C
,ptc_drvinit
,NULL
)