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.13 2005/06/06 15:02:28 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>
60 #include <sys/thread2.h>
62 MALLOC_DEFINE(M_PTY
, "ptys", "pty data structures");
64 static void ptsstart (struct tty
*tp
);
65 static void ptsstop (struct tty
*tp
, int rw
);
66 static void ptcwakeup (struct tty
*tp
, int flag
);
67 static void ptyinit (int n
);
69 static d_open_t ptsopen
;
70 static d_close_t ptsclose
;
71 static d_read_t ptsread
;
72 static d_write_t ptswrite
;
73 static d_ioctl_t ptyioctl
;
74 static d_open_t ptcopen
;
75 static d_close_t ptcclose
;
76 static d_read_t ptcread
;
77 static d_write_t ptcwrite
;
78 static d_poll_t ptcpoll
;
80 #define CDEV_MAJOR_S 5
81 static struct cdevsw pts_cdevsw
= {
83 /* maj */ CDEV_MAJOR_S
,
84 /* flags */ D_TTY
| D_KQFILTER
,
95 /* strategy */ nostrategy
,
98 /* kqfilter */ ttykqfilter
101 #define CDEV_MAJOR_C 6
102 static struct cdevsw ptc_cdevsw
= {
104 /* maj */ CDEV_MAJOR_C
,
105 /* flags */ D_TTY
| D_KQFILTER
| D_MASTER
,
110 /* close */ ptcclose
,
112 /* write */ ptcwrite
,
113 /* ioctl */ ptyioctl
,
116 /* strategy */ nostrategy
,
119 /* kqfilter */ ttykqfilter
122 #define BUFSIZ 100 /* Chunk size iomoved to/from user */
126 struct selinfo pt_selr
, pt_selw
;
131 struct prison
*pt_prison
;
134 #define PF_PKT 0x08 /* packet mode */
135 #define PF_STOPPED 0x10 /* user told stopped */
136 #define PF_REMOTE 0x20 /* remote and flow controlled input */
137 #define PF_NOSTOP 0x40
138 #define PF_UCNTL 0x80 /* user control mode */
141 * This function creates and initializes a pts/ptc pair
143 * pts == /dev/tty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
144 * ptc == /dev/pty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
146 * XXX: define and add mapping of upper minor bits to allow more
154 char *names
= "pqrsPQRS";
157 /* For now we only map the lower 8 bits of the minor */
161 pt
= malloc(sizeof(*pt
), M_PTY
, M_WAITOK
);
162 bzero(pt
, sizeof(*pt
));
163 pt
->devs
= devs
= make_dev(&pts_cdevsw
, n
,
164 0, 0, 0666, "tty%c%r", names
[n
/ 32], n
% 32);
165 pt
->devc
= devc
= make_dev(&ptc_cdevsw
, n
,
166 0, 0, 0666, "pty%c%r", names
[n
/ 32], n
% 32);
168 devs
->si_drv1
= devc
->si_drv1
= pt
;
169 devs
->si_tty
= devc
->si_tty
= &pt
->pt_tty
;
170 pt
->pt_tty
.t_dev
= devs
;
171 ttyregister(&pt
->pt_tty
);
176 ptsopen(dev_t dev
, int flag
, int devtype
, struct thread
*td
)
184 struct pt_ioctl
*pti
;
193 * REMOVED gross hack for devfs. also note that makedev()
194 * no longer exists in this form and reference counting makes
195 * this a problem if we don't clean it out later.
198 nextdev
= make_sub_dev(dev
, minr
+ 1);
199 if (!nextdev
->si_drv1
) {
210 if ((tp
->t_state
& TS_ISOPEN
) == 0) {
211 ttychars(tp
); /* Set up default chars */
212 tp
->t_iflag
= TTYDEF_IFLAG
;
213 tp
->t_oflag
= TTYDEF_OFLAG
;
214 tp
->t_lflag
= TTYDEF_LFLAG
;
215 tp
->t_cflag
= TTYDEF_CFLAG
;
216 tp
->t_ispeed
= tp
->t_ospeed
= TTYDEF_SPEED
;
217 } else if (tp
->t_state
& TS_XCLUDE
&& suser(td
)) {
219 } else if (pti
->pt_prison
!= p
->p_ucred
->cr_prison
) {
222 if (tp
->t_oproc
) /* Ctrlr still around. */
223 (void)(*linesw
[tp
->t_line
].l_modem
)(tp
, 1);
224 while ((tp
->t_state
& TS_CARR_ON
) == 0) {
227 error
= ttysleep(tp
, TSA_CARR_ON(tp
), PCATCH
, "ptsopn", 0);
231 error
= (*linesw
[tp
->t_line
].l_open
)(dev
, tp
);
233 ptcwakeup(tp
, FREAD
|FWRITE
);
238 ptsclose(dev
, flag
, mode
, td
)
247 err
= (*linesw
[tp
->t_line
].l_close
)(tp
, flag
);
248 ptsstop(tp
, FREAD
|FWRITE
);
254 ptsread(dev
, uio
, flag
)
259 struct proc
*p
= curproc
;
260 struct tty
*tp
= dev
->si_tty
;
261 struct pt_ioctl
*pti
= dev
->si_drv1
;
265 if (pti
->pt_flags
& PF_REMOTE
) {
266 while (isbackground(p
, tp
)) {
267 if (SIGISMEMBER(p
->p_sigignore
, SIGTTIN
) ||
268 SIGISMEMBER(p
->p_sigmask
, SIGTTIN
) ||
269 p
->p_pgrp
->pg_jobc
== 0 || p
->p_flag
& P_PPWAIT
)
271 pgsignal(p
->p_pgrp
, SIGTTIN
, 1);
272 error
= ttysleep(tp
, &lbolt
, PCATCH
, "ptsbg", 0);
276 if (tp
->t_canq
.c_cc
== 0) {
277 if (flag
& IO_NDELAY
)
278 return (EWOULDBLOCK
);
279 error
= ttysleep(tp
, TSA_PTS_READ(tp
), PCATCH
,
285 while (tp
->t_canq
.c_cc
> 1 && uio
->uio_resid
> 0)
286 if (ureadc(getc(&tp
->t_canq
), uio
) < 0) {
290 if (tp
->t_canq
.c_cc
== 1)
291 (void) getc(&tp
->t_canq
);
296 error
= (*linesw
[tp
->t_line
].l_read
)(tp
, uio
, flag
);
297 ptcwakeup(tp
, FWRITE
);
302 * Write to pseudo-tty.
303 * Wakeups of controlling tty will happen
304 * indirectly, when tty driver calls ptsstart.
307 ptswrite(dev
, uio
, flag
)
315 if (tp
->t_oproc
== 0)
317 return ((*linesw
[tp
->t_line
].l_write
)(tp
, uio
, flag
));
321 * Start output on pseudo-tty.
322 * Wake up process selecting or sleeping for input from controlling tty.
328 struct pt_ioctl
*pti
= tp
->t_dev
->si_drv1
;
330 if (tp
->t_state
& TS_TTSTOP
)
332 if (pti
->pt_flags
& PF_STOPPED
) {
333 pti
->pt_flags
&= ~PF_STOPPED
;
334 pti
->pt_send
= TIOCPKT_START
;
336 ptcwakeup(tp
, FREAD
);
344 struct pt_ioctl
*pti
= tp
->t_dev
->si_drv1
;
347 selwakeup(&pti
->pt_selr
);
348 wakeup(TSA_PTC_READ(tp
));
351 selwakeup(&pti
->pt_selw
);
352 wakeup(TSA_PTC_WRITE(tp
));
357 ptcopen(dev
, flag
, devtype
, td
)
363 struct pt_ioctl
*pti
;
372 KKASSERT(td
->td_proc
!= NULL
);
373 tp
->t_oproc
= ptsstart
;
374 tp
->t_stop
= ptsstop
;
375 (void)(*linesw
[tp
->t_line
].l_modem
)(tp
, 1);
376 tp
->t_lflag
&= ~EXTPROC
;
378 pti
->pt_prison
= td
->td_proc
->p_ucred
->cr_prison
;
386 ptcclose(dev
, flags
, fmt
, td
)
395 (void)(*linesw
[tp
->t_line
].l_modem
)(tp
, 0);
398 * XXX MDMBUF makes no sense for ptys but would inhibit the above
399 * l_modem(). CLOCAL makes sense but isn't supported. Special
400 * l_modem()s that ignore carrier drop make no sense for ptys but
401 * may be in use because other parts of the line discipline make
402 * sense for ptys. Recover by doing everything that a normal
403 * ttymodem() would have done except for sending a SIGHUP.
405 if (tp
->t_state
& TS_ISOPEN
) {
406 tp
->t_state
&= ~(TS_CARR_ON
| TS_CONNECTED
);
407 tp
->t_state
|= TS_ZOMBIE
;
408 ttyflush(tp
, FREAD
| FWRITE
);
411 tp
->t_oproc
= 0; /* mark closed */
416 ptcread(dev
, uio
, flag
)
421 struct tty
*tp
= dev
->si_tty
;
422 struct pt_ioctl
*pti
= dev
->si_drv1
;
427 * We want to block until the slave
428 * is open, and there's something to read;
429 * but if we lost the slave or we're NBIO,
430 * then return the appropriate error instead.
433 if (tp
->t_state
&TS_ISOPEN
) {
434 if (pti
->pt_flags
&PF_PKT
&& pti
->pt_send
) {
435 error
= ureadc((int)pti
->pt_send
, uio
);
438 if (pti
->pt_send
& TIOCPKT_IOCTL
) {
439 cc
= min(uio
->uio_resid
,
440 sizeof(tp
->t_termios
));
441 uiomove((caddr_t
)&tp
->t_termios
, cc
,
447 if (pti
->pt_flags
&PF_UCNTL
&& pti
->pt_ucntl
) {
448 error
= ureadc((int)pti
->pt_ucntl
, uio
);
454 if (tp
->t_outq
.c_cc
&& (tp
->t_state
&TS_TTSTOP
) == 0)
457 if ((tp
->t_state
& TS_CONNECTED
) == 0)
458 return (0); /* EOF */
459 if (flag
& IO_NDELAY
)
460 return (EWOULDBLOCK
);
461 error
= tsleep(TSA_PTC_READ(tp
), PCATCH
, "ptcin", 0);
465 if (pti
->pt_flags
& (PF_PKT
|PF_UCNTL
))
466 error
= ureadc(0, uio
);
467 while (uio
->uio_resid
> 0 && error
== 0) {
468 cc
= q_to_b(&tp
->t_outq
, buf
, min(uio
->uio_resid
, BUFSIZ
));
471 error
= uiomove(buf
, cc
, uio
);
482 struct pt_ioctl
*pti
= tp
->t_dev
->si_drv1
;
485 /* note: FLUSHREAD and FLUSHWRITE already ok */
487 flush
= TIOCPKT_STOP
;
488 pti
->pt_flags
|= PF_STOPPED
;
490 pti
->pt_flags
&= ~PF_STOPPED
;
491 pti
->pt_send
|= flush
;
492 /* change of perspective */
502 ptcpoll(dev
, events
, td
)
507 struct tty
*tp
= dev
->si_tty
;
508 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
)