30 #include <sys/cdefs.h>
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>
89 #define PTS_FINISHED 0x2
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;
117 if (uio->uio_resid == 0)
153 error =
ureadc(TIOCPKT_DATA, uio);
168 if (fp->f_flag & O_NONBLOCK) {
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;
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);
227 if (fp->f_flag & O_NONBLOCK) {
234 error = cv_wait_sig(&psc->
pts_inwait, tp->t_mtx);
239 if (uio->uio_resid == 0)
251 uio->uio_resid += iblen;
265 struct ucred *active_cred,
struct thread *td)
267 struct tty *tp = fp->f_data;
286 struct fiodgname_arg *fgn;
296 return copyout(p, fgn->buf, i);
312 *(
struct termios*)data = tp->t_termios;
326 #if defined(PTS_COMPAT) || defined(PTS_LINUX)
333 *(
unsigned int *)data = psc->
pts_unit;
339 if (tp->t_pgrp != NULL)
340 *(
int *)data = tp->t_pgrp->pg_id;
342 *(
int *)data = NO_PID;
348 if (tp->t_session == NULL)
351 *(
int *)data = tp->t_session->s_sid;
360 if (sig < 1 || sig >= NSIG)
380 error =
tty_ioctl(tp, cmd, data, fp->f_flag, td);
382 if (error == ENOIOCTL)
389 ptsdev_poll(
struct file *fp,
int events,
struct ucred *active_cred,
392 struct tty *tp = fp->f_data;
401 return ((events & (POLLIN|POLLRDNORM)) | POLLHUP);
404 if (events & (POLLIN|POLLRDNORM)) {
408 revents |= events & (POLLIN|POLLRDNORM);
410 if (events & (POLLOUT|POLLWRNORM)) {
413 revents |= events & (POLLOUT|POLLWRNORM);
428 if (events & (POLLIN|POLLRDNORM))
430 if (events & (POLLOUT|POLLWRNORM))
446 struct file *fp = kn->kn_fp;
447 struct tty *tp = fp->f_data;
456 struct file *fp = kn->kn_fp;
457 struct tty *tp = fp->f_data;
461 kn->kn_flags |= EV_EOF;
465 return (kn->kn_data > 0);
472 struct file *fp = kn->kn_fp;
473 struct tty *tp = fp->f_data;
482 struct file *fp = kn->kn_fp;
483 struct tty *tp = fp->f_data;
487 kn->kn_flags |= EV_EOF;
491 return (kn->kn_data > 0);
509 struct tty *tp = fp->f_data;
515 switch (kn->kn_filter) {
534 ptsdev_stat(
struct file *fp,
struct stat *sb,
struct ucred *active_cred,
537 struct tty *tp = fp->f_data;
541 struct cdev *dev = tp->t_dev;
552 bzero(sb,
sizeof *sb);
555 sb->st_ino = sb->st_rdev = dev2udev(psc->
pts_cdev);
558 sb->st_ino = sb->st_rdev =
tty_udev(tp);
560 sb->st_atim = dev->si_atime;
561 sb->st_ctim = dev->si_ctime;
562 sb->st_mtim = dev->si_mtime;
563 sb->st_uid = dev->si_uid;
564 sb->st_gid = dev->si_gid;
565 sb->st_mode = dev->si_mode | S_IFCHR;
573 struct tty *tp = fp->f_data;
585 if (fp->f_vnode != NULL)
586 return (
vnops.fo_close(fp, td));
602 .fo_flags = DFLAG_PASSABLE,
661 psc->
pts_pkt &= ~TIOCPKT_START;
667 psc->
pts_pkt &= ~TIOCPKT_DOSTOP;
670 psc->
pts_pkt &= ~TIOCPKT_NOSTOP;
706 .tsw_flags = TF_NOPREFIX,
719 pts_alloc(
int fflags,
struct thread *td,
struct file *fp)
724 struct proc *p = td->td_proc;
725 struct ucred *cred = td->td_ucred;
773 struct cdev *dev,
const char *
name)
778 struct proc *p = td->td_proc;
779 struct ucred *cred = td->td_ucred;
828 if (uap->flags & ~(O_RDWR|O_NOCTTY|O_CLOEXEC))
831 error =
falloc(td, &fp, &fd, uap->flags);
836 error =
pts_alloc(FFLAGS(uap->flags & O_ACCMODE), td, fp);
838 fdclose(td->td_proc->p_fd, fp, fd, td);
844 td->td_retval[0] =
fd;
static void ptsdrv_outwakeup(struct tty *tp)
int ureadc(int c, struct uio *uio)
rlim_t lim_cur(struct proc *p, int which)
int racct_add(struct proc *p, int resource, uint64_t amount)
int invfo_chmod(struct file *fp, mode_t mode, struct ucred *active_cred, struct thread *td)
static int pts_kqops_read_event(struct knote *kn, long hint)
void selwakeup(struct selinfo *sip)
void racct_sub_cred(struct ucred *cred, int resource, uint64_t amount)
static int ptsdrv_open(struct tty *tp)
SYSINIT(pts, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, pts_init, NULL)
void selrecord(struct thread *selector, struct selinfo *sip)
void * malloc(unsigned long size, struct malloc_type *mtp, int flags)
struct selinfo pts_inpoll
static int pts_kqops_write_event(struct knote *kn, long hint)
static void pts_kqops_read_detach(struct knote *kn)
void tty_rel_gone(struct tty *tp)
void knote(struct knlist *list, long hint, int lockflags)
size_t ttydisc_rint_simple(struct tty *tp, const void *buf, size_t len)
int alloc_unr(struct unrhdr *uh)
static struct filterops pts_kqops_write
int falloc(struct thread *td, struct file **resultfp, int *resultfd, int flags)
void knlist_destroy(struct knlist *knl)
void ttydisc_rint_done(struct tty *tp)
int invfo_chown(struct file *fp, uid_t uid, gid_t gid, struct ucred *active_cred, struct thread *td)
static struct ttydevsw pts_class
static int ptsdev_truncate(struct file *fp, off_t length, struct ucred *active_cred, struct thread *td)
void racct_sub(struct proc *p, int resource, uint64_t amount)
static struct filterops pts_kqops_read
struct tty * tty_alloc(struct ttydevsw *tsw, void *sc)
static int ptsdev_ioctl(struct file *fp, u_long cmd, void *data, struct ucred *active_cred, struct thread *td)
static struct fileops ptsdev_ops
dev_t tty_udev(struct tty *tp)
static int ptsdev_read(struct file *fp, struct uio *uio, struct ucred *active_cred, int flags, struct thread *td)
static MALLOC_DEFINE(M_PTS,"pts","pseudo tty device")
static void pts_kqops_write_detach(struct knote *kn)
void knlist_remove(struct knlist *knl, struct knote *kn, int islocked)
static void ptsdrv_pktnotify(struct tty *tp, char event)
void seldrain(struct selinfo *sip)
size_t ttydisc_getc_poll(struct tty *tp)
static void ptsdrv_close(struct tty *tp)
struct selinfo pts_outpoll
void crfree(struct ucred *cr)
static void pts_init(void *unused)
static int ptsdev_poll(struct file *fp, int events, struct ucred *active_cred, struct thread *td)
void knlist_add(struct knlist *knl, struct knote *kn, int islocked)
struct unrhdr * new_unrhdr(int low, int high, struct mtx *mutex)
int tty_ioctl(struct tty *tp, u_long cmd, void *data, int fflag, struct thread *td)
static void ptsdrv_free(void *softc)
void fdclose(struct filedesc *fdp, struct file *fp, int idx, struct thread *td)
void cv_init(struct cv *cvp, const char *desc)
struct ucred * crhold(struct ucred *cr)
int sys_posix_openpt(struct thread *td, struct posix_openpt_args *uap)
int uiomove(void *cp, int n, struct uio *uio)
void free(void *addr, struct malloc_type *mtp)
size_t ttydisc_rint_poll(struct tty *tp)
static int ptsdev_stat(struct file *fp, struct stat *sb, struct ucred *active_cred, struct thread *td)
void finit(struct file *fp, u_int flag, short type, void *data, struct fileops *ops)
int ttydisc_getc_uio(struct tty *tp, struct uio *uio)
void tty_makedev(struct tty *tp, struct ucred *cred, const char *fmt,...)
static int ptsdev_kqfilter(struct file *fp, struct knote *kn)
int destroy_dev_sched(struct cdev *dev)
int chgptscnt(struct uidinfo *uip, int diff, rlim_t max)
int pts_alloc_external(int fflags, struct thread *td, struct file *fp, struct cdev *dev, const char *name)
static int ptsdev_close(struct file *fp, struct thread *td)
int pts_alloc(int fflags, struct thread *td, struct file *fp)
void free_unr(struct unrhdr *uh, u_int item)
void tty_signal_pgrp(struct tty *tp, int sig)
static int ptsdev_write(struct file *fp, struct uio *uio, struct ucred *active_cred, int flags, struct thread *td)
void knlist_init_mtx(struct knlist *knl, struct mtx *lock)
static struct unrhdr * pts_pool
static void ptsdrv_inwakeup(struct tty *tp)