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