|
16 | 16 | You should have received a copy of the GNU General Public License |
17 | 17 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
18 | 18 | */ |
| 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}; |
19 | 25 |
|
20 | | -pub mod ipc; |
21 | 26 | pub mod reporter; |
22 | 27 | 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 | +} |
0 commit comments