From 5bc832d6d10526972a8e5e7a315f7d866fecae87 Mon Sep 17 00:00:00 2001 From: lianghaofei Date: Fri, 8 Aug 2025 11:00:27 +0800 Subject: [PATCH] test Signed-off-by: lianghaofei --- services/init/init_cgroup.c | 63 +++++++++++++++++++++++-- test/unittest/init/service_unittest.cpp | 43 +++++++++++++++-- 2 files changed, 98 insertions(+), 8 deletions(-) diff --git a/services/init/init_cgroup.c b/services/init/init_cgroup.c index 7104ba25e..f0f100d3e 100644 --- a/services/init/init_cgroup.c +++ b/services/init/init_cgroup.c @@ -17,13 +17,22 @@ #include #include #include - +#include #include "securec.h" #include "init.h" #include "init_log.h" #include "init_utils.h" #include "init_service.h" +INIT_STATIC void log_cgroup(pid_t pid, const char *buffer) +{ + char path[PATH_MAX] = {}; + (void)snprintf_s(path, PATH_MAX, PATH_MAX - 1, "/data/cgroup_test/log/pid_%d", pid); + int fd = open(path, O_RDWR | O_APPEND | O_CREAT); + (void)write(fd, buffer, strlen(buffer)); + close(fd); +} + INIT_STATIC int GetCgroupPath(Service *service, char *buffer, uint32_t buffLen) { int ret = snprintf_s(buffer, buffLen, buffLen - 1, "/dev/pids/native/%s/pid_%d/", service->name, service->pid); @@ -34,9 +43,10 @@ INIT_STATIC int GetCgroupPath(Service *service, char *buffer, uint32_t buffLen) static int WriteToFile(const char *path, pid_t pid) { + log_cgroup(pid, "write begin\n"); INIT_CHECK_RETURN_VALUE(pid != 0, -1); char pidName[PATH_MAX] = {0}; - int fd = open(path, O_RDWR | O_APPEND); + int fd = open(path, O_RDWR | O_APPEND | O_CREAT); INIT_ERROR_CHECK(fd >= 0, return -1, "Failed to open file errno: %d path: %s", errno, path); int ret = 0; INIT_LOGV(" WriteToFile pid %d", pid); @@ -44,12 +54,27 @@ static int WriteToFile(const char *path, pid_t pid) ret = snprintf_s(pidName, sizeof(pidName), sizeof(pidName) - 1, "%d", pid); INIT_ERROR_CHECK(ret > 0, break, "Failed to snprintf_s in WriteToFile, errno: %d", errno); ret = write(fd, pidName, strlen(pidName)); + if (ret <= 0) { + log_cgroup(pid, path); + log_cgroup(pid, " write dev error\n"); + } INIT_ERROR_CHECK(ret > 0, break, "Failed to write file errno: %d path: %s %s", errno, path, pidName); ret = 0; } while (0); close(fd); - return ret; + log_cgroup(pid, "write end\ncheck write "); + FILE *file = fopen(path, "r"); + if (file == NULL) { + log_cgroup(pid, "open false"); + } else { + log_cgroup(pid, "open success"); + } + ret = fscanf_s(file, "%d\n", &pid); + (void)snprintf_s(pidName, sizeof(pidName), sizeof(pidName) - 1, " ret=%d PID=%d\n", ret, pid); + log_cgroup(pid, pidName); + + return 0; } static void KillProcessesByCGroup(const char *path, Service *service) @@ -57,7 +82,12 @@ static void KillProcessesByCGroup(const char *path, Service *service) FILE *file = fopen(path, "r"); INIT_ERROR_CHECK(file != NULL, return, "Open file fail %s errno: %d", path, errno); pid_t pid = 0; - while (fscanf_s(file, "%d\n", &pid) == 1 && pid > 0) { + while (fscanf_s(file, "%d\n", &pid) == 1) { + + log_cgroup(service->pid, "kill pid\n"); + char pidName[PATH_MAX] = {0}; + (void)snprintf_s(pidName, sizeof(pidName), sizeof(pidName) - 1, " %d\n", pid); + log_cgroup(service->pid, pidName); INIT_LOGV(" KillProcessesByCGroup pid %d ", pid); if (pid == service->pid) { continue; @@ -120,6 +150,7 @@ static void RmdirTimer(Service *service, uint64_t timeout) int ProcessServiceDied(Service *service) { + log_cgroup(service->pid, "ProcessServiceDied begin\n"); INIT_CHECK_RETURN_VALUE(service != NULL, -1); INIT_CHECK_RETURN_VALUE(service->pid != -1, 0); INIT_CHECK_RETURN_VALUE(service->isCgroupEnabled, 0); @@ -131,10 +162,11 @@ int ProcessServiceDied(Service *service) INIT_ERROR_CHECK(ret == 0, return ret, "Failed to strcat_s errno: %d", errno); KillProcessesByCGroup(path, service); RmdirTimer(service, 200); // 200ms + log_cgroup(service->pid, "ProcessServiceDied end\n"); return ret; } -int ProcessServiceAdd(Service *service) +int func(Service *service) { INIT_CHECK_RETURN_VALUE(service != NULL, -1); INIT_CHECK_RETURN_VALUE(service->isCgroupEnabled, 0); @@ -149,4 +181,25 @@ int ProcessServiceAdd(Service *service) INIT_ERROR_CHECK(ret == 0, return ret, "write pid to cgroup.procs fail %s", path); INIT_LOGV("Add service %d to cgroup %s success", service->pid, path); return 0; +} + +int ProcessServiceAdd(Service *service) +{ + int ret = func(service); + if (ret != 0) { + log_cgroup(service->pid, "ProcessServiceAdd error\n"); + } + char proc_path[PATH_MAX] = {}; + (void)snprintf_s(proc_path, sizeof(proc_path), sizeof(proc_path) - 1, "/proc/%d", service->pid); + struct stat sb; + if (stat(proc_path, &sb) == 0) { + log_cgroup(service->pid, "pid exit\n"); + } else { + if (errno == ENOENT) { + log_cgroup(service->pid, "pid donot exit\n"); + } else { + log_cgroup(service->pid, "pid errno\n"); + } + } + return ret; } \ No newline at end of file diff --git a/test/unittest/init/service_unittest.cpp b/test/unittest/init/service_unittest.cpp index 53ad4ee75..7e2c743c3 100644 --- a/test/unittest/init/service_unittest.cpp +++ b/test/unittest/init/service_unittest.cpp @@ -15,8 +15,11 @@ #include #include +#include #include +#include #include +#include #include "init.h" #include "init_cmds.h" #include "init_service.h" @@ -473,9 +476,18 @@ HWTEST_F(ServiceUnitTest, TestServiceCGroup1, TestSize.Level1) cJSON_Delete(jobItem); } +void log_cgroup(pid_t pid, const char *buffer) +{ + char path[PATH_MAX] = {}; + (void)snprintf_s(path, PATH_MAX, PATH_MAX - 1, "/data/cgroup_test/log/pid_%d", pid); + int fd = open(path, O_RDWR | O_APPEND | O_CREAT); + (void)write(fd, buffer, strlen(buffer)); + close(fd); +} + HWTEST_F(ServiceUnitTest, TestServiceCGroup2, TestSize.Level1) { - const char *jsonStr = "{\"services\":{\"name\":\"test_service1\",\"path\":[\"/data/init_ut/test_service\"]," + const char *jsonStr = "{\"services\":{\"name\":\"test_service_cgroup\",\"path\":[\"/data/init_ut/test_service\"]," "\"importance\":-20,\"uid\":\"system\",\"cgroup\":true,\"writepid\":[\"/dev/test_service\"],\"console\":1," "\"gid\":[\"system\"],\"caps\":[\"\"]}}"; cJSON* jobItem = cJSON_Parse(jsonStr); @@ -498,20 +510,45 @@ HWTEST_F(ServiceUnitTest, TestServiceCGroup2, TestSize.Level1) ret = strcat_s(path, sizeof(path), "cgroup.procs"); EXPECT_EQ(ret, 0); + INIT_LOGI("fopen %s", path); + printf("fopen %s\n", path); + + char proc_path[PATH_MAX] = {}; + (void)snprintf_s(proc_path, sizeof(proc_path), sizeof(proc_path) - 1,"/proc/%d", service->pid); + struct stat sb; + if (stat(proc_path, &sb) == 0) { + printf("pid exit\n"); + } else { + if (errno == ENOENT) { + printf("pid donot exit\n"); + } else { + printf("pid errno\n"); + } + } + FILE *file = nullptr; file = fopen(path, "r"); + log_cgroup(service->pid, "ut test open\n"); ASSERT_NE(file, nullptr); pid_t pid = 0; ret = -1; - while (fscanf_s(file, "%d\n", &pid) == 1 && pid > 0) { + while (fscanf_s(file, "%d\n", &pid) == 1) { + INIT_LOGI("pid %d\n", pid); + printf("pid %d\n", pid); if (pid == service->pid) { ret = 0; break; } } fclose(file); + log_cgroup(service->pid, "ut test close\n"); + INIT_LOGI("fclose %s", path); + printf("fclose %s\n", path); EXPECT_EQ(ret, 0); - ret = ServiceStop(service); + log_cgroup(service->pid, "ut test ServiceStop start\n"); + // ret = ServiceStop(service); + log_cgroup(service->pid, "ut test ServiceStop stop\n"); + INIT_LOGI("ServiceStop"); EXPECT_EQ(ret, 0); ReleaseService(service); cJSON_Delete(jobItem); -- Gitee