From a3f37d87eabcfb5dc581abcfa46e5e7d387dfa8c Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Thu, 14 Mar 2019 13:27:16 +0100 Subject: [PATCH] findRuntimeRoots: Simplify/fix handling of /proc files Scanning of /proc//{exe,cwd} was broken because '{memory:' was prepended twice. Also, get rid of the whole '{memory:...}' thing because it's unnecessary, we can just list the file in /proc directly. --- src/libstore/gc.cc | 26 ++++++++++++-------------- src/libstore/store-api.hh | 3 ++- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/src/libstore/gc.cc b/src/libstore/gc.cc index 2a3140354..8c3607f66 100644 --- a/src/libstore/gc.cc +++ b/src/libstore/gc.cc @@ -362,8 +362,7 @@ try_again: } if (res > 0 && buf[0] == '/') roots[std::string(static_cast(buf), res)] - .emplace((format("{memory:%1%") % file).str()); - return; + .emplace(file); } static string quoteRegexChars(const string & raw) @@ -395,10 +394,10 @@ void LocalStore::findRuntimeRoots(Roots & roots) while (errno = 0, ent = readdir(procDir.get())) { checkInterrupt(); if (std::regex_match(ent->d_name, digitsRegex)) { - readProcLink((format("{memory:/proc/%1%/exe}") % ent->d_name).str(), unchecked); - readProcLink((format("{memory:/proc/%1%/cwd}") % ent->d_name).str(), unchecked); + readProcLink(fmt("/proc/%s/exe" ,ent->d_name), unchecked); + readProcLink(fmt("/proc/%s/cwd", ent->d_name), unchecked); - auto fdStr = (format("/proc/%1%/fd") % ent->d_name).str(); + auto fdStr = fmt("/proc/%s/fd", ent->d_name); auto fdDir = AutoCloseDir(opendir(fdStr.c_str())); if (!fdDir) { if (errno == ENOENT || errno == EACCES) @@ -407,9 +406,8 @@ void LocalStore::findRuntimeRoots(Roots & roots) } struct dirent * fd_ent; while (errno = 0, fd_ent = readdir(fdDir.get())) { - if (fd_ent->d_name[0] != '.') { - readProcLink((format("%1%/%2%") % fdStr % fd_ent->d_name).str(), unchecked); - } + if (fd_ent->d_name[0] != '.') + readProcLink(fmt("%s/%s", fdStr, fd_ent->d_name), unchecked); } if (errno) { if (errno == ESRCH) @@ -419,19 +417,19 @@ void LocalStore::findRuntimeRoots(Roots & roots) fdDir.reset(); try { - auto mapFile = (format("/proc/%1%/maps") % ent->d_name).str(); + auto mapFile = fmt("/proc/%s/maps", ent->d_name); auto mapLines = tokenizeString>(readFile(mapFile, true), "\n"); - for (const auto& line : mapLines) { + for (const auto & line : mapLines) { auto match = std::smatch{}; if (std::regex_match(line, match, mapRegex)) - unchecked[match[1]].emplace((format("{memory:%1%}") % mapFile).str()); + unchecked[match[1]].emplace(mapFile); } - auto envFile = (format("/proc/%1%/environ") % ent->d_name).str(); + auto envFile = fmt("/proc/%s/environ", ent->d_name); auto envString = readFile(envFile, true); auto env_end = std::sregex_iterator{}; for (auto i = std::sregex_iterator{envString.begin(), envString.end(), storePathRegex}; i != env_end; ++i) - unchecked[i->str()].emplace((format("{memory:%1%}") % envFile).str()); + unchecked[i->str()].emplace(envFile); } catch (SysError & e) { if (errno == ENOENT || errno == EACCES || errno == ESRCH) continue; @@ -451,7 +449,7 @@ void LocalStore::findRuntimeRoots(Roots & roots) for (const auto & line : lsofLines) { std::smatch match; if (std::regex_match(line, match, lsofRegex)) - unchecked[match[1]].emplace((format("{memory:%1%}" % LSOF).str()); + unchecked[match[1]].emplace("{lsof}"); } } catch (ExecError & e) { /* lsof not installed, lsof failed */ diff --git a/src/libstore/store-api.hh b/src/libstore/store-api.hh index b4e5f5511..bb1665976 100644 --- a/src/libstore/store-api.hh +++ b/src/libstore/store-api.hh @@ -11,6 +11,7 @@ #include #include #include +#include #include #include @@ -47,7 +48,7 @@ const size_t storePathHashLen = 32; // i.e. 160 bits const uint32_t exportMagic = 0x4558494e; -typedef std::map> Roots; +typedef std::unordered_map> Roots; struct GCOptions