systemd/
lib.rs

1#![cfg_attr(feature = "unstable-doc-cfg", feature(doc_cfg))]
2#![warn(rust_2018_idioms)]
3
4#[cfg(all(feature = "journal", doctest))]
5doc_comment::doctest!("../README.md", readme);
6
7extern crate libsystemd_sys as ffi;
8
9/*
10extern crate enumflags2;
11#[macro_use]
12extern crate enumflags2_derive;
13*/
14
15#[cfg(feature = "journal")]
16#[allow(deprecated)]
17pub use journal::JournalFiles;
18#[cfg(feature = "journal")]
19pub use journal::{Journal, JournalLog, JournalRecord, JournalSeek, JournalWaitResult};
20use libc::{c_char, c_void, free, strlen};
21pub use std::io::{Error, Result};
22
23#[cfg(any(feature = "journal", feature = "bus"))]
24fn usec_from_duration(duration: std::time::Duration) -> u64 {
25    let sub_usecs = duration.subsec_micros() as u64;
26    duration.as_secs() * 1_000_000 + sub_usecs
27}
28
29/// Convert a systemd ffi return value into a Result
30pub fn ffi_result(ret: ffi::c_int) -> Result<ffi::c_int> {
31    if ret < 0 {
32        Err(Error::from_raw_os_error(-ret))
33    } else {
34        Ok(ret)
35    }
36}
37
38/// Convert a malloc'd C string into a rust string and call free on it.
39/// Returns None if the pointer is null.
40unsafe fn free_cstring(ptr: *mut c_char) -> Option<String> {
41    if ptr.is_null() {
42        return None;
43    }
44    let len = strlen(ptr);
45    let char_slice = std::slice::from_raw_parts(ptr as *mut u8, len);
46    let s = String::from_utf8_lossy(char_slice).into_owned();
47    free(ptr as *mut c_void);
48    Some(s)
49}
50
51/// High-level interface to the systemd journal.
52///
53/// The main interface for writing to the journal is `fn log()`, and the main
54/// interface for reading the journal is `struct Journal`.
55#[cfg(feature = "journal")]
56pub mod journal;
57
58/// Similar to `log!()`, except it accepts a func argument rather than hard
59/// coding `::log::log()`, and it doesn't filter on `log_enabled!()`.
60#[macro_export]
61macro_rules! log_with{
62    ($func:expr, $lvl:expr, $($arg:tt),+) => ({
63        $func(&::log::Record::builder()
64            .args(format_args!($($arg),+))
65            .level($lvl)
66            .file(Some(file!()))
67            .line(Some(line!()))
68            .module_path(Some(module_path!()))
69            .build())
70    });
71    (@raw $func:expr, $lvl:expr, $($arg:tt),+) => ({
72        $func($lvl, file!(), line!(), module_path!(), &format_args!($($arg),+))
73    });
74    (@target $tgt:expr, $func:expr, $lvl:expr, $($arg:tt),+) => ({
75        $func(&::log::Record::builder()
76            .args(format_args!($($arg),+))
77            .level($lvl)
78            .target($tgt)
79            .file(Some(file!()))
80            .line(Some(line!()))
81            .module_path(Some(module_path!()))
82            .build())
83    })
84}
85
86#[cfg(feature = "journal")]
87#[macro_export]
88macro_rules! sd_journal_log{
89    ($lvl:expr, $($arg:tt)+) => ($crate::log_with!(@raw ::systemd::journal::log, $lvl, $($arg)+))
90}
91
92pub mod daemon;
93
94pub mod id128;
95
96/// Interface to introspect on seats, sessions and users.
97pub mod login;
98
99/// An interface to work with the dbus message bus.
100///
101#[cfg(feature = "bus")]
102pub mod bus;
103
104/// Utilities for working with systemd units.
105pub mod unit;