Update copyright notices with scripts/update-copyrights
[glibc.git] / sysdeps / unix / sysv / linux / openat.c
blob9bb8acec1fa6cc41aff6ebadbb8ce4ebaece5588
1 /* Copyright (C) 2005-2014 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
33 # ifndef __ASSUME_ATFCTS
34 /* Set errno after a failed call. If BUF is not null,
35 it is a /proc/self/fd/ path name we just tried to use. */
36 void
37 attribute_hidden
38 __atfct_seterrno (int errval, int fd, const char *buf)
40 if (buf != NULL)
42 struct stat64 st;
44 if (errval == ENOTDIR || errval == ENOENT)
46 /* This can mean either the file descriptor is invalid or
47 /proc is not mounted. */
48 if (__fxstat64 (_STAT_VER, fd, &st) != 0)
49 /* errno is already set correctly. */
50 return;
52 /* If /proc is not mounted there is nothing we can do. */
53 if ((errval != ENOTDIR || S_ISDIR (st.st_mode))
54 && (__xstat64 (_STAT_VER, "/proc/self/fd", &st) != 0
55 || !S_ISDIR (st.st_mode)))
56 errval = ENOSYS;
60 __set_errno (errval);
63 int __have_atfcts;
64 # endif
65 #endif
68 #define OPENAT_NOT_CANCEL CONCAT (OPENAT)
69 #define CONCAT(name) CONCAT2 (name)
70 #define CONCAT2(name) __##name##_nocancel
73 int
74 OPENAT_NOT_CANCEL (fd, file, oflag, mode)
75 int fd;
76 const char *file;
77 int oflag;
78 mode_t mode;
81 /* We have to add the O_LARGEFILE flag for openat64. */
82 #ifdef MORE_OFLAGS
83 oflag |= MORE_OFLAGS;
84 #endif
86 int res;
88 #ifdef __NR_openat
89 # ifndef __ASSUME_ATFCTS
90 if (__have_atfcts >= 0)
91 # endif
93 res = INLINE_SYSCALL (openat, 4, fd, file, oflag, mode);
95 # ifndef __ASSUME_ATFCTS
96 if (res == -1 && errno == ENOSYS)
97 __have_atfcts = -1;
98 else
99 # endif
100 return res;
102 #endif
104 #ifndef __ASSUME_ATFCTS
105 INTERNAL_SYSCALL_DECL (err);
106 char *buf = NULL;
108 if (fd != AT_FDCWD && file[0] != '/')
110 size_t filelen = strlen (file);
111 if (__builtin_expect (filelen == 0, 0))
113 __set_errno (ENOENT);
114 return -1;
117 static const char procfd[] = "/proc/self/fd/%d/%s";
118 /* Buffer for the path name we are going to use. It consists of
119 - the string /proc/self/fd/
120 - the file descriptor number
121 - the file name provided.
122 The final NUL is included in the sizeof. A bit of overhead
123 due to the format elements compensates for possible negative
124 numbers. */
125 size_t buflen = sizeof (procfd) + sizeof (int) * 3 + filelen;
126 buf = alloca (buflen);
128 /* Note: snprintf cannot be canceled. */
129 __snprintf (buf, buflen, procfd, fd, file);
130 file = buf;
133 res = INTERNAL_SYSCALL (open, err, 3, file, oflag, mode);
135 if (__builtin_expect (INTERNAL_SYSCALL_ERROR_P (res, err), 0))
137 __atfct_seterrno (INTERNAL_SYSCALL_ERRNO (res, err), fd, buf);
138 res = -1;
141 return res;
142 #endif
145 #define UNDERIZE(name) UNDERIZE_1 (name)
146 #define UNDERIZE_1(name) __##name
147 #define __OPENAT UNDERIZE (OPENAT)
150 /* Open FILE with access OFLAG. Interpret relative paths relative to
151 the directory associated with FD. If OFLAG includes O_CREAT, a
152 third argument is the file protection. */
154 __OPENAT (fd, file, oflag)
155 int fd;
156 const char *file;
157 int oflag;
159 mode_t mode = 0;
160 if (oflag & O_CREAT)
162 va_list arg;
163 va_start (arg, oflag);
164 mode = va_arg (arg, mode_t);
165 va_end (arg);
168 if (SINGLE_THREAD_P)
169 return OPENAT_NOT_CANCEL (fd, file, oflag, mode);
171 int oldtype = LIBC_CANCEL_ASYNC ();
173 int res = OPENAT_NOT_CANCEL (fd, file, oflag, mode);
175 LIBC_CANCEL_RESET (oldtype);
177 return res;
179 libc_hidden_def (__OPENAT)
180 weak_alias (__OPENAT, OPENAT)