systemd 0.10.1

A rust interface to libsystemd/libelogind provided APIs
Documentation
#![cfg_attr(feature = "unstable-doc-cfg", feature(doc_cfg))]
#![warn(rust_2018_idioms)]

#[cfg(all(feature = "journal", doctest))]
doc_comment::doctest!("../README.md", readme);

extern crate libsystemd_sys as ffi;

/*
extern crate enumflags2;
#[macro_use]
extern crate enumflags2_derive;
*/

#[cfg(feature = "journal")]
#[allow(deprecated)]
pub use journal::JournalFiles;
#[cfg(feature = "journal")]
pub use journal::{Journal, JournalLog, JournalRecord, JournalSeek, JournalWaitResult};
use libc::{c_char, c_void, free, strlen};
pub use std::io::{Error, Result};

#[cfg(any(feature = "journal", feature = "bus"))]
fn usec_from_duration(duration: std::time::Duration) -> u64 {
    let sub_usecs = duration.subsec_micros() as u64;
    duration.as_secs() * 1_000_000 + sub_usecs
}

/// Convert a systemd ffi return value into a Result
pub fn ffi_result(ret: ffi::c_int) -> Result<ffi::c_int> {
    if ret < 0 {
        Err(Error::from_raw_os_error(-ret))
    } else {
        Ok(ret)
    }
}

/// Convert a malloc'd C string into a rust string and call free on it.
/// Returns None if the pointer is null.
unsafe fn free_cstring(ptr: *mut c_char) -> Option<String> {
    if ptr.is_null() {
        return None;
    }
    let len = strlen(ptr);
    let char_slice = std::slice::from_raw_parts(ptr as *mut u8, len);
    let s = String::from_utf8_lossy(char_slice).into_owned();
    free(ptr as *mut c_void);
    Some(s)
}

/// High-level interface to the systemd journal.
///
/// The main interface for writing to the journal is `fn log()`, and the main
/// interface for reading the journal is `struct Journal`.
#[cfg(feature = "journal")]
pub mod journal;

/// Similar to `log!()`, except it accepts a func argument rather than hard
/// coding `::log::log()`, and it doesn't filter on `log_enabled!()`.
#[macro_export]
macro_rules! log_with{
    ($func:expr, $lvl:expr, $($arg:tt),+) => ({
        $func(&::log::Record::builder()
            .args(format_args!($($arg),+))
            .level($lvl)
            .file(Some(file!()))
            .line(Some(line!()))
            .module_path(Some(module_path!()))
            .build())
    });
    (@raw $func:expr, $lvl:expr, $($arg:tt),+) => ({
        $func($lvl, file!(), line!(), module_path!(), &format_args!($($arg),+))
    });
    (@target $tgt:expr, $func:expr, $lvl:expr, $($arg:tt),+) => ({
        $func(&::log::Record::builder()
            .args(format_args!($($arg),+))
            .level($lvl)
            .target($tgt)
            .file(Some(file!()))
            .line(Some(line!()))
            .module_path(Some(module_path!()))
            .build())
    })
}

#[cfg(feature = "journal")]
#[macro_export]
macro_rules! sd_journal_log{
    ($lvl:expr, $($arg:tt)+) => ($crate::log_with!(@raw ::systemd::journal::log, $lvl, $($arg)+))
}

pub mod daemon;

pub mod id128;

/// Interface to introspect on seats, sessions and users.
pub mod login;

/// An interface to work with the dbus message bus.
///
#[cfg(feature = "bus")]
pub mod bus;

/// Utilities for working with systemd units.
pub mod unit;