Skip to content

Commit e330c51

Browse files
committed
rust: simplify intercept execution event
1 parent 4e92977 commit e330c51

File tree

3 files changed

+88
-34
lines changed

3 files changed

+88
-34
lines changed

rust/intercept/src/bin/wrapper.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ fn report_execution(path_buf: &PathBuf) {
5858
.expect("Cannot create reporter");
5959

6060
// Report the execution
61-
let execution = intercept::Event::Started {
61+
let execution = intercept::Event {
6262
pid: intercept::ProcessId(std::process::id() as u32),
6363
execution: intercept::Execution {
6464
executable: path_buf.clone(),

rust/intercept/src/lib.rs

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,23 @@ use serde::{Deserialize, Serialize};
2626
pub mod collector;
2727
pub mod reporter;
2828

29-
// Reporter id is a unique identifier for a reporter.
30-
//
31-
// It is used to identify the process that sends the execution report.
32-
// Because the OS PID is not unique across a single build (PIDs are
33-
// recycled), we need to use a new unique identifier to identify the process.
29+
/// Reporter id is a unique identifier for a reporter.
30+
///
31+
/// It is used to identify the process that sends the execution report.
32+
/// Because the OS PID is not unique across a single build (PIDs are
33+
/// recycled), we need to use a new unique identifier to identify the process.
3434
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
3535
pub struct ReporterId(pub u64);
3636

37+
/// Process id is a OS identifier for a process.
3738
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
3839
pub struct ProcessId(pub u32);
3940

41+
/// Execution is a representation of a process execution.
42+
///
43+
/// It does not contain information about the outcome of the execution,
44+
/// like the exit code or the duration of the execution. It only contains
45+
/// the information that is necessary to reproduce the execution.
4046
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
4147
pub struct Execution {
4248
pub executable: PathBuf,
@@ -45,26 +51,20 @@ pub struct Execution {
4551
pub environment: HashMap<String, String>,
4652
}
4753

48-
// Represent a relevant life cycle event of a process.
49-
//
50-
// Currently, it's only the process life cycle events (start, signal,
51-
// terminate), but can be extended later with performance related
52-
// events like monitoring the CPU usage or the memory allocation if
53-
// this information is available.
54+
/// Represent a relevant life cycle event of a process.
55+
///
56+
/// In the current implementation, we only have one event, the `Started` event.
57+
/// This event is sent when a process is started. It contains the process id
58+
/// and the execution information.
5459
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
55-
pub enum Event {
56-
Started {
57-
pid: ProcessId,
58-
execution: Execution,
59-
},
60-
Terminated {
61-
status: i64,
62-
},
63-
Signaled {
64-
signal: i32,
65-
},
60+
pub struct Event {
61+
pub pid: ProcessId,
62+
pub execution: Execution,
6663
}
6764

65+
/// Envelope is a wrapper around the event.
66+
///
67+
/// It contains the reporter id, the timestamp of the event and the event itself.
6868
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
6969
pub struct Envelope {
7070
pub rid: ReporterId,
@@ -82,6 +82,10 @@ impl Envelope {
8282
}
8383
}
8484

85+
/// Read an envelope from a reader using TLV format.
86+
///
87+
/// The envelope is serialized using JSON and the length of the JSON
88+
/// is written as a 4 byte big-endian integer before the JSON.
8589
pub fn read_from(reader: &mut impl Read) -> Result<Self, anyhow::Error> {
8690
let mut length_bytes = [0; 4];
8791
reader.read_exact(&mut length_bytes)?;
@@ -94,6 +98,10 @@ impl Envelope {
9498
Ok(envelope)
9599
}
96100

101+
/// Write an envelope to a writer using TLV format.
102+
///
103+
/// The envelope is serialized using JSON and the length of the JSON
104+
/// is written as a 4 byte big-endian integer before the JSON.
97105
pub fn write_into(&self, writer: &mut impl Write) -> Result<u32, anyhow::Error> {
98106
let serialized_envelope = serde_json::to_string(&self)?;
99107
let bytes = serialized_envelope.into_bytes();

rust/intercept/tests/test.rs

Lines changed: 57 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -78,29 +78,75 @@ mod test {
7878
lazy_static! {
7979
static ref ENVELOPES: Vec<Envelope> = vec![
8080
Envelope {
81-
rid: ReporterId(1),
82-
timestamp: 0,
83-
event: Event::Started {
84-
pid: ProcessId(1),
81+
rid: ReporterId::new(),
82+
timestamp: fixtures::timestamp(),
83+
event: Event {
84+
pid: fixtures::pid(),
8585
execution: Execution {
8686
executable: PathBuf::from("/usr/bin/ls"),
87-
arguments: vec!["ls".to_string(), "-l".to_string()],
87+
arguments: vec_of_strings!["ls", "-l"],
8888
working_dir: PathBuf::from("/tmp"),
8989
environment: HashMap::new(),
9090
},
9191
},
9292
},
9393
Envelope {
94-
rid: ReporterId(1),
95-
timestamp: 0,
96-
event: Event::Terminated { status: 0 },
94+
rid: ReporterId::new(),
95+
timestamp: fixtures::timestamp(),
96+
event: Event {
97+
pid: fixtures::pid(),
98+
execution: Execution {
99+
executable: PathBuf::from("/usr/bin/cc"),
100+
arguments: vec_of_strings!["cc", "-c", "./file_a.c", "-o", "./file_a.o"],
101+
working_dir: PathBuf::from("/home/user"),
102+
environment: map_of_strings! {
103+
"PATH" => "/usr/bin:/bin",
104+
"HOME" => "/home/user",
105+
},
106+
},
107+
},
97108
},
98109
Envelope {
99-
rid: ReporterId(1),
100-
timestamp: 0,
101-
event: Event::Signaled { signal: 15 },
110+
rid: ReporterId::new(),
111+
timestamp: fixtures::timestamp(),
112+
event: Event {
113+
pid: fixtures::pid(),
114+
execution: Execution {
115+
executable: PathBuf::from("/usr/bin/ld"),
116+
arguments: vec_of_strings!["ld", "-o", "./file_a", "./file_a.o"],
117+
working_dir: PathBuf::from("/opt/project"),
118+
environment: map_of_strings! {
119+
"PATH" => "/usr/bin:/bin",
120+
"LD_LIBRARY_PATH" => "/usr/lib:/lib",
121+
},
122+
},
123+
},
102124
},
103125
];
104126
static ref EVENTS: Vec<Event> = ENVELOPES.iter().map(|e| e.event.clone()).collect();
105127
}
106128
}
129+
130+
mod fixtures {
131+
use intercept::ProcessId;
132+
133+
pub fn timestamp() -> u64 {
134+
rand::random::<u64>()
135+
}
136+
137+
pub fn pid() -> ProcessId {
138+
ProcessId(rand::random::<u32>())
139+
}
140+
141+
#[macro_export]
142+
macro_rules! vec_of_strings {
143+
($($x:expr),*) => (vec![$($x.to_string()),*]);
144+
}
145+
146+
#[macro_export]
147+
macro_rules! map_of_strings {
148+
($($k:expr => $v:expr),* $(,)?) => {{
149+
core::convert::From::from([$(($k.to_string(), $v.to_string()),)*])
150+
}};
151+
}
152+
}

0 commit comments

Comments
 (0)