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.21 2008/08/13 10:29:38 swildner Exp $
39 * Pseudo-teletype Driver
40 * (Actually two drivers, requiring two dev_ops structures)
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>
54 #include <sys/fcntl.h>
56 #include <sys/kernel.h>
57 #include <sys/vnode.h>
58 #include <sys/signalvar.h>
59 #include <sys/malloc.h>
60 #include <sys/device.h>
61 #include <sys/thread2.h>
63 MALLOC_DEFINE(M_PTY, "ptys", "pty data structures");
65 static void ptsstart (struct tty *tp);
66 static void ptsstop (struct tty *tp, int rw);
67 static void ptcwakeup (struct tty *tp, int flag);
68 static void ptyinit (int n);
70 static d_open_t ptsopen;
71 static d_close_t ptsclose;
72 static d_read_t ptsread;
73 static d_write_t ptswrite;
74 static d_ioctl_t ptyioctl;
75 static d_open_t ptcopen;
76 static d_close_t ptcclose;
77 static d_read_t ptcread;
78 static d_write_t ptcwrite;
79 static d_poll_t ptcpoll;
81 #define CDEV_MAJOR_S 5
82 static struct dev_ops pts_ops = {
83 { "pts", CDEV_MAJOR_S, D_TTY | D_KQFILTER },
90 .d_kqfilter = ttykqfilter
93 #define CDEV_MAJOR_C 6
94 static struct dev_ops ptc_ops = {
95 { "ptc", CDEV_MAJOR_C, D_TTY | D_KQFILTER | D_MASTER },
102 .d_kqfilter = ttykqfilter
105 #define BUFSIZ 100 /* Chunk size iomoved to/from user */
109 struct selinfo pt_selr, pt_selw;
114 struct prison *pt_prison;
117 #define PF_PKT 0x08 /* packet mode */
118 #define PF_STOPPED 0x10 /* user told stopped */
119 #define PF_REMOTE 0x20 /* remote and flow controlled input */
120 #define PF_NOSTOP 0x40
121 #define PF_UCNTL 0x80 /* user control mode */
124 * This function creates and initializes a pts/ptc pair
126 * pts == /dev/tty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
127 * ptc == /dev/pty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
129 * XXX: define and add mapping of upper minor bits to allow more
136 char *names = "pqrsPQRS";
139 /* For now we only map the lower 8 bits of the minor */
143 pt = kmalloc(sizeof(*pt), M_PTY, M_WAITOK | M_ZERO);
144 pt->devs = devs = make_dev(&pts_ops, n,
145 0, 0, 0666, "tty%c%r", names[n / 32], n % 32);
146 pt->devc = devc = make_dev(&ptc_ops, n,
147 0, 0, 0666, "pty%c%r", names[n / 32], n % 32);
149 devs->si_drv1 = devc->si_drv1 = pt;
150 devs->si_tty = devc->si_tty = &pt->pt_tty;
151 pt->pt_tty.t_dev = devs;
152 ttyregister(&pt->pt_tty);
157 ptsopen(struct dev_open_args *ap)
159 cdev_t dev = ap->a_head.a_dev;
166 struct pt_ioctl *pti;
171 * REMOVED gross hack for devfs. also note that makedev()
172 * no longer exists in this form and reference counting makes
173 * this a problem if we don't clean it out later.
176 nextdev = make_sub_dev(dev, minr + 1);
177 if (!nextdev->si_drv1) {
188 if ((tp->t_state & TS_ISOPEN) == 0) {
189 ttychars(tp); /* Set up default chars */
190 tp->t_iflag = TTYDEF_IFLAG;
191 tp->t_oflag = TTYDEF_OFLAG;
192 tp->t_lflag = TTYDEF_LFLAG;
193 tp->t_cflag = TTYDEF_CFLAG;
194 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
195 } else if ((tp->t_state & TS_XCLUDE) && priv_check_cred(ap->a_cred, PRIV_ROOT, 0)) {
197 } else if (pti->pt_prison != ap->a_cred->cr_prison) {
200 if (tp->t_oproc) /* Ctrlr still around. */
201 (void)(*linesw[tp->t_line].l_modem)(tp, 1);
202 while ((tp->t_state & TS_CARR_ON) == 0) {
203 if (ap->a_oflags & FNONBLOCK)
205 error = ttysleep(tp, TSA_CARR_ON(tp), PCATCH, "ptsopn", 0);
209 error = (*linesw[tp->t_line].l_open)(dev, tp);
211 ptcwakeup(tp, FREAD|FWRITE);
216 ptsclose(struct dev_close_args *ap)
218 cdev_t dev = ap->a_head.a_dev;
223 err = (*linesw[tp->t_line].l_close)(tp, ap->a_fflag);
224 ptsstop(tp, FREAD|FWRITE);
230 ptsread(struct dev_read_args *ap)
232 cdev_t dev = ap->a_head.a_dev;
233 struct proc *p = curproc;
234 struct tty *tp = dev->si_tty;
235 struct pt_ioctl *pti = dev->si_drv1;
240 lp = curthread->td_lwp;
243 if (pti->pt_flags & PF_REMOTE) {
244 while (isbackground(p, tp)) {
245 if (SIGISMEMBER(p->p_sigignore, SIGTTIN) ||
246 SIGISMEMBER(lp->lwp_sigmask, SIGTTIN) ||
247 p->p_pgrp->pg_jobc == 0 || p->p_flag & P_PPWAIT)
249 pgsignal(p->p_pgrp, SIGTTIN, 1);
250 error = ttysleep(tp, &lbolt, PCATCH, "ptsbg", 0);
254 if (tp->t_canq.c_cc == 0) {
255 if (ap->a_ioflag & IO_NDELAY)
256 return (EWOULDBLOCK);
257 error = ttysleep(tp, TSA_PTS_READ(tp), PCATCH,
263 while (tp->t_canq.c_cc > 1 && ap->a_uio->uio_resid > 0)
264 if (ureadc(clist_getc(&tp->t_canq), ap->a_uio) < 0) {
268 if (tp->t_canq.c_cc == 1)
269 clist_getc(&tp->t_canq);
274 error = (*linesw[tp->t_line].l_read)(tp, ap->a_uio, ap->a_ioflag);
275 ptcwakeup(tp, FWRITE);
280 * Write to pseudo-tty.
281 * Wakeups of controlling tty will happen
282 * indirectly, when tty driver calls ptsstart.
285 ptswrite(struct dev_write_args *ap)
287 cdev_t dev = ap->a_head.a_dev;
291 if (tp->t_oproc == 0)
293 return ((*linesw[tp->t_line].l_write)(tp, ap->a_uio, ap->a_ioflag));
297 * Start output on pseudo-tty.
298 * Wake up process selecting or sleeping for input from controlling tty.
301 ptsstart(struct tty *tp)
303 struct pt_ioctl *pti = tp->t_dev->si_drv1;
305 if (tp->t_state & TS_TTSTOP)
307 if (pti->pt_flags & PF_STOPPED) {
308 pti->pt_flags &= ~PF_STOPPED;
309 pti->pt_send = TIOCPKT_START;
311 ptcwakeup(tp, FREAD);
315 ptcwakeup(struct tty *tp, int flag)
317 struct pt_ioctl *pti = tp->t_dev->si_drv1;
320 selwakeup(&pti->pt_selr);
321 wakeup(TSA_PTC_READ(tp));
324 selwakeup(&pti->pt_selw);
325 wakeup(TSA_PTC_WRITE(tp));
330 ptcopen(struct dev_open_args *ap)
332 cdev_t dev = ap->a_head.a_dev;
334 struct pt_ioctl *pti;
343 tp->t_oproc = ptsstart;
344 tp->t_stop = ptsstop;
345 (void)(*linesw[tp->t_line].l_modem)(tp, 1);
346 tp->t_lflag &= ~EXTPROC;
348 pti->pt_prison = ap->a_cred->cr_prison;
352 pti->devs->si_uid = ap->a_cred->cr_uid;
357 ptcclose(struct dev_close_args *ap)
359 cdev_t dev = ap->a_head.a_dev;
363 (void)(*linesw[tp->t_line].l_modem)(tp, 0);
366 * XXX MDMBUF makes no sense for ptys but would inhibit the above
367 * l_modem(). CLOCAL makes sense but isn't supported. Special
368 * l_modem()s that ignore carrier drop make no sense for ptys but
369 * may be in use because other parts of the line discipline make
370 * sense for ptys. Recover by doing everything that a normal
371 * ttymodem() would have done except for sending a SIGHUP.
373 if (tp->t_state & TS_ISOPEN) {
374 tp->t_state &= ~(TS_CARR_ON | TS_CONNECTED);
375 tp->t_state |= TS_ZOMBIE;
376 ttyflush(tp, FREAD | FWRITE);
377 tp->t_dev->si_uid = 0;
380 tp->t_oproc = 0; /* mark closed */
385 ptcread(struct dev_read_args *ap)
387 cdev_t dev = ap->a_head.a_dev;
388 struct tty *tp = dev->si_tty;
389 struct pt_ioctl *pti = dev->si_drv1;
394 * We want to block until the slave
395 * is open, and there's something to read;
396 * but if we lost the slave or we're NBIO,
397 * then return the appropriate error instead.
400 if (tp->t_state&TS_ISOPEN) {
401 if (pti->pt_flags&PF_PKT && pti->pt_send) {
402 error = ureadc((int)pti->pt_send, ap->a_uio);
405 if (pti->pt_send & TIOCPKT_IOCTL) {
406 cc = min(ap->a_uio->uio_resid,
407 sizeof(tp->t_termios));
408 uiomove((caddr_t)&tp->t_termios, cc,
414 if (pti->pt_flags&PF_UCNTL && pti->pt_ucntl) {
415 error = ureadc((int)pti->pt_ucntl, ap->a_uio);
421 if (tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0)
424 if ((tp->t_state & TS_CONNECTED) == 0)
425 return (0); /* EOF */
426 if (ap->a_ioflag & IO_NDELAY)
427 return (EWOULDBLOCK);
428 error = tsleep(TSA_PTC_READ(tp), PCATCH, "ptcin", 0);
432 if (pti->pt_flags & (PF_PKT|PF_UCNTL))
433 error = ureadc(0, ap->a_uio);
434 while (ap->a_uio->uio_resid > 0 && error == 0) {
435 cc = q_to_b(&tp->t_outq, buf, min(ap->a_uio->uio_resid, BUFSIZ));
438 error = uiomove(buf, cc, ap->a_uio);
445 ptsstop(struct tty *tp, int flush)
447 struct pt_ioctl *pti = tp->t_dev->si_drv1;
450 /* note: FLUSHREAD and FLUSHWRITE already ok */
452 flush = TIOCPKT_STOP;
453 pti->pt_flags |= PF_STOPPED;
455 pti->pt_flags &= ~PF_STOPPED;
456 pti->pt_send |= flush;
457 /* change of perspective */
467 ptcpoll(struct dev_poll_args *ap)
469 cdev_t dev = ap->a_head.a_dev;
470 struct tty *tp = dev->si_tty;
471 struct pt_ioctl *pti = dev->si_drv1;
474 if ((tp->t_state & TS_CONNECTED) == 0) {
475 ap->a_events = seltrue(dev, ap->a_events) | POLLHUP;
480 * Need to block timeouts (ttrstart).
484 if (ap->a_events & (POLLIN | POLLRDNORM))
485 if ((tp->t_state & TS_ISOPEN) &&
486 ((tp->t_outq.c_cc && (tp->t_state & TS_TTSTOP) == 0) ||
487 ((pti->pt_flags & PF_PKT) && pti->pt_send) ||
488 ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl)))
489 revents |= ap->a_events & (POLLIN | POLLRDNORM);
491 if (ap->a_events & (POLLOUT | POLLWRNORM))
492 if (tp->t_state & TS_ISOPEN &&
493 ((pti->pt_flags & PF_REMOTE) ?
494 (tp->t_canq.c_cc == 0) :
495 ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG - 2) ||
496 (tp->t_canq.c_cc == 0 && (tp->t_lflag & ICANON)))))
497 revents |= ap->a_events & (POLLOUT | POLLWRNORM);
499 if (ap->a_events & POLLHUP)
500 if ((tp->t_state & TS_CARR_ON) == 0)
504 if (ap->a_events & (POLLIN | POLLRDNORM))
505 selrecord(curthread, &pti->pt_selr);
507 if (ap->a_events & (POLLOUT | POLLWRNORM))
508 selrecord(curthread, &pti->pt_selw);
512 ap->a_events = revents;
517 ptcwrite(struct dev_write_args *ap)
519 cdev_t dev = ap->a_head.a_dev;
520 struct tty *tp = dev->si_tty;
523 u_char locbuf[BUFSIZ];
525 struct pt_ioctl *pti = dev->si_drv1;
529 if ((tp->t_state&TS_ISOPEN) == 0)
531 if (pti->pt_flags & PF_REMOTE) {
534 while ((ap->a_uio->uio_resid > 0 || cc > 0) &&
535 tp->t_canq.c_cc < TTYHOG - 1) {
537 cc = min(ap->a_uio->uio_resid, BUFSIZ);
538 cc = min(cc, TTYHOG - 1 - tp->t_canq.c_cc);
540 error = uiomove((caddr_t)cp, cc, ap->a_uio);
543 /* check again for safety */
544 if ((tp->t_state & TS_ISOPEN) == 0) {
545 /* adjust as usual */
546 ap->a_uio->uio_resid += cc;
551 cc = b_to_q((char *)cp, cc, &tp->t_canq);
553 * XXX we don't guarantee that the canq size
554 * is >= TTYHOG, so the above b_to_q() may
555 * leave some bytes uncopied. However, space
556 * is guaranteed for the null terminator if
557 * we don't fail here since (TTYHOG - 1) is
558 * not a multiple of CBSIZE.
564 /* adjust for data copied in but not written */
565 ap->a_uio->uio_resid += cc;
566 clist_putc(0, &tp->t_canq);
568 wakeup(TSA_PTS_READ(tp));
571 while (ap->a_uio->uio_resid > 0 || cc > 0) {
573 cc = min(ap->a_uio->uio_resid, BUFSIZ);
575 error = uiomove((caddr_t)cp, cc, ap->a_uio);
578 /* check again for safety */
579 if ((tp->t_state & TS_ISOPEN) == 0) {
580 /* adjust for data copied in but not written */
581 ap->a_uio->uio_resid += cc;
586 if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 &&
587 (tp->t_canq.c_cc > 0 || !(tp->t_lflag&ICANON))) {
588 wakeup(TSA_HUP_OR_INPUT(tp));
591 (*linesw[tp->t_line].l_rint)(*cp++, tp);
600 * Come here to wait for slave to open, for space
601 * in outq, or space in rawq, or an empty canq.
603 if ((tp->t_state & TS_CONNECTED) == 0) {
604 /* adjust for data copied in but not written */
605 ap->a_uio->uio_resid += cc;
608 if (ap->a_ioflag & IO_NDELAY) {
609 /* adjust for data copied in but not written */
610 ap->a_uio->uio_resid += cc;
612 return (EWOULDBLOCK);
615 error = tsleep(TSA_PTC_WRITE(tp), PCATCH, "ptcout", 0);
617 /* adjust for data copied in but not written */
618 ap->a_uio->uio_resid += cc;
626 ptyioctl(struct dev_ioctl_args *ap)
628 cdev_t dev = ap->a_head.a_dev;
629 struct tty *tp = dev->si_tty;
630 struct pt_ioctl *pti = dev->si_drv1;
631 u_char *cc = tp->t_cc;
634 if (dev_dflags(dev) & D_MASTER) {
639 * We avoid calling ttioctl on the controller since,
640 * in that case, tp must be the controlling terminal.
642 *(int *)ap->a_data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
646 if (*(int *)ap->a_data) {
647 if (pti->pt_flags & PF_UCNTL)
649 pti->pt_flags |= PF_PKT;
651 pti->pt_flags &= ~PF_PKT;
655 if (*(int *)ap->a_data) {
656 if (pti->pt_flags & PF_PKT)
658 pti->pt_flags |= PF_UCNTL;
660 pti->pt_flags &= ~PF_UCNTL;
664 if (*(int *)ap->a_data)
665 pti->pt_flags |= PF_REMOTE;
667 pti->pt_flags &= ~PF_REMOTE;
668 ttyflush(tp, FREAD|FWRITE);
673 * The rest of the ioctls shouldn't be called until
676 if ((tp->t_state & TS_ISOPEN) == 0)
689 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
690 * ttywflush(tp) will hang if there are characters in
693 ndflush(&tp->t_outq, tp->t_outq.c_cc);
697 if (*(unsigned int *)ap->a_data >= NSIG ||
698 *(unsigned int *)ap->a_data == 0)
700 if ((tp->t_lflag&NOFLSH) == 0)
701 ttyflush(tp, FREAD|FWRITE);
702 pgsignal(tp->t_pgrp, *(unsigned int *)ap->a_data, 1);
703 if ((*(unsigned int *)ap->a_data == SIGINFO) &&
704 ((tp->t_lflag&NOKERNINFO) == 0))
709 if (ap->a_cmd == TIOCEXT) {
711 * When the EXTPROC bit is being toggled, we need
712 * to send an TIOCPKT_IOCTL if the packet driver
715 if (*(int *)ap->a_data) {
716 if (pti->pt_flags & PF_PKT) {
717 pti->pt_send |= TIOCPKT_IOCTL;
718 ptcwakeup(tp, FREAD);
720 tp->t_lflag |= EXTPROC;
722 if ((tp->t_lflag & EXTPROC) &&
723 (pti->pt_flags & PF_PKT)) {
724 pti->pt_send |= TIOCPKT_IOCTL;
725 ptcwakeup(tp, FREAD);
727 tp->t_lflag &= ~EXTPROC;
731 error = (*linesw[tp->t_line].l_ioctl)(tp, ap->a_cmd, ap->a_data,
732 ap->a_fflag, ap->a_cred);
733 if (error == ENOIOCTL)
734 error = ttioctl(tp, ap->a_cmd, ap->a_data, ap->a_fflag);
735 if (error == ENOIOCTL) {
736 if (pti->pt_flags & PF_UCNTL &&
737 (ap->a_cmd & ~0xff) == UIOCCMD(0)) {
738 if (ap->a_cmd & 0xff) {
739 pti->pt_ucntl = (u_char)ap->a_cmd;
740 ptcwakeup(tp, FREAD);
747 * If external processing and packet mode send ioctl packet.
749 if ((tp->t_lflag&EXTPROC) && (pti->pt_flags & PF_PKT)) {
758 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
765 pti->pt_send |= TIOCPKT_IOCTL;
766 ptcwakeup(tp, FREAD);
771 stop = (tp->t_iflag & IXON) && CCEQ(cc[VSTOP], CTRL('s'))
772 && CCEQ(cc[VSTART], CTRL('q'));
773 if (pti->pt_flags & PF_NOSTOP) {
775 pti->pt_send &= ~TIOCPKT_NOSTOP;
776 pti->pt_send |= TIOCPKT_DOSTOP;
777 pti->pt_flags &= ~PF_NOSTOP;
778 ptcwakeup(tp, FREAD);
782 pti->pt_send &= ~TIOCPKT_DOSTOP;
783 pti->pt_send |= TIOCPKT_NOSTOP;
784 pti->pt_flags |= PF_NOSTOP;
785 ptcwakeup(tp, FREAD);
792 static void ptc_drvinit (void *unused);
795 ptc_drvinit(void *unused)
797 dev_ops_add(&pts_ops, 0, 0);
798 dev_ops_add(&ptc_ops, 0, 0);
799 /* XXX: Gross hack for DEVFS */
800 /* XXX: DEVFS is no more, should this be removed? */
804 SYSINIT(ptcdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR_C,ptc_drvinit,NULL)