Skip to content

Commit c1dfffa

Browse files
committed
reporting: reporter factory has less responsibility
1 parent 2a6f992 commit c1dfffa

File tree

4 files changed

+18
-38
lines changed

4 files changed

+18
-38
lines changed

bear/src/intercept/reporter.rs

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,10 @@
1313
//!
1414
//! # Usage
1515
//!
16-
//! Use `ReporterFactory::create()` to instantiate a reporter, or `ReporterFactory::create_as_ptr()`
17-
//! to obtain a raw pointer suitable for static/global usage. The reporter sends events to a remote
18-
//! collector, with the destination specified by the `KEY_DESTINATION` environment variable.
16+
//! Use `ReporterFactory::create()` to instantiate a reporter with a socket address, or
17+
//! `ReporterFactory::create_as_ptr()` to obtain a raw pointer suitable for static/global usage.
18+
//! The reporter sends events to a remote collector at the specified address.
1919
20-
use crate::environment::KEY_DESTINATION;
2120
use crate::intercept::{Event, tcp};
2221
use std::net::SocketAddr;
2322
use std::sync::atomic::AtomicPtr;
@@ -48,17 +47,8 @@ impl ReporterFactory {
4847
/// Creates a new TCP-based reporter using the destination from the environment.
4948
///
5049
/// The created reporter is not connected yet; it only stores the destination address.
51-
/// It is safe to presume that the existence of the instance does not imply it is
52-
/// consuming resources until the `report` method is called.
53-
pub fn create() -> Result<tcp::ReporterOnTcp, ReporterCreationError> {
54-
let address_str = std::env::var(KEY_DESTINATION)
55-
.map_err(|_| ReporterCreationError::MissingEnvironmentVariable(KEY_DESTINATION))?;
56-
let address = address_str
57-
.parse::<SocketAddr>()
58-
.map_err(|_| ReporterCreationError::MissingEnvironmentVariable(KEY_DESTINATION))?;
59-
60-
let reporter = tcp::ReporterOnTcp::new(address);
61-
Ok(reporter)
50+
pub fn create(address: SocketAddr) -> impl Reporter {
51+
tcp::ReporterOnTcp::new(address)
6252
}
6353

6454
/// Creates a new reporter and returns it as an atomic pointer.
@@ -74,11 +64,11 @@ impl ReporterFactory {
7464
///
7565
/// Returns a null pointer if reporter creation fails. Caller must check for null
7666
/// before dereferencing.
77-
pub fn create_as_ptr() -> AtomicPtr<tcp::ReporterOnTcp> {
78-
match Self::create() {
79-
Ok(reporter) => {
67+
pub fn create_as_ptr(address_str: &str) -> AtomicPtr<tcp::ReporterOnTcp> {
68+
match address_str.parse::<SocketAddr>() {
69+
Ok(address) => {
8070
// Leak the reporter to get a stable pointer for the lifetime of the program
81-
let boxed_reporter = Box::new(reporter);
71+
let boxed_reporter = Box::new(tcp::ReporterOnTcp::new(address));
8272
let ptr = Box::into_raw(boxed_reporter);
8373

8474
AtomicPtr::new(ptr)
@@ -90,12 +80,3 @@ impl ReporterFactory {
9080
}
9181
}
9282
}
93-
94-
/// Errors that can occur during reporter initialization.
95-
#[derive(Error, Debug)]
96-
pub enum ReporterCreationError {
97-
#[error("Environment variable '{0}' is missing")]
98-
MissingEnvironmentVariable(&'static str),
99-
#[error("Network error: {0}")]
100-
Network(#[from] std::io::Error),
101-
}

intercept-preload/src/implementation.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@ use std::path::PathBuf;
2121
use std::ptr;
2222
use std::sync::atomic::{AtomicPtr, Ordering};
2323

24+
use bear::environment::KEY_DESTINATION;
2425
use bear::intercept::reporter::{Reporter, ReporterFactory};
2526
use bear::intercept::tcp::ReporterOnTcp;
2627
use bear::intercept::{Event, Execution};
2728
use ctor::ctor;
28-
use libc::{c_char, c_int, pid_t, posix_spawn_file_actions_t, posix_spawnattr_t};
29+
use libc::{RTLD_NEXT, c_char, c_int, pid_t, posix_spawn_file_actions_t, posix_spawnattr_t};
2930

3031
/// Constructor function that is called when the library is loaded
3132
///
@@ -48,8 +49,6 @@ unsafe fn on_load() {
4849
log::debug!("Initializing intercept-preload library");
4950
}
5051

51-
const RTLD_NEXT: *mut libc::c_void = -1isize as *mut libc::c_void;
52-
5352
#[ctor]
5453
static REAL_EXECVE: AtomicPtr<libc::c_void> = {
5554
let ptr = unsafe { libc::dlsym(RTLD_NEXT, c"execve".as_ptr() as *const _) };
@@ -112,9 +111,10 @@ static REAL_SYSTEM: AtomicPtr<libc::c_void> = {
112111

113112
#[ctor]
114113
static REPORTER: AtomicPtr<ReporterOnTcp> = {
115-
// This will capture the reporter address for later use, before any environment variable
116-
// might be changed.
117-
ReporterFactory::create_as_ptr()
114+
match std::env::var(KEY_DESTINATION) {
115+
Ok(address_str) => ReporterFactory::create_as_ptr(&address_str),
116+
Err(_) => AtomicPtr::new(std::ptr::null_mut()),
117+
}
118118
};
119119

120120
/// Rust implementation of execv

intercept-preload/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ pub use implementation::*;
1414

1515
/// Version information for the library
1616
#[unsafe(no_mangle)]
17-
pub static LIBEAR_VERSION: &[u8; 6] = b"4.0.0\0";
17+
pub static LIBEAR_VERSION: &[u8; 6] = b"4.0.3\0";

intercept-wrapper/src/main.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@ extern crate core;
1919

2020
use anyhow::{Context, Result};
2121
use bear::environment::KEY_DESTINATION;
22-
use bear::intercept::reporter::Reporter;
22+
use bear::intercept::reporter::{Reporter, ReporterFactory};
2323
use bear::intercept::supervise::supervise_execution;
24-
use bear::intercept::tcp;
2524
use bear::intercept::wrapper::{CONFIG_FILENAME, WrapperConfig, WrapperConfigReader};
2625
use bear::intercept::{Event, Execution};
2726
use std::io::Write;
@@ -70,7 +69,7 @@ fn report(real_execution: &Execution) -> Result<()> {
7069
.parse::<SocketAddr>()
7170
.with_context(|| format!("Failed to parse interceptor address: {}", address_str))?;
7271

73-
tcp::ReporterOnTcp::new(address)
72+
ReporterFactory::create(address)
7473
};
7574
// Report the execution event to collector.
7675
reporter.report(Event::new(real_execution.clone()))?;

0 commit comments

Comments
 (0)