From eef8806871232cda5a8e3525d5d52b83925eb46c Mon Sep 17 00:00:00 2001
From: jokemanfire <hu.dingyang@zte.com.cn>
Date: Thu, 5 Dec 2024 20:28:46 +0800
Subject: [PATCH] fix process exit (not init) event not publish

1.add get id interface in process
2.add process event publish in service

Signed-off-by: jokemanfire <hu.dingyang@zte.com.cn>
---
 crates/runc-shim/src/processes.rs |  5 ++++
 crates/runc-shim/src/service.rs   | 48 +++++++++++++++++--------------
 2 files changed, 31 insertions(+), 22 deletions(-)

diff --git a/crates/runc-shim/src/processes.rs b/crates/runc-shim/src/processes.rs
index 2d523009..b6e85eb9 100644
--- a/crates/runc-shim/src/processes.rs
+++ b/crates/runc-shim/src/processes.rs
@@ -57,6 +57,7 @@ pub trait Process {
     async fn close_io(&mut self) -> Result<()>;
     async fn pause(&mut self) -> Result<()>;
     async fn resume(&mut self) -> Result<()>;
+    async fn id(&self) -> &str;
 }
 
 #[async_trait]
@@ -123,6 +124,10 @@ where
         self.pid
     }
 
+    async fn id(&self) -> &str {
+        self.id.as_str()
+    }
+
     async fn state(&self) -> Result<StateResponse> {
         let mut resp = StateResponse::new();
         resp.id = self.id.to_string();
diff --git a/crates/runc-shim/src/service.rs b/crates/runc-shim/src/service.rs
index c33c96d5..8007d328 100644
--- a/crates/runc-shim/src/service.rs
+++ b/crates/runc-shim/src/service.rs
@@ -163,6 +163,8 @@ async fn process_exits(
                 let exit_code = e.exit_code;
                 for (_k, cont) in containers.write().await.iter_mut() {
                     let bundle = cont.bundle.to_string();
+                    let container_id = cont.id.clone();
+                    let mut change_process: Vec<&mut (dyn Process + Send + Sync)> = Vec::new();
                     // pid belongs to container init process
                     if cont.init.pid == pid {
                         // kill all children process if the container has a private PID namespace
@@ -171,20 +173,30 @@ async fn process_exits(
                                 error!("failed to kill init's children: {}", e)
                             });
                         }
-                        // set exit for init process
-                        cont.init.set_exited(exit_code).await;
-
+                        if let Ok(process_d) = cont.get_mut_process(None) {
+                            change_process.push(process_d);
+                        } else {
+                            break;
+                        }
+                    } else {
+                        // pid belongs to container common process
+                        if let Some((_, p)) = cont.processes.iter_mut().find(|(_, p)| p.pid == pid)
+                        {
+                            change_process.push(p as &mut (dyn Process + Send + Sync));
+                        }
+                    }
+                    let process_len = change_process.len();
+                    for process in change_process {
+                        // set exit for process
+                        process.set_exited(exit_code).await;
+                        let code = process.exit_code().await;
+                        let exited_at = process.exited_at().await;
                         // publish event
-                        let (_, code, exited_at) = match cont.get_exit_info(None).await {
-                            Ok(info) => info,
-                            Err(_) => break,
-                        };
-
                         let ts = convert_to_timestamp(exited_at);
                         let event = TaskExit {
-                            container_id: cont.id.to_string(),
-                            id: cont.id.to_string(),
-                            pid: cont.pid().await as u32,
+                            container_id: container_id.clone(),
+                            id: process.id().await.to_string(),
+                            pid: process.pid().await as u32,
                             exit_status: code as u32,
                             exited_at: Some(ts).into(),
                             ..Default::default()
@@ -193,18 +205,10 @@ async fn process_exits(
                         tx.send((topic.to_string(), Box::new(event)))
                             .await
                             .unwrap_or_else(|e| warn!("send {} to publisher: {}", topic, e));
-
-                        break;
                     }
-
-                    // pid belongs to container common process
-                    for (_exec_id, p) in cont.processes.iter_mut() {
-                        // set exit for exec process
-                        if p.pid == pid {
-                            p.set_exited(exit_code).await;
-                            // TODO: publish event
-                            break;
-                        }
+                    //if process has been find , no need to keep search
+                    if process_len != 0 {
+                        break;
                     }
                 }
             }