Eliminate the "store" global variable
Also, move a few free-standing functions into StoreAPI and Derivation.
Also, introduce a non-nullable smart pointer, ref<T>, which is just a
wrapper around std::shared_ptr ensuring that the pointer is never
null. (For reference-counted values, this is better than passing a
"T&", because the latter doesn't maintain the refcount. Usually, the
caller will have a shared_ptr keeping the value alive, but that's not
always the case, e.g., when passing a reference to a std::thread via
std::bind.)
2016-02-04 13:28:26 +00:00
|
|
|
#include "derivations.hh"
|
2006-09-04 21:06:23 +00:00
|
|
|
#include "globals.hh"
|
2006-11-30 17:43:04 +00:00
|
|
|
#include "local-store.hh"
|
2020-10-09 20:18:08 +00:00
|
|
|
#include "local-fs-store.hh"
|
2017-09-05 18:43:42 +00:00
|
|
|
#include "finally.hh"
|
2005-01-31 10:27:25 +00:00
|
|
|
|
2008-09-17 10:02:55 +00:00
|
|
|
#include <functional>
|
|
|
|
#include <queue>
|
2009-11-24 09:53:18 +00:00
|
|
|
#include <algorithm>
|
2016-07-20 18:00:36 +00:00
|
|
|
#include <regex>
|
2018-06-13 14:56:19 +00:00
|
|
|
#include <random>
|
2008-09-17 10:02:55 +00:00
|
|
|
|
2021-08-16 18:03:32 +00:00
|
|
|
#include <climits>
|
2005-01-31 10:27:25 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2021-08-16 18:03:32 +00:00
|
|
|
#include <poll.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/statvfs.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/un.h>
|
2004-08-25 16:54:08 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2006-09-04 21:06:23 +00:00
|
|
|
namespace nix {
|
|
|
|
|
|
|
|
|
2021-08-16 18:03:32 +00:00
|
|
|
static std::string gcSocketPath = "/gc-socket/socket";
|
|
|
|
static std::string gcRootsDir = "gcroots";
|
2005-01-31 22:01:55 +00:00
|
|
|
|
|
|
|
|
2014-02-27 22:17:53 +00:00
|
|
|
static void makeSymlink(const Path & link, const Path & target)
|
2005-02-01 13:48:46 +00:00
|
|
|
{
|
|
|
|
/* Create directories up to `gcRoot'. */
|
|
|
|
createDirs(dirOf(link));
|
|
|
|
|
2012-04-16 16:47:01 +00:00
|
|
|
/* Create the new symlink. */
|
|
|
|
Path tempLink = (format("%1%.tmp-%2%-%3%")
|
2018-03-06 23:34:44 +00:00
|
|
|
% link % getpid() % random()).str();
|
2014-02-27 22:17:53 +00:00
|
|
|
createSymlink(target, tempLink);
|
2012-04-16 16:47:01 +00:00
|
|
|
|
|
|
|
/* Atomically replace the old one. */
|
|
|
|
if (rename(tempLink.c_str(), link.c_str()) == -1)
|
2020-04-21 23:07:07 +00:00
|
|
|
throw SysError("cannot rename '%1%' to '%2%'",
|
|
|
|
tempLink , link);
|
2005-02-01 13:48:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-12-04 23:29:16 +00:00
|
|
|
void LocalStore::addIndirectRoot(const Path & path)
|
|
|
|
{
|
2022-02-25 15:00:00 +00:00
|
|
|
std::string hash = hashString(htSHA1, path).to_string(Base32, false);
|
|
|
|
Path realRoot = canonPath(fmt("%1%/%2%/auto/%3%", stateDir, gcRootsDir, hash));
|
2014-02-27 22:17:53 +00:00
|
|
|
makeSymlink(realRoot, path);
|
2006-12-04 23:29:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-09-03 09:26:36 +00:00
|
|
|
Path LocalFSStore::addPermRoot(const StorePath & storePath, const Path & _gcRoot)
|
2005-02-01 12:36:25 +00:00
|
|
|
{
|
|
|
|
Path gcRoot(canonPath(_gcRoot));
|
|
|
|
|
2007-06-11 11:36:22 +00:00
|
|
|
if (isInStore(gcRoot))
|
2020-04-21 23:07:07 +00:00
|
|
|
throw Error(
|
2007-06-11 11:36:22 +00:00
|
|
|
"creating a garbage collector root (%1%) in the Nix store is forbidden "
|
2020-04-21 23:07:07 +00:00
|
|
|
"(are you running nix-build inside the store?)", gcRoot);
|
2007-06-11 11:36:22 +00:00
|
|
|
|
2021-08-17 16:53:14 +00:00
|
|
|
/* Register this root with the garbage collector, if it's
|
|
|
|
running. This should be superfluous since the caller should
|
|
|
|
have registered this root yet, but let's be on the safe
|
|
|
|
side. */
|
|
|
|
addTempRoot(storePath);
|
|
|
|
|
2020-09-03 09:26:36 +00:00
|
|
|
/* Don't clobber the link if it already exists and doesn't
|
|
|
|
point to the Nix store. */
|
|
|
|
if (pathExists(gcRoot) && (!isLink(gcRoot) || !isInStore(readLink(gcRoot))))
|
|
|
|
throw Error("cannot create symlink '%1%'; already exists", gcRoot);
|
|
|
|
makeSymlink(gcRoot, printStorePath(storePath));
|
|
|
|
addIndirectRoot(gcRoot);
|
2005-02-01 12:36:25 +00:00
|
|
|
|
|
|
|
return gcRoot;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
void LocalStore::addTempRoot(const StorePath & path)
|
2005-01-31 10:27:25 +00:00
|
|
|
{
|
2016-04-08 16:07:13 +00:00
|
|
|
auto state(_state.lock());
|
|
|
|
|
2005-01-31 10:27:25 +00:00
|
|
|
/* Create the temporary roots file for this process. */
|
2016-07-11 19:44:44 +00:00
|
|
|
if (!state->fdTempRoots) {
|
2005-01-31 10:27:25 +00:00
|
|
|
|
|
|
|
while (1) {
|
2017-09-05 18:39:57 +00:00
|
|
|
if (pathExists(fnTempRoots))
|
2006-06-20 17:48:10 +00:00
|
|
|
/* It *must* be stale, since there can be no two
|
|
|
|
processes with the same pid. */
|
2017-09-05 18:39:57 +00:00
|
|
|
unlink(fnTempRoots.c_str());
|
2006-06-20 17:48:10 +00:00
|
|
|
|
2017-09-05 18:39:57 +00:00
|
|
|
state->fdTempRoots = openLockFile(fnTempRoots, true);
|
2005-01-31 10:27:25 +00:00
|
|
|
|
2021-08-16 18:03:32 +00:00
|
|
|
debug("acquiring write lock on '%s'", fnTempRoots);
|
|
|
|
lockFile(state->fdTempRoots.get(), ltWrite, true);
|
2005-01-31 10:27:25 +00:00
|
|
|
|
|
|
|
/* Check whether the garbage collector didn't get in our
|
|
|
|
way. */
|
|
|
|
struct stat st;
|
2016-07-11 19:44:44 +00:00
|
|
|
if (fstat(state->fdTempRoots.get(), &st) == -1)
|
2020-04-21 23:07:07 +00:00
|
|
|
throw SysError("statting '%1%'", fnTempRoots);
|
2005-01-31 10:27:25 +00:00
|
|
|
if (st.st_size == 0) break;
|
2012-07-30 23:55:41 +00:00
|
|
|
|
2005-01-31 10:27:25 +00:00
|
|
|
/* The garbage collector deleted this file before we could
|
|
|
|
get a lock. (It won't delete the file after we get a
|
|
|
|
lock.) Try again. */
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-08-19 20:43:43 +00:00
|
|
|
if (!state->fdGCLock)
|
|
|
|
state->fdGCLock = openGCLock();
|
|
|
|
|
2021-08-16 18:03:32 +00:00
|
|
|
restart:
|
|
|
|
FdLock gcLock(state->fdGCLock.get(), ltRead, false, "");
|
|
|
|
|
|
|
|
if (!gcLock.acquired) {
|
|
|
|
/* We couldn't get a shared global GC lock, so the garbage
|
|
|
|
collector is running. So we have to connect to the garbage
|
|
|
|
collector and inform it about our root. */
|
|
|
|
if (!state->fdRootsSocket) {
|
|
|
|
auto socketPath = stateDir.get() + gcSocketPath;
|
|
|
|
debug("connecting to '%s'", socketPath);
|
2021-10-28 20:14:47 +00:00
|
|
|
state->fdRootsSocket = createUnixDomainSocket();
|
2021-12-13 15:14:57 +00:00
|
|
|
try {
|
|
|
|
nix::connect(state->fdRootsSocket.get(), socketPath);
|
|
|
|
} catch (SysError & e) {
|
|
|
|
/* The garbage collector may have exited, so we need to
|
|
|
|
restart. */
|
|
|
|
if (e.errNo == ECONNREFUSED) {
|
|
|
|
debug("GC socket connection refused");
|
|
|
|
state->fdRootsSocket.close();
|
|
|
|
goto restart;
|
|
|
|
}
|
|
|
|
}
|
2021-08-16 18:03:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
debug("sending GC root '%s'", printStorePath(path));
|
|
|
|
writeFull(state->fdRootsSocket.get(), printStorePath(path) + "\n", false);
|
|
|
|
char c;
|
|
|
|
readFull(state->fdRootsSocket.get(), &c, 1);
|
|
|
|
assert(c == '1');
|
|
|
|
debug("got ack for GC root '%s'", printStorePath(path));
|
|
|
|
} catch (SysError & e) {
|
|
|
|
/* The garbage collector may have exited, so we need to
|
|
|
|
restart. */
|
|
|
|
if (e.errNo == EPIPE) {
|
|
|
|
debug("GC socket disconnected");
|
|
|
|
state->fdRootsSocket.close();
|
|
|
|
goto restart;
|
|
|
|
}
|
|
|
|
} catch (EndOfFile & e) {
|
|
|
|
debug("GC socket disconnected");
|
|
|
|
state->fdRootsSocket.close();
|
|
|
|
goto restart;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Append the store path to the temporary roots file. */
|
2022-02-25 15:00:00 +00:00
|
|
|
auto s = printStorePath(path) + '\0';
|
2016-07-11 19:44:44 +00:00
|
|
|
writeFull(state->fdTempRoots.get(), s);
|
2005-01-31 10:27:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-14 12:50:07 +00:00
|
|
|
static std::string censored = "{censored}";
|
|
|
|
|
|
|
|
|
2021-08-16 18:03:32 +00:00
|
|
|
void LocalStore::findTempRoots(Roots & tempRoots, bool censor)
|
2005-01-31 10:27:25 +00:00
|
|
|
{
|
|
|
|
/* Read the `temproots' directory for per-process temporary root
|
|
|
|
files. */
|
2017-09-14 13:02:52 +00:00
|
|
|
for (auto & i : readDirectory(tempRootsDir)) {
|
2020-04-09 15:05:29 +00:00
|
|
|
if (i.name[0] == '.') {
|
|
|
|
// Ignore hidden files. Some package managers (notably portage) create
|
|
|
|
// those to keep the directory alive.
|
|
|
|
continue;
|
|
|
|
}
|
2017-09-05 18:39:57 +00:00
|
|
|
Path path = tempRootsDir + "/" + i.name;
|
2005-01-31 10:27:25 +00:00
|
|
|
|
2017-09-14 13:02:52 +00:00
|
|
|
pid_t pid = std::stoi(i.name);
|
|
|
|
|
2017-07-30 11:27:57 +00:00
|
|
|
debug(format("reading temporary root file '%1%'") % path);
|
2021-08-16 18:03:32 +00:00
|
|
|
AutoCloseFD fd(open(path.c_str(), O_CLOEXEC | O_RDWR, 0666));
|
|
|
|
if (!fd) {
|
2005-01-31 10:27:25 +00:00
|
|
|
/* It's okay if the file has disappeared. */
|
|
|
|
if (errno == ENOENT) continue;
|
2020-04-21 23:07:07 +00:00
|
|
|
throw SysError("opening temporary roots file '%1%'", path);
|
2005-01-31 10:27:25 +00:00
|
|
|
}
|
|
|
|
|
2019-08-02 16:37:55 +00:00
|
|
|
/* Try to acquire a write lock without blocking. This can
|
|
|
|
only succeed if the owning process has died. In that case
|
|
|
|
we don't care about its temporary roots. */
|
2021-08-16 18:03:32 +00:00
|
|
|
if (lockFile(fd.get(), ltWrite, false)) {
|
2020-05-13 15:52:36 +00:00
|
|
|
printInfo("removing stale temporary roots file '%1%'", path);
|
2019-08-02 16:37:55 +00:00
|
|
|
unlink(path.c_str());
|
2021-08-16 18:03:32 +00:00
|
|
|
writeFull(fd.get(), "d");
|
2019-08-02 16:37:55 +00:00
|
|
|
continue;
|
2017-09-05 18:39:57 +00:00
|
|
|
}
|
2005-01-31 10:27:25 +00:00
|
|
|
|
|
|
|
/* Read the entire file. */
|
2022-02-25 15:00:00 +00:00
|
|
|
auto contents = readFile(fd.get());
|
2005-01-31 10:27:25 +00:00
|
|
|
|
|
|
|
/* Extract the roots. */
|
2022-02-25 15:00:00 +00:00
|
|
|
std::string::size_type pos = 0, end;
|
2005-01-31 10:27:25 +00:00
|
|
|
|
2022-02-25 15:00:00 +00:00
|
|
|
while ((end = contents.find((char) 0, pos)) != std::string::npos) {
|
2005-01-31 10:27:25 +00:00
|
|
|
Path root(contents, pos, end - pos);
|
2017-09-14 13:02:52 +00:00
|
|
|
debug("got temporary root '%s'", root);
|
2019-12-05 18:11:09 +00:00
|
|
|
tempRoots[parseStorePath(root)].emplace(censor ? censored : fmt("{temp:%d}", pid));
|
2005-01-31 10:27:25 +00:00
|
|
|
pos = end + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
Eliminate the "store" global variable
Also, move a few free-standing functions into StoreAPI and Derivation.
Also, introduce a non-nullable smart pointer, ref<T>, which is just a
wrapper around std::shared_ptr ensuring that the pointer is never
null. (For reference-counted values, this is better than passing a
"T&", because the latter doesn't maintain the refcount. Usually, the
caller will have a shared_ptr keeping the value alive, but that's not
always the case, e.g., when passing a reference to a std::thread via
std::bind.)
2016-02-04 13:28:26 +00:00
|
|
|
void LocalStore::findRoots(const Path & path, unsigned char type, Roots & roots)
|
2005-02-01 15:05:32 +00:00
|
|
|
{
|
Eliminate the "store" global variable
Also, move a few free-standing functions into StoreAPI and Derivation.
Also, introduce a non-nullable smart pointer, ref<T>, which is just a
wrapper around std::shared_ptr ensuring that the pointer is never
null. (For reference-counted values, this is better than passing a
"T&", because the latter doesn't maintain the refcount. Usually, the
caller will have a shared_ptr keeping the value alive, but that's not
always the case, e.g., when passing a reference to a std::thread via
std::bind.)
2016-02-04 13:28:26 +00:00
|
|
|
auto foundRoot = [&](const Path & path, const Path & target) {
|
2020-07-13 14:19:37 +00:00
|
|
|
try {
|
|
|
|
auto storePath = toStorePath(target).first;
|
|
|
|
if (isValidPath(storePath))
|
|
|
|
roots[std::move(storePath)].emplace(path);
|
|
|
|
else
|
|
|
|
printInfo("skipping invalid root from '%1%' to '%2%'", path, target);
|
|
|
|
} catch (BadStorePath &) { }
|
Eliminate the "store" global variable
Also, move a few free-standing functions into StoreAPI and Derivation.
Also, introduce a non-nullable smart pointer, ref<T>, which is just a
wrapper around std::shared_ptr ensuring that the pointer is never
null. (For reference-counted values, this is better than passing a
"T&", because the latter doesn't maintain the refcount. Usually, the
caller will have a shared_ptr keeping the value alive, but that's not
always the case, e.g., when passing a reference to a std::thread via
std::bind.)
2016-02-04 13:28:26 +00:00
|
|
|
};
|
2006-12-05 01:31:45 +00:00
|
|
|
|
2013-07-12 12:01:25 +00:00
|
|
|
try {
|
|
|
|
|
2014-10-03 20:37:51 +00:00
|
|
|
if (type == DT_UNKNOWN)
|
|
|
|
type = getFileType(path);
|
2006-12-05 01:31:45 +00:00
|
|
|
|
2014-08-01 15:20:25 +00:00
|
|
|
if (type == DT_DIR) {
|
2014-08-01 14:37:47 +00:00
|
|
|
for (auto & i : readDirectory(path))
|
Eliminate the "store" global variable
Also, move a few free-standing functions into StoreAPI and Derivation.
Also, introduce a non-nullable smart pointer, ref<T>, which is just a
wrapper around std::shared_ptr ensuring that the pointer is never
null. (For reference-counted values, this is better than passing a
"T&", because the latter doesn't maintain the refcount. Usually, the
caller will have a shared_ptr keeping the value alive, but that's not
always the case, e.g., when passing a reference to a std::thread via
std::bind.)
2016-02-04 13:28:26 +00:00
|
|
|
findRoots(path + "/" + i.name, i.type, roots);
|
2005-02-01 15:05:32 +00:00
|
|
|
}
|
|
|
|
|
2014-08-01 15:20:25 +00:00
|
|
|
else if (type == DT_LNK) {
|
2013-07-12 12:01:25 +00:00
|
|
|
Path target = readLink(path);
|
|
|
|
if (isInStore(target))
|
Eliminate the "store" global variable
Also, move a few free-standing functions into StoreAPI and Derivation.
Also, introduce a non-nullable smart pointer, ref<T>, which is just a
wrapper around std::shared_ptr ensuring that the pointer is never
null. (For reference-counted values, this is better than passing a
"T&", because the latter doesn't maintain the refcount. Usually, the
caller will have a shared_ptr keeping the value alive, but that's not
always the case, e.g., when passing a reference to a std::thread via
std::bind.)
2016-02-04 13:28:26 +00:00
|
|
|
foundRoot(path, target);
|
2013-07-12 12:01:25 +00:00
|
|
|
|
|
|
|
/* Handle indirect roots. */
|
|
|
|
else {
|
|
|
|
target = absPath(target, dirOf(path));
|
|
|
|
if (!pathExists(target)) {
|
2016-06-02 11:33:49 +00:00
|
|
|
if (isInDir(path, stateDir + "/" + gcRootsDir + "/auto")) {
|
2017-07-30 11:27:57 +00:00
|
|
|
printInfo(format("removing stale link from '%1%' to '%2%'") % path % target);
|
2013-07-12 12:01:25 +00:00
|
|
|
unlink(path.c_str());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
struct stat st2 = lstat(target);
|
|
|
|
if (!S_ISLNK(st2.st_mode)) return;
|
|
|
|
Path target2 = readLink(target);
|
Eliminate the "store" global variable
Also, move a few free-standing functions into StoreAPI and Derivation.
Also, introduce a non-nullable smart pointer, ref<T>, which is just a
wrapper around std::shared_ptr ensuring that the pointer is never
null. (For reference-counted values, this is better than passing a
"T&", because the latter doesn't maintain the refcount. Usually, the
caller will have a shared_ptr keeping the value alive, but that's not
always the case, e.g., when passing a reference to a std::thread via
std::bind.)
2016-02-04 13:28:26 +00:00
|
|
|
if (isInStore(target2)) foundRoot(target, target2);
|
2006-12-05 01:31:45 +00:00
|
|
|
}
|
2005-02-01 15:05:32 +00:00
|
|
|
}
|
|
|
|
}
|
2006-12-05 01:31:45 +00:00
|
|
|
|
2014-08-01 15:20:25 +00:00
|
|
|
else if (type == DT_REG) {
|
2020-02-28 17:07:10 +00:00
|
|
|
auto storePath = maybeParseStorePath(storeDir + "/" + std::string(baseNameOf(path)));
|
|
|
|
if (storePath && isValidPath(*storePath))
|
|
|
|
roots[std::move(*storePath)].emplace(path);
|
2014-08-01 14:46:01 +00:00
|
|
|
}
|
|
|
|
|
2006-12-05 01:31:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
catch (SysError & e) {
|
|
|
|
/* We only ignore permanent failures. */
|
|
|
|
if (e.errNo == EACCES || e.errNo == ENOENT || e.errNo == ENOTDIR)
|
2019-12-05 18:11:09 +00:00
|
|
|
printInfo("cannot read potential root '%1%'", path);
|
2006-12-05 01:31:45 +00:00
|
|
|
else
|
|
|
|
throw;
|
2005-02-01 15:05:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-14 12:50:07 +00:00
|
|
|
void LocalStore::findRootsNoTemp(Roots & roots, bool censor)
|
2006-12-05 00:34:42 +00:00
|
|
|
{
|
2018-02-01 09:39:16 +00:00
|
|
|
/* Process direct roots in {gcroots,profiles}. */
|
2016-06-02 11:33:49 +00:00
|
|
|
findRoots(stateDir + "/" + gcRootsDir, DT_UNKNOWN, roots);
|
|
|
|
findRoots(stateDir + "/profiles", DT_UNKNOWN, roots);
|
2006-12-05 01:31:45 +00:00
|
|
|
|
2019-05-01 02:43:24 +00:00
|
|
|
/* Add additional roots returned by different platforms-specific
|
|
|
|
heuristics. This is typically used to add running programs to
|
|
|
|
the set of roots (to prevent them from being garbage collected). */
|
2019-03-14 12:50:07 +00:00
|
|
|
findRuntimeRoots(roots, censor);
|
2017-09-14 12:38:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-14 12:50:07 +00:00
|
|
|
Roots LocalStore::findRoots(bool censor)
|
2017-09-14 12:38:36 +00:00
|
|
|
{
|
2019-03-09 23:37:52 +00:00
|
|
|
Roots roots;
|
2019-03-14 12:50:07 +00:00
|
|
|
findRootsNoTemp(roots, censor);
|
2017-09-14 12:38:36 +00:00
|
|
|
|
2021-08-16 18:03:32 +00:00
|
|
|
findTempRoots(roots, censor);
|
2017-09-14 12:38:36 +00:00
|
|
|
|
2013-07-12 12:01:25 +00:00
|
|
|
return roots;
|
2006-12-05 00:34:42 +00:00
|
|
|
}
|
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
typedef std::unordered_map<Path, std::unordered_set<std::string>> UncheckedRoots;
|
|
|
|
|
2022-02-25 15:00:00 +00:00
|
|
|
static void readProcLink(const std::string & file, UncheckedRoots & roots)
|
2006-07-20 12:17:25 +00:00
|
|
|
{
|
2016-07-20 18:00:36 +00:00
|
|
|
/* 64 is the starting buffer size gnu readlink uses... */
|
|
|
|
auto bufsiz = ssize_t{64};
|
|
|
|
try_again:
|
|
|
|
char buf[bufsiz];
|
|
|
|
auto res = readlink(file.c_str(), buf, bufsiz);
|
|
|
|
if (res == -1) {
|
2018-06-16 16:04:09 +00:00
|
|
|
if (errno == ENOENT || errno == EACCES || errno == ESRCH)
|
2016-07-20 18:00:36 +00:00
|
|
|
return;
|
|
|
|
throw SysError("reading symlink");
|
|
|
|
}
|
|
|
|
if (res == bufsiz) {
|
|
|
|
if (SSIZE_MAX / 2 < bufsiz)
|
|
|
|
throw Error("stupidly long symlink");
|
|
|
|
bufsiz *= 2;
|
|
|
|
goto try_again;
|
|
|
|
}
|
|
|
|
if (res > 0 && buf[0] == '/')
|
2019-02-28 23:54:52 +00:00
|
|
|
roots[std::string(static_cast<char *>(buf), res)]
|
2019-03-14 12:27:16 +00:00
|
|
|
.emplace(file);
|
2016-07-20 18:00:36 +00:00
|
|
|
}
|
2006-07-20 12:17:25 +00:00
|
|
|
|
2022-02-25 15:00:00 +00:00
|
|
|
static std::string quoteRegexChars(const std::string & raw)
|
2016-07-20 18:00:36 +00:00
|
|
|
{
|
|
|
|
static auto specialRegex = std::regex(R"([.^$\\*+?()\[\]{}|])");
|
|
|
|
return std::regex_replace(raw, specialRegex, R"(\$&)");
|
|
|
|
}
|
2012-07-30 23:55:41 +00:00
|
|
|
|
2021-11-22 12:57:56 +00:00
|
|
|
#if __linux__
|
2019-12-05 18:11:09 +00:00
|
|
|
static void readFileRoots(const char * path, UncheckedRoots & roots)
|
2016-07-20 18:00:36 +00:00
|
|
|
{
|
|
|
|
try {
|
2019-02-28 23:54:52 +00:00
|
|
|
roots[readFile(path)].emplace(path);
|
2016-07-20 18:00:36 +00:00
|
|
|
} catch (SysError & e) {
|
|
|
|
if (e.errNo != ENOENT && e.errNo != EACCES)
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
2021-11-19 14:22:31 +00:00
|
|
|
#endif
|
2006-07-20 12:17:25 +00:00
|
|
|
|
2019-03-14 12:50:07 +00:00
|
|
|
void LocalStore::findRuntimeRoots(Roots & roots, bool censor)
|
2016-07-20 18:00:36 +00:00
|
|
|
{
|
2019-12-05 18:11:09 +00:00
|
|
|
UncheckedRoots unchecked;
|
2019-02-27 22:32:12 +00:00
|
|
|
|
2016-07-20 18:00:36 +00:00
|
|
|
auto procDir = AutoCloseDir{opendir("/proc")};
|
|
|
|
if (procDir) {
|
|
|
|
struct dirent * ent;
|
|
|
|
auto digitsRegex = std::regex(R"(^\d+$)");
|
|
|
|
auto mapRegex = std::regex(R"(^\s*\S+\s+\S+\s+\S+\s+\S+\s+\S+\s+(/\S+)\s*$)");
|
|
|
|
auto storePathRegex = std::regex(quoteRegexChars(storeDir) + R"(/[0-9a-z]+[0-9a-zA-Z\+\-\._\?=]*)");
|
2017-01-16 21:39:27 +00:00
|
|
|
while (errno = 0, ent = readdir(procDir.get())) {
|
2016-07-20 18:00:36 +00:00
|
|
|
checkInterrupt();
|
|
|
|
if (std::regex_match(ent->d_name, digitsRegex)) {
|
2019-03-14 12:27:16 +00:00
|
|
|
readProcLink(fmt("/proc/%s/exe" ,ent->d_name), unchecked);
|
|
|
|
readProcLink(fmt("/proc/%s/cwd", ent->d_name), unchecked);
|
2016-07-20 18:00:36 +00:00
|
|
|
|
2019-03-14 12:27:16 +00:00
|
|
|
auto fdStr = fmt("/proc/%s/fd", ent->d_name);
|
2016-07-20 18:00:36 +00:00
|
|
|
auto fdDir = AutoCloseDir(opendir(fdStr.c_str()));
|
|
|
|
if (!fdDir) {
|
|
|
|
if (errno == ENOENT || errno == EACCES)
|
|
|
|
continue;
|
2020-04-21 23:07:07 +00:00
|
|
|
throw SysError("opening %1%", fdStr);
|
2016-07-20 18:00:36 +00:00
|
|
|
}
|
|
|
|
struct dirent * fd_ent;
|
2017-01-16 21:39:27 +00:00
|
|
|
while (errno = 0, fd_ent = readdir(fdDir.get())) {
|
2019-03-14 12:27:16 +00:00
|
|
|
if (fd_ent->d_name[0] != '.')
|
|
|
|
readProcLink(fmt("%s/%s", fdStr, fd_ent->d_name), unchecked);
|
2016-07-20 18:00:36 +00:00
|
|
|
}
|
2018-06-11 13:59:32 +00:00
|
|
|
if (errno) {
|
|
|
|
if (errno == ESRCH)
|
|
|
|
continue;
|
2020-04-21 23:07:07 +00:00
|
|
|
throw SysError("iterating /proc/%1%/fd", ent->d_name);
|
2016-07-20 18:00:36 +00:00
|
|
|
}
|
2018-06-11 13:59:32 +00:00
|
|
|
fdDir.reset();
|
2016-07-20 18:00:36 +00:00
|
|
|
|
|
|
|
try {
|
2019-03-14 12:27:16 +00:00
|
|
|
auto mapFile = fmt("/proc/%s/maps", ent->d_name);
|
2022-02-25 15:00:00 +00:00
|
|
|
auto mapLines = tokenizeString<std::vector<std::string>>(readFile(mapFile), "\n");
|
2019-03-14 12:27:16 +00:00
|
|
|
for (const auto & line : mapLines) {
|
2018-06-11 13:59:32 +00:00
|
|
|
auto match = std::smatch{};
|
|
|
|
if (std::regex_match(line, match, mapRegex))
|
2019-03-14 12:27:16 +00:00
|
|
|
unchecked[match[1]].emplace(mapFile);
|
2018-06-11 13:59:32 +00:00
|
|
|
}
|
|
|
|
|
2019-03-14 12:27:16 +00:00
|
|
|
auto envFile = fmt("/proc/%s/environ", ent->d_name);
|
2020-04-29 16:42:19 +00:00
|
|
|
auto envString = readFile(envFile);
|
2016-07-20 18:00:36 +00:00
|
|
|
auto env_end = std::sregex_iterator{};
|
|
|
|
for (auto i = std::sregex_iterator{envString.begin(), envString.end(), storePathRegex}; i != env_end; ++i)
|
2019-03-14 12:27:16 +00:00
|
|
|
unchecked[i->str()].emplace(envFile);
|
2016-07-20 18:00:36 +00:00
|
|
|
} catch (SysError & e) {
|
2018-06-11 13:59:32 +00:00
|
|
|
if (errno == ENOENT || errno == EACCES || errno == ESRCH)
|
2016-07-20 18:00:36 +00:00
|
|
|
continue;
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (errno)
|
|
|
|
throw SysError("iterating /proc");
|
|
|
|
}
|
|
|
|
|
2017-04-20 17:11:45 +00:00
|
|
|
#if !defined(__linux__)
|
2019-07-30 09:29:03 +00:00
|
|
|
// lsof is really slow on OS X. This actually causes the gc-concurrent.sh test to fail.
|
|
|
|
// See: https://github.com/NixOS/nix/issues/3011
|
|
|
|
// Because of this we disable lsof when running the tests.
|
2020-01-04 23:41:18 +00:00
|
|
|
if (getEnv("_NIX_TEST_NO_LSOF") != "1") {
|
2019-07-30 09:29:03 +00:00
|
|
|
try {
|
|
|
|
std::regex lsofRegex(R"(^n(/.*)$)");
|
|
|
|
auto lsofLines =
|
2022-02-28 14:21:03 +00:00
|
|
|
tokenizeString<std::vector<std::string>>(runProgram(LSOF, true, { "-n", "-w", "-F", "n" }), "\n");
|
2019-07-30 09:29:03 +00:00
|
|
|
for (const auto & line : lsofLines) {
|
|
|
|
std::smatch match;
|
|
|
|
if (std::regex_match(line, match, lsofRegex))
|
|
|
|
unchecked[match[1]].emplace("{lsof}");
|
|
|
|
}
|
|
|
|
} catch (ExecError & e) {
|
|
|
|
/* lsof not installed, lsof failed */
|
2016-07-20 18:00:36 +00:00
|
|
|
}
|
|
|
|
}
|
2017-04-20 17:11:45 +00:00
|
|
|
#endif
|
2006-07-20 12:17:25 +00:00
|
|
|
|
2021-11-22 12:57:56 +00:00
|
|
|
#if __linux__
|
2019-02-27 22:32:12 +00:00
|
|
|
readFileRoots("/proc/sys/kernel/modprobe", unchecked);
|
|
|
|
readFileRoots("/proc/sys/kernel/fbsplash", unchecked);
|
|
|
|
readFileRoots("/proc/sys/kernel/poweroff_cmd", unchecked);
|
2017-04-20 17:11:45 +00:00
|
|
|
#endif
|
2012-07-30 23:55:41 +00:00
|
|
|
|
2019-02-28 23:54:52 +00:00
|
|
|
for (auto & [target, links] : unchecked) {
|
2019-12-05 18:11:09 +00:00
|
|
|
if (!isInStore(target)) continue;
|
2020-07-13 14:19:37 +00:00
|
|
|
try {
|
|
|
|
auto path = toStorePath(target).first;
|
|
|
|
if (!isValidPath(path)) continue;
|
|
|
|
debug("got additional root '%1%'", printStorePath(path));
|
|
|
|
if (censor)
|
|
|
|
roots[path].insert(censored);
|
|
|
|
else
|
|
|
|
roots[path].insert(links.begin(), links.end());
|
|
|
|
} catch (BadStorePath &) { }
|
2019-02-27 22:32:12 +00:00
|
|
|
}
|
2006-07-20 12:17:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-18 14:20:16 +00:00
|
|
|
struct GCLimitReached { };
|
|
|
|
|
|
|
|
|
2021-10-14 11:52:49 +00:00
|
|
|
void LocalStore::collectGarbage(const GCOptions & options, GCResults & results)
|
2008-06-13 18:25:24 +00:00
|
|
|
{
|
2021-10-14 11:52:49 +00:00
|
|
|
bool shouldDelete = options.action == GCOptions::gcDeleteDead || options.action == GCOptions::gcDeleteSpecific;
|
|
|
|
bool gcKeepOutputs = settings.gcKeepOutputs;
|
|
|
|
bool gcKeepDerivations = settings.gcKeepDerivations;
|
|
|
|
|
|
|
|
StorePathSet roots, dead, alive;
|
2008-06-18 14:20:16 +00:00
|
|
|
|
2021-08-16 18:03:32 +00:00
|
|
|
struct Shared
|
|
|
|
{
|
|
|
|
// The temp roots only store the hash part to make it easier to
|
|
|
|
// ignore suffixes like '.lock', '.chroot' and '.check'.
|
|
|
|
std::unordered_set<std::string> tempRoots;
|
2009-11-23 16:34:24 +00:00
|
|
|
|
2021-08-16 18:03:32 +00:00
|
|
|
// Hash part of the store path currently being deleted, if
|
|
|
|
// any.
|
|
|
|
std::optional<std::string> pending;
|
|
|
|
};
|
2008-09-17 12:54:07 +00:00
|
|
|
|
2021-10-14 11:52:49 +00:00
|
|
|
Sync<Shared> _shared;
|
2012-03-26 18:00:02 +00:00
|
|
|
|
2021-08-16 18:03:32 +00:00
|
|
|
std::condition_variable wakeup;
|
|
|
|
|
2010-03-08 21:31:42 +00:00
|
|
|
/* Using `--ignore-liveness' with `--delete' can have unintended
|
2017-08-31 12:28:25 +00:00
|
|
|
consequences if `keep-outputs' or `keep-derivations' are true
|
|
|
|
(the garbage collector will recurse into deleting the outputs
|
|
|
|
or derivers, respectively). So disable them. */
|
2010-03-08 21:31:42 +00:00
|
|
|
if (options.action == GCOptions::gcDeleteSpecific && options.ignoreLiveness) {
|
2021-10-14 11:52:49 +00:00
|
|
|
gcKeepOutputs = false;
|
|
|
|
gcKeepDerivations = false;
|
2010-03-08 21:31:42 +00:00
|
|
|
}
|
2012-07-30 23:55:41 +00:00
|
|
|
|
2021-10-14 11:52:49 +00:00
|
|
|
if (shouldDelete)
|
2016-02-24 16:33:53 +00:00
|
|
|
deletePath(reservedPath);
|
|
|
|
|
2021-10-14 11:52:49 +00:00
|
|
|
/* Acquire the global GC root. Note: we don't use fdGCLock
|
2021-08-19 20:43:43 +00:00
|
|
|
here because then in auto-gc mode, another thread could
|
|
|
|
downgrade our exclusive lock. */
|
|
|
|
auto fdGCLock = openGCLock();
|
|
|
|
FdLock gcLock(fdGCLock.get(), ltWrite, true, "waiting for the big garbage collector lock...");
|
2021-08-16 18:03:32 +00:00
|
|
|
|
|
|
|
/* Start the server for receiving new roots. */
|
|
|
|
auto socketPath = stateDir.get() + gcSocketPath;
|
|
|
|
createDirs(dirOf(socketPath));
|
|
|
|
auto fdServer = createUnixDomainSocket(socketPath, 0666);
|
|
|
|
|
|
|
|
if (fcntl(fdServer.get(), F_SETFL, fcntl(fdServer.get(), F_GETFL) | O_NONBLOCK) == -1)
|
|
|
|
throw SysError("making socket '%1%' non-blocking", socketPath);
|
|
|
|
|
|
|
|
Pipe shutdownPipe;
|
|
|
|
shutdownPipe.create();
|
|
|
|
|
|
|
|
std::thread serverThread([&]() {
|
2021-08-20 09:18:35 +00:00
|
|
|
Sync<std::map<int, std::thread>> connections;
|
|
|
|
|
|
|
|
Finally cleanup([&]() {
|
|
|
|
debug("GC roots server shutting down");
|
|
|
|
while (true) {
|
|
|
|
auto item = remove_begin(*connections.lock());
|
|
|
|
if (!item) break;
|
|
|
|
auto & [fd, thread] = *item;
|
|
|
|
shutdown(fd, SHUT_RDWR);
|
|
|
|
thread.join();
|
|
|
|
}
|
|
|
|
});
|
2021-08-16 18:03:32 +00:00
|
|
|
|
2021-08-20 09:18:35 +00:00
|
|
|
while (true) {
|
2021-08-16 18:03:32 +00:00
|
|
|
std::vector<struct pollfd> fds;
|
|
|
|
fds.push_back({.fd = shutdownPipe.readSide.get(), .events = POLLIN});
|
|
|
|
fds.push_back({.fd = fdServer.get(), .events = POLLIN});
|
|
|
|
auto count = poll(fds.data(), fds.size(), -1);
|
|
|
|
assert(count != -1);
|
|
|
|
|
2021-08-20 09:18:35 +00:00
|
|
|
if (fds[0].revents)
|
|
|
|
/* Parent is asking us to quit. */
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (fds[1].revents) {
|
|
|
|
/* Accept a new connection. */
|
|
|
|
assert(fds[1].revents & POLLIN);
|
|
|
|
AutoCloseFD fdClient = accept(fdServer.get(), nullptr, nullptr);
|
|
|
|
if (!fdClient) continue;
|
|
|
|
|
2021-12-13 16:12:32 +00:00
|
|
|
debug("GC roots server accepted new client");
|
|
|
|
|
2021-08-20 09:18:35 +00:00
|
|
|
/* Process the connection in a separate thread. */
|
|
|
|
auto fdClient_ = fdClient.get();
|
|
|
|
std::thread clientThread([&, fdClient = std::move(fdClient)]() {
|
|
|
|
Finally cleanup([&]() {
|
2021-10-15 14:12:21 +00:00
|
|
|
auto conn(connections.lock());
|
|
|
|
auto i = conn->find(fdClient.get());
|
2021-10-15 14:36:48 +00:00
|
|
|
if (i != conn->end()) {
|
|
|
|
i->second.detach();
|
|
|
|
conn->erase(i);
|
|
|
|
}
|
2021-08-20 09:18:35 +00:00
|
|
|
});
|
|
|
|
|
2021-12-13 16:16:46 +00:00
|
|
|
/* On macOS, accepted sockets inherit the
|
|
|
|
non-blocking flag from the server socket, so
|
|
|
|
explicitly make it blocking. */
|
|
|
|
if (fcntl(fdServer.get(), F_SETFL, fcntl(fdServer.get(), F_GETFL) & ~O_NONBLOCK) == -1)
|
|
|
|
abort();
|
|
|
|
|
2021-08-20 09:18:35 +00:00
|
|
|
while (true) {
|
|
|
|
try {
|
|
|
|
auto path = readLine(fdClient.get());
|
|
|
|
auto storePath = maybeParseStorePath(path);
|
|
|
|
if (storePath) {
|
|
|
|
debug("got new GC root '%s'", path);
|
|
|
|
auto hashPart = std::string(storePath->hashPart());
|
2021-10-14 11:52:49 +00:00
|
|
|
auto shared(_shared.lock());
|
2021-08-20 09:18:35 +00:00
|
|
|
shared->tempRoots.insert(hashPart);
|
|
|
|
/* If this path is currently being
|
|
|
|
deleted, then we have to wait until
|
|
|
|
deletion is finished to ensure that
|
|
|
|
the client doesn't start
|
|
|
|
re-creating it before we're
|
|
|
|
done. FIXME: ideally we would use a
|
|
|
|
FD for this so we don't block the
|
|
|
|
poll loop. */
|
|
|
|
while (shared->pending == hashPart) {
|
|
|
|
debug("synchronising with deletion of path '%s'", path);
|
2021-10-14 11:52:49 +00:00
|
|
|
shared.wait(wakeup);
|
2021-08-20 09:18:35 +00:00
|
|
|
}
|
|
|
|
} else
|
|
|
|
printError("received garbage instead of a root from client");
|
|
|
|
writeFull(fdClient.get(), "1", false);
|
2021-12-13 16:12:32 +00:00
|
|
|
} catch (Error & e) {
|
|
|
|
debug("reading GC root from client: %s", e.msg());
|
|
|
|
break;
|
|
|
|
}
|
2021-08-16 18:03:32 +00:00
|
|
|
}
|
2021-08-20 09:18:35 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
connections.lock()->insert({fdClient_, std::move(clientThread)});
|
2021-08-16 18:03:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Finally stopServer([&]() {
|
|
|
|
writeFull(shutdownPipe.writeSide.get(), "x", false);
|
2021-10-14 11:52:49 +00:00
|
|
|
wakeup.notify_all();
|
2021-08-16 18:03:32 +00:00
|
|
|
if (serverThread.joinable()) serverThread.join();
|
|
|
|
});
|
2005-01-31 10:27:25 +00:00
|
|
|
|
2005-02-01 15:05:32 +00:00
|
|
|
/* Find the roots. Since we've grabbed the GC lock, the set of
|
|
|
|
permanent roots cannot increase now. */
|
2020-05-13 15:52:36 +00:00
|
|
|
printInfo("finding garbage collector roots...");
|
2019-03-09 23:37:52 +00:00
|
|
|
Roots rootMap;
|
|
|
|
if (!options.ignoreLiveness)
|
2019-03-14 12:50:07 +00:00
|
|
|
findRootsNoTemp(rootMap, true);
|
2006-12-05 00:48:36 +00:00
|
|
|
|
2021-10-14 11:52:49 +00:00
|
|
|
for (auto & i : rootMap) roots.insert(i.first);
|
2005-02-01 15:05:32 +00:00
|
|
|
|
2021-08-16 18:03:32 +00:00
|
|
|
/* Read the temporary roots created before we acquired the global
|
|
|
|
GC root. Any new roots will be sent to our socket. */
|
2019-03-09 23:37:52 +00:00
|
|
|
Roots tempRoots;
|
2021-08-16 18:03:32 +00:00
|
|
|
findTempRoots(tempRoots, true);
|
2019-12-05 18:11:09 +00:00
|
|
|
for (auto & root : tempRoots) {
|
2021-10-14 11:52:49 +00:00
|
|
|
_shared.lock()->tempRoots.insert(std::string(root.first.hashPart()));
|
|
|
|
roots.insert(root.first);
|
2019-12-05 18:11:09 +00:00
|
|
|
}
|
2005-01-31 14:00:43 +00:00
|
|
|
|
2021-10-14 11:34:48 +00:00
|
|
|
/* Helper function that deletes a path from the store and throws
|
|
|
|
GCLimitReached if we've deleted enough garbage. */
|
|
|
|
auto deleteFromStore = [&](std::string_view baseName)
|
|
|
|
{
|
|
|
|
Path path = storeDir + "/" + std::string(baseName);
|
|
|
|
Path realPath = realStoreDir + "/" + std::string(baseName);
|
|
|
|
|
|
|
|
printInfo("deleting '%1%'", path);
|
|
|
|
|
2021-10-14 11:52:49 +00:00
|
|
|
results.paths.insert(path);
|
2021-10-14 11:34:48 +00:00
|
|
|
|
|
|
|
uint64_t bytesFreed;
|
|
|
|
deletePath(realPath, bytesFreed);
|
2021-10-14 11:52:49 +00:00
|
|
|
results.bytesFreed += bytesFreed;
|
2021-10-14 11:34:48 +00:00
|
|
|
|
2021-10-14 11:52:49 +00:00
|
|
|
if (results.bytesFreed > options.maxFreed) {
|
|
|
|
printInfo("deleted more than %d bytes; stopping", options.maxFreed);
|
2021-10-14 11:34:48 +00:00
|
|
|
throw GCLimitReached();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-10-15 10:20:23 +00:00
|
|
|
std::map<StorePath, StorePathSet> referrersCache;
|
|
|
|
|
2021-10-14 10:31:21 +00:00
|
|
|
/* Helper function that visits all paths reachable from `start`
|
|
|
|
via the referrers edges and optionally derivers and derivation
|
2021-10-14 11:34:48 +00:00
|
|
|
output edges. If none of those paths are roots, then all
|
|
|
|
visited paths are garbage and are deleted. */
|
2021-10-14 10:31:21 +00:00
|
|
|
auto deleteReferrersClosure = [&](const StorePath & start) {
|
|
|
|
StorePathSet visited;
|
|
|
|
std::queue<StorePath> todo;
|
|
|
|
|
|
|
|
/* Wake up any GC client waiting for deletion of the paths in
|
|
|
|
'visited' to finish. */
|
|
|
|
Finally releasePending([&]() {
|
2021-10-14 11:52:49 +00:00
|
|
|
auto shared(_shared.lock());
|
2021-10-14 10:31:21 +00:00
|
|
|
shared->pending.reset();
|
2021-10-14 11:52:49 +00:00
|
|
|
wakeup.notify_all();
|
2021-10-14 10:31:21 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
auto enqueue = [&](const StorePath & path) {
|
|
|
|
if (visited.insert(path).second)
|
|
|
|
todo.push(path);
|
|
|
|
};
|
|
|
|
|
|
|
|
enqueue(start);
|
|
|
|
|
|
|
|
while (auto path = pop(todo)) {
|
|
|
|
checkInterrupt();
|
|
|
|
|
|
|
|
/* Bail out if we've previously discovered that this path
|
|
|
|
is alive. */
|
2021-10-14 11:52:49 +00:00
|
|
|
if (alive.count(*path)) {
|
|
|
|
alive.insert(start);
|
2021-10-14 10:31:21 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we've previously deleted this path, we don't have to
|
|
|
|
handle it again. */
|
2021-10-14 11:52:49 +00:00
|
|
|
if (dead.count(*path)) continue;
|
2021-10-14 10:31:21 +00:00
|
|
|
|
2021-10-14 12:13:57 +00:00
|
|
|
auto markAlive = [&]()
|
|
|
|
{
|
|
|
|
alive.insert(*path);
|
|
|
|
alive.insert(start);
|
|
|
|
try {
|
|
|
|
StorePathSet closure;
|
|
|
|
computeFSClosure(*path, closure);
|
|
|
|
for (auto & p : closure)
|
|
|
|
alive.insert(p);
|
|
|
|
} catch (InvalidPath &) { }
|
|
|
|
};
|
|
|
|
|
2021-10-14 10:31:21 +00:00
|
|
|
/* If this is a root, bail out. */
|
2021-10-14 11:52:49 +00:00
|
|
|
if (roots.count(*path)) {
|
2021-10-14 10:31:21 +00:00
|
|
|
debug("cannot delete '%s' because it's a root", printStorePath(*path));
|
2021-10-14 12:13:57 +00:00
|
|
|
return markAlive();
|
2021-10-14 10:31:21 +00:00
|
|
|
}
|
2009-11-23 16:34:24 +00:00
|
|
|
|
2021-10-14 11:52:49 +00:00
|
|
|
if (options.action == GCOptions::gcDeleteSpecific
|
|
|
|
&& !options.pathsToDelete.count(*path))
|
2021-10-14 10:31:21 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
{
|
|
|
|
auto hashPart = std::string(path->hashPart());
|
2021-10-14 11:52:49 +00:00
|
|
|
auto shared(_shared.lock());
|
2021-10-14 10:31:21 +00:00
|
|
|
if (shared->tempRoots.count(hashPart)) {
|
|
|
|
debug("cannot delete '%s' because it's a temporary root", printStorePath(*path));
|
2021-10-14 12:13:57 +00:00
|
|
|
return markAlive();
|
2021-10-14 10:31:21 +00:00
|
|
|
}
|
|
|
|
shared->pending = hashPart;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isValidPath(*path)) {
|
|
|
|
|
|
|
|
/* Visit the referrers of this path. */
|
2021-10-15 10:20:23 +00:00
|
|
|
auto i = referrersCache.find(*path);
|
|
|
|
if (i == referrersCache.end()) {
|
|
|
|
StorePathSet referrers;
|
|
|
|
queryReferrers(*path, referrers);
|
|
|
|
referrersCache.emplace(*path, std::move(referrers));
|
|
|
|
i = referrersCache.find(*path);
|
|
|
|
}
|
|
|
|
for (auto & p : i->second)
|
2021-10-14 10:31:21 +00:00
|
|
|
enqueue(p);
|
|
|
|
|
|
|
|
/* If keep-derivations is set and this is a
|
|
|
|
derivation, then visit the derivation outputs. */
|
2021-10-14 11:52:49 +00:00
|
|
|
if (gcKeepDerivations && path->isDerivation()) {
|
2021-10-14 10:31:21 +00:00
|
|
|
for (auto & [name, maybeOutPath] : queryPartialDerivationOutputMap(*path))
|
|
|
|
if (maybeOutPath &&
|
|
|
|
isValidPath(*maybeOutPath) &&
|
|
|
|
queryPathInfo(*maybeOutPath)->deriver == *path)
|
|
|
|
enqueue(*maybeOutPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If keep-outputs is set, then visit the derivers. */
|
2021-10-14 11:52:49 +00:00
|
|
|
if (gcKeepOutputs) {
|
2021-10-14 10:31:21 +00:00
|
|
|
auto derivers = queryValidDerivers(*path);
|
|
|
|
for (auto & i : derivers)
|
|
|
|
enqueue(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto & path : topoSortPaths(visited)) {
|
2021-10-14 11:52:49 +00:00
|
|
|
if (!dead.insert(path).second) continue;
|
|
|
|
if (shouldDelete) {
|
2021-10-14 10:31:21 +00:00
|
|
|
invalidatePathChecked(path);
|
2021-10-14 11:34:48 +00:00
|
|
|
deleteFromStore(path.to_string());
|
2021-10-15 10:20:23 +00:00
|
|
|
referrersCache.erase(path);
|
2021-10-14 10:31:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-10-15 14:52:37 +00:00
|
|
|
/* Synchronisation point for testing, see tests/gc-concurrent.sh. */
|
|
|
|
if (auto p = getEnv("_NIX_TEST_GC_SYNC"))
|
|
|
|
readFile(*p);
|
|
|
|
|
2021-10-14 10:31:21 +00:00
|
|
|
/* Either delete all garbage paths, or just the specified
|
|
|
|
paths (for gcDeleteSpecific). */
|
2009-11-23 16:34:24 +00:00
|
|
|
if (options.action == GCOptions::gcDeleteSpecific) {
|
2008-09-17 10:02:55 +00:00
|
|
|
|
2015-07-17 17:24:28 +00:00
|
|
|
for (auto & i : options.pathsToDelete) {
|
2021-10-14 10:31:21 +00:00
|
|
|
deleteReferrersClosure(i);
|
2021-10-14 11:52:49 +00:00
|
|
|
if (!dead.count(i))
|
2019-12-05 18:11:09 +00:00
|
|
|
throw Error(
|
2021-10-14 10:31:21 +00:00
|
|
|
"Cannot delete path '%1%' since it is still alive. "
|
2021-08-16 18:03:32 +00:00
|
|
|
"To find out why, use: "
|
2019-12-05 18:11:09 +00:00
|
|
|
"nix-store --query --roots",
|
|
|
|
printStorePath(i));
|
2005-12-23 21:08:42 +00:00
|
|
|
}
|
2012-07-30 23:55:41 +00:00
|
|
|
|
2012-08-01 23:14:30 +00:00
|
|
|
} else if (options.maxFreed > 0) {
|
2012-07-30 23:55:41 +00:00
|
|
|
|
2021-10-14 11:52:49 +00:00
|
|
|
if (shouldDelete)
|
2020-05-13 15:52:36 +00:00
|
|
|
printInfo("deleting garbage...");
|
2009-11-23 16:34:24 +00:00
|
|
|
else
|
2020-05-13 15:52:36 +00:00
|
|
|
printInfo("determining live/dead paths...");
|
2012-07-30 23:55:41 +00:00
|
|
|
|
2009-11-23 16:34:24 +00:00
|
|
|
try {
|
2021-06-18 15:04:11 +00:00
|
|
|
AutoCloseDir dir(opendir(realStoreDir.get().c_str()));
|
2020-04-21 23:07:07 +00:00
|
|
|
if (!dir) throw SysError("opening directory '%1%'", realStoreDir);
|
2011-12-22 15:55:53 +00:00
|
|
|
|
2021-08-16 18:03:32 +00:00
|
|
|
/* Read the store and delete all paths that are invalid or
|
|
|
|
unreachable. We don't use readDirectory() here so that
|
|
|
|
GCing can start faster. */
|
2021-10-08 12:58:35 +00:00
|
|
|
auto linksName = baseNameOf(linksDir);
|
2011-12-22 15:55:53 +00:00
|
|
|
Paths entries;
|
|
|
|
struct dirent * dirent;
|
2017-01-16 21:39:27 +00:00
|
|
|
while (errno = 0, dirent = readdir(dir.get())) {
|
2011-12-22 15:55:53 +00:00
|
|
|
checkInterrupt();
|
2022-02-25 15:00:00 +00:00
|
|
|
std::string name = dirent->d_name;
|
2021-10-08 12:58:35 +00:00
|
|
|
if (name == "." || name == ".." || name == linksName) continue;
|
|
|
|
|
2021-10-14 10:31:21 +00:00
|
|
|
if (auto storePath = maybeParseStorePath(storeDir + "/" + name))
|
|
|
|
deleteReferrersClosure(*storePath);
|
|
|
|
else
|
2021-10-14 11:34:48 +00:00
|
|
|
deleteFromStore(name);
|
2021-10-08 12:58:35 +00:00
|
|
|
|
2011-12-22 15:55:53 +00:00
|
|
|
}
|
2009-11-23 16:34:24 +00:00
|
|
|
} catch (GCLimitReached & e) {
|
|
|
|
}
|
2011-12-22 15:55:53 +00:00
|
|
|
}
|
2012-03-26 18:43:33 +00:00
|
|
|
|
2021-10-14 11:52:49 +00:00
|
|
|
if (options.action == GCOptions::gcReturnLive) {
|
|
|
|
for (auto & i : alive)
|
|
|
|
results.paths.insert(printStorePath(i));
|
2012-12-20 16:32:15 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-10-14 11:52:49 +00:00
|
|
|
if (options.action == GCOptions::gcReturnDead) {
|
|
|
|
for (auto & i : dead)
|
|
|
|
results.paths.insert(printStorePath(i));
|
2012-12-20 16:32:15 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-10-14 11:52:49 +00:00
|
|
|
/* Unlink all files in /nix/store/.links that have a link count of 1,
|
|
|
|
which indicates that there are no other links and so they can be
|
|
|
|
safely deleted. FIXME: race condition with optimisePath(): we
|
|
|
|
might see a link count of 1 just before optimisePath() increases
|
|
|
|
the link count. */
|
2012-09-12 22:49:35 +00:00
|
|
|
if (options.action == GCOptions::gcDeleteDead || options.action == GCOptions::gcDeleteSpecific) {
|
2020-05-13 15:52:36 +00:00
|
|
|
printInfo("deleting unused links...");
|
2021-10-14 11:52:49 +00:00
|
|
|
|
|
|
|
AutoCloseDir dir(opendir(linksDir.c_str()));
|
|
|
|
if (!dir) throw SysError("opening directory '%1%'", linksDir);
|
|
|
|
|
|
|
|
int64_t actualSize = 0, unsharedSize = 0;
|
|
|
|
|
|
|
|
struct dirent * dirent;
|
|
|
|
while (errno = 0, dirent = readdir(dir.get())) {
|
|
|
|
checkInterrupt();
|
2022-02-25 15:00:00 +00:00
|
|
|
std::string name = dirent->d_name;
|
2021-10-14 11:52:49 +00:00
|
|
|
if (name == "." || name == "..") continue;
|
|
|
|
Path path = linksDir + "/" + name;
|
|
|
|
|
|
|
|
auto st = lstat(path);
|
|
|
|
|
|
|
|
if (st.st_nlink != 1) {
|
|
|
|
actualSize += st.st_size;
|
|
|
|
unsharedSize += (st.st_nlink - 1) * st.st_size;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
printMsg(lvlTalkative, format("deleting unused link '%1%'") % path);
|
|
|
|
|
|
|
|
if (unlink(path.c_str()) == -1)
|
|
|
|
throw SysError("deleting '%1%'", path);
|
|
|
|
|
|
|
|
results.bytesFreed += st.st_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct stat st;
|
|
|
|
if (stat(linksDir.c_str(), &st) == -1)
|
|
|
|
throw SysError("statting '%1%'", linksDir);
|
|
|
|
int64_t overhead = st.st_blocks * 512ULL;
|
|
|
|
|
|
|
|
printInfo("note: currently hard linking saves %.2f MiB",
|
|
|
|
((unsharedSize - actualSize - overhead) / (1024.0 * 1024.0)));
|
2012-09-12 22:49:35 +00:00
|
|
|
}
|
2012-09-13 18:33:41 +00:00
|
|
|
|
|
|
|
/* While we're at it, vacuum the database. */
|
2014-11-19 17:02:39 +00:00
|
|
|
//if (options.action == GCOptions::gcDeleteDead) vacuumDB();
|
2004-08-25 11:43:49 +00:00
|
|
|
}
|
2006-09-04 21:06:23 +00:00
|
|
|
|
2008-09-17 10:02:55 +00:00
|
|
|
|
2017-09-05 18:43:42 +00:00
|
|
|
void LocalStore::autoGC(bool sync)
|
|
|
|
{
|
2019-11-22 15:06:44 +00:00
|
|
|
static auto fakeFreeSpaceFile = getEnv("_NIX_TEST_FREE_SPACE_FILE");
|
2019-08-02 15:07:33 +00:00
|
|
|
|
|
|
|
auto getAvail = [this]() -> uint64_t {
|
2019-11-22 15:06:44 +00:00
|
|
|
if (fakeFreeSpaceFile)
|
|
|
|
return std::stoll(readFile(*fakeFreeSpaceFile));
|
2019-08-02 15:07:33 +00:00
|
|
|
|
2017-09-05 18:43:42 +00:00
|
|
|
struct statvfs st;
|
2021-06-18 15:04:11 +00:00
|
|
|
if (statvfs(realStoreDir.get().c_str(), &st))
|
2017-09-05 18:43:42 +00:00
|
|
|
throw SysError("getting filesystem info about '%s'", realStoreDir);
|
|
|
|
|
2020-05-04 21:42:06 +00:00
|
|
|
return (uint64_t) st.f_bavail * st.f_frsize;
|
2017-09-05 18:43:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
std::shared_future<void> future;
|
|
|
|
|
|
|
|
{
|
|
|
|
auto state(_state.lock());
|
|
|
|
|
|
|
|
if (state->gcRunning) {
|
|
|
|
future = state->gcFuture;
|
|
|
|
debug("waiting for auto-GC to finish");
|
|
|
|
goto sync;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto now = std::chrono::steady_clock::now();
|
|
|
|
|
2019-08-02 15:07:33 +00:00
|
|
|
if (now < state->lastGCCheck + std::chrono::seconds(settings.minFreeCheckInterval)) return;
|
2017-09-05 18:43:42 +00:00
|
|
|
|
|
|
|
auto avail = getAvail();
|
|
|
|
|
|
|
|
state->lastGCCheck = now;
|
|
|
|
|
|
|
|
if (avail >= settings.minFree || avail >= settings.maxFree) return;
|
|
|
|
|
|
|
|
if (avail > state->availAfterGC * 0.97) return;
|
|
|
|
|
|
|
|
state->gcRunning = true;
|
|
|
|
|
|
|
|
std::promise<void> promise;
|
|
|
|
future = state->gcFuture = promise.get_future().share();
|
|
|
|
|
|
|
|
std::thread([promise{std::move(promise)}, this, avail, getAvail]() mutable {
|
|
|
|
|
2017-09-06 09:37:58 +00:00
|
|
|
try {
|
2017-09-05 18:43:42 +00:00
|
|
|
|
2017-09-06 09:37:58 +00:00
|
|
|
/* Wake up any threads waiting for the auto-GC to finish. */
|
|
|
|
Finally wakeup([&]() {
|
|
|
|
auto state(_state.lock());
|
|
|
|
state->gcRunning = false;
|
|
|
|
state->lastGCCheck = std::chrono::steady_clock::now();
|
|
|
|
promise.set_value();
|
|
|
|
});
|
2017-09-05 18:43:42 +00:00
|
|
|
|
2017-09-06 09:37:58 +00:00
|
|
|
GCOptions options;
|
|
|
|
options.maxFreed = settings.maxFree - avail;
|
2017-09-05 18:43:42 +00:00
|
|
|
|
2019-08-29 10:09:58 +00:00
|
|
|
printInfo("running auto-GC to free %d bytes", options.maxFreed);
|
|
|
|
|
2017-09-06 09:37:58 +00:00
|
|
|
GCResults results;
|
2017-09-05 18:43:42 +00:00
|
|
|
|
2017-09-06 09:37:58 +00:00
|
|
|
collectGarbage(options, results);
|
|
|
|
|
|
|
|
_state.lock()->availAfterGC = getAvail();
|
|
|
|
|
|
|
|
} catch (...) {
|
|
|
|
// FIXME: we could propagate the exception to the
|
|
|
|
// future, but we don't really care.
|
|
|
|
ignoreException();
|
|
|
|
}
|
2017-09-05 18:43:42 +00:00
|
|
|
|
|
|
|
}).detach();
|
|
|
|
}
|
|
|
|
|
|
|
|
sync:
|
|
|
|
// Wait for the future outside of the state lock.
|
|
|
|
if (sync) future.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-04 21:06:23 +00:00
|
|
|
}
|