bug-coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

FYI: work around fchownat bug in glibc-2.4


From: Jim Meyering
Subject: FYI: work around fchownat bug in glibc-2.4
Date: Sun, 24 Dec 2006 18:09:26 +0100

The following patch fixes a test failure in coreutils-6.N (N <= 7),
when run on a system using glibc-2.4.

2006-12-24  Jim Meyering  <address@hidden>

        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.

Index: m4/openat.m4
===================================================================
RCS file: /sources/gnulib/gnulib/m4/openat.m4,v
retrieving revision 1.10
diff -u -p -r1.10 openat.m4
--- m4/openat.m4        12 Dec 2006 07:37:28 -0000      1.10
+++ m4/openat.m4        24 Dec 2006 16:52:23 -0000
@@ -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 @@ AC_DEFUN([gl_FUNC_OPENAT],
       [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],
Index: modules/openat
===================================================================
RCS file: /sources/gnulib/gnulib/modules/openat,v
retrieving revision 1.17
diff -u -p -r1.17 openat
--- modules/openat      20 Nov 2006 22:01:30 -0000      1.17
+++ modules/openat      24 Dec 2006 16:52:23 -0000
@@ -4,6 +4,7 @@ Open a file at a directory.
 Files:
 lib/at-func.c
 lib/fchmodat.c
+lib/fchownat.c
 lib/fstatat.c
 lib/mkdirat.c
 lib/openat.c
Index: lib/openat.c
===================================================================
RCS file: /sources/gnulib/gnulib/lib/openat.c,v
retrieving revision 1.15
diff -u -p -r1.15 openat.c
--- lib/openat.c        5 Oct 2006 22:49:57 -0000       1.15
+++ lib/openat.c        24 Dec 2006 16:52:23 -0000
@@ -268,19 +268,3 @@ fdopendir (int fd)
 #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"
Index: lib/fchownat.c
===================================================================
RCS file: lib/fchownat.c
diff -N lib/fchownat.c
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ lib/fchownat.c      24 Dec 2006 16:52:23 -0000
@@ -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"




reply via email to

[Prev in Thread] Current Thread [Next in Thread]