From: Paul Eggert Date: Sat, 30 Sep 2006 00:37:48 +0000 (+0000) Subject: [lib/ChangeLog] X-Git-Tag: cvs-readonly~1804 X-Git-Url: http://erislabs.net/gitweb/?a=commitdiff_plain;h=75ff6c9d9a4d428c44dbbd8778f0e8080a0d3bdc;p=gnulib.git [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). --- diff --git a/ChangeLog b/ChangeLog index 707e40f05..eb6ac9a75 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2006-09-29 Paul Eggert + + * modules/openat (Files): Add lib/openat-proc.c. + (Depends-on): Add same-inode, stdbool. + 2006-09-28 Bruno Haible * modules/avltreehash-list (Depends-on): Add stdint, remove size_max. diff --git a/lib/ChangeLog b/lib/ChangeLog index 0b3f6c38d..cf4160290 100644 --- a/lib/ChangeLog +++ b/lib/ChangeLog @@ -1,3 +1,20 @@ +2006-09-29 Paul Eggert + + 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 * fwriteerror.h (fwriteerror_no_ebadf): New declaration. diff --git a/lib/at-func.c b/lib/at-func.c index 2bdea7b93..f98c207e7 100644 --- a/lib/at-func.c +++ b/lib/at-func.c @@ -39,14 +39,25 @@ AT_FUNC_NAME (int fd, char const *file AT_FUNC_POST_FILE_PARAM_DECLS) 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) diff --git a/lib/openat-priv.h b/lib/openat-priv.h index 1f420c175..527cff808 100644 --- 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 -#include #include -#include "alloca.h" -#include "intprops.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) +#include + +#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 diff --git a/lib/openat-proc.c b/lib/openat-proc.c new file mode 100644 index 000000000..87293a46f --- /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 + +#include "openat-priv.h" + +#include +#include +#include + +#include +#include + +#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; + } +} diff --git a/lib/openat.c b/lib/openat.c index 49ecde883..82b4a06cf 100644 --- a/lib/openat.c +++ b/lib/openat.c @@ -85,14 +85,23 @@ openat_permissive (int fd, char const *file, int flags, mode_t mode, 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 *file, int flags, mode_t mode, 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; } diff --git a/m4/ChangeLog b/m4/ChangeLog index 2999054b1..51d4a54dd 100644 --- a/m4/ChangeLog +++ b/m4/ChangeLog @@ -1,3 +1,7 @@ +2006-09-29 Paul Eggert + + * openat.m4 (gl_FUNC_OPENAT): Add AC_LIBOBJ(openat-proc). + 2006-09-28 Bruno Haible * strndup.m4 (gl_FUNC_STRNDUP): Don't define __STRNDUP_PREFIX. diff --git a/m4/openat.m4 b/m4/openat.m4 index a17056532..fca3bd061 100644 --- 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_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]) diff --git a/modules/openat b/modules/openat index d3b0272b6..cb6db8f8d 100644 --- a/modules/openat +++ b/modules/openat @@ -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