From a475d8da1122af712dbc79dc5d92f1cb95d519f9 Mon Sep 17 00:00:00 2001
From: zhangxiaoyu <zhangxiaoyu58@huawei.com>
Date: Mon, 25 Jul 2022 20:31:15 +0800
Subject: [PATCH 03/21] don't mount shareable dirs if user set mount for dev
 shm

Signed-off-by: zhangxiaoyu <zhangxiaoyu58@huawei.com>
---
 src/daemon/modules/spec/specs_mount.c | 51 +++++++++++++++------------
 1 file changed, 28 insertions(+), 23 deletions(-)

diff --git a/src/daemon/modules/spec/specs_mount.c b/src/daemon/modules/spec/specs_mount.c
index 85623f79..8966293f 100644
--- a/src/daemon/modules/spec/specs_mount.c
+++ b/src/daemon/modules/spec/specs_mount.c
@@ -2699,7 +2699,12 @@ int setup_ipc_dirs(host_config *host_spec, container_config_v2_common_config *v2
     int ret = -1;
     int nret = 0;
     bool has_mount = false;
-    char *spath = NULL;
+    const char *spath = NULL;
+
+    if (host_spec == NULL || v2_spec == NULL) {
+        ERROR("Invalid args");
+        return -1;
+    }
 
     // ignore shm of system container
     if (host_spec->system_container) {
@@ -2709,16 +2714,20 @@ int setup_ipc_dirs(host_config *host_spec, container_config_v2_common_config *v2
     if (host_spec->ipc_mode != NULL && !namespace_is_shareable(host_spec->ipc_mode)) {
         return 0;
     }
+    // has mount for /dev/shm
+    if (has_mount_shm(host_spec, v2_spec)) {
+        return 0;
+    }
 
-    spath = get_prepare_share_shm_path(host_spec->runtime, v2_spec->id);
+    spath = v2_spec->shm_path;
     if (spath == NULL) {
+        ERROR("No shm path");
         return -1;
     }
 
     // container shm has been mounted
     if (util_detect_mounted(spath)) {
         DEBUG("shm path %s has been mounted", spath);
-        free(spath);
         return 0;
     }
 
@@ -2757,7 +2766,6 @@ out:
     if (ret != 0 && has_mount) {
         (void)umount(spath);
     }
-    free(spath);
     return ret;
 }
 
@@ -2808,20 +2816,6 @@ out_free:
     return ret;
 }
 
-static int set_share_shm(const host_config *host_spec, container_config_v2_common_config *v2_spec)
-{
-    char *spath = NULL;
-
-    spath = get_prepare_share_shm_path(host_spec->runtime, v2_spec->id);
-    if (spath == NULL) {
-        return -1;
-    }
-
-    v2_spec->shm_path = spath;
-
-    return 0;
-}
-
 #define SHM_MOUNT_POINT "/dev/shm"
 static int set_shm_path(host_config *host_spec, container_config_v2_common_config *v2_spec)
 {
@@ -2836,7 +2830,18 @@ static int set_shm_path(host_config *host_spec, container_config_v2_common_confi
     }
     // setup shareable dirs
     if (host_spec->ipc_mode == NULL || namespace_is_shareable(host_spec->ipc_mode)) {
-        return set_share_shm(host_spec, v2_spec);
+        // has mount for /dev/shm
+        if (has_mount_shm(host_spec, v2_spec)) {
+            return 0;
+        }
+
+        v2_spec->shm_path = get_prepare_share_shm_path(host_spec->runtime, v2_spec->id);
+        if (v2_spec->shm_path == NULL) {
+            ERROR("Failed to get prepare share shm path");
+            return -1;
+        }
+
+        return 0;
     }
 
     if (namespace_is_container(host_spec->ipc_mode)) {
@@ -3373,14 +3378,14 @@ int merge_conf_mounts(oci_runtime_spec *oci_spec, host_config *host_spec, contai
         host_spec->shm_size = DEFAULT_SHM_SIZE;
     }
 
-    /* setup ipc dir */
-    if (setup_ipc_dirs(host_spec, v2_spec) != 0) {
+    if (set_shm_path(host_spec, v2_spec) != 0) {
+        ERROR("Failed to set shm path");
         ret = -1;
         goto out;
     }
 
-    if (set_shm_path(host_spec, v2_spec) != 0) {
-        ERROR("Failed to set shm path");
+    /* setup ipc dir */
+    if (setup_ipc_dirs(host_spec, v2_spec) != 0) {
         ret = -1;
         goto out;
     }
-- 
2.25.1

