Home | History | Annotate | Line # | Download | only in ptyfs
ptyfs_vnops.c revision 1.24
      1 /*	$NetBSD: ptyfs_vnops.c,v 1.24 2007/09/24 00:42:14 rumble Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1993, 1995
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Jan-Simon Pendry.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. Neither the name of the University nor the names of its contributors
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  *
     34  *	@(#)procfs_vnops.c	8.18 (Berkeley) 5/21/95
     35  */
     36 
     37 /*
     38  * Copyright (c) 1993 Jan-Simon Pendry
     39  *
     40  * This code is derived from software contributed to Berkeley by
     41  * Jan-Simon Pendry.
     42  *
     43  * Redistribution and use in source and binary forms, with or without
     44  * modification, are permitted provided that the following conditions
     45  * are met:
     46  * 1. Redistributions of source code must retain the above copyright
     47  *    notice, this list of conditions and the following disclaimer.
     48  * 2. Redistributions in binary form must reproduce the above copyright
     49  *    notice, this list of conditions and the following disclaimer in the
     50  *    documentation and/or other materials provided with the distribution.
     51  * 3. All advertising materials mentioning features or use of this software
     52  *    must display the following acknowledgement:
     53  *	This product includes software developed by the University of
     54  *	California, Berkeley and its contributors.
     55  * 4. Neither the name of the University nor the names of its contributors
     56  *    may be used to endorse or promote products derived from this software
     57  *    without specific prior written permission.
     58  *
     59  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     60  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     61  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     62  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     63  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     64  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     65  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     66  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     67  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     68  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     69  * SUCH DAMAGE.
     70  *
     71  *	@(#)procfs_vnops.c	8.18 (Berkeley) 5/21/95
     72  */
     73 
     74 /*
     75  * ptyfs vnode interface
     76  */
     77 
     78 #include <sys/cdefs.h>
     79 __KERNEL_RCSID(0, "$NetBSD: ptyfs_vnops.c,v 1.24 2007/09/24 00:42:14 rumble Exp $");
     80 
     81 #include <sys/param.h>
     82 #include <sys/systm.h>
     83 #include <sys/time.h>
     84 #include <sys/kernel.h>
     85 #include <sys/file.h>
     86 #include <sys/filedesc.h>
     87 #include <sys/proc.h>
     88 #include <sys/vnode.h>
     89 #include <sys/namei.h>
     90 #include <sys/malloc.h>
     91 #include <sys/mount.h>
     92 #include <sys/select.h>
     93 #include <sys/dirent.h>
     94 #include <sys/resourcevar.h>
     95 #include <sys/stat.h>
     96 #include <sys/conf.h>
     97 #include <sys/tty.h>
     98 #include <sys/pty.h>
     99 #include <sys/kauth.h>
    100 
    101 #include <uvm/uvm_extern.h>	/* for PAGE_SIZE */
    102 
    103 #include <machine/reg.h>
    104 
    105 #include <fs/ptyfs/ptyfs.h>
    106 #include <miscfs/genfs/genfs.h>
    107 #include <miscfs/specfs/specdev.h>
    108 
    109 MALLOC_DECLARE(M_PTYFSTMP);
    110 
    111 /*
    112  * Vnode Operations.
    113  *
    114  */
    115 
    116 int	ptyfs_lookup	(void *);
    117 #define	ptyfs_create	genfs_eopnotsupp
    118 #define	ptyfs_mknod	genfs_eopnotsupp
    119 int	ptyfs_open	(void *);
    120 int	ptyfs_close	(void *);
    121 int	ptyfs_access	(void *);
    122 int	ptyfs_getattr	(void *);
    123 int	ptyfs_setattr	(void *);
    124 int	ptyfs_read	(void *);
    125 int	ptyfs_write	(void *);
    126 #define	ptyfs_fcntl	genfs_fcntl
    127 int	ptyfs_ioctl	(void *);
    128 int	ptyfs_poll	(void *);
    129 int	ptyfs_kqfilter	(void *);
    130 #define ptyfs_revoke	genfs_revoke
    131 #define	ptyfs_mmap	genfs_eopnotsupp
    132 #define	ptyfs_fsync	genfs_nullop
    133 #define	ptyfs_seek	genfs_nullop
    134 #define	ptyfs_remove	genfs_eopnotsupp
    135 #define	ptyfs_link	genfs_abortop
    136 #define	ptyfs_rename	genfs_eopnotsupp
    137 #define	ptyfs_mkdir	genfs_eopnotsupp
    138 #define	ptyfs_rmdir	genfs_eopnotsupp
    139 #define	ptyfs_symlink	genfs_abortop
    140 int	ptyfs_readdir	(void *);
    141 #define	ptyfs_readlink	genfs_eopnotsupp
    142 #define	ptyfs_abortop	genfs_abortop
    143 int	ptyfs_reclaim	(void *);
    144 #define	ptyfs_lock	genfs_lock
    145 #define	ptyfs_unlock	genfs_unlock
    146 #define	ptyfs_bmap	genfs_badop
    147 #define	ptyfs_strategy	genfs_badop
    148 int	ptyfs_print	(void *);
    149 int	ptyfs_pathconf	(void *);
    150 #define	ptyfs_islocked	genfs_islocked
    151 #define	ptyfs_advlock	genfs_einval
    152 #define	ptyfs_bwrite	genfs_eopnotsupp
    153 #define ptyfs_putpages	genfs_null_putpages
    154 
    155 static int ptyfs_update(struct vnode *, const struct timespec *,
    156     const struct timespec *, int);
    157 static int ptyfs_chown(struct vnode *, uid_t, gid_t, kauth_cred_t,
    158     struct lwp *);
    159 static int ptyfs_chmod(struct vnode *, mode_t, kauth_cred_t, struct lwp *);
    160 static int atoi(const char *, size_t);
    161 
    162 /*
    163  * ptyfs vnode operations.
    164  */
    165 int (**ptyfs_vnodeop_p)(void *);
    166 const struct vnodeopv_entry_desc ptyfs_vnodeop_entries[] = {
    167 	{ &vop_default_desc, vn_default_error },
    168 	{ &vop_lookup_desc, ptyfs_lookup },		/* lookup */
    169 	{ &vop_create_desc, ptyfs_create },		/* create */
    170 	{ &vop_mknod_desc, ptyfs_mknod },		/* mknod */
    171 	{ &vop_open_desc, ptyfs_open },			/* open */
    172 	{ &vop_close_desc, ptyfs_close },		/* close */
    173 	{ &vop_access_desc, ptyfs_access },		/* access */
    174 	{ &vop_getattr_desc, ptyfs_getattr },		/* getattr */
    175 	{ &vop_setattr_desc, ptyfs_setattr },		/* setattr */
    176 	{ &vop_read_desc, ptyfs_read },			/* read */
    177 	{ &vop_write_desc, ptyfs_write },		/* write */
    178 	{ &vop_ioctl_desc, ptyfs_ioctl },		/* ioctl */
    179 	{ &vop_fcntl_desc, ptyfs_fcntl },		/* fcntl */
    180 	{ &vop_poll_desc, ptyfs_poll },			/* poll */
    181 	{ &vop_kqfilter_desc, ptyfs_kqfilter },		/* kqfilter */
    182 	{ &vop_revoke_desc, ptyfs_revoke },		/* revoke */
    183 	{ &vop_mmap_desc, ptyfs_mmap },			/* mmap */
    184 	{ &vop_fsync_desc, ptyfs_fsync },		/* fsync */
    185 	{ &vop_seek_desc, ptyfs_seek },			/* seek */
    186 	{ &vop_remove_desc, ptyfs_remove },		/* remove */
    187 	{ &vop_link_desc, ptyfs_link },			/* link */
    188 	{ &vop_rename_desc, ptyfs_rename },		/* rename */
    189 	{ &vop_mkdir_desc, ptyfs_mkdir },		/* mkdir */
    190 	{ &vop_rmdir_desc, ptyfs_rmdir },		/* rmdir */
    191 	{ &vop_symlink_desc, ptyfs_symlink },		/* symlink */
    192 	{ &vop_readdir_desc, ptyfs_readdir },		/* readdir */
    193 	{ &vop_readlink_desc, ptyfs_readlink },		/* readlink */
    194 	{ &vop_abortop_desc, ptyfs_abortop },		/* abortop */
    195 	{ &vop_inactive_desc, spec_inactive },		/* inactive */
    196 	{ &vop_reclaim_desc, ptyfs_reclaim },		/* reclaim */
    197 	{ &vop_lock_desc, ptyfs_lock },			/* lock */
    198 	{ &vop_unlock_desc, ptyfs_unlock },		/* unlock */
    199 	{ &vop_bmap_desc, ptyfs_bmap },			/* bmap */
    200 	{ &vop_strategy_desc, ptyfs_strategy },		/* strategy */
    201 	{ &vop_print_desc, ptyfs_print },		/* print */
    202 	{ &vop_islocked_desc, ptyfs_islocked },		/* islocked */
    203 	{ &vop_pathconf_desc, ptyfs_pathconf },		/* pathconf */
    204 	{ &vop_advlock_desc, ptyfs_advlock },		/* advlock */
    205 	{ &vop_bwrite_desc, ptyfs_bwrite },		/* bwrite */
    206 	{ &vop_putpages_desc, ptyfs_putpages },		/* putpages */
    207 	{ NULL, NULL }
    208 };
    209 const struct vnodeopv_desc ptyfs_vnodeop_opv_desc =
    210 	{ &ptyfs_vnodeop_p, ptyfs_vnodeop_entries };
    211 
    212 /*
    213  * _reclaim is called when getnewvnode()
    214  * wants to make use of an entry on the vnode
    215  * free list.  at this time the filesystem needs
    216  * to free any private data and remove the node
    217  * from any private lists.
    218  */
    219 int
    220 ptyfs_reclaim(void *v)
    221 {
    222 	struct vop_reclaim_args /* {
    223 		struct vnode *a_vp;
    224 	} */ *ap = v;
    225 	return ptyfs_freevp(ap->a_vp);
    226 }
    227 
    228 /*
    229  * Return POSIX pathconf information applicable to special devices.
    230  */
    231 int
    232 ptyfs_pathconf(void *v)
    233 {
    234 	struct vop_pathconf_args /* {
    235 		struct vnode *a_vp;
    236 		int a_name;
    237 		register_t *a_retval;
    238 	} */ *ap = v;
    239 
    240 	switch (ap->a_name) {
    241 	case _PC_LINK_MAX:
    242 		*ap->a_retval = LINK_MAX;
    243 		return 0;
    244 	case _PC_MAX_CANON:
    245 		*ap->a_retval = MAX_CANON;
    246 		return 0;
    247 	case _PC_MAX_INPUT:
    248 		*ap->a_retval = MAX_INPUT;
    249 		return 0;
    250 	case _PC_PIPE_BUF:
    251 		*ap->a_retval = PIPE_BUF;
    252 		return 0;
    253 	case _PC_CHOWN_RESTRICTED:
    254 		*ap->a_retval = 1;
    255 		return 0;
    256 	case _PC_VDISABLE:
    257 		*ap->a_retval = _POSIX_VDISABLE;
    258 		return 0;
    259 	case _PC_SYNC_IO:
    260 		*ap->a_retval = 1;
    261 		return 0;
    262 	default:
    263 		return EINVAL;
    264 	}
    265 }
    266 
    267 /*
    268  * _print is used for debugging.
    269  * just print a readable description
    270  * of (vp).
    271  */
    272 int
    273 ptyfs_print(void *v)
    274 {
    275 	struct vop_print_args /* {
    276 		struct vnode *a_vp;
    277 	} */ *ap = v;
    278 	struct ptyfsnode *ptyfs = VTOPTYFS(ap->a_vp);
    279 
    280 	printf("tag VT_PTYFS, type %d, pty %d\n",
    281 	    ptyfs->ptyfs_type, ptyfs->ptyfs_pty);
    282 	return 0;
    283 }
    284 
    285 /*
    286  * Invent attributes for ptyfsnode (vp) and store
    287  * them in (vap).
    288  * Directories lengths are returned as zero since
    289  * any real length would require the genuine size
    290  * to be computed, and nothing cares anyway.
    291  *
    292  * this is relatively minimal for ptyfs.
    293  */
    294 int
    295 ptyfs_getattr(void *v)
    296 {
    297 	struct vop_getattr_args /* {
    298 		struct vnode *a_vp;
    299 		struct vattr *a_vap;
    300 		kauth_cred_t a_cred;
    301 		struct lwp *a_l;
    302 	} */ *ap = v;
    303 	struct ptyfsnode *ptyfs = VTOPTYFS(ap->a_vp);
    304 	struct vattr *vap = ap->a_vap;
    305 
    306 	PTYFS_ITIMES(ptyfs, NULL, NULL, NULL);
    307 
    308 	/* start by zeroing out the attributes */
    309 	VATTR_NULL(vap);
    310 
    311 	/* next do all the common fields */
    312 	vap->va_type = ap->a_vp->v_type;
    313 	vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsidx.__fsid_val[0];
    314 	vap->va_fileid = ptyfs->ptyfs_fileno;
    315 	vap->va_gen = 0;
    316 	vap->va_flags = 0;
    317 	vap->va_nlink = 1;
    318 	vap->va_blocksize = PAGE_SIZE;
    319 
    320 	vap->va_atime = ptyfs->ptyfs_atime;
    321 	vap->va_mtime = ptyfs->ptyfs_mtime;
    322 	vap->va_ctime = ptyfs->ptyfs_ctime;
    323 	vap->va_birthtime = ptyfs->ptyfs_birthtime;
    324 	vap->va_mode = ptyfs->ptyfs_mode;
    325 	vap->va_flags = ptyfs->ptyfs_flags;
    326 	vap->va_uid = ptyfs->ptyfs_uid;
    327 	vap->va_gid = ptyfs->ptyfs_gid;
    328 
    329 	switch (ptyfs->ptyfs_type) {
    330 	case PTYFSpts:
    331 	case PTYFSptc:
    332 		if (pty_isfree(ptyfs->ptyfs_pty, 1))
    333 			return ENOENT;
    334 		vap->va_bytes = vap->va_size = 0;
    335 		vap->va_rdev = ap->a_vp->v_rdev;
    336 		break;
    337 	case PTYFSroot:
    338 		vap->va_rdev = 0;
    339 		vap->va_bytes = vap->va_size = DEV_BSIZE;
    340 		break;
    341 
    342 	default:
    343 		return EOPNOTSUPP;
    344 	}
    345 
    346 	return 0;
    347 }
    348 
    349 /*ARGSUSED*/
    350 int
    351 ptyfs_setattr(void *v)
    352 {
    353 	struct vop_setattr_args /* {
    354 		struct vnodeop_desc *a_desc;
    355 		struct vnode *a_vp;
    356 		struct vattr *a_vap;
    357 		kauth_cred_t a_cred;
    358 		struct lwp *a_l;
    359 	} */ *ap = v;
    360 	struct vnode *vp = ap->a_vp;
    361 	struct ptyfsnode *ptyfs = VTOPTYFS(vp);
    362 	struct vattr *vap = ap->a_vap;
    363 	kauth_cred_t cred = ap->a_cred;
    364 	struct lwp *l = ap->a_l;
    365 	int error;
    366 
    367 	if (vap->va_size != VNOVAL) {
    368  		switch (ptyfs->ptyfs_type) {
    369  		case PTYFSroot:
    370  			return EISDIR;
    371  		case PTYFSpts:
    372  		case PTYFSptc:
    373 			break;
    374 		default:
    375 			return EINVAL;
    376 		}
    377 	}
    378 
    379 	if (vap->va_flags != VNOVAL) {
    380 		if (vp->v_mount->mnt_flag & MNT_RDONLY)
    381 			return EROFS;
    382 		if (kauth_cred_geteuid(cred) != ptyfs->ptyfs_uid &&
    383 		    (error = kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER,
    384 		    NULL)) != 0)
    385 			return error;
    386 		/* Immutable and append-only flags are not supported on ptyfs. */
    387 		if (vap->va_flags & (IMMUTABLE | APPEND))
    388 			return EINVAL;
    389 		if (kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER, NULL) == 0) {
    390 			/* Snapshot flag cannot be set or cleared */
    391 			if ((vap->va_flags & SF_SNAPSHOT) !=
    392 			    (ptyfs->ptyfs_flags & SF_SNAPSHOT))
    393 				return EPERM;
    394 			ptyfs->ptyfs_flags = vap->va_flags;
    395 		} else {
    396 			if ((ptyfs->ptyfs_flags & SF_SETTABLE) !=
    397 			    (vap->va_flags & SF_SETTABLE))
    398 				return EPERM;
    399 			ptyfs->ptyfs_flags &= SF_SETTABLE;
    400 			ptyfs->ptyfs_flags |= (vap->va_flags & UF_SETTABLE);
    401 		}
    402 		ptyfs->ptyfs_flag |= PTYFS_CHANGE;
    403 	}
    404 
    405 	/*
    406 	 * Go through the fields and update iff not VNOVAL.
    407 	 */
    408 	if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
    409 		if (vp->v_mount->mnt_flag & MNT_RDONLY)
    410 			return EROFS;
    411 		if (ptyfs->ptyfs_type == PTYFSroot)
    412 			return EPERM;
    413 		error = ptyfs_chown(vp, vap->va_uid, vap->va_gid, cred, l);
    414 		if (error)
    415 			return error;
    416 	}
    417 
    418 	if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL ||
    419 	    vap->va_birthtime.tv_sec != VNOVAL) {
    420 		if (vp->v_mount->mnt_flag & MNT_RDONLY)
    421 			return EROFS;
    422 		if ((ptyfs->ptyfs_flags & SF_SNAPSHOT) != 0)
    423 			return EPERM;
    424 		if (kauth_cred_geteuid(cred) != ptyfs->ptyfs_uid &&
    425 		    (error = kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER,
    426 		    NULL)) &&
    427 		    ((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
    428 		    (error = VOP_ACCESS(vp, VWRITE, cred, l)) != 0))
    429 			return (error);
    430 		if (vap->va_atime.tv_sec != VNOVAL)
    431 			if (!(vp->v_mount->mnt_flag & MNT_NOATIME))
    432 				ptyfs->ptyfs_flag |= PTYFS_ACCESS;
    433 		if (vap->va_mtime.tv_sec != VNOVAL)
    434 			ptyfs->ptyfs_flag |= PTYFS_CHANGE | PTYFS_MODIFY;
    435 		if (vap->va_birthtime.tv_sec != VNOVAL)
    436 			ptyfs->ptyfs_birthtime = vap->va_birthtime;
    437 		ptyfs->ptyfs_flag |= PTYFS_CHANGE;
    438 		error = ptyfs_update(vp, &vap->va_atime, &vap->va_mtime, 0);
    439 		if (error)
    440 			return error;
    441 	}
    442 	if (vap->va_mode != (mode_t)VNOVAL) {
    443 		if (vp->v_mount->mnt_flag & MNT_RDONLY)
    444 			return EROFS;
    445 		if (ptyfs->ptyfs_type == PTYFSroot)
    446 			return EPERM;
    447 		if ((ptyfs->ptyfs_flags & SF_SNAPSHOT) != 0 &&
    448 		    (vap->va_mode &
    449 		    (S_IXUSR|S_IWUSR|S_IXGRP|S_IWGRP|S_IXOTH|S_IWOTH)))
    450 			return EPERM;
    451 		error = ptyfs_chmod(vp, vap->va_mode, cred, l);
    452 		if (error)
    453 			return error;
    454 	}
    455 	VN_KNOTE(vp, NOTE_ATTRIB);
    456 	return 0;
    457 }
    458 
    459 /*
    460  * Change the mode on a file.
    461  * Inode must be locked before calling.
    462  */
    463 static int
    464 ptyfs_chmod(struct vnode *vp, mode_t mode, kauth_cred_t cred, struct lwp *l)
    465 {
    466 	struct ptyfsnode *ptyfs = VTOPTYFS(vp);
    467 	int error;
    468 
    469 	if (kauth_cred_geteuid(cred) != ptyfs->ptyfs_uid &&
    470 	    (error = kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER,
    471 	    NULL)) != 0)
    472 		return error;
    473 	ptyfs->ptyfs_mode &= ~ALLPERMS;
    474 	ptyfs->ptyfs_mode |= (mode & ALLPERMS);
    475 	return 0;
    476 }
    477 
    478 /*
    479  * Perform chown operation on inode ip;
    480  * inode must be locked prior to call.
    481  */
    482 static int
    483 ptyfs_chown(struct vnode *vp, uid_t uid, gid_t gid, kauth_cred_t cred,
    484     struct lwp *l)
    485 {
    486 	struct ptyfsnode *ptyfs = VTOPTYFS(vp);
    487 	int		error, ismember = 0;
    488 
    489 	if (uid == (uid_t)VNOVAL)
    490 		uid = ptyfs->ptyfs_uid;
    491 	if (gid == (gid_t)VNOVAL)
    492 		gid = ptyfs->ptyfs_gid;
    493 	/*
    494 	 * If we don't own the file, are trying to change the owner
    495 	 * of the file, or are not a member of the target group,
    496 	 * the caller's credentials must imply super-user privilege
    497 	 * or the call fails.
    498 	 */
    499 	if ((kauth_cred_geteuid(cred) != ptyfs->ptyfs_uid || uid != ptyfs->ptyfs_uid ||
    500 	    (gid != ptyfs->ptyfs_gid &&
    501 	    !(kauth_cred_getegid(cred) == gid ||
    502 	    (kauth_cred_ismember_gid(cred, gid, &ismember) == 0 && ismember)))) &&
    503 	    ((error = kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER,
    504 	    NULL)) != 0))
    505 		return error;
    506 
    507 	ptyfs->ptyfs_gid = gid;
    508 	ptyfs->ptyfs_uid = uid;
    509 	return 0;
    510 }
    511 
    512 /*
    513  * implement access checking.
    514  *
    515  * actually, the check for super-user is slightly
    516  * broken since it will allow read access to write-only
    517  * objects.  this doesn't cause any particular trouble
    518  * but does mean that the i/o entry points need to check
    519  * that the operation really does make sense.
    520  */
    521 int
    522 ptyfs_access(void *v)
    523 {
    524 	struct vop_access_args /* {
    525 		struct vnode *a_vp;
    526 		int a_mode;
    527 		kauth_cred_t a_cred;
    528 		struct lwp *a_l;
    529 	} */ *ap = v;
    530 	struct vattr va;
    531 	int error;
    532 
    533 	if ((error = VOP_GETATTR(ap->a_vp, &va, ap->a_cred, ap->a_l)) != 0)
    534 		return error;
    535 
    536 	return vaccess(va.va_type, va.va_mode,
    537 	    va.va_uid, va.va_gid, ap->a_mode, ap->a_cred);
    538 }
    539 
    540 /*
    541  * lookup.  this is incredibly complicated in the
    542  * general case, however for most pseudo-filesystems
    543  * very little needs to be done.
    544  *
    545  * Locking isn't hard here, just poorly documented.
    546  *
    547  * If we're looking up ".", just vref the parent & return it.
    548  *
    549  * If we're looking up "..", unlock the parent, and lock "..". If everything
    550  * went ok, try to re-lock the parent. We do this to prevent lock races.
    551  *
    552  * For anything else, get the needed node.
    553  *
    554  * We try to exit with the parent locked in error cases.
    555  */
    556 int
    557 ptyfs_lookup(void *v)
    558 {
    559 	struct vop_lookup_args /* {
    560 		struct vnode * a_dvp;
    561 		struct vnode ** a_vpp;
    562 		struct componentname * a_cnp;
    563 	} */ *ap = v;
    564 	struct componentname *cnp = ap->a_cnp;
    565 	struct vnode **vpp = ap->a_vpp;
    566 	struct vnode *dvp = ap->a_dvp;
    567 	const char *pname = cnp->cn_nameptr;
    568 	struct ptyfsnode *ptyfs;
    569 	int pty, error;
    570 
    571 	*vpp = NULL;
    572 
    573 	if (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)
    574 		return EROFS;
    575 
    576 	if (cnp->cn_namelen == 1 && *pname == '.') {
    577 		*vpp = dvp;
    578 		VREF(dvp);
    579 		return 0;
    580 	}
    581 
    582 	ptyfs = VTOPTYFS(dvp);
    583 	switch (ptyfs->ptyfs_type) {
    584 	case PTYFSroot:
    585 		/*
    586 		 * Shouldn't get here with .. in the root node.
    587 		 */
    588 		if (cnp->cn_flags & ISDOTDOT)
    589 			return EIO;
    590 
    591 		pty = atoi(pname, cnp->cn_namelen);
    592 
    593 		if (pty < 0 || pty >= npty || pty_isfree(pty, 1))
    594 			break;
    595 
    596 		error = ptyfs_allocvp(dvp->v_mount, vpp, PTYFSpts, pty,
    597 		    curlwp);
    598 		return error;
    599 
    600 	default:
    601 		return ENOTDIR;
    602 	}
    603 
    604 	return cnp->cn_nameiop == LOOKUP ? ENOENT : EROFS;
    605 }
    606 
    607 /*
    608  * readdir returns directory entries from ptyfsnode (vp).
    609  *
    610  * the strategy here with ptyfs is to generate a single
    611  * directory entry at a time (struct dirent) and then
    612  * copy that out to userland using uiomove.  a more efficent
    613  * though more complex implementation, would try to minimize
    614  * the number of calls to uiomove().  for ptyfs, this is
    615  * hardly worth the added code complexity.
    616  *
    617  * this should just be done through read()
    618  */
    619 int
    620 ptyfs_readdir(void *v)
    621 {
    622 	struct vop_readdir_args /* {
    623 		struct vnode *a_vp;
    624 		struct uio *a_uio;
    625 		kauth_cred_t a_cred;
    626 		int *a_eofflag;
    627 		off_t **a_cookies;
    628 		int *a_ncookies;
    629 	} */ *ap = v;
    630 	struct uio *uio = ap->a_uio;
    631 	struct dirent *dp;
    632 	struct ptyfsnode *ptyfs;
    633 	off_t i;
    634 	int error;
    635 	off_t *cookies = NULL;
    636 	int ncookies;
    637 	struct vnode *vp;
    638 	int nc = 0;
    639 
    640 	vp = ap->a_vp;
    641 	ptyfs = VTOPTYFS(vp);
    642 
    643 	if (uio->uio_resid < UIO_MX)
    644 		return EINVAL;
    645 	if (uio->uio_offset < 0)
    646 		return EINVAL;
    647 
    648 	dp = malloc(sizeof(struct dirent), M_PTYFSTMP, M_WAITOK | M_ZERO);
    649 
    650 	error = 0;
    651 	i = uio->uio_offset;
    652 	dp->d_reclen = UIO_MX;
    653 	ncookies = uio->uio_resid / UIO_MX;
    654 
    655 	switch (ptyfs->ptyfs_type) {
    656 	case PTYFSroot: /* root */
    657 
    658 		if (i >= npty)
    659 			return 0;
    660 
    661 		if (ap->a_ncookies) {
    662 			ncookies = min(ncookies, (npty + 2 - i));
    663 			cookies = malloc(ncookies * sizeof (off_t),
    664 			    M_TEMP, M_WAITOK);
    665 			*ap->a_cookies = cookies;
    666 		}
    667 
    668 		for (; i < 2; i++) {
    669 			switch (i) {
    670 			case 0:		/* `.' */
    671 			case 1:		/* `..' */
    672 				dp->d_fileno = PTYFS_FILENO(0, PTYFSroot);
    673 				dp->d_namlen = i + 1;
    674 				(void)memcpy(dp->d_name, "..", dp->d_namlen);
    675 				dp->d_name[i + 1] = '\0';
    676 				dp->d_type = DT_DIR;
    677 				break;
    678 			}
    679 			if ((error = uiomove(dp, UIO_MX, uio)) != 0)
    680 				break;
    681 			if (cookies)
    682 				*cookies++ = i + 1;
    683 			nc++;
    684 		}
    685 		if (error) {
    686 			ncookies = nc;
    687 			break;
    688 		}
    689 		for (; uio->uio_resid >= UIO_MX && i < npty; i++) {
    690 			/* check for used ptys */
    691 			if (pty_isfree(i - 2, 1))
    692 				continue;
    693 
    694 			dp->d_fileno = PTYFS_FILENO(i - 2, PTYFSpts);
    695 			dp->d_namlen = snprintf(dp->d_name, sizeof(dp->d_name),
    696 			    "%lld", (long long)(i - 2));
    697 			dp->d_type = DT_CHR;
    698 			if ((error = uiomove(dp, UIO_MX, uio)) != 0)
    699 				break;
    700 			if (cookies)
    701 				*cookies++ = i + 1;
    702 			nc++;
    703 		}
    704 		ncookies = nc;
    705 		break;
    706 
    707 	default:
    708 		error = ENOTDIR;
    709 		break;
    710 	}
    711 
    712 	if (ap->a_ncookies) {
    713 		if (error) {
    714 			if (cookies)
    715 				free(*ap->a_cookies, M_TEMP);
    716 			*ap->a_ncookies = 0;
    717 			*ap->a_cookies = NULL;
    718 		} else
    719 			*ap->a_ncookies = ncookies;
    720 	}
    721 	uio->uio_offset = i;
    722 	free(dp, M_PTYFSTMP);
    723 	return error;
    724 }
    725 
    726 int
    727 ptyfs_open(void *v)
    728 {
    729 	struct vop_open_args /* {
    730 		struct vnode *a_vp;
    731 		int  a_mode;
    732 		kauth_cred_t a_cred;
    733 		struct lwp *a_l;
    734 	} */ *ap = v;
    735 	struct vnode *vp = ap->a_vp;
    736 	struct ptyfsnode *ptyfs = VTOPTYFS(vp);
    737 
    738 	ptyfs->ptyfs_flag |= PTYFS_CHANGE|PTYFS_ACCESS;
    739 	switch (ptyfs->ptyfs_type) {
    740 	case PTYFSpts:
    741 	case PTYFSptc:
    742 		return spec_open(v);
    743 	case PTYFSroot:
    744 		return 0;
    745 	default:
    746 		return EINVAL;
    747 	}
    748 }
    749 
    750 int
    751 ptyfs_close(void *v)
    752 {
    753 	struct vop_close_args /* {
    754 		struct vnode *a_vp;
    755 		int  a_fflag;
    756 		kauth_cred_t a_cred;
    757 		struct lwp *a_l;
    758 	} */ *ap = v;
    759 	struct vnode *vp = ap->a_vp;
    760 	struct ptyfsnode *ptyfs = VTOPTYFS(vp);
    761 
    762 	simple_lock(&vp->v_interlock);
    763 	if (vp->v_usecount > 1)
    764 		PTYFS_ITIMES(ptyfs, NULL, NULL, NULL);
    765 	simple_unlock(&vp->v_interlock);
    766 
    767 	switch (ptyfs->ptyfs_type) {
    768 	case PTYFSpts:
    769 	case PTYFSptc:
    770 		return spec_close(v);
    771 	case PTYFSroot:
    772 		return 0;
    773 	default:
    774 		return EINVAL;
    775 	}
    776 }
    777 
    778 int
    779 ptyfs_read(void *v)
    780 {
    781 	struct vop_read_args /* {
    782 		struct vnode *a_vp;
    783 		struct uio *a_uio;
    784 		int  a_ioflag;
    785 		kauth_cred_t a_cred;
    786 	} */ *ap = v;
    787 	struct timespec ts;
    788 	struct vnode *vp = ap->a_vp;
    789 	struct ptyfsnode *ptyfs = VTOPTYFS(vp);
    790 	int error;
    791 
    792 	ptyfs->ptyfs_flag |= PTYFS_ACCESS;
    793 	/* hardclock() resolution is good enough for ptyfs */
    794 	getnanotime(&ts);
    795 	(void)ptyfs_update(vp, &ts, &ts, 0);
    796 
    797 	switch (ptyfs->ptyfs_type) {
    798 	case PTYFSpts:
    799 	case PTYFSptc:
    800 		VOP_UNLOCK(vp, 0);
    801 		error = cdev_read(vp->v_rdev, ap->a_uio, ap->a_ioflag);
    802 		vn_lock(vp, LK_RETRY|LK_EXCLUSIVE);
    803 		return error;
    804 	default:
    805 		return EOPNOTSUPP;
    806 	}
    807 }
    808 
    809 int
    810 ptyfs_write(void *v)
    811 {
    812 	struct vop_write_args /* {
    813 		struct vnode *a_vp;
    814 		struct uio *a_uio;
    815 		int  a_ioflag;
    816 		kauth_cred_t a_cred;
    817 	} */ *ap = v;
    818 	struct timespec ts;
    819 	struct vnode *vp = ap->a_vp;
    820 	struct ptyfsnode *ptyfs = VTOPTYFS(vp);
    821 	int error;
    822 
    823 	ptyfs->ptyfs_flag |= PTYFS_MODIFY;
    824 	getnanotime(&ts);
    825 	(void)ptyfs_update(vp, &ts, &ts, 0);
    826 
    827 	switch (ptyfs->ptyfs_type) {
    828 	case PTYFSpts:
    829 	case PTYFSptc:
    830 		VOP_UNLOCK(vp, 0);
    831 		error = cdev_write(vp->v_rdev, ap->a_uio, ap->a_ioflag);
    832 		vn_lock(vp, LK_RETRY|LK_EXCLUSIVE);
    833 		return error;
    834 	default:
    835 		return EOPNOTSUPP;
    836 	}
    837 }
    838 
    839 int
    840 ptyfs_ioctl(void *v)
    841 {
    842 	struct vop_ioctl_args /* {
    843 		struct vnode *a_vp;
    844 		u_long a_command;
    845 		void *a_data;
    846 		int  a_fflag;
    847 		kauth_cred_t a_cred;
    848 		struct lwp *a_l;
    849 	} */ *ap = v;
    850 	struct vnode *vp = ap->a_vp;
    851 	struct ptyfsnode *ptyfs = VTOPTYFS(vp);
    852 
    853 	switch (ptyfs->ptyfs_type) {
    854 	case PTYFSpts:
    855 	case PTYFSptc:
    856 		return cdev_ioctl(vp->v_rdev, ap->a_command,
    857 		    ap->a_data, ap->a_fflag, ap->a_l);
    858 	default:
    859 		return EOPNOTSUPP;
    860 	}
    861 }
    862 
    863 int
    864 ptyfs_poll(void *v)
    865 {
    866 	struct vop_poll_args /* {
    867 		struct vnode *a_vp;
    868 		int a_events;
    869 		struct lwp *a_l;
    870 	} */ *ap = v;
    871 	struct vnode *vp = ap->a_vp;
    872 	struct ptyfsnode *ptyfs = VTOPTYFS(vp);
    873 
    874 	switch (ptyfs->ptyfs_type) {
    875 	case PTYFSpts:
    876 	case PTYFSptc:
    877 		return cdev_poll(vp->v_rdev, ap->a_events, ap->a_l);
    878 	default:
    879 		return genfs_poll(v);
    880 	}
    881 }
    882 
    883 int
    884 ptyfs_kqfilter(void *v)
    885 {
    886 	struct vop_kqfilter_args /* {
    887 		struct vnode *a_vp;
    888 		struct knote *a_kn;
    889 	} */ *ap = v;
    890 	struct vnode *vp = ap->a_vp;
    891 	struct ptyfsnode *ptyfs = VTOPTYFS(vp);
    892 
    893 	switch (ptyfs->ptyfs_type) {
    894 	case PTYFSpts:
    895 	case PTYFSptc:
    896 		return cdev_kqfilter(vp->v_rdev, ap->a_kn);
    897 	default:
    898 		return genfs_kqfilter(v);
    899 	}
    900 }
    901 
    902 static int
    903 ptyfs_update(struct vnode *vp, const struct timespec *acc,
    904     const struct timespec *mod, int flags)
    905 {
    906 	struct ptyfsnode *ptyfs = VTOPTYFS(vp);
    907 
    908 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
    909 		return 0;
    910 
    911 	PTYFS_ITIMES(ptyfs, acc, mod, NULL);
    912 	return 0;
    913 }
    914 
    915 void
    916 ptyfs_itimes(struct ptyfsnode *ptyfs, const struct timespec *acc,
    917     const struct timespec *mod, const struct timespec *cre)
    918 {
    919 	struct timespec now;
    920 
    921 	KASSERT(ptyfs->ptyfs_flag & (PTYFS_ACCESS|PTYFS_CHANGE|PTYFS_MODIFY));
    922 
    923 	getnanotime(&now);
    924 	if (ptyfs->ptyfs_flag & PTYFS_ACCESS) {
    925 		if (acc == NULL)
    926 			acc = &now;
    927 		ptyfs->ptyfs_atime = *acc;
    928 	}
    929 	if (ptyfs->ptyfs_flag & PTYFS_MODIFY) {
    930 		if (mod == NULL)
    931 			mod = &now;
    932 		ptyfs->ptyfs_mtime = *mod;
    933 	}
    934 	if (ptyfs->ptyfs_flag & PTYFS_CHANGE) {
    935 		if (cre == NULL)
    936 			cre = &now;
    937 		ptyfs->ptyfs_ctime = *cre;
    938 	}
    939 	ptyfs->ptyfs_flag &= ~(PTYFS_ACCESS|PTYFS_CHANGE|PTYFS_MODIFY);
    940 }
    941 
    942 /*
    943  * convert decimal ascii to int
    944  */
    945 static int
    946 atoi(const char *b, size_t len)
    947 {
    948 	int p = 0;
    949 
    950 	while (len--) {
    951 		char c = *b++;
    952 		if (c < '0' || c > '9')
    953 			return -1;
    954 		p = 10 * p + (c - '0');
    955 	}
    956 
    957 	return p;
    958 }
    959