1 /* Copyright (C) 2005, 2006 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, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
26 #include <kernel-features.h>
27 #include <sysdep-cancel.h>
28 #include <not-cancel.h>
32 # define OPENAT openat
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. */
39 __atfct_seterrno (int errval
, int fd
, const char *buf
)
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. */
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
)))
69 #define OPENAT_NOT_CANCEL CONCAT (OPENAT)
70 #define CONCAT(name) CONCAT2 (name)
71 #define CONCAT2(name) __##name##_nocancel
75 OPENAT_NOT_CANCEL (fd
, file
, oflag
, mode
)
82 /* We have to add the O_LARGEFILE flag for openat64. */
87 INTERNAL_SYSCALL_DECL (err
);
91 # ifndef __ASSUME_ATFCTS
92 if (__have_atfcts
>= 0)
95 res
= INLINE_SYSCALL (openat
, 4, fd
, file
, oflag
, mode
);
97 # ifndef __ASSUME_ATFCTS
98 if (res
== -1 && errno
== ENOSYS
)
106 #ifndef __ASSUME_ATFCTS
109 if (fd
!= AT_FDCWD
&& file
[0] != '/')
111 size_t filelen
= strlen (file
);
112 static const char procfd
[] = "/proc/self/fd/%d/%s";
113 /* Buffer for the path name we are going to use. It consists of
114 - the string /proc/self/fd/
115 - the file descriptor number
116 - the file name provided.
117 The final NUL is included in the sizeof. A bit of overhead
118 due to the format elements compensates for possible negative
120 size_t buflen
= sizeof (procfd
) + sizeof (int) * 3 + filelen
;
121 buf
= alloca (buflen
);
123 /* Note: snprintf cannot be canceled. */
124 __snprintf (buf
, buflen
, procfd
, fd
, file
);
128 res
= INTERNAL_SYSCALL (open
, err
, 3, file
, oflag
, mode
);
130 if (__builtin_expect (INTERNAL_SYSCALL_ERROR_P (res
, err
), 0))
132 __atfct_seterrno (INTERNAL_SYSCALL_ERRNO (res
, err
), fd
, buf
);
140 #define UNDERIZE(name) UNDERIZE_1 (name)
141 #define UNDERIZE_1(name) __##name
142 #define __OPENAT UNDERIZE (OPENAT)
145 /* Open FILE with access OFLAG. Interpret relative paths relative to
146 the directory associated with FD. If OFLAG includes O_CREAT, a
147 third argument is the file protection. */
149 __OPENAT (fd
, file
, oflag
)
158 va_start (arg
, oflag
);
159 mode
= va_arg (arg
, mode_t
);
164 return OPENAT_NOT_CANCEL (fd
, file
, oflag
, mode
);
166 int oldtype
= LIBC_CANCEL_ASYNC ();
168 int res
= OPENAT_NOT_CANCEL (fd
, file
, oflag
, mode
);
170 LIBC_CANCEL_RESET (oldtype
);
174 libc_hidden_def (__OPENAT
)
175 weak_alias (__OPENAT
, OPENAT
)