* Replace the FD used for fopendir() instead of closing the old one - according

to the specs, the application may still use the FD without changing its state,
  and only closedir() should finally close it.
* This fixes bug #5055.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@34362 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler
2009-11-30 08:29:12 +00:00
parent ac628ead05
commit a4faebeb11
+13 -6
View File
@@ -133,18 +133,25 @@ fdopendir(int fd)
return NULL;
}
// Since applications are allowed to use the file descriptor after a call
// to fdopendir() without changing its state (like for other *at()
// functions), we cannot close it now.
// We dup2() the new FD to the previous location instead.
if (dup2(dirFD, fd) == -1)
close(fd);
else {
close(dirFD);
dirFD = fd;
fcntl(dirFD, F_SETFD, FD_CLOEXEC);
// reset close-on-exec which is cleared by dup()
}
dir = __create_dir_struct(dirFD);
if (dir == NULL) {
close(dirFD);
return NULL;
}
// According to the spec, "the file descriptor is under the control of the
// system" now. It's not quite clear whether we're allowed to close it now,
// though. We could dup2() the new FD over the old one and close the new
// one, if it turns out to be a problem.
close(fd);
return dir;
}