kern_descrip.c revision 95969
1/* 2 * Copyright (c) 1982, 1986, 1989, 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * (c) UNIX System Laboratories, Inc. 5 * All or some portions of this file are derived from material licensed 6 * to the University of California by American Telephone and Telegraph 7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8 * the permission of UNIX System Laboratories, Inc. 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. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the University of 21 * California, Berkeley and its contributors. 22 * 4. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 * 38 * @(#)kern_descrip.c 8.6 (Berkeley) 4/19/94 39 * $FreeBSD: head/sys/kern/kern_descrip.c 95969 2002-05-03 05:32:25Z tanimura $ 40 */ 41 42#include "opt_compat.h" 43 44#include <sys/param.h> 45#include <sys/systm.h> 46#include <sys/lock.h> 47#include <sys/malloc.h> 48#include <sys/mutex.h> 49#include <sys/sysproto.h> 50#include <sys/conf.h> 51#include <sys/filedesc.h> 52#include <sys/kernel.h> 53#include <sys/sysctl.h> 54#include <sys/vnode.h> 55#include <sys/proc.h> 56#include <sys/namei.h> 57#include <sys/file.h> 58#include <sys/stat.h> 59#include <sys/filio.h> 60#include <sys/fcntl.h> 61#include <sys/unistd.h> 62#include <sys/resourcevar.h> 63#include <sys/event.h> 64#include <sys/sx.h> 65#include <sys/socketvar.h> 66#include <sys/signalvar.h> 67 68#include <machine/limits.h> 69 70#include <vm/vm.h> 71#include <vm/vm_extern.h> 72#include <vm/uma.h> 73 74static MALLOC_DEFINE(M_FILEDESC, "file desc", "Open file descriptor table"); 75static MALLOC_DEFINE(M_SIGIO, "sigio", "sigio structures"); 76 77uma_zone_t file_zone; 78 79static d_open_t fdopen; 80#define NUMFDESC 64 81 82#define CDEV_MAJOR 22 83static struct cdevsw fildesc_cdevsw = { 84 /* open */ fdopen, 85 /* close */ noclose, 86 /* read */ noread, 87 /* write */ nowrite, 88 /* ioctl */ noioctl, 89 /* poll */ nopoll, 90 /* mmap */ nommap, 91 /* strategy */ nostrategy, 92 /* name */ "FD", 93 /* maj */ CDEV_MAJOR, 94 /* dump */ nodump, 95 /* psize */ nopsize, 96 /* flags */ 0, 97}; 98 99static int do_dup(struct filedesc *fdp, int old, int new, register_t *retval, struct thread *td); 100static int badfo_readwrite(struct file *fp, struct uio *uio, 101 struct ucred *cred, int flags, struct thread *td); 102static int badfo_ioctl(struct file *fp, u_long com, caddr_t data, 103 struct thread *td); 104static int badfo_poll(struct file *fp, int events, 105 struct ucred *cred, struct thread *td); 106static int badfo_kqfilter(struct file *fp, struct knote *kn); 107static int badfo_stat(struct file *fp, struct stat *sb, struct thread *td); 108static int badfo_close(struct file *fp, struct thread *td); 109 110/* 111 * Descriptor management. 112 */ 113struct filelist filehead; /* head of list of open files */ 114int nfiles; /* actual number of open files */ 115extern int cmask; 116struct sx filelist_lock; /* sx to protect filelist */ 117struct mtx sigio_lock; /* mtx to protect pointers to sigio */ 118 119/* 120 * System calls on descriptors. 121 */ 122#ifndef _SYS_SYSPROTO_H_ 123struct getdtablesize_args { 124 int dummy; 125}; 126#endif 127/* 128 * MPSAFE 129 */ 130/* ARGSUSED */ 131int 132getdtablesize(td, uap) 133 struct thread *td; 134 struct getdtablesize_args *uap; 135{ 136 struct proc *p = td->td_proc; 137 138 mtx_lock(&Giant); 139 td->td_retval[0] = 140 min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfilesperproc); 141 mtx_unlock(&Giant); 142 return (0); 143} 144 145/* 146 * Duplicate a file descriptor to a particular value. 147 * 148 * note: keep in mind that a potential race condition exists when closing 149 * descriptors from a shared descriptor table (via rfork). 150 */ 151#ifndef _SYS_SYSPROTO_H_ 152struct dup2_args { 153 u_int from; 154 u_int to; 155}; 156#endif 157/* 158 * MPSAFE 159 */ 160/* ARGSUSED */ 161int 162dup2(td, uap) 163 struct thread *td; 164 struct dup2_args *uap; 165{ 166 struct proc *p = td->td_proc; 167 register struct filedesc *fdp = td->td_proc->p_fd; 168 register u_int old = uap->from, new = uap->to; 169 int i, error; 170 171 FILEDESC_LOCK(fdp); 172retry: 173 if (old >= fdp->fd_nfiles || 174 fdp->fd_ofiles[old] == NULL || 175 new >= p->p_rlimit[RLIMIT_NOFILE].rlim_cur || 176 new >= maxfilesperproc) { 177 FILEDESC_UNLOCK(fdp); 178 return (EBADF); 179 } 180 if (old == new) { 181 td->td_retval[0] = new; 182 FILEDESC_UNLOCK(fdp); 183 return (0); 184 } 185 if (new >= fdp->fd_nfiles) { 186 if ((error = fdalloc(td, new, &i))) { 187 FILEDESC_UNLOCK(fdp); 188 return (error); 189 } 190 /* 191 * fdalloc() may block, retest everything. 192 */ 193 goto retry; 194 } 195 error = do_dup(fdp, (int)old, (int)new, td->td_retval, td); 196 return(error); 197} 198 199/* 200 * Duplicate a file descriptor. 201 */ 202#ifndef _SYS_SYSPROTO_H_ 203struct dup_args { 204 u_int fd; 205}; 206#endif 207/* 208 * MPSAFE 209 */ 210/* ARGSUSED */ 211int 212dup(td, uap) 213 struct thread *td; 214 struct dup_args *uap; 215{ 216 register struct filedesc *fdp; 217 u_int old; 218 int new, error; 219 220 old = uap->fd; 221 fdp = td->td_proc->p_fd; 222 FILEDESC_LOCK(fdp); 223 if (old >= fdp->fd_nfiles || fdp->fd_ofiles[old] == NULL) { 224 FILEDESC_UNLOCK(fdp); 225 return (EBADF); 226 } 227 if ((error = fdalloc(td, 0, &new))) { 228 FILEDESC_UNLOCK(fdp); 229 return (error); 230 } 231 error = do_dup(fdp, (int)old, new, td->td_retval, td); 232 return (error); 233} 234 235/* 236 * The file control system call. 237 */ 238#ifndef _SYS_SYSPROTO_H_ 239struct fcntl_args { 240 int fd; 241 int cmd; 242 long arg; 243}; 244#endif 245/* 246 * MPSAFE 247 */ 248/* ARGSUSED */ 249int 250fcntl(td, uap) 251 struct thread *td; 252 register struct fcntl_args *uap; 253{ 254 register struct proc *p = td->td_proc; 255 register struct filedesc *fdp; 256 register struct file *fp; 257 register char *pop; 258 struct vnode *vp; 259 int i, tmp, error = 0, flg = F_POSIX; 260 struct flock fl; 261 u_int newmin; 262 struct proc *leaderp; 263 264 mtx_lock(&Giant); 265 266 fdp = p->p_fd; 267 FILEDESC_LOCK(fdp); 268 if ((unsigned)uap->fd >= fdp->fd_nfiles || 269 (fp = fdp->fd_ofiles[uap->fd]) == NULL) { 270 FILEDESC_UNLOCK(fdp); 271 error = EBADF; 272 goto done2; 273 } 274 pop = &fdp->fd_ofileflags[uap->fd]; 275 276 switch (uap->cmd) { 277 case F_DUPFD: 278 newmin = uap->arg; 279 if (newmin >= p->p_rlimit[RLIMIT_NOFILE].rlim_cur || 280 newmin >= maxfilesperproc) { 281 FILEDESC_UNLOCK(fdp); 282 error = EINVAL; 283 break; 284 } 285 if ((error = fdalloc(td, newmin, &i))) { 286 FILEDESC_UNLOCK(fdp); 287 break; 288 } 289 error = do_dup(fdp, uap->fd, i, td->td_retval, td); 290 break; 291 292 case F_GETFD: 293 td->td_retval[0] = (*pop & UF_EXCLOSE) ? FD_CLOEXEC : 0; 294 FILEDESC_UNLOCK(fdp); 295 break; 296 297 case F_SETFD: 298 *pop = (*pop &~ UF_EXCLOSE) | 299 (uap->arg & FD_CLOEXEC ? UF_EXCLOSE : 0); 300 FILEDESC_UNLOCK(fdp); 301 break; 302 303 case F_GETFL: 304 FILE_LOCK(fp); 305 FILEDESC_UNLOCK(fdp); 306 td->td_retval[0] = OFLAGS(fp->f_flag); 307 FILE_UNLOCK(fp); 308 break; 309 310 case F_SETFL: 311 fhold(fp); 312 FILEDESC_UNLOCK(fdp); 313 fp->f_flag &= ~FCNTLFLAGS; 314 fp->f_flag |= FFLAGS(uap->arg & ~O_ACCMODE) & FCNTLFLAGS; 315 tmp = fp->f_flag & FNONBLOCK; 316 error = fo_ioctl(fp, FIONBIO, (caddr_t)&tmp, td); 317 if (error) { 318 fdrop(fp, td); 319 break; 320 } 321 tmp = fp->f_flag & FASYNC; 322 error = fo_ioctl(fp, FIOASYNC, (caddr_t)&tmp, td); 323 if (!error) { 324 fdrop(fp, td); 325 break; 326 } 327 fp->f_flag &= ~FNONBLOCK; 328 tmp = 0; 329 (void)fo_ioctl(fp, FIONBIO, (caddr_t)&tmp, td); 330 fdrop(fp, td); 331 break; 332 333 case F_GETOWN: 334 fhold(fp); 335 FILEDESC_UNLOCK(fdp); 336 error = fo_ioctl(fp, FIOGETOWN, (caddr_t)td->td_retval, td); 337 fdrop(fp, td); 338 break; 339 340 case F_SETOWN: 341 fhold(fp); 342 FILEDESC_UNLOCK(fdp); 343 error = fo_ioctl(fp, FIOSETOWN, (caddr_t)&uap->arg, td); 344 fdrop(fp, td); 345 break; 346 347 case F_SETLKW: 348 flg |= F_WAIT; 349 /* Fall into F_SETLK */ 350 351 case F_SETLK: 352 if (fp->f_type != DTYPE_VNODE) { 353 FILEDESC_UNLOCK(fdp); 354 error = EBADF; 355 break; 356 } 357 vp = (struct vnode *)fp->f_data; 358 /* 359 * copyin/lockop may block 360 */ 361 fhold(fp); 362 FILEDESC_UNLOCK(fdp); 363 vp = (struct vnode *)fp->f_data; 364 365 /* Copy in the lock structure */ 366 error = copyin((caddr_t)(intptr_t)uap->arg, (caddr_t)&fl, 367 sizeof(fl)); 368 if (error) { 369 fdrop(fp, td); 370 break; 371 } 372 if (fl.l_whence == SEEK_CUR) { 373 if (fp->f_offset < 0 || 374 (fl.l_start > 0 && 375 fp->f_offset > OFF_MAX - fl.l_start)) { 376 fdrop(fp, td); 377 error = EOVERFLOW; 378 break; 379 } 380 fl.l_start += fp->f_offset; 381 } 382 383 switch (fl.l_type) { 384 case F_RDLCK: 385 if ((fp->f_flag & FREAD) == 0) { 386 error = EBADF; 387 break; 388 } 389 PROC_LOCK(p); 390 p->p_flag |= P_ADVLOCK; 391 leaderp = p->p_leader; 392 PROC_UNLOCK(p); 393 error = VOP_ADVLOCK(vp, (caddr_t)leaderp, F_SETLK, 394 &fl, flg); 395 break; 396 case F_WRLCK: 397 if ((fp->f_flag & FWRITE) == 0) { 398 error = EBADF; 399 break; 400 } 401 PROC_LOCK(p); 402 p->p_flag |= P_ADVLOCK; 403 leaderp = p->p_leader; 404 PROC_UNLOCK(p); 405 error = VOP_ADVLOCK(vp, (caddr_t)leaderp, F_SETLK, 406 &fl, flg); 407 break; 408 case F_UNLCK: 409 PROC_LOCK(p); 410 leaderp = p->p_leader; 411 PROC_UNLOCK(p); 412 error = VOP_ADVLOCK(vp, (caddr_t)leaderp, F_UNLCK, 413 &fl, F_POSIX); 414 break; 415 default: 416 error = EINVAL; 417 break; 418 } 419 fdrop(fp, td); 420 break; 421 422 case F_GETLK: 423 if (fp->f_type != DTYPE_VNODE) { 424 FILEDESC_UNLOCK(fdp); 425 error = EBADF; 426 break; 427 } 428 vp = (struct vnode *)fp->f_data; 429 /* 430 * copyin/lockop may block 431 */ 432 fhold(fp); 433 FILEDESC_UNLOCK(fdp); 434 vp = (struct vnode *)fp->f_data; 435 436 /* Copy in the lock structure */ 437 error = copyin((caddr_t)(intptr_t)uap->arg, (caddr_t)&fl, 438 sizeof(fl)); 439 if (error) { 440 fdrop(fp, td); 441 break; 442 } 443 if (fl.l_type != F_RDLCK && fl.l_type != F_WRLCK && 444 fl.l_type != F_UNLCK) { 445 fdrop(fp, td); 446 error = EINVAL; 447 break; 448 } 449 if (fl.l_whence == SEEK_CUR) { 450 if ((fl.l_start > 0 && 451 fp->f_offset > OFF_MAX - fl.l_start) || 452 (fl.l_start < 0 && 453 fp->f_offset < OFF_MIN - fl.l_start)) { 454 fdrop(fp, td); 455 error = EOVERFLOW; 456 break; 457 } 458 fl.l_start += fp->f_offset; 459 } 460 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_GETLK, 461 &fl, F_POSIX); 462 fdrop(fp, td); 463 if (error == 0) { 464 error = copyout((caddr_t)&fl, 465 (caddr_t)(intptr_t)uap->arg, sizeof(fl)); 466 } 467 break; 468 default: 469 FILEDESC_UNLOCK(fdp); 470 error = EINVAL; 471 break; 472 } 473done2: 474 mtx_unlock(&Giant); 475 return (error); 476} 477 478/* 479 * Common code for dup, dup2, and fcntl(F_DUPFD). 480 * filedesc must be locked, but will be unlocked as a side effect. 481 */ 482static int 483do_dup(fdp, old, new, retval, td) 484 register struct filedesc *fdp; 485 register int old, new; 486 register_t *retval; 487 struct thread *td; 488{ 489 struct file *fp; 490 struct file *delfp; 491 492 FILEDESC_LOCK_ASSERT(fdp, MA_OWNED); 493 494 /* 495 * Save info on the descriptor being overwritten. We have 496 * to do the unmap now, but we cannot close it without 497 * introducing an ownership race for the slot. 498 */ 499 delfp = fdp->fd_ofiles[new]; 500#if 0 501 if (delfp && (fdp->fd_ofileflags[new] & UF_MAPPED)) 502 (void) munmapfd(td, new); 503#endif 504 505 /* 506 * Duplicate the source descriptor, update lastfile 507 */ 508 fp = fdp->fd_ofiles[old]; 509 fdp->fd_ofiles[new] = fp; 510 fdp->fd_ofileflags[new] = fdp->fd_ofileflags[old] &~ UF_EXCLOSE; 511 fhold(fp); 512 if (new > fdp->fd_lastfile) 513 fdp->fd_lastfile = new; 514 *retval = new; 515 516 FILEDESC_UNLOCK(fdp); 517 518 /* 519 * If we dup'd over a valid file, we now own the reference to it 520 * and must dispose of it using closef() semantics (as if a 521 * close() were performed on it). 522 */ 523 if (delfp) { 524 mtx_lock(&Giant); 525 (void) closef(delfp, td); 526 mtx_unlock(&Giant); 527 } 528 return (0); 529} 530 531/* 532 * If sigio is on the list associated with a process or process group, 533 * disable signalling from the device, remove sigio from the list and 534 * free sigio. 535 */ 536void 537funsetown(sigio) 538 struct sigio *sigio; 539{ 540 541 SIGIO_LOCK(); 542 if (sigio == NULL) { 543 SIGIO_UNLOCK(); 544 return; 545 } 546 *(sigio->sio_myref) = NULL; 547 SIGIO_UNLOCK(); 548 if ((sigio)->sio_pgid < 0) { 549 struct pgrp *pg = (sigio)->sio_pgrp; 550 PGRP_LOCK(pg); 551 SLIST_REMOVE(&sigio->sio_pgrp->pg_sigiolst, sigio, 552 sigio, sio_pgsigio); 553 PGRP_UNLOCK(pg); 554 } else { 555 struct proc *p = (sigio)->sio_proc; 556 PROC_LOCK(p); 557 SLIST_REMOVE(&sigio->sio_proc->p_sigiolst, sigio, 558 sigio, sio_pgsigio); 559 PROC_UNLOCK(p); 560 } 561 crfree(sigio->sio_ucred); 562 FREE(sigio, M_SIGIO); 563} 564 565/* Free a list of sigio structures. */ 566void 567funsetownlst(sigiolst) 568 struct sigiolst *sigiolst; 569{ 570 struct sigio *sigio; 571 struct proc *p; 572 struct pgrp *pg; 573 574 SIGIO_ASSERT(MA_OWNED); 575 576 sigio = SLIST_FIRST(sigiolst); 577 if (sigio == NULL) 578 return; 579 580 p = NULL; 581 pg = NULL; 582 583 /* 584 * Every entry of the list should belong 585 * to a single proc or pgrp. 586 */ 587 if (sigio->sio_pgid < 0) { 588 pg = sigio->sio_pgrp; 589 PGRP_LOCK_ASSERT(pg, MA_OWNED); 590 } else /* if (sigio->sio_pgid > 0) */ { 591 p = sigio->sio_proc; 592 PROC_LOCK_ASSERT(p, MA_OWNED); 593 } 594 595 while ((sigio = SLIST_FIRST(sigiolst)) != NULL) { 596 *(sigio->sio_myref) = NULL; 597 if (pg != NULL) { 598 KASSERT(sigio->sio_pgid < 0, ("Proc sigio in pgrp sigio list")); 599 KASSERT(sigio->sio_pgrp == pg, ("Bogus pgrp in sigio list")); 600 SLIST_REMOVE(&pg->pg_sigiolst, sigio, sigio, sio_pgsigio); 601 PGRP_UNLOCK(pg); 602 SIGIO_UNLOCK(); 603 crfree(sigio->sio_ucred); 604 mtx_lock(&Giant); 605 FREE(sigio, M_SIGIO); 606 mtx_unlock(&Giant); 607 SIGIO_LOCK(); 608 PGRP_LOCK(pg); 609 } else /* if (p != NULL) */ { 610 KASSERT(sigio->sio_pgid > 0, ("Pgrp sigio in proc sigio list")); 611 KASSERT(sigio->sio_proc == p, ("Bogus proc in sigio list")); 612 SLIST_REMOVE(&p->p_sigiolst, sigio, sigio, sio_pgsigio); 613 PROC_UNLOCK(p); 614 SIGIO_UNLOCK(); 615 crfree(sigio->sio_ucred); 616 mtx_lock(&Giant); 617 FREE(sigio, M_SIGIO); 618 mtx_unlock(&Giant); 619 SIGIO_LOCK(); 620 PROC_LOCK(p); 621 } 622 } 623} 624 625/* 626 * This is common code for FIOSETOWN ioctl called by fcntl(fd, F_SETOWN, arg). 627 * 628 * After permission checking, add a sigio structure to the sigio list for 629 * the process or process group. 630 */ 631int 632fsetown(pgid, sigiop) 633 pid_t pgid; 634 struct sigio **sigiop; 635{ 636 struct proc *proc; 637 struct pgrp *pgrp; 638 struct sigio *sigio; 639 int ret; 640 641 if (pgid == 0) { 642 funsetown(*sigiop); 643 return (0); 644 } 645 646 ret = 0; 647 648 /* Allocate and fill in the new sigio out of locks. */ 649 MALLOC(sigio, struct sigio *, sizeof(struct sigio), M_SIGIO, M_WAITOK); 650 sigio->sio_pgid = pgid; 651 sigio->sio_ucred = crhold(curthread->td_ucred); 652 sigio->sio_myref = sigiop; 653 654 sx_slock(&proctree_lock); 655 if (pgid > 0) { 656 proc = pfind(pgid); 657 if (proc == NULL) { 658 ret = ESRCH; 659 goto fail; 660 } 661 662 /* 663 * Policy - Don't allow a process to FSETOWN a process 664 * in another session. 665 * 666 * Remove this test to allow maximum flexibility or 667 * restrict FSETOWN to the current process or process 668 * group for maximum safety. 669 */ 670 PROC_UNLOCK(proc); 671 if (proc->p_session != curthread->td_proc->p_session) { 672 ret = EPERM; 673 goto fail; 674 } 675 676 pgrp = NULL; 677 } else /* if (pgid < 0) */ { 678 pgrp = pgfind(-pgid); 679 if (pgrp == NULL) { 680 ret = ESRCH; 681 goto fail; 682 } 683 PGRP_UNLOCK(pgrp); 684 685 /* 686 * Policy - Don't allow a process to FSETOWN a process 687 * in another session. 688 * 689 * Remove this test to allow maximum flexibility or 690 * restrict FSETOWN to the current process or process 691 * group for maximum safety. 692 */ 693 if (pgrp->pg_session != curthread->td_proc->p_session) { 694 ret = EPERM; 695 goto fail; 696 } 697 698 proc = NULL; 699 } 700 funsetown(*sigiop); 701 if (pgid > 0) { 702 PROC_LOCK(proc); 703 SLIST_INSERT_HEAD(&proc->p_sigiolst, sigio, sio_pgsigio); 704 sigio->sio_proc = proc; 705 PROC_UNLOCK(proc); 706 } else { 707 PGRP_LOCK(pgrp); 708 SLIST_INSERT_HEAD(&pgrp->pg_sigiolst, sigio, sio_pgsigio); 709 sigio->sio_pgrp = pgrp; 710 PGRP_UNLOCK(pgrp); 711 } 712 sx_sunlock(&proctree_lock); 713 SIGIO_LOCK(); 714 *sigiop = sigio; 715 SIGIO_UNLOCK(); 716 return (0); 717 718fail: 719 sx_sunlock(&proctree_lock); 720 crfree(sigio->sio_ucred); 721 FREE(sigio, M_SIGIO); 722 return (ret); 723} 724 725/* 726 * This is common code for FIOGETOWN ioctl called by fcntl(fd, F_GETOWN, arg). 727 */ 728pid_t 729fgetown(sigio) 730 struct sigio *sigio; 731{ 732 return (sigio != NULL ? sigio->sio_pgid : 0); 733} 734 735/* 736 * Close a file descriptor. 737 */ 738#ifndef _SYS_SYSPROTO_H_ 739struct close_args { 740 int fd; 741}; 742#endif 743/* 744 * MPSAFE 745 */ 746/* ARGSUSED */ 747int 748close(td, uap) 749 struct thread *td; 750 struct close_args *uap; 751{ 752 register struct filedesc *fdp; 753 register struct file *fp; 754 register int fd = uap->fd; 755 int error = 0; 756 757 mtx_lock(&Giant); 758 fdp = td->td_proc->p_fd; 759 FILEDESC_LOCK(fdp); 760 if ((unsigned)fd >= fdp->fd_nfiles || 761 (fp = fdp->fd_ofiles[fd]) == NULL) { 762 FILEDESC_UNLOCK(fdp); 763 error = EBADF; 764 goto done2; 765 } 766#if 0 767 if (fdp->fd_ofileflags[fd] & UF_MAPPED) 768 (void) munmapfd(td, fd); 769#endif 770 fdp->fd_ofiles[fd] = NULL; 771 fdp->fd_ofileflags[fd] = 0; 772 773 /* 774 * we now hold the fp reference that used to be owned by the descriptor 775 * array. 776 */ 777 while (fdp->fd_lastfile > 0 && fdp->fd_ofiles[fdp->fd_lastfile] == NULL) 778 fdp->fd_lastfile--; 779 if (fd < fdp->fd_freefile) 780 fdp->fd_freefile = fd; 781 if (fd < fdp->fd_knlistsize) { 782 FILEDESC_UNLOCK(fdp); 783 knote_fdclose(td, fd); 784 } else 785 FILEDESC_UNLOCK(fdp); 786 787 error = closef(fp, td); 788done2: 789 mtx_unlock(&Giant); 790 return(error); 791} 792 793#if defined(COMPAT_43) || defined(COMPAT_SUNOS) 794/* 795 * Return status information about a file descriptor. 796 */ 797#ifndef _SYS_SYSPROTO_H_ 798struct ofstat_args { 799 int fd; 800 struct ostat *sb; 801}; 802#endif 803/* 804 * MPSAFE 805 */ 806/* ARGSUSED */ 807int 808ofstat(td, uap) 809 struct thread *td; 810 register struct ofstat_args *uap; 811{ 812 struct file *fp; 813 struct stat ub; 814 struct ostat oub; 815 int error; 816 817 mtx_lock(&Giant); 818 if ((error = fget(td, uap->fd, &fp)) != 0) 819 goto done2; 820 error = fo_stat(fp, &ub, td); 821 if (error == 0) { 822 cvtstat(&ub, &oub); 823 error = copyout((caddr_t)&oub, (caddr_t)uap->sb, sizeof (oub)); 824 } 825 fdrop(fp, td); 826done2: 827 mtx_unlock(&Giant); 828 return (error); 829} 830#endif /* COMPAT_43 || COMPAT_SUNOS */ 831 832/* 833 * Return status information about a file descriptor. 834 */ 835#ifndef _SYS_SYSPROTO_H_ 836struct fstat_args { 837 int fd; 838 struct stat *sb; 839}; 840#endif 841/* 842 * MPSAFE 843 */ 844/* ARGSUSED */ 845int 846fstat(td, uap) 847 struct thread *td; 848 struct fstat_args *uap; 849{ 850 struct file *fp; 851 struct stat ub; 852 int error; 853 854 mtx_lock(&Giant); 855 if ((error = fget(td, uap->fd, &fp)) != 0) 856 goto done2; 857 error = fo_stat(fp, &ub, td); 858 if (error == 0) 859 error = copyout((caddr_t)&ub, (caddr_t)uap->sb, sizeof (ub)); 860 fdrop(fp, td); 861done2: 862 mtx_unlock(&Giant); 863 return (error); 864} 865 866/* 867 * Return status information about a file descriptor. 868 */ 869#ifndef _SYS_SYSPROTO_H_ 870struct nfstat_args { 871 int fd; 872 struct nstat *sb; 873}; 874#endif 875/* 876 * MPSAFE 877 */ 878/* ARGSUSED */ 879int 880nfstat(td, uap) 881 struct thread *td; 882 register struct nfstat_args *uap; 883{ 884 struct file *fp; 885 struct stat ub; 886 struct nstat nub; 887 int error; 888 889 mtx_lock(&Giant); 890 if ((error = fget(td, uap->fd, &fp)) != 0) 891 goto done2; 892 error = fo_stat(fp, &ub, td); 893 if (error == 0) { 894 cvtnstat(&ub, &nub); 895 error = copyout((caddr_t)&nub, (caddr_t)uap->sb, sizeof (nub)); 896 } 897 fdrop(fp, td); 898done2: 899 mtx_unlock(&Giant); 900 return (error); 901} 902 903/* 904 * Return pathconf information about a file descriptor. 905 */ 906#ifndef _SYS_SYSPROTO_H_ 907struct fpathconf_args { 908 int fd; 909 int name; 910}; 911#endif 912/* 913 * MPSAFE 914 */ 915/* ARGSUSED */ 916int 917fpathconf(td, uap) 918 struct thread *td; 919 register struct fpathconf_args *uap; 920{ 921 struct file *fp; 922 struct vnode *vp; 923 int error; 924 925 if ((error = fget(td, uap->fd, &fp)) != 0) 926 return (error); 927 928 switch (fp->f_type) { 929 case DTYPE_PIPE: 930 case DTYPE_SOCKET: 931 if (uap->name != _PC_PIPE_BUF) { 932 error = EINVAL; 933 } else { 934 td->td_retval[0] = PIPE_BUF; 935 error = 0; 936 } 937 break; 938 case DTYPE_FIFO: 939 case DTYPE_VNODE: 940 vp = (struct vnode *)fp->f_data; 941 mtx_lock(&Giant); 942 error = VOP_PATHCONF(vp, uap->name, td->td_retval); 943 mtx_unlock(&Giant); 944 break; 945 default: 946 error = EOPNOTSUPP; 947 break; 948 } 949 fdrop(fp, td); 950 return(error); 951} 952 953/* 954 * Allocate a file descriptor for the process. 955 */ 956static int fdexpand; 957SYSCTL_INT(_debug, OID_AUTO, fdexpand, CTLFLAG_RD, &fdexpand, 0, ""); 958 959int 960fdalloc(td, want, result) 961 struct thread *td; 962 int want; 963 int *result; 964{ 965 struct proc *p = td->td_proc; 966 register struct filedesc *fdp = td->td_proc->p_fd; 967 register int i; 968 int lim, last, nfiles; 969 struct file **newofile, **oldofile; 970 char *newofileflags; 971 972 FILEDESC_LOCK_ASSERT(fdp, MA_OWNED); 973 974 /* 975 * Search for a free descriptor starting at the higher 976 * of want or fd_freefile. If that fails, consider 977 * expanding the ofile array. 978 */ 979 lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfilesperproc); 980 for (;;) { 981 last = min(fdp->fd_nfiles, lim); 982 if ((i = want) < fdp->fd_freefile) 983 i = fdp->fd_freefile; 984 for (; i < last; i++) { 985 if (fdp->fd_ofiles[i] == NULL) { 986 fdp->fd_ofileflags[i] = 0; 987 if (i > fdp->fd_lastfile) 988 fdp->fd_lastfile = i; 989 if (want <= fdp->fd_freefile) 990 fdp->fd_freefile = i; 991 *result = i; 992 return (0); 993 } 994 } 995 996 /* 997 * No space in current array. Expand? 998 */ 999 if (fdp->fd_nfiles >= lim) 1000 return (EMFILE); 1001 if (fdp->fd_nfiles < NDEXTENT) 1002 nfiles = NDEXTENT; 1003 else 1004 nfiles = 2 * fdp->fd_nfiles; 1005 FILEDESC_UNLOCK(fdp); 1006 mtx_lock(&Giant); 1007 MALLOC(newofile, struct file **, nfiles * OFILESIZE, 1008 M_FILEDESC, M_WAITOK); 1009 mtx_unlock(&Giant); 1010 FILEDESC_LOCK(fdp); 1011 1012 /* 1013 * deal with file-table extend race that might have occured 1014 * when malloc was blocked. 1015 */ 1016 if (fdp->fd_nfiles >= nfiles) { 1017 FILEDESC_UNLOCK(fdp); 1018 mtx_lock(&Giant); 1019 FREE(newofile, M_FILEDESC); 1020 mtx_unlock(&Giant); 1021 FILEDESC_LOCK(fdp); 1022 continue; 1023 } 1024 newofileflags = (char *) &newofile[nfiles]; 1025 /* 1026 * Copy the existing ofile and ofileflags arrays 1027 * and zero the new portion of each array. 1028 */ 1029 bcopy(fdp->fd_ofiles, newofile, 1030 (i = sizeof(struct file *) * fdp->fd_nfiles)); 1031 bzero((char *)newofile + i, nfiles * sizeof(struct file *) - i); 1032 bcopy(fdp->fd_ofileflags, newofileflags, 1033 (i = sizeof(char) * fdp->fd_nfiles)); 1034 bzero(newofileflags + i, nfiles * sizeof(char) - i); 1035 if (fdp->fd_nfiles > NDFILE) 1036 oldofile = fdp->fd_ofiles; 1037 else 1038 oldofile = NULL; 1039 fdp->fd_ofiles = newofile; 1040 fdp->fd_ofileflags = newofileflags; 1041 fdp->fd_nfiles = nfiles; 1042 fdexpand++; 1043 if (oldofile != NULL) { 1044 FILEDESC_UNLOCK(fdp); 1045 mtx_lock(&Giant); 1046 FREE(oldofile, M_FILEDESC); 1047 mtx_unlock(&Giant); 1048 FILEDESC_LOCK(fdp); 1049 } 1050 } 1051 return (0); 1052} 1053 1054/* 1055 * Check to see whether n user file descriptors 1056 * are available to the process p. 1057 */ 1058int 1059fdavail(td, n) 1060 struct thread *td; 1061 register int n; 1062{ 1063 struct proc *p = td->td_proc; 1064 register struct filedesc *fdp = td->td_proc->p_fd; 1065 register struct file **fpp; 1066 register int i, lim, last; 1067 1068 FILEDESC_LOCK_ASSERT(fdp, MA_OWNED); 1069 1070 lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfilesperproc); 1071 if ((i = lim - fdp->fd_nfiles) > 0 && (n -= i) <= 0) 1072 return (1); 1073 1074 last = min(fdp->fd_nfiles, lim); 1075 fpp = &fdp->fd_ofiles[fdp->fd_freefile]; 1076 for (i = last - fdp->fd_freefile; --i >= 0; fpp++) { 1077 if (*fpp == NULL && --n <= 0) 1078 return (1); 1079 } 1080 return (0); 1081} 1082 1083/* 1084 * Create a new open file structure and allocate 1085 * a file decriptor for the process that refers to it. 1086 */ 1087int 1088falloc(td, resultfp, resultfd) 1089 register struct thread *td; 1090 struct file **resultfp; 1091 int *resultfd; 1092{ 1093 struct proc *p = td->td_proc; 1094 register struct file *fp, *fq; 1095 int error, i; 1096 1097 sx_xlock(&filelist_lock); 1098 if (nfiles >= maxfiles) { 1099 sx_xunlock(&filelist_lock); 1100 tablefull("file"); 1101 return (ENFILE); 1102 } 1103 nfiles++; 1104 sx_xunlock(&filelist_lock); 1105 /* 1106 * Allocate a new file descriptor. 1107 * If the process has file descriptor zero open, add to the list 1108 * of open files at that point, otherwise put it at the front of 1109 * the list of open files. 1110 */ 1111 fp = uma_zalloc(file_zone, M_WAITOK); 1112 bzero(fp, sizeof(*fp)); 1113 1114 /* 1115 * wait until after malloc (which may have blocked) returns before 1116 * allocating the slot, else a race might have shrunk it if we had 1117 * allocated it before the malloc. 1118 */ 1119 FILEDESC_LOCK(p->p_fd); 1120 if ((error = fdalloc(td, 0, &i))) { 1121 FILEDESC_UNLOCK(p->p_fd); 1122 sx_xlock(&filelist_lock); 1123 nfiles--; 1124 sx_xunlock(&filelist_lock); 1125 uma_zfree(file_zone, fp); 1126 return (error); 1127 } 1128 fp->f_mtxp = mtx_pool_alloc(); 1129 fp->f_gcflag = 0; 1130 fp->f_count = 1; 1131 fp->f_cred = crhold(td->td_ucred); 1132 fp->f_ops = &badfileops; 1133 fp->f_seqcount = 1; 1134 FILEDESC_UNLOCK(p->p_fd); 1135 sx_xlock(&filelist_lock); 1136 FILEDESC_LOCK(p->p_fd); 1137 if ((fq = p->p_fd->fd_ofiles[0])) { 1138 LIST_INSERT_AFTER(fq, fp, f_list); 1139 } else { 1140 LIST_INSERT_HEAD(&filehead, fp, f_list); 1141 } 1142 p->p_fd->fd_ofiles[i] = fp; 1143 FILEDESC_UNLOCK(p->p_fd); 1144 sx_xunlock(&filelist_lock); 1145 if (resultfp) 1146 *resultfp = fp; 1147 if (resultfd) 1148 *resultfd = i; 1149 return (0); 1150} 1151 1152/* 1153 * Free a file descriptor. 1154 */ 1155void 1156ffree(fp) 1157 register struct file *fp; 1158{ 1159 1160 KASSERT((fp->f_count == 0), ("ffree: fp_fcount not 0!")); 1161 sx_xlock(&filelist_lock); 1162 LIST_REMOVE(fp, f_list); 1163 nfiles--; 1164 sx_xunlock(&filelist_lock); 1165 crfree(fp->f_cred); 1166 uma_zfree(file_zone, fp); 1167} 1168 1169/* 1170 * Build a new filedesc structure. 1171 */ 1172struct filedesc * 1173fdinit(td) 1174 struct thread *td; 1175{ 1176 register struct filedesc0 *newfdp; 1177 register struct filedesc *fdp = td->td_proc->p_fd; 1178 1179 MALLOC(newfdp, struct filedesc0 *, sizeof(struct filedesc0), 1180 M_FILEDESC, M_WAITOK | M_ZERO); 1181 mtx_init(&newfdp->fd_fd.fd_mtx, FILEDESC_LOCK_DESC, NULL, MTX_DEF); 1182 FILEDESC_LOCK(&newfdp->fd_fd); 1183 newfdp->fd_fd.fd_cdir = fdp->fd_cdir; 1184 if (newfdp->fd_fd.fd_cdir) 1185 VREF(newfdp->fd_fd.fd_cdir); 1186 newfdp->fd_fd.fd_rdir = fdp->fd_rdir; 1187 if (newfdp->fd_fd.fd_rdir) 1188 VREF(newfdp->fd_fd.fd_rdir); 1189 newfdp->fd_fd.fd_jdir = fdp->fd_jdir; 1190 if (newfdp->fd_fd.fd_jdir) 1191 VREF(newfdp->fd_fd.fd_jdir); 1192 1193 /* Create the file descriptor table. */ 1194 newfdp->fd_fd.fd_refcnt = 1; 1195 newfdp->fd_fd.fd_cmask = cmask; 1196 newfdp->fd_fd.fd_ofiles = newfdp->fd_dfiles; 1197 newfdp->fd_fd.fd_ofileflags = newfdp->fd_dfileflags; 1198 newfdp->fd_fd.fd_nfiles = NDFILE; 1199 newfdp->fd_fd.fd_knlistsize = -1; 1200 FILEDESC_UNLOCK(&newfdp->fd_fd); 1201 1202 return (&newfdp->fd_fd); 1203} 1204 1205/* 1206 * Share a filedesc structure. 1207 */ 1208struct filedesc * 1209fdshare(p) 1210 struct proc *p; 1211{ 1212 FILEDESC_LOCK(p->p_fd); 1213 p->p_fd->fd_refcnt++; 1214 FILEDESC_UNLOCK(p->p_fd); 1215 return (p->p_fd); 1216} 1217 1218/* 1219 * Copy a filedesc structure. 1220 */ 1221struct filedesc * 1222fdcopy(td) 1223 struct thread *td; 1224{ 1225 register struct filedesc *newfdp, *fdp = td->td_proc->p_fd; 1226 register struct file **fpp; 1227 register int i, j; 1228 1229 /* Certain daemons might not have file descriptors. */ 1230 if (fdp == NULL) 1231 return (NULL); 1232 1233 FILEDESC_LOCK_ASSERT(fdp, MA_OWNED); 1234 1235 FILEDESC_UNLOCK(fdp); 1236 MALLOC(newfdp, struct filedesc *, sizeof(struct filedesc0), 1237 M_FILEDESC, M_WAITOK); 1238 FILEDESC_LOCK(fdp); 1239 bcopy(fdp, newfdp, sizeof(struct filedesc)); 1240 FILEDESC_UNLOCK(fdp); 1241 bzero(&newfdp->fd_mtx, sizeof(newfdp->fd_mtx)); 1242 mtx_init(&newfdp->fd_mtx, FILEDESC_LOCK_DESC, NULL, MTX_DEF); 1243 if (newfdp->fd_cdir) 1244 VREF(newfdp->fd_cdir); 1245 if (newfdp->fd_rdir) 1246 VREF(newfdp->fd_rdir); 1247 if (newfdp->fd_jdir) 1248 VREF(newfdp->fd_jdir); 1249 newfdp->fd_refcnt = 1; 1250 1251 /* 1252 * If the number of open files fits in the internal arrays 1253 * of the open file structure, use them, otherwise allocate 1254 * additional memory for the number of descriptors currently 1255 * in use. 1256 */ 1257 FILEDESC_LOCK(fdp); 1258 newfdp->fd_lastfile = fdp->fd_lastfile; 1259 newfdp->fd_nfiles = fdp->fd_nfiles; 1260 if (newfdp->fd_lastfile < NDFILE) { 1261 newfdp->fd_ofiles = ((struct filedesc0 *) newfdp)->fd_dfiles; 1262 newfdp->fd_ofileflags = 1263 ((struct filedesc0 *) newfdp)->fd_dfileflags; 1264 i = NDFILE; 1265 } else { 1266 /* 1267 * Compute the smallest multiple of NDEXTENT needed 1268 * for the file descriptors currently in use, 1269 * allowing the table to shrink. 1270 */ 1271retry: 1272 i = newfdp->fd_nfiles; 1273 while (i > 2 * NDEXTENT && i > newfdp->fd_lastfile * 2) 1274 i /= 2; 1275 FILEDESC_UNLOCK(fdp); 1276 MALLOC(newfdp->fd_ofiles, struct file **, i * OFILESIZE, 1277 M_FILEDESC, M_WAITOK); 1278 FILEDESC_LOCK(fdp); 1279 newfdp->fd_lastfile = fdp->fd_lastfile; 1280 newfdp->fd_nfiles = fdp->fd_nfiles; 1281 j = newfdp->fd_nfiles; 1282 while (j > 2 * NDEXTENT && j > newfdp->fd_lastfile * 2) 1283 j /= 2; 1284 if (i != j) { 1285 /* 1286 * The size of the original table has changed. 1287 * Go over once again. 1288 */ 1289 FILEDESC_UNLOCK(fdp); 1290 FREE(newfdp->fd_ofiles, M_FILEDESC); 1291 FILEDESC_LOCK(fdp); 1292 newfdp->fd_lastfile = fdp->fd_lastfile; 1293 newfdp->fd_nfiles = fdp->fd_nfiles; 1294 goto retry; 1295 } 1296 newfdp->fd_ofileflags = (char *) &newfdp->fd_ofiles[i]; 1297 } 1298 newfdp->fd_nfiles = i; 1299 bcopy(fdp->fd_ofiles, newfdp->fd_ofiles, i * sizeof(struct file **)); 1300 bcopy(fdp->fd_ofileflags, newfdp->fd_ofileflags, i * sizeof(char)); 1301 1302 /* 1303 * kq descriptors cannot be copied. 1304 */ 1305 if (newfdp->fd_knlistsize != -1) { 1306 fpp = &newfdp->fd_ofiles[newfdp->fd_lastfile]; 1307 for (i = newfdp->fd_lastfile; i >= 0; i--, fpp--) { 1308 if (*fpp != NULL && (*fpp)->f_type == DTYPE_KQUEUE) { 1309 *fpp = NULL; 1310 if (i < newfdp->fd_freefile) 1311 newfdp->fd_freefile = i; 1312 } 1313 if (*fpp == NULL && i == newfdp->fd_lastfile && i > 0) 1314 newfdp->fd_lastfile--; 1315 } 1316 newfdp->fd_knlist = NULL; 1317 newfdp->fd_knlistsize = -1; 1318 newfdp->fd_knhash = NULL; 1319 newfdp->fd_knhashmask = 0; 1320 } 1321 1322 fpp = newfdp->fd_ofiles; 1323 for (i = newfdp->fd_lastfile; i-- >= 0; fpp++) { 1324 if (*fpp != NULL) { 1325 fhold(*fpp); 1326 } 1327 } 1328 return (newfdp); 1329} 1330 1331/* 1332 * Release a filedesc structure. 1333 */ 1334void 1335fdfree(td) 1336 struct thread *td; 1337{ 1338 register struct filedesc *fdp; 1339 struct file **fpp; 1340 register int i; 1341 1342 fdp = td->td_proc->p_fd; 1343 /* Certain daemons might not have file descriptors. */ 1344 if (fdp == NULL) 1345 return; 1346 1347 FILEDESC_LOCK(fdp); 1348 if (--fdp->fd_refcnt > 0) { 1349 FILEDESC_UNLOCK(fdp); 1350 return; 1351 } 1352 /* 1353 * we are the last reference to the structure, we can 1354 * safely assume it will not change out from under us. 1355 */ 1356 FILEDESC_UNLOCK(fdp); 1357 fpp = fdp->fd_ofiles; 1358 for (i = fdp->fd_lastfile; i-- >= 0; fpp++) { 1359 if (*fpp) 1360 (void) closef(*fpp, td); 1361 } 1362 1363 PROC_LOCK(td->td_proc); 1364 td->td_proc->p_fd = NULL; 1365 PROC_UNLOCK(td->td_proc); 1366 1367 if (fdp->fd_nfiles > NDFILE) 1368 FREE(fdp->fd_ofiles, M_FILEDESC); 1369 if (fdp->fd_cdir) 1370 vrele(fdp->fd_cdir); 1371 if (fdp->fd_rdir) 1372 vrele(fdp->fd_rdir); 1373 if (fdp->fd_jdir) 1374 vrele(fdp->fd_jdir); 1375 if (fdp->fd_knlist) 1376 FREE(fdp->fd_knlist, M_KQUEUE); 1377 if (fdp->fd_knhash) 1378 FREE(fdp->fd_knhash, M_KQUEUE); 1379 mtx_destroy(&fdp->fd_mtx); 1380 FREE(fdp, M_FILEDESC); 1381} 1382 1383/* 1384 * For setugid programs, we don't want to people to use that setugidness 1385 * to generate error messages which write to a file which otherwise would 1386 * otherwise be off-limits to the process. 1387 * 1388 * This is a gross hack to plug the hole. A better solution would involve 1389 * a special vop or other form of generalized access control mechanism. We 1390 * go ahead and just reject all procfs file systems accesses as dangerous. 1391 * 1392 * Since setugidsafety calls this only for fd 0, 1 and 2, this check is 1393 * sufficient. We also don't for check setugidness since we know we are. 1394 */ 1395static int 1396is_unsafe(struct file *fp) 1397{ 1398 if (fp->f_type == DTYPE_VNODE && 1399 ((struct vnode *)(fp->f_data))->v_tag == VT_PROCFS) 1400 return (1); 1401 return (0); 1402} 1403 1404/* 1405 * Make this setguid thing safe, if at all possible. 1406 */ 1407void 1408setugidsafety(td) 1409 struct thread *td; 1410{ 1411 struct filedesc *fdp = td->td_proc->p_fd; 1412 register int i; 1413 1414 /* Certain daemons might not have file descriptors. */ 1415 if (fdp == NULL) 1416 return; 1417 1418 /* 1419 * note: fdp->fd_ofiles may be reallocated out from under us while 1420 * we are blocked in a close. Be careful! 1421 */ 1422 FILEDESC_LOCK(fdp); 1423 for (i = 0; i <= fdp->fd_lastfile; i++) { 1424 if (i > 2) 1425 break; 1426 if (fdp->fd_ofiles[i] && is_unsafe(fdp->fd_ofiles[i])) { 1427 struct file *fp; 1428 1429#if 0 1430 if ((fdp->fd_ofileflags[i] & UF_MAPPED) != 0) 1431 (void) munmapfd(td, i); 1432#endif 1433 if (i < fdp->fd_knlistsize) { 1434 FILEDESC_UNLOCK(fdp); 1435 knote_fdclose(td, i); 1436 FILEDESC_LOCK(fdp); 1437 } 1438 /* 1439 * NULL-out descriptor prior to close to avoid 1440 * a race while close blocks. 1441 */ 1442 fp = fdp->fd_ofiles[i]; 1443 fdp->fd_ofiles[i] = NULL; 1444 fdp->fd_ofileflags[i] = 0; 1445 if (i < fdp->fd_freefile) 1446 fdp->fd_freefile = i; 1447 FILEDESC_UNLOCK(fdp); 1448 (void) closef(fp, td); 1449 FILEDESC_LOCK(fdp); 1450 } 1451 } 1452 while (fdp->fd_lastfile > 0 && fdp->fd_ofiles[fdp->fd_lastfile] == NULL) 1453 fdp->fd_lastfile--; 1454 FILEDESC_UNLOCK(fdp); 1455} 1456 1457/* 1458 * Close any files on exec? 1459 */ 1460void 1461fdcloseexec(td) 1462 struct thread *td; 1463{ 1464 struct filedesc *fdp = td->td_proc->p_fd; 1465 register int i; 1466 1467 /* Certain daemons might not have file descriptors. */ 1468 if (fdp == NULL) 1469 return; 1470 1471 FILEDESC_LOCK(fdp); 1472 1473 /* 1474 * We cannot cache fd_ofiles or fd_ofileflags since operations 1475 * may block and rip them out from under us. 1476 */ 1477 for (i = 0; i <= fdp->fd_lastfile; i++) { 1478 if (fdp->fd_ofiles[i] != NULL && 1479 (fdp->fd_ofileflags[i] & UF_EXCLOSE)) { 1480 struct file *fp; 1481 1482#if 0 1483 if (fdp->fd_ofileflags[i] & UF_MAPPED) 1484 (void) munmapfd(td, i); 1485#endif 1486 if (i < fdp->fd_knlistsize) { 1487 FILEDESC_UNLOCK(fdp); 1488 knote_fdclose(td, i); 1489 FILEDESC_LOCK(fdp); 1490 } 1491 /* 1492 * NULL-out descriptor prior to close to avoid 1493 * a race while close blocks. 1494 */ 1495 fp = fdp->fd_ofiles[i]; 1496 fdp->fd_ofiles[i] = NULL; 1497 fdp->fd_ofileflags[i] = 0; 1498 if (i < fdp->fd_freefile) 1499 fdp->fd_freefile = i; 1500 FILEDESC_UNLOCK(fdp); 1501 (void) closef(fp, td); 1502 FILEDESC_LOCK(fdp); 1503 } 1504 } 1505 while (fdp->fd_lastfile > 0 && fdp->fd_ofiles[fdp->fd_lastfile] == NULL) 1506 fdp->fd_lastfile--; 1507 FILEDESC_UNLOCK(fdp); 1508} 1509 1510/* 1511 * It is unsafe for set[ug]id processes to be started with file 1512 * descriptors 0..2 closed, as these descriptors are given implicit 1513 * significance in the Standard C library. fdcheckstd() will create a 1514 * descriptor referencing /dev/null for each of stdin, stdout, and 1515 * stderr that is not already open. 1516 */ 1517int 1518fdcheckstd(td) 1519 struct thread *td; 1520{ 1521 struct nameidata nd; 1522 struct filedesc *fdp; 1523 struct file *fp; 1524 register_t retval; 1525 int fd, i, error, flags, devnull; 1526 1527 fdp = td->td_proc->p_fd; 1528 if (fdp == NULL) 1529 return (0); 1530 devnull = -1; 1531 error = 0; 1532 for (i = 0; i < 3; i++) { 1533 if (fdp->fd_ofiles[i] != NULL) 1534 continue; 1535 if (devnull < 0) { 1536 error = falloc(td, &fp, &fd); 1537 if (error != 0) 1538 break; 1539 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, "/dev/null", 1540 td); 1541 flags = FREAD | FWRITE; 1542 error = vn_open(&nd, &flags, 0); 1543 if (error != 0) { 1544 FILEDESC_LOCK(fdp); 1545 fdp->fd_ofiles[i] = NULL; 1546 FILEDESC_UNLOCK(fdp); 1547 fdrop(fp, td); 1548 break; 1549 } 1550 NDFREE(&nd, NDF_ONLY_PNBUF); 1551 fp->f_data = (caddr_t)nd.ni_vp; 1552 fp->f_flag = flags; 1553 fp->f_ops = &vnops; 1554 fp->f_type = DTYPE_VNODE; 1555 VOP_UNLOCK(nd.ni_vp, 0, td); 1556 devnull = fd; 1557 } else { 1558 FILEDESC_LOCK(fdp); 1559 error = fdalloc(td, 0, &fd); 1560 if (error != 0) { 1561 FILEDESC_UNLOCK(fdp); 1562 break; 1563 } 1564 error = do_dup(fdp, devnull, fd, &retval, td); 1565 if (error != 0) 1566 break; 1567 } 1568 } 1569 return (error); 1570} 1571 1572/* 1573 * Internal form of close. 1574 * Decrement reference count on file structure. 1575 * Note: td may be NULL when closing a file 1576 * that was being passed in a message. 1577 */ 1578int 1579closef(fp, td) 1580 register struct file *fp; 1581 register struct thread *td; 1582{ 1583 struct vnode *vp; 1584 struct flock lf; 1585 1586 if (fp == NULL) 1587 return (0); 1588 /* 1589 * POSIX record locking dictates that any close releases ALL 1590 * locks owned by this process. This is handled by setting 1591 * a flag in the unlock to free ONLY locks obeying POSIX 1592 * semantics, and not to free BSD-style file locks. 1593 * If the descriptor was in a message, POSIX-style locks 1594 * aren't passed with the descriptor. 1595 */ 1596 if (td && (td->td_proc->p_flag & P_ADVLOCK) && 1597 fp->f_type == DTYPE_VNODE) { 1598 lf.l_whence = SEEK_SET; 1599 lf.l_start = 0; 1600 lf.l_len = 0; 1601 lf.l_type = F_UNLCK; 1602 vp = (struct vnode *)fp->f_data; 1603 (void) VOP_ADVLOCK(vp, (caddr_t)td->td_proc->p_leader, 1604 F_UNLCK, &lf, F_POSIX); 1605 } 1606 return (fdrop(fp, td)); 1607} 1608 1609/* 1610 * Drop reference on struct file passed in, may call closef if the 1611 * reference hits zero. 1612 */ 1613int 1614fdrop(fp, td) 1615 struct file *fp; 1616 struct thread *td; 1617{ 1618 1619 FILE_LOCK(fp); 1620 return (fdrop_locked(fp, td)); 1621} 1622 1623/* 1624 * Extract the file pointer associated with the specified descriptor for 1625 * the current user process. 1626 * 1627 * If the descriptor doesn't exist, EBADF is returned. 1628 * 1629 * If the descriptor exists but doesn't match 'flags' then 1630 * return EBADF for read attempts and EINVAL for write attempts. 1631 * 1632 * If 'hold' is set (non-zero) the file's refcount will be bumped on return. 1633 * It should be droped with fdrop(). 1634 * If it is not set, then the refcount will not be bumped however the 1635 * thread's filedesc struct will be returned locked (for fgetsock). 1636 * 1637 * If an error occured the non-zero error is returned and *fpp is set to NULL. 1638 * Otherwise *fpp is set and zero is returned. 1639 */ 1640static __inline 1641int 1642_fget(struct thread *td, int fd, struct file **fpp, int flags, int hold) 1643{ 1644 struct filedesc *fdp; 1645 struct file *fp; 1646 1647 *fpp = NULL; 1648 if (td == NULL || (fdp = td->td_proc->p_fd) == NULL) 1649 return(EBADF); 1650 FILEDESC_LOCK(fdp); 1651 if ((fp = fget_locked(fdp, fd)) == NULL || fp->f_ops == &badfileops) { 1652 FILEDESC_UNLOCK(fdp); 1653 return(EBADF); 1654 } 1655 1656 /* 1657 * Note: FREAD failures returns EBADF to maintain backwards 1658 * compatibility with what routines returned before. 1659 * 1660 * Only one flag, or 0, may be specified. 1661 */ 1662 if (flags == FREAD && (fp->f_flag & FREAD) == 0) { 1663 FILEDESC_UNLOCK(fdp); 1664 return(EBADF); 1665 } 1666 if (flags == FWRITE && (fp->f_flag & FWRITE) == 0) { 1667 FILEDESC_UNLOCK(fdp); 1668 return(EINVAL); 1669 } 1670 if (hold) { 1671 fhold(fp); 1672 FILEDESC_UNLOCK(fdp); 1673 } 1674 *fpp = fp; 1675 return(0); 1676} 1677 1678int 1679fget(struct thread *td, int fd, struct file **fpp) 1680{ 1681 return(_fget(td, fd, fpp, 0, 1)); 1682} 1683 1684int 1685fget_read(struct thread *td, int fd, struct file **fpp) 1686{ 1687 return(_fget(td, fd, fpp, FREAD, 1)); 1688} 1689 1690int 1691fget_write(struct thread *td, int fd, struct file **fpp) 1692{ 1693 return(_fget(td, fd, fpp, FWRITE, 1)); 1694} 1695 1696/* 1697 * Like fget() but loads the underlying vnode, or returns an error if 1698 * the descriptor does not represent a vnode. Note that pipes use vnodes 1699 * but never have VM objects (so VOP_GETVOBJECT() calls will return an 1700 * error). The returned vnode will be vref()d. 1701 */ 1702 1703static __inline 1704int 1705_fgetvp(struct thread *td, int fd, struct vnode **vpp, int flags) 1706{ 1707 struct file *fp; 1708 int error; 1709 1710 *vpp = NULL; 1711 if ((error = _fget(td, fd, &fp, 0, 0)) != 0) 1712 return (error); 1713 if (fp->f_type != DTYPE_VNODE && fp->f_type != DTYPE_FIFO) { 1714 error = EINVAL; 1715 } else { 1716 *vpp = (struct vnode *)fp->f_data; 1717 vref(*vpp); 1718 } 1719 FILEDESC_UNLOCK(td->td_proc->p_fd); 1720 return (error); 1721} 1722 1723int 1724fgetvp(struct thread *td, int fd, struct vnode **vpp) 1725{ 1726 return(_fgetvp(td, fd, vpp, 0)); 1727} 1728 1729int 1730fgetvp_read(struct thread *td, int fd, struct vnode **vpp) 1731{ 1732 return(_fgetvp(td, fd, vpp, FREAD)); 1733} 1734 1735int 1736fgetvp_write(struct thread *td, int fd, struct vnode **vpp) 1737{ 1738 return(_fgetvp(td, fd, vpp, FWRITE)); 1739} 1740 1741/* 1742 * Like fget() but loads the underlying socket, or returns an error if 1743 * the descriptor does not represent a socket. 1744 * 1745 * We bump the ref count on the returned socket. XXX Also obtain the SX lock in 1746 * the future. 1747 */ 1748int 1749fgetsock(struct thread *td, int fd, struct socket **spp, u_int *fflagp) 1750{ 1751 struct file *fp; 1752 int error; 1753 1754 *spp = NULL; 1755 if (fflagp) 1756 *fflagp = 0; 1757 if ((error = _fget(td, fd, &fp, 0, 0)) != 0) 1758 return (error); 1759 if (fp->f_type != DTYPE_SOCKET) { 1760 error = ENOTSOCK; 1761 } else { 1762 *spp = (struct socket *)fp->f_data; 1763 if (fflagp) 1764 *fflagp = fp->f_flag; 1765 soref(*spp); 1766 } 1767 FILEDESC_UNLOCK(td->td_proc->p_fd); 1768 return(error); 1769} 1770 1771/* 1772 * Drop the reference count on the the socket and XXX release the SX lock in 1773 * the future. The last reference closes the socket. 1774 */ 1775void 1776fputsock(struct socket *so) 1777{ 1778 sorele(so); 1779} 1780 1781/* 1782 * Drop reference on struct file passed in, may call closef if the 1783 * reference hits zero. 1784 * Expects struct file locked, and will unlock it. 1785 */ 1786int 1787fdrop_locked(fp, td) 1788 struct file *fp; 1789 struct thread *td; 1790{ 1791 struct flock lf; 1792 struct vnode *vp; 1793 int error; 1794 1795 FILE_LOCK_ASSERT(fp, MA_OWNED); 1796 1797 if (--fp->f_count > 0) { 1798 FILE_UNLOCK(fp); 1799 return (0); 1800 } 1801 mtx_lock(&Giant); 1802 if (fp->f_count < 0) 1803 panic("fdrop: count < 0"); 1804 if ((fp->f_flag & FHASLOCK) && fp->f_type == DTYPE_VNODE) { 1805 lf.l_whence = SEEK_SET; 1806 lf.l_start = 0; 1807 lf.l_len = 0; 1808 lf.l_type = F_UNLCK; 1809 vp = (struct vnode *)fp->f_data; 1810 FILE_UNLOCK(fp); 1811 (void) VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK); 1812 } else 1813 FILE_UNLOCK(fp); 1814 if (fp->f_ops != &badfileops) 1815 error = fo_close(fp, td); 1816 else 1817 error = 0; 1818 ffree(fp); 1819 mtx_unlock(&Giant); 1820 return (error); 1821} 1822 1823/* 1824 * Apply an advisory lock on a file descriptor. 1825 * 1826 * Just attempt to get a record lock of the requested type on 1827 * the entire file (l_whence = SEEK_SET, l_start = 0, l_len = 0). 1828 */ 1829#ifndef _SYS_SYSPROTO_H_ 1830struct flock_args { 1831 int fd; 1832 int how; 1833}; 1834#endif 1835/* 1836 * MPSAFE 1837 */ 1838/* ARGSUSED */ 1839int 1840flock(td, uap) 1841 struct thread *td; 1842 register struct flock_args *uap; 1843{ 1844 struct file *fp; 1845 struct vnode *vp; 1846 struct flock lf; 1847 int error; 1848 1849 if ((error = fget(td, uap->fd, &fp)) != 0) 1850 return (error); 1851 if (fp->f_type != DTYPE_VNODE) { 1852 fdrop(fp, td); 1853 return (EOPNOTSUPP); 1854 } 1855 1856 mtx_lock(&Giant); 1857 vp = (struct vnode *)fp->f_data; 1858 lf.l_whence = SEEK_SET; 1859 lf.l_start = 0; 1860 lf.l_len = 0; 1861 if (uap->how & LOCK_UN) { 1862 lf.l_type = F_UNLCK; 1863 FILE_LOCK(fp); 1864 fp->f_flag &= ~FHASLOCK; 1865 FILE_UNLOCK(fp); 1866 error = VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK); 1867 goto done2; 1868 } 1869 if (uap->how & LOCK_EX) 1870 lf.l_type = F_WRLCK; 1871 else if (uap->how & LOCK_SH) 1872 lf.l_type = F_RDLCK; 1873 else { 1874 error = EBADF; 1875 goto done2; 1876 } 1877 FILE_LOCK(fp); 1878 fp->f_flag |= FHASLOCK; 1879 FILE_UNLOCK(fp); 1880 error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, 1881 (uap->how & LOCK_NB) ? F_FLOCK : F_FLOCK | F_WAIT); 1882done2: 1883 fdrop(fp, td); 1884 mtx_unlock(&Giant); 1885 return (error); 1886} 1887 1888/* 1889 * File Descriptor pseudo-device driver (/dev/fd/). 1890 * 1891 * Opening minor device N dup()s the file (if any) connected to file 1892 * descriptor N belonging to the calling process. Note that this driver 1893 * consists of only the ``open()'' routine, because all subsequent 1894 * references to this file will be direct to the other driver. 1895 */ 1896/* ARGSUSED */ 1897static int 1898fdopen(dev, mode, type, td) 1899 dev_t dev; 1900 int mode, type; 1901 struct thread *td; 1902{ 1903 1904 /* 1905 * XXX Kludge: set curthread->td_dupfd to contain the value of the 1906 * the file descriptor being sought for duplication. The error 1907 * return ensures that the vnode for this device will be released 1908 * by vn_open. Open will detect this special error and take the 1909 * actions in dupfdopen below. Other callers of vn_open or VOP_OPEN 1910 * will simply report the error. 1911 */ 1912 td->td_dupfd = dev2unit(dev); 1913 return (ENODEV); 1914} 1915 1916/* 1917 * Duplicate the specified descriptor to a free descriptor. 1918 */ 1919int 1920dupfdopen(td, fdp, indx, dfd, mode, error) 1921 struct thread *td; 1922 struct filedesc *fdp; 1923 int indx, dfd; 1924 int mode; 1925 int error; 1926{ 1927 register struct file *wfp; 1928 struct file *fp; 1929 1930 /* 1931 * If the to-be-dup'd fd number is greater than the allowed number 1932 * of file descriptors, or the fd to be dup'd has already been 1933 * closed, then reject. 1934 */ 1935 FILEDESC_LOCK(fdp); 1936 if ((u_int)dfd >= fdp->fd_nfiles || 1937 (wfp = fdp->fd_ofiles[dfd]) == NULL) { 1938 FILEDESC_UNLOCK(fdp); 1939 return (EBADF); 1940 } 1941 1942 /* 1943 * There are two cases of interest here. 1944 * 1945 * For ENODEV simply dup (dfd) to file descriptor 1946 * (indx) and return. 1947 * 1948 * For ENXIO steal away the file structure from (dfd) and 1949 * store it in (indx). (dfd) is effectively closed by 1950 * this operation. 1951 * 1952 * Any other error code is just returned. 1953 */ 1954 switch (error) { 1955 case ENODEV: 1956 /* 1957 * Check that the mode the file is being opened for is a 1958 * subset of the mode of the existing descriptor. 1959 */ 1960 FILE_LOCK(wfp); 1961 if (((mode & (FREAD|FWRITE)) | wfp->f_flag) != wfp->f_flag) { 1962 FILE_UNLOCK(wfp); 1963 FILEDESC_UNLOCK(fdp); 1964 return (EACCES); 1965 } 1966 fp = fdp->fd_ofiles[indx]; 1967#if 0 1968 if (fp && fdp->fd_ofileflags[indx] & UF_MAPPED) 1969 (void) munmapfd(td, indx); 1970#endif 1971 fdp->fd_ofiles[indx] = wfp; 1972 fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd]; 1973 fhold_locked(wfp); 1974 FILE_UNLOCK(wfp); 1975 if (indx > fdp->fd_lastfile) 1976 fdp->fd_lastfile = indx; 1977 if (fp != NULL) 1978 FILE_LOCK(fp); 1979 FILEDESC_UNLOCK(fdp); 1980 /* 1981 * we now own the reference to fp that the ofiles[] array 1982 * used to own. Release it. 1983 */ 1984 if (fp != NULL) 1985 fdrop_locked(fp, td); 1986 return (0); 1987 1988 case ENXIO: 1989 /* 1990 * Steal away the file pointer from dfd, and stuff it into indx. 1991 */ 1992 fp = fdp->fd_ofiles[indx]; 1993#if 0 1994 if (fp && fdp->fd_ofileflags[indx] & UF_MAPPED) 1995 (void) munmapfd(td, indx); 1996#endif 1997 fdp->fd_ofiles[indx] = fdp->fd_ofiles[dfd]; 1998 fdp->fd_ofiles[dfd] = NULL; 1999 fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd]; 2000 fdp->fd_ofileflags[dfd] = 0; 2001 2002 /* 2003 * Complete the clean up of the filedesc structure by 2004 * recomputing the various hints. 2005 */ 2006 if (indx > fdp->fd_lastfile) { 2007 fdp->fd_lastfile = indx; 2008 } else { 2009 while (fdp->fd_lastfile > 0 && 2010 fdp->fd_ofiles[fdp->fd_lastfile] == NULL) { 2011 fdp->fd_lastfile--; 2012 } 2013 if (dfd < fdp->fd_freefile) 2014 fdp->fd_freefile = dfd; 2015 } 2016 if (fp != NULL) 2017 FILE_LOCK(fp); 2018 FILEDESC_UNLOCK(fdp); 2019 2020 /* 2021 * we now own the reference to fp that the ofiles[] array 2022 * used to own. Release it. 2023 */ 2024 if (fp != NULL) 2025 fdrop_locked(fp, td); 2026 return (0); 2027 2028 default: 2029 FILEDESC_UNLOCK(fdp); 2030 return (error); 2031 } 2032 /* NOTREACHED */ 2033} 2034 2035/* 2036 * Get file structures. 2037 */ 2038static int 2039sysctl_kern_file(SYSCTL_HANDLER_ARGS) 2040{ 2041 int error; 2042 struct file *fp; 2043 2044 sx_slock(&filelist_lock); 2045 if (!req->oldptr) { 2046 /* 2047 * overestimate by 10 files 2048 */ 2049 error = SYSCTL_OUT(req, 0, sizeof(filehead) + 2050 (nfiles + 10) * sizeof(struct file)); 2051 sx_sunlock(&filelist_lock); 2052 return (error); 2053 } 2054 2055 error = SYSCTL_OUT(req, (caddr_t)&filehead, sizeof(filehead)); 2056 if (error) { 2057 sx_sunlock(&filelist_lock); 2058 return (error); 2059 } 2060 2061 /* 2062 * followed by an array of file structures 2063 */ 2064 LIST_FOREACH(fp, &filehead, f_list) { 2065 error = SYSCTL_OUT(req, (caddr_t)fp, sizeof (struct file)); 2066 if (error) { 2067 sx_sunlock(&filelist_lock); 2068 return (error); 2069 } 2070 } 2071 sx_sunlock(&filelist_lock); 2072 return (0); 2073} 2074 2075SYSCTL_PROC(_kern, KERN_FILE, file, CTLTYPE_OPAQUE|CTLFLAG_RD, 2076 0, 0, sysctl_kern_file, "S,file", "Entire file table"); 2077 2078SYSCTL_INT(_kern, KERN_MAXFILESPERPROC, maxfilesperproc, CTLFLAG_RW, 2079 &maxfilesperproc, 0, "Maximum files allowed open per process"); 2080 2081SYSCTL_INT(_kern, KERN_MAXFILES, maxfiles, CTLFLAG_RW, 2082 &maxfiles, 0, "Maximum number of files"); 2083 2084SYSCTL_INT(_kern, OID_AUTO, openfiles, CTLFLAG_RD, 2085 &nfiles, 0, "System-wide number of open files"); 2086 2087static void 2088fildesc_drvinit(void *unused) 2089{ 2090 dev_t dev; 2091 2092 dev = make_dev(&fildesc_cdevsw, 0, UID_BIN, GID_BIN, 0666, "fd/0"); 2093 make_dev_alias(dev, "stdin"); 2094 dev = make_dev(&fildesc_cdevsw, 1, UID_BIN, GID_BIN, 0666, "fd/1"); 2095 make_dev_alias(dev, "stdout"); 2096 dev = make_dev(&fildesc_cdevsw, 2, UID_BIN, GID_BIN, 0666, "fd/2"); 2097 make_dev_alias(dev, "stderr"); 2098 if (!devfs_present) { 2099 int fd; 2100 2101 for (fd = 3; fd < NUMFDESC; fd++) 2102 make_dev(&fildesc_cdevsw, fd, UID_BIN, GID_BIN, 0666, 2103 "fd/%d", fd); 2104 } 2105} 2106 2107struct fileops badfileops = { 2108 badfo_readwrite, 2109 badfo_readwrite, 2110 badfo_ioctl, 2111 badfo_poll, 2112 badfo_kqfilter, 2113 badfo_stat, 2114 badfo_close 2115}; 2116 2117static int 2118badfo_readwrite(fp, uio, cred, flags, td) 2119 struct file *fp; 2120 struct uio *uio; 2121 struct ucred *cred; 2122 struct thread *td; 2123 int flags; 2124{ 2125 2126 return (EBADF); 2127} 2128 2129static int 2130badfo_ioctl(fp, com, data, td) 2131 struct file *fp; 2132 u_long com; 2133 caddr_t data; 2134 struct thread *td; 2135{ 2136 2137 return (EBADF); 2138} 2139 2140static int 2141badfo_poll(fp, events, cred, td) 2142 struct file *fp; 2143 int events; 2144 struct ucred *cred; 2145 struct thread *td; 2146{ 2147 2148 return (0); 2149} 2150 2151static int 2152badfo_kqfilter(fp, kn) 2153 struct file *fp; 2154 struct knote *kn; 2155{ 2156 2157 return (0); 2158} 2159 2160static int 2161badfo_stat(fp, sb, td) 2162 struct file *fp; 2163 struct stat *sb; 2164 struct thread *td; 2165{ 2166 2167 return (EBADF); 2168} 2169 2170static int 2171badfo_close(fp, td) 2172 struct file *fp; 2173 struct thread *td; 2174{ 2175 2176 return (EBADF); 2177} 2178 2179SYSINIT(fildescdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR, 2180 fildesc_drvinit,NULL) 2181 2182static void filelistinit(void *); 2183SYSINIT(select, SI_SUB_LOCK, SI_ORDER_FIRST, filelistinit, NULL) 2184 2185/* ARGSUSED*/ 2186static void 2187filelistinit(dummy) 2188 void *dummy; 2189{ 2190 file_zone = uma_zcreate("Files", sizeof(struct file), NULL, NULL, 2191 NULL, NULL, UMA_ALIGN_PTR, 0); 2192 2193 sx_init(&filelist_lock, "filelist lock"); 2194 mtx_init(&sigio_lock, "sigio lock", NULL, MTX_DEF); 2195} 2196