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