From c2703d07662909f228e7e93ecec585bc4c826449 Mon Sep 17 00:00:00 2001
From: yanan-rock <yanan@huawei.com>
Date: Fri, 21 Jul 2023 08:34:53 +0000
Subject: [PATCH] refactoring preload module

Signed-off-by: yanan-rock <yanan@huawei.com>
---
 src/lstack/core/lstack_init.c       | 40 +--------------------
 src/lstack/core/lstack_preload.c    | 55 +++++++++++++++++++++++++++++
 src/lstack/include/lstack_preload.h | 16 +++++++++
 3 files changed, 72 insertions(+), 39 deletions(-)
 create mode 100644 src/lstack/core/lstack_preload.c
 create mode 100644 src/lstack/include/lstack_preload.h

diff --git a/src/lstack/core/lstack_init.c b/src/lstack/core/lstack_init.c
index 4fbbc14..53bc541 100644
--- a/src/lstack/core/lstack_init.c
+++ b/src/lstack/core/lstack_init.c
@@ -46,11 +46,7 @@
 #include "posix/lstack_unistd.h"
 #include "gazelle_base_func.h"
 #include "lstack_protocol_stack.h"
-
-#define LSTACK_PRELOAD_ENV_SYS      "LD_PRELOAD"
-#define LSTACK_SO_NAME              "liblstack.so"
-#define LSTACK_PRELOAD_NAME_LEN     PATH_MAX
-#define LSTACK_PRELOAD_ENV_PROC     "GAZELLE_BIND_PROCNAME"
+#include "lstack_preload.h"
 
 static volatile bool g_init_fail = false;
 
@@ -64,40 +60,6 @@ bool get_init_fail(void)
     return g_init_fail;
 }
 
-struct lstack_preload {
-    int32_t preload_switch;
-    char env_procname[LSTACK_PRELOAD_NAME_LEN];
-};
-static struct lstack_preload g_preload_info = {0};
-
-static int32_t preload_info_init(void)
-{
-    char *enval = NULL;
-
-    g_preload_info.preload_switch = 0;
-
-    enval = getenv(LSTACK_PRELOAD_ENV_SYS);
-    if (enval == NULL) {
-        return 0;
-    }
-
-    if (strstr(enval, LSTACK_SO_NAME) == NULL) {
-        return 0;
-    }
-
-    enval = getenv(LSTACK_PRELOAD_ENV_PROC);
-    if (enval == NULL) {
-        return -1;
-    }
-    if (strcpy_s(g_preload_info.env_procname, LSTACK_PRELOAD_NAME_LEN, enval) != EOK) {
-        return -1;
-    }
-
-    g_preload_info.preload_switch = 1;
-    LSTACK_PRE_LOG(LSTACK_INFO, "LD_PRELOAD ok\n");
-    return 0;
-}
-
 static void check_process_start(void)
 {
     if (get_global_cfg_params()->is_primary) {
diff --git a/src/lstack/core/lstack_preload.c b/src/lstack/core/lstack_preload.c
new file mode 100644
index 0000000..9fc5819
--- /dev/null
+++ b/src/lstack/core/lstack_preload.c
@@ -0,0 +1,55 @@
+/*
+* Copyright (c) Huawei Technologies Co., Ltd. 2020-2021. All rights reserved.
+* gazelle is licensed under the Mulan PSL v2.
+* You can use this software according to the terms and conditions of the Mulan PSL v2.
+* You may obtain a copy of Mulan PSL v2 at:
+*     http://license.coscl.org.cn/MulanPSL2
+* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
+* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
+* PURPOSE.
+* See the Mulan PSL v2 for more details.
+*/
+#include <string.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include "lstack_preload.h"
+
+#define LSTACK_PRELOAD_ENV_SYS      "LD_PRELOAD"
+#define LSTACK_SO_NAME              "liblstack.so"
+#define LSTACK_PRELOAD_NAME_LEN     PATH_MAX
+#define LSTACK_PRELOAD_ENV_PROC     "GAZELLE_BIND_PROCNAME"
+
+struct lstack_preload {
+    int32_t preload_switch;
+    char env_procname[LSTACK_PRELOAD_NAME_LEN];
+};
+static struct lstack_preload g_preload_info = {0};
+
+static int32_t preload_info_init(void)
+{
+    char *enval = NULL;
+
+    g_preload_info.preload_switch = 0;
+
+    enval = getenv(LSTACK_PRELOAD_ENV_SYS);
+    if (enval == NULL) {
+        return 0;
+    }
+
+    if (strstr(enval, LSTACK_SO_NAME) == NULL) {
+        return 0;
+    }
+
+    enval = getenv(LSTACK_PRELOAD_ENV_PROC);
+    if (enval == NULL) {
+        return -1;
+    }
+    if (strcpy_s(g_preload_info.env_procname, LSTACK_PRELOAD_NAME_LEN, enval) != EOK) {
+        return -1;
+    }
+
+    g_preload_info.preload_switch = 1;
+    LSTACK_PRE_LOG(LSTACK_INFO, "LD_PRELOAD ok\n");
+    return 0;
+}
diff --git a/src/lstack/include/lstack_preload.h b/src/lstack/include/lstack_preload.h
new file mode 100644
index 0000000..4ba6525
--- /dev/null
+++ b/src/lstack/include/lstack_preload.h
@@ -0,0 +1,16 @@
+/*
+* Copyright (c) Huawei Technologies Co., Ltd. 2020-2021. All rights reserved.
+* gazelle is licensed under the Mulan PSL v2.
+* You can use this software according to the terms and conditions of the Mulan PSL v2.
+* You may obtain a copy of Mulan PSL v2 at:
+*     http://license.coscl.org.cn/MulanPSL2
+* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
+* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
+* PURPOSE.
+* See the Mulan PSL v2 for more details.
+*/
+#ifndef __LSTACK_PRELOAD_H__
+#define __LSTACK_PRELOAD_H__
+
+int preload_info_init(void);
+#endif
-- 
2.23.0

