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>
47 #if defined(COMPAT_43)
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 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)
142 /* For now we only map the lower 8 bits of the minor */
146 devc->si_flags &= ~SI_CHEAPCLONE;
148 pt = malloc(sizeof(*pt), M_PTY, M_WAITOK | M_ZERO);
149 pt->devs = devs = make_dev_cred(&pts_cdevsw, n, td->td_ucred,
150 UID_ROOT, GID_WHEEL, 0666, "tty%c%r", names[n / 32], n % 32);
153 pt->pt_tty = ttymalloc(pt->pt_tty);
154 pt->pt_tty->t_sc = pt;
155 devs->si_drv1 = devc->si_drv1 = pt;
156 devs->si_tty = devc->si_tty = pt->pt_tty;
157 pt->pt_tty->t_dev = devs;
163 ptsopen(struct cdev *dev, int flag, int devtype, struct thread *td)
173 if ((tp->t_state & TS_ISOPEN) == 0) {
174 ttyinitmode(tp, 1, 0);
175 } else if (tp->t_state & TS_XCLUDE && suser(td))
177 else if (pt->pt_prison != td->td_ucred->cr_prison)
179 if (tp->t_oproc) /* Ctrlr still around. */
180 (void)ttyld_modem(tp, 1);
181 while ((tp->t_state & TS_CARR_ON) == 0) {
184 error = ttysleep(tp, TSA_CARR_ON(tp), TTIPRI | PCATCH,
189 error = ttyld_open(tp, dev);
191 ptcwakeup(tp, FREAD|FWRITE);
196 ptsclose(struct cdev *dev, int flag, int mode, struct thread *td)
202 err = ttyld_close(tp, flag);
203 (void) tty_close(tp);
208 ptsread(struct cdev *dev, struct uio *uio, int flag)
210 struct tty *tp = dev->si_tty;
214 error = ttyld_read(tp, uio, flag);
215 ptcwakeup(tp, FWRITE);
220 * Write to pseudo-tty.
221 * Wakeups of controlling tty will happen
222 * indirectly, when tty driver calls ptsstart.
225 ptswrite(struct cdev *dev, struct uio *uio, int flag)
230 if (tp->t_oproc == 0)
232 return (ttyld_write(tp, uio, flag));
236 * Start output on pseudo-tty.
237 * Wake up process selecting or sleeping for input from controlling tty.
240 ptsstart(struct tty *tp)
242 struct ptsc *pt = tp->t_sc;
244 if (tp->t_state & TS_TTSTOP)
246 if (pt->pt_flags & PF_STOPPED) {
247 pt->pt_flags &= ~PF_STOPPED;
248 pt->pt_send = TIOCPKT_START;
250 ptcwakeup(tp, FREAD);
254 ptcwakeup(struct tty *tp, int flag)
256 struct ptsc *pt = tp->t_sc;
259 selwakeuppri(&pt->pt_selr, TTIPRI);
260 wakeup(TSA_PTC_READ(tp));
263 selwakeuppri(&pt->pt_selw, TTOPRI);
264 wakeup(TSA_PTC_WRITE(tp));
269 ptcopen(struct cdev *dev, int flag, int devtype, struct thread *td)
282 tp->t_oproc = ptsstart;
283 tp->t_stop = ptsstop;
284 (void)ttyld_modem(tp, 1);
285 tp->t_lflag &= ~EXTPROC;
287 pt->pt_prison = td->td_ucred->cr_prison;
295 ptcclose(struct cdev *dev, int flags, int fmt, struct thread *td)
300 (void)ttyld_modem(tp, 0);
303 * XXX MDMBUF makes no sense for ptys but would inhibit the above
304 * l_modem(). CLOCAL makes sense but isn't supported. Special
305 * l_modem()s that ignore carrier drop make no sense for ptys but
306 * may be in use because other parts of the line discipline make
307 * sense for ptys. Recover by doing everything that a normal
308 * ttymodem() would have done except for sending a SIGHUP.
310 if (tp->t_state & TS_ISOPEN) {
311 tp->t_state &= ~(TS_CARR_ON | TS_CONNECTED);
312 tp->t_state |= TS_ZOMBIE;
313 ttyflush(tp, FREAD | FWRITE);
316 tp->t_oproc = 0; /* mark closed */
321 ptcread(struct cdev *dev, struct uio *uio, int flag)
323 struct tty *tp = dev->si_tty;
324 struct ptsc *pt = dev->si_drv1;
329 * We want to block until the slave
330 * is open, and there's something to read;
331 * but if we lost the slave or we're NBIO,
332 * then return the appropriate error instead.
335 if (tp->t_state&TS_ISOPEN) {
336 if (pt->pt_flags&PF_PKT && pt->pt_send) {
337 error = ureadc((int)pt->pt_send, uio);
340 if (pt->pt_send & TIOCPKT_IOCTL) {
341 cc = min(uio->uio_resid,
342 sizeof(tp->t_termios));
343 uiomove(&tp->t_termios, cc, uio);
348 if (pt->pt_flags&PF_UCNTL && pt->pt_ucntl) {
349 error = ureadc((int)pt->pt_ucntl, uio);
355 if (tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0)
358 if ((tp->t_state & TS_CONNECTED) == 0)
359 return (0); /* EOF */
360 if (flag & O_NONBLOCK)
361 return (EWOULDBLOCK);
362 error = tsleep(TSA_PTC_READ(tp), TTIPRI | PCATCH, "ptcin", 0);
366 if (pt->pt_flags & (PF_PKT|PF_UCNTL))
367 error = ureadc(0, uio);
368 while (uio->uio_resid > 0 && error == 0) {
369 cc = q_to_b(&tp->t_outq, buf, min(uio->uio_resid, BUFSIZ));
372 error = uiomove(buf, cc, uio);
379 ptsstop(struct tty *tp, int flush)
381 struct ptsc *pt = tp->t_sc;
384 /* note: FLUSHREAD and FLUSHWRITE already ok */
386 flush = TIOCPKT_STOP;
387 pt->pt_flags |= PF_STOPPED;
389 pt->pt_flags &= ~PF_STOPPED;
390 pt->pt_send |= flush;
391 /* change of perspective */
401 ptcpoll(struct cdev *dev, int events, struct thread *td)
403 struct tty *tp = dev->si_tty;
404 struct ptsc *pt = dev->si_drv1;
408 if ((tp->t_state & TS_CONNECTED) == 0)
410 (POLLHUP | POLLIN | POLLRDNORM | POLLOUT | POLLWRNORM));
413 * Need to block timeouts (ttrstart).
417 if (events & (POLLIN | POLLRDNORM))
418 if ((tp->t_state & TS_ISOPEN) &&
419 ((tp->t_outq.c_cc && (tp->t_state & TS_TTSTOP) == 0) ||
420 ((pt->pt_flags & PF_PKT) && pt->pt_send) ||
421 ((pt->pt_flags & PF_UCNTL) && pt->pt_ucntl)))
422 revents |= events & (POLLIN | POLLRDNORM);
424 if (events & (POLLOUT | POLLWRNORM))
425 if (tp->t_state & TS_ISOPEN &&
426 (((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG - 2) ||
427 (tp->t_canq.c_cc == 0 && (tp->t_lflag & ICANON)))))
428 revents |= events & (POLLOUT | POLLWRNORM);
430 if (events & POLLHUP)
431 if ((tp->t_state & TS_CARR_ON) == 0)
435 if (events & (POLLIN | POLLRDNORM))
436 selrecord(td, &pt->pt_selr);
438 if (events & (POLLOUT | POLLWRNORM))
439 selrecord(td, &pt->pt_selw);
447 ptcwrite(struct cdev *dev, struct uio *uio, int flag)
449 struct tty *tp = dev->si_tty;
452 u_char locbuf[BUFSIZ];
457 if ((tp->t_state&TS_ISOPEN) == 0)
459 while (uio->uio_resid > 0 || cc > 0) {
461 cc = min(uio->uio_resid, BUFSIZ);
463 error = uiomove(cp, cc, uio);
466 /* check again for safety */
467 if ((tp->t_state & TS_ISOPEN) == 0) {
468 /* adjust for data copied in but not written */
469 uio->uio_resid += cc;
474 if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 &&
475 (tp->t_canq.c_cc > 0 || !(tp->t_lflag&ICANON))) {
476 wakeup(TSA_HUP_OR_INPUT(tp));
479 ttyld_rint(tp, *cp++);
488 * Come here to wait for slave to open, for space
489 * in outq, or space in rawq, or an empty canq.
491 if ((tp->t_state & TS_CONNECTED) == 0) {
492 /* adjust for data copied in but not written */
493 uio->uio_resid += cc;
496 if (flag & O_NONBLOCK) {
497 /* adjust for data copied in but not written */
498 uio->uio_resid += cc;
500 return (EWOULDBLOCK);
503 error = tsleep(TSA_PTC_WRITE(tp), TTOPRI | PCATCH, "ptcout", 0);
505 /* adjust for data copied in but not written */
506 uio->uio_resid += cc;
514 ptcioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
516 struct tty *tp = dev->si_tty;
517 struct ptsc *pt = dev->si_drv1;
523 * We avoid calling ttioctl on the controller since,
524 * in that case, tp must be the controlling terminal.
526 *(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
531 if (pt->pt_flags & PF_UCNTL)
533 pt->pt_flags |= PF_PKT;
535 pt->pt_flags &= ~PF_PKT;
540 if (pt->pt_flags & PF_PKT)
542 pt->pt_flags |= PF_UCNTL;
544 pt->pt_flags &= ~PF_UCNTL;
549 * The rest of the ioctls shouldn't be called until
552 if ((tp->t_state & TS_ISOPEN) == 0)
567 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
568 * ttywflush(tp) will hang if there are characters in
571 ndflush(&tp->t_outq, tp->t_outq.c_cc);
575 if (*(unsigned int *)data >= NSIG ||
576 *(unsigned int *)data == 0)
578 if ((tp->t_lflag&NOFLSH) == 0)
579 ttyflush(tp, FREAD|FWRITE);
580 if (tp->t_pgrp != NULL) {
581 PGRP_LOCK(tp->t_pgrp);
582 pgsignal(tp->t_pgrp, *(unsigned int *)data, 1);
583 PGRP_UNLOCK(tp->t_pgrp);
585 if ((*(unsigned int *)data == SIGINFO) &&
586 ((tp->t_lflag&NOKERNINFO) == 0))
591 return (ptsioctl(dev, cmd, data, flag, td));
596 ptsioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
598 struct tty *tp = dev->si_tty;
599 struct ptsc *pt = dev->si_drv1;
600 u_char *cc = tp->t_cc;
603 if (cmd == TIOCEXT) {
605 * When the EXTPROC bit is being toggled, we need
606 * to send an TIOCPKT_IOCTL if the packet driver
610 if (pt->pt_flags & PF_PKT) {
611 pt->pt_send |= TIOCPKT_IOCTL;
612 ptcwakeup(tp, FREAD);
614 tp->t_lflag |= EXTPROC;
616 if ((tp->t_lflag & EXTPROC) &&
617 (pt->pt_flags & PF_PKT)) {
618 pt->pt_send |= TIOCPKT_IOCTL;
619 ptcwakeup(tp, FREAD);
621 tp->t_lflag &= ~EXTPROC;
625 error = ttyioctl(dev, cmd, data, flag, td);
626 if (error == ENOTTY) {
627 if (pt->pt_flags & PF_UCNTL &&
628 (cmd & ~0xff) == UIOCCMD(0)) {
630 pt->pt_ucntl = (u_char)cmd;
631 ptcwakeup(tp, FREAD);
638 * If external processing and packet mode send ioctl packet.
640 if ((tp->t_lflag&EXTPROC) && (pt->pt_flags & PF_PKT)) {
656 pt->pt_send |= TIOCPKT_IOCTL;
657 ptcwakeup(tp, FREAD);
663 stop = (tp->t_iflag & IXON) && CCEQ(cc[VSTOP], CTRL('s'))
664 && CCEQ(cc[VSTART], CTRL('q'));
665 if (pt->pt_flags & PF_NOSTOP) {
667 pt->pt_send &= ~TIOCPKT_NOSTOP;
668 pt->pt_send |= TIOCPKT_DOSTOP;
669 pt->pt_flags &= ~PF_NOSTOP;
670 ptcwakeup(tp, FREAD);
674 pt->pt_send &= ~TIOCPKT_DOSTOP;
675 pt->pt_send |= TIOCPKT_NOSTOP;
676 pt->pt_flags |= PF_NOSTOP;
677 ptcwakeup(tp, FREAD);
684 pty_clone(void *arg, struct ucred *cr, char *name, int namelen,
691 if (bcmp(name, "pty", 3) != 0)
696 case 'p': u = 0; break;
697 case 'q': u = 32; break;
698 case 'r': u = 64; break;
699 case 's': u = 96; break;
700 case 'P': u = 128; break;
701 case 'Q': u = 160; break;
702 case 'R': u = 192; break;
703 case 'S': u = 224; break;
706 if (name[4] >= '0' && name[4] <= '9')
708 else if (name[4] >= 'a' && name[4] <= 'v')
709 u += name[4] - 'a' + 10;
712 *dev = make_dev_cred(&ptc_cdevsw, u, cr,
713 UID_ROOT, GID_WHEEL, 0666, "pty%c%r", names[u / 32], u % 32);
715 (*dev)->si_flags |= SI_CHEAPCLONE;
720 ptc_drvinit(void *unused)
723 EVENTHANDLER_REGISTER(dev_clone, pty_clone, 0, 1000);
726 SYSINIT(ptcdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE,ptc_drvinit,NULL)