44#include <sys/condvar.h>
48#include <sys/filedesc.h>
50#include <sys/kernel.h>
51#include <sys/limits.h>
52#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>
67#include <sys/ttycom.h>
71#include <machine/stdarg.h>
94#define PTS_FINISHED 0x2
114ptsdev_read(
struct file *fp,
struct uio *uio,
struct ucred *active_cred,
115 int flags,
struct thread *td)
117 struct tty *tp = fp->f_data;
122 if (uio->uio_resid == 0)
158 error =
ureadc(TIOCPKT_DATA, uio);
173 if (fp->f_flag & O_NONBLOCK) {
188ptsdev_write(
struct file *fp,
struct uio *uio,
struct ucred *active_cred,
189 int flags,
struct thread *td)
191 struct tty *tp = fp->f_data;
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);
232 if (fp->f_flag & O_NONBLOCK) {
239 error = cv_wait_sig(&psc->
pts_inwait, tp->t_mtx);
244 if (uio->uio_resid == 0)
256 uio->uio_resid += iblen;
262 struct ucred *active_cred,
struct thread *td)
264 struct tty *tp = fp->f_data;
270 *(
int *)
data = D_TTY;
286#ifdef COMPAT_FREEBSD32
290 struct fiodgname_arg *fgn;
300 return (copyout(p, fiodgname_buf_get_ptr(fgn, cmd), i));
316 *(
struct termios*)
data = tp->t_termios;
330#if defined(PTS_COMPAT) || defined(PTS_LINUX)
343 if (tp->t_pgrp != NULL)
344 *(
int *)
data = tp->t_pgrp->pg_id;
346 *(
int *)
data = NO_PID;
352 if (tp->t_session == NULL)
355 *(
int *)
data = tp->t_session->s_sid;
364 if (sig < 1 || sig >= NSIG)
386 if (error == ENOIOCTL)
393ptsdev_poll(
struct file *fp,
int events,
struct ucred *active_cred,
396 struct tty *tp = fp->f_data;
405 return ((events & (POLLIN|POLLRDNORM)) | POLLHUP);
408 if (events & (POLLIN|POLLRDNORM)) {
412 revents |= events & (POLLIN|POLLRDNORM);
414 if (events & (POLLOUT|POLLWRNORM)) {
417 revents |= events & (POLLOUT|POLLWRNORM);
432 if (events & (POLLIN|POLLRDNORM))
434 if (events & (POLLOUT|POLLWRNORM))
450 struct file *fp = kn->kn_fp;
451 struct tty *tp = fp->f_data;
460 struct file *fp = kn->kn_fp;
461 struct tty *tp = fp->f_data;
465 kn->kn_flags |= EV_EOF;
469 return (kn->kn_data > 0);
476 struct file *fp = kn->kn_fp;
477 struct tty *tp = fp->f_data;
486 struct file *fp = kn->kn_fp;
487 struct tty *tp = fp->f_data;
491 kn->kn_flags |= EV_EOF;
495 return (kn->kn_data > 0);
513 struct tty *tp = fp->f_data;
519 switch (kn->kn_filter) {
538ptsdev_stat(
struct file *fp,
struct stat *sb,
struct ucred *active_cred)
540 struct tty *tp = fp->f_data;
544 struct cdev *dev = tp->t_dev;
555 bzero(sb,
sizeof *sb);
558 sb->st_ino = sb->st_rdev = dev2udev(psc->
pts_cdev);
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;
576 struct tty *tp = fp->f_data;
588 if (fp->f_vnode != NULL)
589 return (
vnops.fo_close(fp, td));
599 kif->kf_type = KF_TYPE_PTS;
601 kif->kf_un.kf_pts.kf_pts_dev =
tty_udev(tp);
602 kif->kf_un.kf_pts.kf_pts_dev_freebsd11 =
603 kif->kf_un.kf_pts.kf_pts_dev;
604 strlcpy(kif->kf_path, tty_devname(tp),
sizeof(kif->kf_path));
621 .fo_flags = DFLAG_PASSABLE,
680 psc->
pts_pkt &= ~TIOCPKT_START;
686 psc->
pts_pkt &= ~TIOCPKT_DOSTOP;
689 psc->
pts_pkt &= ~TIOCPKT_NOSTOP;
707 racct_sub_cred(psc->
pts_cred, RACCT_NPTS, 1);
725 .tsw_flags = TF_NOPREFIX,
738pts_alloc(
int fflags,
struct thread *td,
struct file *fp)
743 struct proc *p = td->td_proc;
744 struct ucred *cred = td->td_ucred;
748 error = racct_add(p, RACCT_NPTS, 1);
755 racct_sub(p, RACCT_NPTS, 1);
764 racct_sub(p, RACCT_NPTS, 1);
782 tty_makedev(tp, td->td_ucred,
"pts/%u", psc->
pts_unit);
792 struct cdev *dev,
const char *
name)
797 struct proc *p = td->td_proc;
798 struct ucred *cred = td->td_ucred;
802 error = racct_add(p, RACCT_NPTS, 1);
809 racct_sub(p, RACCT_NPTS, 1);
829 tty_makedev(tp, td->td_ucred,
"%s",
name);
847 if (uap->flags & ~(O_RDWR|O_NOCTTY|O_CLOEXEC))
850 error = falloc(td, &fp, &
fd, uap->flags);
855 error =
pts_alloc(FFLAGS(uap->flags & O_ACCMODE), td, fp);
863 td->td_retval[0] =
fd;
void cv_init(struct cv *cvp, const char *desc)
int destroy_dev_sched(struct cdev *dev)
int invfo_truncate(struct file *fp, off_t length, struct ucred *active_cred, struct thread *td)
int invfo_chown(struct file *fp, uid_t uid, gid_t gid, struct ucred *active_cred, struct thread *td)
void fdclose(struct thread *td, struct file *fp, int idx)
void finit(struct file *fp, u_int flag, short type, void *data, struct fileops *ops)
int invfo_sendfile(struct file *fp, int sockfd, struct uio *hdr_uio, struct uio *trl_uio, off_t offset, size_t nbytes, off_t *sent, int flags, struct thread *td)
int invfo_chmod(struct file *fp, mode_t mode, struct ucred *active_cred, struct thread *td)
void knlist_remove(struct knlist *knl, struct knote *kn, int islocked)
void knlist_add(struct knlist *knl, struct knote *kn, int islocked)
void knlist_destroy(struct knlist *knl)
void knote(struct knlist *list, long hint, int lockflags)
void knlist_init_mtx(struct knlist *knl, struct mtx *lock)
void *() malloc(size_t size, struct malloc_type *mtp, int flags)
void free(void *addr, struct malloc_type *mtp)
struct ucred * crhold(struct ucred *cr)
void crfree(struct ucred *cr)
int chgptscnt(struct uidinfo *uip, int diff, rlim_t max)
rlim_t() lim_cur(struct thread *td, int which)
struct selinfo pts_outpoll
struct selinfo pts_inpoll
int uiomove(void *cp, int n, struct uio *uio)
int ureadc(int c, struct uio *uio)
int alloc_unr(struct unrhdr *uh)
struct unrhdr * new_unrhdr(int low, int high, struct mtx *mutex)
void free_unr(struct unrhdr *uh, u_int item)
void seldrain(struct selinfo *sip)
void selrecord(struct thread *selector, struct selinfo *sip)
void selwakeup(struct selinfo *sip)
dev_t tty_udev(struct tty *tp)
int tty_ioctl(struct tty *tp, u_long cmd, void *data, int fflag, struct thread *td)
void tty_signal_pgrp(struct tty *tp, int sig)
void tty_rel_gone(struct tty *tp)
struct tty * tty_alloc(struct ttydevsw *tsw, void *sc)
int pts_alloc_external(int fflags, struct thread *td, struct file *fp, struct cdev *dev, const char *name)
static void pts_kqops_write_detach(struct knote *kn)
static int ptsdev_close(struct file *fp, struct thread *td)
static int ptsdev_read(struct file *fp, struct uio *uio, struct ucred *active_cred, int flags, struct thread *td)
static struct fileops ptsdev_ops
static int ptsdev_ioctl(struct file *fp, u_long cmd, void *data, struct ucred *active_cred, struct thread *td)
static void ptsdrv_outwakeup(struct tty *tp)
static void ptsdrv_pktnotify(struct tty *tp, char event)
static struct unrhdr * pts_pool
int sys_posix_openpt(struct thread *td, struct posix_openpt_args *uap)
static struct filterops pts_kqops_read
static int ptsdrv_open(struct tty *tp)
static int ptsdev_fill_kinfo(struct file *fp, struct kinfo_file *kif, struct filedesc *fdp)
static void ptsdrv_close(struct tty *tp)
static struct ttydevsw pts_class
static MALLOC_DEFINE(M_PTS, "pts", "pseudo tty device")
SYSINIT(pts, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, pts_init, NULL)
static void ptsdrv_free(void *softc)
static struct filterops pts_kqops_write
static int pts_kqops_read_event(struct knote *kn, long hint)
static int pts_kqops_write_event(struct knote *kn, long hint)
static int ptsdev_poll(struct file *fp, int events, struct ucred *active_cred, struct thread *td)
static void pts_init(void *unused)
static int ptsdev_kqfilter(struct file *fp, struct knote *kn)
static void pts_kqops_read_detach(struct knote *kn)
static void ptsdrv_inwakeup(struct tty *tp)
static int ptsdev_write(struct file *fp, struct uio *uio, struct ucred *active_cred, int flags, struct thread *td)
static int ptsdev_stat(struct file *fp, struct stat *sb, struct ucred *active_cred)
int pts_alloc(int fflags, struct thread *td, struct file *fp)
size_t ttydisc_getc_poll(struct tty *tp)
int ttydisc_getc_uio(struct tty *tp, struct uio *uio)
size_t ttydisc_rint_poll(struct tty *tp)
void ttydisc_rint_done(struct tty *tp)
size_t ttydisc_rint_simple(struct tty *tp, const void *buf, size_t len)