Skip to content
Snippets Groups Projects
Commit 2979f2d3 authored by Guy Zana's avatar Guy Zana
Browse files

fd: dup2() and dup3() now close newfd in case it was open

fdset(fd, fp) now installs fp on fd and closes a previousely opened file
if there was any, this makes dup2() and dup3() behave as expected.
parent a51c6201
No related branches found
No related tags found
No related merge requests found
...@@ -69,7 +69,7 @@ int fdclose(int fd) ...@@ -69,7 +69,7 @@ int fdclose(int fd)
} }
/* /*
* Try to set assign a file pointer to a specific file descriptor. * Assigns a file pointer to a specific file descriptor.
* Grabs a reference to the file pointer if successful. * Grabs a reference to the file pointer if successful.
*/ */
int fdset(int fd, struct file *fp) int fdset(int fd, struct file *fp)
...@@ -80,16 +80,14 @@ int fdset(int fd, struct file *fp) ...@@ -80,16 +80,14 @@ int fdset(int fd, struct file *fp)
return EBADF; return EBADF;
fhold(fp); fhold(fp);
mutex_lock(&gfdt_lock);
/* Make sure that no file pointer is currently installed on fd */ mutex_lock(&gfdt_lock);
orig = gfdt[fd]; orig = gfdt[fd];
if (orig != NULL) { if (orig != NULL) {
mutex_unlock(&gfdt_lock); fdrop(orig);
fdrop(fp);
return EBADF;
} }
/* Install new file structure in place */
gfdt[fd] = fp; gfdt[fd] = fp;
mutex_unlock(&gfdt_lock); mutex_unlock(&gfdt_lock);
......
...@@ -782,7 +782,7 @@ out_errno: ...@@ -782,7 +782,7 @@ out_errno:
*/ */
int dup3(int oldfd, int newfd, int flags) int dup3(int oldfd, int newfd, int flags)
{ {
struct file *fp, *org = NULL; struct file *fp;
int error; int error;
/* /*
...@@ -803,10 +803,6 @@ int dup3(int oldfd, int newfd, int flags) ...@@ -803,10 +803,6 @@ int dup3(int oldfd, int newfd, int flags)
if (error) if (error)
goto out_errno; goto out_errno;
/* FIXME: Should atomically close newfd or report error */
error = fget(newfd, &org);
assert(error == 0);
error = fdset(newfd, fp); error = fdset(newfd, fp);
if (error) { if (error) {
fdrop(fp); fdrop(fp);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment