kern_descrip.c (221808) | kern_descrip.c (223694) |
---|---|
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. --- 21 unchanged lines hidden (view full) --- 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * @(#)kern_descrip.c 8.6 (Berkeley) 4/19/94 35 */ 36 37#include <sys/cdefs.h> | 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. --- 21 unchanged lines hidden (view full) --- 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * @(#)kern_descrip.c 8.6 (Berkeley) 4/19/94 35 */ 36 37#include <sys/cdefs.h> |
38__FBSDID("$FreeBSD: head/sys/kern/kern_descrip.c 221808 2011-05-12 10:56:33Z stas $"); | 38__FBSDID("$FreeBSD: head/sys/kern/kern_descrip.c 223694 2011-06-30 15:22:49Z jonathan $"); |
39 40#include "opt_compat.h" 41#include "opt_ddb.h" 42#include "opt_ktrace.h" 43 44#include <sys/param.h> 45#include <sys/systm.h> 46 --- 1509 unchanged lines hidden (view full) --- 1556 * process that refers to it. We add one reference to the file for the 1557 * descriptor table and one reference for resultfp. This is to prevent us 1558 * being preempted and the entry in the descriptor table closed after we 1559 * release the FILEDESC lock. 1560 */ 1561int 1562falloc(struct thread *td, struct file **resultfp, int *resultfd, int flags) 1563{ | 39 40#include "opt_compat.h" 41#include "opt_ddb.h" 42#include "opt_ktrace.h" 43 44#include <sys/param.h> 45#include <sys/systm.h> 46 --- 1509 unchanged lines hidden (view full) --- 1556 * process that refers to it. We add one reference to the file for the 1557 * descriptor table and one reference for resultfp. This is to prevent us 1558 * being preempted and the entry in the descriptor table closed after we 1559 * release the FILEDESC lock. 1560 */ 1561int 1562falloc(struct thread *td, struct file **resultfp, int *resultfd, int flags) 1563{ |
1564 struct proc *p = td->td_proc; | |
1565 struct file *fp; | 1564 struct file *fp; |
1566 int error, i; | 1565 int error, fd; 1566 1567 error = falloc_noinstall(td, &fp); 1568 if (error) 1569 return (error); /* no reference held on error */ 1570 1571 error = finstall(td, fp, &fd, flags); 1572 if (error) { 1573 fdrop(fp, td); /* one reference (fp only) */ 1574 return (error); 1575 } 1576 1577 if (resultfp != NULL) 1578 *resultfp = fp; /* copy out result */ 1579 else 1580 fdrop(fp, td); /* release local reference */ 1581 1582 if (resultfd != NULL) 1583 *resultfd = fd; 1584 1585 return (0); 1586} 1587 1588/* 1589 * Create a new open file structure without allocating a file descriptor. 1590 */ 1591int 1592falloc_noinstall(struct thread *td, struct file **resultfp) 1593{ 1594 struct file *fp; |
1567 int maxuserfiles = maxfiles - (maxfiles / 20); 1568 static struct timeval lastfail; 1569 static int curfail; 1570 | 1595 int maxuserfiles = maxfiles - (maxfiles / 20); 1596 static struct timeval lastfail; 1597 static int curfail; 1598 |
1571 fp = uma_zalloc(file_zone, M_WAITOK | M_ZERO); | 1599 KASSERT(resultfp != NULL, ("%s: resultfp == NULL", __func__)); 1600 |
1572 if ((openfiles >= maxuserfiles && 1573 priv_check(td, PRIV_MAXFILES) != 0) || 1574 openfiles >= maxfiles) { 1575 if (ppsratecheck(&lastfail, &curfail, 1)) { | 1601 if ((openfiles >= maxuserfiles && 1602 priv_check(td, PRIV_MAXFILES) != 0) || 1603 openfiles >= maxfiles) { 1604 if (ppsratecheck(&lastfail, &curfail, 1)) { |
1576 printf("kern.maxfiles limit exceeded by uid %i, please see tuning(7).\n", 1577 td->td_ucred->cr_ruid); | 1605 printf("kern.maxfiles limit exceeded by uid %i, " 1606 "please see tuning(7).\n", td->td_ucred->cr_ruid); |
1578 } | 1607 } |
1579 uma_zfree(file_zone, fp); | |
1580 return (ENFILE); 1581 } 1582 atomic_add_int(&openfiles, 1); | 1608 return (ENFILE); 1609 } 1610 atomic_add_int(&openfiles, 1); |
1583 1584 /* 1585 * If the process has file descriptor zero open, add the new file 1586 * descriptor to the list of open files at that point, otherwise 1587 * put it at the front of the list of open files. 1588 */ | 1611 fp = uma_zalloc(file_zone, M_WAITOK | M_ZERO); |
1589 refcount_init(&fp->f_count, 1); | 1612 refcount_init(&fp->f_count, 1); |
1590 if (resultfp) 1591 fhold(fp); | |
1592 fp->f_cred = crhold(td->td_ucred); 1593 fp->f_ops = &badfileops; 1594 fp->f_data = NULL; 1595 fp->f_vnode = NULL; | 1613 fp->f_cred = crhold(td->td_ucred); 1614 fp->f_ops = &badfileops; 1615 fp->f_data = NULL; 1616 fp->f_vnode = NULL; |
1596 FILEDESC_XLOCK(p->p_fd); 1597 if ((error = fdalloc(td, 0, &i))) { 1598 FILEDESC_XUNLOCK(p->p_fd); 1599 fdrop(fp, td); 1600 if (resultfp) 1601 fdrop(fp, td); | 1617 *resultfp = fp; 1618 return (0); 1619} 1620 1621/* 1622 * Install a file in a file descriptor table. 1623 */ 1624int 1625finstall(struct thread *td, struct file *fp, int *fd, int flags) 1626{ 1627 struct filedesc *fdp = td->td_proc->p_fd; 1628 int error; 1629 1630 KASSERT(fd != NULL, ("%s: fd == NULL", __func__)); 1631 KASSERT(fp != NULL, ("%s: fp == NULL", __func__)); 1632 1633 FILEDESC_XLOCK(fdp); 1634 if ((error = fdalloc(td, 0, fd))) { 1635 FILEDESC_XUNLOCK(fdp); |
1602 return (error); 1603 } | 1636 return (error); 1637 } |
1604 p->p_fd->fd_ofiles[i] = fp; | 1638 fhold(fp); 1639 fdp->fd_ofiles[*fd] = fp; |
1605 if ((flags & O_CLOEXEC) != 0) | 1640 if ((flags & O_CLOEXEC) != 0) |
1606 p->p_fd->fd_ofileflags[i] |= UF_EXCLOSE; 1607 FILEDESC_XUNLOCK(p->p_fd); 1608 if (resultfp) 1609 *resultfp = fp; 1610 if (resultfd) 1611 *resultfd = i; | 1641 fdp->fd_ofileflags[*fd] |= UF_EXCLOSE; 1642 FILEDESC_XUNLOCK(fdp); |
1612 return (0); 1613} 1614 1615/* 1616 * Build a new filedesc structure from another. 1617 * Copy the current, root, and jail root vnode references. 1618 */ 1619struct filedesc * --- 2018 unchanged lines hidden --- | 1643 return (0); 1644} 1645 1646/* 1647 * Build a new filedesc structure from another. 1648 * Copy the current, root, and jail root vnode references. 1649 */ 1650struct filedesc * --- 2018 unchanged lines hidden --- |