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