#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: tty_ptm.c,v 1.47 2025/08/05 23:56:21 gutteridge Exp $");
#ifdef _KERNEL_OPT
#include "opt_compat_netbsd.h"
#include "opt_ptm.h"
#endif
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/ioctl.h>
#include <sys/proc.h>
#include <sys/tty.h>
#include <sys/stat.h>
#include <sys/file.h>
#include <sys/uio.h>
#include <sys/kernel.h>
#include <sys/vnode.h>
#include <sys/namei.h>
#include <sys/signalvar.h>
#include <sys/filedesc.h>
#include <sys/conf.h>
#include <sys/poll.h>
#include <sys/pty.h>
#include <sys/kauth.h>
#include <sys/compat_stub.h>
#include <miscfs/specfs/specdev.h>
#include <compat/sys/ttycom.h>
#include "ioconf.h"
#ifdef DEBUG_PTM
#define DPRINTF(a) printf a
#else
#define DPRINTF(a)
#endif
#ifdef NO_DEV_PTM
const struct cdevsw ptm_cdevsw = {
.d_open = noopen,
.d_close = noclose,
.d_read = noread,
.d_write = nowrite,
.d_ioctl = noioctl,
.d_stop = nostop,
.d_tty = notty,
.d_poll = nopoll,
.d_mmap = nommap,
.d_kqfilter = nokqfilter,
.d_discard = nodiscard,
.d_flag = D_TTY
};
#else
int pts_major, ptc_major;
static dev_t pty_getfree(void);
static int pty_alloc_master(struct lwp *, int *, dev_t *, struct mount *, int);
static int pty_alloc_slave(struct lwp *, int *, dev_t, struct mount *);
static int pty_vn_open(struct vnode *, struct lwp *);
int
pty_getmp(struct lwp *l, struct mount **mpp)
{
if (ptm == NULL)
return EOPNOTSUPP;
return (*ptm->getmp)(l, mpp);
}
dev_t
pty_makedev(char ms, int minor)
{
return makedev(ms == 't' ? pts_major : ptc_major, minor);
}
static dev_t
pty_getfree(void)
{
extern kmutex_t pt_softc_mutex;
int i;
mutex_enter(&pt_softc_mutex);
for (i = 0; i < npty; i++) {
if (pty_isfree(i, 0))
break;
}
mutex_exit(&pt_softc_mutex);
return pty_makedev('t', i);
}
int
pty_vn_open(struct vnode *vp, struct lwp *l)
{
int error;
if (vp->v_type != VCHR) {
vput(vp);
return EINVAL;
}
error = VOP_OPEN(vp, FREAD|FWRITE, lwp0.l_cred);
if (error) {
KASSERT(error != EDUPFD);
KASSERT(error != EMOVEFD);
vput(vp);
return error;
}
mutex_enter(vp->v_interlock);
vp->v_writecount++;
mutex_exit(vp->v_interlock);
return 0;
}
static int
pty_alloc_master(struct lwp *l, int *fd, dev_t *dev, struct mount *mp,
int flags)
{
int error;
struct file *fp;
struct vnode *vp;
int md;
if ((error = fd_allocfile(&fp, fd)) != 0) {
DPRINTF(("fd_allocfile %d\n", error));
return error;
}
retry:
*dev = pty_getfree();
md = minor(*dev);
if ((error = pty_check(md)) != 0) {
DPRINTF(("pty_check %d\n", error));
goto bad;
}
if (ptm == NULL) {
DPRINTF(("no ptm\n"));
error = EOPNOTSUPP;
goto bad;
}
if ((error = (*ptm->allocvp)(mp, l, &vp, *dev, 'p')) != 0) {
DPRINTF(("pty_allocvp %d\n", error));
goto bad;
}
if ((error = pty_vn_open(vp, l)) != 0) {
DPRINTF(("pty_vn_open %d\n", error));
if (error != EIO)
goto bad;
error = !pty_isfree(md, 1);
DPRINTF(("pty_isfree %d\n", error));
if (error)
goto retry;
else
goto bad;
}
fp->f_flag = FREAD|FWRITE|(flags&FMASK);
fp->f_type = DTYPE_VNODE;
fp->f_ops = &vnops;
fp->f_vnode = vp;
VOP_UNLOCK(vp);
fd_set_exclose(l, *fd, (flags & O_CLOEXEC) != 0);
fd_set_foclose(l, *fd, (flags & O_CLOFORK) != 0);
fd_affix(curproc, fp, *fd);
return 0;
bad:
fd_abort(curproc, fp, *fd);
return error;
}
int
pty_grant_slave(struct lwp *l, dev_t dev, struct mount *mp)
{
int error;
struct vnode *vp;
if (ptm == NULL)
return EOPNOTSUPP;
if ((error = (*ptm->allocvp)(mp, l, &vp, dev, 't')) != 0)
return error;
if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
struct vattr vattr;
(*ptm->getvattr)(mp, l, &vattr);
error = VOP_SETATTR(vp, &vattr, lwp0.l_cred);
if (error) {
DPRINTF(("setattr %d\n", error));
vput(vp);
return error;
}
}
VOP_UNLOCK(vp);
VOP_REVOKE(vp, REVOKEALL);
vrele(vp);
return 0;
}
static int
pty_alloc_slave(struct lwp *l, int *fd, dev_t dev, struct mount *mp)
{
int error;
struct file *fp;
struct vnode *vp;
if ((error = fd_allocfile(&fp, fd)) != 0) {
DPRINTF(("fd_allocfile %d\n", error));
return error;
}
if (ptm == NULL) {
error = EOPNOTSUPP;
goto bad;
}
if ((error = (*ptm->allocvp)(mp, l, &vp, dev, 't')) != 0)
goto bad;
if ((error = pty_vn_open(vp, l)) != 0)
goto bad;
fp->f_flag = FREAD|FWRITE;
fp->f_type = DTYPE_VNODE;
fp->f_ops = &vnops;
fp->f_vnode = vp;
VOP_UNLOCK(vp);
fd_affix(curproc, fp, *fd);
return 0;
bad:
fd_abort(curproc, fp, *fd);
return error;
}
struct ptm_pty *
pty_sethandler(struct ptm_pty *nptm)
{
struct ptm_pty *optm = ptm;
ptm = nptm;
return optm;
}
int
pty_fill_ptmget(struct lwp *l, dev_t dev, int cfd, int sfd, void *data, struct mount *mp)
{
struct ptmget *ptmg = data;
int error;
if (ptm == NULL)
return EOPNOTSUPP;
ptmg->cfd = cfd == -1 ? minor(dev) : cfd;
ptmg->sfd = sfd == -1 ? minor(dev) : sfd;
error = (*ptm->makename)(mp, l, ptmg->cn, sizeof(ptmg->cn), dev, 'p');
if (error)
return error;
return (*ptm->makename)(mp, l, ptmg->sn, sizeof(ptmg->sn), dev, 't');
}
void
ptmattach(int n)
{
extern const struct cdevsw pts_cdevsw, ptc_cdevsw;
if ((pts_major = cdevsw_lookup_major(&pts_cdevsw)) == -1)
panic("%s: Can't find pty slave in cdevsw", __func__);
if ((ptc_major = cdevsw_lookup_major(&ptc_cdevsw)) == -1)
panic("%s: Can't find pty master in cdevsw", __func__);
#ifdef COMPAT_BSDPTY
ptm = &ptm_bsdpty;
#endif
}
static int
ptmopen(dev_t dev, int flag, int mode, struct lwp *l)
{
int error;
int fd;
dev_t ttydev;
struct mount *mp;
switch(minor(dev)) {
case 0:
case 2:
if ((error = pty_getmp(l, &mp)) != 0)
return error;
if ((error = pty_alloc_master(l, &fd, &ttydev, mp, flag)) != 0)
return error;
if (minor(dev) == 2) {
if ((error = pty_grant_slave(l, ttydev, mp)) != 0) {
file_t *fp = fd_getfile(fd);
if (fp != NULL) {
fd_close(fd);
}
return error;
}
}
curlwp->l_dupfd = fd;
return EMOVEFD;
case 1:
return 0;
default:
return ENODEV;
}
}
static int
ptmclose(dev_t dev, int flag, int mode, struct lwp *l)
{
return (0);
}
static int
ptmioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
{
int error;
dev_t newdev;
int cfd, sfd;
file_t *fp;
struct mount *mp;
error = 0;
switch (cmd) {
case TIOCPTMGET:
if ((error = pty_getmp(l, &mp)) != 0)
return error;
if ((error = pty_alloc_master(l, &cfd, &newdev, mp, 0)) != 0)
return error;
if ((error = pty_grant_slave(l, newdev, mp)) != 0)
goto bad;
if ((error = pty_alloc_slave(l, &sfd, newdev, mp)) != 0)
goto bad;
if ((error = pty_fill_ptmget(l, newdev, cfd, sfd, data, mp)) != 0)
goto bad2;
return 0;
default:
MODULE_HOOK_CALL(tty_ptmioctl_60_hook,
(dev, cmd, data, flag, l), EPASSTHROUGH, error);
if (error != EPASSTHROUGH)
return error;
DPRINTF(("ptmioctl EINVAL\n"));
return EINVAL;
}
bad2:
fp = fd_getfile(sfd);
if (fp != NULL) {
fd_close(sfd);
}
bad:
fp = fd_getfile(cfd);
if (fp != NULL) {
fd_close(cfd);
}
return error;
}
const struct cdevsw ptm_cdevsw = {
.d_open = ptmopen,
.d_close = ptmclose,
.d_read = noread,
.d_write = nowrite,
.d_ioctl = ptmioctl,
.d_stop = nullstop,
.d_tty = notty,
.d_poll = nopoll,
.d_mmap = nommap,
.d_kqfilter = nokqfilter,
.d_discard = nodiscard,
.d_flag = D_TTY
};
#endif