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