Update copyright notices with scripts/update-copyrights.
[glibc.git] / sysdeps / unix / sysv / linux / openat.c
blobd2ef4297368fa3fb8fd87826157af28b863fa16b
1 /* Copyright (C) 2005-2013 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library 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 GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>. */
18 #include <errno.h>
19 #include <fcntl.h>
20 #include <stdarg.h>
21 #include <stddef.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <sys/stat.h>
25 #include <kernel-features.h>
26 #include <sysdep-cancel.h>
27 #include <not-cancel.h>
30 #ifndef OPENAT
31 # define OPENAT openat
32 # define __OPENAT_2 __openat_2
34 # ifndef __ASSUME_ATFCTS
35 /* Set errno after a failed call. If BUF is not null,
36 it is a /proc/self/fd/ path name we just tried to use. */
37 void
38 attribute_hidden
39 __atfct_seterrno (int errval, int fd, const char *buf)
41 if (buf != NULL)
43 struct stat64 st;
45 if (errval == ENOTDIR || errval == ENOENT)
47 /* This can mean either the file descriptor is invalid or
48 /proc is not mounted. */
49 if (__fxstat64 (_STAT_VER, fd, &st) != 0)
50 /* errno is already set correctly. */
51 return;
53 /* If /proc is not mounted there is nothing we can do. */
54 if ((errval != ENOTDIR || S_ISDIR (st.st_mode))
55 && (__xstat64 (_STAT_VER, "/proc/self/fd", &st) != 0
56 || !S_ISDIR (st.st_mode)))
57 errval = ENOSYS;
61 __set_errno (errval);
64 int __have_atfcts;
65 # endif
66 #endif
69 #define OPENAT_NOT_CANCEL CONCAT (OPENAT)
70 #define CONCAT(name) CONCAT2 (name)
71 #define CONCAT2(name) __##name##_nocancel
74 int
75 OPENAT_NOT_CANCEL (fd, file, oflag, mode)
76 int fd;
77 const char *file;
78 int oflag;
79 mode_t mode;
82 /* We have to add the O_LARGEFILE flag for openat64. */
83 #ifdef MORE_OFLAGS
84 oflag |= MORE_OFLAGS;
85 #endif
87 int res;
89 #ifdef __NR_openat
90 # ifndef __ASSUME_ATFCTS
91 if (__have_atfcts >= 0)
92 # endif
94 res = INLINE_SYSCALL (openat, 4, fd, file, oflag, mode);
96 # ifndef __ASSUME_ATFCTS
97 if (res == -1 && errno == ENOSYS)
98 __have_atfcts = -1;
99 else
100 # endif
101 return res;
103 #endif
105 #ifndef __ASSUME_ATFCTS
106 INTERNAL_SYSCALL_DECL (err);
107 char *buf = NULL;
109 if (fd != AT_FDCWD && file[0] != '/')
111 size_t filelen = strlen (file);
112 if (__builtin_expect (filelen == 0, 0))
114 __set_errno (ENOENT);
115 return -1;
118 static const char procfd[] = "/proc/self/fd/%d/%s";
119 /* Buffer for the path name we are going to use. It consists of
120 - the string /proc/self/fd/
121 - the file descriptor number
122 - the file name provided.
123 The final NUL is included in the sizeof. A bit of overhead
124 due to the format elements compensates for possible negative
125 numbers. */
126 size_t buflen = sizeof (procfd) + sizeof (int) * 3 + filelen;
127 buf = alloca (buflen);
129 /* Note: snprintf cannot be canceled. */
130 __snprintf (buf, buflen, procfd, fd, file);
131 file = buf;
134 res = INTERNAL_SYSCALL (open, err, 3, file, oflag, mode);
136 if (__builtin_expect (INTERNAL_SYSCALL_ERROR_P (res, err), 0))
138 __atfct_seterrno (INTERNAL_SYSCALL_ERRNO (res, err), fd, buf);
139 res = -1;
142 return res;
143 #endif
146 #define UNDERIZE(name) UNDERIZE_1 (name)
147 #define UNDERIZE_1(name) __##name
148 #define __OPENAT UNDERIZE (OPENAT)
151 /* Open FILE with access OFLAG. Interpret relative paths relative to
152 the directory associated with FD. If OFLAG includes O_CREAT, a
153 third argument is the file protection. */
155 __OPENAT (fd, file, oflag)
156 int fd;
157 const char *file;
158 int oflag;
160 mode_t mode = 0;
161 if (oflag & O_CREAT)
163 va_list arg;
164 va_start (arg, oflag);
165 mode = va_arg (arg, mode_t);
166 va_end (arg);
169 if (SINGLE_THREAD_P)
170 return OPENAT_NOT_CANCEL (fd, file, oflag, mode);
172 int oldtype = LIBC_CANCEL_ASYNC ();
174 int res = OPENAT_NOT_CANCEL (fd, file, oflag, mode);
176 LIBC_CANCEL_RESET (oldtype);
178 return res;
180 libc_hidden_def (__OPENAT)
181 weak_alias (__OPENAT, OPENAT)
185 __OPENAT_2 (fd, file, oflag)
186 int fd;
187 const char *file;
188 int oflag;
190 if (oflag & O_CREAT)
191 #define MSG(s) MSG2 (s)
192 #define MSG2(s) "invalid " #s " call: O_CREAT without mode"
193 __fortify_fail (MSG (OPENAT));
195 return __OPENAT (fd, file, oflag);