xref: /xnu-12377.61.12/bsd/miscfs/devfs/devfs_fdesc_support.c (revision 4d495c6e23c53686cf65f45067f79024cf5dcee8)
1 /*
2  * Copyright (c) 2000-2019 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
29 /*
30  * Copyright (c) 1992, 1993
31  *	The Regents of the University of California.  All rights reserved.
32  *
33  * This code is derived from software donated to Berkeley by
34  * Jan-Simon Pendry.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  * 1. Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  * 2. Redistributions in binary form must reproduce the above copyright
42  *    notice, this list of conditions and the following disclaimer in the
43  *    documentation and/or other materials provided with the distribution.
44  * 3. All advertising materials mentioning features or use of this software
45  *    must display the following acknowledgement:
46  *	This product includes software developed by the University of
47  *	California, Berkeley and its contributors.
48  * 4. Neither the name of the University nor the names of its contributors
49  *    may be used to endorse or promote products derived from this software
50  *    without specific prior written permission.
51  *
52  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62  * SUCH DAMAGE.
63  *
64  *	@(#)fdesc_vnops.c	8.17 (Berkeley) 5/22/95
65  *
66  */
67 
68 /*
69  * /dev/fd Filesystem
70  */
71 
72 #include <sys/param.h>
73 #include <sys/systm.h>
74 #include <sys/types.h>
75 #include <sys/time.h>
76 #include <sys/proc_internal.h>
77 #include <sys/kernel.h> /* boottime */
78 #include <sys/resourcevar.h>
79 #include <sys/filedesc.h>
80 #include <sys/kauth.h>
81 #include <sys/vnode_internal.h>
82 #include <sys/malloc.h>
83 #include <sys/file_internal.h>
84 #include <sys/stat.h>
85 #include <sys/mount_internal.h>
86 #include <sys/namei.h>
87 #include <sys/dirent.h>
88 #include <sys/ubc.h>
89 #include <sys/socketvar.h>
90 #include <sys/pipe.h>
91 #include <sys/uio_internal.h>
92 #include <vfs/vfs_support.h>
93 #include <pexpert/pexpert.h>
94 #include <miscfs/devfs/fdesc.h>
95 #include <miscfs/devfs/devfs.h>
96 #include <miscfs/devfs/devfsdefs.h>
97 
98 #define FDL_WANT        0x01
99 #define FDL_LOCKED      0x02
100 static int fdcache_lock;
101 
102 
103 #if (FD_STDIN != FD_STDOUT - 1) || (FD_STDOUT != FD_STDERR - 1)
104 FD_STDIN, FD_STDOUT, FD_STDERR must be a sequence n, n + 1, n + 2
105 #endif
106 
107 #define NFDCACHE 3
108 
109 #define FD_NHASH(ix) \
110 	(&fdhashtbl[(ix) & fdhash])
111 LIST_HEAD(fdhashhead, fdescnode) * fdhashtbl;
112 u_long fdhash;
113 
114 static int fdesc_attr(int fd, struct vnode_attr *vap, vfs_context_t a_context);
115 
116 static LCK_GRP_DECLARE(fdesc_lckgrp, "fdesc");
117 static LCK_MTX_DECLARE(fdesc_mtx, &fdesc_lckgrp);
118 
119 static void
fdesc_lock(void)120 fdesc_lock(void)
121 {
122 	lck_mtx_lock(&fdesc_mtx);
123 }
124 
125 static void
fdesc_unlock(void)126 fdesc_unlock(void)
127 {
128 	lck_mtx_unlock(&fdesc_mtx);
129 }
130 
131 
132 /*
133  * Initialise cache headers, create the devfs node
134  */
135 int
devfs_fdesc_init()136 devfs_fdesc_init()
137 {
138 	int error = 0;
139 	devnode_t *rootdir = dev_root->de_dnp;
140 	devdirent_t *direntp;
141 
142 	/* XXX Make sure you have the right path... */
143 	fdhashtbl = hashinit(NFDCACHE, M_CACHE, &fdhash);
144 
145 	DEVFS_LOCK();
146 	dev_add_entry("fd", rootdir, DEV_DEVFD, NULL, NULL, NULL, &direntp);
147 	devfs_fdesc_makelinks();
148 	DEVFS_UNLOCK();
149 
150 	return error;
151 }
152 
153 /*
154  * Called during early startup, no need to synchronize
155  */
156 int
devfs_fdesc_makelinks()157 devfs_fdesc_makelinks()
158 {
159 	int error = 0;
160 	devdirent_t *stdin_ent = NULL, *stdout_ent = NULL, *stderr_ent = NULL;
161 	devnode_t *root_devnode = dev_root->de_dnp;
162 
163 	/* We do this ugliness to get around some "const" warnings */
164 	char in[] = "stdin";
165 	char out[] = "stdout";
166 	char err[] = "stderr";
167 	char zero[] = "fd/0";
168 	char one[] = "fd/1";
169 	char two[] = "fd/2";
170 
171 	if ((error = devfs_make_symlink(root_devnode, in, 0555, zero, &stdin_ent))) {
172 		printf("Couldn't make stdin, err %d.\n", error);
173 		goto bad;
174 	}
175 
176 	if ((error = devfs_make_symlink(root_devnode, out, 0555, one, &stdout_ent))) {
177 		printf("Couldn't make stdout, err %d.\n", error);
178 		goto bad;
179 	}
180 
181 	if ((error = devfs_make_symlink(root_devnode, err, 0555, two, &stderr_ent))) {
182 		printf("Couldn't make stderr, err %d.\n", error);
183 		goto bad;
184 	}
185 
186 	return 0;
187 
188 bad:
189 	if (stdin_ent) {
190 		dev_free_name(stdin_ent);
191 	}
192 	if (stdout_ent) {
193 		dev_free_name(stdout_ent);
194 	}
195 	if (stderr_ent) {
196 		dev_free_name(stderr_ent);
197 	}
198 
199 	return error;
200 }
201 
202 int
fdesc_allocvp(fdntype ftype,int ix,struct mount * mp,struct vnode ** vpp,enum vtype vtype,int fdno)203 fdesc_allocvp(fdntype ftype, int ix, struct mount *mp, struct vnode **vpp, enum vtype vtype, int fdno)
204 {
205 	struct fdhashhead *fc;
206 	struct fdescnode *fd;
207 	int error = 0;
208 	int vid = 0;
209 	struct vnode_fsparam vfsp;
210 	struct vnode *vp;
211 
212 	fdesc_lock();
213 
214 	fc = FD_NHASH(ix);
215 loop:
216 	for (fd = fc->lh_first; fd != 0; fd = fd->fd_hash.le_next) {
217 		if (fd->fd_ix == ix && vnode_mount(fd->fd_vnode) == mp) {
218 			vid = vnode_vid(fd->fd_vnode);
219 			vp = fd->fd_vnode;
220 			vnode_hold(vp);
221 			fdesc_unlock();
222 
223 			if (vnode_getwithvid(vp, vid)) {
224 				vnode_drop(vp);
225 				fdesc_lock();
226 				goto loop;
227 			}
228 
229 			vnode_drop(vp);
230 			*vpp = vp;
231 			(*vpp)->v_type = (uint16_t)vtype;
232 
233 			return error;
234 		}
235 	}
236 
237 	/* Only one thread can add to the hash at a time */
238 	if (fdcache_lock & FDL_LOCKED) {
239 		fdcache_lock |= FDL_WANT;
240 		msleep((caddr_t) &fdcache_lock, &fdesc_mtx, PINOD, "fdesc_allocvp", NULL);
241 		goto loop;
242 	}
243 
244 	fdcache_lock |= FDL_LOCKED;
245 	fdesc_unlock();
246 
247 	fd = kalloc_type(struct fdescnode, Z_WAITOK);
248 
249 	vfsp.vnfs_mp = mp;
250 	vfsp.vnfs_vtype = vtype;
251 	vfsp.vnfs_str = "fdesc";
252 	vfsp.vnfs_dvp = NULL;
253 	vfsp.vnfs_fsnode = fd;
254 	vfsp.vnfs_cnp = NULL;
255 	vfsp.vnfs_vops = fdesc_vnodeop_p;
256 	vfsp.vnfs_rdev = 0;
257 	vfsp.vnfs_filesize = 0;
258 	vfsp.vnfs_flags = VNFS_NOCACHE | VNFS_CANTCACHE;
259 	vfsp.vnfs_marksystem = 0;
260 	vfsp.vnfs_markroot = 0;
261 
262 	error = vnode_create_ext(VNCREATE_FLAVOR, VCREATESIZE, &vfsp, vpp,
263 	    VNODE_CREATE_DEFAULT);
264 	if (error) {
265 		kfree_type(struct fdescnode, fd);
266 		fdesc_lock();
267 		goto out;
268 	}
269 
270 	(*vpp)->v_tag = VT_FDESC;
271 	fd->fd_vnode = *vpp;
272 	fd->fd_type = ftype;
273 	fd->fd_fd = fdno;
274 	fd->fd_link = NULL;
275 	fd->fd_ix = ix;
276 
277 	fdesc_lock();
278 
279 	LIST_INSERT_HEAD(fc, fd, fd_hash);
280 out:
281 	/* Hold the lock when we get here */
282 	fdcache_lock &= ~FDL_LOCKED;
283 
284 	if (fdcache_lock & FDL_WANT) {
285 		fdcache_lock &= ~FDL_WANT;
286 		wakeup((caddr_t) &fdcache_lock);
287 	}
288 
289 	fdesc_unlock();
290 
291 	return error;
292 }
293 
294 /*
295  * vp is the current namei directory
296  * ndp is the name to locate in that directory...
297  *
298  * This vnop should only be called on the special directory /dev/fd.
299  */
300 int
devfs_devfd_lookup(struct vnop_lookup_args * ap)301 devfs_devfd_lookup(struct vnop_lookup_args *ap)
302 {
303 	struct vnode **vpp = ap->a_vpp;
304 	struct vnode *dvp = ap->a_dvp;
305 	struct componentname *cnp = ap->a_cnp;
306 	char *pname = cnp->cn_nameptr;
307 	struct proc *p = vfs_context_proc(ap->a_context);
308 	int fd;
309 	int error;
310 	struct vnode *fvp;
311 
312 	if (cnp->cn_namelen == 1 && *pname == '.') {
313 		*vpp = dvp;
314 
315 		if ((error = vnode_get(dvp))) {
316 			goto bad;
317 		}
318 		return 0;
319 	}
320 
321 	fd = 0;
322 	while (*pname >= '0' && *pname <= '9') {
323 		if (os_mul_and_add_overflow(fd, 10, *pname++ - '0', &fd)) {
324 			break;
325 		}
326 	}
327 
328 	if (*pname != '\0') {
329 		error = ENOENT;
330 		goto bad;
331 	}
332 
333 	proc_fdlock_spin(p);
334 	if (fd < 0 || fd >= p->p_fd.fd_nfiles ||
335 	    *fdfile(p, fd) == NULL ||
336 	    (*fdflags(p, fd) & UF_RESERVED)) {
337 		proc_fdunlock(p);
338 		error = EBADF;
339 		goto bad;
340 	}
341 	proc_fdunlock(p);
342 
343 	/*
344 	 * Don't allow mount/unmount operations on fdesc vnodes
345 	 */
346 	if (cnp->cn_ndp->ni_op == OP_MOUNT || cnp->cn_ndp->ni_op == OP_UNMOUNT) {
347 		error = ENOTSUP;
348 		goto bad;
349 	}
350 
351 	/*
352 	 * For OP_SETATTR lookups, resolve the backing vnode now so we do
353 	 * MACF checks on the right thing.
354 	 *
355 	 * If there is no vnode (i.e. something else is open at that fd) then
356 	 * just return an fdescnode.
357 	 */
358 	if ((cnp->cn_ndp->ni_op == OP_SETATTR) &&
359 	    (0 == vnode_getfromfd(ap->a_context, fd, &fvp))) {
360 		cnp->cn_flags &= ~MAKEENTRY;
361 		*vpp = fvp;
362 		return 0;
363 	}
364 
365 	error = fdesc_allocvp(Fdesc, FD_DESC + fd, dvp->v_mount, &fvp, VNON, fd);
366 	if (error) {
367 		goto bad;
368 	}
369 	*vpp = fvp;
370 	return 0;
371 
372 bad:
373 	*vpp = NULL;
374 	return error;
375 }
376 
377 int
fdesc_open(struct vnop_open_args * ap)378 fdesc_open(struct vnop_open_args *ap)
379 {
380 	struct vnode *vp = ap->a_vp;
381 	thread_t thr = vfs_context_thread(ap->a_context);
382 	uthread_t uu;
383 	int error = 0;
384 
385 	if (thr == NULL) {
386 		return EINVAL;
387 	}
388 
389 	uu = get_bsdthread_info(thr);
390 
391 	switch (VTOFDESC(vp)->fd_type) {
392 	case Fdesc:
393 		/*
394 		 * XXX Kludge: set uu->uu_dupfd to contain the value of the
395 		 * the file descriptor being sought for duplication. The error
396 		 * return ensures that the vnode for this device will be
397 		 * released by vn_open. Open will detect this special error and
398 		 * take the actions in dupfdopen.  Other callers of vn_open or
399 		 * vnop_open will simply report the error.
400 		 */
401 		uu->uu_dupfd = VTOFDESC(vp)->fd_fd;     /* XXX */
402 		error = ENODEV;
403 		break;
404 	default:
405 		panic("Invalid type for fdesc node!");
406 		break;
407 	}
408 
409 	return error;
410 }
411 
412 static int
fdesc_attr(int fd,struct vnode_attr * vap,vfs_context_t a_context)413 fdesc_attr(int fd, struct vnode_attr *vap, vfs_context_t a_context)
414 {
415 	struct fileproc *fp;
416 	struct proc *p = vfs_context_proc(a_context);
417 	struct stat stb;
418 	int error;
419 
420 	if ((error = fp_lookup(p, fd, &fp, 0))) {
421 		return error;
422 	}
423 	switch (FILEGLOB_DTYPE(fp->fp_glob)) {
424 	case DTYPE_VNODE: {
425 		vnode_t vp = (vnode_t)fp_get_data(fp);
426 
427 		if ((error = vnode_getwithref(vp)) != 0) {
428 			break;
429 		}
430 		if ((error = vnode_authorize(vp,
431 		    NULL,
432 		    KAUTH_VNODE_READ_ATTRIBUTES | KAUTH_VNODE_READ_SECURITY,
433 		    a_context)) == 0) {
434 			error = vnode_getattr(vp, vap, a_context);
435 		}
436 		if (error == 0 && vap->va_type == VDIR) {
437 			/*
438 			 * directories can cause loops in the namespace,
439 			 * so turn off the 'x' bits to avoid trouble.
440 			 *
441 			 * XXX ACLs break this, of course
442 			 */
443 			vap->va_mode &= ~((VEXEC) | (VEXEC >> 3) | (VEXEC >> 6));
444 		}
445 		(void)vnode_put(vp);
446 		break;
447 	}
448 	case DTYPE_SOCKET:
449 	case DTYPE_PIPE:
450 #if SOCKETS
451 		if (FILEGLOB_DTYPE(fp->fp_glob) == DTYPE_SOCKET) {
452 			error = soo_stat((struct socket *)fp_get_data(fp), (void *)&stb, 0);
453 		} else
454 #endif /* SOCKETS */
455 		error = pipe_stat((struct pipe *)fp_get_data(fp), (void *)&stb, 0);
456 
457 		if (error == 0) {
458 			if (FILEGLOB_DTYPE(fp->fp_glob) == DTYPE_SOCKET) {
459 				VATTR_RETURN(vap, va_type, VSOCK);
460 			} else {
461 				VATTR_RETURN(vap, va_type, VFIFO);
462 			}
463 
464 			VATTR_RETURN(vap, va_mode, stb.st_mode);
465 			VATTR_RETURN(vap, va_nlink, stb.st_nlink);
466 			VATTR_RETURN(vap, va_uid, stb.st_uid);
467 			VATTR_RETURN(vap, va_gid, stb.st_gid);
468 			VATTR_RETURN(vap, va_fsid, stb.st_dev);
469 			VATTR_RETURN(vap, va_fileid, stb.st_ino);
470 			VATTR_RETURN(vap, va_data_size, stb.st_size);
471 			VATTR_RETURN(vap, va_access_time, stb.st_atimespec);
472 			VATTR_RETURN(vap, va_modify_time, stb.st_mtimespec);
473 			VATTR_RETURN(vap, va_change_time, stb.st_ctimespec);
474 			VATTR_RETURN(vap, va_gen, stb.st_gen);
475 			VATTR_RETURN(vap, va_flags, stb.st_flags);
476 			VATTR_RETURN(vap, va_rdev, stb.st_rdev);
477 			VATTR_RETURN(vap, va_total_alloc, stb.st_blocks * stb.st_blksize);
478 			VATTR_RETURN(vap, va_acl, NULL);
479 		}
480 		break;
481 
482 	default:
483 		error = EBADF;
484 	}
485 
486 	/* Update 'va_mode' to take into account the bits on the fd. */
487 	if (error == 0 && VATTR_IS_SUPPORTED(vap, va_mode)) {
488 		int flags;
489 
490 		flags = fp->fp_glob->fg_flag;
491 		if (!(flags & FREAD)) {
492 			vap->va_mode &= ~(S_IRUSR | S_IRGRP | S_IROTH);
493 		}
494 		if (!(flags & FWRITE)) {
495 			vap->va_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
496 		}
497 		if (!(flags & O_EXEC)) {
498 			vap->va_mode &= ~(S_IXUSR | S_IXGRP | S_IXOTH);
499 		}
500 	}
501 
502 	fp_drop(p, fd, fp, 0);
503 	return error;
504 }
505 
506 int
fdesc_getattr(struct vnop_getattr_args * ap)507 fdesc_getattr(struct vnop_getattr_args *ap)
508 {
509 	struct vnode *vp = ap->a_vp;
510 	struct vnode_attr *vap = ap->a_vap;
511 	unsigned fd;
512 	int error = 0;
513 
514 	switch (VTOFDESC(vp)->fd_type) {
515 	case Fdesc:
516 		fd = VTOFDESC(vp)->fd_fd;
517 		error = fdesc_attr(fd, vap, ap->a_context);
518 		break;
519 
520 	default:
521 		panic("Invalid type for an fdesc node!");
522 		break;
523 	}
524 
525 	/*
526 	 * Yes, we do this without locking, but this value is always just
527 	 * a snapshot.
528 	 */
529 	if (error == 0) {
530 		vp->v_type = (uint16_t)vap->va_type;
531 
532 		/* We need an inactive to reset type to VNON */
533 		vnode_setneedinactive(vp);
534 	}
535 
536 	return error;
537 }
538 
539 int
fdesc_setattr(struct vnop_setattr_args * ap)540 fdesc_setattr(struct vnop_setattr_args *ap)
541 {
542 	struct fileproc *fp;
543 	unsigned fd;
544 	int error;
545 	struct proc * p = vfs_context_proc(ap->a_context);
546 
547 	/*
548 	 * Can't mess with the root vnode
549 	 */
550 	switch (VTOFDESC(ap->a_vp)->fd_type) {
551 	case Fdesc:
552 		break;
553 	default:
554 		panic("Invalid type for an fdesc node!");
555 		return EACCES;
556 	}
557 
558 	fd = VTOFDESC(ap->a_vp)->fd_fd;
559 	if ((error = fp_lookup(vfs_context_proc(ap->a_context), fd, &fp, 0))) {
560 		return error;
561 	}
562 
563 	switch (FILEGLOB_DTYPE(fp->fp_glob)) {
564 	case DTYPE_VNODE:
565 	/*
566 	 * We shouldn't get here unless we were raced with close/open.
567 	 * In that case, shenanigans are happening and it's unsafe to
568 	 * take action on the `setattr`; the MACF and UNIX permission
569 	 * checks are no longer valid (TOCTOU).
570 	 *
571 	 * Fall through to report success.
572 	 */
573 	case DTYPE_SOCKET:
574 	case DTYPE_PIPE:
575 		error = 0;
576 		break;
577 
578 	default:
579 		error = EBADF;
580 		break;
581 	}
582 
583 	fp_drop(p, fd, fp, 0);
584 	return error;
585 }
586 
587 #define UIO_MX 16
588 
589 /*
590  *  static struct dirtmp {
591  *       u_int32_t d_fileno;
592  *       u_short d_reclen;
593  *       u_short d_namlen;
594  *       char d_name[8];
595  *  } rootent[] = {
596  *       { FD_DEVFD, UIO_MX, 2, "fd" },
597  *       { FD_STDIN, UIO_MX, 5, "stdin" },
598  *       { FD_STDOUT, UIO_MX, 6, "stdout" },
599  *       { FD_STDERR, UIO_MX, 6, "stderr" },
600  *       { 0, 0, 0, "" }
601  *  };
602  */
603 
604 /* Only called on /dev/fd */
605 int
devfs_devfd_readdir(struct vnop_readdir_args * ap)606 devfs_devfd_readdir(struct vnop_readdir_args *ap)
607 {
608 	struct uio *uio = ap->a_uio;
609 	struct proc *p = current_proc();
610 	off_t i;
611 	int error;
612 	bool proc_fd_locked = false;
613 
614 	/*
615 	 * We don't allow exporting fdesc mounts, and currently local
616 	 * requests do not need cookies.
617 	 */
618 	if (ap->a_flags & (VNODE_READDIR_EXTENDED | VNODE_READDIR_REQSEEKOFF)) {
619 		return EINVAL;
620 	}
621 
622 	/*
623 	 * There needs to be space for at least one entry.
624 	 */
625 	if (uio_resid(uio) < UIO_MX) {
626 		return EINVAL;
627 	}
628 
629 	i = uio->uio_offset / UIO_MX;
630 	error = 0;
631 	while (uio_resid(uio) >= UIO_MX) {
632 		proc_fdlock(p);
633 		if (i >= p->p_fd.fd_nfiles || i < 0) {
634 			proc_fdunlock(p);
635 			break;
636 		}
637 		proc_fd_locked = true;
638 
639 		if (*fdfile(p, i) != NULL && !(*fdflags(p, i) & UF_RESERVED)) {
640 			struct dirent d;
641 			struct dirent *dp = &d;
642 
643 			proc_fdunlock(p);
644 			proc_fd_locked = false;
645 
646 			bzero((caddr_t) dp, UIO_MX);
647 
648 			dp->d_namlen = (__uint8_t)scnprintf(dp->d_name, sizeof(dp->d_name),
649 			    "%lld", i);
650 			dp->d_reclen = UIO_MX;
651 			dp->d_type = DT_UNKNOWN;
652 			dp->d_fileno = (ino_t)i + FD_STDIN;
653 			/*
654 			 * And ship to userland
655 			 */
656 			error = uiomove((caddr_t) dp, UIO_MX, uio);
657 			if (error) {
658 				break;
659 			}
660 		}
661 
662 		if (proc_fd_locked) {
663 			proc_fdunlock(p);
664 			proc_fd_locked = false;
665 		}
666 		i++;
667 	}
668 
669 	uio->uio_offset = i * UIO_MX;
670 	return error;
671 }
672 
673 int
fdesc_read(__unused struct vnop_read_args * ap)674 fdesc_read(__unused struct vnop_read_args *ap)
675 {
676 	return ENOTSUP;
677 }
678 
679 int
fdesc_write(__unused struct vnop_write_args * ap)680 fdesc_write(__unused struct vnop_write_args *ap)
681 {
682 	return ENOTSUP;
683 }
684 
685 int
fdesc_ioctl(__unused struct vnop_ioctl_args * ap)686 fdesc_ioctl(__unused struct vnop_ioctl_args *ap)
687 {
688 	return ENOTSUP;
689 }
690 
691 int
fdesc_select(__unused struct vnop_select_args * ap)692 fdesc_select(__unused struct vnop_select_args *ap)
693 {
694 	return ENOTSUP;
695 }
696 
697 int
fdesc_inactive(struct vnop_inactive_args * ap)698 fdesc_inactive(struct vnop_inactive_args *ap)
699 {
700 	struct vnode *vp = ap->a_vp;
701 
702 	/*
703 	 * Clear out the v_type field to avoid
704 	 * nasty things happening in vgone().
705 	 */
706 	vp->v_type = VNON;
707 
708 	return 0;
709 }
710 
711 int
fdesc_reclaim(struct vnop_reclaim_args * ap)712 fdesc_reclaim(struct vnop_reclaim_args *ap)
713 {
714 	struct vnode *vp = ap->a_vp;
715 	struct fdescnode *fd = VTOFDESC(vp);
716 
717 	fdesc_lock();
718 
719 	LIST_REMOVE(fd, fd_hash);
720 	kfree_type(struct fdescnode, vp->v_data);
721 
722 	fdesc_unlock();
723 
724 	return 0;
725 }
726 
727 /*
728  * Return POSIX pathconf information applicable to special devices.
729  */
730 int
fdesc_pathconf(struct vnop_pathconf_args * ap)731 fdesc_pathconf(struct vnop_pathconf_args *ap)
732 {
733 	switch (ap->a_name) {
734 	case _PC_LINK_MAX:
735 		*ap->a_retval = LINK_MAX;
736 		return 0;
737 	case _PC_MAX_CANON:
738 		*ap->a_retval = MAX_CANON;
739 		return 0;
740 	case _PC_MAX_INPUT:
741 		*ap->a_retval = MAX_INPUT;
742 		return 0;
743 	case _PC_PIPE_BUF:
744 		*ap->a_retval = PIPE_BUF;
745 		return 0;
746 	case _PC_CHOWN_RESTRICTED:
747 		*ap->a_retval = 200112;         /* _POSIX_CHOWN_RESTRICTED */
748 		return 0;
749 	case _PC_VDISABLE:
750 		*ap->a_retval = _POSIX_VDISABLE;
751 		return 0;
752 	default:
753 		return EINVAL;
754 	}
755 	/* NOTREACHED */
756 }
757 
758 /*
759  * /dev/fd "should never get here" operation
760  */
761 int
fdesc_badop(void)762 fdesc_badop(void)
763 {
764 	return ENOTSUP;
765 	/* NOTREACHED */
766 }
767 
768 #define VOPFUNC int (*)(void *)
769 
770 #define fdesc_revoke nop_revoke
771 #define fdesc_strategy (void (*)(void))fdesc_badop
772 
773 #define fdesc_nullop (void (*)(void ))nullop
774 #define fdesc_error (void (*)(void ))vn_default_error
775 #define fdesc_notsupp (void (*)(void ))eopnotsupp
776 
777 int(**fdesc_vnodeop_p)(void *);
778 const struct vnodeopv_entry_desc devfs_fdesc_vnodeop_entries[] = {
779 	{ .opve_op = &vnop_default_desc, .opve_impl = (VOPFUNC)fdesc_error },
780 	{ .opve_op = &vnop_lookup_desc, .opve_impl = (VOPFUNC)fdesc_error},        /* lookup */
781 	{ .opve_op = &vnop_create_desc, .opve_impl = (VOPFUNC)fdesc_notsupp },   /* create */
782 	{ .opve_op = &vnop_mknod_desc, .opve_impl = (VOPFUNC)fdesc_notsupp },     /* mknod */
783 	{ .opve_op = &vnop_open_desc, .opve_impl = (VOPFUNC)fdesc_open },       /* open */
784 	{ .opve_op = &vnop_close_desc, .opve_impl = (VOPFUNC)fdesc_nullop },     /* close */
785 	{ .opve_op = &vnop_access_desc, .opve_impl = (VOPFUNC)fdesc_nullop },   /* access */
786 	{ .opve_op = &vnop_getattr_desc, .opve_impl = (VOPFUNC)fdesc_getattr }, /* getattr */
787 	{ .opve_op = &vnop_setattr_desc, .opve_impl = (VOPFUNC)fdesc_setattr }, /* setattr */
788 	{ .opve_op = &vnop_read_desc, .opve_impl = (VOPFUNC)fdesc_read },       /* read */
789 	{ .opve_op = &vnop_write_desc, .opve_impl = (VOPFUNC)fdesc_write },     /* write */
790 	{ .opve_op = &vnop_ioctl_desc, .opve_impl = (VOPFUNC)fdesc_ioctl },     /* ioctl */
791 	{ .opve_op = &vnop_select_desc, .opve_impl = (VOPFUNC)fdesc_select },   /* select */
792 	{ .opve_op = &vnop_revoke_desc, .opve_impl = (VOPFUNC)fdesc_revoke },   /* revoke */
793 	{ .opve_op = &vnop_mmap_desc, .opve_impl = (VOPFUNC)fdesc_notsupp },       /* mmap */
794 	{ .opve_op = &vnop_fsync_desc, .opve_impl = (VOPFUNC)fdesc_nullop },     /* fsync */
795 	{ .opve_op = &vnop_remove_desc, .opve_impl = (VOPFUNC)fdesc_notsupp },   /* remove */
796 	{ .opve_op = &vnop_link_desc, .opve_impl = (VOPFUNC)fdesc_notsupp },       /* link */
797 	{ .opve_op = &vnop_rename_desc, .opve_impl =  (VOPFUNC)fdesc_notsupp },   /* rename */
798 	{ .opve_op = &vnop_mkdir_desc, .opve_impl = (VOPFUNC)fdesc_notsupp },     /* mkdir */
799 	{ .opve_op = &vnop_rmdir_desc, .opve_impl = (VOPFUNC)fdesc_notsupp },     /* rmdir */
800 	{ .opve_op = &vnop_symlink_desc, .opve_impl = (VOPFUNC)fdesc_notsupp }, /* symlink */
801 	{ .opve_op = &vnop_readdir_desc, .opve_impl = (VOPFUNC)fdesc_error},/* readdir */
802 	{ .opve_op = &vnop_readlink_desc, .opve_impl = (VOPFUNC)err_readlink}, /* readlink */
803 	{ .opve_op = &vnop_inactive_desc, .opve_impl = (VOPFUNC)fdesc_inactive },/* inactive */
804 	{ .opve_op = &vnop_reclaim_desc, .opve_impl = (VOPFUNC)fdesc_reclaim }, /* reclaim */
805 	{ .opve_op = &vnop_strategy_desc, .opve_impl = (VOPFUNC)fdesc_strategy },       /* strategy */
806 	{ .opve_op = &vnop_pathconf_desc, .opve_impl = (VOPFUNC)fdesc_pathconf },       /* pathconf */
807 	{ .opve_op = &vnop_advlock_desc, .opve_impl = (VOPFUNC)fdesc_notsupp }, /* advlock */
808 	{ .opve_op = &vnop_bwrite_desc, .opve_impl = (VOPFUNC)fdesc_notsupp },   /* bwrite */
809 	{ .opve_op = &vnop_pagein_desc, .opve_impl = (VOPFUNC)err_pagein },     /* pagein */
810 	{ .opve_op = &vnop_pageout_desc, .opve_impl = (VOPFUNC)err_pageout },   /* pageout */
811 	{ .opve_op = &vnop_copyfile_desc, .opve_impl = (VOPFUNC)err_copyfile }, /* Copyfile */
812 	{ .opve_op = &vnop_blktooff_desc, .opve_impl = (VOPFUNC)fdesc_notsupp },       /* blktooff */
813 	{ .opve_op = &vnop_blktooff_desc, .opve_impl = (VOPFUNC)fdesc_notsupp },       /* offtoblk */
814 	{ .opve_op = &vnop_blockmap_desc, .opve_impl = (VOPFUNC)fdesc_notsupp },       /* blockmap */
815 	{ .opve_op = (struct vnodeop_desc*)NULL, .opve_impl = (VOPFUNC)NULL }
816 };
817 
818 const struct vnodeopv_desc devfs_fdesc_vnodeop_opv_desc =
819 { .opv_desc_vector_p = &fdesc_vnodeop_p, .opv_desc_ops = devfs_fdesc_vnodeop_entries };
820