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