Skip to content

Commit 8081817

Browse files
bors[bot]c-nixon
andauthored
Merge #138
138: Implement AsRawFd for JournalRef r=jmesmon a=c-nixon Implement AsRawFd for JournalRef as it is for BusRef. The immediate use case is to allow asynchronous polling for events Co-authored-by: Chris Nixon <[email protected]>
2 parents 7225e6e + 105ebc1 commit 8081817

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/journal.rs

+22
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use std::ffi::CString;
1414
use std::io::ErrorKind::InvalidData;
1515
use std::mem::MaybeUninit;
1616
use std::os::raw::c_void;
17+
use std::os::unix::io::AsRawFd;
1718
use std::u64;
1819
use std::{fmt, io, ptr, result, slice, time};
1920

@@ -384,6 +385,20 @@ impl Journal {
384385
}
385386

386387
impl JournalRef {
388+
/// Returns a file descriptor a file descriptor that may be
389+
/// asynchronously polled in an external event loop and is signaled as
390+
/// soon as the journal changes, because new entries or files were added,
391+
/// rotation took place, or files have been deleted, and similar. The
392+
/// file descriptor is suitable for usage in poll(2).
393+
///
394+
/// This corresponds to [`sd_journal_get_fd`]
395+
///
396+
/// [`sd_journal_get_fd`]: https://www.freedesktop.org/software/systemd/man/sd_journal_get_fd.html
397+
#[inline]
398+
pub fn fd(&self) -> Result<c_int> {
399+
Ok(sd_try!(ffi::sd_journal_get_fd(self.as_ptr())))
400+
}
401+
387402
/// Fields that are longer that this number of bytes _may_ be truncated when retrieved by this [`Journal`]
388403
/// instance.
389404
///
@@ -760,3 +775,10 @@ impl JournalRef {
760775
Ok(self)
761776
}
762777
}
778+
779+
impl AsRawFd for JournalRef {
780+
#[inline]
781+
fn as_raw_fd(&self) -> c_int {
782+
self.fd().unwrap()
783+
}
784+
}

0 commit comments

Comments
 (0)