1 /* provide a replacement openat function
2 Copyright (C) 2004-2009 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17 /* written by Jim Meyering */
27 #include "dirname.h" /* solely for definition of IS_ABSOLUTE_FILE_NAME */
28 #include "openat-priv.h"
31 /* Replacement for Solaris' openat function.
32 <http://www.google.com/search?q=openat+site:docs.sun.com>
33 First, try to simulate it via open ("/proc/self/fd/FD/FILE").
34 Failing that, simulate it by doing save_cwd/fchdir/open/restore_cwd.
35 If either the save_cwd or the restore_cwd fails (relatively unlikely),
36 then give a diagnostic and exit nonzero.
37 Otherwise, upon failure, set errno and return -1, as openat does.
38 Upon successful completion, return a file descriptor. */
40 openat (int fd, char const *file, int flags, ...)
47 va_start (arg, flags);
49 /* We have to use PROMOTED_MODE_T instead of mode_t, otherwise GCC 4
50 creates crashing code when 'mode_t' is smaller than 'int'. */
51 mode = va_arg (arg, PROMOTED_MODE_T);
56 return openat_permissive (fd, file, flags, mode, NULL);
59 /* Like openat (FD, FILE, FLAGS, MODE), but if CWD_ERRNO is
60 nonnull, set *CWD_ERRNO to an errno value if unable to save
61 or restore the initial working directory. This is needed only
62 the first time remove.c's remove_dir opens a command-line
65 If a previous attempt to restore the current working directory
66 failed, then we must not even try to access a `.'-relative name.
67 It is the caller's responsibility not to call this function
71 openat_permissive (int fd, char const *file, int flags, mode_t mode,
74 struct saved_cwd saved_cwd;
79 if (fd == AT_FDCWD || IS_ABSOLUTE_FILE_NAME (file))
80 return open (file, flags, mode);
83 char buf[OPENAT_BUFFER_SIZE];
84 char *proc_file = openat_proc_name (buf, fd, file);
87 int open_result = open (proc_file, flags, mode);
88 int open_errno = errno;
91 /* If the syscall succeeds, or if it fails with an unexpected
92 errno value, then return right away. Otherwise, fall through
93 and resort to using save_cwd/restore_cwd. */
94 if (0 <= open_result || ! EXPECTED_ERRNO (open_errno))
102 save_ok = (save_cwd (&saved_cwd) == 0);
106 openat_save_fail (errno);
115 err = open (file, flags, mode);
117 if (save_ok && restore_cwd (&saved_cwd) != 0)
121 /* Don't write a message to just-created fd 2. */
123 if (err == STDERR_FILENO)
125 openat_restore_fail (saved_errno);
131 free_cwd (&saved_cwd);
136 /* Return true if our openat implementation must resort to
137 using save_cwd and restore_cwd. */
139 openat_needs_fchdir (void)
141 bool needs_fchdir = true;
142 int fd = open ("/", O_RDONLY);
146 char buf[OPENAT_BUFFER_SIZE];
147 char *proc_file = openat_proc_name (buf, fd, ".");
150 needs_fchdir = false;
151 if (proc_file != buf)