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