-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[libc] Implement faccessat #161065
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
[libc] Implement faccessat #161065
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
5522dcc
Implement faccessat
mleleszi 5466675
Formatting
mleleszi db56a8d
Merge branch 'llvm:main' into libc-faccessat
mleleszi 30febb7
Remove redundant param from faccessat syscall
mleleszi eecd8f3
Fix AT_EACCESS definition
mleleszi bce623e
Cleanup
mleleszi 7eb506d
Minor formatting, dependency fixes
mleleszi b59cc47
Clean up tests, add faccessat2 to syscall defs
mleleszi 0ef2bd7
Clean up tests, add faccessat2 to syscall defs
mleleszi f06645a
Merge branch 'main' into libc-faccessat
mleleszi 35fd9ca
Clean up dependencies
mleleszi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
//===-- Implementation header for faccessat ---------------------*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_LIBC_SRC_UNISTD_FACCESSAT_H | ||
#define LLVM_LIBC_SRC_UNISTD_FACCESSAT_H | ||
|
||
#include "src/__support/macros/config.h" | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
|
||
int faccessat(int fd, const char *path, int amode, int flag); | ||
|
||
} // namespace LIBC_NAMESPACE_DECL | ||
|
||
#endif // LLVM_LIBC_SRC_UNISTD_FACCESSAT_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
//===-- Linux implementation of faccessat ---------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "src/unistd/faccessat.h" | ||
|
||
#include "src/__support/OSUtil/syscall.h" // For internal syscall function. | ||
#include "src/__support/common.h" | ||
|
||
#include "hdr/fcntl_macros.h" | ||
#include "src/__support/libc_errno.h" | ||
#include "src/__support/macros/config.h" | ||
#include <sys/syscall.h> // For syscall numbers. | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
|
||
LLVM_LIBC_FUNCTION(int, faccessat, | ||
(int fd, const char *path, int amode, int flag)) { | ||
#ifdef SYS_faccessat2 | ||
int ret = | ||
LIBC_NAMESPACE::syscall_impl<int>(SYS_faccessat2, fd, path, amode, flag); | ||
#else | ||
#error "faccessat2 syscall is not available." | ||
#endif | ||
|
||
if (ret < 0) { | ||
libc_errno = -ret; | ||
return -1; | ||
} | ||
return 0; | ||
} | ||
|
||
} // namespace LIBC_NAMESPACE_DECL |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
//===-- Unittests for faccessat -------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "src/fcntl/open.h" | ||
#include "src/unistd/close.h" | ||
#include "src/unistd/faccessat.h" | ||
#include "src/unistd/unlink.h" | ||
#include "test/UnitTest/ErrnoCheckingTest.h" | ||
#include "test/UnitTest/ErrnoSetterMatcher.h" | ||
#include "test/UnitTest/Test.h" | ||
|
||
#include <fcntl.h> | ||
#include <sys/stat.h> | ||
#include <unistd.h> | ||
|
||
namespace { | ||
|
||
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails; | ||
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds; | ||
|
||
using LlvmLibcFaccessatTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest; | ||
|
||
TEST_F(LlvmLibcFaccessatTest, WithAtFdcwd) { | ||
// Test access checks on a file with AT_FDCWD and no flags, equivalent to | ||
// access(). | ||
constexpr const char *FILENAME = "faccessat_basic.test"; | ||
auto TEST_FILE = libc_make_test_file_path(FILENAME); | ||
|
||
// Check permissions on a file with full permissions | ||
int fd = LIBC_NAMESPACE::open(TEST_FILE, O_WRONLY | O_CREAT, S_IRWXU); | ||
ASSERT_ERRNO_SUCCESS(); | ||
ASSERT_GT(fd, 0); | ||
ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0)); | ||
|
||
ASSERT_THAT(LIBC_NAMESPACE::faccessat(AT_FDCWD, TEST_FILE, F_OK, 0), | ||
Succeeds(0)); | ||
ASSERT_THAT( | ||
LIBC_NAMESPACE::faccessat(AT_FDCWD, TEST_FILE, X_OK | W_OK | R_OK, 0), | ||
Succeeds(0)); | ||
ASSERT_THAT(LIBC_NAMESPACE::unlink(TEST_FILE), Succeeds(0)); | ||
|
||
// Check permissions on a file with execute-only permission | ||
fd = LIBC_NAMESPACE::open(TEST_FILE, O_WRONLY | O_CREAT, S_IXUSR); | ||
ASSERT_ERRNO_SUCCESS(); | ||
ASSERT_GT(fd, 0); | ||
ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0)); | ||
|
||
ASSERT_THAT(LIBC_NAMESPACE::faccessat(AT_FDCWD, TEST_FILE, F_OK, 0), | ||
Succeeds(0)); | ||
ASSERT_THAT(LIBC_NAMESPACE::faccessat(AT_FDCWD, TEST_FILE, X_OK, 0), | ||
Succeeds(0)); | ||
ASSERT_THAT(LIBC_NAMESPACE::faccessat(AT_FDCWD, TEST_FILE, R_OK, 0), | ||
Fails(EACCES)); | ||
ASSERT_THAT(LIBC_NAMESPACE::faccessat(AT_FDCWD, TEST_FILE, W_OK, 0), | ||
Fails(EACCES)); | ||
ASSERT_THAT(LIBC_NAMESPACE::unlink(TEST_FILE), Succeeds(0)); | ||
} | ||
|
||
TEST_F(LlvmLibcFaccessatTest, NonExistentFile) { | ||
ASSERT_THAT(LIBC_NAMESPACE::faccessat(AT_FDCWD, "faccessat_nonexistent.test", | ||
F_OK, 0), | ||
Fails(ENOENT)); | ||
} | ||
|
||
TEST_F(LlvmLibcFaccessatTest, AtEaccess) { | ||
// With AT_EACCESS, faccessat checks permissions using the effective user ID, | ||
// but the effective and real user ID will be the same here and changing that | ||
// is not feasible in a test, so this is just a basic sanity check. | ||
constexpr const char *FILENAME = "faccessat_eaccess.test"; | ||
auto TEST_FILE = libc_make_test_file_path(FILENAME); | ||
|
||
int fd = LIBC_NAMESPACE::open(TEST_FILE, O_WRONLY | O_CREAT, S_IRWXU); | ||
ASSERT_ERRNO_SUCCESS(); | ||
ASSERT_GT(fd, 0); | ||
ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0)); | ||
|
||
ASSERT_THAT(LIBC_NAMESPACE::faccessat(AT_FDCWD, TEST_FILE, X_OK | W_OK | R_OK, | ||
AT_EACCESS), | ||
Succeeds(0)); | ||
|
||
ASSERT_THAT(LIBC_NAMESPACE::unlink(TEST_FILE), Succeeds(0)); | ||
} | ||
|
||
TEST_F(LlvmLibcFaccessatTest, AtEmptyPath) { | ||
constexpr const char *FILENAME = "faccessat_atemptypath.test"; | ||
auto TEST_FILE = libc_make_test_file_path(FILENAME); | ||
|
||
int fd = LIBC_NAMESPACE::open(TEST_FILE, O_WRONLY | O_CREAT, S_IRWXU); | ||
ASSERT_ERRNO_SUCCESS(); | ||
ASSERT_GT(fd, 0); | ||
|
||
// Check permissions on the file referred to by fd | ||
ASSERT_THAT(LIBC_NAMESPACE::faccessat(fd, "", F_OK, AT_EMPTY_PATH), | ||
Succeeds(0)); | ||
ASSERT_THAT( | ||
LIBC_NAMESPACE::faccessat(fd, "", X_OK | W_OK | R_OK, AT_EMPTY_PATH), | ||
Succeeds(0)); | ||
|
||
ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0)); | ||
ASSERT_THAT(LIBC_NAMESPACE::unlink(TEST_FILE), Succeeds(0)); | ||
|
||
// Check permissions on the current working directory | ||
ASSERT_THAT(LIBC_NAMESPACE::faccessat(AT_FDCWD, "", F_OK, AT_EMPTY_PATH), | ||
Succeeds(0)); | ||
ASSERT_THAT(LIBC_NAMESPACE::faccessat(AT_FDCWD, "", X_OK | W_OK | R_OK, | ||
AT_EMPTY_PATH), | ||
Succeeds(0)); | ||
} | ||
|
||
} // namespace |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.