+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.
+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.
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)
-/* 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
/* written by Jim Meyering */
-#include <stdio.h>
-#include <string.h>
#include <errno.h>
-#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 <stdlib.h>
+
+#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
--- /dev/null
+/* 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;
+ }
+}
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);
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
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. */
if (dir)
close (fd);
+ if (proc_file != buf)
+ free (proc_file);
errno = saved_errno;
return dir;
}
+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.
-#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.
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])
lib/openat.h
lib/openat-die.c
lib/openat-priv.h
+lib/openat-proc.c
m4/openat.m4
Depends-on:
intprops
lchown
lstat
+same-inode
save-cwd
+stdbool
configure.ac:
gl_FUNC_OPENAT