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>
53 #include <sys/fcntl.h>
55 #include <sys/kernel.h>
57 #include <sys/signalvar.h>
58 #include <sys/malloc.h>
60 static MALLOC_DEFINE(M_PTY, "ptys", "pty data structures");
62 static void ptsstart(struct tty *tp);
63 static void ptsstop(struct tty *tp, int rw);
64 static void ptcwakeup(struct tty *tp, int flag);
65 static struct cdev *ptyinit(struct cdev *cdev, struct thread *td);
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 ptsioctl;
72 static d_open_t ptcopen;
73 static d_close_t ptcclose;
74 static d_read_t ptcread;
75 static d_ioctl_t ptcioctl;
76 static d_write_t ptcwrite;
77 static d_poll_t ptcpoll;
79 static struct cdevsw pts_cdevsw = {
80 .d_version = D_VERSION,
87 .d_flags = D_TTY | D_NEEDGIANT,
90 static struct cdevsw ptc_cdevsw = {
91 .d_version = D_VERSION,
99 .d_flags = D_TTY | D_NEEDGIANT,
102 #define BUFSIZ 100 /* Chunk size iomoved to/from user */
106 struct selinfo pt_selr, pt_selw;
110 struct cdev *devs, *devc;
111 int pt_devs_open, pt_devc_open;
112 struct prison *pt_prison;
115 #define PF_PKT 0x08 /* packet mode */
116 #define PF_STOPPED 0x10 /* user told stopped */
117 #define PF_NOSTOP 0x40
118 #define PF_UCNTL 0x80 /* user control mode */
120 #define TSA_PTC_READ(tp) ((void *)&(tp)->t_outq.c_cf)
121 #define TSA_PTC_WRITE(tp) ((void *)&(tp)->t_rawq.c_cl)
122 #define TSA_PTS_READ(tp) ((void *)&(tp)->t_canq)
124 static char *names = "pqrsPQRS";
126 * This function creates and initializes a pts/ptc pair
128 * pts == /dev/tty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
129 * ptc == /dev/pty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
131 * XXX: define and add mapping of upper minor bits to allow more
135 ptyinit(struct cdev *devc, struct thread *td)
141 /* For now we only map the lower 8 bits of the minor */
145 devc->si_flags &= ~SI_CHEAPCLONE;
148 * Initially do not create a slave endpoint.
150 pt = malloc(sizeof(*pt), M_PTY, M_WAITOK | M_ZERO);
153 pt->pt_tty = ttyalloc();
154 pt->pt_tty->t_sc = pt;
156 devc->si_tty = pt->pt_tty;
161 pty_create_slave(struct ucred *cred, struct ptsc *pt, int n)
164 pt->devs = make_dev_cred(&pts_cdevsw, n, cred, UID_ROOT, GID_WHEEL,
165 0666, "tty%c%r", names[n / 32], n % 32);
166 pt->devs->si_drv1 = pt;
167 pt->devs->si_tty = pt->pt_tty;
168 pt->pt_tty->t_dev = pt->devs;
172 pty_destroy_slave(struct ptsc *pt)
175 if (pt->pt_tty->t_refcnt > 1)
177 pt->pt_tty->t_dev = NULL;
180 destroy_dev(pt->devs);
185 pty_maybe_destroy_slave(struct ptsc *pt)
189 * vfs bugs and complications near revoke() make
190 * it currently impossible to destroy struct cdev
192 if (0 && pt->pt_devc_open == 0 && pt->pt_devs_open == 0)
193 pty_destroy_slave(pt);
198 ptsopen(struct cdev *dev, int flag, int devtype, struct thread *td)
209 if ((tp->t_state & TS_ISOPEN) == 0) {
210 ttyinitmode(tp, 1, 0);
211 } else if (tp->t_state & TS_XCLUDE && priv_check(td,
214 else if (pt->pt_prison != td->td_ucred->cr_prison &&
215 priv_check(td, PRIV_TTY_PRISON))
217 if (tp->t_oproc) /* Ctrlr still around. */
218 (void)ttyld_modem(tp, 1);
219 while ((tp->t_state & TS_CARR_ON) == 0) {
222 error = ttysleep(tp, TSA_CARR_ON(tp), TTIPRI | PCATCH,
227 error = ttyld_open(tp, dev);
229 ptcwakeup(tp, FREAD|FWRITE);
230 pt->pt_devs_open = 1;
232 pty_maybe_destroy_slave(pt);
237 ptsclose(struct cdev *dev, int flag, int mode, struct thread *td)
246 KASSERT(dev == pti->devs, ("ptsclose: dev != pti->devs"));
248 err = ttyld_close(tp, flag);
249 (void) tty_close(tp);
251 pti->pt_devs_open = 0;
252 pty_maybe_destroy_slave(pti);
258 ptsread(struct cdev *dev, struct uio *uio, int flag)
260 struct tty *tp = dev->si_tty;
264 error = ttyld_read(tp, uio, flag);
265 ptcwakeup(tp, FWRITE);
270 * Write to pseudo-tty.
271 * Wakeups of controlling tty will happen
272 * indirectly, when tty driver calls ptsstart.
275 ptswrite(struct cdev *dev, struct uio *uio, int flag)
280 if (tp->t_oproc == 0)
282 return (ttyld_write(tp, uio, flag));
286 * Start output on pseudo-tty.
287 * Wake up process selecting or sleeping for input from controlling tty.
290 ptsstart(struct tty *tp)
292 struct ptsc *pt = tp->t_sc;
294 if (tp->t_state & TS_TTSTOP)
296 if (pt->pt_flags & PF_STOPPED) {
297 pt->pt_flags &= ~PF_STOPPED;
298 pt->pt_send = TIOCPKT_START;
300 ptcwakeup(tp, FREAD);
304 ptcwakeup(struct tty *tp, int flag)
306 struct ptsc *pt = tp->t_sc;
309 selwakeuppri(&pt->pt_selr, TTIPRI);
310 wakeup(TSA_PTC_READ(tp));
313 selwakeuppri(&pt->pt_selw, TTOPRI);
314 wakeup(TSA_PTC_WRITE(tp));
319 ptcopen(struct cdev *dev, int flag, int devtype, struct thread *td)
331 * In case we have destroyed the struct tty at the last connect time,
332 * we need to recreate it.
334 if (pt->pt_tty == NULL) {
335 pt->pt_tty = ttyalloc();
336 pt->pt_tty->t_sc = pt;
337 dev->si_tty = pt->pt_tty;
344 tp->t_oproc = ptsstart;
345 tp->t_stop = ptsstop;
346 (void)ttyld_modem(tp, 1);
347 tp->t_lflag &= ~EXTPROC;
348 pt->pt_prison = td->td_ucred->cr_prison;
354 pty_create_slave(td->td_ucred, pt, minor(dev));
355 pt->pt_devc_open = 1;
361 ptcclose(struct cdev *dev, int flags, int fmt, struct thread *td)
363 struct ptsc *pti = dev->si_drv1;
367 (void)ttyld_modem(tp, 0);
370 * XXX MDMBUF makes no sense for ptys but would inhibit the above
371 * l_modem(). CLOCAL makes sense but isn't supported. Special
372 * l_modem()s that ignore carrier drop make no sense for ptys but
373 * may be in use because other parts of the line discipline make
374 * sense for ptys. Recover by doing everything that a normal
375 * ttymodem() would have done except for sending a SIGHUP.
377 if (tp->t_state & TS_ISOPEN) {
378 tp->t_state &= ~(TS_CARR_ON | TS_CONNECTED);
379 tp->t_state |= TS_ZOMBIE;
380 ttyflush(tp, FREAD | FWRITE);
383 tp->t_oproc = 0; /* mark closed */
384 pti->pt_devc_open = 0;
385 pty_maybe_destroy_slave(pti);
390 ptcread(struct cdev *dev, struct uio *uio, int flag)
392 struct tty *tp = dev->si_tty;
393 struct ptsc *pt = dev->si_drv1;
398 * We want to block until the slave
399 * is open, and there's something to read;
400 * but if we lost the slave or we're NBIO,
401 * then return the appropriate error instead.
404 if (tp->t_state&TS_ISOPEN) {
405 if (pt->pt_flags&PF_PKT && pt->pt_send) {
406 error = ureadc((int)pt->pt_send, uio);
409 if (pt->pt_send & TIOCPKT_IOCTL) {
410 cc = min(uio->uio_resid,
411 sizeof(tp->t_termios));
412 uiomove(&tp->t_termios, cc, uio);
417 if (pt->pt_flags&PF_UCNTL && pt->pt_ucntl) {
418 error = ureadc((int)pt->pt_ucntl, uio);
424 if (tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0)
427 if ((tp->t_state & TS_CONNECTED) == 0)
428 return (0); /* EOF */
429 if (flag & O_NONBLOCK)
430 return (EWOULDBLOCK);
431 error = tsleep(TSA_PTC_READ(tp), TTIPRI | PCATCH, "ptcin", 0);
435 if (pt->pt_flags & (PF_PKT|PF_UCNTL))
436 error = ureadc(0, uio);
437 while (uio->uio_resid > 0 && error == 0) {
438 cc = q_to_b(&tp->t_outq, buf, min(uio->uio_resid, BUFSIZ));
441 error = uiomove(buf, cc, uio);
448 ptsstop(struct tty *tp, int flush)
450 struct ptsc *pt = tp->t_sc;
453 /* note: FLUSHREAD and FLUSHWRITE already ok */
455 flush = TIOCPKT_STOP;
456 pt->pt_flags |= PF_STOPPED;
458 pt->pt_flags &= ~PF_STOPPED;
459 pt->pt_send |= flush;
460 /* change of perspective */
470 ptcpoll(struct cdev *dev, int events, struct thread *td)
472 struct tty *tp = dev->si_tty;
473 struct ptsc *pt = dev->si_drv1;
477 if ((tp->t_state & TS_CONNECTED) == 0)
479 (POLLHUP | POLLIN | POLLRDNORM | POLLOUT | POLLWRNORM));
482 * Need to block timeouts (ttrstart).
486 if (events & (POLLIN | POLLRDNORM))
487 if ((tp->t_state & TS_ISOPEN) &&
488 ((tp->t_outq.c_cc && (tp->t_state & TS_TTSTOP) == 0) ||
489 ((pt->pt_flags & PF_PKT) && pt->pt_send) ||
490 ((pt->pt_flags & PF_UCNTL) && pt->pt_ucntl)))
491 revents |= events & (POLLIN | POLLRDNORM);
493 if (events & (POLLOUT | POLLWRNORM))
494 if (tp->t_state & TS_ISOPEN &&
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 |= events & (POLLOUT | POLLWRNORM);
499 if (events & POLLHUP)
500 if ((tp->t_state & TS_CARR_ON) == 0)
504 if (events & (POLLIN | POLLRDNORM))
505 selrecord(td, &pt->pt_selr);
507 if (events & (POLLOUT | POLLWRNORM))
508 selrecord(td, &pt->pt_selw);
516 ptcwrite(struct cdev *dev, struct uio *uio, int flag)
518 struct tty *tp = dev->si_tty;
521 u_char locbuf[BUFSIZ];
526 if ((tp->t_state&TS_ISOPEN) == 0)
528 while (uio->uio_resid > 0 || cc > 0) {
530 cc = min(uio->uio_resid, BUFSIZ);
532 error = uiomove(cp, cc, uio);
535 /* check again for safety */
536 if ((tp->t_state & TS_ISOPEN) == 0) {
537 /* adjust for data copied in but not written */
538 uio->uio_resid += cc;
543 if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 &&
544 (tp->t_canq.c_cc > 0 || !(tp->t_lflag&ICANON))) {
545 wakeup(TSA_HUP_OR_INPUT(tp));
548 ttyld_rint(tp, *cp++);
557 * Come here to wait for slave to open, for space
558 * in outq, or space in rawq, or an empty canq.
560 if ((tp->t_state & TS_CONNECTED) == 0) {
561 /* adjust for data copied in but not written */
562 uio->uio_resid += cc;
565 if (flag & O_NONBLOCK) {
566 /* adjust for data copied in but not written */
567 uio->uio_resid += cc;
569 return (EWOULDBLOCK);
572 error = tsleep(TSA_PTC_WRITE(tp), TTOPRI | PCATCH, "ptcout", 0);
574 /* adjust for data copied in but not written */
575 uio->uio_resid += cc;
583 ptcioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
585 struct tty *tp = dev->si_tty;
586 struct ptsc *pt = dev->si_drv1;
587 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
588 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
596 * We avoid calling ttioctl on the controller since,
597 * in that case, tp must be the controlling terminal.
599 *(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
604 if (pt->pt_flags & PF_UCNTL)
606 pt->pt_flags |= PF_PKT;
608 pt->pt_flags &= ~PF_PKT;
613 if (pt->pt_flags & PF_PKT)
615 pt->pt_flags |= PF_UCNTL;
617 pt->pt_flags &= ~PF_UCNTL;
622 * The rest of the ioctls shouldn't be called until
625 if ((tp->t_state & TS_ISOPEN) == 0)
638 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
639 * ttywflush(tp) will hang if there are characters in
642 ndflush(&tp->t_outq, tp->t_outq.c_cc);
645 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
646 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
648 ival = IOCPARM_IVAL(data);
649 data = (caddr_t)&ival;
653 if (*(unsigned int *)data >= NSIG ||
654 *(unsigned int *)data == 0)
656 if ((tp->t_lflag&NOFLSH) == 0)
657 ttyflush(tp, FREAD|FWRITE);
658 if (tp->t_pgrp != NULL) {
659 PGRP_LOCK(tp->t_pgrp);
660 pgsignal(tp->t_pgrp, *(unsigned int *)data, 1);
661 PGRP_UNLOCK(tp->t_pgrp);
663 if ((*(unsigned int *)data == SIGINFO) &&
664 ((tp->t_lflag&NOKERNINFO) == 0))
669 return (ptsioctl(dev, cmd, data, flag, td));
674 ptsioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
676 struct tty *tp = dev->si_tty;
677 struct ptsc *pt = dev->si_drv1;
678 u_char *cc = tp->t_cc;
681 if (cmd == TIOCEXT) {
683 * When the EXTPROC bit is being toggled, we need
684 * to send an TIOCPKT_IOCTL if the packet driver
688 if (pt->pt_flags & PF_PKT) {
689 pt->pt_send |= TIOCPKT_IOCTL;
690 ptcwakeup(tp, FREAD);
692 tp->t_lflag |= EXTPROC;
694 if ((tp->t_lflag & EXTPROC) &&
695 (pt->pt_flags & PF_PKT)) {
696 pt->pt_send |= TIOCPKT_IOCTL;
697 ptcwakeup(tp, FREAD);
699 tp->t_lflag &= ~EXTPROC;
703 error = ttyioctl(dev, cmd, data, flag, td);
704 if (error == ENOTTY) {
705 if (pt->pt_flags & PF_UCNTL &&
706 (cmd & ~0xff) == UIOCCMD(0)) {
708 pt->pt_ucntl = (u_char)cmd;
709 ptcwakeup(tp, FREAD);
716 * If external processing and packet mode send ioctl packet.
718 if ((tp->t_lflag&EXTPROC) && (pt->pt_flags & PF_PKT)) {
732 pt->pt_send |= TIOCPKT_IOCTL;
733 ptcwakeup(tp, FREAD);
739 stop = (tp->t_iflag & IXON) && CCEQ(cc[VSTOP], CTRL('s'))
740 && CCEQ(cc[VSTART], CTRL('q'));
741 if (pt->pt_flags & PF_NOSTOP) {
743 pt->pt_send &= ~TIOCPKT_NOSTOP;
744 pt->pt_send |= TIOCPKT_DOSTOP;
745 pt->pt_flags &= ~PF_NOSTOP;
746 ptcwakeup(tp, FREAD);
750 pt->pt_send &= ~TIOCPKT_DOSTOP;
751 pt->pt_send |= TIOCPKT_NOSTOP;
752 pt->pt_flags |= PF_NOSTOP;
753 ptcwakeup(tp, FREAD);
760 pty_clone(void *arg, struct ucred *cr, char *name, int namelen,
767 if (bcmp(name, "pty", 3) != 0)
772 case 'p': u = 0; break;
773 case 'q': u = 32; break;
774 case 'r': u = 64; break;
775 case 's': u = 96; break;
776 case 'P': u = 128; break;
777 case 'Q': u = 160; break;
778 case 'R': u = 192; break;
779 case 'S': u = 224; break;
782 if (name[4] >= '0' && name[4] <= '9')
784 else if (name[4] >= 'a' && name[4] <= 'v')
785 u += name[4] - 'a' + 10;
788 *dev = make_dev_cred(&ptc_cdevsw, u, cr,
789 UID_ROOT, GID_WHEEL, 0666, "pty%c%r", names[u / 32], u % 32);
791 (*dev)->si_flags |= SI_CHEAPCLONE;
796 ptc_drvinit(void *unused)
799 EVENTHANDLER_REGISTER(dev_clone, pty_clone, 0, 1000);
802 SYSINIT(ptcdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE,ptc_drvinit,NULL)