Skip to content

Commit

Permalink
Update LoggingUtils to work on cgroup v2 as well
Browse files Browse the repository at this point in the history
  • Loading branch information
varumugam123 committed Apr 3, 2024
1 parent 9aa6c40 commit 0bb14d3
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions WebKitBrowser/LoggingUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,23 @@ 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) {
fprintf(stderr, "RedirectLog: Invalid serivce name: *.service format expected\n");
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;
}

Expand All @@ -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));
Expand Down

0 comments on commit 0bb14d3

Please sign in to comment.