2 * SPDX-License-Identifier: BSD-2-Clause
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 /* Add compatibility bits for FreeBSD. */
35 /* Add pty(4) compat bits. */
37 /* Add bits to make Linux binaries work. */
40 #include <sys/param.h>
42 #include <sys/condvar.h>
44 #include <sys/fcntl.h>
46 #include <sys/filedesc.h>
47 #include <sys/filio.h>
48 #include <sys/kernel.h>
49 #include <sys/limits.h>
50 #include <sys/malloc.h>
51 #include <sys/mutex.h>
54 #include <sys/racct.h>
55 #include <sys/resourcevar.h>
56 #include <sys/serial.h>
58 #include <sys/syscall.h>
59 #include <sys/syscallsubr.h>
60 #include <sys/sysctl.h>
61 #include <sys/sysproto.h>
62 #include <sys/systm.h>
64 #include <sys/ttycom.h>
68 #include <machine/stdarg.h>
71 * Our utmp(5) format is limited to 8-byte TTY line names. This means
72 * we can at most allocate 1000 pseudo-terminals ("pts/999"). Allow
73 * users to increase this number, assuming they have manually increased
76 static struct unrhdr *pts_pool;
78 static MALLOC_DEFINE(M_PTS, "pts", "pseudo tty device");
84 * (t) locked by tty_lock()
85 * (c) const until freeing
88 int pts_unit; /* (c) Device unit number. */
89 unsigned int pts_flags; /* (t) Device flags. */
90 #define PTS_PKT 0x1 /* Packet mode. */
91 #define PTS_FINISHED 0x2 /* Return errors on read()/write(). */
92 char pts_pkt; /* (t) Unread packet mode data. */
94 struct cv pts_inwait; /* (t) Blocking write() on master. */
95 struct selinfo pts_inpoll; /* (t) Select queue for write(). */
96 struct cv pts_outwait; /* (t) Blocking read() on master. */
97 struct selinfo pts_outpoll; /* (t) Select queue for read(). */
100 struct cdev *pts_cdev; /* (c) Master device node. */
101 #endif /* PTS_EXTERNAL */
103 struct ucred *pts_cred; /* (c) Resource limit. */
107 * Controller-side file operations.
111 ptsdev_read(struct file *fp, struct uio *uio, struct ucred *active_cred,
112 int flags, struct thread *td)
114 struct tty *tp = fp->f_data;
115 struct pts_softc *psc = tty_softc(tp);
119 if (uio->uio_resid == 0)
126 * Implement packet mode. When packet mode is turned on,
127 * the first byte contains a bitmask of events that
128 * occurred (start, stop, flush, window size, etc).
130 if (psc->pts_flags & PTS_PKT && psc->pts_pkt) {
135 error = ureadc(pkt, uio);
140 * Transmit regular data.
142 * XXX: We shouldn't use ttydisc_getc_poll()! Even
143 * though in this implementation, there is likely going
144 * to be data, we should just call ttydisc_getc_uio()
145 * and use its return value to sleep.
147 if (ttydisc_getc_poll(tp)) {
148 if (psc->pts_flags & PTS_PKT) {
150 * XXX: Small race. Fortunately PTY
151 * consumers aren't multithreaded.
155 error = ureadc(TIOCPKT_DATA, uio);
161 error = ttydisc_getc_uio(tp, uio);
165 /* Maybe the device isn't used anyway. */
166 if (psc->pts_flags & PTS_FINISHED)
169 /* Wait for more data. */
170 if (fp->f_flag & O_NONBLOCK) {
174 error = cv_wait_sig(&psc->pts_outwait, tp->t_mtx);
185 ptsdev_write(struct file *fp, struct uio *uio, struct ucred *active_cred,
186 int flags, struct thread *td)
188 struct tty *tp = fp->f_data;
189 struct pts_softc *psc = tty_softc(tp);
190 char ib[256], *ibstart;
191 size_t iblen, rintlen;
194 if (uio->uio_resid == 0)
199 iblen = MIN(uio->uio_resid, sizeof ib);
200 error = uiomove(ib, iblen, uio);
209 * When possible, avoid the slow path. rint_bypass()
210 * copies all input to the input queue at once.
214 rintlen = ttydisc_rint_simple(tp, ibstart, iblen);
218 /* All data written. */
222 /* Maybe the device isn't used anyway. */
223 if (psc->pts_flags & PTS_FINISHED) {
228 /* Wait for more data. */
229 if (fp->f_flag & O_NONBLOCK) {
234 /* Wake up users on the slave side. */
235 ttydisc_rint_done(tp);
236 error = cv_wait_sig(&psc->pts_inwait, tp->t_mtx);
241 if (uio->uio_resid == 0)
246 done: ttydisc_rint_done(tp);
250 * Don't account for the part of the buffer that we couldn't
253 uio->uio_resid += iblen;
258 ptsdev_ioctl(struct file *fp, u_long cmd, void *data,
259 struct ucred *active_cred, struct thread *td)
261 struct tty *tp = fp->f_data;
262 struct pts_softc *psc = tty_softc(tp);
267 *(int *)data = D_TTY;
270 /* This device supports non-blocking operation. */
274 *(int *)data = ttydisc_getc_poll(tp);
278 #ifdef COMPAT_FREEBSD32
282 struct fiodgname_arg *fgn;
286 /* Reverse device name lookups, for ptsname() and ttyname(). */
292 return (copyout(p, fiodgname_buf_get_ptr(fgn, cmd), i));
296 * We need to implement TIOCGPGRP and TIOCGSID here again. When
297 * called on the pseudo-terminal master, it should not check if
298 * the terminal is the foreground terminal of the calling
301 * TIOCGETA is also implemented here. Various Linux PTY routines
302 * often call isatty(), which is implemented by tcgetattr().
306 /* Obtain terminal flags through tcgetattr(). */
308 *(struct termios*)data = tp->t_termios;
311 #endif /* PTS_LINUX */
315 * We must make sure we turn tcsetattr() calls of TCSAFLUSH and
316 * TCSADRAIN into something different. If an application would
317 * call TCSAFLUSH or TCSADRAIN on the master descriptor, it may
318 * deadlock waiting for all data to be read.
322 #if defined(PTS_COMPAT) || defined(PTS_LINUX)
325 * Get the device unit number.
327 if (psc->pts_unit < 0)
329 *(unsigned int *)data = psc->pts_unit;
331 #endif /* PTS_COMPAT || PTS_LINUX */
333 /* Get the foreground process group ID. */
335 if (tp->t_pgrp != NULL)
336 *(int *)data = tp->t_pgrp->pg_id;
338 *(int *)data = NO_PID;
342 /* Get the session leader process ID. */
344 if (tp->t_session == NULL)
347 *(int *)data = tp->t_session->s_sid;
351 /* Yes, we are a pseudo-terminal master. */
354 /* Signal the foreground process group. */
356 if (sig < 1 || sig >= NSIG)
360 tty_signal_pgrp(tp, sig);
364 /* Enable/disable packet mode. */
367 psc->pts_flags |= PTS_PKT;
369 psc->pts_flags &= ~PTS_PKT;
374 /* Just redirect this ioctl to the slave device. */
376 error = tty_ioctl(tp, cmd, data, fp->f_flag, td);
378 if (error == ENOIOCTL)
385 ptsdev_poll(struct file *fp, int events, struct ucred *active_cred,
388 struct tty *tp = fp->f_data;
389 struct pts_softc *psc = tty_softc(tp);
394 if (psc->pts_flags & PTS_FINISHED) {
395 /* Slave device is not opened. */
397 return ((events & (POLLIN|POLLRDNORM)) | POLLHUP);
400 if (events & (POLLIN|POLLRDNORM)) {
401 /* See if we can getc something. */
402 if (ttydisc_getc_poll(tp) ||
403 (psc->pts_flags & PTS_PKT && psc->pts_pkt))
404 revents |= events & (POLLIN|POLLRDNORM);
406 if (events & (POLLOUT|POLLWRNORM)) {
407 /* See if we can rint something. */
408 if (ttydisc_rint_poll(tp))
409 revents |= events & (POLLOUT|POLLWRNORM);
413 * No need to check for POLLHUP here. This device cannot be used
414 * as a callout device, which means we always have a carrier,
415 * because the master is.
420 * This code might look misleading, but the naming of
421 * poll events on this side is the opposite of the slave
424 if (events & (POLLIN|POLLRDNORM))
425 selrecord(td, &psc->pts_outpoll);
426 if (events & (POLLOUT|POLLWRNORM))
427 selrecord(td, &psc->pts_inpoll);
440 pts_kqops_read_detach(struct knote *kn)
442 struct file *fp = kn->kn_fp;
443 struct tty *tp = fp->f_data;
444 struct pts_softc *psc = tty_softc(tp);
446 knlist_remove(&psc->pts_outpoll.si_note, kn, 0);
450 pts_kqops_read_event(struct knote *kn, long hint)
452 struct file *fp = kn->kn_fp;
453 struct tty *tp = fp->f_data;
454 struct pts_softc *psc = tty_softc(tp);
456 if (psc->pts_flags & PTS_FINISHED) {
457 kn->kn_flags |= EV_EOF;
460 kn->kn_data = ttydisc_getc_poll(tp);
461 return (kn->kn_data > 0);
466 pts_kqops_write_detach(struct knote *kn)
468 struct file *fp = kn->kn_fp;
469 struct tty *tp = fp->f_data;
470 struct pts_softc *psc = tty_softc(tp);
472 knlist_remove(&psc->pts_inpoll.si_note, kn, 0);
476 pts_kqops_write_event(struct knote *kn, long hint)
478 struct file *fp = kn->kn_fp;
479 struct tty *tp = fp->f_data;
480 struct pts_softc *psc = tty_softc(tp);
482 if (psc->pts_flags & PTS_FINISHED) {
483 kn->kn_flags |= EV_EOF;
486 kn->kn_data = ttydisc_rint_poll(tp);
487 return (kn->kn_data > 0);
491 static struct filterops pts_kqops_read = {
493 .f_detach = pts_kqops_read_detach,
494 .f_event = pts_kqops_read_event,
496 static struct filterops pts_kqops_write = {
498 .f_detach = pts_kqops_write_detach,
499 .f_event = pts_kqops_write_event,
503 ptsdev_kqfilter(struct file *fp, struct knote *kn)
505 struct tty *tp = fp->f_data;
506 struct pts_softc *psc = tty_softc(tp);
511 switch (kn->kn_filter) {
513 kn->kn_fop = &pts_kqops_read;
514 knlist_add(&psc->pts_outpoll.si_note, kn, 1);
517 kn->kn_fop = &pts_kqops_write;
518 knlist_add(&psc->pts_inpoll.si_note, kn, 1);
530 ptsdev_stat(struct file *fp, struct stat *sb, struct ucred *active_cred)
532 struct tty *tp = fp->f_data;
534 struct pts_softc *psc = tty_softc(tp);
535 #endif /* PTS_EXTERNAL */
536 struct cdev *dev = tp->t_dev;
539 * According to POSIX, we must implement an fstat(). This also
540 * makes this implementation compatible with Linux binaries,
541 * because Linux calls fstat() on the pseudo-terminal master to
544 * XXX: POSIX also mentions we must fill in st_dev, but how?
547 bzero(sb, sizeof *sb);
549 if (psc->pts_cdev != NULL)
550 sb->st_ino = sb->st_rdev = dev2udev(psc->pts_cdev);
552 #endif /* PTS_EXTERNAL */
553 sb->st_ino = sb->st_rdev = tty_udev(tp);
555 sb->st_atim = dev->si_atime;
556 sb->st_ctim = dev->si_ctime;
557 sb->st_mtim = dev->si_mtime;
558 sb->st_uid = dev->si_uid;
559 sb->st_gid = dev->si_gid;
560 sb->st_mode = dev->si_mode | S_IFCHR;
566 ptsdev_close(struct file *fp, struct thread *td)
568 struct tty *tp = fp->f_data;
570 /* Deallocate TTY device. */
575 * Open of /dev/ptmx or /dev/ptyXX changes the type of file
576 * from DTYPE_VNODE to DTYPE_PTS. vn_open() increases vnode
577 * use count, we need to decrement it, and possibly do other
580 if (fp->f_vnode != NULL)
581 return (vnops.fo_close(fp, td));
587 ptsdev_fill_kinfo(struct file *fp, struct kinfo_file *kif, struct filedesc *fdp)
591 kif->kf_type = KF_TYPE_PTS;
593 kif->kf_un.kf_pts.kf_pts_dev = tty_udev(tp);
594 kif->kf_un.kf_pts.kf_pts_dev_freebsd11 =
595 kif->kf_un.kf_pts.kf_pts_dev; /* truncate */
596 strlcpy(kif->kf_path, tty_devname(tp), sizeof(kif->kf_path));
600 static struct fileops ptsdev_ops = {
601 .fo_read = ptsdev_read,
602 .fo_write = ptsdev_write,
603 .fo_truncate = invfo_truncate,
604 .fo_ioctl = ptsdev_ioctl,
605 .fo_poll = ptsdev_poll,
606 .fo_kqfilter = ptsdev_kqfilter,
607 .fo_stat = ptsdev_stat,
608 .fo_close = ptsdev_close,
609 .fo_chmod = invfo_chmod,
610 .fo_chown = invfo_chown,
611 .fo_sendfile = invfo_sendfile,
612 .fo_fill_kinfo = ptsdev_fill_kinfo,
613 .fo_cmp = file_kcmp_generic,
614 .fo_flags = DFLAG_PASSABLE,
622 ptsdrv_outwakeup(struct tty *tp)
624 struct pts_softc *psc = tty_softc(tp);
626 cv_broadcast(&psc->pts_outwait);
627 selwakeup(&psc->pts_outpoll);
628 KNOTE_LOCKED(&psc->pts_outpoll.si_note, 0);
632 ptsdrv_inwakeup(struct tty *tp)
634 struct pts_softc *psc = tty_softc(tp);
636 cv_broadcast(&psc->pts_inwait);
637 selwakeup(&psc->pts_inpoll);
638 KNOTE_LOCKED(&psc->pts_inpoll.si_note, 0);
642 ptsdrv_open(struct tty *tp)
644 struct pts_softc *psc = tty_softc(tp);
646 psc->pts_flags &= ~PTS_FINISHED;
652 ptsdrv_close(struct tty *tp)
654 struct pts_softc *psc = tty_softc(tp);
656 /* Wake up any blocked readers/writers. */
657 psc->pts_flags |= PTS_FINISHED;
658 ptsdrv_outwakeup(tp);
663 ptsdrv_pktnotify(struct tty *tp, char event)
665 struct pts_softc *psc = tty_softc(tp);
668 * Clear conflicting flags.
673 psc->pts_pkt &= ~TIOCPKT_START;
676 psc->pts_pkt &= ~TIOCPKT_STOP;
679 psc->pts_pkt &= ~TIOCPKT_DOSTOP;
682 psc->pts_pkt &= ~TIOCPKT_NOSTOP;
686 psc->pts_pkt |= event;
687 ptsdrv_outwakeup(tp);
691 ptsdrv_free(void *softc)
693 struct pts_softc *psc = softc;
695 /* Make device number available again. */
696 if (psc->pts_unit >= 0)
697 free_unr(pts_pool, psc->pts_unit);
699 chgptscnt(psc->pts_cred->cr_ruidinfo, -1, 0);
700 racct_sub_cred(psc->pts_cred, RACCT_NPTS, 1);
701 crfree(psc->pts_cred);
703 seldrain(&psc->pts_inpoll);
704 seldrain(&psc->pts_outpoll);
705 knlist_destroy(&psc->pts_inpoll.si_note);
706 knlist_destroy(&psc->pts_outpoll.si_note);
709 /* Destroy master device as well. */
710 if (psc->pts_cdev != NULL)
711 destroy_dev_sched(psc->pts_cdev);
712 #endif /* PTS_EXTERNAL */
717 static struct ttydevsw pts_class = {
718 .tsw_flags = TF_NOPREFIX,
719 .tsw_outwakeup = ptsdrv_outwakeup,
720 .tsw_inwakeup = ptsdrv_inwakeup,
721 .tsw_open = ptsdrv_open,
722 .tsw_close = ptsdrv_close,
723 .tsw_pktnotify = ptsdrv_pktnotify,
724 .tsw_free = ptsdrv_free,
729 #endif /* !PTS_EXTERNAL */
731 pts_alloc(int fflags, struct thread *td, struct file *fp)
735 struct pts_softc *psc;
736 struct proc *p = td->td_proc;
737 struct ucred *cred = td->td_ucred;
739 /* Resource limiting. */
741 error = racct_add(p, RACCT_NPTS, 1);
746 ok = chgptscnt(cred->cr_ruidinfo, 1, lim_cur(td, RLIMIT_NPTS));
748 racct_sub(p, RACCT_NPTS, 1);
754 /* Try to allocate a new pts unit number. */
755 unit = alloc_unr(pts_pool);
757 racct_sub(p, RACCT_NPTS, 1);
758 chgptscnt(cred->cr_ruidinfo, -1, 0);
762 /* Allocate TTY and softc. */
763 psc = malloc(sizeof(struct pts_softc), M_PTS, M_WAITOK|M_ZERO);
764 cv_init(&psc->pts_inwait, "ptsin");
765 cv_init(&psc->pts_outwait, "ptsout");
767 psc->pts_unit = unit;
768 psc->pts_cred = crhold(cred);
770 tp = tty_alloc(&pts_class, psc);
771 knlist_init_mtx(&psc->pts_inpoll.si_note, tp->t_mtx);
772 knlist_init_mtx(&psc->pts_outpoll.si_note, tp->t_mtx);
774 /* Expose the slave device as well. */
775 tty_makedev(tp, td->td_ucred, "pts/%u", psc->pts_unit);
777 finit(fp, fflags, DTYPE_PTS, tp, &ptsdev_ops);
784 pts_alloc_external(int fflags, struct thread *td, struct file *fp,
785 struct cdev *dev, const char *name)
789 struct pts_softc *psc;
790 struct proc *p = td->td_proc;
791 struct ucred *cred = td->td_ucred;
793 /* Resource limiting. */
795 error = racct_add(p, RACCT_NPTS, 1);
800 ok = chgptscnt(cred->cr_ruidinfo, 1, lim_cur(td, RLIMIT_NPTS));
802 racct_sub(p, RACCT_NPTS, 1);
808 /* Allocate TTY and softc. */
809 psc = malloc(sizeof(struct pts_softc), M_PTS, M_WAITOK|M_ZERO);
810 cv_init(&psc->pts_inwait, "ptsin");
811 cv_init(&psc->pts_outwait, "ptsout");
815 psc->pts_cred = crhold(cred);
817 tp = tty_alloc(&pts_class, psc);
818 knlist_init_mtx(&psc->pts_inpoll.si_note, tp->t_mtx);
819 knlist_init_mtx(&psc->pts_outpoll.si_note, tp->t_mtx);
821 /* Expose the slave device as well. */
822 tty_makedev(tp, td->td_ucred, "%s", name);
824 finit(fp, fflags, DTYPE_PTS, tp, &ptsdev_ops);
828 #endif /* PTS_EXTERNAL */
831 sys_posix_openpt(struct thread *td, struct posix_openpt_args *uap)
837 * POSIX states it's unspecified when other flags are passed. We
840 if (uap->flags & ~(O_RDWR|O_NOCTTY|O_CLOEXEC))
843 error = falloc(td, &fp, &fd, uap->flags);
847 /* Allocate the actual pseudo-TTY. */
848 error = pts_alloc(FFLAGS(uap->flags & O_ACCMODE), td, fp);
855 /* Pass it back to userspace. */
856 td->td_retval[0] = fd;
863 pts_init(void *unused)
866 pts_pool = new_unrhdr(0, INT_MAX, NULL);
869 SYSINIT(pts, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, pts_init, NULL);