/*-
* Copyright (c) 2004, 2020 The NetBSD Foundation, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
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;
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);
}
/*
* Hacked up version of vn_open. We _only_ handle ptys and only open
* them with FREAD|FWRITE and never deal with creat or stuff like that.
*
* We need it because we have to fake up root credentials to open the pty.
*/
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) {
/* only ptys mean we can't get these */
KASSERT(error != EDUPFD);
KASSERT(error != EMOVEFD);
vput(vp);
return error;
}
int
pty_grant_slave(struct lwp *l, dev_t dev, struct mount *mp)
{
int error;
struct vnode *vp;
/*
* Open the slave.
* namei -> setattr -> unlock -> revoke -> vrele ->
* namei -> open -> unlock
* Three stage rocket:
* 1. Change the owner and permissions on the slave.
* 2. Revoke all the users of the slave.
* 3. open the slave.
*/
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);
/* Do the VOP_SETATTR() as root. */
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);
/*
* The vnode is useless after the revoke, we need to get it again.
*/
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;
/* Grab a filedescriptor for the slave */
if ((error = fd_allocfile(&fp, fd)) != 0) {
DPRINTF(("fd_allocfile %d\n", error));
return error;
}