1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
//! This module contains FFI-bindings to the journald APi. See
//! sd-journal(3) for detailed information about the API.
//!
//! Only calls required by journaldriver are implemented.
/// This type represents an opaque pointer to an `sd_journal` struct.
/// It should be changed to an `extern type` once RF1861 is
/// stabilized.
enum SdJournal {}
use failure::Error;
use std::mem;
extern {
fn sd_journal_open(sd_journal: *mut SdJournal, flags: usize) -> usize;
fn sd_journal_close(sd_journal: *mut SdJournal);
fn sd_journal_next(sd_journal: *mut SdJournal) -> usize;
}
// Safe interface:
/// This struct contains the opaque data used by libsystemd to
/// reference the journal.
pub struct Journal {
sd_journal: *mut SdJournal,
}
impl Drop for Journal {
fn drop(&mut self) {
unsafe {
sd_journal_close(self.sd_journal);
}
}
}
/// Open the journal for reading. No flags are supplied to libsystemd,
/// which means that all journal entries will become available.
pub fn open_journal() -> Result<Journal, Error> {
let (mut sd_journal, result) = unsafe {
let mut journal: SdJournal = mem::uninitialized();
let result = sd_journal_open(&mut journal, 0);
(journal, result)
};
ensure!(result == 0, "Could not open journal (errno: {})", result);
Ok(Journal { sd_journal: &mut sd_journal })
}
#[derive(Debug)]
pub enum NextEntry {
/// If no new entries are available in the journal this variant is
/// returned.
NoEntry,
Entry,
}
impl Journal {
pub fn read_next(&self) -> Result<NextEntry, Error> {
let result = unsafe {
sd_journal_next(self.sd_journal)
};
match result {
0 => Ok(NextEntry::NoEntry),
1 => Ok(NextEntry::Entry),
n if n > 1 => bail!("Journal unexpectedly advanced by {} entries!", n),
_ => bail!("An error occured while advancing the journal (errno: {})", result),
}
}
}
|