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/resourcevar.h>
54 #include <sys/serial.h>
56 #include <sys/syscall.h>
57 #include <sys/syscallsubr.h>
58 #include <sys/sysctl.h>
59 #include <sys/sysent.h>
60 #include <sys/sysproto.h>
61 #include <sys/systm.h>
63 #include <sys/ttycom.h>
65 #include <machine/stdarg.h>
68 * Our utmp(5) format is limited to 8-byte TTY line names. This means
69 * we can at most allocate 1000 pseudo-terminals ("pts/999"). Allow
70 * users to increase this number, assuming they have manually increased
73 static struct unrhdr *pts_pool;
74 static unsigned int pts_maxdev = 999;
75 SYSCTL_UINT(_kern, OID_AUTO, pts_maxdev, CTLFLAG_RW, &pts_maxdev, 0,
76 "Maximum amount of pts(4) pseudo-terminals");
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 uidinfo *pts_uidinfo; /* (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 * occured (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_truncate(struct file *fp, off_t length, struct ucred *active_cred,
266 ptsdev_ioctl(struct file *fp, u_long cmd, void *data,
267 struct ucred *active_cred, struct thread *td)
269 struct tty *tp = fp->f_data;
270 struct pts_softc *psc = tty_softc(tp);
275 /* This device supports non-blocking operation. */
279 if (psc->pts_flags & PTS_FINISHED) {
280 /* Force read() to be called. */
283 *(int *)data = ttydisc_getc_poll(tp);
288 struct fiodgname_arg *fgn;
292 /* Reverse device name lookups, for ptsname() and ttyname(). */
298 return copyout(p, fgn->buf, i);
302 * We need to implement TIOCGPGRP and TIOCGSID here again. When
303 * called on the pseudo-terminal master, it should not check if
304 * the terminal is the foreground terminal of the calling
307 * TIOCGETA is also implemented here. Various Linux PTY routines
308 * often call isatty(), which is implemented by tcgetattr().
312 /* Obtain terminal flags through tcgetattr(). */
314 *(struct termios*)data = tp->t_termios;
317 #endif /* PTS_LINUX */
321 * We must make sure we turn tcsetattr() calls of TCSAFLUSH and
322 * TCSADRAIN into something different. If an application would
323 * call TCSAFLUSH or TCSADRAIN on the master descriptor, it may
324 * deadlock waiting for all data to be read.
328 #if defined(PTS_COMPAT) || defined(PTS_LINUX)
331 * Get the device unit number.
333 if (psc->pts_unit < 0)
335 *(unsigned int *)data = psc->pts_unit;
337 #endif /* PTS_COMPAT || PTS_LINUX */
339 /* Get the foreground process group ID. */
341 if (tp->t_pgrp != NULL)
342 *(int *)data = tp->t_pgrp->pg_id;
344 *(int *)data = NO_PID;
348 /* Get the session leader process ID. */
350 if (tp->t_session == NULL)
353 *(int *)data = tp->t_session->s_sid;
357 /* Yes, we are a pseudo-terminal master. */
360 /* Signal the foreground process group. */
362 if (sig < 1 || sig >= NSIG)
366 tty_signal_pgrp(tp, sig);
370 /* Enable/disable packet mode. */
373 psc->pts_flags |= PTS_PKT;
375 psc->pts_flags &= ~PTS_PKT;
380 /* Just redirect this ioctl to the slave device. */
382 error = tty_ioctl(tp, cmd, data, td);
384 if (error == ENOIOCTL)
391 ptsdev_poll(struct file *fp, int events, struct ucred *active_cred,
394 struct tty *tp = fp->f_data;
395 struct pts_softc *psc = tty_softc(tp);
400 if (psc->pts_flags & PTS_FINISHED) {
401 /* Slave device is not opened. */
403 return ((events & (POLLIN|POLLRDNORM)) | POLLHUP);
406 if (events & (POLLIN|POLLRDNORM)) {
407 /* See if we can getc something. */
408 if (ttydisc_getc_poll(tp) ||
409 (psc->pts_flags & PTS_PKT && psc->pts_pkt))
410 revents |= events & (POLLIN|POLLRDNORM);
412 if (events & (POLLOUT|POLLWRNORM)) {
413 /* See if we can rint something. */
414 if (ttydisc_rint_poll(tp))
415 revents |= events & (POLLOUT|POLLWRNORM);
419 * No need to check for POLLHUP here. This device cannot be used
420 * as a callout device, which means we always have a carrier,
421 * because the master is.
426 * This code might look misleading, but the naming of
427 * poll events on this side is the opposite of the slave
430 if (events & (POLLIN|POLLRDNORM))
431 selrecord(td, &psc->pts_outpoll);
432 if (events & (POLLOUT|POLLWRNORM))
433 selrecord(td, &psc->pts_inpoll);
446 pts_kqops_read_detach(struct knote *kn)
448 struct file *fp = kn->kn_fp;
449 struct tty *tp = fp->f_data;
450 struct pts_softc *psc = tty_softc(tp);
452 knlist_remove(&psc->pts_outpoll.si_note, kn, 0);
456 pts_kqops_read_event(struct knote *kn, long hint)
458 struct file *fp = kn->kn_fp;
459 struct tty *tp = fp->f_data;
460 struct pts_softc *psc = tty_softc(tp);
462 if (psc->pts_flags & PTS_FINISHED) {
463 kn->kn_flags |= EV_EOF;
466 kn->kn_data = ttydisc_getc_poll(tp);
467 return (kn->kn_data > 0);
472 pts_kqops_write_detach(struct knote *kn)
474 struct file *fp = kn->kn_fp;
475 struct tty *tp = fp->f_data;
476 struct pts_softc *psc = tty_softc(tp);
478 knlist_remove(&psc->pts_inpoll.si_note, kn, 0);
482 pts_kqops_write_event(struct knote *kn, long hint)
484 struct file *fp = kn->kn_fp;
485 struct tty *tp = fp->f_data;
486 struct pts_softc *psc = tty_softc(tp);
488 if (psc->pts_flags & PTS_FINISHED) {
489 kn->kn_flags |= EV_EOF;
492 kn->kn_data = ttydisc_rint_poll(tp);
493 return (kn->kn_data > 0);
497 static struct filterops pts_kqops_read = {
499 .f_detach = pts_kqops_read_detach,
500 .f_event = pts_kqops_read_event,
502 static struct filterops pts_kqops_write = {
504 .f_detach = pts_kqops_write_detach,
505 .f_event = pts_kqops_write_event,
509 ptsdev_kqfilter(struct file *fp, struct knote *kn)
511 struct tty *tp = fp->f_data;
512 struct pts_softc *psc = tty_softc(tp);
517 switch (kn->kn_filter) {
519 kn->kn_fop = &pts_kqops_read;
520 knlist_add(&psc->pts_outpoll.si_note, kn, 1);
523 kn->kn_fop = &pts_kqops_write;
524 knlist_add(&psc->pts_inpoll.si_note, kn, 1);
536 ptsdev_stat(struct file *fp, struct stat *sb, struct ucred *active_cred,
539 struct tty *tp = fp->f_data;
541 struct pts_softc *psc = tty_softc(tp);
542 #endif /* PTS_EXTERNAL */
543 struct cdev *dev = tp->t_dev;
546 * According to POSIX, we must implement an fstat(). This also
547 * makes this implementation compatible with Linux binaries,
548 * because Linux calls fstat() on the pseudo-terminal master to
551 * XXX: POSIX also mentions we must fill in st_dev, but how?
554 bzero(sb, sizeof *sb);
556 if (psc->pts_cdev != NULL)
557 sb->st_ino = sb->st_rdev = dev2udev(psc->pts_cdev);
559 #endif /* PTS_EXTERNAL */
560 sb->st_ino = sb->st_rdev = tty_udev(tp);
562 sb->st_atimespec = dev->si_atime;
563 sb->st_ctimespec = dev->si_ctime;
564 sb->st_mtimespec = dev->si_mtime;
565 sb->st_uid = dev->si_uid;
566 sb->st_gid = dev->si_gid;
567 sb->st_mode = dev->si_mode | S_IFCHR;
573 ptsdev_close(struct file *fp, struct thread *td)
575 struct tty *tp = fp->f_data;
577 /* Deallocate TTY device. */
584 static struct fileops ptsdev_ops = {
585 .fo_read = ptsdev_read,
586 .fo_write = ptsdev_write,
587 .fo_truncate = ptsdev_truncate,
588 .fo_ioctl = ptsdev_ioctl,
589 .fo_poll = ptsdev_poll,
590 .fo_kqfilter = ptsdev_kqfilter,
591 .fo_stat = ptsdev_stat,
592 .fo_close = ptsdev_close,
593 .fo_flags = DFLAG_PASSABLE,
601 ptsdrv_outwakeup(struct tty *tp)
603 struct pts_softc *psc = tty_softc(tp);
605 cv_broadcast(&psc->pts_outwait);
606 selwakeup(&psc->pts_outpoll);
607 KNOTE_LOCKED(&psc->pts_outpoll.si_note, 0);
611 ptsdrv_inwakeup(struct tty *tp)
613 struct pts_softc *psc = tty_softc(tp);
615 cv_broadcast(&psc->pts_inwait);
616 selwakeup(&psc->pts_inpoll);
617 KNOTE_LOCKED(&psc->pts_inpoll.si_note, 0);
621 ptsdrv_open(struct tty *tp)
623 struct pts_softc *psc = tty_softc(tp);
625 psc->pts_flags &= ~PTS_FINISHED;
631 ptsdrv_close(struct tty *tp)
633 struct pts_softc *psc = tty_softc(tp);
635 /* Wake up any blocked readers/writers. */
636 psc->pts_flags |= PTS_FINISHED;
637 ptsdrv_outwakeup(tp);
642 ptsdrv_pktnotify(struct tty *tp, char event)
644 struct pts_softc *psc = tty_softc(tp);
647 * Clear conflicting flags.
652 psc->pts_pkt &= ~TIOCPKT_START;
655 psc->pts_pkt &= ~TIOCPKT_STOP;
658 psc->pts_pkt &= ~TIOCPKT_DOSTOP;
661 psc->pts_pkt &= ~TIOCPKT_NOSTOP;
665 psc->pts_pkt |= event;
666 ptsdrv_outwakeup(tp);
670 ptsdrv_free(void *softc)
672 struct pts_softc *psc = softc;
674 /* Make device number available again. */
675 if (psc->pts_unit >= 0)
676 free_unr(pts_pool, psc->pts_unit);
678 chgptscnt(psc->pts_uidinfo, -1, 0);
679 uifree(psc->pts_uidinfo);
681 knlist_destroy(&psc->pts_inpoll.si_note);
682 knlist_destroy(&psc->pts_outpoll.si_note);
685 /* Destroy master device as well. */
686 if (psc->pts_cdev != NULL)
687 destroy_dev_sched(psc->pts_cdev);
688 #endif /* PTS_EXTERNAL */
693 static struct ttydevsw pts_class = {
694 .tsw_flags = TF_NOPREFIX,
695 .tsw_outwakeup = ptsdrv_outwakeup,
696 .tsw_inwakeup = ptsdrv_inwakeup,
697 .tsw_open = ptsdrv_open,
698 .tsw_close = ptsdrv_close,
699 .tsw_pktnotify = ptsdrv_pktnotify,
700 .tsw_free = ptsdrv_free,
705 #endif /* !PTS_EXTERNAL */
707 pts_alloc(int fflags, struct thread *td, struct file *fp)
711 struct pts_softc *psc;
712 struct proc *p = td->td_proc;
713 struct uidinfo *uid = td->td_ucred->cr_ruidinfo;
715 /* Resource limiting. */
717 ok = chgptscnt(uid, 1, lim_cur(p, RLIMIT_NPTS));
722 /* Try to allocate a new pts unit number. */
723 unit = alloc_unr(pts_pool);
725 chgptscnt(uid, -1, 0);
728 if (unit > pts_maxdev) {
729 free_unr(pts_pool, unit);
730 chgptscnt(uid, -1, 0);
734 /* Allocate TTY and softc. */
735 psc = malloc(sizeof(struct pts_softc), M_PTS, M_WAITOK|M_ZERO);
736 cv_init(&psc->pts_inwait, "pts inwait");
737 cv_init(&psc->pts_outwait, "pts outwait");
739 psc->pts_unit = unit;
740 psc->pts_uidinfo = uid;
743 tp = tty_alloc(&pts_class, psc);
744 knlist_init_mtx(&psc->pts_inpoll.si_note, tp->t_mtx);
745 knlist_init_mtx(&psc->pts_outpoll.si_note, tp->t_mtx);
747 /* Expose the slave device as well. */
748 tty_makedev(tp, td->td_ucred, "pts/%u", psc->pts_unit);
750 finit(fp, fflags, DTYPE_PTS, tp, &ptsdev_ops);
757 pts_alloc_external(int fflags, struct thread *td, struct file *fp,
758 struct cdev *dev, const char *name)
762 struct pts_softc *psc;
763 struct proc *p = td->td_proc;
764 struct uidinfo *uid = td->td_ucred->cr_ruidinfo;
766 /* Resource limiting. */
768 ok = chgptscnt(uid, 1, lim_cur(p, RLIMIT_NPTS));
773 /* Allocate TTY and softc. */
774 psc = malloc(sizeof(struct pts_softc), M_PTS, M_WAITOK|M_ZERO);
775 cv_init(&psc->pts_inwait, "pts inwait");
776 cv_init(&psc->pts_outwait, "pts outwait");
780 psc->pts_uidinfo = uid;
783 tp = tty_alloc(&pts_class, psc);
784 knlist_init_mtx(&psc->pts_inpoll.si_note, tp->t_mtx);
785 knlist_init_mtx(&psc->pts_outpoll.si_note, tp->t_mtx);
787 /* Expose the slave device as well. */
788 tty_makedev(tp, td->td_ucred, "%s", name);
790 finit(fp, fflags, DTYPE_PTS, tp, &ptsdev_ops);
794 #endif /* PTS_EXTERNAL */
797 posix_openpt(struct thread *td, struct posix_openpt_args *uap)
803 * POSIX states it's unspecified when other flags are passed. We
806 if (uap->flags & ~(O_RDWR|O_NOCTTY))
809 error = falloc(td, &fp, &fd);
813 /* Allocate the actual pseudo-TTY. */
814 error = pts_alloc(FFLAGS(uap->flags & O_ACCMODE), td, fp);
816 fdclose(td->td_proc->p_fd, fp, fd, td);
820 /* Pass it back to userspace. */
821 td->td_retval[0] = fd;
828 pts_init(void *unused)
831 pts_pool = new_unrhdr(0, INT_MAX, NULL);
834 SYSINIT(pts, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, pts_init, NULL);