changeset 7389:0f8d70c02f29

[lib/ChangeLog] Work around bug in Solaris 10 /proc file system: /proc/self/fd/NNN/.. isn't the parent directory of the directory whose file descriptor is NNN. This needs to be worked around at run time, not compile time, since a program might be built on Solaris 8, where things work, and run on Solaris 10. * openat-priv.h (BUILD_PROC_NAME): Remove. All callers changed to use the following interface instead: (OPENAT_BUFFER_SIZE): New macro. (openat_proc_name): New function. * at-func.c (AT_FUNC_NAME): Adjust to above changes. * openat.c (openat_permissive, openat_needs_fchdir, fdopendir): Likewise. * openat-proc.c: New file. [ChangeLog] * modules/openat (Files): Add lib/openat-proc.c. (Depends-on): Add same-inode, stdbool. [m4/ChangeLog] * openat.m4 (gl_FUNC_OPENAT): Add AC_LIBOBJ(openat-proc).
author Paul Eggert <eggert@cs.ucla.edu>
date Sat, 30 Sep 2006 00:37:48 +0000
parents de85bc61c2dc
children 913e53ae88c6
files ChangeLog lib/ChangeLog lib/at-func.c lib/openat-priv.h lib/openat-proc.c lib/openat.c m4/ChangeLog m4/openat.m4 modules/openat
diffstat 9 files changed, 191 insertions(+), 55 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2006-09-29  Paul Eggert  <eggert@cs.ucla.edu>
+
+	* modules/openat (Files): Add lib/openat-proc.c.
+	(Depends-on): Add same-inode, stdbool.
+
 2006-09-28  Bruno Haible  <bruno@clisp.org>
 
 	* modules/avltreehash-list (Depends-on): Add stdint, remove size_max.
--- a/lib/ChangeLog
+++ b/lib/ChangeLog
@@ -1,3 +1,20 @@
+2006-09-29  Paul Eggert  <eggert@cs.ucla.edu>
+
+	Work around bug in Solaris 10 /proc file system:
+	/proc/self/fd/NNN/.. isn't the parent directory of
+	the directory whose file descriptor is NNN.  This needs to
+	be worked around at run time, not compile time, since a
+	program might be built on Solaris 8, where things work, and
+	run on Solaris 10.
+	* openat-priv.h (BUILD_PROC_NAME): Remove.  All callers changed
+	to use the following interface instead:
+	(OPENAT_BUFFER_SIZE): New macro.
+	(openat_proc_name): New function.
+	* at-func.c (AT_FUNC_NAME): Adjust to above changes.
+	* openat.c (openat_permissive, openat_needs_fchdir, fdopendir):
+	Likewise.
+	* openat-proc.c: New file.
+
 2006-09-29  Bruno Haible  <bruno@clisp.org>
 
 	* fwriteerror.h (fwriteerror_no_ebadf): New declaration.
--- a/lib/at-func.c
+++ b/lib/at-func.c
@@ -39,14 +39,25 @@
     return CALL_FUNC (file);
 
   {
-    char *proc_file;
-    BUILD_PROC_NAME (proc_file, fd, file);
-    err = CALL_FUNC (proc_file);
-    /* If the syscall succeeds, or if it fails with an unexpected
-       errno value, then return right away.  Otherwise, fall through
-       and resort to using save_cwd/restore_cwd.  */
-    if (0 <= err || ! EXPECTED_ERRNO (errno))
-      return err;
+    char buf[OPENAT_BUFFER_SIZE];
+    char *proc_file = openat_proc_name (buf, fd, file);
+    if (proc_file)
+      {
+	int proc_result = CALL_FUNC (proc_file);
+	int proc_errno = errno;
+	if (proc_file != buf)
+	  free (proc_file);
+	/* If the syscall succeeds, or if it fails with an unexpected
+	   errno value, then return right away.  Otherwise, fall through
+	   and resort to using save_cwd/restore_cwd.  */
+	if (0 <= proc_result)
+	  return proc_result;
+	if (! EXPECTED_ERRNO (proc_errno))
+	  {
+	    errno = proc_errno;
+	    return proc_result;
+	  }
+      }
   }
 
   if (save_cwd (&saved_cwd) != 0)
--- a/lib/openat-priv.h
+++ b/lib/openat-priv.h
@@ -1,5 +1,6 @@
-/* macros used by openat-like functions
-   Copyright (C) 2005 Free Software Foundation, Inc.
+/* Internals for openat-like functions.
+
+   Copyright (C) 2005, 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
@@ -17,30 +18,11 @@
 
 /* written by Jim Meyering */
 
