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);
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 #define CDEV_MAJOR_S 5
81 static struct cdevsw pts_cdevsw = {
82 .d_version = D_VERSION,
89 .d_maj = CDEV_MAJOR_S,
90 .d_flags = D_TTY | D_NEEDGIANT,
93 #define CDEV_MAJOR_C 6
94 static struct cdevsw ptc_cdevsw = {
95 .d_version = D_VERSION,
103 .d_maj = CDEV_MAJOR_C,
104 .d_flags = D_TTY | D_NEEDGIANT,
107 #define BUFSIZ 100 /* Chunk size iomoved to/from user */
111 struct selinfo pt_selr, pt_selw;
115 struct cdev *devs, *devc;
116 struct prison *pt_prison;
119 #define PF_PKT 0x08 /* packet mode */
120 #define PF_STOPPED 0x10 /* user told stopped */
121 #define PF_NOSTOP 0x40
122 #define PF_UCNTL 0x80 /* user control mode */
124 #define TSA_PTC_READ(tp) ((void *)&(tp)->t_outq.c_cf)
125 #define TSA_PTC_WRITE(tp) ((void *)&(tp)->t_rawq.c_cl)
126 #define TSA_PTS_READ(tp) ((void *)&(tp)->t_canq)
128 static char *names = "pqrsPQRS";
130 * This function creates and initializes a pts/ptc pair
132 * pts == /dev/tty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
133 * ptc == /dev/pty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
135 * XXX: define and add mapping of upper minor bits to allow more
139 ptyinit(struct cdev *devc)
146 /* For now we only map the lower 8 bits of the minor */
150 devc->si_flags &= ~SI_CHEAPCLONE;
152 pt = malloc(sizeof(*pt), M_PTY, M_WAITOK | M_ZERO);
153 pt->devs = devs = make_dev(&pts_cdevsw, n,
154 UID_ROOT, GID_WHEEL, 0666, "tty%c%r", names[n / 32], n % 32);
157 pt->pt_tty = ttymalloc(pt->pt_tty);
158 pt->pt_tty->t_sc = pt;
159 devs->si_drv1 = devc->si_drv1 = pt;
160 devs->si_tty = devc->si_tty = pt->pt_tty;
161 pt->pt_tty->t_dev = devs;
167 ptsopen(struct cdev *dev, int flag, int devtype, struct thread *td)
177 if ((tp->t_state & TS_ISOPEN) == 0) {
178 ttyinitmode(tp, 1, 0);
179 } else if (tp->t_state & TS_XCLUDE && suser(td))
181 else if (pt->pt_prison != td->td_ucred->cr_prison)
183 if (tp->t_oproc) /* Ctrlr still around. */
184 (void)ttyld_modem(tp, 1);
185 while ((tp->t_state & TS_CARR_ON) == 0) {
188 error = ttysleep(tp, TSA_CARR_ON(tp), TTIPRI | PCATCH,
193 error = ttyld_open(tp, dev);
195 ptcwakeup(tp, FREAD|FWRITE);
200 ptsclose(struct cdev *dev, int flag, int mode, struct thread *td)
206 err = ttyld_close(tp, flag);
207 (void) tty_close(tp);
212 ptsread(struct cdev *dev, struct uio *uio, int flag)
214 struct tty *tp = dev->si_tty;
218 error = ttyld_read(tp, uio, flag);
219 ptcwakeup(tp, FWRITE);
224 * Write to pseudo-tty.
225 * Wakeups of controlling tty will happen
226 * indirectly, when tty driver calls ptsstart.
229 ptswrite(struct cdev *dev, struct uio *uio, int flag)
234 if (tp->t_oproc == 0)
236 return (ttyld_write(tp, uio, flag));
240 * Start output on pseudo-tty.
241 * Wake up process selecting or sleeping for input from controlling tty.
244 ptsstart(struct tty *tp)
246 struct ptsc *pt = tp->t_sc;
248 if (tp->t_state & TS_TTSTOP)
250 if (pt->pt_flags & PF_STOPPED) {
251 pt->pt_flags &= ~PF_STOPPED;
252 pt->pt_send = TIOCPKT_START;
254 ptcwakeup(tp, FREAD);
258 ptcwakeup(struct tty *tp, int flag)
260 struct ptsc *pt = tp->t_sc;
263 selwakeuppri(&pt->pt_selr, TTIPRI);
264 wakeup(TSA_PTC_READ(tp));
267 selwakeuppri(&pt->pt_selw, TTOPRI);
268 wakeup(TSA_PTC_WRITE(tp));
273 ptcopen(struct cdev *dev, int flag, int devtype, struct thread *td)
286 tp->t_oproc = ptsstart;
287 tp->t_stop = ptsstop;
288 (void)ttyld_modem(tp, 1);
289 tp->t_lflag &= ~EXTPROC;
291 pt->pt_prison = td->td_ucred->cr_prison;
299 ptcclose(struct cdev *dev, int flags, int fmt, struct thread *td)
304 (void)ttyld_modem(tp, 0);
307 * XXX MDMBUF makes no sense for ptys but would inhibit the above
308 * l_modem(). CLOCAL makes sense but isn't supported. Special
309 * l_modem()s that ignore carrier drop make no sense for ptys but
310 * may be in use because other parts of the line discipline make
311 * sense for ptys. Recover by doing everything that a normal
312 * ttymodem() would have done except for sending a SIGHUP.
314 if (tp->t_state & TS_ISOPEN) {
315 tp->t_state &= ~(TS_CARR_ON | TS_CONNECTED);
316 tp->t_state |= TS_ZOMBIE;
317 ttyflush(tp, FREAD | FWRITE);
320 tp->t_oproc = 0; /* mark closed */
325 ptcread(struct cdev *dev, struct uio *uio, int flag)
327 struct tty *tp = dev->si_tty;
328 struct ptsc *pt = dev->si_drv1;
333 * We want to block until the slave
334 * is open, and there's something to read;
335 * but if we lost the slave or we're NBIO,
336 * then return the appropriate error instead.
339 if (tp->t_state&TS_ISOPEN) {
340 if (pt->pt_flags&PF_PKT && pt->pt_send) {
341 error = ureadc((int)pt->pt_send, uio);
344 if (pt->pt_send & TIOCPKT_IOCTL) {
345 cc = min(uio->uio_resid,
346 sizeof(tp->t_termios));
347 uiomove(&tp->t_termios, cc, uio);
352 if (pt->pt_flags&PF_UCNTL && pt->pt_ucntl) {
353 error = ureadc((int)pt->pt_ucntl, uio);
359 if (tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0)
362 if ((tp->t_state & TS_CONNECTED) == 0)
363 return (0); /* EOF */
364 if (flag & O_NONBLOCK)
365 return (EWOULDBLOCK);
366 error = tsleep(TSA_PTC_READ(tp), TTIPRI | PCATCH, "ptcin", 0);
370 if (pt->pt_flags & (PF_PKT|PF_UCNTL))
371 error = ureadc(0, uio);
372 while (uio->uio_resid > 0 && error == 0) {
373 cc = q_to_b(&tp->t_outq, buf, min(uio->uio_resid, BUFSIZ));
376 error = uiomove(buf, cc, uio);
383 ptsstop(struct tty *tp, int flush)
385 struct ptsc *pt = tp->t_sc;
388 /* note: FLUSHREAD and FLUSHWRITE already ok */
390 flush = TIOCPKT_STOP;
391 pt->pt_flags |= PF_STOPPED;
393 pt->pt_flags &= ~PF_STOPPED;
394 pt->pt_send |= flush;
395 /* change of perspective */
405 ptcpoll(struct cdev *dev, int events, struct thread *td)
407 struct tty *tp = dev->si_tty;
408 struct ptsc *pt = dev->si_drv1;
412 if ((tp->t_state & TS_CONNECTED) == 0)
414 (POLLHUP | POLLIN | POLLRDNORM | POLLOUT | POLLWRNORM));
417 * Need to block timeouts (ttrstart).
421 if (events & (POLLIN | POLLRDNORM))
422 if ((tp->t_state & TS_ISOPEN) &&
423 ((tp->t_outq.c_cc && (tp->t_state & TS_TTSTOP) == 0) ||
424 ((pt->pt_flags & PF_PKT) && pt->pt_send) ||
425 ((pt->pt_flags & PF_UCNTL) && pt->pt_ucntl)))
426 revents |= events & (POLLIN | POLLRDNORM);
428 if (events & (POLLOUT | POLLWRNORM))
429 if (tp->t_state & TS_ISOPEN &&
430 (((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG - 2) ||
431 (tp->t_canq.c_cc == 0 && (tp->t_lflag & ICANON)))))
432 revents |= events & (POLLOUT | POLLWRNORM);
434 if (events & POLLHUP)
435 if ((tp->t_state & TS_CARR_ON) == 0)
439 if (events & (POLLIN | POLLRDNORM))
440 selrecord(td, &pt->pt_selr);
442 if (events & (POLLOUT | POLLWRNORM))
443 selrecord(td, &pt->pt_selw);
451 ptcwrite(struct cdev *dev, struct uio *uio, int flag)
453 struct tty *tp = dev->si_tty;
456 u_char locbuf[BUFSIZ];
461 if ((tp->t_state&TS_ISOPEN) == 0)
463 while (uio->uio_resid > 0 || cc > 0) {
465 cc = min(uio->uio_resid, BUFSIZ);
467 error = uiomove(cp, cc, uio);
470 /* check again for safety */
471 if ((tp->t_state & TS_ISOPEN) == 0) {
472 /* adjust for data copied in but not written */
473 uio->uio_resid += cc;
478 if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 &&
479 (tp->t_canq.c_cc > 0 || !(tp->t_lflag&ICANON))) {
480 wakeup(TSA_HUP_OR_INPUT(tp));
483 ttyld_rint(tp, *cp++);
492 * Come here to wait for slave to open, for space
493 * in outq, or space in rawq, or an empty canq.
495 if ((tp->t_state & TS_CONNECTED) == 0) {
496 /* adjust for data copied in but not written */
497 uio->uio_resid += cc;
500 if (flag & O_NONBLOCK) {
501 /* adjust for data copied in but not written */
502 uio->uio_resid += cc;
504 return (EWOULDBLOCK);
507 error = tsleep(TSA_PTC_WRITE(tp), TTOPRI | PCATCH, "ptcout", 0);
509 /* adjust for data copied in but not written */
510 uio->uio_resid += cc;
518 ptcioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
520 struct tty *tp = dev->si_tty;
521 struct ptsc *pt = dev->si_drv1;
527 * We avoid calling ttioctl on the controller since,
528 * in that case, tp must be the controlling terminal.
530 *(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
535 if (pt->pt_flags & PF_UCNTL)
537 pt->pt_flags |= PF_PKT;
539 pt->pt_flags &= ~PF_PKT;
544 if (pt->pt_flags & PF_PKT)
546 pt->pt_flags |= PF_UCNTL;
548 pt->pt_flags &= ~PF_UCNTL;
553 * The rest of the ioctls shouldn't be called until
556 if ((tp->t_state & TS_ISOPEN) == 0)
571 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
572 * ttywflush(tp) will hang if there are characters in
575 ndflush(&tp->t_outq, tp->t_outq.c_cc);
579 if (*(unsigned int *)data >= NSIG ||
580 *(unsigned int *)data == 0)
582 if ((tp->t_lflag&NOFLSH) == 0)
583 ttyflush(tp, FREAD|FWRITE);
584 if (tp->t_pgrp != NULL) {
585 PGRP_LOCK(tp->t_pgrp);
586 pgsignal(tp->t_pgrp, *(unsigned int *)data, 1);
587 PGRP_UNLOCK(tp->t_pgrp);
589 if ((*(unsigned int *)data == SIGINFO) &&
590 ((tp->t_lflag&NOKERNINFO) == 0))
595 return (ptsioctl(dev, cmd, data, flag, td));
600 ptsioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
602 struct tty *tp = dev->si_tty;
603 struct ptsc *pt = dev->si_drv1;
604 u_char *cc = tp->t_cc;
607 if (cmd == TIOCEXT) {
609 * When the EXTPROC bit is being toggled, we need
610 * to send an TIOCPKT_IOCTL if the packet driver
614 if (pt->pt_flags & PF_PKT) {
615 pt->pt_send |= TIOCPKT_IOCTL;
616 ptcwakeup(tp, FREAD);
618 tp->t_lflag |= EXTPROC;
620 if ((tp->t_lflag & EXTPROC) &&
621 (pt->pt_flags & PF_PKT)) {
622 pt->pt_send |= TIOCPKT_IOCTL;
623 ptcwakeup(tp, FREAD);
625 tp->t_lflag &= ~EXTPROC;
629 error = ttyioctl(dev, cmd, data, flag, td);
630 if (error == ENOTTY) {
631 if (pt->pt_flags & PF_UCNTL &&
632 (cmd & ~0xff) == UIOCCMD(0)) {
634 pt->pt_ucntl = (u_char)cmd;
635 ptcwakeup(tp, FREAD);
642 * If external processing and packet mode send ioctl packet.
644 if ((tp->t_lflag&EXTPROC) && (pt->pt_flags & PF_PKT)) {
660 pt->pt_send |= TIOCPKT_IOCTL;
661 ptcwakeup(tp, FREAD);
667 stop = (tp->t_iflag & IXON) && CCEQ(cc[VSTOP], CTRL('s'))
668 && CCEQ(cc[VSTART], CTRL('q'));
669 if (pt->pt_flags & PF_NOSTOP) {
671 pt->pt_send &= ~TIOCPKT_NOSTOP;
672 pt->pt_send |= TIOCPKT_DOSTOP;
673 pt->pt_flags &= ~PF_NOSTOP;
674 ptcwakeup(tp, FREAD);
678 pt->pt_send &= ~TIOCPKT_DOSTOP;
679 pt->pt_send |= TIOCPKT_NOSTOP;
680 pt->pt_flags |= PF_NOSTOP;
681 ptcwakeup(tp, FREAD);
688 pty_clone(void *arg, char *name, int namelen, struct cdev **dev)
694 if (bcmp(name, "pty", 3) != 0)
699 case 'p': u = 0; break;
700 case 'q': u = 32; break;
701 case 'r': u = 64; break;
702 case 's': u = 96; break;
703 case 'P': u = 128; break;
704 case 'Q': u = 160; break;
705 case 'R': u = 192; break;
706 case 'S': u = 224; break;
709 if (name[4] >= '0' && name[4] <= '9')
711 else if (name[4] >= 'a' && name[4] <= 'v')
712 u += name[4] - 'a' + 10;
715 *dev = make_dev(&ptc_cdevsw, u,
716 UID_ROOT, GID_WHEEL, 0666, "pty%c%r", names[u / 32], u % 32);
717 (*dev)->si_flags |= SI_CHEAPCLONE;
722 ptc_drvinit(void *unused)
725 EVENTHANDLER_REGISTER(dev_clone, pty_clone, 0, 1000);
728 SYSINIT(ptcdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR_C,ptc_drvinit,NULL)