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>
43 #include <sys/libkern.h>
45 #include <sys/mutex.h>
47 #if defined(COMPAT_43TTY)
48 #include <sys/ioctl_compat.h>
54 #include <sys/fcntl.h>
56 #include <sys/kernel.h>
58 #include <sys/signalvar.h>
59 #include <sys/malloc.h>
61 static MALLOC_DEFINE(M_PTY, "ptys", "pty data structures");
63 static void ptsstart(struct tty *tp);
64 static void ptsstop(struct tty *tp, int rw);
65 static void ptcwakeup(struct tty *tp, int flag);
66 static struct cdev *ptyinit(struct cdev *cdev, struct thread *td);
68 static d_open_t ptsopen;
69 static d_close_t ptsclose;
70 static d_read_t ptsread;
71 static d_write_t ptswrite;
72 static d_ioctl_t ptsioctl;
73 static d_open_t ptcopen;
74 static d_close_t ptcclose;
75 static d_read_t ptcread;
76 static d_ioctl_t ptcioctl;
77 static d_write_t ptcwrite;
78 static d_poll_t ptcpoll;
80 static struct cdevsw pts_cdevsw = {
81 .d_version = D_VERSION,
88 .d_flags = D_TTY | D_NEEDGIANT,
91 static struct cdevsw ptc_cdevsw = {
92 .d_version = D_VERSION,
100 .d_flags = D_TTY | D_NEEDGIANT,
103 #define BUFSIZ 100 /* Chunk size iomoved to/from user */
107 struct selinfo pt_selr, pt_selw;
111 struct cdev *devs, *devc;
112 int pt_devs_open, pt_devc_open;
113 struct prison *pt_prison;
116 #define PF_PKT 0x08 /* packet mode */
117 #define PF_STOPPED 0x10 /* user told stopped */
118 #define PF_NOSTOP 0x40
119 #define PF_UCNTL 0x80 /* user control mode */
121 #define TSA_PTC_READ(tp) ((void *)&(tp)->t_outq.c_cf)
122 #define TSA_PTC_WRITE(tp) ((void *)&(tp)->t_rawq.c_cl)
123 #define TSA_PTS_READ(tp) ((void *)&(tp)->t_canq)
125 static const char names[] = "pqrsPQRSlmnoLMNO";
127 * This function creates and initializes a pts/ptc pair
129 * pts == /dev/tty[pqrsPQRSlmnoLMNO][0123456789abcdefghijklmnopqrstuv]
130 * ptc == /dev/pty[pqrsPQRSlmnoLMNO][0123456789abcdefghijklmnopqrstuv]
133 ptyinit(struct cdev *devc, struct thread *td)
138 n = minor2unit(minor(devc));
140 /* We only allow for up to 32 ptys per char in "names". */
141 if (n >= 32 * (sizeof(names) - 1))
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 m)
165 KASSERT(n >= 0 && n / 32 < sizeof(names),
166 ("pty_create_slave: n %d ptsc %p", n, pt));
167 pt->devs = make_dev_cred(&pts_cdevsw, m, cred, UID_ROOT, GID_WHEEL,
168 0666, "tty%c%r", names[n / 32], n % 32);
169 pt->devs->si_drv1 = pt;
170 pt->devs->si_tty = pt->pt_tty;
171 pt->pt_tty->t_dev = pt->devs;
175 pty_destroy_slave(struct ptsc *pt)
178 if (pt->pt_tty->t_refcnt > 1)
180 pt->pt_tty->t_dev = NULL;
183 destroy_dev(pt->devs);
188 pty_maybe_destroy_slave(struct ptsc *pt)
192 * vfs bugs and complications near revoke() make
193 * it currently impossible to destroy struct cdev
195 if (0 && pt->pt_devc_open == 0 && pt->pt_devs_open == 0)
196 pty_destroy_slave(pt);
201 ptsopen(struct cdev *dev, int flag, int devtype, struct thread *td)
212 if ((tp->t_state & TS_ISOPEN) == 0) {
213 ttyinitmode(tp, 1, 0);
214 } else if (tp->t_state & TS_XCLUDE && priv_check(td,
217 else if (pt->pt_prison != td->td_ucred->cr_prison &&
218 priv_check(td, PRIV_TTY_PRISON))
220 if (tp->t_oproc) /* Ctrlr still around. */
221 (void)ttyld_modem(tp, 1);
222 while ((tp->t_state & TS_CARR_ON) == 0) {
225 error = ttysleep(tp, TSA_CARR_ON(tp), TTIPRI | PCATCH,
230 error = ttyld_open(tp, dev);
232 ptcwakeup(tp, FREAD|FWRITE);
233 pt->pt_devs_open = 1;
235 pty_maybe_destroy_slave(pt);
240 ptsclose(struct cdev *dev, int flag, int mode, struct thread *td)
249 KASSERT(dev == pti->devs, ("ptsclose: dev != pti->devs"));
251 err = ttyld_close(tp, flag);
252 (void) tty_close(tp);
254 pti->pt_devs_open = 0;
255 pty_maybe_destroy_slave(pti);
261 ptsread(struct cdev *dev, struct uio *uio, int flag)
263 struct tty *tp = dev->si_tty;
267 error = ttyld_read(tp, uio, flag);
268 ptcwakeup(tp, FWRITE);
273 * Write to pseudo-tty.
274 * Wakeups of controlling tty will happen
275 * indirectly, when tty driver calls ptsstart.
278 ptswrite(struct cdev *dev, struct uio *uio, int flag)
283 if (tp->t_oproc == 0)
285 return (ttyld_write(tp, uio, flag));
289 * Start output on pseudo-tty.
290 * Wake up process selecting or sleeping for input from controlling tty.
293 ptsstart(struct tty *tp)
295 struct ptsc *pt = tp->t_sc;
297 if (tp->t_state & TS_TTSTOP)
299 if (pt->pt_flags & PF_STOPPED) {
300 pt->pt_flags &= ~PF_STOPPED;
301 pt->pt_send = TIOCPKT_START;
303 ptcwakeup(tp, FREAD);
307 ptcwakeup(struct tty *tp, int flag)
309 struct ptsc *pt = tp->t_sc;
312 selwakeuppri(&pt->pt_selr, TTIPRI);
313 wakeup(TSA_PTC_READ(tp));
316 selwakeuppri(&pt->pt_selw, TTOPRI);
317 wakeup(TSA_PTC_WRITE(tp));
322 ptcopen(struct cdev *dev, int flag, int devtype, struct thread *td)
334 * In case we have destroyed the struct tty at the last connect time,
335 * we need to recreate it.
337 if (pt->pt_tty == NULL) {
338 pt->pt_tty = ttyalloc();
339 pt->pt_tty->t_sc = pt;
340 dev->si_tty = pt->pt_tty;
347 tp->t_oproc = ptsstart;
348 tp->t_stop = ptsstop;
349 (void)ttyld_modem(tp, 1);
350 tp->t_lflag &= ~EXTPROC;
351 pt->pt_prison = td->td_ucred->cr_prison;
357 pty_create_slave(td->td_ucred, pt, minor(dev));
358 pt->pt_devc_open = 1;
364 ptcclose(struct cdev *dev, int flags, int fmt, struct thread *td)
366 struct ptsc *pti = dev->si_drv1;
370 (void)ttyld_modem(tp, 0);
373 * XXX MDMBUF makes no sense for ptys but would inhibit the above
374 * l_modem(). CLOCAL makes sense but isn't supported. Special
375 * l_modem()s that ignore carrier drop make no sense for ptys but
376 * may be in use because other parts of the line discipline make
377 * sense for ptys. Recover by doing everything that a normal
378 * ttymodem() would have done except for sending a SIGHUP.
380 if (tp->t_state & TS_ISOPEN) {
381 tp->t_state &= ~(TS_CARR_ON | TS_CONNECTED);
382 tp->t_state |= TS_ZOMBIE;
383 ttyflush(tp, FREAD | FWRITE);
386 tp->t_oproc = 0; /* mark closed */
387 pti->pt_devc_open = 0;
388 pty_maybe_destroy_slave(pti);
393 ptcread(struct cdev *dev, struct uio *uio, int flag)
395 struct tty *tp = dev->si_tty;
396 struct ptsc *pt = dev->si_drv1;
401 * We want to block until the slave
402 * is open, and there's something to read;
403 * but if we lost the slave or we're NBIO,
404 * then return the appropriate error instead.
407 if (tp->t_state&TS_ISOPEN) {
408 if (pt->pt_flags&PF_PKT && pt->pt_send) {
409 error = ureadc((int)pt->pt_send, uio);
412 if (pt->pt_send & TIOCPKT_IOCTL) {
413 cc = min(uio->uio_resid,
414 sizeof(tp->t_termios));
415 uiomove(&tp->t_termios, cc, uio);
420 if (pt->pt_flags&PF_UCNTL && pt->pt_ucntl) {
421 error = ureadc((int)pt->pt_ucntl, uio);
427 if (tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0)
430 if ((tp->t_state & TS_CONNECTED) == 0)
431 return (0); /* EOF */
432 if (flag & O_NONBLOCK)
433 return (EWOULDBLOCK);
434 error = tsleep(TSA_PTC_READ(tp), TTIPRI | PCATCH, "ptcin", 0);
438 if (pt->pt_flags & (PF_PKT|PF_UCNTL))
439 error = ureadc(0, uio);
440 while (uio->uio_resid > 0 && error == 0) {
441 cc = q_to_b(&tp->t_outq, buf, min(uio->uio_resid, BUFSIZ));
444 error = uiomove(buf, cc, uio);
451 ptsstop(struct tty *tp, int flush)
453 struct ptsc *pt = tp->t_sc;
456 /* note: FLUSHREAD and FLUSHWRITE already ok */
458 flush = TIOCPKT_STOP;
459 pt->pt_flags |= PF_STOPPED;
461 pt->pt_flags &= ~PF_STOPPED;
462 pt->pt_send |= flush;
463 /* change of perspective */
473 ptcpoll(struct cdev *dev, int events, struct thread *td)
475 struct tty *tp = dev->si_tty;
476 struct ptsc *pt = dev->si_drv1;
480 if ((tp->t_state & TS_CONNECTED) == 0)
482 (POLLHUP | POLLIN | POLLRDNORM | POLLOUT | POLLWRNORM));
485 * Need to block timeouts (ttrstart).
489 if (events & (POLLIN | POLLRDNORM))
490 if ((tp->t_state & TS_ISOPEN) &&
491 ((tp->t_outq.c_cc && (tp->t_state & TS_TTSTOP) == 0) ||
492 ((pt->pt_flags & PF_PKT) && pt->pt_send) ||
493 ((pt->pt_flags & PF_UCNTL) && pt->pt_ucntl)))
494 revents |= events & (POLLIN | POLLRDNORM);
496 if (events & (POLLOUT | POLLWRNORM))
497 if (tp->t_state & TS_ISOPEN &&
498 (((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG - 2) ||
499 (tp->t_canq.c_cc == 0 && (tp->t_lflag & ICANON)))))
500 revents |= events & (POLLOUT | POLLWRNORM);
502 if (events & POLLHUP)
503 if ((tp->t_state & TS_CARR_ON) == 0)
507 if (events & (POLLIN | POLLRDNORM))
508 selrecord(td, &pt->pt_selr);
510 if (events & (POLLOUT | POLLWRNORM))
511 selrecord(td, &pt->pt_selw);
519 ptcwrite(struct cdev *dev, struct uio *uio, int flag)
521 struct tty *tp = dev->si_tty;
524 u_char locbuf[BUFSIZ];
529 if ((tp->t_state&TS_ISOPEN) == 0)
531 while (uio->uio_resid > 0 || cc > 0) {
533 cc = min(uio->uio_resid, BUFSIZ);
535 error = uiomove(cp, cc, uio);
538 /* check again for safety */
539 if ((tp->t_state & TS_ISOPEN) == 0) {
540 /* adjust for data copied in but not written */
541 uio->uio_resid += cc;
546 if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 &&
547 (tp->t_canq.c_cc > 0 || !(tp->t_lflag&ICANON))) {
548 wakeup(TSA_HUP_OR_INPUT(tp));
551 ttyld_rint(tp, *cp++);
560 * Come here to wait for slave to open, for space
561 * in outq, or space in rawq, or an empty canq.
563 if ((tp->t_state & TS_CONNECTED) == 0) {
564 /* adjust for data copied in but not written */
565 uio->uio_resid += cc;
568 if (flag & O_NONBLOCK) {
569 /* adjust for data copied in but not written */
570 uio->uio_resid += cc;
572 return (EWOULDBLOCK);
575 error = tsleep(TSA_PTC_WRITE(tp), TTOPRI | PCATCH, "ptcout", 0);
577 /* adjust for data copied in but not written */
578 uio->uio_resid += cc;
586 ptcioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
588 struct tty *tp = dev->si_tty;
589 struct ptsc *pt = dev->si_drv1;
590 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
591 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
599 * We avoid calling ttioctl on the controller since,
600 * in that case, tp must be the controlling terminal.
602 *(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
607 if (pt->pt_flags & PF_UCNTL)
609 pt->pt_flags |= PF_PKT;
611 pt->pt_flags &= ~PF_PKT;
616 if (pt->pt_flags & PF_PKT)
618 pt->pt_flags |= PF_UCNTL;
620 pt->pt_flags &= ~PF_UCNTL;
625 * The rest of the ioctls shouldn't be called until
628 if ((tp->t_state & TS_ISOPEN) == 0)
641 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
642 * ttywflush(tp) will hang if there are characters in
645 ndflush(&tp->t_outq, tp->t_outq.c_cc);
648 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
649 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
651 ival = IOCPARM_IVAL(data);
652 data = (caddr_t)&ival;
656 if (*(unsigned int *)data >= NSIG ||
657 *(unsigned int *)data == 0)
659 if ((tp->t_lflag&NOFLSH) == 0)
660 ttyflush(tp, FREAD|FWRITE);
661 if (tp->t_pgrp != NULL) {
662 PGRP_LOCK(tp->t_pgrp);
663 pgsignal(tp->t_pgrp, *(unsigned int *)data, 1);
664 PGRP_UNLOCK(tp->t_pgrp);
666 if ((*(unsigned int *)data == SIGINFO) &&
667 ((tp->t_lflag&NOKERNINFO) == 0))
672 return (ptsioctl(dev, cmd, data, flag, td));
677 ptsioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
679 struct tty *tp = dev->si_tty;
680 struct ptsc *pt = dev->si_drv1;
681 u_char *cc = tp->t_cc;
684 if (cmd == TIOCEXT) {
686 * When the EXTPROC bit is being toggled, we need
687 * to send an TIOCPKT_IOCTL if the packet driver
691 if (pt->pt_flags & PF_PKT) {
692 pt->pt_send |= TIOCPKT_IOCTL;
693 ptcwakeup(tp, FREAD);
695 tp->t_lflag |= EXTPROC;
697 if ((tp->t_lflag & EXTPROC) &&
698 (pt->pt_flags & PF_PKT)) {
699 pt->pt_send |= TIOCPKT_IOCTL;
700 ptcwakeup(tp, FREAD);
702 tp->t_lflag &= ~EXTPROC;
706 error = ttyioctl(dev, cmd, data, flag, td);
707 if (error == ENOTTY) {
708 if (pt->pt_flags & PF_UCNTL &&
709 (cmd & ~0xff) == UIOCCMD(0)) {
711 pt->pt_ucntl = (u_char)cmd;
712 ptcwakeup(tp, FREAD);
719 * If external processing and packet mode send ioctl packet.
721 if ((tp->t_lflag&EXTPROC) && (pt->pt_flags & PF_PKT)) {
735 pt->pt_send |= TIOCPKT_IOCTL;
736 ptcwakeup(tp, FREAD);
742 stop = (tp->t_iflag & IXON) && CCEQ(cc[VSTOP], CTRL('s'))
743 && CCEQ(cc[VSTART], CTRL('q'));
744 if (pt->pt_flags & PF_NOSTOP) {
746 pt->pt_send &= ~TIOCPKT_NOSTOP;
747 pt->pt_send |= TIOCPKT_DOSTOP;
748 pt->pt_flags &= ~PF_NOSTOP;
749 ptcwakeup(tp, FREAD);
753 pt->pt_send &= ~TIOCPKT_DOSTOP;
754 pt->pt_send |= TIOCPKT_NOSTOP;
755 pt->pt_flags |= PF_NOSTOP;
756 ptcwakeup(tp, FREAD);
763 pty_clone(void *arg, struct ucred *cr, char *name, int namelen,
771 if (bcmp(name, "pty", 3) != 0)
773 if (name[5] != '\0' || name[3] == '\0')
775 cp = index(names, name[3]);
778 u = (cp - names) * 32;
779 if (name[4] >= '0' && name[4] <= '9')
781 else if (name[4] >= 'a' && name[4] <= 'v')
782 u += name[4] - 'a' + 10;
785 *dev = make_dev_credf(MAKEDEV_REF, &ptc_cdevsw, unit2minor(u), cr,
786 UID_ROOT, GID_WHEEL, 0666, "pty%c%r", names[u / 32], u % 32);
787 (*dev)->si_flags |= SI_CHEAPCLONE;
792 ptc_drvinit(void *unused)
795 EVENTHANDLER_REGISTER(dev_clone, pty_clone, 0, 1000);
798 SYSINIT(ptcdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE,ptc_drvinit,NULL)