Skip to content

Commit caad0a0

Browse files
qq906907952sharang
authored andcommitted
[Agent] support proc tag spread from parent proc
1 parent 43e9aad commit caad0a0

File tree

1 file changed

+54
-1
lines changed

1 file changed

+54
-1
lines changed

agent/src/platform/platform_synchronizer/linux_process.rs

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ use std::time::{Duration, SystemTime, UNIX_EPOCH};
1919
use std::{collections::HashMap, os::unix::process::CommandExt, process::Command};
2020

2121
use envmnt::{ExpandOptions, ExpansionType};
22-
2322
use log::error;
2423
use nom::AsBytes;
2524
use procfs::{process::Process, ProcError, ProcResult};
@@ -42,6 +41,7 @@ use crate::config::{
4241
pub struct ProcessData {
4342
pub name: String, // the replaced name
4443
pub pid: u64,
44+
pub ppid: u64,
4545
pub process_name: String, // raw process name
4646
pub cmd: Vec<String>,
4747
pub user_id: u32,
@@ -113,6 +113,12 @@ impl TryFrom<&Process> for ProcessData {
113113
Ok(ProcessData {
114114
name: proc_name.to_string_lossy().to_string(),
115115
pid: proc.pid as u64,
116+
ppid: if let Ok(stat) = proc.stat().as_ref() {
117+
stat.ppid as u64
118+
} else {
119+
error!("pid {} get stat fail", proc.pid);
120+
0
121+
},
116122
process_name: proc_name.to_string_lossy().to_string(),
117123
cmd,
118124
user_id: uid,
@@ -356,6 +362,7 @@ pub(super) fn get_all_process(conf: &OsProcScanConfig) -> Vec<ProcessData> {
356362
}
357363
}
358364
}
365+
fill_child_proc_tag_by_parent(ret.as_mut());
359366
proc_scan_hook(&mut ret);
360367
}
361368
return ret;
@@ -408,3 +415,49 @@ fn get_os_app_tag_by_exec(
408415
Err(e) => Err(format!("unmarshal to yaml fail: {}\nstdout: {}", e, stdout).to_string()),
409416
}
410417
}
418+
419+
fn fill_child_proc_tag_by_parent(procs: &mut Vec<ProcessData>) {
420+
let merge_tag = |child_tag: &mut Vec<OsAppTagKV>, parent_tag: &[OsAppTagKV]| {
421+
'l: for pt in parent_tag {
422+
// ignore key is in exist in child
423+
for ct in child_tag.iter() {
424+
if ct.key == pt.key {
425+
continue 'l;
426+
}
427+
}
428+
child_tag.push(pt.clone());
429+
}
430+
};
431+
432+
// Hashmap<pid, proc_idx> use for fill child proc tag from parent tag
433+
let mut pid_map = HashMap::new();
434+
for (i, p) in procs.iter().enumerate() {
435+
pid_map.insert(p.pid, i);
436+
}
437+
438+
for child_idx in 0..procs.len() {
439+
let child_ppid = procs.get(child_idx).unwrap().ppid;
440+
let Some(parent_idx) = pid_map.get(&child_ppid) else {
441+
continue;
442+
};
443+
444+
if child_idx == *parent_idx {
445+
error!("pid: {} child pid equal to parent pid", child_ppid);
446+
continue;
447+
}
448+
449+
let (child, parent) = if child_idx > *parent_idx {
450+
let (left, right) = procs.split_at_mut(child_idx);
451+
let child = right.get_mut(0).unwrap();
452+
let parent: &ProcessData = left.get(*parent_idx).unwrap();
453+
(child, parent)
454+
} else {
455+
let (left, right) = procs.split_at_mut(*parent_idx);
456+
let child = left.get_mut(child_idx).unwrap();
457+
let parent: &ProcessData = right.get(0).unwrap();
458+
(child, parent)
459+
};
460+
461+
merge_tag(&mut child.os_app_tags, &parent.os_app_tags);
462+
}
463+
}

0 commit comments

Comments
 (0)