This patch initiates the refactoring of Linux syscalls as described in the RFC (https://discourse.llvm.org/t/rfc-linux-syscall-cleanup/87248/). It introduces a new infrastructure in `src/__support/OSUtil/linux/syscall_wrappers/` to house header-only syscall wrappers. These wrappers utilize `ErrorOr` to provide a consistent, type-safe interface for error handling across the library, standardizing how syscall return values are converted into errno-compatible Error objects. Summary of changes: - Created the `syscall_wrappers` directory and added `close.h`, `read.h`, `write.h`, and `open.h`. - Moved the existing `getrandom.h` into the new `syscall_wrappers` directory and updated its callers (including HashTable/randomness.h). - Refactored core entrypoints (`close`, `read`, `write`, `open`) to use the new wrappers, removing direct `syscall_impl` logic and manual errno setting. - Updated `shm_open.cpp` to use the new `open` wrapper. - Cleaned up `OSUtil/linux/fcntl.cpp` by removing redundant internal implementations of `open` and `close`. - Added a developer guide in `docs/dev/syscall_wrapper_refactor.rst` outlining the established pattern for future migrations. --------- Co-authored-by: Michael Jones <michaelrj@google.com>
30 lines
984 B
C++
30 lines
984 B
C++
//===-- Linux implementation of getrandom ---------------------------------===//
|
|
//
|
|
// 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/sys/random/getrandom.h"
|
|
|
|
#include "src/__support/OSUtil/linux/syscall_wrappers/getrandom.h"
|
|
#include "src/__support/common.h"
|
|
#include "src/__support/error_or.h"
|
|
#include "src/__support/libc_errno.h"
|
|
#include "src/__support/macros/config.h"
|
|
|
|
namespace LIBC_NAMESPACE_DECL {
|
|
|
|
LLVM_LIBC_FUNCTION(ssize_t, getrandom,
|
|
(void *buf, size_t buflen, unsigned int flags)) {
|
|
auto rand = linux_syscalls::getrandom(buf, buflen, flags);
|
|
if (!rand.has_value()) {
|
|
libc_errno = static_cast<int>(rand.error());
|
|
return -1;
|
|
}
|
|
return rand.value();
|
|
}
|
|
|
|
} // namespace LIBC_NAMESPACE_DECL
|