Skip to content
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

feat(monitor/kernel): allow monitoring kernel events #50

Merged
merged 1 commit into from
Jul 29, 2024
Merged
Changes from all 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
28 changes: 26 additions & 2 deletions src/monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,42 @@ impl Drop for Builder {

as_ffi_with_context!(Builder, monitor, ffi::udev_monitor, ffi::udev_monitor_ref);

/// The event source to monitor.
pub(crate) enum EventSource {
Udev,
Kernel,
}

impl Builder {
/// Creates a new `Monitor`.
/// Creates a new udev event `Monitor`.
pub fn new() -> Result<Self> {
// Create a new Udev context for this monitor
// It would be more efficient to allow callers to create just one context and use multiple
// monitors, however that would be an API-breaking change.
Self::with_udev(Udev::new()?)
}

/// Creates a new kernel event `Monitor`.
pub fn new_kernel() -> Result<Self> {
Self::with_kernel(Udev::new()?)
}

/// Creates a new `Monitor` using an existing `Udev` instance
pub(crate) fn with_udev(udev: Udev) -> Result<Self> {
let name = b"udev\0".as_ptr() as *const libc::c_char;
Self::with_source(udev, EventSource::Udev)
}

/// Creates a new kernel event `Monitor` using an existing `Udev` instance.
pub(crate) fn with_kernel(udev: Udev) -> Result<Self> {
Self::with_source(udev, EventSource::Kernel)
}

/// Creates a `Monitor` for the given source, using an existing `Udev` instance.
pub(crate) fn with_source(udev: Udev, source: EventSource) -> Result<Self> {
let name = match source {
EventSource::Udev => b"udev\0".as_ptr() as *const libc::c_char,
EventSource::Kernel => b"kernel\0".as_ptr() as *const libc::c_char,
};

let ptr = try_alloc!(unsafe { ffi::udev_monitor_new_from_netlink(udev.as_raw(), name) });

Expand Down
Loading