Skip to content

Commit 942e06e

Browse files
committed
rust: intercept types moved top level module
1 parent c9936da commit 942e06e

File tree

14 files changed

+96
-117
lines changed

14 files changed

+96
-117
lines changed

rust/bear/src/events.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use std::path::PathBuf;
2222

2323
use serde_json::{Deserializer, Error, Value};
2424

25-
use intercept::ipc::Execution;
25+
use intercept::Execution;
2626

2727
// Based on stream serializer from `serde_json` crate.
2828
//

rust/bear/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919
use crate::output::OutputWriter;
2020
use anyhow::Context;
21-
use intercept::ipc::Execution;
21+
use intercept::Execution;
2222
use log;
2323
use semantic;
2424
use std::fs::{File, OpenOptions};

rust/intercept/src/bin/intercept.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use anyhow::Result;
2525
use clap::{arg, ArgAction, command};
2626
use crossbeam_channel::bounded;
2727

28-
use intercept::ipc::{Envelope, Event, ReporterId};
28+
use intercept::{Envelope, Event, ReporterId};
2929
use intercept::collector::{EventCollector, EventCollectorOnTcp};
3030

3131
#[derive(Debug, PartialEq)]

rust/intercept/src/bin/wrapper.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ extern crate core;
2121

2222
use anyhow::Result;
2323

24-
use intercept::ipc::{Envelope, Event, ReporterId};
25-
2624
fn main() -> Result<()> {
2725
Ok(())
2826
}

rust/intercept/src/collector.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use crossbeam::channel::{Receiver, Sender};
2323
use crossbeam_channel::bounded;
2424
use serde::{Deserialize, Serialize};
2525

26-
use super::ipc::Envelope;
26+
use super::Envelope;
2727

2828
#[derive(Serialize, Deserialize, Debug, PartialEq)]
2929
pub struct SessionLocator(pub String);

rust/intercept/src/ipc.rs

Lines changed: 0 additions & 102 deletions
This file was deleted.

rust/intercept/src/lib.rs

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,89 @@
1616
You should have received a copy of the GNU General Public License
1717
along with this program. If not, see <http://www.gnu.org/licenses/>.
1818
*/
19+
use std::collections::HashMap;
20+
use std::io::{Read, Write};
21+
use std::path::PathBuf;
22+
23+
use chrono::Utc;
24+
use serde::{Deserialize, Serialize};
1925

20-
pub mod ipc;
2126
pub mod reporter;
2227
pub mod collector;
28+
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.
34+
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
35+
pub struct ReporterId(pub u64);
36+
37+
#[derive(Serialize, Deserialize, Debug, PartialEq)]
38+
pub struct ProcessId(pub u32);
39+
40+
#[derive(Serialize, Deserialize, Debug, PartialEq)]
41+
pub struct Execution {
42+
pub executable: PathBuf,
43+
pub arguments: Vec<String>,
44+
pub working_dir: PathBuf,
45+
pub environment: HashMap<String, String>,
46+
}
47+
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+
#[derive(Serialize, Deserialize, Debug, PartialEq)]
55+
pub enum Event {
56+
Started {
57+
pid: ProcessId,
58+
ppid: ProcessId,
59+
execution: Execution,
60+
},
61+
Terminated {
62+
status: i64
63+
},
64+
Signaled {
65+
signal: i32,
66+
},
67+
}
68+
69+
#[derive(Serialize, Deserialize, Debug, PartialEq)]
70+
pub struct Envelope {
71+
pub rid: ReporterId,
72+
pub timestamp: u64,
73+
pub event: Event,
74+
}
75+
76+
impl Envelope {
77+
pub fn new(rid: &ReporterId, event: Event) -> Self {
78+
let timestamp = Utc::now().timestamp_millis() as u64;
79+
Envelope { rid: rid.clone(), timestamp, event }
80+
}
81+
82+
pub fn read_from(mut reader: impl Read) -> Result<Self, anyhow::Error> {
83+
let mut length_bytes = [0; 4];
84+
reader.read_exact(&mut length_bytes)?;
85+
let length = u32::from_be_bytes(length_bytes) as usize;
86+
87+
let mut buffer = vec![0; length];
88+
reader.read_exact(&mut buffer)?;
89+
let envelope = serde_json::from_slice(buffer.as_ref())?;
90+
91+
Ok(envelope)
92+
}
93+
94+
pub fn write_into(&self, mut writer: impl Write) -> Result<u32, anyhow::Error> {
95+
let serialized_envelope = serde_json::to_string(&self)?;
96+
let bytes = serialized_envelope.into_bytes();
97+
let length = bytes.len() as u32;
98+
99+
writer.write_all(&length.to_be_bytes())?;
100+
writer.write_all(&bytes)?;
101+
102+
Ok(length)
103+
}
104+
}

rust/intercept/src/reporter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use std::net::TcpStream;
2121

2222
use rand::random;
2323

24-
use crate::ipc::{Envelope, Event, ReporterId};
24+
use super::{Envelope, Event, ReporterId};
2525

2626
impl ReporterId {
2727
pub fn new() -> Self {

rust/semantic/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
mod fixtures;
2121
pub mod tools;
2222

23-
use intercept::ipc::Execution;
23+
use intercept::Execution;
2424
use std::path::PathBuf;
2525

2626
/// Represents a semantic recognition result.

rust/semantic/src/tools/combinators.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
along with this program. If not, see <http://www.gnu.org/licenses/>.
1818
*/
1919

20-
use crate::{Meaning, RecognitionResult, Tool};
21-
use intercept::ipc::Execution;
20+
use crate::{RecognitionResult, Tool};
21+
use intercept::Execution;
2222

2323
/// Represents a set of tools, where any of them can recognize the semantic.
2424
/// The evaluation is done in the order of the tools. The first one which
@@ -52,6 +52,7 @@ mod test {
5252
use std::collections::HashMap;
5353
use std::path::PathBuf;
5454

55+
use super::super::super::Meaning;
5556
use super::*;
5657

5758
#[test]

0 commit comments

Comments
 (0)