From 2966d0d21fdc455b705e1b477dfe7e1b37f5f8e9 Mon Sep 17 00:00:00 2001 From: Eric Blake Date: Tue, 8 Dec 2009 06:13:05 -0700 Subject: [PATCH] fchdir: fix logic bugs Configuring with ac_cv_func_fchdir=no on a system that has fchdir and where open handles directories, just to test out the replacement capabilities, uncovered an m4 test bug and a link failure on rpl_fstat. * m4/fchdir.m4 (gl_FUNC_FCHDIR): Fix logic bug. * tests/test-fchdir.c (main): Enhance test. * lib/fchdir.c (rpl_fstat): Always provide if fchdir replacement is in use. Signed-off-by: Eric Blake --- ChangeLog | 6 ++++++ lib/fchdir.c | 7 +++---- m4/fchdir.m4 | 4 ++-- tests/test-fchdir.c | 8 +++++++- 4 files changed, 18 insertions(+), 7 deletions(-) diff --git a/ChangeLog b/ChangeLog index 40d2dacc8..24d7e2018 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,11 @@ 2009-12-08 Eric Blake + fchdir: fix logic bugs + * m4/fchdir.m4 (gl_FUNC_FCHDIR): Fix logic bug. + * tests/test-fchdir.c (main): Enhance test. + * lib/fchdir.c (rpl_fstat): Always provide if fchdir replacement + is in use. + dup2: fix logic bugs * lib/dup2.c (dup2): Fix logic bugs. Use HAVE_DUP2 rather than REPLACE_DUP2 to decide when rpl_dup2 is needed. diff --git a/lib/fchdir.c b/lib/fchdir.c index 16b17b4e8..4cc0f33ce 100644 --- a/lib/fchdir.c +++ b/lib/fchdir.c @@ -216,16 +216,15 @@ _gl_directory_name (int fd) /* Return stat information about FD in STATBUF. Needed when rpl_open() used a dummy file to work around an open() that can't normally visit directories. */ -#if REPLACE_OPEN_DIRECTORY -# undef fstat +#undef fstat int rpl_fstat (int fd, struct stat *statbuf) { - if (0 <= fd && fd < dirs_allocated && dirs[fd].name != NULL) + if (REPLACE_OPEN_DIRECTORY + && 0 <= fd && fd < dirs_allocated && dirs[fd].name != NULL) return stat (dirs[fd].name, statbuf); return fstat (fd, statbuf); } -#endif /* Override opendir() and closedir(), to keep track of the open file descriptors. Needed because there is a function dirfd(). */ diff --git a/m4/fchdir.m4 b/m4/fchdir.m4 index f0e4dc00d..6243cc283 100644 --- a/m4/fchdir.m4 +++ b/m4/fchdir.m4 @@ -1,4 +1,4 @@ -# fchdir.m4 serial 10 +# fchdir.m4 serial 11 dnl Copyright (C) 2006-2009 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, @@ -31,7 +31,7 @@ AC_DEFUN([gl_FUNC_FCHDIR], AC_CACHE_CHECK([whether open can visit directories], [gl_cv_func_open_directory_works], [AC_RUN_IFELSE([AC_LANG_PROGRAM([[#include -]], [return open(".", O_RDONLY);])], +]], [return open(".", O_RDONLY) < 0;])], [gl_cv_func_open_directory_works=yes], [gl_cv_func_open_directory_works=no], [gl_cv_func_open_directory_works="guessing no"])]) diff --git a/tests/test-fchdir.c b/tests/test-fchdir.c index 53d6631cc..0419f43fd 100644 --- a/tests/test-fchdir.c +++ b/tests/test-fchdir.c @@ -26,6 +26,8 @@ #include #include +#include "cloexec.h" + #define ASSERT(expr) \ do \ { \ @@ -82,7 +84,11 @@ main (void) int new_fd = dup (fd); ASSERT (0 <= new_fd); ASSERT (close (fd) == 0); - fd = new_fd; + ASSERT (dup2 (new_fd, fd) == fd); + ASSERT (close (new_fd) == 0); + ASSERT (dup_cloexec (fd) == new_fd); + ASSERT (dup2 (new_fd, fd) == fd); + ASSERT (close (new_fd) == 0); } } -- 2.11.0