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.5 2003/07/19 21:14:39 dillon Exp $
39 * Pseudo-teletype Driver
40 * (Actually two drivers, requiring two entries in 'cdevsw')
42 #include "pty.h" /* XXX */
43 #include "opt_compat.h"
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
47 #include <sys/ioctl_compat.h>
52 #include <sys/fcntl.h>
54 #include <sys/kernel.h>
55 #include <sys/vnode.h>
56 #include <sys/signalvar.h>
57 #include <sys/malloc.h>
59 MALLOC_DEFINE(M_PTY, "ptys", "pty data structures");
61 static void ptsstart __P((struct tty *tp));
62 static void ptsstop __P((struct tty *tp, int rw));
63 static void ptcwakeup __P((struct tty *tp, int flag));
64 static void ptyinit __P((int n));
66 static d_open_t ptsopen;
67 static d_close_t ptsclose;
68 static d_read_t ptsread;
69 static d_write_t ptswrite;
70 static d_ioctl_t ptyioctl;
71 static d_open_t ptcopen;
72 static d_close_t ptcclose;
73 static d_read_t ptcread;
74 static d_write_t ptcwrite;
75 static d_poll_t ptcpoll;
77 #define CDEV_MAJOR_S 5
78 static struct cdevsw pts_cdevsw = {
86 /* strategy */ nostrategy,
88 /* maj */ CDEV_MAJOR_S,
91 /* flags */ D_TTY | D_KQFILTER,
93 /* kqfilter */ ttykqfilter,
96 #define CDEV_MAJOR_C 6
97 static struct cdevsw ptc_cdevsw = {
101 /* write */ ptcwrite,
102 /* ioctl */ ptyioctl,
105 /* strategy */ nostrategy,
107 /* maj */ CDEV_MAJOR_C,
110 /* flags */ D_TTY | D_KQFILTER,
112 /* kqfilter */ ttykqfilter,
115 #define BUFSIZ 100 /* Chunk size iomoved to/from user */
119 struct selinfo pt_selr, pt_selw;
124 struct prison *pt_prison;
127 #define PF_PKT 0x08 /* packet mode */
128 #define PF_STOPPED 0x10 /* user told stopped */
129 #define PF_REMOTE 0x20 /* remote and flow controlled input */
130 #define PF_NOSTOP 0x40
131 #define PF_UCNTL 0x80 /* user control mode */
134 * This function creates and initializes a pts/ptc pair
136 * pts == /dev/tty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
137 * ptc == /dev/pty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
139 * XXX: define and add mapping of upper minor bits to allow more
147 char *names = "pqrsPQRS";
150 /* For now we only map the lower 8 bits of the minor */
154 pt = malloc(sizeof(*pt), M_PTY, M_WAITOK);
155 bzero(pt, sizeof(*pt));
156 pt->devs = devs = make_dev(&pts_cdevsw, n,
157 0, 0, 0666, "tty%c%r", names[n / 32], n % 32);
158 pt->devc = devc = make_dev(&ptc_cdevsw, n,
159 0, 0, 0666, "pty%c%r", names[n / 32], n % 32);
161 devs->si_drv1 = devc->si_drv1 = pt;
162 devs->si_tty = devc->si_tty = &pt->pt_tty;
163 pt->pt_tty.t_dev = devs;
164 ttyregister(&pt->pt_tty);
169 ptsopen(dev_t dev, int flag, int devtype, struct thread *td)
171 register struct tty *tp;
175 struct pt_ioctl *pti;
182 * XXX: Gross hack for DEVFS:
183 * XXX: DEVFS is no more, should this be removed?
184 * If we openned this device, ensure we have the
185 * next one too, so people can open it.
189 nextdev = makedev(major(dev), minr + 1);
190 if (!nextdev->si_drv1) {
200 if ((tp->t_state & TS_ISOPEN) == 0) {
201 ttychars(tp); /* Set up default chars */
202 tp->t_iflag = TTYDEF_IFLAG;
203 tp->t_oflag = TTYDEF_OFLAG;
204 tp->t_lflag = TTYDEF_LFLAG;
205 tp->t_cflag = TTYDEF_CFLAG;
206 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
207 } else if (tp->t_state & TS_XCLUDE && suser(td)) {
209 } else if (pti->pt_prison != p->p_ucred->cr_prison) {
212 if (tp->t_oproc) /* Ctrlr still around. */
213 (void)(*linesw[tp->t_line].l_modem)(tp, 1);
214 while ((tp->t_state & TS_CARR_ON) == 0) {
217 error = ttysleep(tp, TSA_CARR_ON(tp), PCATCH, "ptsopn", 0);
221 error = (*linesw[tp->t_line].l_open)(dev, tp);
223 ptcwakeup(tp, FREAD|FWRITE);
228 ptsclose(dev, flag, mode, td)
233 register struct tty *tp;
237 err = (*linesw[tp->t_line].l_close)(tp, flag);
238 ptsstop(tp, FREAD|FWRITE);
244 ptsread(dev, uio, flag)
249 struct proc *p = curproc;
250 register struct tty *tp = dev->si_tty;
251 register struct pt_ioctl *pti = dev->si_drv1;
255 if (pti->pt_flags & PF_REMOTE) {
256 while (isbackground(p, tp)) {
257 if (SIGISMEMBER(p->p_sigignore, SIGTTIN) ||
258 SIGISMEMBER(p->p_sigmask, SIGTTIN) ||
259 p->p_pgrp->pg_jobc == 0 || p->p_flag & P_PPWAIT)
261 pgsignal(p->p_pgrp, SIGTTIN, 1);
262 error = ttysleep(tp, &lbolt, PCATCH, "ptsbg", 0);
266 if (tp->t_canq.c_cc == 0) {
267 if (flag & IO_NDELAY)
268 return (EWOULDBLOCK);
269 error = ttysleep(tp, TSA_PTS_READ(tp), PCATCH,
275 while (tp->t_canq.c_cc > 1 && uio->uio_resid > 0)
276 if (ureadc(getc(&tp->t_canq), uio) < 0) {
280 if (tp->t_canq.c_cc == 1)
281 (void) getc(&tp->t_canq);
286 error = (*linesw[tp->t_line].l_read)(tp, uio, flag);
287 ptcwakeup(tp, FWRITE);
292 * Write to pseudo-tty.
293 * Wakeups of controlling tty will happen
294 * indirectly, when tty driver calls ptsstart.
297 ptswrite(dev, uio, flag)
302 register struct tty *tp;
305 if (tp->t_oproc == 0)
307 return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
311 * Start output on pseudo-tty.
312 * Wake up process selecting or sleeping for input from controlling tty.
318 register struct pt_ioctl *pti = tp->t_dev->si_drv1;
320 if (tp->t_state & TS_TTSTOP)
322 if (pti->pt_flags & PF_STOPPED) {
323 pti->pt_flags &= ~PF_STOPPED;
324 pti->pt_send = TIOCPKT_START;
326 ptcwakeup(tp, FREAD);
334 struct pt_ioctl *pti = tp->t_dev->si_drv1;
337 selwakeup(&pti->pt_selr);
338 wakeup(TSA_PTC_READ(tp));
341 selwakeup(&pti->pt_selw);
342 wakeup(TSA_PTC_WRITE(tp));
347 ptcopen(dev, flag, devtype, td)
352 register struct tty *tp;
353 struct pt_ioctl *pti;
362 KKASSERT(td->td_proc != NULL);
363 tp->t_oproc = ptsstart;
364 tp->t_stop = ptsstop;
365 (void)(*linesw[tp->t_line].l_modem)(tp, 1);
366 tp->t_lflag &= ~EXTPROC;
368 pti->pt_prison = td->td_proc->p_ucred->cr_prison;
376 ptcclose(dev, flags, fmt, td)
382 register struct tty *tp;
385 (void)(*linesw[tp->t_line].l_modem)(tp, 0);
388 * XXX MDMBUF makes no sense for ptys but would inhibit the above
389 * l_modem(). CLOCAL makes sense but isn't supported. Special
390 * l_modem()s that ignore carrier drop make no sense for ptys but
391 * may be in use because other parts of the line discipline make
392 * sense for ptys. Recover by doing everything that a normal
393 * ttymodem() would have done except for sending a SIGHUP.
395 if (tp->t_state & TS_ISOPEN) {
396 tp->t_state &= ~(TS_CARR_ON | TS_CONNECTED);
397 tp->t_state |= TS_ZOMBIE;
398 ttyflush(tp, FREAD | FWRITE);
401 tp->t_oproc = 0; /* mark closed */
406 ptcread(dev, uio, flag)
411 register struct tty *tp = dev->si_tty;
412 struct pt_ioctl *pti = dev->si_drv1;
417 * We want to block until the slave
418 * is open, and there's something to read;
419 * but if we lost the slave or we're NBIO,
420 * then return the appropriate error instead.
423 if (tp->t_state&TS_ISOPEN) {
424 if (pti->pt_flags&PF_PKT && pti->pt_send) {
425 error = ureadc((int)pti->pt_send, uio);
428 if (pti->pt_send & TIOCPKT_IOCTL) {
429 cc = min(uio->uio_resid,
430 sizeof(tp->t_termios));
431 uiomove((caddr_t)&tp->t_termios, cc,
437 if (pti->pt_flags&PF_UCNTL && pti->pt_ucntl) {
438 error = ureadc((int)pti->pt_ucntl, uio);
444 if (tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0)
447 if ((tp->t_state & TS_CONNECTED) == 0)
448 return (0); /* EOF */
449 if (flag & IO_NDELAY)
450 return (EWOULDBLOCK);
451 error = tsleep(TSA_PTC_READ(tp), PCATCH, "ptcin", 0);
455 if (pti->pt_flags & (PF_PKT|PF_UCNTL))
456 error = ureadc(0, uio);
457 while (uio->uio_resid > 0 && error == 0) {
458 cc = q_to_b(&tp->t_outq, buf, min(uio->uio_resid, BUFSIZ));
461 error = uiomove(buf, cc, uio);
469 register struct tty *tp;
472 struct pt_ioctl *pti = tp->t_dev->si_drv1;
475 /* note: FLUSHREAD and FLUSHWRITE already ok */
477 flush = TIOCPKT_STOP;
478 pti->pt_flags |= PF_STOPPED;
480 pti->pt_flags &= ~PF_STOPPED;
481 pti->pt_send |= flush;
482 /* change of perspective */
492 ptcpoll(dev, events, td)
497 register struct tty *tp = dev->si_tty;
498 struct pt_ioctl *pti = dev->si_drv1;
502 if ((tp->t_state & TS_CONNECTED) == 0)
503 return (seltrue(dev, events, td) | POLLHUP);
506 * Need to block timeouts (ttrstart).
510 if (events & (POLLIN | POLLRDNORM))
511 if ((tp->t_state & TS_ISOPEN) &&
512 ((tp->t_outq.c_cc && (tp->t_state & TS_TTSTOP) == 0) ||
513 ((pti->pt_flags & PF_PKT) && pti->pt_send) ||
514 ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl)))
515 revents |= events & (POLLIN | POLLRDNORM);
517 if (events & (POLLOUT | POLLWRNORM))
518 if (tp->t_state & TS_ISOPEN &&
519 ((pti->pt_flags & PF_REMOTE) ?
520 (tp->t_canq.c_cc == 0) :
521 ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG - 2) ||
522 (tp->t_canq.c_cc == 0 && (tp->t_lflag & ICANON)))))
523 revents |= events & (POLLOUT | POLLWRNORM);
525 if (events & POLLHUP)
526 if ((tp->t_state & TS_CARR_ON) == 0)
530 if (events & (POLLIN | POLLRDNORM))
531 selrecord(td, &pti->pt_selr);
533 if (events & (POLLOUT | POLLWRNORM))
534 selrecord(td, &pti->pt_selw);
542 ptcwrite(dev, uio, flag)
544 register struct uio *uio;
547 register struct tty *tp = dev->si_tty;
548 register u_char *cp = 0;
550 u_char locbuf[BUFSIZ];
552 struct pt_ioctl *pti = dev->si_drv1;
556 if ((tp->t_state&TS_ISOPEN) == 0)
558 if (pti->pt_flags & PF_REMOTE) {
561 while ((uio->uio_resid > 0 || cc > 0) &&
562 tp->t_canq.c_cc < TTYHOG - 1) {
564 cc = min(uio->uio_resid, BUFSIZ);
565 cc = min(cc, TTYHOG - 1 - tp->t_canq.c_cc);
567 error = uiomove((caddr_t)cp, cc, uio);
570 /* check again for safety */
571 if ((tp->t_state & TS_ISOPEN) == 0) {
572 /* adjust as usual */
573 uio->uio_resid += cc;
578 cc = b_to_q((char *)cp, cc, &tp->t_canq);
580 * XXX we don't guarantee that the canq size
581 * is >= TTYHOG, so the above b_to_q() may
582 * leave some bytes uncopied. However, space
583 * is guaranteed for the null terminator if
584 * we don't fail here since (TTYHOG - 1) is
585 * not a multiple of CBSIZE.
591 /* adjust for data copied in but not written */
592 uio->uio_resid += cc;
593 (void) putc(0, &tp->t_canq);
595 wakeup(TSA_PTS_READ(tp));
598 while (uio->uio_resid > 0 || cc > 0) {
600 cc = min(uio->uio_resid, BUFSIZ);
602 error = uiomove((caddr_t)cp, cc, uio);
605 /* check again for safety */
606 if ((tp->t_state & TS_ISOPEN) == 0) {
607 /* adjust for data copied in but not written */
608 uio->uio_resid += cc;
613 if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 &&
614 (tp->t_canq.c_cc > 0 || !(tp->t_lflag&ICANON))) {
615 wakeup(TSA_HUP_OR_INPUT(tp));
618 (*linesw[tp->t_line].l_rint)(*cp++, tp);
627 * Come here to wait for slave to open, for space
628 * in outq, or space in rawq, or an empty canq.
630 if ((tp->t_state & TS_CONNECTED) == 0) {
631 /* adjust for data copied in but not written */
632 uio->uio_resid += cc;
635 if (flag & IO_NDELAY) {
636 /* adjust for data copied in but not written */
637 uio->uio_resid += cc;
639 return (EWOULDBLOCK);
642 error = tsleep(TSA_PTC_WRITE(tp), PCATCH, "ptcout", 0);
644 /* adjust for data copied in but not written */
645 uio->uio_resid += cc;
653 ptyioctl(dev, cmd, data, flag, td)
660 register struct tty *tp = dev->si_tty;
661 register struct pt_ioctl *pti = dev->si_drv1;
662 register u_char *cc = tp->t_cc;
665 if (devsw(dev)->d_open == ptcopen) {
670 * We avoid calling ttioctl on the controller since,
671 * in that case, tp must be the controlling terminal.
673 *(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
678 if (pti->pt_flags & PF_UCNTL)
680 pti->pt_flags |= PF_PKT;
682 pti->pt_flags &= ~PF_PKT;
687 if (pti->pt_flags & PF_PKT)
689 pti->pt_flags |= PF_UCNTL;
691 pti->pt_flags &= ~PF_UCNTL;
696 pti->pt_flags |= PF_REMOTE;
698 pti->pt_flags &= ~PF_REMOTE;
699 ttyflush(tp, FREAD|FWRITE);
704 * The rest of the ioctls shouldn't be called until
707 if ((tp->t_state & TS_ISOPEN) == 0)
720 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
721 * ttywflush(tp) will hang if there are characters in
724 ndflush(&tp->t_outq, tp->t_outq.c_cc);
728 if (*(unsigned int *)data >= NSIG ||
729 *(unsigned int *)data == 0)
731 if ((tp->t_lflag&NOFLSH) == 0)
732 ttyflush(tp, FREAD|FWRITE);
733 pgsignal(tp->t_pgrp, *(unsigned int *)data, 1);
734 if ((*(unsigned int *)data == SIGINFO) &&
735 ((tp->t_lflag&NOKERNINFO) == 0))
740 if (cmd == TIOCEXT) {
742 * When the EXTPROC bit is being toggled, we need
743 * to send an TIOCPKT_IOCTL if the packet driver
747 if (pti->pt_flags & PF_PKT) {
748 pti->pt_send |= TIOCPKT_IOCTL;
749 ptcwakeup(tp, FREAD);
751 tp->t_lflag |= EXTPROC;
753 if ((tp->t_lflag & EXTPROC) &&
754 (pti->pt_flags & PF_PKT)) {
755 pti->pt_send |= TIOCPKT_IOCTL;
756 ptcwakeup(tp, FREAD);
758 tp->t_lflag &= ~EXTPROC;
762 error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, td);
763 if (error == ENOIOCTL)
764 error = ttioctl(tp, cmd, data, flag);
765 if (error == ENOIOCTL) {
766 if (pti->pt_flags & PF_UCNTL &&
767 (cmd & ~0xff) == UIOCCMD(0)) {
769 pti->pt_ucntl = (u_char)cmd;
770 ptcwakeup(tp, FREAD);
777 * If external processing and packet mode send ioctl packet.
779 if ((tp->t_lflag&EXTPROC) && (pti->pt_flags & PF_PKT)) {
788 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
795 pti->pt_send |= TIOCPKT_IOCTL;
796 ptcwakeup(tp, FREAD);
801 stop = (tp->t_iflag & IXON) && CCEQ(cc[VSTOP], CTRL('s'))
802 && CCEQ(cc[VSTART], CTRL('q'));
803 if (pti->pt_flags & PF_NOSTOP) {
805 pti->pt_send &= ~TIOCPKT_NOSTOP;
806 pti->pt_send |= TIOCPKT_DOSTOP;
807 pti->pt_flags &= ~PF_NOSTOP;
808 ptcwakeup(tp, FREAD);
812 pti->pt_send &= ~TIOCPKT_DOSTOP;
813 pti->pt_send |= TIOCPKT_NOSTOP;
814 pti->pt_flags |= PF_NOSTOP;
815 ptcwakeup(tp, FREAD);
822 static void ptc_drvinit __P((void *unused));
828 cdevsw_add(&pts_cdevsw);
829 cdevsw_add(&ptc_cdevsw);
830 /* XXX: Gross hack for DEVFS */
831 /* XXX: DEVFS is no more, should this be removed? */
835 SYSINIT(ptcdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR_C,ptc_drvinit,NULL)