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.8 2003/07/26 19:42:11 rob 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>
58 #include <sys/device.h>
60 MALLOC_DEFINE(M_PTY, "ptys", "pty data structures");
62 static void ptsstart __P((struct tty *tp));
63 static void ptsstop __P((struct tty *tp, int rw));
64 static void ptcwakeup __P((struct tty *tp, int flag));
65 static void ptyinit __P((int n));
67 static d_open_t ptsopen;
68 static d_close_t ptsclose;
69 static d_read_t ptsread;
70 static d_write_t ptswrite;
71 static d_ioctl_t ptyioctl;
72 static d_open_t ptcopen;
73 static d_close_t ptcclose;
74 static d_read_t ptcread;
75 static d_write_t ptcwrite;
76 static d_poll_t ptcpoll;
78 #define CDEV_MAJOR_S 5
79 static struct cdevsw pts_cdevsw = {
81 /* maj */ CDEV_MAJOR_S,
82 /* flags */ D_TTY | D_KQFILTER,
93 /* strategy */ nostrategy,
96 /* kqfilter */ ttykqfilter
99 #define CDEV_MAJOR_C 6
100 static struct cdevsw ptc_cdevsw = {
102 /* maj */ CDEV_MAJOR_C,
103 /* flags */ D_TTY | D_KQFILTER | D_MASTER,
108 /* close */ ptcclose,
110 /* write */ ptcwrite,
111 /* ioctl */ ptyioctl,
114 /* strategy */ nostrategy,
117 /* kqfilter */ ttykqfilter
120 #define BUFSIZ 100 /* Chunk size iomoved to/from user */
124 struct selinfo pt_selr, pt_selw;
129 struct prison *pt_prison;
132 #define PF_PKT 0x08 /* packet mode */
133 #define PF_STOPPED 0x10 /* user told stopped */
134 #define PF_REMOTE 0x20 /* remote and flow controlled input */
135 #define PF_NOSTOP 0x40
136 #define PF_UCNTL 0x80 /* user control mode */
139 * This function creates and initializes a pts/ptc pair
141 * pts == /dev/tty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
142 * ptc == /dev/pty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
144 * XXX: define and add mapping of upper minor bits to allow more
152 char *names = "pqrsPQRS";
155 /* For now we only map the lower 8 bits of the minor */
159 pt = malloc(sizeof(*pt), M_PTY, M_WAITOK);
160 bzero(pt, sizeof(*pt));
161 pt->devs = devs = make_dev(&pts_cdevsw, n,
162 0, 0, 0666, "tty%c%r", names[n / 32], n % 32);
163 pt->devc = devc = make_dev(&ptc_cdevsw, n,
164 0, 0, 0666, "pty%c%r", names[n / 32], n % 32);
166 devs->si_drv1 = devc->si_drv1 = pt;
167 devs->si_tty = devc->si_tty = &pt->pt_tty;
168 pt->pt_tty.t_dev = devs;
169 ttyregister(&pt->pt_tty);
174 ptsopen(dev_t dev, int flag, int devtype, struct thread *td)
180 struct pt_ioctl *pti;
187 * XXX: Gross hack for DEVFS:
188 * XXX: DEVFS is no more, should this be removed?
189 * If we openned this device, ensure we have the
190 * next one too, so people can open it.
194 nextdev = makedev(major(dev), minr + 1);
195 if (!nextdev->si_drv1) {
205 if ((tp->t_state & TS_ISOPEN) == 0) {
206 ttychars(tp); /* Set up default chars */
207 tp->t_iflag = TTYDEF_IFLAG;
208 tp->t_oflag = TTYDEF_OFLAG;
209 tp->t_lflag = TTYDEF_LFLAG;
210 tp->t_cflag = TTYDEF_CFLAG;
211 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
212 } else if (tp->t_state & TS_XCLUDE && suser(td)) {
214 } else if (pti->pt_prison != p->p_ucred->cr_prison) {
217 if (tp->t_oproc) /* Ctrlr still around. */
218 (void)(*linesw[tp->t_line].l_modem)(tp, 1);
219 while ((tp->t_state & TS_CARR_ON) == 0) {
222 error = ttysleep(tp, TSA_CARR_ON(tp), PCATCH, "ptsopn", 0);
226 error = (*linesw[tp->t_line].l_open)(dev, tp);
228 ptcwakeup(tp, FREAD|FWRITE);
233 ptsclose(dev, flag, mode, td)
242 err = (*linesw[tp->t_line].l_close)(tp, flag);
243 ptsstop(tp, FREAD|FWRITE);
249 ptsread(dev, uio, flag)
254 struct proc *p = curproc;
255 struct tty *tp = dev->si_tty;
256 struct pt_ioctl *pti = dev->si_drv1;
260 if (pti->pt_flags & PF_REMOTE) {
261 while (isbackground(p, tp)) {
262 if (SIGISMEMBER(p->p_sigignore, SIGTTIN) ||
263 SIGISMEMBER(p->p_sigmask, SIGTTIN) ||
264 p->p_pgrp->pg_jobc == 0 || p->p_flag & P_PPWAIT)
266 pgsignal(p->p_pgrp, SIGTTIN, 1);
267 error = ttysleep(tp, &lbolt, PCATCH, "ptsbg", 0);
271 if (tp->t_canq.c_cc == 0) {
272 if (flag & IO_NDELAY)
273 return (EWOULDBLOCK);
274 error = ttysleep(tp, TSA_PTS_READ(tp), PCATCH,
280 while (tp->t_canq.c_cc > 1 && uio->uio_resid > 0)
281 if (ureadc(getc(&tp->t_canq), uio) < 0) {
285 if (tp->t_canq.c_cc == 1)
286 (void) getc(&tp->t_canq);
291 error = (*linesw[tp->t_line].l_read)(tp, uio, flag);
292 ptcwakeup(tp, FWRITE);
297 * Write to pseudo-tty.
298 * Wakeups of controlling tty will happen
299 * indirectly, when tty driver calls ptsstart.
302 ptswrite(dev, uio, flag)
310 if (tp->t_oproc == 0)
312 return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
316 * Start output on pseudo-tty.
317 * Wake up process selecting or sleeping for input from controlling tty.
323 struct pt_ioctl *pti = tp->t_dev->si_drv1;
325 if (tp->t_state & TS_TTSTOP)
327 if (pti->pt_flags & PF_STOPPED) {
328 pti->pt_flags &= ~PF_STOPPED;
329 pti->pt_send = TIOCPKT_START;
331 ptcwakeup(tp, FREAD);
339 struct pt_ioctl *pti = tp->t_dev->si_drv1;
342 selwakeup(&pti->pt_selr);
343 wakeup(TSA_PTC_READ(tp));
346 selwakeup(&pti->pt_selw);
347 wakeup(TSA_PTC_WRITE(tp));
352 ptcopen(dev, flag, devtype, td)
358 struct pt_ioctl *pti;
367 KKASSERT(td->td_proc != NULL);
368 tp->t_oproc = ptsstart;
369 tp->t_stop = ptsstop;
370 (void)(*linesw[tp->t_line].l_modem)(tp, 1);
371 tp->t_lflag &= ~EXTPROC;
373 pti->pt_prison = td->td_proc->p_ucred->cr_prison;
381 ptcclose(dev, flags, fmt, td)
390 (void)(*linesw[tp->t_line].l_modem)(tp, 0);
393 * XXX MDMBUF makes no sense for ptys but would inhibit the above
394 * l_modem(). CLOCAL makes sense but isn't supported. Special
395 * l_modem()s that ignore carrier drop make no sense for ptys but
396 * may be in use because other parts of the line discipline make
397 * sense for ptys. Recover by doing everything that a normal
398 * ttymodem() would have done except for sending a SIGHUP.
400 if (tp->t_state & TS_ISOPEN) {
401 tp->t_state &= ~(TS_CARR_ON | TS_CONNECTED);
402 tp->t_state |= TS_ZOMBIE;
403 ttyflush(tp, FREAD | FWRITE);
406 tp->t_oproc = 0; /* mark closed */
411 ptcread(dev, uio, flag)
416 struct tty *tp = dev->si_tty;
417 struct pt_ioctl *pti = dev->si_drv1;
422 * We want to block until the slave
423 * is open, and there's something to read;
424 * but if we lost the slave or we're NBIO,
425 * then return the appropriate error instead.
428 if (tp->t_state&TS_ISOPEN) {
429 if (pti->pt_flags&PF_PKT && pti->pt_send) {
430 error = ureadc((int)pti->pt_send, uio);
433 if (pti->pt_send & TIOCPKT_IOCTL) {
434 cc = min(uio->uio_resid,
435 sizeof(tp->t_termios));
436 uiomove((caddr_t)&tp->t_termios, cc,
442 if (pti->pt_flags&PF_UCNTL && pti->pt_ucntl) {
443 error = ureadc((int)pti->pt_ucntl, uio);
449 if (tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0)
452 if ((tp->t_state & TS_CONNECTED) == 0)
453 return (0); /* EOF */
454 if (flag & IO_NDELAY)
455 return (EWOULDBLOCK);
456 error = tsleep(TSA_PTC_READ(tp), PCATCH, "ptcin", 0);
460 if (pti->pt_flags & (PF_PKT|PF_UCNTL))
461 error = ureadc(0, uio);
462 while (uio->uio_resid > 0 && error == 0) {
463 cc = q_to_b(&tp->t_outq, buf, min(uio->uio_resid, BUFSIZ));
466 error = uiomove(buf, cc, uio);
477 struct pt_ioctl *pti = tp->t_dev->si_drv1;
480 /* note: FLUSHREAD and FLUSHWRITE already ok */
482 flush = TIOCPKT_STOP;
483 pti->pt_flags |= PF_STOPPED;
485 pti->pt_flags &= ~PF_STOPPED;
486 pti->pt_send |= flush;
487 /* change of perspective */
497 ptcpoll(dev, events, td)
502 struct tty *tp = dev->si_tty;
503 struct pt_ioctl *pti = dev->si_drv1;
507 if ((tp->t_state & TS_CONNECTED) == 0)
508 return (seltrue(dev, events, td) | POLLHUP);
511 * Need to block timeouts (ttrstart).
515 if (events & (POLLIN | POLLRDNORM))
516 if ((tp->t_state & TS_ISOPEN) &&
517 ((tp->t_outq.c_cc && (tp->t_state & TS_TTSTOP) == 0) ||
518 ((pti->pt_flags & PF_PKT) && pti->pt_send) ||
519 ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl)))
520 revents |= events & (POLLIN | POLLRDNORM);
522 if (events & (POLLOUT | POLLWRNORM))
523 if (tp->t_state & TS_ISOPEN &&
524 ((pti->pt_flags & PF_REMOTE) ?
525 (tp->t_canq.c_cc == 0) :
526 ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG - 2) ||
527 (tp->t_canq.c_cc == 0 && (tp->t_lflag & ICANON)))))
528 revents |= events & (POLLOUT | POLLWRNORM);
530 if (events & POLLHUP)
531 if ((tp->t_state & TS_CARR_ON) == 0)
535 if (events & (POLLIN | POLLRDNORM))
536 selrecord(td, &pti->pt_selr);
538 if (events & (POLLOUT | POLLWRNORM))
539 selrecord(td, &pti->pt_selw);
547 ptcwrite(dev, uio, flag)
552 struct tty *tp = dev->si_tty;
555 u_char locbuf[BUFSIZ];
557 struct pt_ioctl *pti = dev->si_drv1;
561 if ((tp->t_state&TS_ISOPEN) == 0)
563 if (pti->pt_flags & PF_REMOTE) {
566 while ((uio->uio_resid > 0 || cc > 0) &&
567 tp->t_canq.c_cc < TTYHOG - 1) {
569 cc = min(uio->uio_resid, BUFSIZ);
570 cc = min(cc, TTYHOG - 1 - tp->t_canq.c_cc);
572 error = uiomove((caddr_t)cp, cc, uio);
575 /* check again for safety */
576 if ((tp->t_state & TS_ISOPEN) == 0) {
577 /* adjust as usual */
578 uio->uio_resid += cc;
583 cc = b_to_q((char *)cp, cc, &tp->t_canq);
585 * XXX we don't guarantee that the canq size
586 * is >= TTYHOG, so the above b_to_q() may
587 * leave some bytes uncopied. However, space
588 * is guaranteed for the null terminator if
589 * we don't fail here since (TTYHOG - 1) is
590 * not a multiple of CBSIZE.
596 /* adjust for data copied in but not written */
597 uio->uio_resid += cc;
598 (void) putc(0, &tp->t_canq);
600 wakeup(TSA_PTS_READ(tp));
603 while (uio->uio_resid > 0 || cc > 0) {
605 cc = min(uio->uio_resid, BUFSIZ);
607 error = uiomove((caddr_t)cp, cc, uio);
610 /* check again for safety */
611 if ((tp->t_state & TS_ISOPEN) == 0) {
612 /* adjust for data copied in but not written */
613 uio->uio_resid += cc;
618 if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 &&
619 (tp->t_canq.c_cc > 0 || !(tp->t_lflag&ICANON))) {
620 wakeup(TSA_HUP_OR_INPUT(tp));
623 (*linesw[tp->t_line].l_rint)(*cp++, tp);
632 * Come here to wait for slave to open, for space
633 * in outq, or space in rawq, or an empty canq.
635 if ((tp->t_state & TS_CONNECTED) == 0) {
636 /* adjust for data copied in but not written */
637 uio->uio_resid += cc;
640 if (flag & IO_NDELAY) {
641 /* adjust for data copied in but not written */
642 uio->uio_resid += cc;
644 return (EWOULDBLOCK);
647 error = tsleep(TSA_PTC_WRITE(tp), PCATCH, "ptcout", 0);
649 /* adjust for data copied in but not written */
650 uio->uio_resid += cc;
658 ptyioctl(dev, cmd, data, flag, td)
665 struct tty *tp = dev->si_tty;
666 struct pt_ioctl *pti = dev->si_drv1;
667 u_char *cc = tp->t_cc;
670 if (dev_dflags(dev) & D_MASTER) {
675 * We avoid calling ttioctl on the controller since,
676 * in that case, tp must be the controlling terminal.
678 *(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
683 if (pti->pt_flags & PF_UCNTL)
685 pti->pt_flags |= PF_PKT;
687 pti->pt_flags &= ~PF_PKT;
692 if (pti->pt_flags & PF_PKT)
694 pti->pt_flags |= PF_UCNTL;
696 pti->pt_flags &= ~PF_UCNTL;
701 pti->pt_flags |= PF_REMOTE;
703 pti->pt_flags &= ~PF_REMOTE;
704 ttyflush(tp, FREAD|FWRITE);
709 * The rest of the ioctls shouldn't be called until
712 if ((tp->t_state & TS_ISOPEN) == 0)
725 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
726 * ttywflush(tp) will hang if there are characters in
729 ndflush(&tp->t_outq, tp->t_outq.c_cc);
733 if (*(unsigned int *)data >= NSIG ||
734 *(unsigned int *)data == 0)
736 if ((tp->t_lflag&NOFLSH) == 0)
737 ttyflush(tp, FREAD|FWRITE);
738 pgsignal(tp->t_pgrp, *(unsigned int *)data, 1);
739 if ((*(unsigned int *)data == SIGINFO) &&
740 ((tp->t_lflag&NOKERNINFO) == 0))
745 if (cmd == TIOCEXT) {
747 * When the EXTPROC bit is being toggled, we need
748 * to send an TIOCPKT_IOCTL if the packet driver
752 if (pti->pt_flags & PF_PKT) {
753 pti->pt_send |= TIOCPKT_IOCTL;
754 ptcwakeup(tp, FREAD);
756 tp->t_lflag |= EXTPROC;
758 if ((tp->t_lflag & EXTPROC) &&
759 (pti->pt_flags & PF_PKT)) {
760 pti->pt_send |= TIOCPKT_IOCTL;
761 ptcwakeup(tp, FREAD);
763 tp->t_lflag &= ~EXTPROC;
767 error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, td);
768 if (error == ENOIOCTL)
769 error = ttioctl(tp, cmd, data, flag);
770 if (error == ENOIOCTL) {
771 if (pti->pt_flags & PF_UCNTL &&
772 (cmd & ~0xff) == UIOCCMD(0)) {
774 pti->pt_ucntl = (u_char)cmd;
775 ptcwakeup(tp, FREAD);
782 * If external processing and packet mode send ioctl packet.
784 if ((tp->t_lflag&EXTPROC) && (pti->pt_flags & PF_PKT)) {
793 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
800 pti->pt_send |= TIOCPKT_IOCTL;
801 ptcwakeup(tp, FREAD);
806 stop = (tp->t_iflag & IXON) && CCEQ(cc[VSTOP], CTRL('s'))
807 && CCEQ(cc[VSTART], CTRL('q'));
808 if (pti->pt_flags & PF_NOSTOP) {
810 pti->pt_send &= ~TIOCPKT_NOSTOP;
811 pti->pt_send |= TIOCPKT_DOSTOP;
812 pti->pt_flags &= ~PF_NOSTOP;
813 ptcwakeup(tp, FREAD);
817 pti->pt_send &= ~TIOCPKT_DOSTOP;
818 pti->pt_send |= TIOCPKT_NOSTOP;
819 pti->pt_flags |= PF_NOSTOP;
820 ptcwakeup(tp, FREAD);
827 static void ptc_drvinit __P((void *unused));
833 cdevsw_add(&pts_cdevsw);
834 cdevsw_add(&ptc_cdevsw);
835 /* XXX: Gross hack for DEVFS */
836 /* XXX: DEVFS is no more, should this be removed? */
840 SYSINIT(ptcdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR_C,ptc_drvinit,NULL)