changeset 7749:eda4a9c5e8ee

Work around an fchownat bug in glibc-2.4: http://lists.ubuntu.com/archives/ubuntu-users/2006-September/093218.html This bug would cause "chown -RP ... DIR" to follow symlinks in DIR, in spite of the -P option. * m4/openat.m4 (gl_FUNC_FCHOWNAT, gl_FUNC_FCHOWNAT_DEREF_BUG): New macros. (gl_PREREQ_OPENAT): Require gl_FUNC_FCHOWNAT. * modules/openat (Files): Add lib/fchownat.c. * lib/openat.c (fchownat): Don't define here. Move to... * lib/fchownat.c: ...this new file.
author Jim Meyering <jim@meyering.net>
date Sun, 24 Dec 2006 17:08:04 +0000
parents 13a0cdc745f8
children 3b5c380a3425
files ChangeLog lib/fchownat.c lib/openat.c m4/openat.m4 modules/openat
diffstat 5 files changed, 125 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,16 @@
+2006-12-24  Jim Meyering  <jim@meyering.net>
+
+	Work around an fchownat bug in glibc-2.4:
+	http://lists.ubuntu.com/archives/ubuntu-users/2006-September/093218.html
+	This bug would cause "chown -RP ... DIR" to follow symlinks in DIR,
+	in spite of the -P option.
+	* m4/openat.m4 (gl_FUNC_FCHOWNAT, gl_FUNC_FCHOWNAT_DEREF_BUG):
+	New macros.
+	(gl_PREREQ_OPENAT): Require gl_FUNC_FCHOWNAT.
+	* modules/openat (Files): Add lib/fchownat.c.
+	* lib/openat.c (fchownat): Don't define here.  Move to...
+	* lib/fchownat.c: ...this new file.
+
 2006-12-23  Paul Eggert  <eggert@cs.ucla.edu>
 
 	Fix bug reported by Bruno Haible in
new file mode 100644
--- /dev/null
+++ b/lib/fchownat.c
@@ -0,0 +1,49 @@
+/* This function serves as replacement for a missing fchownat function,
+   as well as a work around for the fchownat bug in glibc-2.4:
+    <http://lists.ubuntu.com/archives/ubuntu-users/2006-September/093218.html>
+   when the buggy fchownat-with-AT_SYMLINK_NOFOLLOW operates on a symlink, it
+   mistakenly affects the symlink referent, rather than the symlink itself.
+
+   Copyright (C) 2006 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2, or (at your option)
+   any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software Foundation,
+   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
+
+/* written by Jim Meyering */
+
+#include <config.h>
+
+#include "openat.h"
+
+#include <unistd.h>
+
+#include "dirname.h" /* solely for definition of IS_ABSOLUTE_FILE_NAME */
+#include "save-cwd.h"
+#include "openat-priv.h"
+
+/* Replacement for Solaris' function by the same name.
+   Invoke chown or lchown on file, FILE, using OWNER and GROUP, in the
+   directory open on descriptor FD.  If FLAG is AT_SYMLINK_NOFOLLOW, then
+   use lchown, otherwise, use chown.  If possible, do it without changing
+   the working directory.  Otherwise, resort to using save_cwd/fchdir,
+   then mkdir/restore_cwd.  If either the save_cwd or the restore_cwd
+   fails, then give a diagnostic and exit nonzero.  */
+
+#define AT_FUNC_NAME fchownat
+#define AT_FUNC_F1 lchown
+#define AT_FUNC_F2 chown
+#define AT_FUNC_USE_F1_COND flag == AT_SYMLINK_NOFOLLOW
+#define AT_FUNC_POST_FILE_PARAM_DECLS , uid_t owner, gid_t group, int flag
+#define AT_FUNC_POST_FILE_ARGS        , owner, group
+#include "at-func.c"
--- a/lib/openat.c
+++ b/lib/openat.c
@@ -268,19 +268,3 @@
 #undef AT_FUNC_USE_F1_COND
 #undef AT_FUNC_POST_FILE_PARAM_DECLS
 #undef AT_FUNC_POST_FILE_ARGS
