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