diff --git a/services/init/init_cgroup.c b/services/init/init_cgroup.c index 7104ba25ecfba1858b8fba9174d40241f1b0519e..5038cdfcb68166606b3322044e57f1aa3740f59b 100644 --- a/services/init/init_cgroup.c +++ b/services/init/init_cgroup.c @@ -24,9 +24,25 @@ #include "init_utils.h" #include "init_service.h" +INIT_STATIC int __GetCgroupPath(Service *service, char *buffer, uint32_t buffLen) +{ + int ret = snprintf_s(buffer, buffLen, buffLen - 1, "/data/cgroup_test/rm/%s/pid_%d/", service->name, service->pid); + INIT_ERROR_CHECK(ret > 0, return ret, "Failed to snprintf_s in GetCgroupPath, errno: %d", errno); + INIT_LOGV("Cgroup path %s ", buffer); + return 0; +} + +INIT_STATIC int _GetCgroupPath(Service *service, char *buffer, uint32_t buffLen) +{ + int ret = snprintf_s(buffer, buffLen, buffLen - 1, "/data/cgroup_test/noR/%s/pid_%d/", service->name, service->pid); + INIT_ERROR_CHECK(ret > 0, return ret, "Failed to snprintf_s in GetCgroupPath, errno: %d", errno); + INIT_LOGV("Cgroup path %s ", buffer); + return 0; +} + 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); + int ret = snprintf_s(buffer, buffLen, buffLen - 1, "/data/cgroup_test/%s/pid_%d/", service->name, service->pid); INIT_ERROR_CHECK(ret > 0, return ret, "Failed to snprintf_s in GetCgroupPath, errno: %d", errno); INIT_LOGV("Cgroup path %s ", buffer); return 0; @@ -36,7 +52,7 @@ static int WriteToFile(const char *path, pid_t pid) { 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); @@ -131,6 +147,13 @@ 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 + + ret = __GetCgroupPath(service, path, sizeof(path)); + INIT_ERROR_CHECK(ret == 0, return -1, "Failed to get real path errno: %d", errno); + (void)MakeDirRecursive(path, 0755); // 0755 default mode + ret = strcat_s(path, sizeof(path), "cgroup.procs"); + INIT_ERROR_CHECK(ret == 0, return ret, "Failed to strcat_s errno: %d", errno); + ret = WriteToFile(path, service->pid); return ret; } @@ -148,5 +171,14 @@ int ProcessServiceAdd(Service *service) ret = WriteToFile(path, service->pid); 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); + // test + ret = _GetCgroupPath(service, path, sizeof(path)); + INIT_ERROR_CHECK(ret == 0, return -1, "Failed to get real path errno: %d", errno); + (void)MakeDirRecursive(path, 0755); // 0755 default mode + ret = strcat_s(path, sizeof(path), "cgroup.procs"); + INIT_ERROR_CHECK(ret == 0, return ret, "Failed to strcat_s errno: %d", errno); + ret = WriteToFile(path, service->pid); + 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; } \ No newline at end of file diff --git a/test/unittest/init/service_unittest.cpp b/test/unittest/init/service_unittest.cpp index 53ad4ee7572683388c6ad7814848ea0b0ec3796e..90a1918bc2ac558e6bc477c481cb6ec61f4216ed 100644 --- a/test/unittest/init/service_unittest.cpp +++ b/test/unittest/init/service_unittest.cpp @@ -475,7 +475,7 @@ HWTEST_F(ServiceUnitTest, TestServiceCGroup1, TestSize.Level1) 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 +498,27 @@ 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); FILE *file = nullptr; file = fopen(path, "r"); 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); + INIT_LOGI("fclose %s", path); + printf("fclose %s\n", path); EXPECT_EQ(ret, 0); ret = ServiceStop(service); + INIT_LOGI("ServiceStop"); EXPECT_EQ(ret, 0); ReleaseService(service); cJSON_Delete(jobItem);