| 1 | /* |
| 2 | * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved. |
| 3 | * |
| 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
| 5 | * |
| 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. |
| 14 | * |
| 15 | * Please obtain a copy of the License at |
| 16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. |
| 17 | * |
| 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 |
| 20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
| 21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, |
| 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. |
| 25 | * |
| 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
| 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> |
| 76 | #include <sys/proc_internal.h> |
| 77 | #include <sys/kernel.h> /* boottime */ |
| 78 | #include <sys/resourcevar.h> |
| 79 | #include <sys/filedesc.h> |
| 80 | #include <sys/kauth.h> |
| 81 | #include <sys/vnode_internal.h> |
| 82 | #include <sys/malloc.h> |
| 83 | #include <sys/file_internal.h> |
| 84 | #include <sys/stat.h> |
| 85 | #include <sys/mount_internal.h> |
| 86 | #include <sys/namei.h> |
| 87 | #include <sys/dirent.h> |
| 88 | #include <sys/ubc.h> |
| 89 | #include <sys/socketvar.h> |
| 90 | #include <sys/pipe.h> |
| 91 | #include <sys/uio_internal.h> |
| 92 | #include <vfs/vfs_support.h> |
| 93 | #include <pexpert/pexpert.h> |
| 94 | #include <miscfs/devfs/fdesc.h> |
| 95 | #include <miscfs/devfs/devfs.h> |
| 96 | #include <miscfs/devfs/devfsdefs.h> |
| 97 | |
| 98 | #define FDL_WANT 0x01 |
| 99 | #define FDL_LOCKED 0x02 |
| 100 | static int fdcache_lock; |
| 101 | |
| 102 | |
| 103 | #if (FD_STDIN != FD_STDOUT-1) || (FD_STDOUT != FD_STDERR-1) |
| 104 | FD_STDIN, FD_STDOUT, FD_STDERR must be a sequence n, n+1, n+2 |
| 105 | #endif |
| 106 | |
| 107 | #define NFDCACHE 3 |
| 108 | |
| 109 | #define FD_NHASH(ix) \ |
| 110 | (&fdhashtbl[(ix) & fdhash]) |
| 111 | LIST_HEAD(fdhashhead, fdescnode) *fdhashtbl; |
| 112 | u_long fdhash; |
| 113 | |
| 114 | static int fdesc_attr(int fd, struct vnode_attr *vap, vfs_context_t a_context); |
| 115 | |
| 116 | lck_mtx_t fdesc_mtx; |
| 117 | lck_grp_t *fdesc_lckgrp; |
| 118 | |
| 119 | static void |
| 120 | fdesc_lock(void) |
| 121 | { |
| 122 | lck_mtx_lock(&fdesc_mtx); |
| 123 | } |
| 124 | |
| 125 | static void |
| 126 | fdesc_unlock(void) |
| 127 | { |
| 128 | lck_mtx_unlock(&fdesc_mtx); |
| 129 | } |
| 130 | |
| 131 | |
| 132 | /* |
| 133 | * Initialise cache headers, create the devfs node |
| 134 | */ |
| 135 | int |
| 136 | devfs_fdesc_init() |
| 137 | { |
| 138 | int error = 0; |
| 139 | devnode_t *rootdir = dev_root->de_dnp; |
| 140 | devdirent_t *direntp; |
| 141 | |
| 142 | /* XXX Make sure you have the right path... */ |
| 143 | fdhashtbl = hashinit(NFDCACHE, M_CACHE, &fdhash); |
| 144 | fdesc_lckgrp = lck_grp_alloc_init("fdesc" , NULL); |
| 145 | lck_mtx_init(&fdesc_mtx, fdesc_lckgrp, NULL); |
| 146 | |
| 147 | DEVFS_LOCK(); |
| 148 | dev_add_entry("fd" , rootdir, DEV_DEVFD, NULL, NULL, NULL, &direntp); |
| 149 | devfs_fdesc_makelinks(); |
| 150 | DEVFS_UNLOCK(); |
| 151 | |
| 152 | return(error); |
| 153 | } |
| 154 | |
| 155 | /* |
| 156 | * Called during early startup, no need to synchronize |
| 157 | */ |
| 158 | int |
| 159 | devfs_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 | } |
| 187 | |
| 188 | return 0; |
| 189 | |
| 190 | bad: |
| 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 | |
| 204 | int |
| 205 | fdesc_allocvp(fdntype ftype, int ix, struct mount *mp, struct vnode **vpp, enum vtype vtype, int fdno) |
| 206 | { |
| 207 | struct fdhashhead *fc; |
| 208 | struct fdescnode *fd; |
| 209 | int error = 0; |
| 210 | int vid = 0; |
| 211 | struct vnode_fsparam vfsp; |
| 212 | |
| 213 | fdesc_lock(); |
| 214 | |
| 215 | fc = FD_NHASH(ix); |
| 216 | loop: |
| 217 | for (fd = fc->lh_first; fd != 0; fd = fd->fd_hash.le_next) { |
| 218 | if (fd->fd_ix == ix && vnode_mount(fd->fd_vnode) == mp) { |
| 219 | vid = vnode_vid(fd->fd_vnode); |
| 220 | fdesc_unlock(); |
| 221 | |
| 222 | if (vnode_getwithvid(fd->fd_vnode, vid)) { |
| 223 | fdesc_lock(); |
| 224 | goto loop; |
| 225 | } |
| 226 | |
| 227 | *vpp = fd->fd_vnode; |
| 228 | (*vpp)->v_type = vtype; |
| 229 | |
| 230 | return (error); |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | /* Only one thread can add to the hash at a time */ |
| 235 | if (fdcache_lock & FDL_LOCKED) { |
| 236 | fdcache_lock |= FDL_WANT; |
| 237 | msleep((caddr_t) &fdcache_lock, &fdesc_mtx, PINOD, "fdesc_allocvp" , NULL); |
| 238 | goto loop; |
| 239 | } |
| 240 | |
| 241 | fdcache_lock |= FDL_LOCKED; |
| 242 | fdesc_unlock(); |
| 243 | |
| 244 | MALLOC(fd, void *, sizeof(struct fdescnode), M_TEMP, M_WAITOK); |
| 245 | |
| 246 | vfsp.vnfs_mp = mp; |
| 247 | vfsp.vnfs_vtype = vtype; |
| 248 | vfsp.vnfs_str = "fdesc" ; |
| 249 | vfsp.vnfs_dvp = NULL; |
| 250 | vfsp.vnfs_fsnode = fd; |
| 251 | vfsp.vnfs_cnp = NULL; |
| 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; |
| 257 | vfsp.vnfs_markroot = 0; |
| 258 | |
| 259 | error = vnode_create(VNCREATE_FLAVOR, VCREATESIZE, &vfsp, vpp); |
| 260 | if (error) { |
| 261 | FREE(fd, M_TEMP); |
| 262 | fdesc_lock(); |
| 263 | goto out; |
| 264 | } |
| 265 | |
| 266 | (*vpp)->v_tag = VT_FDESC; |
| 267 | fd->fd_vnode = *vpp; |
| 268 | fd->fd_type = ftype; |
| 269 | fd->fd_fd = -1; |
| 270 | fd->fd_link = NULL; |
| 271 | fd->fd_ix = ix; |
| 272 | fd->fd_fd = fdno; |
| 273 | |
| 274 | fdesc_lock(); |
| 275 | |
| 276 | LIST_INSERT_HEAD(fc, fd, fd_hash); |
| 277 | out: |
| 278 | /* Hold the lock when we get here */ |
| 279 | fdcache_lock &= ~FDL_LOCKED; |
| 280 | |
| 281 | if (fdcache_lock & FDL_WANT) { |
| 282 | fdcache_lock &= ~FDL_WANT; |
| 283 | wakeup((caddr_t) &fdcache_lock); |
| 284 | } |
| 285 | |
| 286 | fdesc_unlock(); |
| 287 | |
| 288 | return (error); |
| 289 | } |
| 290 | |
| 291 | /* |
| 292 | * vp is the current namei directory |
| 293 | * ndp is the name to locate in that directory... |
| 294 | * |
| 295 | * This vnop should only be called on the special directory /dev/fd. |
| 296 | */ |
| 297 | int |
| 298 | devfs_devfd_lookup(struct vnop_lookup_args *ap) |
| 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; |
| 304 | struct proc *p = vfs_context_proc(ap->a_context); |
| 305 | int numfiles = p->p_fd->fd_nfiles; |
| 306 | int fd; |
| 307 | int error; |
| 308 | struct vnode *fvp; |
| 309 | |
| 310 | if (cnp->cn_namelen == 1 && *pname == '.') { |
| 311 | *vpp = dvp; |
| 312 | |
| 313 | if ( (error = vnode_get(dvp)) ) { |
| 314 | return(error); |
| 315 | } |
| 316 | return (0); |
| 317 | } |
| 318 | |
| 319 | fd = 0; |
| 320 | while (*pname >= '0' && *pname <= '9') { |
| 321 | fd = 10 * fd + *pname++ - '0'; |
| 322 | if (fd >= numfiles) |
| 323 | break; |
| 324 | } |
| 325 | |
| 326 | if (*pname != '\0') { |
| 327 | error = ENOENT; |
| 328 | goto bad; |
| 329 | } |
| 330 | |
| 331 | if (fd < 0 || fd >= numfiles || |
| 332 | *fdfile(p, fd) == NULL || |
| 333 | (*fdflags(p, fd) & UF_RESERVED)) { |
| 334 | error = EBADF; |
| 335 | goto bad; |
| 336 | } |
| 337 | |
| 338 | error = fdesc_allocvp(Fdesc, FD_DESC+fd, dvp->v_mount, &fvp, VNON, fd); |
| 339 | if (error) |
| 340 | goto bad; |
| 341 | *vpp = fvp; |
| 342 | return (0); |
| 343 | |
| 344 | bad: |
| 345 | *vpp = NULL; |
| 346 | return (error); |
| 347 | } |
| 348 | |
| 349 | int |
| 350 | fdesc_open(struct vnop_open_args *ap) |
| 351 | { |
| 352 | struct vnode *vp = ap->a_vp; |
| 353 | thread_t thr = vfs_context_thread(ap->a_context); |
| 354 | uthread_t uu; |
| 355 | int error = 0; |
| 356 | |
| 357 | if (thr == NULL) |
| 358 | return (EINVAL); |
| 359 | |
| 360 | uu = get_bsdthread_info(thr); |
| 361 | |
| 362 | switch (VTOFDESC(vp)->fd_type) { |
| 363 | case Fdesc: |
| 364 | /* |
| 365 | * XXX Kludge: set uu->uu_dupfd to contain the value of the |
| 366 | * the file descriptor being sought for duplication. The error |
| 367 | * return ensures that the vnode for this device will be |
| 368 | * released by vn_open. Open will detect this special error and |
| 369 | * take the actions in dupfdopen. Other callers of vn_open or |
| 370 | * vnop_open will simply report the error. |
| 371 | */ |
| 372 | uu->uu_dupfd = VTOFDESC(vp)->fd_fd; /* XXX */ |
| 373 | error = ENODEV; |
| 374 | break; |
| 375 | default: |
| 376 | panic("Invalid type for fdesc node!" ); |
| 377 | break; |
| 378 | } |
| 379 | |
| 380 | return (error); |
| 381 | } |
| 382 | |
| 383 | static int |
| 384 | fdesc_attr(int fd, struct vnode_attr *vap, vfs_context_t a_context) |
| 385 | { |
| 386 | struct fileproc *fp; |
| 387 | struct proc *p = vfs_context_proc(a_context); |
| 388 | struct stat stb; |
| 389 | int error; |
| 390 | |
| 391 | if ((error = fp_lookup(p, fd, &fp, 0))) |
| 392 | return (error); |
| 393 | switch (FILEGLOB_DTYPE(fp->f_fglob)) { |
| 394 | case DTYPE_VNODE: |
| 395 | if((error = vnode_getwithref((struct vnode *) fp->f_fglob->fg_data)) != 0) { |
| 396 | break; |
| 397 | } |
| 398 | if ((error = vnode_authorize((struct vnode *)fp->f_fglob->fg_data, |
| 399 | NULL, |
| 400 | KAUTH_VNODE_READ_ATTRIBUTES | KAUTH_VNODE_READ_SECURITY, |
| 401 | a_context)) == 0) |
| 402 | error = vnode_getattr((struct vnode *)fp->f_fglob->fg_data, vap, a_context); |
| 403 | if (error == 0 && vap->va_type == VDIR) { |
| 404 | /* |
| 405 | * directories can cause loops in the namespace, |
| 406 | * so turn off the 'x' bits to avoid trouble. |
| 407 | * |
| 408 | * XXX ACLs break this, of course |
| 409 | */ |
| 410 | vap->va_mode &= ~((VEXEC)|(VEXEC>>3)|(VEXEC>>6)); |
| 411 | } |
| 412 | (void)vnode_put((struct vnode *) fp->f_fglob->fg_data); |
| 413 | break; |
| 414 | |
| 415 | case DTYPE_SOCKET: |
| 416 | case DTYPE_PIPE: |
| 417 | #if SOCKETS |
| 418 | if (FILEGLOB_DTYPE(fp->f_fglob) == DTYPE_SOCKET) |
| 419 | error = soo_stat((struct socket *)fp->f_fglob->fg_data, (void *)&stb, 0); |
| 420 | else |
| 421 | #endif /* SOCKETS */ |
| 422 | error = pipe_stat((struct pipe *)fp->f_fglob->fg_data, (void *)&stb, 0); |
| 423 | |
| 424 | if (error == 0) { |
| 425 | if (FILEGLOB_DTYPE(fp->f_fglob) == DTYPE_SOCKET) |
| 426 | VATTR_RETURN(vap, va_type, VSOCK); |
| 427 | else |
| 428 | VATTR_RETURN(vap, va_type, VFIFO); |
| 429 | |
| 430 | VATTR_RETURN(vap, va_mode, stb.st_mode); |
| 431 | VATTR_RETURN(vap, va_nlink, stb.st_nlink); |
| 432 | VATTR_RETURN(vap, va_uid, stb.st_uid); |
| 433 | VATTR_RETURN(vap, va_gid, stb.st_gid); |
| 434 | VATTR_RETURN(vap, va_fsid, stb.st_dev); |
| 435 | VATTR_RETURN(vap, va_fileid, stb.st_ino); |
| 436 | VATTR_RETURN(vap, va_data_size, stb.st_size); |
| 437 | VATTR_RETURN(vap, va_access_time, stb.st_atimespec); |
| 438 | VATTR_RETURN(vap, va_modify_time, stb.st_mtimespec); |
| 439 | VATTR_RETURN(vap, va_change_time, stb.st_ctimespec); |
| 440 | VATTR_RETURN(vap, va_gen, stb.st_gen); |
| 441 | VATTR_RETURN(vap, va_flags, stb.st_flags); |
| 442 | VATTR_RETURN(vap, va_rdev, stb.st_rdev); |
| 443 | VATTR_RETURN(vap, va_total_alloc, stb.st_blocks * stb.st_blksize); |
| 444 | VATTR_RETURN(vap, va_acl, NULL); |
| 445 | } |
| 446 | break; |
| 447 | |
| 448 | default: |
| 449 | error = EBADF; |
| 450 | } |
| 451 | |
| 452 | fp_drop(p, fd, fp, 0); |
| 453 | return (error); |
| 454 | } |
| 455 | |
| 456 | int |
| 457 | fdesc_getattr(struct vnop_getattr_args *ap) |
| 458 | { |
| 459 | struct vnode *vp = ap->a_vp; |
| 460 | struct vnode_attr *vap = ap->a_vap; |
| 461 | unsigned fd; |
| 462 | int error = 0; |
| 463 | |
| 464 | switch (VTOFDESC(vp)->fd_type) { |
| 465 | case Fdesc: |
| 466 | fd = VTOFDESC(vp)->fd_fd; |
| 467 | error = fdesc_attr(fd, vap, ap->a_context); |
| 468 | break; |
| 469 | |
| 470 | default: |
| 471 | panic("Invalid type for an fdesc node!\n" ); |
| 472 | break; |
| 473 | } |
| 474 | |
| 475 | /* |
| 476 | * Yes, we do this without locking, but this value is always just |
| 477 | * a snapshot. |
| 478 | */ |
| 479 | if (error == 0) { |
| 480 | vp->v_type = vap->va_type; |
| 481 | |
| 482 | /* We need an inactive to reset type to VNON */ |
| 483 | vnode_setneedinactive(vp); |
| 484 | } |
| 485 | |
| 486 | return (error); |
| 487 | } |
| 488 | |
| 489 | int |
| 490 | fdesc_setattr(struct vnop_setattr_args *ap) |
| 491 | { |
| 492 | struct fileproc *fp; |
| 493 | unsigned fd; |
| 494 | int error; |
| 495 | struct proc * p = vfs_context_proc(ap->a_context); |
| 496 | |
| 497 | /* |
| 498 | * Can't mess with the root vnode |
| 499 | */ |
| 500 | switch (VTOFDESC(ap->a_vp)->fd_type) { |
| 501 | case Fdesc: |
| 502 | break; |
| 503 | default: |
| 504 | panic("Invalid type for an fdesc node!\n" ); |
| 505 | return (EACCES); |
| 506 | } |
| 507 | |
| 508 | fd = VTOFDESC(ap->a_vp)->fd_fd; |
| 509 | if ((error = fp_lookup(vfs_context_proc(ap->a_context), fd, &fp, 0))) |
| 510 | return (error); |
| 511 | |
| 512 | /* |
| 513 | * Can setattr the underlying vnode, but not sockets! |
| 514 | */ |
| 515 | switch (FILEGLOB_DTYPE(fp->f_fglob)) { |
| 516 | case DTYPE_VNODE: |
| 517 | { |
| 518 | if ((error = vnode_getwithref((struct vnode *) fp->f_fglob->fg_data)) != 0) |
| 519 | break; |
| 520 | error = vnode_setattr((struct vnode *) fp->f_fglob->fg_data, ap->a_vap, ap->a_context); |
| 521 | (void)vnode_put((struct vnode *) fp->f_fglob->fg_data); |
| 522 | break; |
| 523 | } |
| 524 | |
| 525 | case DTYPE_SOCKET: |
| 526 | case DTYPE_PIPE: |
| 527 | error = 0; |
| 528 | break; |
| 529 | |
| 530 | default: |
| 531 | error = EBADF; |
| 532 | break; |
| 533 | } |
| 534 | |
| 535 | fp_drop(p, fd, fp, 0); |
| 536 | return (error); |
| 537 | } |
| 538 | |
| 539 | #define UIO_MX 16 |
| 540 | |
| 541 | /* |
| 542 | static struct dirtmp { |
| 543 | u_int32_t d_fileno; |
| 544 | u_short d_reclen; |
| 545 | u_short d_namlen; |
| 546 | char d_name[8]; |
| 547 | } rootent[] = { |
| 548 | { FD_DEVFD, UIO_MX, 2, "fd" }, |
| 549 | { FD_STDIN, UIO_MX, 5, "stdin" }, |
| 550 | { FD_STDOUT, UIO_MX, 6, "stdout" }, |
| 551 | { FD_STDERR, UIO_MX, 6, "stderr" }, |
| 552 | { 0, 0, 0, "" } |
| 553 | }; |
| 554 | */ |
| 555 | |
| 556 | /* Only called on /dev/fd */ |
| 557 | int |
| 558 | devfs_devfd_readdir(struct vnop_readdir_args *ap) |
| 559 | { |
| 560 | struct uio *uio = ap->a_uio; |
| 561 | struct proc *p = current_proc(); |
| 562 | int i, error; |
| 563 | |
| 564 | /* |
| 565 | * We don't allow exporting fdesc mounts, and currently local |
| 566 | * requests do not need cookies. |
| 567 | */ |
| 568 | if (ap->a_flags & (VNODE_READDIR_EXTENDED | VNODE_READDIR_REQSEEKOFF)) |
| 569 | return (EINVAL); |
| 570 | |
| 571 | /* |
| 572 | * There needs to be space for at least one entry. |
| 573 | */ |
| 574 | if (uio_resid(uio) < UIO_MX) |
| 575 | return (EINVAL); |
| 576 | |
| 577 | i = uio->uio_offset / UIO_MX; |
| 578 | error = 0; |
| 579 | while (uio_resid(uio) >= UIO_MX) { |
| 580 | if (i >= p->p_fd->fd_nfiles) |
| 581 | break; |
| 582 | |
| 583 | if (*fdfile(p, i) != NULL && !(*fdflags(p, i) & UF_RESERVED)) { |
| 584 | struct dirent d; |
| 585 | struct dirent *dp = &d; |
| 586 | |
| 587 | bzero((caddr_t) dp, UIO_MX); |
| 588 | |
| 589 | dp->d_namlen = snprintf(dp->d_name, sizeof(dp->d_name), |
| 590 | "%d" , i); |
| 591 | dp->d_reclen = UIO_MX; |
| 592 | dp->d_type = DT_UNKNOWN; |
| 593 | dp->d_fileno = i + FD_STDIN; |
| 594 | /* |
| 595 | * And ship to userland |
| 596 | */ |
| 597 | error = uiomove((caddr_t) dp, UIO_MX, uio); |
| 598 | if (error) |
| 599 | break; |
| 600 | } |
| 601 | i++; |
| 602 | } |
| 603 | |
| 604 | uio->uio_offset = i * UIO_MX; |
| 605 | return (error); |
| 606 | } |
| 607 | |
| 608 | int |
| 609 | fdesc_read(__unused struct vnop_read_args *ap) |
| 610 | { |
| 611 | return (ENOTSUP); |
| 612 | } |
| 613 | |
| 614 | int |
| 615 | fdesc_write(__unused struct vnop_write_args *ap) |
| 616 | { |
| 617 | return (ENOTSUP); |
| 618 | } |
| 619 | |
| 620 | int |
| 621 | fdesc_ioctl(__unused struct vnop_ioctl_args *ap) |
| 622 | { |
| 623 | return (ENOTSUP); |
| 624 | } |
| 625 | |
| 626 | int |
| 627 | fdesc_select(__unused struct vnop_select_args *ap) |
| 628 | { |
| 629 | return (ENOTSUP); |
| 630 | } |
| 631 | |
| 632 | int |
| 633 | fdesc_inactive(struct vnop_inactive_args *ap) |
| 634 | { |
| 635 | struct vnode *vp = ap->a_vp; |
| 636 | |
| 637 | /* |
| 638 | * Clear out the v_type field to avoid |
| 639 | * nasty things happening in vgone(). |
| 640 | */ |
| 641 | vp->v_type = VNON; |
| 642 | |
| 643 | return (0); |
| 644 | } |
| 645 | |
| 646 | int |
| 647 | fdesc_reclaim(struct vnop_reclaim_args *ap) |
| 648 | { |
| 649 | struct vnode *vp = ap->a_vp; |
| 650 | struct fdescnode *fd = VTOFDESC(vp); |
| 651 | |
| 652 | fdesc_lock(); |
| 653 | |
| 654 | LIST_REMOVE(fd, fd_hash); |
| 655 | FREE(vp->v_data, M_TEMP); |
| 656 | vp->v_data = NULL; |
| 657 | |
| 658 | fdesc_unlock(); |
| 659 | |
| 660 | return (0); |
| 661 | } |
| 662 | |
| 663 | /* |
| 664 | * Return POSIX pathconf information applicable to special devices. |
| 665 | */ |
| 666 | int |
| 667 | fdesc_pathconf(struct vnop_pathconf_args *ap) |
| 668 | { |
| 669 | |
| 670 | switch (ap->a_name) { |
| 671 | case _PC_LINK_MAX: |
| 672 | *ap->a_retval = LINK_MAX; |
| 673 | return (0); |
| 674 | case _PC_MAX_CANON: |
| 675 | *ap->a_retval = MAX_CANON; |
| 676 | return (0); |
| 677 | case _PC_MAX_INPUT: |
| 678 | *ap->a_retval = MAX_INPUT; |
| 679 | return (0); |
| 680 | case _PC_PIPE_BUF: |
| 681 | *ap->a_retval = PIPE_BUF; |
| 682 | return (0); |
| 683 | case _PC_CHOWN_RESTRICTED: |
| 684 | *ap->a_retval = 200112; /* _POSIX_CHOWN_RESTRICTED */ |
| 685 | return (0); |
| 686 | case _PC_VDISABLE: |
| 687 | *ap->a_retval = _POSIX_VDISABLE; |
| 688 | return (0); |
| 689 | default: |
| 690 | return (EINVAL); |
| 691 | } |
| 692 | /* NOTREACHED */ |
| 693 | } |
| 694 | |
| 695 | /* |
| 696 | * /dev/fd "should never get here" operation |
| 697 | */ |
| 698 | int |
| 699 | fdesc_badop(void) |
| 700 | { |
| 701 | |
| 702 | return (ENOTSUP); |
| 703 | /* NOTREACHED */ |
| 704 | } |
| 705 | |
| 706 | #define VOPFUNC int (*)(void *) |
| 707 | |
| 708 | #define fdesc_create (int (*) (struct vnop_create_args *))eopnotsupp |
| 709 | #define fdesc_mknod (int (*) (struct vnop_mknod_args *))eopnotsupp |
| 710 | #define fdesc_close (int (*) (struct vnop_close_args *))nullop |
| 711 | #define fdesc_access (int (*) (struct vnop_access_args *))nullop |
| 712 | #define fdesc_mmap (int (*) (struct vnop_mmap_args *))eopnotsupp |
| 713 | #define fdesc_revoke nop_revoke |
| 714 | #define fdesc_fsync (int (*) (struct vnop_fsync_args *))nullop |
| 715 | #define fdesc_remove (int (*) (struct vnop_remove_args *))eopnotsupp |
| 716 | #define fdesc_link (int (*) (struct vnop_link_args *))eopnotsupp |
| 717 | #define fdesc_rename (int (*) (struct vnop_rename_args *))eopnotsupp |
| 718 | #define fdesc_mkdir (int (*) (struct vnop_mkdir_args *))eopnotsupp |
| 719 | #define fdesc_rmdir (int (*) (struct vnop_rmdir_args *))eopnotsupp |
| 720 | #define fdesc_symlink (int (*) (struct vnop_symlink_args *))eopnotsupp |
| 721 | #define fdesc_strategy (int (*) (struct vnop_strategy_args *))fdesc_badop |
| 722 | #define fdesc_advlock (int (*) (struct vnop_advlock_args *))eopnotsupp |
| 723 | #define fdesc_bwrite (int (*) (struct vnop_bwrite_args *))eopnotsupp |
| 724 | #define fdesc_blktooff (int (*) (struct vnop_blktooff_args *))eopnotsupp |
| 725 | #define fdesc_offtoblk (int (*) (struct vnop_offtoblk_args *))eopnotsupp |
| 726 | #define fdesc_blockmap (int (*) (struct vnop_blockmap_args *))eopnotsupp |
| 727 | |
| 728 | int (**fdesc_vnodeop_p)(void *); |
| 729 | struct vnodeopv_entry_desc devfs_fdesc_vnodeop_entries[] = { |
| 730 | { &vnop_default_desc, (VOPFUNC)vn_default_error }, |
| 731 | { &vnop_lookup_desc, (VOPFUNC)vn_default_error}, /* lookup */ |
| 732 | { &vnop_create_desc, (VOPFUNC)fdesc_create }, /* create */ |
| 733 | { &vnop_mknod_desc, (VOPFUNC)fdesc_mknod }, /* mknod */ |
| 734 | { &vnop_open_desc, (VOPFUNC)fdesc_open }, /* open */ |
| 735 | { &vnop_close_desc, (VOPFUNC)fdesc_close }, /* close */ |
| 736 | { &vnop_access_desc, (VOPFUNC)fdesc_access }, /* access */ |
| 737 | { &vnop_getattr_desc, (VOPFUNC)fdesc_getattr }, /* getattr */ |
| 738 | { &vnop_setattr_desc, (VOPFUNC)fdesc_setattr }, /* setattr */ |
| 739 | { &vnop_read_desc, (VOPFUNC)fdesc_read }, /* read */ |
| 740 | { &vnop_write_desc, (VOPFUNC)fdesc_write }, /* write */ |
| 741 | { &vnop_ioctl_desc, (VOPFUNC)fdesc_ioctl }, /* ioctl */ |
| 742 | { &vnop_select_desc, (VOPFUNC)fdesc_select }, /* select */ |
| 743 | { &vnop_revoke_desc, (VOPFUNC)fdesc_revoke }, /* revoke */ |
| 744 | { &vnop_mmap_desc, (VOPFUNC)fdesc_mmap }, /* mmap */ |
| 745 | { &vnop_fsync_desc, (VOPFUNC)fdesc_fsync }, /* fsync */ |
| 746 | { &vnop_remove_desc, (VOPFUNC)fdesc_remove }, /* remove */ |
| 747 | { &vnop_link_desc, (VOPFUNC)fdesc_link }, /* link */ |
| 748 | { &vnop_rename_desc, (VOPFUNC)fdesc_rename }, /* rename */ |
| 749 | { &vnop_mkdir_desc, (VOPFUNC)fdesc_mkdir }, /* mkdir */ |
| 750 | { &vnop_rmdir_desc, (VOPFUNC)fdesc_rmdir }, /* rmdir */ |
| 751 | { &vnop_symlink_desc, (VOPFUNC)fdesc_symlink }, /* symlink */ |
| 752 | { &vnop_readdir_desc, (VOPFUNC)vn_default_error},/* readdir */ |
| 753 | { &vnop_readlink_desc, (VOPFUNC)err_readlink}, /* readlink */ |
| 754 | { &vnop_inactive_desc, (VOPFUNC)fdesc_inactive },/* inactive */ |
| 755 | { &vnop_reclaim_desc, (VOPFUNC)fdesc_reclaim }, /* reclaim */ |
| 756 | { &vnop_strategy_desc, (VOPFUNC)fdesc_strategy }, /* strategy */ |
| 757 | { &vnop_pathconf_desc, (VOPFUNC)fdesc_pathconf }, /* pathconf */ |
| 758 | { &vnop_advlock_desc, (VOPFUNC)fdesc_advlock }, /* advlock */ |
| 759 | { &vnop_bwrite_desc, (VOPFUNC)fdesc_bwrite }, /* bwrite */ |
| 760 | { &vnop_pagein_desc, (VOPFUNC)err_pagein }, /* pagein */ |
| 761 | { &vnop_pageout_desc, (VOPFUNC)err_pageout }, /* pageout */ |
| 762 | { &vnop_copyfile_desc, (VOPFUNC)err_copyfile }, /* Copyfile */ |
| 763 | { &vnop_blktooff_desc, (VOPFUNC)fdesc_blktooff }, /* blktooff */ |
| 764 | { &vnop_blktooff_desc, (VOPFUNC)fdesc_offtoblk }, /* offtoblk */ |
| 765 | { &vnop_blockmap_desc, (VOPFUNC)fdesc_blockmap }, /* blockmap */ |
| 766 | { (struct vnodeop_desc*)NULL, (VOPFUNC)NULL } |
| 767 | }; |
| 768 | |
| 769 | struct vnodeopv_desc devfs_fdesc_vnodeop_opv_desc = |
| 770 | { &fdesc_vnodeop_p, devfs_fdesc_vnodeop_entries }; |
| 771 | |
| 772 | |