2 * Copyright (c) 2008 Ed Schouten <ed@FreeBSD.org>
5 * Portions of this software were developed under sponsorship from Snow
6 * B.V., the Netherlands.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
35 /* Add compatibility bits for FreeBSD. */
38 /* Add /dev/ptyXX compat bits. */
41 /* Add bits to make Linux binaries work. */
44 #include <sys/param.h>
46 #include <sys/condvar.h>
48 #include <sys/fcntl.h>
50 #include <sys/filedesc.h>
51 #include <sys/filio.h>
52 #include <sys/kernel.h>
53 #include <sys/limits.h>
54 #include <sys/malloc.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>
69 #include <machine/stdarg.h>
72 * Our utmp(5) format is limited to 8-byte TTY line names. This means
73 * we can at most allocate 1000 pseudo-terminals ("pts/999"). Allow
74 * users to increase this number, assuming they have manually increased
77 static struct unrhdr *pts_pool;
78 static unsigned int pts_maxdev = 999;
79 SYSCTL_UINT(_kern, OID_AUTO, pts_maxdev, CTLFLAG_RW, &pts_maxdev, 0,
80 "Maximum amount of pts(4) pseudo-terminals");
82 static MALLOC_DEFINE(M_PTS, "pts", "pseudo tty device");
88 * (t) locked by tty_lock()
89 * (c) const until freeing
92 int pts_unit; /* (c) Device unit number. */
93 unsigned int pts_flags; /* (t) Device flags. */
94 #define PTS_PKT 0x1 /* Packet mode. */
95 #define PTS_FINISHED 0x2 /* Return errors on read()/write(). */
96 char pts_pkt; /* (t) Unread packet mode data. */
98 struct cv pts_inwait; /* (t) Blocking write() on master. */
99 struct selinfo pts_inpoll; /* (t) Select queue for write(). */
100 struct cv pts_outwait; /* (t) Blocking read() on master. */
101 struct selinfo pts_outpoll; /* (t) Select queue for read(). */
104 struct cdev *pts_cdev; /* (c) Master device node. */
105 #endif /* PTS_EXTERNAL */
107 struct uidinfo *pts_uidinfo; /* (c) Resource limit. */
111 * Controller-side file operations.
115 ptsdev_read(struct file *fp, struct uio *uio, struct ucred *active_cred,
116 int flags, struct thread *td)
118 struct tty *tp = fp->f_data;
119 struct pts_softc *psc = tty_softc(tp);
123 if (uio->uio_resid == 0)
130 * Implement packet mode. When packet mode is turned on,
131 * the first byte contains a bitmask of events that
132 * occured (start, stop, flush, window size, etc).
134 if (psc->pts_flags & PTS_PKT && psc->pts_pkt) {
139 error = ureadc(pkt, uio);
144 * Transmit regular data.
146 * XXX: We shouldn't use ttydisc_getc_poll()! Even
147 * though in this implementation, there is likely going
148 * to be data, we should just call ttydisc_getc_uio()
149 * and use its return value to sleep.
151 if (ttydisc_getc_poll(tp)) {
152 if (psc->pts_flags & PTS_PKT) {
154 * XXX: Small race. Fortunately PTY
155 * consumers aren't multithreaded.
159 error = ureadc(TIOCPKT_DATA, uio);
165 error = ttydisc_getc_uio(tp, uio);
169 /* Maybe the device isn't used anyway. */
170 if (psc->pts_flags & PTS_FINISHED)
173 /* Wait for more data. */
174 if (fp->f_flag & O_NONBLOCK) {
178 error = cv_wait_sig(&psc->pts_outwait, tp->t_mtx);
189 ptsdev_write(struct file *fp, struct uio *uio, struct ucred *active_cred,
190 int flags, struct thread *td)
192 struct tty *tp = fp->f_data;
193 struct pts_softc *psc = tty_softc(tp);
194 char ib[256], *ibstart;
195 size_t iblen, rintlen;
198 if (uio->uio_resid == 0)
203 iblen = MIN(uio->uio_resid, sizeof ib);
204 error = uiomove(ib, iblen, uio);
213 * When possible, avoid the slow path. rint_bypass()
214 * copies all input to the input queue at once.
218 if (ttydisc_can_bypass(tp)) {
219 /* Store data at once. */
220 rintlen = ttydisc_rint_bypass(tp,
226 /* All data written. */
230 error = ttydisc_rint(tp, *ibstart, 0);
232 /* Character stored successfully. */
239 /* Maybe the device isn't used anyway. */
240 if (psc->pts_flags & PTS_FINISHED) {
245 /* Wait for more data. */
246 if (fp->f_flag & O_NONBLOCK) {
251 /* Wake up users on the slave side. */
252 ttydisc_rint_done(tp);
253 error = cv_wait_sig(&psc->pts_inwait, tp->t_mtx);
258 if (uio->uio_resid == 0)
263 done: ttydisc_rint_done(tp);
267 * Don't account for the part of the buffer that we couldn't
270 uio->uio_resid += iblen;
275 ptsdev_truncate(struct file *fp, off_t length, struct ucred *active_cred,
283 ptsdev_ioctl(struct file *fp, u_long cmd, void *data,
284 struct ucred *active_cred, struct thread *td)
286 struct tty *tp = fp->f_data;
287 struct pts_softc *psc = tty_softc(tp);
292 /* This device supports non-blocking operation. */
296 if (psc->pts_flags & PTS_FINISHED) {
297 /* Force read() to be called. */
300 *(int *)data = ttydisc_getc_poll(tp);
305 struct fiodgname_arg *fgn;
309 /* Reverse device name lookups, for ptsname() and ttyname(). */
315 return copyout(p, fgn->buf, i);
319 * We need to implement TIOCGPGRP and TIOCGSID here again. When
320 * called on the pseudo-terminal master, it should not check if
321 * the terminal is the foreground terminal of the calling
324 * TIOCGETA is also implemented here. Various Linux PTY routines
325 * often call isatty(), which is implemented by tcgetattr().
329 /* Obtain terminal flags through tcgetattr(). */
331 *(struct termios*)data = tp->t_termios;
334 #endif /* PTS_LINUX */
338 * We must make sure we turn tcsetattr() calls of TCSAFLUSH and
339 * TCSADRAIN into something different. If an application would
340 * call TCSAFLUSH or TCSADRAIN on the master descriptor, it may
341 * deadlock waiting for all data to be read.
345 #if defined(PTS_COMPAT) || defined(PTS_LINUX)
348 * Get the device unit number.
350 if (psc->pts_unit < 0)
352 *(unsigned int *)data = psc->pts_unit;
354 #endif /* PTS_COMPAT || PTS_LINUX */
356 /* Get the foreground process group ID. */
358 if (tp->t_pgrp != NULL)
359 *(int *)data = tp->t_pgrp->pg_id;
361 *(int *)data = NO_PID;
365 /* Get the session leader process ID. */
367 if (tp->t_session == NULL)
370 *(int *)data = tp->t_session->s_sid;
374 /* Yes, we are a pseudo-terminal master. */
377 /* Signal the foreground process group. */
379 if (sig < 1 || sig >= NSIG)
383 tty_signal_pgrp(tp, sig);
387 /* Enable/disable packet mode. */
390 psc->pts_flags |= PTS_PKT;
392 psc->pts_flags &= ~PTS_PKT;
397 /* Just redirect this ioctl to the slave device. */
399 error = tty_ioctl(tp, cmd, data, fp->f_flag, td);
401 if (error == ENOIOCTL)
408 ptsdev_poll(struct file *fp, int events, struct ucred *active_cred,
411 struct tty *tp = fp->f_data;
412 struct pts_softc *psc = tty_softc(tp);
417 if (psc->pts_flags & PTS_FINISHED) {
418 /* Slave device is not opened. */
420 return ((events & (POLLIN|POLLRDNORM)) | POLLHUP);
423 if (events & (POLLIN|POLLRDNORM)) {
424 /* See if we can getc something. */
425 if (ttydisc_getc_poll(tp) ||
426 (psc->pts_flags & PTS_PKT && psc->pts_pkt))
427 revents |= events & (POLLIN|POLLRDNORM);
429 if (events & (POLLOUT|POLLWRNORM)) {
430 /* See if we can rint something. */
431 if (ttydisc_rint_poll(tp))
432 revents |= events & (POLLOUT|POLLWRNORM);
436 * No need to check for POLLHUP here. This device cannot be used
437 * as a callout device, which means we always have a carrier,
438 * because the master is.
443 * This code might look misleading, but the naming of
444 * poll events on this side is the opposite of the slave
447 if (events & (POLLIN|POLLRDNORM))
448 selrecord(td, &psc->pts_outpoll);
449 if (events & (POLLOUT|POLLWRNORM))
450 selrecord(td, &psc->pts_inpoll);
463 pts_kqops_read_detach(struct knote *kn)
465 struct file *fp = kn->kn_fp;
466 struct tty *tp = fp->f_data;
467 struct pts_softc *psc = tty_softc(tp);
469 knlist_remove(&psc->pts_outpoll.si_note, kn, 0);
473 pts_kqops_read_event(struct knote *kn, long hint)
475 struct file *fp = kn->kn_fp;
476 struct tty *tp = fp->f_data;
477 struct pts_softc *psc = tty_softc(tp);
479 if (psc->pts_flags & PTS_FINISHED) {
480 kn->kn_flags |= EV_EOF;
483 kn->kn_data = ttydisc_getc_poll(tp);
484 return (kn->kn_data > 0);
489 pts_kqops_write_detach(struct knote *kn)
491 struct file *fp = kn->kn_fp;
492 struct tty *tp = fp->f_data;
493 struct pts_softc *psc = tty_softc(tp);
495 knlist_remove(&psc->pts_inpoll.si_note, kn, 0);
499 pts_kqops_write_event(struct knote *kn, long hint)
501 struct file *fp = kn->kn_fp;
502 struct tty *tp = fp->f_data;
503 struct pts_softc *psc = tty_softc(tp);
505 if (psc->pts_flags & PTS_FINISHED) {
506 kn->kn_flags |= EV_EOF;
509 kn->kn_data = ttydisc_rint_poll(tp);
510 return (kn->kn_data > 0);
514 static struct filterops pts_kqops_read =
515 { 1, NULL, pts_kqops_read_detach, pts_kqops_read_event };
516 static struct filterops pts_kqops_write =
517 { 1, NULL, pts_kqops_write_detach, pts_kqops_write_event };
520 ptsdev_kqfilter(struct file *fp, struct knote *kn)
522 struct tty *tp = fp->f_data;
523 struct pts_softc *psc = tty_softc(tp);
528 switch (kn->kn_filter) {
530 kn->kn_fop = &pts_kqops_read;
531 knlist_add(&psc->pts_outpoll.si_note, kn, 1);
534 kn->kn_fop = &pts_kqops_write;
535 knlist_add(&psc->pts_inpoll.si_note, kn, 1);
547 ptsdev_stat(struct file *fp, struct stat *sb, struct ucred *active_cred,
550 struct tty *tp = fp->f_data;
552 struct pts_softc *psc = tty_softc(tp);
553 #endif /* PTS_EXTERNAL */
554 struct cdev *dev = tp->t_dev;
557 * According to POSIX, we must implement an fstat(). This also
558 * makes this implementation compatible with Linux binaries,
559 * because Linux calls fstat() on the pseudo-terminal master to
562 * XXX: POSIX also mentions we must fill in st_dev, but how?
565 bzero(sb, sizeof *sb);
567 if (psc->pts_cdev != NULL)
568 sb->st_ino = sb->st_rdev = dev2udev(psc->pts_cdev);
570 #endif /* PTS_EXTERNAL */
571 sb->st_ino = sb->st_rdev = tty_udev(tp);
573 sb->st_atimespec = dev->si_atime;
574 sb->st_ctimespec = dev->si_ctime;
575 sb->st_mtimespec = dev->si_mtime;
576 sb->st_uid = dev->si_uid;
577 sb->st_gid = dev->si_gid;
578 sb->st_mode = dev->si_mode | S_IFCHR;
584 ptsdev_close(struct file *fp, struct thread *td)
586 struct tty *tp = fp->f_data;
588 /* Deallocate TTY device. */
593 * Open of /dev/ptmx or /dev/ptyXX changes the type of file
594 * from DTYPE_VNODE to DTYPE_PTS. vn_open() increases vnode
595 * use count, we need to decrement it, and possibly do other
598 if (fp->f_vnode != NULL)
599 return (vnops.fo_close(fp, td));
604 static struct fileops ptsdev_ops = {
605 .fo_read = ptsdev_read,
606 .fo_write = ptsdev_write,
607 .fo_truncate = ptsdev_truncate,
608 .fo_ioctl = ptsdev_ioctl,
609 .fo_poll = ptsdev_poll,
610 .fo_kqfilter = ptsdev_kqfilter,
611 .fo_stat = ptsdev_stat,
612 .fo_close = ptsdev_close,
613 .fo_flags = DFLAG_PASSABLE,
621 ptsdrv_outwakeup(struct tty *tp)
623 struct pts_softc *psc = tty_softc(tp);
625 cv_broadcast(&psc->pts_outwait);
626 selwakeup(&psc->pts_outpoll);
627 KNOTE_LOCKED(&psc->pts_outpoll.si_note, 0);
631 ptsdrv_inwakeup(struct tty *tp)
633 struct pts_softc *psc = tty_softc(tp);
635 cv_broadcast(&psc->pts_inwait);
636 selwakeup(&psc->pts_inpoll);
637 KNOTE_LOCKED(&psc->pts_inpoll.si_note, 0);
641 ptsdrv_open(struct tty *tp)
643 struct pts_softc *psc = tty_softc(tp);
645 psc->pts_flags &= ~PTS_FINISHED;
651 ptsdrv_close(struct tty *tp)
653 struct pts_softc *psc = tty_softc(tp);
655 /* Wake up any blocked readers/writers. */
656 psc->pts_flags |= PTS_FINISHED;
657 ptsdrv_outwakeup(tp);
662 ptsdrv_pktnotify(struct tty *tp, char event)
664 struct pts_softc *psc = tty_softc(tp);
667 * Clear conflicting flags.
672 psc->pts_pkt &= ~TIOCPKT_START;
675 psc->pts_pkt &= ~TIOCPKT_STOP;
678 psc->pts_pkt &= ~TIOCPKT_DOSTOP;
681 psc->pts_pkt &= ~TIOCPKT_NOSTOP;
685 psc->pts_pkt |= event;
686 ptsdrv_outwakeup(tp);
690 ptsdrv_free(void *softc)
692 struct pts_softc *psc = softc;
694 /* Make device number available again. */
695 if (psc->pts_unit >= 0)
696 free_unr(pts_pool, psc->pts_unit);
698 chgptscnt(psc->pts_uidinfo, -1, 0);
699 uifree(psc->pts_uidinfo);
701 seldrain(&psc->pts_inpoll);
702 seldrain(&psc->pts_outpoll);
703 knlist_destroy(&psc->pts_inpoll.si_note);
704 knlist_destroy(&psc->pts_outpoll.si_note);
707 /* Destroy master device as well. */
708 if (psc->pts_cdev != NULL)
709 destroy_dev_sched(psc->pts_cdev);
710 #endif /* PTS_EXTERNAL */
715 static struct ttydevsw pts_class = {
716 .tsw_flags = TF_NOPREFIX,
717 .tsw_outwakeup = ptsdrv_outwakeup,
718 .tsw_inwakeup = ptsdrv_inwakeup,
719 .tsw_open = ptsdrv_open,
720 .tsw_close = ptsdrv_close,
721 .tsw_pktnotify = ptsdrv_pktnotify,
722 .tsw_free = ptsdrv_free,
726 pts_alloc(int fflags, struct thread *td, struct file *fp)
730 struct pts_softc *psc;
731 struct proc *p = td->td_proc;
732 struct uidinfo *uid = td->td_ucred->cr_ruidinfo;
734 /* Resource limiting. */
736 ok = chgptscnt(uid, 1, lim_cur(p, RLIMIT_NPTS));
741 /* Try to allocate a new pts unit number. */
742 unit = alloc_unr(pts_pool);
744 chgptscnt(uid, -1, 0);
747 if (unit > pts_maxdev) {
748 free_unr(pts_pool, unit);
749 chgptscnt(uid, -1, 0);
753 /* Allocate TTY and softc. */
754 psc = malloc(sizeof(struct pts_softc), M_PTS, M_WAITOK|M_ZERO);
755 cv_init(&psc->pts_inwait, "pts inwait");
756 cv_init(&psc->pts_outwait, "pts outwait");
758 psc->pts_unit = unit;
759 psc->pts_uidinfo = uid;
762 tp = tty_alloc(&pts_class, psc);
763 knlist_init_mtx(&psc->pts_inpoll.si_note, tp->t_mtx);
764 knlist_init_mtx(&psc->pts_outpoll.si_note, tp->t_mtx);
766 /* Expose the slave device as well. */
767 tty_makedev(tp, td->td_ucred, "pts/%u", psc->pts_unit);
769 finit(fp, fflags, DTYPE_PTS, tp, &ptsdev_ops);
776 pts_alloc_external(int fflags, struct thread *td, struct file *fp,
777 struct cdev *dev, const char *name)
781 struct pts_softc *psc;
782 struct proc *p = td->td_proc;
783 struct uidinfo *uid = td->td_ucred->cr_ruidinfo;
785 /* Resource limiting. */
787 ok = chgptscnt(uid, 1, lim_cur(p, RLIMIT_NPTS));
792 /* Allocate TTY and softc. */
793 psc = malloc(sizeof(struct pts_softc), M_PTS, M_WAITOK|M_ZERO);
794 cv_init(&psc->pts_inwait, "pts inwait");
795 cv_init(&psc->pts_outwait, "pts outwait");
799 psc->pts_uidinfo = uid;
802 tp = tty_alloc(&pts_class, psc);
803 knlist_init_mtx(&psc->pts_inpoll.si_note, tp->t_mtx);
804 knlist_init_mtx(&psc->pts_outpoll.si_note, tp->t_mtx);
806 /* Expose the slave device as well. */
807 tty_makedev(tp, td->td_ucred, "%s", name);
809 finit(fp, fflags, DTYPE_PTS, tp, &ptsdev_ops);
813 #endif /* PTS_EXTERNAL */
816 posix_openpt(struct thread *td, struct posix_openpt_args *uap)
822 * POSIX states it's unspecified when other flags are passed. We
825 if (uap->flags & ~(O_RDWR|O_NOCTTY))
828 error = falloc(td, &fp, &fd);
832 /* Allocate the actual pseudo-TTY. */
833 error = pts_alloc(FFLAGS(uap->flags & O_ACCMODE), td, fp);
835 fdclose(td->td_proc->p_fd, fp, fd, td);
840 /* Pass it back to userspace. */
841 td->td_retval[0] = fd;
847 #if defined(PTS_COMPAT) || defined(PTS_LINUX)
849 ptmx_fdopen(struct cdev *dev, int fflags, struct thread *td, struct file *fp)
852 return (pts_alloc(fflags & (FREAD|FWRITE), td, fp));
855 static struct cdevsw ptmx_cdevsw = {
856 .d_version = D_VERSION,
857 .d_fdopen = ptmx_fdopen,
860 #endif /* PTS_COMPAT || PTS_LINUX */
863 pts_init(void *unused)
866 pts_pool = new_unrhdr(0, INT_MAX, NULL);
867 #if defined(PTS_COMPAT) || defined(PTS_LINUX)
868 make_dev(&ptmx_cdevsw, 0, UID_ROOT, GID_WHEEL, 0666, "ptmx");
869 #endif /* PTS_COMPAT || PTS_LINUX */
872 SYSINIT(pts, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, pts_init, NULL);