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 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)tty_pty.c 8.4 (Berkeley) 2/20/95
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
36 * Pseudo-teletype Driver
37 * (Actually two drivers, requiring two entries in 'cdevsw')
39 #include "opt_compat.h"
41 #include <sys/param.h>
42 #include <sys/systm.h>
44 #include <sys/mutex.h>
46 #if defined(COMPAT_43TTY)
47 #include <sys/ioctl_compat.h>
52 #include <sys/fcntl.h>
54 #include <sys/kernel.h>
56 #include <sys/signalvar.h>
57 #include <sys/malloc.h>
59 static MALLOC_DEFINE(M_PTY, "ptys", "pty data structures");
61 static void ptsstart(struct tty *tp);
62 static void ptsstop(struct tty *tp, int rw);
63 static void ptcwakeup(struct tty *tp, int flag);
64 static struct cdev *ptyinit(struct cdev *cdev, struct thread *td);
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 ptsioctl;
71 static d_open_t ptcopen;
72 static d_close_t ptcclose;
73 static d_read_t ptcread;
74 static d_ioctl_t ptcioctl;
75 static d_write_t ptcwrite;
76 static d_poll_t ptcpoll;
78 static struct cdevsw pts_cdevsw = {
79 .d_version = D_VERSION,
86 .d_flags = D_TTY | D_NEEDGIANT,
89 static struct cdevsw ptc_cdevsw = {
90 .d_version = D_VERSION,
98 .d_flags = D_TTY | D_NEEDGIANT,
101 #define BUFSIZ 100 /* Chunk size iomoved to/from user */
105 struct selinfo pt_selr, pt_selw;
109 struct cdev *devs, *devc;
110 int pt_devs_open, pt_devc_open;
111 struct prison *pt_prison;
114 #define PF_PKT 0x08 /* packet mode */
115 #define PF_STOPPED 0x10 /* user told stopped */
116 #define PF_NOSTOP 0x40
117 #define PF_UCNTL 0x80 /* user control mode */
119 #define TSA_PTC_READ(tp) ((void *)&(tp)->t_outq.c_cf)
120 #define TSA_PTC_WRITE(tp) ((void *)&(tp)->t_rawq.c_cl)
121 #define TSA_PTS_READ(tp) ((void *)&(tp)->t_canq)
123 static char *names = "pqrsPQRS";
125 * This function creates and initializes a pts/ptc pair
127 * pts == /dev/tty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
128 * ptc == /dev/pty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
130 * XXX: define and add mapping of upper minor bits to allow more
134 ptyinit(struct cdev *devc, struct thread *td)
140 /* For now we only map the lower 8 bits of the minor */
144 devc->si_flags &= ~SI_CHEAPCLONE;
147 * Initially do not create a slave endpoint.
149 pt = malloc(sizeof(*pt), M_PTY, M_WAITOK | M_ZERO);
152 pt->pt_tty = ttyalloc();
153 pt->pt_tty->t_sc = pt;
155 devc->si_tty = pt->pt_tty;
160 pty_create_slave(struct ucred *cred, struct ptsc *pt, int n)
163 pt->devs = make_dev_cred(&pts_cdevsw, n, cred, UID_ROOT, GID_WHEEL,
164 0666, "tty%c%r", names[n / 32], n % 32);
165 pt->devs->si_drv1 = pt;
166 pt->devs->si_tty = pt->pt_tty;
167 pt->pt_tty->t_dev = pt->devs;
171 pty_destroy_slave(struct ptsc *pt)
174 pt->pt_tty->t_dev = NULL;
175 destroy_dev(pt->devs);
180 pty_maybe_destroy_slave(struct ptsc *pt)
183 if (pt->pt_devc_open == 0 && pt->pt_devs_open == 0)
184 pty_destroy_slave(pt);
189 ptsopen(struct cdev *dev, int flag, int devtype, struct thread *td)
199 if ((tp->t_state & TS_ISOPEN) == 0) {
200 ttyinitmode(tp, 1, 0);
201 } else if (tp->t_state & TS_XCLUDE && suser(td))
203 else if (pt->pt_prison != td->td_ucred->cr_prison && suser(td))
205 if (tp->t_oproc) /* Ctrlr still around. */
206 (void)ttyld_modem(tp, 1);
207 while ((tp->t_state & TS_CARR_ON) == 0) {
210 error = ttysleep(tp, TSA_CARR_ON(tp), TTIPRI | PCATCH,
215 error = ttyld_open(tp, dev);
217 ptcwakeup(tp, FREAD|FWRITE);
218 pt->pt_devs_open = 1;
220 pty_maybe_destroy_slave(pt);
225 ptsclose(struct cdev *dev, int flag, int mode, struct thread *td)
234 KASSERT(dev == pti->devs, ("ptsclose: dev != pti->devs"));
236 err = ttyld_close(tp, flag);
237 (void) tty_close(tp);
239 pti->pt_devs_open = 0;
240 pty_maybe_destroy_slave(pti);
246 ptsread(struct cdev *dev, struct uio *uio, int flag)
248 struct tty *tp = dev->si_tty;
252 error = ttyld_read(tp, uio, flag);
253 ptcwakeup(tp, FWRITE);
258 * Write to pseudo-tty.
259 * Wakeups of controlling tty will happen
260 * indirectly, when tty driver calls ptsstart.
263 ptswrite(struct cdev *dev, struct uio *uio, int flag)
268 if (tp->t_oproc == 0)
270 return (ttyld_write(tp, uio, flag));
274 * Start output on pseudo-tty.
275 * Wake up process selecting or sleeping for input from controlling tty.
278 ptsstart(struct tty *tp)
280 struct ptsc *pt = tp->t_sc;
282 if (tp->t_state & TS_TTSTOP)
284 if (pt->pt_flags & PF_STOPPED) {
285 pt->pt_flags &= ~PF_STOPPED;
286 pt->pt_send = TIOCPKT_START;
288 ptcwakeup(tp, FREAD);
292 ptcwakeup(struct tty *tp, int flag)
294 struct ptsc *pt = tp->t_sc;
297 selwakeuppri(&pt->pt_selr, TTIPRI);
298 wakeup(TSA_PTC_READ(tp));
301 selwakeuppri(&pt->pt_selw, TTOPRI);
302 wakeup(TSA_PTC_WRITE(tp));
307 ptcopen(struct cdev *dev, int flag, int devtype, struct thread *td)
320 tp->t_oproc = ptsstart;
321 tp->t_stop = ptsstop;
322 (void)ttyld_modem(tp, 1);
323 tp->t_lflag &= ~EXTPROC;
325 pt->pt_prison = td->td_ucred->cr_prison;
331 pty_create_slave(td->td_ucred, pt, minor(dev));
332 pt->pt_devc_open = 1;
338 ptcclose(struct cdev *dev, int flags, int fmt, struct thread *td)
340 struct ptsc *pti = dev->si_drv1;
344 (void)ttyld_modem(tp, 0);
347 * XXX MDMBUF makes no sense for ptys but would inhibit the above
348 * l_modem(). CLOCAL makes sense but isn't supported. Special
349 * l_modem()s that ignore carrier drop make no sense for ptys but
350 * may be in use because other parts of the line discipline make
351 * sense for ptys. Recover by doing everything that a normal
352 * ttymodem() would have done except for sending a SIGHUP.
354 if (tp->t_state & TS_ISOPEN) {
355 tp->t_state &= ~(TS_CARR_ON | TS_CONNECTED);
356 tp->t_state |= TS_ZOMBIE;
357 ttyflush(tp, FREAD | FWRITE);
360 tp->t_oproc = 0; /* mark closed */
361 pti->pt_devc_open = 0;
362 pty_maybe_destroy_slave(pti);
367 ptcread(struct cdev *dev, struct uio *uio, int flag)
369 struct tty *tp = dev->si_tty;
370 struct ptsc *pt = dev->si_drv1;
375 * We want to block until the slave
376 * is open, and there's something to read;
377 * but if we lost the slave or we're NBIO,
378 * then return the appropriate error instead.
381 if (tp->t_state&TS_ISOPEN) {
382 if (pt->pt_flags&PF_PKT && pt->pt_send) {
383 error = ureadc((int)pt->pt_send, uio);
386 if (pt->pt_send & TIOCPKT_IOCTL) {
387 cc = min(uio->uio_resid,
388 sizeof(tp->t_termios));
389 uiomove(&tp->t_termios, cc, uio);
394 if (pt->pt_flags&PF_UCNTL && pt->pt_ucntl) {
395 error = ureadc((int)pt->pt_ucntl, uio);
401 if (tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0)
404 if ((tp->t_state & TS_CONNECTED) == 0)
405 return (0); /* EOF */
406 if (flag & O_NONBLOCK)
407 return (EWOULDBLOCK);
408 error = tsleep(TSA_PTC_READ(tp), TTIPRI | PCATCH, "ptcin", 0);
412 if (pt->pt_flags & (PF_PKT|PF_UCNTL))
413 error = ureadc(0, uio);
414 while (uio->uio_resid > 0 && error == 0) {
415 cc = q_to_b(&tp->t_outq, buf, min(uio->uio_resid, BUFSIZ));
418 error = uiomove(buf, cc, uio);
425 ptsstop(struct tty *tp, int flush)
427 struct ptsc *pt = tp->t_sc;
430 /* note: FLUSHREAD and FLUSHWRITE already ok */
432 flush = TIOCPKT_STOP;
433 pt->pt_flags |= PF_STOPPED;
435 pt->pt_flags &= ~PF_STOPPED;
436 pt->pt_send |= flush;
437 /* change of perspective */
447 ptcpoll(struct cdev *dev, int events, struct thread *td)
449 struct tty *tp = dev->si_tty;
450 struct ptsc *pt = dev->si_drv1;
454 if ((tp->t_state & TS_CONNECTED) == 0)
456 (POLLHUP | POLLIN | POLLRDNORM | POLLOUT | POLLWRNORM));
459 * Need to block timeouts (ttrstart).
463 if (events & (POLLIN | POLLRDNORM))
464 if ((tp->t_state & TS_ISOPEN) &&
465 ((tp->t_outq.c_cc && (tp->t_state & TS_TTSTOP) == 0) ||
466 ((pt->pt_flags & PF_PKT) && pt->pt_send) ||
467 ((pt->pt_flags & PF_UCNTL) && pt->pt_ucntl)))
468 revents |= events & (POLLIN | POLLRDNORM);
470 if (events & (POLLOUT | POLLWRNORM))
471 if (tp->t_state & TS_ISOPEN &&
472 (((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG - 2) ||
473 (tp->t_canq.c_cc == 0 && (tp->t_lflag & ICANON)))))
474 revents |= events & (POLLOUT | POLLWRNORM);
476 if (events & POLLHUP)
477 if ((tp->t_state & TS_CARR_ON) == 0)
481 if (events & (POLLIN | POLLRDNORM))
482 selrecord(td, &pt->pt_selr);
484 if (events & (POLLOUT | POLLWRNORM))
485 selrecord(td, &pt->pt_selw);
493 ptcwrite(struct cdev *dev, struct uio *uio, int flag)
495 struct tty *tp = dev->si_tty;
498 u_char locbuf[BUFSIZ];
503 if ((tp->t_state&TS_ISOPEN) == 0)
505 while (uio->uio_resid > 0 || cc > 0) {
507 cc = min(uio->uio_resid, BUFSIZ);
509 error = uiomove(cp, cc, uio);
512 /* check again for safety */
513 if ((tp->t_state & TS_ISOPEN) == 0) {
514 /* adjust for data copied in but not written */
515 uio->uio_resid += cc;
520 if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 &&
521 (tp->t_canq.c_cc > 0 || !(tp->t_lflag&ICANON))) {
522 wakeup(TSA_HUP_OR_INPUT(tp));
525 ttyld_rint(tp, *cp++);
534 * Come here to wait for slave to open, for space
535 * in outq, or space in rawq, or an empty canq.
537 if ((tp->t_state & TS_CONNECTED) == 0) {
538 /* adjust for data copied in but not written */
539 uio->uio_resid += cc;
542 if (flag & O_NONBLOCK) {
543 /* adjust for data copied in but not written */
544 uio->uio_resid += cc;
546 return (EWOULDBLOCK);
549 error = tsleep(TSA_PTC_WRITE(tp), TTOPRI | PCATCH, "ptcout", 0);
551 /* adjust for data copied in but not written */
552 uio->uio_resid += cc;
560 ptcioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
562 struct tty *tp = dev->si_tty;
563 struct ptsc *pt = dev->si_drv1;
569 * We avoid calling ttioctl on the controller since,
570 * in that case, tp must be the controlling terminal.
572 *(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
577 if (pt->pt_flags & PF_UCNTL)
579 pt->pt_flags |= PF_PKT;
581 pt->pt_flags &= ~PF_PKT;
586 if (pt->pt_flags & PF_PKT)
588 pt->pt_flags |= PF_UCNTL;
590 pt->pt_flags &= ~PF_UCNTL;
595 * The rest of the ioctls shouldn't be called until
598 if ((tp->t_state & TS_ISOPEN) == 0)
611 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
612 * ttywflush(tp) will hang if there are characters in
615 ndflush(&tp->t_outq, tp->t_outq.c_cc);
619 if (*(unsigned int *)data >= NSIG ||
620 *(unsigned int *)data == 0)
622 if ((tp->t_lflag&NOFLSH) == 0)
623 ttyflush(tp, FREAD|FWRITE);
624 if (tp->t_pgrp != NULL) {
625 PGRP_LOCK(tp->t_pgrp);
626 pgsignal(tp->t_pgrp, *(unsigned int *)data, 1);
627 PGRP_UNLOCK(tp->t_pgrp);
629 if ((*(unsigned int *)data == SIGINFO) &&
630 ((tp->t_lflag&NOKERNINFO) == 0))
635 return (ptsioctl(dev, cmd, data, flag, td));
640 ptsioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
642 struct tty *tp = dev->si_tty;
643 struct ptsc *pt = dev->si_drv1;
644 u_char *cc = tp->t_cc;
647 if (cmd == TIOCEXT) {
649 * When the EXTPROC bit is being toggled, we need
650 * to send an TIOCPKT_IOCTL if the packet driver
654 if (pt->pt_flags & PF_PKT) {
655 pt->pt_send |= TIOCPKT_IOCTL;
656 ptcwakeup(tp, FREAD);
658 tp->t_lflag |= EXTPROC;
660 if ((tp->t_lflag & EXTPROC) &&
661 (pt->pt_flags & PF_PKT)) {
662 pt->pt_send |= TIOCPKT_IOCTL;
663 ptcwakeup(tp, FREAD);
665 tp->t_lflag &= ~EXTPROC;
669 error = ttyioctl(dev, cmd, data, flag, td);
670 if (error == ENOTTY) {
671 if (pt->pt_flags & PF_UCNTL &&
672 (cmd & ~0xff) == UIOCCMD(0)) {
674 pt->pt_ucntl = (u_char)cmd;
675 ptcwakeup(tp, FREAD);
682 * If external processing and packet mode send ioctl packet.
684 if ((tp->t_lflag&EXTPROC) && (pt->pt_flags & PF_PKT)) {
698 pt->pt_send |= TIOCPKT_IOCTL;
699 ptcwakeup(tp, FREAD);
705 stop = (tp->t_iflag & IXON) && CCEQ(cc[VSTOP], CTRL('s'))
706 && CCEQ(cc[VSTART], CTRL('q'));
707 if (pt->pt_flags & PF_NOSTOP) {
709 pt->pt_send &= ~TIOCPKT_NOSTOP;
710 pt->pt_send |= TIOCPKT_DOSTOP;
711 pt->pt_flags &= ~PF_NOSTOP;
712 ptcwakeup(tp, FREAD);
716 pt->pt_send &= ~TIOCPKT_DOSTOP;
717 pt->pt_send |= TIOCPKT_NOSTOP;
718 pt->pt_flags |= PF_NOSTOP;
719 ptcwakeup(tp, FREAD);
726 pty_clone(void *arg, struct ucred *cr, char *name, int namelen,
733 if (bcmp(name, "pty", 3) != 0)
738 case 'p': u = 0; break;
739 case 'q': u = 32; break;
740 case 'r': u = 64; break;
741 case 's': u = 96; break;
742 case 'P': u = 128; break;
743 case 'Q': u = 160; break;
744 case 'R': u = 192; break;
745 case 'S': u = 224; break;
748 if (name[4] >= '0' && name[4] <= '9')
750 else if (name[4] >= 'a' && name[4] <= 'v')
751 u += name[4] - 'a' + 10;
754 *dev = make_dev_cred(&ptc_cdevsw, u, cr,
755 UID_ROOT, GID_WHEEL, 0666, "pty%c%r", names[u / 32], u % 32);
757 (*dev)->si_flags |= SI_CHEAPCLONE;
762 ptc_drvinit(void *unused)
765 EVENTHANDLER_REGISTER(dev_clone, pty_clone, 0, 1000);
768 SYSINIT(ptcdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE,ptc_drvinit,NULL)