Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/libstore/unix/build/linux-derivation-builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -226,18 +226,18 @@ struct ChrootLinuxDerivationBuilder : ChrootDerivationBuilder, LinuxDerivationBu
/* If we're running from the daemon, then this will return the
root cgroup of the service. Otherwise, it will return the
current cgroup. */
auto rootCgroup = getRootCgroup();
auto cgroupFS = getCgroupFS();
if (!cgroupFS)
throw Error("cannot determine the cgroups file system");
auto rootCgroupPath = canonPath((*cgroupFS / rootCgroup).native());
auto rootCgroupPath = *cgroupFS / getRootCgroup().rel();
if (!pathExists(rootCgroupPath))
throw Error("expected cgroup directory '%s'", rootCgroupPath);

static std::atomic<unsigned int> counter{0};

cgroup = buildUser ? fmt("%s/nix-build-uid-%d", rootCgroupPath, buildUser->getUID())
: fmt("%s/nix-build-pid-%d-%d", rootCgroupPath, getpid(), counter++);
cgroup = rootCgroupPath
/ (buildUser ? fmt("nix-build-uid-%d", buildUser->getUID())
: fmt("nix-build-pid-%d-%d", getpid(), counter++));

debug("using cgroup %s", *cgroup);

Expand All @@ -248,7 +248,7 @@ struct ChrootLinuxDerivationBuilder : ChrootDerivationBuilder, LinuxDerivationBu
auto cgroupsDir = std::filesystem::path{settings.nixStateDir} / "cgroups";
createDirs(cgroupsDir);

auto cgroupFile = fmt("%s/%d", cgroupsDir, buildUser->getUID());
auto cgroupFile = cgroupsDir / std::to_string(buildUser->getUID());

if (pathExists(cgroupFile)) {
auto prevCgroup = readFile(cgroupFile);
Expand Down
2 changes: 1 addition & 1 deletion src/libutil/current-process.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ unsigned int getMaxCPU()
if (!cgroupFS)
return 0;

auto cpuFile = *cgroupFS + "/" + getCurrentCgroup() + "/cpu.max";
auto cpuFile = *cgroupFS / getCurrentCgroup().rel() / "cpu.max";

auto cpuMax = readFile(cpuFile);
auto cpuMaxParts = tokenizeString<std::vector<std::string>>(cpuMax, " \n");
Expand Down
8 changes: 4 additions & 4 deletions src/libutil/linux/cgroup.cc
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ CgroupStats destroyCgroup(const std::filesystem::path & cgroup)
return destroyCgroup(cgroup, true);
}

std::string getCurrentCgroup()
CanonPath getCurrentCgroup()
{
auto cgroupFS = getCgroupFS();
if (!cgroupFS)
Expand All @@ -165,12 +165,12 @@ std::string getCurrentCgroup()
auto ourCgroup = ourCgroups[""];
if (ourCgroup == "")
throw Error("cannot determine cgroup name from /proc/self/cgroup");
return ourCgroup;
return CanonPath{ourCgroup};
}

std::string getRootCgroup()
CanonPath getRootCgroup()
{
static std::string rootCgroup = getCurrentCgroup();
static auto rootCgroup = getCurrentCgroup();
return rootCgroup;
}

Expand Down
5 changes: 3 additions & 2 deletions src/libutil/linux/include/nix/util/cgroup.hh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <filesystem>

#include "nix/util/types.hh"
#include "nix/util/canon-path.hh"

namespace nix {

Expand All @@ -31,13 +32,13 @@ CgroupStats getCgroupStats(const std::filesystem::path & cgroup);
*/
CgroupStats destroyCgroup(const std::filesystem::path & cgroup);

std::string getCurrentCgroup();
CanonPath getCurrentCgroup();

/**
* Get the cgroup that should be used as the parent when creating new
* sub-cgroups. The first time this is called, the current cgroup will be
* returned, and then all subsequent calls will return the original cgroup.
*/
std::string getRootCgroup();
CanonPath getRootCgroup();

} // namespace nix
2 changes: 1 addition & 1 deletion src/nix/unix/daemon.cc
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ static void daemonLoop(std::optional<TrustedFlag> forceTrustClientOpt)
auto cgroupFS = getCgroupFS();
if (!cgroupFS)
throw Error("cannot determine the cgroups file system");
auto rootCgroupPath = canonPath(*cgroupFS + "/" + rootCgroup);
auto rootCgroupPath = *cgroupFS / rootCgroup.rel();
if (!pathExists(rootCgroupPath))
throw Error("expected cgroup directory '%s'", rootCgroupPath);
auto daemonCgroupPath = rootCgroupPath + "/nix-daemon";
Expand Down
Loading