Skip to content

Commit 70110ab

Browse files
ai-agent: inherit child proc lifecycle
1 parent 89878d1 commit 70110ab

2 files changed

Lines changed: 71 additions & 1 deletion

File tree

agent/src/common/proc_event/linux.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,9 @@ const PROC_LC_UID_OFF: usize = 9;
238238
const PROC_LC_GID_OFF: usize = 13;
239239
const PROC_LC_TS_OFF: usize = 17;
240240
const PROC_LC_COMM_OFF: usize = 25;
241+
pub const PROC_LIFECYCLE_FORK: u8 = 1;
242+
pub const PROC_LIFECYCLE_EXEC: u8 = 2;
243+
pub const PROC_LIFECYCLE_EXIT: u8 = 3;
241244

242245
struct ProcLifecycleEventData {
243246
lifecycle_type: u8,
@@ -249,6 +252,13 @@ struct ProcLifecycleEventData {
249252
comm: Vec<u8>,
250253
}
251254

255+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
256+
pub struct ProcLifecycleInfo {
257+
pub lifecycle_type: u8,
258+
pub pid: u32,
259+
pub parent_pid: u32,
260+
}
261+
252262
impl TryFrom<&[u8]> for ProcLifecycleEventData {
253263
type Error = Error;
254264

@@ -440,6 +450,17 @@ impl ProcEvent {
440450

441451
Ok(BoxedProcEvents(Box::new(proc_event)))
442452
}
453+
454+
pub fn proc_lifecycle_info(&self) -> Option<ProcLifecycleInfo> {
455+
match &self.event_data {
456+
EventData::ProcLifecycleEvent(data) => Some(ProcLifecycleInfo {
457+
lifecycle_type: data.lifecycle_type,
458+
pid: data.pid,
459+
parent_pid: data.parent_pid,
460+
}),
461+
_ => None,
462+
}
463+
}
443464
}
444465

445466
#[derive(Debug)]
@@ -491,3 +512,37 @@ impl Sendable for BoxedProcEvents {
491512
SendMessageType::ProcEvents
492513
}
493514
}
515+
516+
#[cfg(test)]
517+
mod tests {
518+
use super::*;
519+
520+
#[test]
521+
fn test_proc_lifecycle_info_extracts_fields() {
522+
let event_data = ProcLifecycleEventData {
523+
lifecycle_type: 1,
524+
pid: 4321,
525+
parent_pid: 1234,
526+
uid: 0,
527+
gid: 0,
528+
timestamp: 42,
529+
comm: b"sleep".to_vec(),
530+
};
531+
let proc_event = ProcEvent {
532+
pid: 1234,
533+
pod_id: 0,
534+
thread_id: 0,
535+
coroutine_id: 0,
536+
process_kname: b"python3".to_vec(),
537+
start_time: 42,
538+
end_time: 43,
539+
event_type: EventType::ProcLifecycleEvent,
540+
event_data: EventData::ProcLifecycleEvent(event_data),
541+
};
542+
543+
let info = proc_event.proc_lifecycle_info().expect("missing info");
544+
assert_eq!(info.lifecycle_type, 1);
545+
assert_eq!(info.pid, 4321);
546+
assert_eq!(info.parent_pid, 1234);
547+
}
548+
}

agent/src/ebpf_dispatcher.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ use crate::common::l7_protocol_log::{
6161
get_all_protocol, L7ProtocolBitmap, L7ProtocolParserInterface,
6262
};
6363
use crate::common::meta_packet::{MetaPacket, SegmentFlags};
64-
use crate::common::proc_event::{BoxedProcEvents, EventType, ProcEvent};
64+
use crate::common::proc_event::{BoxedProcEvents, EventType, ProcEvent, PROC_LIFECYCLE_FORK};
6565
use crate::common::{FlowAclListener, FlowAclListenerId};
6666
use crate::config::handler::{CollectorAccess, EbpfAccess, EbpfConfig, LogParserAccess};
6767
use crate::config::FlowAccess;
@@ -118,6 +118,19 @@ pub struct SyncEbpfCounter {
118118
counter: Arc<EbpfCounter>,
119119
}
120120

121+
#[cfg(feature = "enterprise")]
122+
fn register_ai_agent_child(event: &BoxedProcEvents) {
123+
if let Some(info) = event.0.proc_lifecycle_info() {
124+
if info.lifecycle_type != PROC_LIFECYCLE_FORK {
125+
return;
126+
}
127+
if let Some(registry) = enterprise_utils::ai_agent::global_registry() {
128+
let now = Duration::from_nanos(event.0.start_time);
129+
registry.register_child(info.parent_pid, info.pid, now);
130+
}
131+
}
132+
}
133+
121134
impl OwnedCountable for SyncEbpfCounter {
122135
fn get_counters(&self) -> Vec<Counter> {
123136
let rx = self.counter.rx.swap(0, Ordering::Relaxed);
@@ -644,6 +657,8 @@ impl EbpfCollector {
644657
if let Some(policy) = POLICY_GETTER.as_ref() {
645658
event.0.pod_id = policy.lookup_pod_id(&container_id);
646659
}
660+
#[cfg(feature = "enterprise")]
661+
register_ai_agent_child(&event);
647662
if let Err(e) = PROC_EVENT_SENDER.as_mut().unwrap().send(event) {
648663
warn!("event send ebpf error: {:?}", e);
649664
}

0 commit comments

Comments
 (0)