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

dlopen #11

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
8 changes: 8 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ name: Continuous integration

jobs:
check:
strategy:
matrix:
features: [ libsystemd-sys, open ]
name: Check
runs-on: ubuntu-latest
steps:
Expand All @@ -20,8 +23,12 @@ jobs:
- uses: actions-rs/cargo@v1
with:
command: check
args: --no-default-features --features ${{ matrix.features }}

test:
strategy:
matrix:
features: [ libsystemd-sys, open ]
name: Test Suite
runs-on: ubuntu-latest
steps:
Expand All @@ -38,6 +45,7 @@ jobs:
- uses: actions-rs/cargo@v1
with:
command: test
args: --no-default-features --features ${{ matrix.features }}
timeout-minutes: 6

fmt:
Expand Down
104 changes: 96 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,20 @@ documentation = "https://docs.rs/journald"
edition = "2018"

[features]
default = []
systemd_v245 = [ "libsystemd-sys/systemd_v245" ]
default = [ "libsystemd-sys" ]
systemd_v245 = [ "libsystemd-sys", "libsystemd-sys/systemd_v245" ]
dl_namespace = [ "open" ]
open = [ "dlopen", "dlopen_derive" ]


[dependencies]
cstr-argument = "~0.0"
libc = "~0.2"
libsystemd-sys = "0.9"
log = "~0.4"
utf8-cstr = "~0.1"
libsystemd-sys = { version = "0.9", optional = true }
dlopen = { version = "0.1", optional = true }
dlopen_derive = { version = "0.1", optional = true }

[dependencies.serde]
package = "serde"
Expand Down
54 changes: 46 additions & 8 deletions src/journal_writer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use libc::c_int;
use libsystemd_sys::{c_void, const_iovec, journal as ffi, size_t};
use libc::{c_int, c_void, size_t};
#[cfg(feature = "libsystemd-sys")]
use libsystemd_sys::{const_iovec, journal as ffi};

use super::{JournalEntry, Result};
use crate::ffi_result;
Expand All @@ -13,12 +14,18 @@ pub fn submit(entry: &JournalEntry) -> Result<()> {

let fields_iovec = array_to_iovecs(&fields.iter().map(|v| v.as_str()).collect::<Vec<&str>>());

unsafe {
ffi_result(ffi::sd_journal_sendv(
fields_iovec.as_ptr(),
fields_iovec.len() as c_int,
))?
};
ffi_result(unsafe {
#[cfg(feature = "libsystemd-sys")]
{
ffi::sd_journal_sendv(fields_iovec.as_ptr(), fields_iovec.len() as c_int)
}

#[cfg(feature = "open")]
{
crate::open_systemd()?
.sd_journal_sendv(fields_iovec.as_ptr(), fields_iovec.len() as c_int)
}
})?;

Ok(())
}
Expand All @@ -31,3 +38,34 @@ pub fn array_to_iovecs(args: &[&str]) -> Vec<const_iovec> {
})
.collect()
}

// START COPY FROM LIBSYSTEMD_SYS
/// Helper type to mark functions systemd functions that promise not to modify the underying iovec
/// data. There is no corresponding type in libc, so their function signatures take *const iovec,
/// which technically allow iov_base to be modified. However, const_iovec provides the same ABI, so
/// it can be used to make the function interface easier to work with.
#[cfg(feature = "open")]
#[repr(C)]
pub struct const_iovec {
pub iov_base: *const c_void,
pub iov_len: size_t,
}

#[cfg(feature = "open")]
impl const_iovec {
///
/// # Safety
///
/// Lifetime of `arg` must be long enough to cover future dereferences of the internal
/// `Self::iov_base` pointer.
pub unsafe fn from_str<T>(arg: T) -> Self
where
T: AsRef<str>,
{
const_iovec {
iov_base: arg.as_ref().as_ptr() as *const c_void,
iov_len: arg.as_ref().len() as size_t,
}
}
}
// END COPY FROM LIBSYSTEMD_SYS
Loading