From ea31808c3b07eb7bdea0f8f552508ddf620f043f Mon Sep 17 00:00:00 2001 From: Mateusz Guzik Date: Sun, 14 Jun 2015 14:08:52 +0000 Subject: [PATCH] fd: move out actual fp installation to _finstall Use it in fd passing functions as the first step towards fd code cleanup. --- sys/kern/kern_descrip.c | 36 +++++++++++++++++++++++------------- sys/kern/uipc_usrreq.c | 13 +++++-------- sys/sys/filedesc.h | 4 +++- 3 files changed, 31 insertions(+), 22 deletions(-) diff --git a/sys/kern/kern_descrip.c b/sys/kern/kern_descrip.c index f6f819464f30..0ba507fe36fb 100644 --- a/sys/kern/kern_descrip.c +++ b/sys/kern/kern_descrip.c @@ -1750,26 +1750,18 @@ falloc_noinstall(struct thread *td, struct file **resultfp) /* * Install a file in a file descriptor table. */ -int -finstall(struct thread *td, struct file *fp, int *fd, int flags, +void +_finstall(struct filedesc *fdp, struct file *fp, int fd, int flags, struct filecaps *fcaps) { - struct filedesc *fdp = td->td_proc->p_fd; struct filedescent *fde; - int error; - KASSERT(fd != NULL, ("%s: fd == NULL", __func__)); - KASSERT(fp != NULL, ("%s: fp == NULL", __func__)); + MPASS(fp != NULL); if (fcaps != NULL) filecaps_validate(fcaps, __func__); + FILEDESC_XLOCK_ASSERT(fdp); - FILEDESC_XLOCK(fdp); - if ((error = fdalloc(td, 0, fd))) { - FILEDESC_XUNLOCK(fdp); - return (error); - } - fhold(fp); - fde = &fdp->fd_ofiles[*fd]; + fde = &fdp->fd_ofiles[fd]; #ifdef CAPABILITIES seq_write_begin(&fde->fde_seq); #endif @@ -1783,6 +1775,24 @@ finstall(struct thread *td, struct file *fp, int *fd, int flags, #ifdef CAPABILITIES seq_write_end(&fde->fde_seq); #endif +} + +int +finstall(struct thread *td, struct file *fp, int *fd, int flags, + struct filecaps *fcaps) +{ + struct filedesc *fdp = td->td_proc->p_fd; + int error; + + MPASS(fd != NULL); + + FILEDESC_XLOCK(fdp); + if ((error = fdalloc(td, 0, fd))) { + FILEDESC_XUNLOCK(fdp); + return (error); + } + fhold(fp); + _finstall(fdp, fp, *fd, flags, fcaps); FILEDESC_XUNLOCK(fdp); return (0); } diff --git a/sys/kern/uipc_usrreq.c b/sys/kern/uipc_usrreq.c index 808020e5e61d..acf9fe9bd1e0 100644 --- a/sys/kern/uipc_usrreq.c +++ b/sys/kern/uipc_usrreq.c @@ -1736,7 +1736,7 @@ unp_externalize(struct mbuf *control, struct mbuf **controlp, int flags) int i; int *fdp; struct filedesc *fdesc = td->td_proc->p_fd; - struct filedescent *fde, **fdep; + struct filedescent **fdep; void *data; socklen_t clen = control->m_len, datalen; int error, newfds; @@ -1795,13 +1795,10 @@ unp_externalize(struct mbuf *control, struct mbuf **controlp, int flags) goto next; } for (i = 0; i < newfds; i++, fdp++) { - fde = &fdesc->fd_ofiles[*fdp]; - fde->fde_file = fdep[i]->fde_file; - filecaps_move(&fdep[i]->fde_caps, - &fde->fde_caps); - if ((flags & MSG_CMSG_CLOEXEC) != 0) - fde->fde_flags |= UF_EXCLOSE; - unp_externalize_fp(fde->fde_file); + _finstall(fdesc, fdep[i]->fde_file, *fdp, + (flags & MSG_CMSG_CLOEXEC) != 0 ? UF_EXCLOSE : 0, + &fdep[i]->fde_caps); + unp_externalize_fp(fdep[i]->fde_file); } FILEDESC_XUNLOCK(fdesc); free(fdep[0], M_FILECAPS); diff --git a/sys/sys/filedesc.h b/sys/sys/filedesc.h index 3fe61c883cd7..da80ed95672b 100644 --- a/sys/sys/filedesc.h +++ b/sys/sys/filedesc.h @@ -147,7 +147,9 @@ int dupfdopen(struct thread *td, struct filedesc *fdp, int dfd, int mode, int falloc(struct thread *td, struct file **resultfp, int *resultfd, int flags); int falloc_noinstall(struct thread *td, struct file **resultfp); -int finstall(struct thread *td, struct file *fp, int *resultfp, int flags, +void _finstall(struct filedesc *fdp, struct file *fp, int fd, int flags, + struct filecaps *fcaps); +int finstall(struct thread *td, struct file *fp, int *resultfd, int flags, struct filecaps *fcaps); int fdalloc(struct thread *td, int minfd, int *result); int fdallocn(struct thread *td, int minfd, int *fds, int n);