From a4faebeb11aec132d75cb08999423c72f747f1b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Axel=20D=C3=B6rfler?= Date: Mon, 30 Nov 2009 08:29:12 +0000 Subject: [PATCH] * 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 --- src/system/libroot/posix/dirent.c | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/system/libroot/posix/dirent.c b/src/system/libroot/posix/dirent.c index c3e54155a5..c1e93e93cc 100644 --- a/src/system/libroot/posix/dirent.c +++ b/src/system/libroot/posix/dirent.c @@ -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; }