-#include <stdio.h>
-#include <string.h>
 #include <errno.h>
-#include "alloca.h"
-#include "intprops.h"
+#include <stdlib.h>
 
-/* Set PROC_FD_FILENAME to the expansion of "/proc/self/fd/%d/%s" in
-   alloca'd memory, using FD and FILE, respectively for %d and %s. */
-#define BUILD_PROC_NAME(Proc_fd_filename, Fd, File)			\
-  do									\
-    {									\
-      size_t filelen = strlen (File);					\
-      static const char procfd[] = "/proc/self/fd/%d/%s";		\
-      /* Buffer for the file name we are going to use.  It consists of	\
-	 - the string /proc/self/fd/					\
-	 - the file descriptor number					\
-	 - the file name provided.					\
-	 The final NUL is included in the sizeof.			\
-	 Subtract 4 to account for %d and %s.  */			\
-      size_t buflen = sizeof (procfd) - 4 + INT_STRLEN_BOUND (Fd) + filelen; \
-      (Proc_fd_filename) = alloca (buflen);				\
-      snprintf ((Proc_fd_filename), buflen, procfd, (Fd), (File));	\
-    }									\
-  while (0)
+#define OPENAT_BUFFER_SIZE 512
+char *openat_proc_name (char buf[OPENAT_BUFFER_SIZE], int fd, char const *file);
 
 /* Some systems don't have ENOSYS.  */
 #ifndef ENOSYS
new file mode 100644
--- /dev/null
+++ b/lib/openat-proc.c
@@ -0,0 +1,90 @@
+/* Create /proc/self/fd-related names for subfiles of open directories.
+
+   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 Paul Eggert.  */
+
+#include <config.h>
+
+#include "openat-priv.h"
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
+#include <stdio.h>
+#include <string.h>
+
+#include "dirname.h"
+#include "intprops.h"
+#include "same-inode.h"
+#include "xalloc.h"
+
+#define PROC_SELF_FD_FORMAT "/proc/self/fd/%d/%s"
+
+#define PROC_SELF_FD_NAME_SIZE_BOUND(len) \
+  (sizeof PROC_SELF_FD_FORMAT - sizeof "%d%s" \
+   + INT_STRLEN_BOUND (int) + (len) + 1)
+
+
+/* Set BUF to the expansion of PROC_SELF_FD_FORMAT, using FD and FILE
+   respectively for %d and %s.  If successful, return BUF if the
+   result fits in BUF, dynamically allocated memory otherwise.  But
+   return NULL if /proc is not reliable.  */
+char *
+openat_proc_name (char buf[OPENAT_BUFFER_SIZE], int fd, char const *file)
+{
+  static int proc_status = 0;
+
+  if (! proc_status)
+    {
+      /* Set PROC_STATUS to a positive value if /proc/self/fd is
+	 reliable, and a negative value otherwise.  Solaris 10
+	 /proc/self/fd mishandles "..", and any file name might expand
+	 to ".." after symbolic link expansion, so avoid /proc/self/fd
+	 if it mishandles "..".  Solaris 10 has openat, but this
+	 problem is exhibited on code that built on Solaris 8 and
+	 running on Solaris 10.  */
+
+      int proc_self_fd = open ("/proc/self/fd", O_RDONLY);
+      if (proc_self_fd < 0)
+	proc_status = -1;
+      else
+	{
+	  struct stat proc_self_fd_dotdot_st;
+	  struct stat proc_self_st;
+	  char dotdot_buf[PROC_SELF_FD_NAME_SIZE_BOUND (sizeof ".." - 1)];
+	  sprintf (dotdot_buf, PROC_SELF_FD_FORMAT, proc_self_fd, "..");
+	  proc_status =
+	    ((stat (dotdot_buf, &proc_self_fd_dotdot_st) == 0
+	      && stat ("/proc/self", &proc_self_st) == 0
+	      && SAME_INODE (proc_self_fd_dotdot_st, proc_self_st))
+	     ? 1 : -1);
+	  close (proc_self_fd);
+	}
+    }
+
+  if (proc_status < 0)
+    return NULL;
+  else
+    {
+      size_t bufsize = PROC_SELF_FD_NAME_SIZE_BOUND (strlen (file));
+      char *result = (bufsize < OPENAT_BUFFER_SIZE ? buf : xmalloc (bufsize));
+      sprintf (result, PROC_SELF_FD_FORMAT, fd, file);
+      return result;
+    }
+}
--- a/lib/openat.c
+++ b/lib/openat.c
@@ -85,14 +85,23 @@
     return open (file, flags, mode);
 
   {
-    char *proc_file;
-    BUILD_PROC_NAME (proc_file, fd, file);
-    err = open (proc_file, flags, mode);
-    /* If the syscall succeeds, or if it fails with an unexpected
-       errno value, then return right away.  Otherwise, fall through
-       and resort to using save_cwd/restore_cwd.  */
-    if (0 <= err || ! EXPECTED_ERRNO (errno))
-      return err;
+    char buf[OPENAT_BUFFER_SIZE];
+    char *proc_file = openat_proc_name (buf, fd, file);
+    if (proc_file)
+      {
+	int open_result = open (proc_file, flags, mode);
+	int open_errno = errno;
+	if (proc_file != buf)
+	  free (proc_file);
+	/* If the syscall succeeds, or if it fails with an unexpected
+	   errno value, then return right away.  Otherwise, fall through
+	   and resort to using save_cwd/restore_cwd.  */
+	if (0 <= open_result || ! EXPECTED_ERRNO (open_errno))
+	  {
+	    errno = open_errno;
+	    return open_result;
+	  }
+      }
   }
 
   save_ok = (save_cwd (&saved_cwd) == 0);
