1 /* $NetBSD: fdesc_vnops.c,v 1.89 2005/12/11 12:24:50 christos Exp $ */ 2 3 /* 4 * Copyright (c) 1992, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software donated to Berkeley by 8 * Jan-Simon Pendry. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * @(#)fdesc_vnops.c 8.17 (Berkeley) 5/22/95 35 * 36 * #Id: fdesc_vnops.c,v 1.12 1993/04/06 16:17:17 jsp Exp # 37 */ 38 39 /* 40 * /dev/fd Filesystem 41 */ 42 43 #include <sys/cdefs.h> 44 __KERNEL_RCSID(0, "$NetBSD: fdesc_vnops.c,v 1.89 2005/12/11 12:24:50 christos Exp $"); 45 46 #include <sys/param.h> 47 #include <sys/systm.h> 48 #include <sys/time.h> 49 #include <sys/proc.h> 50 #include <sys/kernel.h> /* boottime */ 51 #include <sys/resourcevar.h> 52 #include <sys/socketvar.h> 53 #include <sys/filedesc.h> 54 #include <sys/vnode.h> 55 #include <sys/malloc.h> 56 #include <sys/conf.h> 57 #include <sys/file.h> 58 #include <sys/stat.h> 59 #include <sys/mount.h> 60 #include <sys/namei.h> 61 #include <sys/buf.h> 62 #include <sys/dirent.h> 63 #include <sys/tty.h> 64 65 #include <miscfs/fdesc/fdesc.h> 66 #include <miscfs/genfs/genfs.h> 67 68 #define cttyvp(p) ((p)->p_flag & P_CONTROLT ? (p)->p_session->s_ttyvp : NULL) 69 70 #define FDL_WANT 0x01 71 #define FDL_LOCKED 0x02 72 static int fdcache_lock; 73 74 dev_t devctty; 75 76 #if (FD_STDIN != FD_STDOUT-1) || (FD_STDOUT != FD_STDERR-1) 77 FD_STDIN, FD_STDOUT, FD_STDERR must be a sequence n, n+1, n+2 78 #endif 79 80 #define NFDCACHE 4 81 82 #define FD_NHASH(ix) \ 83 (&fdhashtbl[(ix) & fdhash]) 84 LIST_HEAD(fdhashhead, fdescnode) *fdhashtbl; 85 u_long fdhash; 86 87 int fdesc_lookup(void *); 88 #define fdesc_create genfs_eopnotsupp 89 #define fdesc_mknod genfs_eopnotsupp 90 int fdesc_open(void *); 91 #define fdesc_close genfs_nullop 92 #define fdesc_access genfs_nullop 93 int fdesc_getattr(void *); 94 int fdesc_setattr(void *); 95 int fdesc_read(void *); 96 int fdesc_write(void *); 97 int fdesc_ioctl(void *); 98 int fdesc_poll(void *); 99 int fdesc_kqfilter(void *); 100 #define fdesc_mmap genfs_eopnotsupp 101 #define fdesc_fcntl genfs_fcntl 102 #define fdesc_fsync genfs_nullop 103 #define fdesc_seek genfs_seek 104 #define fdesc_remove genfs_eopnotsupp 105 int fdesc_link(void *); 106 #define fdesc_rename genfs_eopnotsupp 107 #define fdesc_mkdir genfs_eopnotsupp 108 #define fdesc_rmdir genfs_eopnotsupp 109 int fdesc_symlink(void *); 110 int fdesc_readdir(void *); 111 int fdesc_readlink(void *); 112 #define fdesc_abortop genfs_abortop 113 int fdesc_inactive(void *); 114 int fdesc_reclaim(void *); 115 #define fdesc_lock genfs_lock 116 #define fdesc_unlock genfs_unlock 117 #define fdesc_bmap genfs_badop 118 #define fdesc_strategy genfs_badop 119 int fdesc_print(void *); 120 int fdesc_pathconf(void *); 121 #define fdesc_islocked genfs_islocked 122 #define fdesc_advlock genfs_einval 123 #define fdesc_bwrite genfs_eopnotsupp 124 #define fdesc_revoke genfs_revoke 125 #define fdesc_putpages genfs_null_putpages 126 127 static int fdesc_attr(int, struct vattr *, struct ucred *, struct lwp *); 128 129 int (**fdesc_vnodeop_p)(void *); 130 const struct vnodeopv_entry_desc fdesc_vnodeop_entries[] = { 131 { &vop_default_desc, vn_default_error }, 132 { &vop_lookup_desc, fdesc_lookup }, /* lookup */ 133 { &vop_create_desc, fdesc_create }, /* create */ 134 { &vop_mknod_desc, fdesc_mknod }, /* mknod */ 135 { &vop_open_desc, fdesc_open }, /* open */ 136 { &vop_close_desc, fdesc_close }, /* close */ 137 { &vop_access_desc, fdesc_access }, /* access */ 138 { &vop_getattr_desc, fdesc_getattr }, /* getattr */ 139 { &vop_setattr_desc, fdesc_setattr }, /* setattr */ 140 { &vop_read_desc, fdesc_read }, /* read */ 141 { &vop_write_desc, fdesc_write }, /* write */ 142 { &vop_ioctl_desc, fdesc_ioctl }, /* ioctl */ 143 { &vop_fcntl_desc, fdesc_fcntl }, /* fcntl */ 144 { &vop_poll_desc, fdesc_poll }, /* poll */ 145 { &vop_kqfilter_desc, fdesc_kqfilter }, /* kqfilter */ 146 { &vop_revoke_desc, fdesc_revoke }, /* revoke */ 147 { &vop_mmap_desc, fdesc_mmap }, /* mmap */ 148 { &vop_fsync_desc, fdesc_fsync }, /* fsync */ 149 { &vop_seek_desc, fdesc_seek }, /* seek */ 150 { &vop_remove_desc, fdesc_remove }, /* remove */ 151 { &vop_link_desc, fdesc_link }, /* link */ 152 { &vop_rename_desc, fdesc_rename }, /* rename */ 153 { &vop_mkdir_desc, fdesc_mkdir }, /* mkdir */ 154 { &vop_rmdir_desc, fdesc_rmdir }, /* rmdir */ 155 { &vop_symlink_desc, fdesc_symlink }, /* symlink */ 156 { &vop_readdir_desc, fdesc_readdir }, /* readdir */ 157 { &vop_readlink_desc, fdesc_readlink }, /* readlink */ 158 { &vop_abortop_desc, fdesc_abortop }, /* abortop */ 159 { &vop_inactive_desc, fdesc_inactive }, /* inactive */ 160 { &vop_reclaim_desc, fdesc_reclaim }, /* reclaim */ 161 { &vop_lock_desc, fdesc_lock }, /* lock */ 162 { &vop_unlock_desc, fdesc_unlock }, /* unlock */ 163 { &vop_bmap_desc, fdesc_bmap }, /* bmap */ 164 { &vop_strategy_desc, fdesc_strategy }, /* strategy */ 165 { &vop_print_desc, fdesc_print }, /* print */ 166 { &vop_islocked_desc, fdesc_islocked }, /* islocked */ 167 { &vop_pathconf_desc, fdesc_pathconf }, /* pathconf */ 168 { &vop_advlock_desc, fdesc_advlock }, /* advlock */ 169 { &vop_bwrite_desc, fdesc_bwrite }, /* bwrite */ 170 { &vop_putpages_desc, fdesc_putpages }, /* putpages */ 171 { NULL, NULL } 172 }; 173 174 const struct vnodeopv_desc fdesc_vnodeop_opv_desc = 175 { &fdesc_vnodeop_p, fdesc_vnodeop_entries }; 176 177 extern const struct cdevsw ctty_cdevsw; 178 179 /* 180 * Initialise cache headers 181 */ 182 void 183 fdesc_init() 184 { 185 int cttymajor; 186 187 /* locate the major number */ 188 cttymajor = cdevsw_lookup_major(&ctty_cdevsw); 189 devctty = makedev(cttymajor, 0); 190 fdhashtbl = hashinit(NFDCACHE, HASH_LIST, M_CACHE, M_NOWAIT, &fdhash); 191 } 192 193 /* 194 * Free hash table. 195 */ 196 void 197 fdesc_done() 198 { 199 hashdone(fdhashtbl, M_CACHE); 200 } 201 202 /* 203 * Return a locked vnode of the correct type. 204 */ 205 int 206 fdesc_allocvp(ftype, ix, mp, vpp) 207 fdntype ftype; 208 int ix; 209 struct mount *mp; 210 struct vnode **vpp; 211 { 212 struct fdhashhead *fc; 213 struct fdescnode *fd; 214 int error = 0; 215 216 fc = FD_NHASH(ix); 217 loop: 218 for (fd = fc->lh_first; fd != 0; fd = fd->fd_hash.le_next) { 219 if (fd->fd_ix == ix && fd->fd_vnode->v_mount == mp) { 220 if (vget(fd->fd_vnode, LK_EXCLUSIVE)) 221 goto loop; 222 *vpp = fd->fd_vnode; 223 return (error); 224 } 225 } 226 227 /* 228 * otherwise lock the array while we call getnewvnode 229 * since that can block. 230 */ 231 if (fdcache_lock & FDL_LOCKED) { 232 fdcache_lock |= FDL_WANT; 233 (void) tsleep(&fdcache_lock, PINOD, "fdcache", 0); 234 goto loop; 235 } 236 fdcache_lock |= FDL_LOCKED; 237 238 error = getnewvnode(VT_FDESC, mp, fdesc_vnodeop_p, vpp); 239 if (error) 240 goto out; 241 MALLOC(fd, void *, sizeof(struct fdescnode), M_TEMP, M_WAITOK); 242 (*vpp)->v_data = fd; 243 fd->fd_vnode = *vpp; 244 fd->fd_type = ftype; 245 fd->fd_fd = -1; 246 fd->fd_link = 0; 247 fd->fd_ix = ix; 248 VOP_LOCK(*vpp, LK_EXCLUSIVE); 249 LIST_INSERT_HEAD(fc, fd, fd_hash); 250 251 out:; 252 fdcache_lock &= ~FDL_LOCKED; 253 254 if (fdcache_lock & FDL_WANT) { 255 fdcache_lock &= ~FDL_WANT; 256 wakeup(&fdcache_lock); 257 } 258 259 return (error); 260 } 261 262 /* 263 * vp is the current namei directory 264 * ndp is the name to locate in that directory... 265 */ 266 int 267 fdesc_lookup(v) 268 void *v; 269 { 270 struct vop_lookup_args /* { 271 struct vnode * a_dvp; 272 struct vnode ** a_vpp; 273 struct componentname * a_cnp; 274 } */ *ap = v; 275 struct vnode **vpp = ap->a_vpp; 276 struct vnode *dvp = ap->a_dvp; 277 struct componentname *cnp = ap->a_cnp; 278 struct lwp *l = cnp->cn_lwp; 279 const char *pname = cnp->cn_nameptr; 280 struct proc *p = l->l_proc; 281 int numfiles = p->p_fd->fd_nfiles; 282 unsigned fd = 0; 283 int error; 284 struct vnode *fvp; 285 const char *ln; 286 287 if (cnp->cn_namelen == 1 && *pname == '.') { 288 *vpp = dvp; 289 VREF(dvp); 290 return (0); 291 } 292 293 switch (VTOFDESC(dvp)->fd_type) { 294 default: 295 case Flink: 296 case Fdesc: 297 case Fctty: 298 error = ENOTDIR; 299 goto bad; 300 301 case Froot: 302 if (cnp->cn_namelen == 2 && memcmp(pname, "fd", 2) == 0) { 303 error = fdesc_allocvp(Fdevfd, FD_DEVFD, dvp->v_mount, &fvp); 304 if (error) 305 goto bad; 306 *vpp = fvp; 307 fvp->v_type = VDIR; 308 goto good; 309 } 310 311 if (cnp->cn_namelen == 3 && memcmp(pname, "tty", 3) == 0) { 312 struct vnode *ttyvp = cttyvp(p); 313 if (ttyvp == NULL) { 314 error = ENXIO; 315 goto bad; 316 } 317 error = fdesc_allocvp(Fctty, FD_CTTY, dvp->v_mount, &fvp); 318 if (error) 319 goto bad; 320 *vpp = fvp; 321 fvp->v_type = VCHR; 322 goto good; 323 } 324 325 ln = 0; 326 switch (cnp->cn_namelen) { 327 case 5: 328 if (memcmp(pname, "stdin", 5) == 0) { 329 ln = "fd/0"; 330 fd = FD_STDIN; 331 } 332 break; 333 case 6: 334 if (memcmp(pname, "stdout", 6) == 0) { 335 ln = "fd/1"; 336 fd = FD_STDOUT; 337 } else 338 if (memcmp(pname, "stderr", 6) == 0) { 339 ln = "fd/2"; 340 fd = FD_STDERR; 341 } 342 break; 343 } 344 345 if (ln) { 346 error = fdesc_allocvp(Flink, fd, dvp->v_mount, &fvp); 347 if (error) 348 goto bad; 349 /* XXXUNCONST */ 350 VTOFDESC(fvp)->fd_link = __UNCONST(ln); 351 *vpp = fvp; 352 fvp->v_type = VLNK; 353 goto good; 354 } else { 355 error = ENOENT; 356 goto bad; 357 } 358 359 /* FALL THROUGH */ 360 361 case Fdevfd: 362 if (cnp->cn_namelen == 2 && memcmp(pname, "..", 2) == 0) { 363 VOP_UNLOCK(dvp, 0); 364 cnp->cn_flags |= PDIRUNLOCK; 365 error = fdesc_root(dvp->v_mount, vpp); 366 if (error) 367 goto bad; 368 /* 369 * If we're at the last component and need the 370 * parent locked, undo the unlock above. 371 */ 372 if (((~cnp->cn_flags & (ISLASTCN | LOCKPARENT)) == 0) && 373 ((error = vn_lock(dvp, LK_EXCLUSIVE)) == 0)) 374 cnp->cn_flags &= ~PDIRUNLOCK; 375 return (error); 376 } 377 378 fd = 0; 379 while (*pname >= '0' && *pname <= '9') { 380 fd = 10 * fd + *pname++ - '0'; 381 if (fd >= numfiles) 382 break; 383 } 384 385 if (*pname != '\0') { 386 error = ENOENT; 387 goto bad; 388 } 389 390 if (fd >= numfiles || p->p_fd->fd_ofiles[fd] == NULL || 391 FILE_IS_USABLE(p->p_fd->fd_ofiles[fd]) == 0) { 392 error = EBADF; 393 goto bad; 394 } 395 396 error = fdesc_allocvp(Fdesc, FD_DESC+fd, dvp->v_mount, &fvp); 397 if (error) 398 goto bad; 399 VTOFDESC(fvp)->fd_fd = fd; 400 *vpp = fvp; 401 goto good; 402 } 403 404 bad:; 405 *vpp = NULL; 406 return (error); 407 408 good:; 409 /* 410 * As "." was special cased above, we now unlock the parent if we're 411 * suppoed to. We're only supposed to not unlock if this is the 412 * last component, and the caller requested LOCKPARENT. So if either 413 * condition is false, unlock. 414 */ 415 if (((~cnp->cn_flags) & (ISLASTCN | LOCKPARENT)) != 0) { 416 VOP_UNLOCK(dvp, 0); 417 cnp->cn_flags |= PDIRUNLOCK; 418 } 419 return (0); 420 } 421 422 int 423 fdesc_open(v) 424 void *v; 425 { 426 struct vop_open_args /* { 427 struct vnode *a_vp; 428 int a_mode; 429 struct ucred *a_cred; 430 struct lwp *a_l; 431 } */ *ap = v; 432 struct vnode *vp = ap->a_vp; 433 434 switch (VTOFDESC(vp)->fd_type) { 435 case Fdesc: 436 /* 437 * XXX Kludge: set dupfd to contain the value of the 438 * the file descriptor being sought for duplication. The error 439 * return ensures that the vnode for this device will be 440 * released by vn_open. Open will detect this special error and 441 * take the actions in dupfdopen. Other callers of vn_open or 442 * VOP_OPEN will simply report the error. 443 */ 444 curlwp->l_dupfd = VTOFDESC(vp)->fd_fd; /* XXX */ 445 return EDUPFD; 446 447 case Fctty: 448 return ((*ctty_cdevsw.d_open)(devctty, ap->a_mode, 0, ap->a_l)); 449 case Froot: 450 case Fdevfd: 451 case Flink: 452 break; 453 } 454 455 return (0); 456 } 457 458 static int 459 fdesc_attr(fd, vap, cred, l) 460 int fd; 461 struct vattr *vap; 462 struct ucred *cred; 463 struct lwp *l; 464 { 465 struct proc *p = l->l_proc; 466 struct filedesc *fdp = p->p_fd; 467 struct file *fp; 468 struct stat stb; 469 int error; 470 471 if ((fp = fd_getfile(fdp, fd)) == NULL) 472 return (EBADF); 473 474 switch (fp->f_type) { 475 case DTYPE_VNODE: 476 simple_unlock(&fp->f_slock); 477 error = VOP_GETATTR((struct vnode *) fp->f_data, vap, cred, l); 478 if (error == 0 && vap->va_type == VDIR) { 479 /* 480 * directories can cause loops in the namespace, 481 * so turn off the 'x' bits to avoid trouble. 482 */ 483 vap->va_mode &= ~(S_IXUSR|S_IXGRP|S_IXOTH); 484 } 485 break; 486 487 default: 488 FILE_USE(fp); 489 memset(&stb, 0, sizeof(stb)); 490 error = (*fp->f_ops->fo_stat)(fp, &stb, l); 491 FILE_UNUSE(fp, l); 492 if (error) 493 break; 494 495 vattr_null(vap); 496 switch(fp->f_type) { 497 case DTYPE_SOCKET: 498 vap->va_type = VSOCK; 499 break; 500 case DTYPE_PIPE: 501 vap->va_type = VFIFO; 502 break; 503 default: 504 /* use VNON perhaps? */ 505 vap->va_type = VBAD; 506 break; 507 } 508 vap->va_mode = stb.st_mode; 509 vap->va_nlink = stb.st_nlink; 510 vap->va_uid = stb.st_uid; 511 vap->va_gid = stb.st_gid; 512 vap->va_fsid = stb.st_dev; 513 vap->va_fileid = stb.st_ino; 514 vap->va_size = stb.st_size; 515 vap->va_blocksize = stb.st_blksize; 516 vap->va_atime = stb.st_atimespec; 517 vap->va_mtime = stb.st_mtimespec; 518 vap->va_ctime = stb.st_ctimespec; 519 vap->va_gen = stb.st_gen; 520 vap->va_flags = stb.st_flags; 521 vap->va_rdev = stb.st_rdev; 522 vap->va_bytes = stb.st_blocks * stb.st_blksize; 523 break; 524 } 525 526 return (error); 527 } 528 529 int 530 fdesc_getattr(v) 531 void *v; 532 { 533 struct vop_getattr_args /* { 534 struct vnode *a_vp; 535 struct vattr *a_vap; 536 struct ucred *a_cred; 537 struct lwp *a_l; 538 } */ *ap = v; 539 struct vnode *vp = ap->a_vp; 540 struct vattr *vap = ap->a_vap; 541 unsigned fd; 542 int error = 0; 543 544 switch (VTOFDESC(vp)->fd_type) { 545 case Froot: 546 case Fdevfd: 547 case Flink: 548 case Fctty: 549 VATTR_NULL(vap); 550 vap->va_fileid = VTOFDESC(vp)->fd_ix; 551 552 #define R_ALL (S_IRUSR|S_IRGRP|S_IROTH) 553 #define W_ALL (S_IWUSR|S_IWGRP|S_IWOTH) 554 #define X_ALL (S_IXUSR|S_IXGRP|S_IXOTH) 555 556 switch (VTOFDESC(vp)->fd_type) { 557 case Flink: 558 vap->va_mode = R_ALL|X_ALL; 559 vap->va_type = VLNK; 560 vap->va_rdev = 0; 561 vap->va_nlink = 1; 562 vap->va_size = strlen(VTOFDESC(vp)->fd_link); 563 break; 564 565 case Fctty: 566 vap->va_mode = R_ALL|W_ALL; 567 vap->va_type = VCHR; 568 vap->va_rdev = devctty; 569 vap->va_nlink = 1; 570 vap->va_size = 0; 571 break; 572 573 default: 574 vap->va_mode = R_ALL|X_ALL; 575 vap->va_type = VDIR; 576 vap->va_rdev = 0; 577 vap->va_nlink = 2; 578 vap->va_size = DEV_BSIZE; 579 break; 580 } 581 vap->va_uid = 0; 582 vap->va_gid = 0; 583 vap->va_fsid = vp->v_mount->mnt_stat.f_fsidx.__fsid_val[0]; 584 vap->va_blocksize = DEV_BSIZE; 585 vap->va_atime.tv_sec = boottime.tv_sec; 586 vap->va_atime.tv_nsec = 0; 587 vap->va_mtime = vap->va_atime; 588 vap->va_ctime = vap->va_mtime; 589 vap->va_gen = 0; 590 vap->va_flags = 0; 591 vap->va_bytes = 0; 592 break; 593 594 case Fdesc: 595 fd = VTOFDESC(vp)->fd_fd; 596 error = fdesc_attr(fd, vap, ap->a_cred, ap->a_l); 597 break; 598 599 default: 600 panic("fdesc_getattr"); 601 break; 602 } 603 604 if (error == 0) 605 vp->v_type = vap->va_type; 606 607 return (error); 608 } 609 610 int 611 fdesc_setattr(v) 612 void *v; 613 { 614 struct vop_setattr_args /* { 615 struct vnode *a_vp; 616 struct vattr *a_vap; 617 struct ucred *a_cred; 618 struct lwp *a_l; 619 } */ *ap = v; 620 struct filedesc *fdp = ap->a_l->l_proc->p_fd; 621 struct file *fp; 622 unsigned fd; 623 624 /* 625 * Can't mess with the root vnode 626 */ 627 switch (VTOFDESC(ap->a_vp)->fd_type) { 628 case Fdesc: 629 break; 630 631 case Fctty: 632 return (0); 633 634 default: 635 return (EACCES); 636 } 637 638 fd = VTOFDESC(ap->a_vp)->fd_fd; 639 if ((fp = fd_getfile(fdp, fd)) == NULL) 640 return (EBADF); 641 642 /* 643 * XXX: Can't reasonably set the attr's on any types currently. 644 * On vnode's this will cause truncation and socket/pipes make 645 * no sense. 646 */ 647 simple_unlock(&fp->f_slock); 648 return (0); 649 } 650 651 652 struct fdesc_target { 653 ino_t ft_fileno; 654 u_char ft_type; 655 u_char ft_namlen; 656 const char *ft_name; 657 } fdesc_targets[] = { 658 #define N(s) sizeof(s)-1, s 659 { FD_DEVFD, DT_DIR, N("fd") }, 660 { FD_STDIN, DT_LNK, N("stdin") }, 661 { FD_STDOUT, DT_LNK, N("stdout") }, 662 { FD_STDERR, DT_LNK, N("stderr") }, 663 { FD_CTTY, DT_UNKNOWN, N("tty") }, 664 #undef N 665 #define UIO_MX _DIRENT_RECLEN((struct dirent *)NULL, sizeof("stderr") - 1) 666 }; 667 static int nfdesc_targets = sizeof(fdesc_targets) / sizeof(fdesc_targets[0]); 668 669 int 670 fdesc_readdir(v) 671 void *v; 672 { 673 struct vop_readdir_args /* { 674 struct vnode *a_vp; 675 struct uio *a_uio; 676 struct ucred *a_cred; 677 int *a_eofflag; 678 off_t **a_cookies; 679 int *a_ncookies; 680 } */ *ap = v; 681 struct uio *uio = ap->a_uio; 682 struct dirent d; 683 struct filedesc *fdp; 684 off_t i; 685 int j; 686 int error; 687 off_t *cookies = NULL; 688 int ncookies; 689 690 switch (VTOFDESC(ap->a_vp)->fd_type) { 691 case Fctty: 692 return 0; 693 694 case Fdesc: 695 return ENOTDIR; 696 697 default: 698 break; 699 } 700 701 fdp = uio->uio_lwp ? uio->uio_lwp->l_proc->p_fd : NULL; 702 703 if (uio->uio_resid < UIO_MX) 704 return EINVAL; 705 if (uio->uio_offset < 0) 706 return EINVAL; 707 708 error = 0; 709 i = uio->uio_offset; 710 (void)memset(&d, 0, UIO_MX); 711 d.d_reclen = UIO_MX; 712 if (ap->a_ncookies) 713 ncookies = uio->uio_resid / UIO_MX; 714 else 715 ncookies = 0; 716 717 if (VTOFDESC(ap->a_vp)->fd_type == Froot) { 718 struct fdesc_target *ft; 719 720 if (i >= nfdesc_targets) 721 return 0; 722 723 if (ap->a_ncookies) { 724 ncookies = min(ncookies, (nfdesc_targets - i)); 725 cookies = malloc(ncookies * sizeof(off_t), 726 M_TEMP, M_WAITOK); 727 *ap->a_cookies = cookies; 728 *ap->a_ncookies = ncookies; 729 } 730 731 for (ft = &fdesc_targets[i]; uio->uio_resid >= UIO_MX && 732 i < nfdesc_targets; ft++, i++) { 733 switch (ft->ft_fileno) { 734 case FD_CTTY: 735 if (uio->uio_lwp == NULL || 736 cttyvp(uio->uio_lwp->l_proc) == NULL) 737 continue; 738 break; 739 740 case FD_STDIN: 741 case FD_STDOUT: 742 case FD_STDERR: 743 if (fdp == NULL) 744 continue; 745 if ((ft->ft_fileno - FD_STDIN) >= 746 fdp->fd_nfiles) 747 continue; 748 if (fdp->fd_ofiles[ft->ft_fileno - FD_STDIN] 749 == NULL 750 || FILE_IS_USABLE( 751 fdp->fd_ofiles[ft->ft_fileno - FD_STDIN]) 752 == 0) 753 continue; 754 break; 755 } 756 757 d.d_fileno = ft->ft_fileno; 758 d.d_namlen = ft->ft_namlen; 759 (void)memcpy(d.d_name, ft->ft_name, ft->ft_namlen + 1); 760 d.d_type = ft->ft_type; 761 762 if ((error = uiomove(&d, UIO_MX, uio)) != 0) 763 break; 764 if (cookies) 765 *cookies++ = i + 1; 766 } 767 } else { 768 int nfdp = fdp ? fdp->fd_nfiles : 0; 769 if (ap->a_ncookies) { 770 ncookies = min(ncookies, nfdp + 2); 771 cookies = malloc(ncookies * sizeof(off_t), 772 M_TEMP, M_WAITOK); 773 *ap->a_cookies = cookies; 774 *ap->a_ncookies = ncookies; 775 } 776 for (; i - 2 < nfdp && uio->uio_resid >= UIO_MX; i++) { 777 switch (i) { 778 case 0: 779 case 1: 780 d.d_fileno = FD_ROOT; /* XXX */ 781 d.d_namlen = i + 1; 782 (void)memcpy(d.d_name, "..", d.d_namlen); 783 d.d_name[i + 1] = '\0'; 784 d.d_type = DT_DIR; 785 break; 786 787 default: 788 KASSERT(fdp != NULL); 789 j = (int)i - 2; 790 if (fdp->fd_ofiles[j] == NULL || 791 FILE_IS_USABLE(fdp->fd_ofiles[j]) == 0) 792 continue; 793 d.d_fileno = j + FD_STDIN; 794 d.d_namlen = sprintf(d.d_name, "%d", j); 795 d.d_type = DT_UNKNOWN; 796 break; 797 } 798 799 if ((error = uiomove(&d, UIO_MX, uio)) != 0) 800 break; 801 if (cookies) 802 *cookies++ = i + 1; 803 } 804 } 805 806 if (ap->a_ncookies && error) { 807 free(*ap->a_cookies, M_TEMP); 808 *ap->a_ncookies = 0; 809 *ap->a_cookies = NULL; 810 } 811 812 uio->uio_offset = i; 813 return error; 814 } 815 816 int 817 fdesc_readlink(v) 818 void *v; 819 { 820 struct vop_readlink_args /* { 821 struct vnode *a_vp; 822 struct uio *a_uio; 823 struct ucred *a_cred; 824 } */ *ap = v; 825 struct vnode *vp = ap->a_vp; 826 int error; 827 828 if (vp->v_type != VLNK) 829 return (EPERM); 830 831 if (VTOFDESC(vp)->fd_type == Flink) { 832 char *ln = VTOFDESC(vp)->fd_link; 833 error = uiomove(ln, strlen(ln), ap->a_uio); 834 } else { 835 error = EOPNOTSUPP; 836 } 837 838 return (error); 839 } 840 841 int 842 fdesc_read(v) 843 void *v; 844 { 845 struct vop_read_args /* { 846 struct vnode *a_vp; 847 struct uio *a_uio; 848 int a_ioflag; 849 struct ucred *a_cred; 850 } */ *ap = v; 851 int error = EOPNOTSUPP; 852 struct vnode *vp = ap->a_vp; 853 854 switch (VTOFDESC(vp)->fd_type) { 855 case Fctty: 856 VOP_UNLOCK(vp, 0); 857 error = (*ctty_cdevsw.d_read)(devctty, ap->a_uio, ap->a_ioflag); 858 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 859 break; 860 861 default: 862 error = EOPNOTSUPP; 863 break; 864 } 865 866 return (error); 867 } 868 869 int 870 fdesc_write(v) 871 void *v; 872 { 873 struct vop_write_args /* { 874 struct vnode *a_vp; 875 struct uio *a_uio; 876 int a_ioflag; 877 struct ucred *a_cred; 878 } */ *ap = v; 879 int error = EOPNOTSUPP; 880 struct vnode *vp = ap->a_vp; 881 882 switch (VTOFDESC(vp)->fd_type) { 883 case Fctty: 884 VOP_UNLOCK(vp, 0); 885 error = (*ctty_cdevsw.d_write)(devctty, ap->a_uio, 886 ap->a_ioflag); 887 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 888 break; 889 890 default: 891 error = EOPNOTSUPP; 892 break; 893 } 894 895 return (error); 896 } 897 898 int 899 fdesc_ioctl(v) 900 void *v; 901 { 902 struct vop_ioctl_args /* { 903 struct vnode *a_vp; 904 u_long a_command; 905 void *a_data; 906 int a_fflag; 907 struct ucred *a_cred; 908 struct lwp *a_l; 909 } */ *ap = v; 910 int error = EOPNOTSUPP; 911 912 switch (VTOFDESC(ap->a_vp)->fd_type) { 913 case Fctty: 914 error = (*ctty_cdevsw.d_ioctl)(devctty, ap->a_command, 915 ap->a_data, ap->a_fflag, 916 ap->a_l); 917 break; 918 919 default: 920 error = EOPNOTSUPP; 921 break; 922 } 923 924 return (error); 925 } 926 927 int 928 fdesc_poll(v) 929 void *v; 930 { 931 struct vop_poll_args /* { 932 struct vnode *a_vp; 933 int a_events; 934 struct lwp *a_l; 935 } */ *ap = v; 936 int revents; 937 938 switch (VTOFDESC(ap->a_vp)->fd_type) { 939 case Fctty: 940 revents = (*ctty_cdevsw.d_poll)(devctty, ap->a_events, ap->a_l); 941 break; 942 943 default: 944 revents = genfs_poll(v); 945 break; 946 } 947 948 return (revents); 949 } 950 951 int 952 fdesc_kqfilter(v) 953 void *v; 954 { 955 struct vop_kqfilter_args /* { 956 struct vnode *a_vp; 957 struct knote *a_kn; 958 } */ *ap = v; 959 int error; 960 struct proc *p; 961 struct lwp *l; 962 struct file *fp; 963 964 switch (VTOFDESC(ap->a_vp)->fd_type) { 965 case Fctty: 966 error = (*ctty_cdevsw.d_kqfilter)(devctty, ap->a_kn); 967 break; 968 969 case Fdesc: 970 /* just invoke kqfilter for the underlying descriptor */ 971 l = curlwp; /* XXX hopefully ok to use curproc here */ 972 p = l->l_proc; 973 if ((fp = fd_getfile(p->p_fd, VTOFDESC(ap->a_vp)->fd_fd)) == NULL) 974 return (1); 975 976 FILE_USE(fp); 977 error = (*fp->f_ops->fo_kqfilter)(fp, ap->a_kn); 978 FILE_UNUSE(fp, l); 979 break; 980 981 default: 982 return (genfs_kqfilter(v)); 983 } 984 985 return (error); 986 } 987 988 int 989 fdesc_inactive(v) 990 void *v; 991 { 992 struct vop_inactive_args /* { 993 struct vnode *a_vp; 994 struct lwp *a_l; 995 } */ *ap = v; 996 struct vnode *vp = ap->a_vp; 997 998 /* 999 * Clear out the v_type field to avoid 1000 * nasty things happening in vgone(). 1001 */ 1002 VOP_UNLOCK(vp, 0); 1003 vp->v_type = VNON; 1004 return (0); 1005 } 1006 1007 int 1008 fdesc_reclaim(v) 1009 void *v; 1010 { 1011 struct vop_reclaim_args /* { 1012 struct vnode *a_vp; 1013 } */ *ap = v; 1014 struct vnode *vp = ap->a_vp; 1015 struct fdescnode *fd = VTOFDESC(vp); 1016 1017 LIST_REMOVE(fd, fd_hash); 1018 FREE(vp->v_data, M_TEMP); 1019 vp->v_data = 0; 1020 1021 return (0); 1022 } 1023 1024 /* 1025 * Return POSIX pathconf information applicable to special devices. 1026 */ 1027 int 1028 fdesc_pathconf(v) 1029 void *v; 1030 { 1031 struct vop_pathconf_args /* { 1032 struct vnode *a_vp; 1033 int a_name; 1034 register_t *a_retval; 1035 } */ *ap = v; 1036 1037 switch (ap->a_name) { 1038 case _PC_LINK_MAX: 1039 *ap->a_retval = LINK_MAX; 1040 return (0); 1041 case _PC_MAX_CANON: 1042 *ap->a_retval = MAX_CANON; 1043 return (0); 1044 case _PC_MAX_INPUT: 1045 *ap->a_retval = MAX_INPUT; 1046 return (0); 1047 case _PC_PIPE_BUF: 1048 *ap->a_retval = PIPE_BUF; 1049 return (0); 1050 case _PC_CHOWN_RESTRICTED: 1051 *ap->a_retval = 1; 1052 return (0); 1053 case _PC_VDISABLE: 1054 *ap->a_retval = _POSIX_VDISABLE; 1055 return (0); 1056 case _PC_SYNC_IO: 1057 *ap->a_retval = 1; 1058 return (0); 1059 default: 1060 return (EINVAL); 1061 } 1062 /* NOTREACHED */ 1063 } 1064 1065 /* 1066 * Print out the contents of a /dev/fd vnode. 1067 */ 1068 /* ARGSUSED */ 1069 int 1070 fdesc_print(v) 1071 void *v; 1072 { 1073 printf("tag VT_NON, fdesc vnode\n"); 1074 return (0); 1075 } 1076 1077 int 1078 fdesc_link(v) 1079 void *v; 1080 { 1081 struct vop_link_args /* { 1082 struct vnode *a_dvp; 1083 struct vnode *a_vp; 1084 struct componentname *a_cnp; 1085 } */ *ap = v; 1086 1087 VOP_ABORTOP(ap->a_dvp, ap->a_cnp); 1088 vput(ap->a_dvp); 1089 return (EROFS); 1090 } 1091 1092 int 1093 fdesc_symlink(v) 1094 void *v; 1095 { 1096 struct vop_symlink_args /* { 1097 struct vnode *a_dvp; 1098 struct vnode **a_vpp; 1099 struct componentname *a_cnp; 1100 struct vattr *a_vap; 1101 char *a_target; 1102 } */ *ap = v; 1103 1104 VOP_ABORTOP(ap->a_dvp, ap->a_cnp); 1105 vput(ap->a_dvp); 1106 return (EROFS); 1107 } 1108