From ea509db2c4d1e7d80d2ce250e74d61b200f09605 Mon Sep 17 00:00:00 2001
From: Kanonenvogel <kanonenvogel.87g@gmail.com>
Date: Tue, 14 Apr 2015 04:45:33 +0400
Subject: [PATCH 3/4] dupfdopen() api modification

---
 kern/kern_descrip.c | 13 +++++++------
 kern/vfs_syscalls.c |  3 +--
 sys/filedesc.h      |  2 +-
 3 files changed, 9 insertions(+), 9 deletions(-)

diff --git a/kern/kern_descrip.c b/kern/kern_descrip.c
index 0990561..c820838 100644
--- a/kern/kern_descrip.c
+++ b/kern/kern_descrip.c
@@ -1211,8 +1211,9 @@ filedescopen(dev_t dev, int mode, int type, struct proc *p)
  * Duplicate the specified descriptor to a free descriptor.
  */
 int
-dupfdopen(struct filedesc *fdp, int indx, int dfd, int mode)
+dupfdopen(struct proc *p, int indx, int mode)
 {
+	struct filedesc *fdp = p->p_fd;
 	struct file *wfp;
 
 	fdpassertlocked(fdp);
@@ -1221,10 +1222,10 @@ dupfdopen(struct filedesc *fdp, int indx, int dfd, int mode)
 	 * Assume that the filename was user-specified; applications do
 	 * not tend to open /dev/fd/# when they can just call dup()
 	 */
-	if ((curproc->p_p->ps_flags & (PS_SUGIDEXEC | PS_SUGID))) {
-		if (curproc->p_descfd == 255)
+	if ((p->p_p->ps_flags & (PS_SUGIDEXEC | PS_SUGID))) {
+		if (p->p_descfd == 255)
 			return (EPERM);
-		if (curproc->p_descfd != curproc->p_dupfd)
+		if (p->p_descfd != p->p_dupfd)
 			return (EPERM);
 	}
 
@@ -1235,7 +1236,7 @@ dupfdopen(struct filedesc *fdp, int indx, int dfd, int mode)
 	 * because fd_getfile will return NULL if the file at indx is
 	 * newly created by falloc (FIF_LARVAL).
 	 */
-	if ((wfp = fd_getfile(fdp, dfd)) == NULL)
+	if ((wfp = fd_getfile(fdp, p->p_dupfd)) == NULL)
 		return (EBADF);
 
 	/*
@@ -1249,7 +1250,7 @@ dupfdopen(struct filedesc *fdp, int indx, int dfd, int mode)
 
 	fdp->fd_ofiles[indx] = wfp;
 	fdp->fd_ofileflags[indx] = (fdp->fd_ofileflags[indx] & UF_EXCLOSE) |
-	    (fdp->fd_ofileflags[dfd] & ~UF_EXCLOSE);
+	    (fdp->fd_ofileflags[p->p_dupfd] & ~UF_EXCLOSE);
 	wfp->f_count++;
 	fd_used(fdp, indx);
 	return (0);
diff --git a/kern/vfs_syscalls.c b/kern/vfs_syscalls.c
index 6a3a2c8..19a7257 100644
--- a/kern/vfs_syscalls.c
+++ b/kern/vfs_syscalls.c
@@ -863,8 +863,7 @@ doopenat(struct proc *p, int fd, const char *path, int oflags, mode_t mode,
 	if ((error = vn_open(&nd, flags, cmode)) != 0) {
 		if (error == ENODEV &&
 		    p->p_dupfd >= 0 &&			/* XXX from fdopen */
-		    (error =
-			dupfdopen(fdp, indx, p->p_dupfd, flags)) == 0) {
+		    (error = dupfdopen(p, indx, flags)) == 0) {
 			closef(fp, p);
 			*retval = indx;
 			goto out;
diff --git a/sys/filedesc.h b/sys/filedesc.h
index 867aed5..b3c90b6 100644
--- a/sys/filedesc.h
+++ b/sys/filedesc.h
@@ -121,7 +121,7 @@ struct filedesc0 {
  * Kernel global variables and routines.
  */
 void	filedesc_init(void);
-int	dupfdopen(struct filedesc *, int, int, int);
+int	dupfdopen(struct proc *, int, int);
 int	fdalloc(struct proc *p, int want, int *result);
 void	fdexpand(struct proc *);
 int	falloc(struct proc *p, struct file **resultfp, int *resultfd);
-- 
1.9.3

