From 1a01994447c2c031daca77b75325a0418104a91f Mon Sep 17 00:00:00 2001
From: Qizhi Zhang <zhangqizhi3@h-partners.com>
Date: Thu, 16 Oct 2025 18:47:37 +0800
Subject: [PATCH] sysSentry: Add SOC Ring sentry function

driver inclusion
category: feature
bugzilla: https://gitee.com/src-openeuler/sysSentry/issues/ID1UOY
CVE: NA

----------------------------------------------------------------------

Signed-off-by: Yihang Li <liyihang9@h-partners.com>
Signed-off-by: Qizhi Zhang <zhangqizhi3@h-partners.com>
---
 Makefile                                      |  17 +-
 config/env/soc_ring_sentry.env                |   6 +
 config/tasks/soc_ring_sentry.mod              |   7 +
 src/sentryPlugins/soc_ring_sentry/Makefile    |   9 +
 src/sentryPlugins/soc_ring_sentry/README.md   |  34 +++
 .../soc_ring_sentry/soc_ring_sentry.c         | 261 ++++++++++++++++++
 .../soc_ring_sentry/soc_ring_sentry.h         |  27 ++
 7 files changed, 359 insertions(+), 2 deletions(-)
 create mode 100644 config/env/soc_ring_sentry.env
 create mode 100644 config/tasks/soc_ring_sentry.mod
 create mode 100644 src/sentryPlugins/soc_ring_sentry/Makefile
 create mode 100644 src/sentryPlugins/soc_ring_sentry/README.md
 create mode 100644 src/sentryPlugins/soc_ring_sentry/soc_ring_sentry.c
 create mode 100644 src/sentryPlugins/soc_ring_sentry/soc_ring_sentry.h