-
-/* Replacement for Solaris' function by the same name.
-   Invoke chown or lchown on file, FILE, using OWNER and GROUP, in the
-   directory open on descriptor FD.  If FLAG is AT_SYMLINK_NOFOLLOW, then
-   use lchown, otherwise, use chown.  If possible, do it without changing
-   the working directory.  Otherwise, resort to using save_cwd/fchdir,
-   then mkdir/restore_cwd.  If either the save_cwd or the restore_cwd
-   fails, then give a diagnostic and exit nonzero.  */
-
-#define AT_FUNC_NAME fchownat
-#define AT_FUNC_F1 lchown
-#define AT_FUNC_F2 chown
-#define AT_FUNC_USE_F1_COND flag == AT_SYMLINK_NOFOLLOW
-#define AT_FUNC_POST_FILE_PARAM_DECLS , uid_t owner, gid_t group, int flag
-#define AT_FUNC_POST_FILE_ARGS        , owner, group
-#include "at-func.c"
--- a/m4/openat.m4
+++ b/m4/openat.m4
@@ -1,4 +1,4 @@
-#serial 12
+#serial 13
 # See if we need to use our replacement for Solaris' openat et al functions.
 
 dnl Copyright (C) 2004, 2005, 2006 Free Software Foundation, Inc.
@@ -24,6 +24,67 @@
       [Define to rpl_ if the openat replacement function should be used.])
     gl_PREREQ_OPENAT;;
   esac
+  AC_REQUIRE([gl_FUNC_FCHOWNAT])
+])
+
+# gl_FUNC_FCHOWNAT_DEREF_BUG([ACTION-IF-BUGGY[, ACTION-IF-NOT_BUGGY]])
+AC_DEFUN([gl_FUNC_FCHOWNAT_DEREF_BUG],
+[
+  AC_CACHE_CHECK([whether fchownat works with AT_SYMLINK_NOFOLLOW],
+    gl_cv_func_fchownat_nofollow_works,
+    [
+     gl_dangle=conftest.dangle
+     # Remove any remnants of a previous test.
+     rm -f $gl_dangle
+     # Arrange for deletion of the temporary file this test creates.
+     ac_clean_files="$ac_clean_files $gl_dangle"
+     AC_RUN_IFELSE(
+       [AC_LANG_SOURCE(
+	  [[
+#include <fcntl.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <sys/types.h>
+int
+main ()
+{
+  return (fchownat (AT_FDCWD, "$gl_dangle", -1, getgid (),
+		    AT_SYMLINK_NOFOLLOW) != 0
+	  && errno == ENOENT);
+}
+          ]])],
+    [gl_cv_func_fchownat_nofollow_works=yes],
+    [gl_cv_func_fchownat_nofollow_works=no],
+    [gl_cv_func_fchownat_nofollow_works=no],
+    )
+  ])
+  AS_IF([test $gl_cv_func_fchownat_nofollow_works = no], [$1], [$2])
+])
+
+# If we have the fchownat function, and it has the bug (in glibc-2.4)
+# that it dereferences symlinks even with AT_SYMLINK_NOFOLLOW, then
+# use the replacement function.
+# Also use the replacement function if fchownat is simply not available.
+AC_DEFUN([gl_FUNC_FCHOWNAT],
+[
+  # Assume we'll use the replacement function.
+  # The only case in which we won't is when we have fchownat, and it works.
+  use_replacement_fchownat=yes
+
+  AC_CHECK_FUNC([fchownat], [have_fchownat=yes], [have_fchownat=no])
+  if test $have_fchownat = yes; then
+    gl_FUNC_FCHOWNAT_DEREF_BUG([have_fchownat_bug=yes])
+    if test $have_fchownat_bug = no; then
+      use_replacement_fchownat=no
+    fi
+  fi
+
+  if test $use_replacement_fchownat = yes; then
+    AC_LIBOBJ(fchownat)
+    AC_DEFINE(fchownat, rpl_fchownat,
+      [Define to rpl_fchownat if the replacement function should be used.])
+  fi
 ])
 
 AC_DEFUN([gl_PREREQ_OPENAT],
--- a/modules/openat
+++ b/modules/openat
@@ -4,6 +4,7 @@
 Files:
 lib/at-func.c
 lib/fchmodat.c
+lib/fchownat.c
 lib/fstatat.c
 lib/mkdirat.c
 lib/openat.c