Skip to content

Commit 25c2185

Browse files
committed
agent: update the processes hashmap to use exec_id as primary key
Signed-off-by: Ankita Pareek <ankitapareek@microsoft.com>
1 parent 7025072 commit 25c2185

File tree

3 files changed

+34
-40
lines changed

3 files changed

+34
-40
lines changed

src/agent/rustjail/src/container.rs

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ pub struct LinuxContainer {
277277
pub init_process_start_time: u64,
278278
pub uid_map_path: String,
279279
pub gid_map_path: String,
280-
pub processes: HashMap<pid_t, Process>,
280+
pub processes: HashMap<String, Process>,
281281
pub status: ContainerStatus,
282282
pub created: SystemTime,
283283
pub logger: Logger,
@@ -956,17 +956,13 @@ impl BaseContainer for LinuxContainer {
956956
}
957957

958958
fn processes(&self) -> Result<Vec<i32>> {
959-
Ok(self.processes.keys().cloned().collect())
959+
Ok(self.processes.values().map(|p| p.pid).collect())
960960
}
961961

962962
fn get_process(&mut self, eid: &str) -> Result<&mut Process> {
963-
for (_, v) in self.processes.iter_mut() {
964-
if eid == v.exec_id.as_str() {
965-
return Ok(v);
966-
}
967-
}
968-
969-
Err(anyhow!("invalid eid {}", eid))
963+
self.processes
964+
.get_mut(eid)
965+
.ok_or_else(|| anyhow!("invalid eid {}", eid))
970966
}
971967

972968
fn stats(&self) -> Result<StatsContainerResponse> {
@@ -1258,7 +1254,7 @@ impl BaseContainer for LinuxContainer {
12581254
let spec = self.config.spec.as_mut().unwrap();
12591255
update_namespaces(&self.logger, spec, p.pid)?;
12601256
}
1261-
self.processes.insert(p.pid, p);
1257+
self.processes.insert(p.exec_id.clone(), p);
12621258

12631259
info!(logger, "wait on child log handler");
12641260
let _ = log_handler
@@ -1284,13 +1280,13 @@ impl BaseContainer for LinuxContainer {
12841280
let spec = self.config.spec.as_ref().unwrap();
12851281
let st = self.oci_state()?;
12861282

1287-
for pid in self.processes.keys() {
1288-
match signal::kill(Pid::from_raw(*pid), Some(Signal::SIGKILL)) {
1283+
for process in self.processes.values() {
1284+
match signal::kill(process.pid()), Some(Signal::SIGKILL)) {
12891285
Err(Errno::ESRCH) => {
12901286
info!(
12911287
self.logger,
12921288
"kill encounters ESRCH, pid: {}, container: {}",
1293-
pid,
1289+
process.pid(),
12941290
self.id.clone()
12951291
);
12961292
continue;
@@ -2111,10 +2107,10 @@ mod tests {
21112107
#[test]
21122108
fn test_linuxcontainer_get_process() {
21132109
let _ = new_linux_container_and_then(|mut c: LinuxContainer| {
2114-
c.processes.insert(
2115-
1,
2116-
Process::new(&sl(), &oci::Process::default(), "123", true, 1, None).unwrap(),
2117-
);
2110+
let process = Process::new(&sl(), &oci::Process::default(), "123", true, 1, None).unwrap();
2111+
let exec_id = process.exec_id.clone();
2112+
c.processes.insert(exec_id, process);
2113+
21182114
let p = c.get_process("123");
21192115
assert!(p.is_ok(), "Expecting Ok, Got {:?}", p);
21202116
Ok(())

src/agent/src/rpc.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ impl AgentService {
584584
let p = sandbox.find_container_process(cid.as_str(), eid.as_str())?;
585585

586586
p.exit_watchers.push(exit_send);
587-
pid = p.pid;
587+
// pid = p.pid;
588588

589589
p.exit_rx.clone()
590590
};
@@ -600,7 +600,7 @@ impl AgentService {
600600
.get_container(&cid)
601601
.ok_or_else(|| anyhow!("Invalid container id"))?;
602602

603-
let p = match ctr.processes.get_mut(&pid) {
603+
let p = match ctr.processes.get_mut(&eid) {
604604
Some(p) => p,
605605
None => {
606606
// Lost race, pick up exit code from channel
@@ -624,7 +624,7 @@ impl AgentService {
624624
let _ = s.send(p.exit_code).await;
625625
}
626626

627-
ctr.processes.remove(&pid);
627+
ctr.processes.remove(&eid);
628628

629629
Ok(resp)
630630
}
@@ -2625,7 +2625,7 @@ mod tests {
26252625
}
26262626
linux_container
26272627
.processes
2628-
.insert(exec_process_id, exec_process);
2628+
.insert(exec_process.exec_id.clone(), exec_process);
26292629

26302630
sandbox.add_container(linux_container);
26312631
}

src/agent/src/sandbox.rs

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,10 @@ impl Sandbox {
272272

273273
pub fn find_process(&mut self, pid: pid_t) -> Option<&mut Process> {
274274
for (_, c) in self.containers.iter_mut() {
275-
if let Some(p) = c.processes.get_mut(&pid) {
276-
return Some(p);
275+
for p in c.processes.values_mut() {
276+
if p.pid == pid {
277+
return Some(p);
278+
}
277279
}
278280
}
279281

@@ -286,9 +288,11 @@ impl Sandbox {
286288
.ok_or_else(|| anyhow!(ERR_INVALID_CONTAINER_ID))?;
287289

288290
if eid.is_empty() {
291+
let init_pid = ctr.init_process_pid;
289292
return ctr
290293
.processes
291-
.get_mut(&ctr.init_process_pid)
294+
.values_mut()
295+
.find(|p| p.pid == init_pid)
292296
.ok_or_else(|| anyhow!("cannot find init process!"));
293297
}
294298

@@ -1017,23 +1021,17 @@ mod tests {
10171021
linux_container.init_process_pid = 1;
10181022
linux_container.id = cid.to_string();
10191023
// add init process
1024+
let mut init_process = Process::new(&logger, &oci::Process::default(), "1", true, 1, None).unwrap();
1025+
init_process.pid = 1;
10201026
linux_container.processes.insert(
1021-
1,
1022-
Process::new(&logger, &oci::Process::default(), "1", true, 1, None).unwrap(),
1023-
);
1027+
"1".to_string(),
1028+
init_process);
10241029
// add exec process
1030+
let mut exec_process = Process::new(&logger, &oci::Process::default(), "exec-123", false, 1, None).unwrap();
1031+
exec_process.pid = 123;
10251032
linux_container.processes.insert(
1026-
123,
1027-
Process::new(
1028-
&logger,
1029-
&oci::Process::default(),
1030-
"exec-123",
1031-
false,
1032-
1,
1033-
None,
1034-
)
1035-
.unwrap(),
1036-
);
1033+
"exec-123".to_string(),
1034+
exec_process);
10371035

10381036
s.add_container(linux_container);
10391037

@@ -1084,8 +1082,8 @@ mod tests {
10841082
.unwrap();
10851083
// processes interally only have pids when manually set
10861084
test_process.pid = test_pid;
1087-
1088-
linux_container.processes.insert(test_pid, test_process);
1085+
let test_exec_id = test_process.exec_id.clone();
1086+
linux_container.processes.insert(test_exec_id, test_process);
10891087

10901088
s.add_container(linux_container);
10911089

0 commit comments

Comments
 (0)