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