@@ -128,19 +137,23 @@
 bool
 openat_needs_fchdir (void)
 {
-  int fd2;
+  bool needs_fchdir = true;
   int fd = open ("/", O_RDONLY);
-  char *proc_file;
 
-  if (fd < 0)
-    return true;
-  BUILD_PROC_NAME (proc_file, fd, ".");
-  fd2 = open (proc_file, O_RDONLY);
-  close (fd);
-  if (0 <= fd2)
-    close (fd2);
+  if (0 <= fd)
+    {
+      char buf[OPENAT_BUFFER_SIZE];
+      char *proc_file = openat_proc_name (buf, fd, ".");
+      if (proc_file)
+	{
+	  needs_fchdir = false;
+	  if (proc_file != buf)
+	    free (proc_file);
+	}
+      close (fd);
+    }
 
-  return fd2 < 0;
+  return needs_fchdir;
 }
 
 #if !HAVE_FDOPENDIR
@@ -164,10 +177,18 @@
   int saved_errno;
   DIR *dir;
 
-  char *proc_file;
-  BUILD_PROC_NAME (proc_file, fd, ".");
-  dir = opendir (proc_file);
-  saved_errno = errno;
+  char buf[OPENAT_BUFFER_SIZE];
+  char *proc_file = openat_proc_name (buf, fd, ".");
+  if (proc_file)
+    {
+      dir = opendir (proc_file);
+      saved_errno = errno;
+    }
+  else
+    {
+      dir = NULL;
+      saved_errno = EOPNOTSUPP;
+    }
 
   /* If the syscall fails with an expected errno value, resort to
      save_cwd/restore_cwd.  */
@@ -195,6 +216,8 @@
 
   if (dir)
     close (fd);
+  if (proc_file != buf)
+    free (proc_file);
   errno = saved_errno;
   return dir;
 }
--- a/m4/ChangeLog
+++ b/m4/ChangeLog
@@ -1,3 +1,7 @@
+2006-09-29  Paul Eggert  <eggert@cs.ucla.edu>
+
+	* openat.m4 (gl_FUNC_OPENAT): Add AC_LIBOBJ(openat-proc).
+
 2006-09-28  Bruno Haible  <bruno@clisp.org>
 
 	* strndup.m4 (gl_FUNC_STRNDUP): Don't define __STRNDUP_PREFIX.
--- a/m4/openat.m4
+++ b/m4/openat.m4
@@ -1,4 +1,4 @@
-#serial 10
+#serial 11
 # See if we need to use our replacement for Solaris' openat et al functions.
 
 dnl Copyright (C) 2004, 2005, 2006 Free Software Foundation, Inc.
@@ -15,6 +15,7 @@
   AC_LIBOBJ([fchmodat])
 
   AC_LIBOBJ([openat-die])
+  AC_LIBOBJ([openat-proc])
   AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
   AC_CHECK_FUNCS_ONCE([lchmod])
   AC_CHECK_FUNCS_ONCE([fdopendir])
--- a/modules/openat
+++ b/modules/openat
@@ -9,6 +9,7 @@
 lib/openat.h
 lib/openat-die.c
 lib/openat-priv.h
+lib/openat-proc.c
 m4/openat.m4
 
 Depends-on:
@@ -18,7 +19,9 @@
 intprops
 lchown
 lstat
+same-inode
 save-cwd
+stdbool
 
 configure.ac:
 gl_FUNC_OPENAT