[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
workaround /proc/self/fd bug on Solaris 10
From: |
Paul Eggert |
Subject: |
workaround /proc/self/fd bug on Solaris 10 |
Date: |
Fri, 29 Sep 2006 17:41:20 -0700 |
User-agent: |
Gnus/5.1008 (Gnus v5.10.8) Emacs/21.4 (gnu/linux) |
I noticed that a build of CVS coreutils on Solaris 8 created
executables that didn't run on Solaris 10, because on Solaris 10
/proc/self/fd/NNN/.. is equivalent to /proc/sef/fd, not to the parent
directory of the file with descriptor NNN. I'm not sure why Sun made
this change, but it broke the openat emulation. I installed the
following into gnulib to work around the problem.
2006-09-29 Paul Eggert <address@hidden>
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.
* lib/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.
* lib/at-func.c (AT_FUNC_NAME): Adjust to above changes.
* lib/openat.c (openat_permissive, openat_needs_fchdir, fdopendir):
Likewise.
* lib/openat-proc.c: New file.
* modules/openat (Files): Add lib/openat-proc.c.
(Depends-on): Add same-inode, stdbool.
* m4/openat.m4 (gl_FUNC_OPENAT): Add AC_LIBOBJ(openat-proc).
Index: lib/at-func.c
===================================================================
RCS file: /cvsroot/gnulib/gnulib/lib/at-func.c,v
retrieving revision 1.1
diff -p -u -r1.1 at-func.c
--- lib/at-func.c 17 Aug 2006 20:34:21 -0000 1.1
+++ lib/at-func.c 30 Sep 2006 00:35:30 -0000
@@ -39,14 +39,25 @@ AT_FUNC_NAME (int fd, char const *file A
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)
Index: lib/openat-priv.h
===================================================================
RCS file: /cvsroot/gnulib/gnulib/lib/openat-priv.h,v
retrieving revision 1.2
diff -p -u -r1.2 openat-priv.h
--- lib/openat-priv.h 17 Aug 2006 20:34:21 -0000 1.2
+++ lib/openat-priv.h 30 Sep 2006 00:35:30 -0000
@@ -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
Index: lib/openat.c
===================================================================
RCS file: /cvsroot/gnulib/gnulib/lib/openat.c,v
retrieving revision 1.13
diff -p -u -r1.13 openat.c
--- lib/openat.c 13 Sep 2006 22:38:14 -0000 1.13
+++ lib/openat.c 30 Sep 2006 00:35:30 -0000
@@ -85,14 +85,23 @@ openat_permissive (int fd, char const *f
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 @@ openat_permissive (int fd, char const *f
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 @@ fdopendir (int fd)
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 @@ fdopendir (int fd)
if (dir)
close (fd);
+ if (proc_file != buf)
+ free (proc_file);
errno = saved_errno;
return dir;
}
Index: m4/openat.m4
===================================================================
RCS file: /cvsroot/gnulib/gnulib/m4/openat.m4,v
retrieving revision 1.7
diff -p -u -r1.7 openat.m4
--- m4/openat.m4 21 Aug 2006 21:46:31 -0000 1.7
+++ m4/openat.m4 30 Sep 2006 00:35:31 -0000
@@ -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_DEFUN([gl_FUNC_OPENAT],
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])
Index: modules/openat
===================================================================
RCS file: /cvsroot/gnulib/gnulib/modules/openat,v
retrieving revision 1.13
diff -p -u -r1.13 openat
--- modules/openat 23 Aug 2006 23:07:01 -0000 1.13
+++ modules/openat 30 Sep 2006 00:35:31 -0000
@@ -9,6 +9,7 @@ lib/openat.c
lib/openat.h
lib/openat-die.c
lib/openat-priv.h
+lib/openat-proc.c
m4/openat.m4
Depends-on:
@@ -18,7 +19,9 @@ gettext-h
intprops
lchown
lstat
+same-inode
save-cwd
+stdbool
configure.ac:
gl_FUNC_OPENAT
--- /dev/null 2005-09-24 22:00:15.000000000 -0700
+++ lib/openat-proc.c 2006-09-29 17:30:45.000000000 -0700
@@ -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;
+ }
+}
- workaround /proc/self/fd bug on Solaris 10,
Paul Eggert <=