(
cmd_string: &str,
ipc_server_name: Option<String>,
out_dir: &Path,
)
| 72 | } |
| 73 | |
| 74 | fn track_command( |
| 75 | cmd_string: &str, |
| 76 | ipc_server_name: Option<String>, |
| 77 | out_dir: &Path, |
| 78 | ) -> anyhow::Result<std::process::ExitStatus> { |
| 79 | // First, establish IPC connection if needed to avoid timeouts on the runner because |
| 80 | // creating the Tracker instance takes some time. |
| 81 | let ipc_channel = if let Some(server_name) = ipc_server_name { |
| 82 | debug!("Connecting to IPC server: {server_name}"); |
| 83 | |
| 84 | let (tx, rx) = ipc::channel::<MemtrackIpcMessage>()?; |
| 85 | let sender = ipc::IpcSender::connect(server_name)?; |
| 86 | sender.send(tx)?; |
| 87 | |
| 88 | Some(rx) |
| 89 | } else { |
| 90 | None |
| 91 | }; |
| 92 | |
| 93 | let tracker = Arc::new(Tracker::new()?); |
| 94 | |
| 95 | // Spawn IPC handler thread with the now-available tracker |
| 96 | let ipc_handle = if let Some(rx) = ipc_channel { |
| 97 | let tracker = tracker.clone(); |
| 98 | Some(thread::spawn(move || { |
| 99 | while let Ok(msg) = rx.recv() { |
| 100 | handle_ipc_message(msg, &tracker); |
| 101 | } |
| 102 | })) |
| 103 | } else { |
| 104 | // Without IPC, nothing toggles the tracking_enabled map, so allocator |
| 105 | // events would be dropped by the eBPF is_enabled() check. Enable it up |
| 106 | // front. |
| 107 | tracker.enable_tracking()?; |
| 108 | None |
| 109 | }; |
| 110 | |
| 111 | // Run the target command through bash to handle shell syntax. Drop |
| 112 | // privileges if running under sudo to avoid permission issues when the |
| 113 | // target accesses files owned by the original user. |
| 114 | let mut cmd = Command::new("bash"); |
| 115 | cmd.arg("-c").arg(cmd_string); |
| 116 | let uid_gid = get_user_uid_gid(); |
| 117 | if let Some((uid, gid)) = uid_gid { |
| 118 | debug!("Running under sudo, dropping privileges to uid={uid}, gid={gid}"); |
| 119 | } |
| 120 | |
| 121 | let mut session = tracker |
| 122 | .spawn(&cmd, uid_gid) |
| 123 | .map_err(|e| anyhow!("Failed to spawn child process: {e}"))?; |
| 124 | let root_pid = session.pid(); |
| 125 | let event_rx = session.take_events()?; |
| 126 | debug!("Spawned child with pid {root_pid}"); |
| 127 | |
| 128 | // Generate output file name and create file for streaming events |
| 129 | let file_name = MemtrackArtifact::file_name(Some(root_pid)); |
| 130 | let out_file = std::fs::File::create(out_dir.join(file_name))?; |
| 131 |
no test coverage detected