Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions libc/config/linux/aarch64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.unistd.dup2
libc.src.unistd.dup3
libc.src.unistd.execve
libc.src.unistd.faccessat
libc.src.unistd.fchdir
libc.src.unistd.fpathconf
libc.src.unistd.fsync
Expand Down
1 change: 1 addition & 0 deletions libc/config/linux/x86_64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.unistd.dup2
libc.src.unistd.dup3
libc.src.unistd.execve
libc.src.unistd.faccessat
libc.src.unistd.fchdir
libc.src.unistd.fpathconf
libc.src.unistd.fsync
Expand Down
3 changes: 3 additions & 0 deletions libc/include/llvm-libc-macros/linux/fcntl-macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@
// Allow empty relative pathname.
#define AT_EMPTY_PATH 0x1000

// Perform access checks using the effective user and group IDs.
#define AT_EACCESS 0x200

// Values of SYS_fcntl commands.
#define F_DUPFD 0
#define F_GETFD 1
Expand Down
9 changes: 9 additions & 0 deletions libc/include/unistd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,15 @@ functions:
- type: const char *
- type: __exec_argv_t
- type: __exec_envp_t
- name: faccessat
standards:
- POSIX
return_type: int
arguments:
- type: int
- type: const char *
- type: int
- type: int
- name: fchdir
standards:
- POSIX
Expand Down
7 changes: 7 additions & 0 deletions libc/src/unistd/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ add_entrypoint_object(
.${LIBC_TARGET_OS}.dup3
)

add_entrypoint_object(
faccessat
ALIAS
DEPENDS
.${LIBC_TARGET_OS}.faccessat
)

add_entrypoint_object(
fchdir
ALIAS
Expand Down
21 changes: 21 additions & 0 deletions libc/src/unistd/faccessat.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//===-- 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
14 changes: 14 additions & 0 deletions libc/src/unistd/linux/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,20 @@ add_entrypoint_object(
libc.src.errno.errno
)

add_entrypoint_object(
faccessat
SRCS
faccessat.cpp
HDRS
../faccessat.h
DEPENDS
libc.hdr.fcntl_macros
libc.include.unistd
libc.include.sys_syscall
libc.src.__support.OSUtil.osutil
libc.src.errno.errno
)

add_entrypoint_object(
fchdir
SRCS
Expand Down
2 changes: 1 addition & 1 deletion libc/src/unistd/linux/access.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ LLVM_LIBC_FUNCTION(int, access, (const char *path, int mode)) {
int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_access, path, mode);
#elif defined(SYS_faccessat)
int ret =
LIBC_NAMESPACE::syscall_impl<int>(SYS_faccessat, AT_FDCWD, path, mode, 0);
LIBC_NAMESPACE::syscall_impl<int>(SYS_faccessat, AT_FDCWD, path, mode);
#else
#error "access and faccessat syscalls not available."
#endif
Expand Down
38 changes: 38 additions & 0 deletions libc/src/unistd/linux/faccessat.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//===-- 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
22 changes: 22 additions & 0 deletions libc/test/src/unistd/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,28 @@ add_libc_unittest(
libc.test.UnitTest.ErrnoSetterMatcher
)

add_libc_unittest(
faccessat_test
SUITE
libc_unistd_unittests
SRCS
faccessat_test.cpp
DEPENDS
libc.include.unistd
libc.src.__support.CPP.string
libc.src.sys.stat.mkdir
libc.src.errno.errno
libc.src.fcntl.open
libc.src.unistd.faccessat
libc.src.unistd.close
libc.src.unistd.symlink
libc.src.unistd.unlink
libc.src.unistd.unlinkat
libc.src.unistd.rmdir
libc.test.UnitTest.ErrnoCheckingTest
libc.test.UnitTest.ErrnoSetterMatcher
)

add_libc_unittest(
fchdir_test
SUITE
Expand Down
172 changes: 172 additions & 0 deletions libc/test/src/unistd/faccessat_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
//===-- 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/__support/CPP/string.h"
#include "src/fcntl/open.h"
#include "src/sys/stat/mkdir.h"
#include "src/unistd/close.h"
#include "src/unistd/faccessat.h"
#include "src/unistd/rmdir.h"
#include "src/unistd/symlink.h"
#include "src/unistd/unlink.h"
#include "src/unistd/unlinkat.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 {

namespace cpp = LIBC_NAMESPACE::cpp;
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));

// Check permissions on a non existent file
ASSERT_THAT(LIBC_NAMESPACE::faccessat(AT_FDCWD, TEST_FILE, F_OK, 0),
Fails(ENOENT));
}

TEST_F(LlvmLibcFaccessatTest, RelativePathWithDirFD) {
// Check permissions on a file releative to dir_fd
const cpp::string DIRNAME = "faccessat_dir";
const cpp::string FILENAME = "relative_file.txt";

auto TEST_DIR = libc_make_test_file_path(DIRNAME.data());
ASSERT_THAT(LIBC_NAMESPACE::mkdir(TEST_DIR, S_IRWXU), Succeeds(0));

int dir_fd = LIBC_NAMESPACE::open(TEST_DIR, O_RDONLY | O_DIRECTORY);
ASSERT_ERRNO_SUCCESS();
ASSERT_GT(dir_fd, 0);

auto FULL_FILE_PATH =
libc_make_test_file_path((DIRNAME + "/" + FILENAME).data());
int fd = LIBC_NAMESPACE::open(FULL_FILE_PATH, 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(dir_fd, FILENAME.data(), R_OK | W_OK, 0),
Succeeds(0));

ASSERT_THAT(LIBC_NAMESPACE::unlinkat(dir_fd, FILENAME.data(), 0),
Succeeds(0));
ASSERT_THAT(LIBC_NAMESPACE::close(dir_fd), Succeeds(0));
ASSERT_THAT(LIBC_NAMESPACE::rmdir(TEST_DIR), Succeeds(0));
}

TEST_F(LlvmLibcFaccessatTest, SymlinkNoFollow) {
// Check permissions on a symlink itself, not what it links to
constexpr const char *TARGET = "faccessat_target";
constexpr const char *SYMLINK = "faccessat_link";
auto TEST_TARGET = libc_make_test_file_path(TARGET);
auto TEST_SYMLINK = libc_make_test_file_path(SYMLINK);

int fd = LIBC_NAMESPACE::open(TEST_TARGET, O_WRONLY | O_CREAT, 0);
ASSERT_ERRNO_SUCCESS();
ASSERT_GT(fd, 0);
ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0));

ASSERT_THAT(LIBC_NAMESPACE::symlink(TARGET, TEST_SYMLINK), Succeeds(0));

ASSERT_THAT(LIBC_NAMESPACE::faccessat(AT_FDCWD, TEST_SYMLINK, R_OK, 0),
Fails(EACCES));
ASSERT_THAT(LIBC_NAMESPACE::faccessat(AT_FDCWD, TEST_SYMLINK, F_OK,
AT_SYMLINK_NOFOLLOW),
Succeeds(0));

ASSERT_THAT(LIBC_NAMESPACE::unlink(TEST_SYMLINK), Succeeds(0));
ASSERT_THAT(LIBC_NAMESPACE::unlink(TEST_TARGET), Succeeds(0));
}

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
Loading