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