diff --git a/Makefile b/Makefile
index 54e62f4..43991f9 100644
--- a/Makefile
+++ b/Makefile
@@ -27,7 +27,7 @@ PYTHON_VERSION := $(shell $(PYBIN) --version 2>&1 | awk '{print $$2}' | cut -d '
 PKGVER := syssentry-$(VERSION)-py$(PYTHON_VERSION)

 PKGVEREGG := syssentry-$(VERSION)-py$(PYTHON_VERSION).egg-info

 

-all: lib ebpf hbm_online_repair sentry_msg_monitor bmc_block_io

+all: lib ebpf hbm_online_repair sentry_msg_monitor bmc_block_io soc_ring_sentry

 

 lib:libxalarm log

 

@@ -53,6 +53,9 @@ sentry_msg_monitor: lib
 bmc_block_io: lib

 	cd $(CURSRCDIR)/sentryPlugins/bmc_block_io/ && sh build.sh

 

+soc_ring_sentry: lib

+	cd $(CURSRCDIR)/sentryPlugins/soc_ring_sentry/ && make

+

 install: all dirs isentry

 

 dirs:

@@ -129,6 +132,11 @@ isentry:
 	install -m 600 $(CURCONFIGDIR)/env/hbm_online_repair.env $(ETCDIR)/sysconfig/

 	install -m 600 $(CURCONFIGDIR)/tasks/hbm_online_repair.mod $(ETCDIR)/sysSentry/tasks/

 

+	# soc_ring_sentry

+	install -m 750 $(CURSRCDIR)/sentryPlugins/soc_ring_sentry/soc_ring_sentry $(BINDIR)

+	install -m 600 $(CURCONFIGDIR)/env/soc_ring_sentry.env $(ETCDIR)/sysconfig/

+	install -m 600 $(CURCONFIGDIR)/tasks/soc_ring_sentry.mod $(ETCDIR)/sysSentry/tasks/

+

 	# sentry_msg_monitor

 	install -m 550 $(CURSRCDIR)/sentryPlugins/sentry_msg_monitor/sentry_msg_monitor $(BINDIR)

 	install -m 600 $(CURCONFIGDIR)/env/sentry_msg_monitor.env $(ETCDIR)/sysconfig/

@@ -172,7 +180,10 @@ smm_clean:
 bmc_clean:

 	cd $(CURSRCDIR)/sentryPlugins/bmc_block_io && sh build.sh clean

 

-clean: ebpf_clean hbm_clean smm_clean bmc_clean

+srs_clean:

+	cd $(CURSRCDIR)/sentryPlugins/soc_ring_sentry && make clean

+

+clean: ebpf_clean hbm_clean smm_clean bmc_clean srs_clean

 	rm -rf $(CURLIBDIR)/build

 	rm -rf $(CURSRCDIR)/build

 	rm -rf $(CURSRCDIR)/libsentry/c/log/build

@@ -185,6 +196,7 @@ uninstall:
 	rm -rf $(BINDIR)/xalarmd

 	rm -rf $(BINDIR)/sentryCollector

 	rm -rf $(BINDIR)/hbm_online_repair

+	rm -rf $(BINDIR)/soc_ring_sentry

 	rm -rf $(BINDIR)/sentry_msg_monitor

 	rm -rf $(BINDIR)/bmc_block_io

 	rm -rf $(BINDIR)/ebpf_collector

@@ -194,6 +206,7 @@ uninstall:
 	rm -rf $(INCLUDEDIR)/libsentry

 	rm -rf $(ETCDIR)/sysSentry

 	rm -rf $(ETCDIR)/hbm_online_repair.env

+	rm -rf $(ETCDIR)/soc_ring_sentry.env

 	rm -rf $(ETCDIR)/sentry_msg_monitor.env

 	rm -rf $(LOGSAVEDIR)/sysSentry

 	rm -rf $(PYDIR)/syssentry

diff --git a/config/env/soc_ring_sentry.env b/config/env/soc_ring_sentry.env
new file mode 100644
index 0000000..b1fdd68
--- /dev/null
+++ b/config/env/soc_ring_sentry.env
@@ -0,0 +1,6 @@
+LOG_LEVEL=info
+SOC_RING_SENTRY_INTENSITY_DELAY=600
+SOC_RING_SENTRY_MEM_SIZE=4096
+SOC_RING_SENTRY_LOOP_CNT=0
+SOC_RING_SENTRY_FAULT_HANDLING=1
+SOC_RING_SENTRY_BLACKLIST=
\ No newline at end of file
diff --git a/config/tasks/soc_ring_sentry.mod b/config/tasks/soc_ring_sentry.mod
new file mode 100644
index 0000000..303567a
--- /dev/null
+++ b/config/tasks/soc_ring_sentry.mod
@@ -0,0 +1,7 @@
+[common]
+enabled=yes
+task_start=/usr/bin/soc_ring_sentry
+task_stop=pkill -f soc_ring_sentry
+type=oneshot
+onstart=yes
+env_file=/etc/sysconfig/soc_ring_sentry.env
\ No newline at end of file
diff --git a/src/sentryPlugins/soc_ring_sentry/Makefile b/src/sentryPlugins/soc_ring_sentry/Makefile
new file mode 100644
index 0000000..02032d9
--- /dev/null
+++ b/src/sentryPlugins/soc_ring_sentry/Makefile
@@ -0,0 +1,9 @@
+all: soc_ring_sentry
+
+soc_ring_sentry:
+	gcc *.c -Wall -o3 -I../../libsentry/c/log -L../../libsentry/c/log/build -I../../libs/libxalarm -L../../libs/build/libxalarm -lxalarm -lsentry_log -ljson-c -pthread -lnuma -o soc_ring_sentry
+
+clean:
+	rm -f soc_ring_sentry
+
+.PHONY: all clean
diff --git a/src/sentryPlugins/soc_ring_sentry/README.md b/src/sentryPlugins/soc_ring_sentry/README.md
new file mode 100644
index 0000000..e4b3c53
--- /dev/null
+++ b/src/sentryPlugins/soc_ring_sentry/README.md
@@ -0,0 +1,34 @@
+# soc_ring_sentry
+
+#### 介绍
+soc_ring_sentry是一款依赖sysSentry并用于SOC STL巡检的插件,该插件的使用方法是:
+
+usage: soc_ring_sentry [OPTIONS]
+
+Options:
+  -h,            Show this help message and exit.
+  -g,            Get the SOC Ring sentry case.
+
+用户可以通过 `/etc/sysconfig/soc_ring_sentry.env` 修改环境变量以配置不同参数
+该文件中各个环境变量含义为:
+`LOG_LEVEL`
+日志登记配置,默认配置为info级别。也可以配置为debug, warning, 或者error.
+
+`SOC_RING_SENTRY_INTENSITY_DELAY`
+巡检间隔时长配置,单位ms,默认配置为600ms。用户可自定义其他所需间隔时长。
+
+`SOC_RING_SENTRY_MEM_SIZE`
+巡检空间大小配置,单位KB,默认配置为4096KB。也可配置为其他64KB的倍数。
+
+`SOC_RING_SENTRY_LOOP_CNT`
+巡检次数配置,默认配置为0,即持续巡检。若配置为其他值则为巡检次数。
+
+`SOC_RING_SENTRY_FAULT_HANDLING`
+后处理标识配置,默认配置为1,即主动触发panic。
+设置为0则表示检测到错误不做任何处理。
+设置为2则表示检测到错误主动关机。
+设置为3则表示检测到错误主动重启。
+
+`SOC_RING_SENTRY_BLACKLIST`
+巡检黑名单配置,默认配置为空。用户可将不运行巡检的CPU核号写入该环境变量,例如`SOC_RING_SENTRY_BLACKLIST=0,2,4,6-10`
+
diff --git a/src/sentryPlugins/soc_ring_sentry/soc_ring_sentry.c b/src/sentryPlugins/soc_ring_sentry/soc_ring_sentry.c
new file mode 100644
index 0000000..f407fb8
--- /dev/null
+++ b/src/sentryPlugins/soc_ring_sentry/soc_ring_sentry.c
@@ -0,0 +1,261 @@
+/*
+ * Copyright (c) Huawei Technologies Co., Ltd. 2025. All rights reserved.
+ * Description: SOC Ring sentry main program
+ * Author: Yihang Li
+ * Create: 2025-7-10
+ */
+
+#include <argp.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <ctype.h>
+#include <stdint.h>
+#include <numa.h>
+#include "register_xalarm.h"
+#include "log_utils.h"
+#include "soc_ring_sentry.h"
+
+#define DEFAULT_INTENSITY_DELAY 600
+#define DEFAULT_HANDLE 1
+#define DEFAULT_LOOP_CNT 0
+#define LIMITE_MEM_SIZE 64
+#define KB 1024
+#define DEFAULT_MEM_SIZE (4 * KB * KB)
+
+uint64_t g_intensity_delay;
+uint64_t g_handle;
+uint64_t g_mem_size;
+uint64_t g_loop_cnt;
+bool *g_blacklist;
+
+static void print_opts_help()
+{
+    printf("usage: soc_ring_sentry [OPTIONS]\n"
+           "\n"
+           "Options:\n"
+           "  -h,            Show this help message and exit.\n"
+           "  -g,            Get the SOC Ring sentry case.\n");
+}
+
+static void soc_ring_sentry_case_get()
+{
+    //todo: print the test case name
+}
+
+static bool soc_ring_sentry_envtoull(char *env, uint64_t *value)
+{
+    char *endptr;
+    errno = 0;
+
+    if (env) {
+        if (*env == '-') {
+            logging_error("Negative input not allowed.\n");
+            return false;
+        }
+
+        *value = strtoull(env, &endptr, 10);
+        if (errno == 0 && endptr != env) {
+            while (isspace((unsigned char)*endptr)) {
+                endptr++;
+            }
+
+            if (*endptr == '\0') {
+                return true;
+            }
+        }
+    }
+
+    return false;
+}
+
+static void soc_ring_sentry_log_level_init()
+{
+    setLogLevel();
+}
+
+static void soc_ring_sentry_intensity_delay_init()
+{
+    char *env = getenv("SOC_RING_SENTRY_INTENSITY_DELAY");
+    g_intensity_delay = DEFAULT_INTENSITY_DELAY;
+    uint64_t value;
+
+    if (soc_ring_sentry_envtoull(env, &value)) {
+        g_intensity_delay = value;
+        logging_info("soc_ring_sentry intensity delay set %lums\n", g_intensity_delay);
+        return;
+    }
+
+    logging_warn("Environment variable SOC_RING_SENTRY_INTENSITY_DELAY invalid, using default value %lums\n", g_intensity_delay);
+}
+
+static void soc_ring_sentry_handle_init()
+{
+    char *env = getenv("SOC_RING_SENTRY_FAULT_HANDLING");
+    g_handle = DEFAULT_HANDLE;
+    uint64_t value;
+
+    if (soc_ring_sentry_envtoull(env, &value) && value < HANDLE_LEVEL_INVALID) {
+        g_handle = value;
+        logging_info("soc_ring_sentry handle set %lu\n", g_handle);
+        return;
+    }
+
+    logging_warn("Environment variable SOC_RING_SENTRY_FAULT_HANDLING invalid, using default value %lu\n", g_handle);
+}
+
+static void soc_ring_sentry_mem_size_init()
+{
+    char *env = getenv("SOC_RING_SENTRY_MEM_SIZE");
+    g_mem_size = DEFAULT_MEM_SIZE;
+    uint64_t value;
+
+    if (soc_ring_sentry_envtoull(env, &value)) {
+        if (value != 0 && value < (UINT64_MAX / KB) && (value % LIMITE_MEM_SIZE) == 0) {
+            g_mem_size = value * KB;
+            logging_info("soc_ring_sentry memory size set %luKB\n", value);
+            return;
+        }
+    }
+
+    logging_warn("Environment variable SOC_RING_SENTRY_MEM_SIZE invalid, using default value %luKB\n", g_mem_size / KB);
+}
+
+static void soc_ring_sentry_loop_cnt_init()
+{
+    char *env = getenv("SOC_RING_SENTRY_LOOP_CNT");
+    g_loop_cnt = DEFAULT_LOOP_CNT;
+    uint64_t value;
+
+    if (soc_ring_sentry_envtoull(env, &value)) {
+        g_loop_cnt = value;
+        logging_info("soc_ring_sentry loop cnt set %lu\n", g_loop_cnt);
+        return;
+    }
+
+    logging_warn("Environment variable SOC_RING_SENTRY_LOOP_CNT invalid, using default value %lu\n", g_loop_cnt);
+}
+
+static void soc_ring_sentry_blacklist_init(size_t core_num)
+{
+    char *env = getenv("SOC_RING_SENTRY_BLACKLIST");
+    char *log_buf, *log_end_ptr;
+    size_t log_buf_len, i;
+    int offset;
+
+    g_blacklist = (bool *)calloc(core_num, sizeof(bool));
+    if (!g_blacklist) {
+        logging_error("Failed to allocate memory for blacklist, none CPU set to blacklist\n");
+        return;
+    }
+
+    if (env && strlen(env) > 0) {
+        struct bitmask *cpuset = numa_parse_cpustring(env);
+
+        if (!cpuset) {
+            logging_error("Failed to parse environment variable SOC_RING_SENTRY_BLACKLIST: %s\n", env);
+            return;
+        }
+
+        for (i = 0; i < core_num; i++) {
+            if (numa_bitmask_isbitset(cpuset, i)) {
+                g_blacklist[i] = true;
+            }
+        }
+
+        numa_bitmask_free(cpuset);
+        cpuset = NULL;
+        logging_info("soc_ring_sentry blacklist set successful\n");
+        log_buf_len = strlen("blacklist cores: ") + core_num * 4 + 2;
+        log_buf = (char *)calloc(log_buf_len, sizeof(char));
+        if (log_buf) {
+            offset = snprintf(log_buf, log_buf_len * sizeof(char), "blacklist cores: ");
+            log_end_ptr = log_buf + offset;
+            for (i = 0; i < core_num; i++) {
+                if (g_blacklist[i]) {
+                    offset = snprintf(log_end_ptr, log_buf_len - (log_end_ptr - log_buf), "%ld ", i);
+                    if (offset < 0 || offset >= (log_buf_len - (log_end_ptr - log_buf))) {
+                        logging_error("Log buffer overflow during snprintf\n");
+                        break;
+                    }
+
+                    log_end_ptr += offset;
+                }
+            }
+
+            logging_info("%s\n", log_buf);
+            free(log_buf);
+            log_buf = NULL;
+        }
+    }
+}
+
+static void soc_ring_sentry_init(size_t core_num)
+{
+    soc_ring_sentry_log_level_init();
+    soc_ring_sentry_intensity_delay_init();
+    soc_ring_sentry_handle_init();
+    soc_ring_sentry_mem_size_init();
+    soc_ring_sentry_loop_cnt_init();
+    soc_ring_sentry_blacklist_init(core_num);
+}
+
+static int soc_ring_sentry_delivery(size_t core_num)
+{
+    //todo: add delivery test
+    return 0;
+}
+
+size_t get_system_core_num(void)
+{
+    long core_num = sysconf(_SC_NPROCESSORS_CONF);
+
+    return (core_num > 0) ? (size_t)core_num : 1;
+}
+
+static void soc_ring_sentry_exec()
+{
+    size_t core_num = get_system_core_num();
+    int ret;
+
+    soc_ring_sentry_init(core_num);
+    ret = soc_ring_sentry_delivery(core_num);
+    if (ret == 0) {
+        report_result(TOOL_NAME, RESULT_LEVEL_PASS, "{\"msg\":\"SOC STL test pass\", \"code\":1001}");
+    }
+
+    if (g_blacklist) {
+        free(g_blacklist);
+        g_blacklist = NULL;
+    }
+}
+
+int main(int argc, char *argv[])
+{
+    int opt;
+
+    if (argc > 2) {
+        print_opts_help();
+        return -1;
+    } else if (argc == 2) {
+        while ((opt = getopt(argc, argv, "hg")) != -1) {
+            switch ((char)opt) {
+                case 'h':
+                    print_opts_help();
+                    return 0;
+                case 'g':
+                    soc_ring_sentry_case_get();
+                    return 0;
+                default:
+                    print_opts_help();
+                    return -1;
+            }
+        }
+    } else {
+        soc_ring_sentry_exec();
+    }
+
+    return 0;
+}
+
diff --git a/src/sentryPlugins/soc_ring_sentry/soc_ring_sentry.h b/src/sentryPlugins/soc_ring_sentry/soc_ring_sentry.h
new file mode 100644
index 0000000..bea991f
--- /dev/null
+++ b/src/sentryPlugins/soc_ring_sentry/soc_ring_sentry.h
@@ -0,0 +1,27 @@
+/*
+ * Copyright (c) Huawei Technologies Co., Ltd. 2025. All rights reserved.
+ * Description: SOC Ring sentry main header
+ * Author: Yihang Li
+ * Create: 2025-7-10
+ */
+
+#ifndef __SOC_RING_SENTRY_H
+#define __SOC_RING_SENTRY_H
+
+#define TOOL_NAME "soc_ring_sentry"
+
+enum handle_level {
+    HANDLE_NONE,
+    HANDLE_PANIC,
+    HANDLE_POWEROFF,
+    HANDLE_REBOOT,
+    HANDLE_LEVEL_INVALID
+};
+
+extern uint64_t g_intensity_delay;
+extern uint64_t g_handle;
+extern uint64_t g_mem_size;
+extern uint64_t g_loop_cnt;
+extern bool *g_blacklist;
+
+#endif
-- 
2.34.1