Skip to content

Commit 3de71b3

Browse files
mleleszimahesh-attarde
authored andcommitted
[libc] Implement faccessat (llvm#161065)
llvm#160404 - Implement POSIX function "faccessat" - Remove redundant param in facessat syscall in access implementation, faccessat syscall does not take a flags arg
1 parent 60d5a57 commit 3de71b3

File tree

12 files changed

+228
-1
lines changed

12 files changed

+228
-1
lines changed

libc/config/linux/aarch64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,7 @@ set(TARGET_LIBC_ENTRYPOINTS
325325
libc.src.unistd.dup2
326326
libc.src.unistd.dup3
327327
libc.src.unistd.execve
328+
libc.src.unistd.faccessat
328329
libc.src.unistd.fchdir
329330
libc.src.unistd.fpathconf
330331
libc.src.unistd.fsync

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,7 @@ set(TARGET_LIBC_ENTRYPOINTS
331331
libc.src.unistd.dup2
332332
libc.src.unistd.dup3
333333
libc.src.unistd.execve
334+
libc.src.unistd.faccessat
334335
libc.src.unistd.fchdir
335336
libc.src.unistd.fpathconf
336337
libc.src.unistd.fsync

libc/include/llvm-libc-macros/linux/fcntl-macros.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@
6161
// Allow empty relative pathname.
6262
#define AT_EMPTY_PATH 0x1000
6363

64+
// Perform access checks using the effective user and group IDs.
65+
#define AT_EACCESS 0x200
66+
6467
// Values of SYS_fcntl commands.
6568
#define F_DUPFD 0
6669
#define F_GETFD 1

libc/include/sys/syscall.h.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,10 @@
309309
#define SYS_faccessat __NR_faccessat
310310
#endif
311311

312+
#ifdef __NR_faccessat2
313+
#define SYS_faccessat2 __NR_faccessat2
314+
#endif
315+
312316
#ifdef __NR_fadvise64
313317
#define SYS_fadvise64 __NR_fadvise64
314318
#endif

libc/include/unistd.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,15 @@ functions:
9696
- type: const char *
9797
- type: __exec_argv_t
9898
- type: __exec_envp_t
99+
- name: faccessat
100+
standards:
101+
- POSIX
102+
return_type: int
103+
arguments:
104+
- type: int
105+
- type: const char *
106+
- type: int
107+
- type: int
99108
- name: fchdir
100109
standards:
101110
- POSIX

libc/src/unistd/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@ add_entrypoint_object(
5555
.${LIBC_TARGET_OS}.dup3
5656
)
5757

58+
add_entrypoint_object(
59+
faccessat
60+
ALIAS
61+
DEPENDS
62+
.${LIBC_TARGET_OS}.faccessat
63+
)
64+
5865
add_entrypoint_object(
5966
fchdir
6067
ALIAS

libc/src/unistd/faccessat.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Implementation header for faccessat ---------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_UNISTD_FACCESSAT_H
10+
#define LLVM_LIBC_SRC_UNISTD_FACCESSAT_H
11+
12+
#include "src/__support/macros/config.h"
13+
14+
namespace LIBC_NAMESPACE_DECL {
15+
16+
int faccessat(int fd, const char *path, int amode, int flag);
17+
18+
} // namespace LIBC_NAMESPACE_DECL
19+
20+
#endif // LLVM_LIBC_SRC_UNISTD_FACCESSAT_H

libc/src/unistd/linux/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,19 @@ add_entrypoint_object(
8080
libc.src.errno.errno
8181
)
8282

83+
add_entrypoint_object(
84+
faccessat
85+
SRCS
86+
faccessat.cpp
87+
HDRS
88+
../faccessat.h
89+
DEPENDS
90+
libc.hdr.fcntl_macros
91+
libc.include.sys_syscall
92+
libc.src.__support.OSUtil.osutil
93+
libc.src.errno.errno
94+
)
95+
8396
add_entrypoint_object(
8497
fchdir
8598
SRCS

libc/src/unistd/linux/access.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ LLVM_LIBC_FUNCTION(int, access, (const char *path, int mode)) {
2323
int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_access, path, mode);
2424
#elif defined(SYS_faccessat)
2525
int ret =
26-
LIBC_NAMESPACE::syscall_impl<int>(SYS_faccessat, AT_FDCWD, path, mode, 0);
26+
LIBC_NAMESPACE::syscall_impl<int>(SYS_faccessat, AT_FDCWD, path, mode);
2727
#else
2828
#error "access and faccessat syscalls not available."
2929
#endif
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//===-- Linux implementation of faccessat ---------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/unistd/faccessat.h"
10+
11+
#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
12+
#include "src/__support/common.h"
13+
14+
#include "hdr/fcntl_macros.h"
15+
#include "src/__support/libc_errno.h"
16+
#include "src/__support/macros/config.h"
17+
#include <sys/syscall.h> // For syscall numbers.
18+
19+
namespace LIBC_NAMESPACE_DECL {
20+
21+
LLVM_LIBC_FUNCTION(int, faccessat,
22+
(int fd, const char *path, int amode, int flag)) {
23+
#ifdef SYS_faccessat2
24+
int ret =
25+
LIBC_NAMESPACE::syscall_impl<int>(SYS_faccessat2, fd, path, amode, flag);
26+
#else
27+
#error "faccessat2 syscall is not available."
28+
#endif
29+
30+
if (ret < 0) {
31+
libc_errno = -ret;
32+
return -1;
33+
}
34+
return 0;
35+
}
36+
37+
} // namespace LIBC_NAMESPACE_DECL

0 commit comments

Comments
 (0)