diff --git a/agent/Cargo.toml b/agent/Cargo.toml index 53b3f9e..2b99cff 100644 --- a/agent/Cargo.toml +++ b/agent/Cargo.toml @@ -6,6 +6,9 @@ edition = "2021" license = "GPL-3.0-or-later" authors = ["The crypto-auditing developers"] +[features] +default = ["tokio-uring"] + [dependencies] anyhow = "1.0" bytes = "1.2" @@ -21,7 +24,7 @@ serde = "1.0" serde_cbor = "0.10" time = { version = "0.3", features=["formatting", "local-offset", "macros"] } tokio = { version = "1.23", features=["full"] } -tokio-uring = "0.4" +tokio-uring = { version = "0.4", optional = true } toml = "0.6" tracing = "0.1" tracing-subscriber = { version = "0.3", features=["env-filter"] } diff --git a/agent/src/log_writer.rs b/agent/src/log_writer.rs index eeac1d3..d7933f7 100644 --- a/agent/src/log_writer.rs +++ b/agent/src/log_writer.rs @@ -8,6 +8,12 @@ use serde_cbor::{ser::IoWrite, Serializer}; use std::path::PathBuf; use time::{macros::format_description, OffsetDateTime}; use tokio::time::{Duration, Instant}; +#[cfg(not(feature = "tokio-uring"))] +use tokio::{ + fs::{rename, File}, + io::AsyncWriteExt, +}; +#[cfg(feature = "tokio-uring")] use tokio_uring::fs::{rename, File}; pub struct LogWriter { @@ -97,6 +103,7 @@ impl LogWriter { .await .with_context(|| format!("unable to sync file `{}`", self.config.log_file.display()))?; + #[cfg(feature = "tokio-uring")] file.close().await.with_context(|| { format!("unable to close file `{}`", self.config.log_file.display()) })?; @@ -151,8 +158,14 @@ impl LogWriter { config::Format::Packed => serde_cbor::ser::to_vec_packed(&group)?, config::Format::Minimal => to_vec_minimal(&group)?, }; - let (res, _) = match self.file { - Some(ref file) => file.write_at(v, self.offset).await, + #[cfg(feature = "tokio-uring")] + let res = match self.file { + Some(ref file) => file.write_at(v, self.offset).await.0, + _ => bail!("log file is not opened"), + }; + #[cfg(not(feature = "tokio-uring"))] + let res = match self.file { + Some(ref mut file) => file.write(&v).await, _ => bail!("log file is not opened"), }; let n = res?; diff --git a/agent/src/main.rs b/agent/src/main.rs index 52bb9b0..9e6fc3a 100644 --- a/agent/src/main.rs +++ b/agent/src/main.rs @@ -3,6 +3,7 @@ use anyhow::{bail, Context as _, Result}; use bytes::BytesMut; +use core::future::Future; use crypto_auditing::types::{ContextID, EventGroup}; use openssl::{ rand::rand_bytes, @@ -71,6 +72,20 @@ impl Tracer { } } +#[cfg(feature = "tokio-uring")] +fn start(future: F) -> F::Output { + tokio_uring::start(future) +} + +#[cfg(not(feature = "tokio-uring"))] +fn start(future: F) -> F::Output { + tokio::runtime::Builder::new_current_thread() + .enable_all() + .build() + .unwrap() + .block_on(future) +} + fn main() -> Result<(), Box> { let config = config::Config::new()?; @@ -136,7 +151,7 @@ fn main() -> Result<(), Box> { let mut encryption_key = vec![0; cipher.key_len()]; rand_bytes(&mut encryption_key)?; - tokio_uring::start(async { + start(async { let mut rb = ringbuf::RingBuffer::new(skel.obj.map_mut("ringbuf").unwrap()); if let Some((ref user, ref group)) = config.user {