Add std::expected syscall wrappers and adopt them for fd creation - #25
Merged
Conversation
Introduce src/utils/sys.h: thin std::expected<UniqueFd, std::error_code> wrappers over the fd-creating syscalls (open, eventfd, signalfd, timerfd). A fallible call now hands back a [[nodiscard]], allocation-free error channel (errno mapped to std::generic_category()) with the fd already owned, instead of a bare -1 the caller must remember to check. This is the errno layer only, and it establishes the error-model convention for the project: syscalls -> std::expected<T, std::error_code>; D-Bus failures -> sdbus::Error exceptions (sdbus-c++'s model); absent-but-valid values -> std::optional. Each is the right tool for its layer. Adopted at the fd-creation sites: EventLoop's wake eventfd, SignalSource's signalfd, and the three controllers' hidraw open() (which now also logs the specific errno message). An opt-in unit test (sys_test) covers success, the ENOENT error path, and the eventfd/timerfd wrappers. Further adoption (e.g. the hidraw ioctl chain) can follow the same shape; this change lands the foundation and the reference call sites. Signed-off-by: Joel Winarske <[email protected]>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Add std::expected syscall wrappers and adopt them for fd creation
Introduce src/utils/sys.h: thin std::expected<UniqueFd, std::error_code>
wrappers over the fd-creating syscalls (open, eventfd, signalfd, timerfd). A
fallible call now hands back a [[nodiscard]], allocation-free error channel
(errno mapped to std::generic_category()) with the fd already owned, instead of a bare -1 the caller must remember to check.
This is the errno layer only, and it establishes the error-model convention for
the project: syscalls -> std::expected<T, std::error_code>; D-Bus failures ->
sdbus::Error exceptions (sdbus-c++'s model); absent-but-valid values ->
std::optional. Each is the right tool for its layer.
Adopted at the fd-creation sites: EventLoop's wake eventfd, SignalSource's
signalfd, and the three controllers' hidraw open() (which now also logs the
specific errno message). An opt-in unit test (sys_test) covers success, the
ENOENT error path, and the eventfd/timerfd wrappers.
Further adoption (e.g. the hidraw ioctl chain) can follow the same shape; this
change lands the foundation and the reference call sites.