2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2008 Ed Schouten <ed@FreeBSD.org>
7 * Portions of this software were developed under sponsorship from Snow
8 * B.V., the Netherlands.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 /* Add compatibility bits for FreeBSD. */
37 /* Add pty(4) compat bits. */
39 /* Add bits to make Linux binaries work. */
42 #include <sys/param.h>
44 #include <sys/condvar.h>
46 #include <sys/fcntl.h>
48 #include <sys/filedesc.h>
49 #include <sys/filio.h>
50 #include <sys/kernel.h>
51 #include <sys/limits.h>
52 #include <sys/malloc.h>
53 #include <sys/mutex.h>
56 #include <sys/racct.h>
57 #include <sys/resourcevar.h>
58 #include <sys/serial.h>
60 #include <sys/syscall.h>
61 #include <sys/syscallsubr.h>
62 #include <sys/sysctl.h>
63 #include <sys/sysent.h>
64 #include <sys/sysproto.h>
65 #include <sys/systm.h>
67 #include <sys/ttycom.h>
71 #include <machine/stdarg.h>
74 * Our utmp(5) format is limited to 8-byte TTY line names. This means
75 * we can at most allocate 1000 pseudo-terminals ("pts/999"). Allow
76 * users to increase this number, assuming they have manually increased
79 static struct unrhdr *pts_pool;
81 static MALLOC_DEFINE(M_PTS, "pts", "pseudo tty device");
87 * (t) locked by tty_lock()
88 * (c) const until freeing
91 int pts_unit; /* (c) Device unit number. */
92 unsigned int pts_flags; /* (t) Device flags. */
93 #define PTS_PKT 0x1 /* Packet mode. */
94 #define PTS_FINISHED 0x2 /* Return errors on read()/write(). */
95 char pts_pkt; /* (t) Unread packet mode data. */
97 struct cv pts_inwait; /* (t) Blocking write() on master. */
98 struct selinfo pts_inpoll; /* (t) Select queue for write(). */
99 struct cv pts_outwait; /* (t) Blocking read() on master. */
100 struct selinfo pts_outpoll; /* (t) Select queue for read(). */
103 struct cdev *pts_cdev; /* (c) Master device node. */
104 #endif /* PTS_EXTERNAL */
106 struct ucred *pts_cred; /* (c) Resource limit. */
110 * Controller-side file operations.
114 ptsdev_read(struct file *fp, struct uio *uio, struct ucred *active_cred,
115 int flags, struct thread *td)
117 struct tty *tp = fp->f_data;
118 struct pts_softc *psc = tty_softc(tp);
122 if (uio->uio_resid == 0)
129 * Implement packet mode. When packet mode is turned on,
130 * the first byte contains a bitmask of events that
131 * occurred (start, stop, flush, window size, etc).
133 if (psc->pts_flags & PTS_PKT && psc->pts_pkt) {
138 error = ureadc(pkt, uio);
143 * Transmit regular data.
145 * XXX: We shouldn't use ttydisc_getc_poll()! Even
146 * though in this implementation, there is likely going
147 * to be data, we should just call ttydisc_getc_uio()
148 * and use its return value to sleep.
150 if (ttydisc_getc_poll(tp)) {
151 if (psc->pts_flags & PTS_PKT) {
153 * XXX: Small race. Fortunately PTY
154 * consumers aren't multithreaded.
158 error = ureadc(TIOCPKT_DATA, uio);
164 error = ttydisc_getc_uio(tp, uio);
168 /* Maybe the device isn't used anyway. */
169 if (psc->pts_flags & PTS_FINISHED)
172 /* Wait for more data. */
173 if (fp->f_flag & O_NONBLOCK) {
177 error = cv_wait_sig(&psc->pts_outwait, tp->t_mtx);
188 ptsdev_write(struct file *fp, struct uio *uio, struct ucred *active_cred,
189 int flags, struct thread *td)
191 struct tty *tp = fp->f_data;
192 struct pts_softc *psc = tty_softc(tp);
193 char ib[256], *ibstart;
194 size_t iblen, rintlen;
197 if (uio->uio_resid == 0)
202 iblen = MIN(uio->uio_resid, sizeof ib);
203 error = uiomove(ib, iblen, uio);
212 * When possible, avoid the slow path. rint_bypass()
213 * copies all input to the input queue at once.
217 rintlen = ttydisc_rint_simple(tp, ibstart, iblen);
221 /* All data written. */
225 /* Maybe the device isn't used anyway. */
226 if (psc->pts_flags & PTS_FINISHED) {
231 /* Wait for more data. */
232 if (fp->f_flag & O_NONBLOCK) {
237 /* Wake up users on the slave side. */
238 ttydisc_rint_done(tp);
239 error = cv_wait_sig(&psc->pts_inwait, tp->t_mtx);
244 if (uio->uio_resid == 0)
249 done: ttydisc_rint_done(tp);
253 * Don't account for the part of the buffer that we couldn't
256 uio->uio_resid += iblen;
261 ptsdev_ioctl(struct file *fp, u_long cmd, void *data,
262 struct ucred *active_cred, struct thread *td)
264 struct tty *tp = fp->f_data;
265 struct pts_softc *psc = tty_softc(tp);
270 *(int *)data = D_TTY;
273 /* This device supports non-blocking operation. */
277 if (psc->pts_flags & PTS_FINISHED) {
278 /* Force read() to be called. */
281 *(int *)data = ttydisc_getc_poll(tp);
286 struct fiodgname_arg *fgn;
290 /* Reverse device name lookups, for ptsname() and ttyname(). */
296 return copyout(p, fgn->buf, i);
300 * We need to implement TIOCGPGRP and TIOCGSID here again. When
301 * called on the pseudo-terminal master, it should not check if
302 * the terminal is the foreground terminal of the calling
305 * TIOCGETA is also implemented here. Various Linux PTY routines
306 * often call isatty(), which is implemented by tcgetattr().
310 /* Obtain terminal flags through tcgetattr(). */
312 *(struct termios*)data = tp->t_termios;
315 #endif /* PTS_LINUX */
319 * We must make sure we turn tcsetattr() calls of TCSAFLUSH and
320 * TCSADRAIN into something different. If an application would
321 * call TCSAFLUSH or TCSADRAIN on the master descriptor, it may
322 * deadlock waiting for all data to be read.
326 #if defined(PTS_COMPAT) || defined(PTS_LINUX)
329 * Get the device unit number.
331 if (psc->pts_unit < 0)
333 *(unsigned int *)data = psc->pts_unit;
335 #endif /* PTS_COMPAT || PTS_LINUX */
337 /* Get the foreground process group ID. */
339 if (tp->t_pgrp != NULL)
340 *(int *)data = tp->t_pgrp->pg_id;
342 *(int *)data = NO_PID;
346 /* Get the session leader process ID. */
348 if (tp->t_session == NULL)
351 *(int *)data = tp->t_session->s_sid;
355 /* Yes, we are a pseudo-terminal master. */
358 /* Signal the foreground process group. */
360 if (sig < 1 || sig >= NSIG)
364 tty_signal_pgrp(tp, sig);
368 /* Enable/disable packet mode. */
371 psc->pts_flags |= PTS_PKT;
373 psc->pts_flags &= ~PTS_PKT;
378 /* Just redirect this ioctl to the slave device. */
380 error = tty_ioctl(tp, cmd, data, fp->f_flag, td);
382 if (error == ENOIOCTL)
389 ptsdev_poll(struct file *fp, int events, struct ucred *active_cred,
392 struct tty *tp = fp->f_data;
393 struct pts_softc *psc = tty_softc(tp);
398 if (psc->pts_flags & PTS_FINISHED) {
399 /* Slave device is not opened. */
401 return ((events & (POLLIN|POLLRDNORM)) | POLLHUP);
404 if (events & (POLLIN|POLLRDNORM)) {
405 /* See if we can getc something. */
406 if (ttydisc_getc_poll(tp) ||
407 (psc->pts_flags & PTS_PKT && psc->pts_pkt))
408 revents |= events & (POLLIN|POLLRDNORM);
410 if (events & (POLLOUT|POLLWRNORM)) {
411 /* See if we can rint something. */
412 if (ttydisc_rint_poll(tp))
413 revents |= events & (POLLOUT|POLLWRNORM);
417 * No need to check for POLLHUP here. This device cannot be used
418 * as a callout device, which means we always have a carrier,
419 * because the master is.
424 * This code might look misleading, but the naming of
425 * poll events on this side is the opposite of the slave
428 if (events & (POLLIN|POLLRDNORM))
429 selrecord(td, &psc->pts_outpoll);
430 if (events & (POLLOUT|POLLWRNORM))
431 selrecord(td, &psc->pts_inpoll);
444 pts_kqops_read_detach(struct knote *kn)
446 struct file *fp = kn->kn_fp;
447 struct tty *tp = fp->f_data;
448 struct pts_softc *psc = tty_softc(tp);
450 knlist_remove(&psc->pts_outpoll.si_note, kn, 0);
454 pts_kqops_read_event(struct knote *kn, long hint)
456 struct file *fp = kn->kn_fp;
457 struct tty *tp = fp->f_data;
458 struct pts_softc *psc = tty_softc(tp);
460 if (psc->pts_flags & PTS_FINISHED) {
461 kn->kn_flags |= EV_EOF;
464 kn->kn_data = ttydisc_getc_poll(tp);
465 return (kn->kn_data > 0);
470 pts_kqops_write_detach(struct knote *kn)
472 struct file *fp = kn->kn_fp;
473 struct tty *tp = fp->f_data;
474 struct pts_softc *psc = tty_softc(tp);
476 knlist_remove(&psc->pts_inpoll.si_note, kn, 0);
480 pts_kqops_write_event(struct knote *kn, long hint)
482 struct file *fp = kn->kn_fp;
483 struct tty *tp = fp->f_data;
484 struct pts_softc *psc = tty_softc(tp);
486 if (psc->pts_flags & PTS_FINISHED) {
487 kn->kn_flags |= EV_EOF;
490 kn->kn_data = ttydisc_rint_poll(tp);
491 return (kn->kn_data > 0);
495 static struct filterops pts_kqops_read = {
497 .f_detach = pts_kqops_read_detach,
498 .f_event = pts_kqops_read_event,
500 static struct filterops pts_kqops_write = {
502 .f_detach = pts_kqops_write_detach,
503 .f_event = pts_kqops_write_event,
507 ptsdev_kqfilter(struct file *fp, struct knote *kn)
509 struct tty *tp = fp->f_data;
510 struct pts_softc *psc = tty_softc(tp);
515 switch (kn->kn_filter) {
517 kn->kn_fop = &pts_kqops_read;
518 knlist_add(&psc->pts_outpoll.si_note, kn, 1);
521 kn->kn_fop = &pts_kqops_write;
522 knlist_add(&psc->pts_inpoll.si_note, kn, 1);
534 ptsdev_stat(struct file *fp, struct stat *sb, struct ucred *active_cred,
537 struct tty *tp = fp->f_data;
539 struct pts_softc *psc = tty_softc(tp);
540 #endif /* PTS_EXTERNAL */
541 struct cdev *dev = tp->t_dev;
544 * According to POSIX, we must implement an fstat(). This also
545 * makes this implementation compatible with Linux binaries,
546 * because Linux calls fstat() on the pseudo-terminal master to
549 * XXX: POSIX also mentions we must fill in st_dev, but how?
552 bzero(sb, sizeof *sb);
554 if (psc->pts_cdev != NULL)
555 sb->st_ino = sb->st_rdev = dev2udev(psc->pts_cdev);
557 #endif /* PTS_EXTERNAL */
558 sb->st_ino = sb->st_rdev = tty_udev(tp);
560 sb->st_atim = dev->si_atime;
561 sb->st_ctim = dev->si_ctime;
562 sb->st_mtim = dev->si_mtime;
563 sb->st_uid = dev->si_uid;
564 sb->st_gid = dev->si_gid;
565 sb->st_mode = dev->si_mode | S_IFCHR;
571 ptsdev_close(struct file *fp, struct thread *td)
573 struct tty *tp = fp->f_data;
575 /* Deallocate TTY device. */
580 * Open of /dev/ptmx or /dev/ptyXX changes the type of file
581 * from DTYPE_VNODE to DTYPE_PTS. vn_open() increases vnode
582 * use count, we need to decrement it, and possibly do other
585 if (fp->f_vnode != NULL)
586 return (vnops.fo_close(fp, td));
592 ptsdev_fill_kinfo(struct file *fp, struct kinfo_file *kif, struct filedesc *fdp)
596 kif->kf_type = KF_TYPE_PTS;
598 kif->kf_un.kf_pts.kf_pts_dev = tty_udev(tp);
599 kif->kf_un.kf_pts.kf_pts_dev_freebsd11 =
600 kif->kf_un.kf_pts.kf_pts_dev; /* truncate */
601 strlcpy(kif->kf_path, tty_devname(tp), sizeof(kif->kf_path));
605 static struct fileops ptsdev_ops = {
606 .fo_read = ptsdev_read,
607 .fo_write = ptsdev_write,
608 .fo_truncate = invfo_truncate,
609 .fo_ioctl = ptsdev_ioctl,
610 .fo_poll = ptsdev_poll,
611 .fo_kqfilter = ptsdev_kqfilter,
612 .fo_stat = ptsdev_stat,
613 .fo_close = ptsdev_close,
614 .fo_chmod = invfo_chmod,
615 .fo_chown = invfo_chown,
616 .fo_sendfile = invfo_sendfile,
617 .fo_fill_kinfo = ptsdev_fill_kinfo,
618 .fo_flags = DFLAG_PASSABLE,
626 ptsdrv_outwakeup(struct tty *tp)
628 struct pts_softc *psc = tty_softc(tp);
630 cv_broadcast(&psc->pts_outwait);
631 selwakeup(&psc->pts_outpoll);
632 KNOTE_LOCKED(&psc->pts_outpoll.si_note, 0);
636 ptsdrv_inwakeup(struct tty *tp)
638 struct pts_softc *psc = tty_softc(tp);
640 cv_broadcast(&psc->pts_inwait);
641 selwakeup(&psc->pts_inpoll);
642 KNOTE_LOCKED(&psc->pts_inpoll.si_note, 0);
646 ptsdrv_open(struct tty *tp)
648 struct pts_softc *psc = tty_softc(tp);
650 psc->pts_flags &= ~PTS_FINISHED;
656 ptsdrv_close(struct tty *tp)
658 struct pts_softc *psc = tty_softc(tp);
660 /* Wake up any blocked readers/writers. */
661 psc->pts_flags |= PTS_FINISHED;
662 ptsdrv_outwakeup(tp);
667 ptsdrv_pktnotify(struct tty *tp, char event)
669 struct pts_softc *psc = tty_softc(tp);
672 * Clear conflicting flags.
677 psc->pts_pkt &= ~TIOCPKT_START;
680 psc->pts_pkt &= ~TIOCPKT_STOP;
683 psc->pts_pkt &= ~TIOCPKT_DOSTOP;
686 psc->pts_pkt &= ~TIOCPKT_NOSTOP;
690 psc->pts_pkt |= event;
691 ptsdrv_outwakeup(tp);
695 ptsdrv_free(void *softc)
697 struct pts_softc *psc = softc;
699 /* Make device number available again. */
700 if (psc->pts_unit >= 0)
701 free_unr(pts_pool, psc->pts_unit);
703 chgptscnt(psc->pts_cred->cr_ruidinfo, -1, 0);
704 racct_sub_cred(psc->pts_cred, RACCT_NPTS, 1);
705 crfree(psc->pts_cred);
707 seldrain(&psc->pts_inpoll);
708 seldrain(&psc->pts_outpoll);
709 knlist_destroy(&psc->pts_inpoll.si_note);
710 knlist_destroy(&psc->pts_outpoll.si_note);
713 /* Destroy master device as well. */
714 if (psc->pts_cdev != NULL)
715 destroy_dev_sched(psc->pts_cdev);
716 #endif /* PTS_EXTERNAL */
721 static struct ttydevsw pts_class = {
722 .tsw_flags = TF_NOPREFIX,
723 .tsw_outwakeup = ptsdrv_outwakeup,
724 .tsw_inwakeup = ptsdrv_inwakeup,
725 .tsw_open = ptsdrv_open,
726 .tsw_close = ptsdrv_close,
727 .tsw_pktnotify = ptsdrv_pktnotify,
728 .tsw_free = ptsdrv_free,
733 #endif /* !PTS_EXTERNAL */
735 pts_alloc(int fflags, struct thread *td, struct file *fp)
739 struct pts_softc *psc;
740 struct proc *p = td->td_proc;
741 struct ucred *cred = td->td_ucred;
743 /* Resource limiting. */
745 error = racct_add(p, RACCT_NPTS, 1);
750 ok = chgptscnt(cred->cr_ruidinfo, 1, lim_cur(td, RLIMIT_NPTS));
752 racct_sub(p, RACCT_NPTS, 1);
758 /* Try to allocate a new pts unit number. */
759 unit = alloc_unr(pts_pool);
761 racct_sub(p, RACCT_NPTS, 1);
762 chgptscnt(cred->cr_ruidinfo, -1, 0);
766 /* Allocate TTY and softc. */
767 psc = malloc(sizeof(struct pts_softc), M_PTS, M_WAITOK|M_ZERO);
768 cv_init(&psc->pts_inwait, "ptsin");
769 cv_init(&psc->pts_outwait, "ptsout");
771 psc->pts_unit = unit;
772 psc->pts_cred = crhold(cred);
774 tp = tty_alloc(&pts_class, psc);
775 knlist_init_mtx(&psc->pts_inpoll.si_note, tp->t_mtx);
776 knlist_init_mtx(&psc->pts_outpoll.si_note, tp->t_mtx);
778 /* Expose the slave device as well. */
779 tty_makedev(tp, td->td_ucred, "pts/%u", psc->pts_unit);
781 finit(fp, fflags, DTYPE_PTS, tp, &ptsdev_ops);
788 pts_alloc_external(int fflags, struct thread *td, struct file *fp,
789 struct cdev *dev, const char *name)
793 struct pts_softc *psc;
794 struct proc *p = td->td_proc;
795 struct ucred *cred = td->td_ucred;
797 /* Resource limiting. */
799 error = racct_add(p, RACCT_NPTS, 1);
804 ok = chgptscnt(cred->cr_ruidinfo, 1, lim_cur(td, RLIMIT_NPTS));
806 racct_sub(p, RACCT_NPTS, 1);
812 /* Allocate TTY and softc. */
813 psc = malloc(sizeof(struct pts_softc), M_PTS, M_WAITOK|M_ZERO);
814 cv_init(&psc->pts_inwait, "ptsin");
815 cv_init(&psc->pts_outwait, "ptsout");
819 psc->pts_cred = crhold(cred);
821 tp = tty_alloc(&pts_class, psc);
822 knlist_init_mtx(&psc->pts_inpoll.si_note, tp->t_mtx);
823 knlist_init_mtx(&psc->pts_outpoll.si_note, tp->t_mtx);
825 /* Expose the slave device as well. */
826 tty_makedev(tp, td->td_ucred, "%s", name);
828 finit(fp, fflags, DTYPE_PTS, tp, &ptsdev_ops);
832 #endif /* PTS_EXTERNAL */
835 sys_posix_openpt(struct thread *td, struct posix_openpt_args *uap)
841 * POSIX states it's unspecified when other flags are passed. We
844 if (uap->flags & ~(O_RDWR|O_NOCTTY|O_CLOEXEC))
847 error = falloc(td, &fp, &fd, uap->flags);
851 /* Allocate the actual pseudo-TTY. */
852 error = pts_alloc(FFLAGS(uap->flags & O_ACCMODE), td, fp);
859 /* Pass it back to userspace. */
860 td->td_retval[0] = fd;
867 pts_init(void *unused)
870 pts_pool = new_unrhdr(0, INT_MAX, NULL);
873 SYSINIT(pts, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, pts_init, NULL);