diff --git a/WebKitBrowser/LoggingUtils.cpp b/WebKitBrowser/LoggingUtils.cpp index 65fa91eea..d17a5e415 100644 --- a/WebKitBrowser/LoggingUtils.cpp +++ b/WebKitBrowser/LoggingUtils.cpp @@ -38,6 +38,7 @@ bool RedirectAllLogsToService(const string& target_service) fprintf(stderr, "RedirectLog: Invalid argument. Logs target service expected\n"); return false; } + const string kServiceExt = ".service"; if (target_service.size() <= kServiceExt.size() || target_service.substr(target_service.size() - kServiceExt.size(), kServiceExt.size()) != kServiceExt) { @@ -45,10 +46,15 @@ bool RedirectAllLogsToService(const string& target_service) return false; } + const bool isCGroupV2 = g_file_test("/sys/fs/cgroup/unified", G_FILE_TEST_EXISTS); + const string targetHierarchy = isCGroupV2 ? "unified" : "systemd"; + const string targetPIDList = isCGroupV2 ? "cgroup.procs" : "tasks"; + const string targetServiceName = target_service.substr(0, target_service.size() - kServiceExt.size()); - const string kSystemdCgroupTargetTasksFilePath = "/sys/fs/cgroup/systemd/system.slice/" + target_service + "/tasks"; + const string kSystemdCgroupTargetTasksFilePath = "/sys/fs/cgroup/" + targetHierarchy + "/system.slice/" + target_service + "/" + targetPIDList; + if (!g_file_test(kSystemdCgroupTargetTasksFilePath.c_str(), G_FILE_TEST_EXISTS)) { - fprintf(stderr, "RedirectLog: %s unit cgroup doesn't exist\n", target_service.c_str()); + fprintf(stderr, "RedirectLog: %s doesn't exist for unit %s\n", kSystemdCgroupTargetTasksFilePath.c_str(), target_service.c_str()); return false; } @@ -57,10 +63,17 @@ bool RedirectAllLogsToService(const string& target_service) while (taskDir.Next() == true) { if (taskDir.Name() == "." || taskDir.Name() == "..") continue; + FILE* f = fopen(kSystemdCgroupTargetTasksFilePath.c_str(), "a"); if (f) { fprintf(f, "%s", taskDir.Name().c_str()); fclose(f); + + // In case of cgroup v2, it is enough to move just one task/thread + // to achieve migration of entire process + if (isCGroupV2) + break; + } else { fprintf(stderr, "RedirectLog: cannot move %s thread to '%s': %s\n", taskDir.Name().c_str(), kSystemdCgroupTargetTasksFilePath.c_str(), strerror(errno));