Skip to content

Add std::expected syscall wrappers and adopt them for fd creation - #25

Merged
jwinarske merged 1 commit into
mainfrom
jw/expected-syscalls
Jul 12, 2026
Merged

Add std::expected syscall wrappers and adopt them for fd creation#25
jwinarske merged 1 commit into
mainfrom
jw/expected-syscalls

Conversation

@jwinarske

@jwinarske jwinarske commented Jul 12, 2026

Copy link
Copy Markdown
Owner

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.

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]>
@jwinarske
jwinarske merged commit 477b3a1 into main Jul 12, 2026
6 checks passed
@jwinarske
jwinarske deleted the jw/expected-syscalls branch July 12, 2026 23:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant