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$");
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>
53 #include <sys/racct.h>
54 #include <sys/resourcevar.h>
55 #include <sys/serial.h>
57 #include <sys/syscall.h>
58 #include <sys/syscallsubr.h>
59 #include <sys/sysctl.h>
60 #include <sys/sysent.h>
61 #include <sys/sysproto.h>
62 #include <sys/systm.h>
64 #include <sys/ttycom.h>
66 #include <machine/stdarg.h>
69 * Our utmp(5) format is limited to 8-byte TTY line names. This means
70 * we can at most allocate 1000 pseudo-terminals ("pts/999"). Allow
71 * users to increase this number, assuming they have manually increased
74 static struct unrhdr *pts_pool;
76 static MALLOC_DEFINE(M_PTS, "pts", "pseudo tty device");
82 * (t) locked by tty_lock()
83 * (c) const until freeing
86 int pts_unit; /* (c) Device unit number. */
87 unsigned int pts_flags; /* (t) Device flags. */
88 #define PTS_PKT 0x1 /* Packet mode. */
89 #define PTS_FINISHED 0x2 /* Return errors on read()/write(). */
90 char pts_pkt; /* (t) Unread packet mode data. */
92 struct cv pts_inwait; /* (t) Blocking write() on master. */
93 struct selinfo pts_inpoll; /* (t) Select queue for write(). */
94 struct cv pts_outwait; /* (t) Blocking read() on master. */
95 struct selinfo pts_outpoll; /* (t) Select queue for read(). */
98 struct cdev *pts_cdev; /* (c) Master device node. */
99 #endif /* PTS_EXTERNAL */
101 struct ucred *pts_cred; /* (c) Resource limit. */
105 * Controller-side file operations.
109 ptsdev_read(struct file *fp, struct uio *uio, struct ucred *active_cred,
110 int flags, struct thread *td)
112 struct tty *tp = fp->f_data;
113 struct pts_softc *psc = tty_softc(tp);
117 if (uio->uio_resid == 0)
124 * Implement packet mode. When packet mode is turned on,
125 * the first byte contains a bitmask of events that
126 * occured (start, stop, flush, window size, etc).
128 if (psc->pts_flags & PTS_PKT && psc->pts_pkt) {
133 error = ureadc(pkt, uio);
138 * Transmit regular data.
140 * XXX: We shouldn't use ttydisc_getc_poll()! Even
141 * though in this implementation, there is likely going
142 * to be data, we should just call ttydisc_getc_uio()
143 * and use its return value to sleep.
145 if (ttydisc_getc_poll(tp)) {
146 if (psc->pts_flags & PTS_PKT) {
148 * XXX: Small race. Fortunately PTY
149 * consumers aren't multithreaded.
153 error = ureadc(TIOCPKT_DATA, uio);
159 error = ttydisc_getc_uio(tp, uio);
163 /* Maybe the device isn't used anyway. */
164 if (psc->pts_flags & PTS_FINISHED)
167 /* Wait for more data. */
168 if (fp->f_flag & O_NONBLOCK) {
172 error = cv_wait_sig(&psc->pts_outwait, tp->t_mtx);
183 ptsdev_write(struct file *fp, struct uio *uio, struct ucred *active_cred,
184 int flags, struct thread *td)
186 struct tty *tp = fp->f_data;
187 struct pts_softc *psc = tty_softc(tp);
188 char ib[256], *ibstart;
189 size_t iblen, rintlen;
192 if (uio->uio_resid == 0)
197 iblen = MIN(uio->uio_resid, sizeof ib);
198 error = uiomove(ib, iblen, uio);
207 * When possible, avoid the slow path. rint_bypass()
208 * copies all input to the input queue at once.
212 rintlen = ttydisc_rint_simple(tp, ibstart, iblen);
216 /* All data written. */
220 /* Maybe the device isn't used anyway. */
221 if (psc->pts_flags & PTS_FINISHED) {
226 /* Wait for more data. */
227 if (fp->f_flag & O_NONBLOCK) {
232 /* Wake up users on the slave side. */
233 ttydisc_rint_done(tp);
234 error = cv_wait_sig(&psc->pts_inwait, tp->t_mtx);
239 if (uio->uio_resid == 0)
244 done: ttydisc_rint_done(tp);
248 * Don't account for the part of the buffer that we couldn't
251 uio->uio_resid += iblen;
256 ptsdev_truncate(struct file *fp, off_t length, struct ucred *active_cred,
264 ptsdev_ioctl(struct file *fp, u_long cmd, void *data,
265 struct ucred *active_cred, struct thread *td)
267 struct tty *tp = fp->f_data;
268 struct pts_softc *psc = tty_softc(tp);
273 *(int *)data = D_TTY;
276 /* This device supports non-blocking operation. */
280 if (psc->pts_flags & PTS_FINISHED) {
281 /* Force read() to be called. */
284 *(int *)data = ttydisc_getc_poll(tp);
289 struct fiodgname_arg *fgn;
293 /* Reverse device name lookups, for ptsname() and ttyname(). */
299 return copyout(p, fgn->buf, i);
303 * We need to implement TIOCGPGRP and TIOCGSID here again. When
304 * called on the pseudo-terminal master, it should not check if
305 * the terminal is the foreground terminal of the calling
308 * TIOCGETA is also implemented here. Various Linux PTY routines
309 * often call isatty(), which is implemented by tcgetattr().
313 /* Obtain terminal flags through tcgetattr(). */
315 *(struct termios*)data = tp->t_termios;
318 #endif /* PTS_LINUX */
322 * We must make sure we turn tcsetattr() calls of TCSAFLUSH and
323 * TCSADRAIN into something different. If an application would
324 * call TCSAFLUSH or TCSADRAIN on the master descriptor, it may
325 * deadlock waiting for all data to be read.
329 #if defined(PTS_COMPAT) || defined(PTS_LINUX)
332 * Get the device unit number.
334 if (psc->pts_unit < 0)
336 *(unsigned int *)data = psc->pts_unit;
338 #endif /* PTS_COMPAT || PTS_LINUX */
340 /* Get the foreground process group ID. */
342 if (tp->t_pgrp != NULL)
343 *(int *)data = tp->t_pgrp->pg_id;
345 *(int *)data = NO_PID;
349 /* Get the session leader process ID. */
351 if (tp->t_session == NULL)
354 *(int *)data = tp->t_session->s_sid;
358 /* Yes, we are a pseudo-terminal master. */
361 /* Signal the foreground process group. */
363 if (sig < 1 || sig >= NSIG)
367 tty_signal_pgrp(tp, sig);
371 /* Enable/disable packet mode. */
374 psc->pts_flags |= PTS_PKT;
376 psc->pts_flags &= ~PTS_PKT;
381 /* Just redirect this ioctl to the slave device. */
383 error = tty_ioctl(tp, cmd, data, fp->f_flag, td);
385 if (error == ENOIOCTL)
392 ptsdev_poll(struct file *fp, int events, struct ucred *active_cred,
395 struct tty *tp = fp->f_data;
396 struct pts_softc *psc = tty_softc(tp);
401 if (psc->pts_flags & PTS_FINISHED) {
402 /* Slave device is not opened. */
404 return ((events & (POLLIN|POLLRDNORM)) | POLLHUP);
407 if (events & (POLLIN|POLLRDNORM)) {
408 /* See if we can getc something. */
409 if (ttydisc_getc_poll(tp) ||
410 (psc->pts_flags & PTS_PKT && psc->pts_pkt))
411 revents |= events & (POLLIN|POLLRDNORM);
413 if (events & (POLLOUT|POLLWRNORM)) {
414 /* See if we can rint something. */
415 if (ttydisc_rint_poll(tp))
416 revents |= events & (POLLOUT|POLLWRNORM);
420 * No need to check for POLLHUP here. This device cannot be used
421 * as a callout device, which means we always have a carrier,
422 * because the master is.
427 * This code might look misleading, but the naming of
428 * poll events on this side is the opposite of the slave
431 if (events & (POLLIN|POLLRDNORM))
432 selrecord(td, &psc->pts_outpoll);
433 if (events & (POLLOUT|POLLWRNORM))
434 selrecord(td, &psc->pts_inpoll);
447 pts_kqops_read_detach(struct knote *kn)
449 struct file *fp = kn->kn_fp;
450 struct tty *tp = fp->f_data;
451 struct pts_softc *psc = tty_softc(tp);
453 knlist_remove(&psc->pts_outpoll.si_note, kn, 0);
457 pts_kqops_read_event(struct knote *kn, long hint)
459 struct file *fp = kn->kn_fp;
460 struct tty *tp = fp->f_data;
461 struct pts_softc *psc = tty_softc(tp);
463 if (psc->pts_flags & PTS_FINISHED) {
464 kn->kn_flags |= EV_EOF;
467 kn->kn_data = ttydisc_getc_poll(tp);
468 return (kn->kn_data > 0);
473 pts_kqops_write_detach(struct knote *kn)
475 struct file *fp = kn->kn_fp;
476 struct tty *tp = fp->f_data;
477 struct pts_softc *psc = tty_softc(tp);
479 knlist_remove(&psc->pts_inpoll.si_note, kn, 0);
483 pts_kqops_write_event(struct knote *kn, long hint)
485 struct file *fp = kn->kn_fp;
486 struct tty *tp = fp->f_data;
487 struct pts_softc *psc = tty_softc(tp);
489 if (psc->pts_flags & PTS_FINISHED) {
490 kn->kn_flags |= EV_EOF;
493 kn->kn_data = ttydisc_rint_poll(tp);
494 return (kn->kn_data > 0);
498 static struct filterops pts_kqops_read = {
500 .f_detach = pts_kqops_read_detach,
501 .f_event = pts_kqops_read_event,
503 static struct filterops pts_kqops_write = {
505 .f_detach = pts_kqops_write_detach,
506 .f_event = pts_kqops_write_event,
510 ptsdev_kqfilter(struct file *fp, struct knote *kn)
512 struct tty *tp = fp->f_data;
513 struct pts_softc *psc = tty_softc(tp);
518 switch (kn->kn_filter) {
520 kn->kn_fop = &pts_kqops_read;
521 knlist_add(&psc->pts_outpoll.si_note, kn, 1);
524 kn->kn_fop = &pts_kqops_write;
525 knlist_add(&psc->pts_inpoll.si_note, kn, 1);
537 ptsdev_stat(struct file *fp, struct stat *sb, struct ucred *active_cred,
540 struct tty *tp = fp->f_data;
542 struct pts_softc *psc = tty_softc(tp);
543 #endif /* PTS_EXTERNAL */
544 struct cdev *dev = tp->t_dev;
547 * According to POSIX, we must implement an fstat(). This also
548 * makes this implementation compatible with Linux binaries,
549 * because Linux calls fstat() on the pseudo-terminal master to
552 * XXX: POSIX also mentions we must fill in st_dev, but how?
555 bzero(sb, sizeof *sb);
557 if (psc->pts_cdev != NULL)
558 sb->st_ino = sb->st_rdev = dev2udev(psc->pts_cdev);
560 #endif /* PTS_EXTERNAL */
561 sb->st_ino = sb->st_rdev = tty_udev(tp);
563 sb->st_atim = dev->si_atime;
564 sb->st_ctim = dev->si_ctime;
565 sb->st_mtim = dev->si_mtime;
566 sb->st_uid = dev->si_uid;
567 sb->st_gid = dev->si_gid;
568 sb->st_mode = dev->si_mode | S_IFCHR;
574 ptsdev_close(struct file *fp, struct thread *td)
576 struct tty *tp = fp->f_data;
578 /* Deallocate TTY device. */
583 * Open of /dev/ptmx or /dev/ptyXX changes the type of file
584 * from DTYPE_VNODE to DTYPE_PTS. vn_open() increases vnode
585 * use count, we need to decrement it, and possibly do other
588 if (fp->f_vnode != NULL)
589 return (vnops.fo_close(fp, td));
594 static struct fileops ptsdev_ops = {
595 .fo_read = ptsdev_read,
596 .fo_write = ptsdev_write,
597 .fo_truncate = ptsdev_truncate,
598 .fo_ioctl = ptsdev_ioctl,
599 .fo_poll = ptsdev_poll,
600 .fo_kqfilter = ptsdev_kqfilter,
601 .fo_stat = ptsdev_stat,
602 .fo_close = ptsdev_close,
603 .fo_chmod = invfo_chmod,
604 .fo_chown = invfo_chown,
605 .fo_sendfile = invfo_sendfile,
606 .fo_flags = DFLAG_PASSABLE,
614 ptsdrv_outwakeup(struct tty *tp)
616 struct pts_softc *psc = tty_softc(tp);
618 cv_broadcast(&psc->pts_outwait);
619 selwakeup(&psc->pts_outpoll);
620 KNOTE_LOCKED(&psc->pts_outpoll.si_note, 0);
624 ptsdrv_inwakeup(struct tty *tp)
626 struct pts_softc *psc = tty_softc(tp);
628 cv_broadcast(&psc->pts_inwait);
629 selwakeup(&psc->pts_inpoll);
630 KNOTE_LOCKED(&psc->pts_inpoll.si_note, 0);
634 ptsdrv_open(struct tty *tp)
636 struct pts_softc *psc = tty_softc(tp);
638 psc->pts_flags &= ~PTS_FINISHED;
644 ptsdrv_close(struct tty *tp)
646 struct pts_softc *psc = tty_softc(tp);
648 /* Wake up any blocked readers/writers. */
649 psc->pts_flags |= PTS_FINISHED;
650 ptsdrv_outwakeup(tp);
655 ptsdrv_pktnotify(struct tty *tp, char event)
657 struct pts_softc *psc = tty_softc(tp);
660 * Clear conflicting flags.
665 psc->pts_pkt &= ~TIOCPKT_START;
668 psc->pts_pkt &= ~TIOCPKT_STOP;
671 psc->pts_pkt &= ~TIOCPKT_DOSTOP;
674 psc->pts_pkt &= ~TIOCPKT_NOSTOP;
678 psc->pts_pkt |= event;
679 ptsdrv_outwakeup(tp);
683 ptsdrv_free(void *softc)
685 struct pts_softc *psc = softc;
687 /* Make device number available again. */
688 if (psc->pts_unit >= 0)
689 free_unr(pts_pool, psc->pts_unit);
691 chgptscnt(psc->pts_cred->cr_ruidinfo, -1, 0);
692 racct_sub_cred(psc->pts_cred, RACCT_NPTS, 1);
693 crfree(psc->pts_cred);
695 seldrain(&psc->pts_inpoll);
696 seldrain(&psc->pts_outpoll);
697 knlist_destroy(&psc->pts_inpoll.si_note);
698 knlist_destroy(&psc->pts_outpoll.si_note);
701 /* Destroy master device as well. */
702 if (psc->pts_cdev != NULL)
703 destroy_dev_sched(psc->pts_cdev);
704 #endif /* PTS_EXTERNAL */
709 static struct ttydevsw pts_class = {
710 .tsw_flags = TF_NOPREFIX,
711 .tsw_outwakeup = ptsdrv_outwakeup,
712 .tsw_inwakeup = ptsdrv_inwakeup,
713 .tsw_open = ptsdrv_open,
714 .tsw_close = ptsdrv_close,
715 .tsw_pktnotify = ptsdrv_pktnotify,
716 .tsw_free = ptsdrv_free,
721 #endif /* !PTS_EXTERNAL */
723 pts_alloc(int fflags, struct thread *td, struct file *fp)
727 struct pts_softc *psc;
728 struct proc *p = td->td_proc;
729 struct ucred *cred = td->td_ucred;
731 /* Resource limiting. */
733 error = racct_add(p, RACCT_NPTS, 1);
738 ok = chgptscnt(cred->cr_ruidinfo, 1, lim_cur(p, RLIMIT_NPTS));
740 racct_sub(p, RACCT_NPTS, 1);
746 /* Try to allocate a new pts unit number. */
747 unit = alloc_unr(pts_pool);
749 racct_sub(p, RACCT_NPTS, 1);
750 chgptscnt(cred->cr_ruidinfo, -1, 0);
754 /* Allocate TTY and softc. */
755 psc = malloc(sizeof(struct pts_softc), M_PTS, M_WAITOK|M_ZERO);
756 cv_init(&psc->pts_inwait, "ptsin");
757 cv_init(&psc->pts_outwait, "ptsout");
759 psc->pts_unit = unit;
760 psc->pts_cred = crhold(cred);
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 ucred *cred = td->td_ucred;
785 /* Resource limiting. */
787 error = racct_add(p, RACCT_NPTS, 1);
792 ok = chgptscnt(cred->cr_ruidinfo, 1, lim_cur(p, RLIMIT_NPTS));
794 racct_sub(p, RACCT_NPTS, 1);
800 /* Allocate TTY and softc. */
801 psc = malloc(sizeof(struct pts_softc), M_PTS, M_WAITOK|M_ZERO);
802 cv_init(&psc->pts_inwait, "ptsin");
803 cv_init(&psc->pts_outwait, "ptsout");
807 psc->pts_cred = crhold(cred);
809 tp = tty_alloc(&pts_class, psc);
810 knlist_init_mtx(&psc->pts_inpoll.si_note, tp->t_mtx);
811 knlist_init_mtx(&psc->pts_outpoll.si_note, tp->t_mtx);
813 /* Expose the slave device as well. */
814 tty_makedev(tp, td->td_ucred, "%s", name);
816 finit(fp, fflags, DTYPE_PTS, tp, &ptsdev_ops);
820 #endif /* PTS_EXTERNAL */
823 sys_posix_openpt(struct thread *td, struct posix_openpt_args *uap)
829 * POSIX states it's unspecified when other flags are passed. We
832 if (uap->flags & ~(O_RDWR|O_NOCTTY|O_CLOEXEC))
835 error = falloc(td, &fp, &fd, uap->flags);
839 /* Allocate the actual pseudo-TTY. */
840 error = pts_alloc(FFLAGS(uap->flags & O_ACCMODE), td, fp);
842 fdclose(td->td_proc->p_fd, fp, fd, td);
847 /* Pass it back to userspace. */
848 td->td_retval[0] = fd;
855 pts_init(void *unused)
858 pts_pool = new_unrhdr(0, INT_MAX, NULL);
861 SYSINIT(pts, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, pts_init, NULL);