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 15:28:26 +02:00
|
|
|
#include "derivations.hh"
|
2006-09-05 00:06:23 +03:00
|
|
|
#include "globals.hh"
|
2006-11-30 19:43:04 +02:00
|
|
|
#include "local-store.hh"
|
2017-09-05 21:43:42 +03:00
|
|
|
#include "finally.hh"
|
2023-10-25 07:43:36 +03:00
|
|
|
#include "unix-domain-socket.hh"
|
|
|
|
#include "signals.hh"
|
|
|
|
|
|
|
|
#if !defined(__linux__)
|
|
|
|
// For shelling out to lsof
|
|
|
|
# include "processes.hh"
|
|
|
|
#endif
|
2005-01-31 12:27:25 +02:00
|
|
|
|
2008-09-17 13:02:55 +03:00
|
|
|
#include <functional>
|
|
|
|
#include <queue>
|
2009-11-24 11:53:18 +02:00
|
|
|
#include <algorithm>
|
2016-07-20 21:00:36 +03:00
|
|
|
#include <regex>
|
2018-06-13 17:56:19 +03:00
|
|
|
#include <random>
|
2008-09-17 13:02:55 +03:00
|
|
|
|
2021-08-16 21:03:32 +03:00
|
|
|
#include <climits>
|
2005-01-31 12:27:25 +02:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2021-08-16 21:03:32 +03: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 19:54:08 +03:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2006-09-05 00:06:23 +03:00
|
|
|
namespace nix {
|
|
|
|
|
2024-04-17 18:34:09 +03:00
|
|
|
using namespace nix::unix;
|
2006-09-05 00:06:23 +03:00
|
|
|
|
2021-08-16 21:03:32 +03:00
|
|
|
static std::string gcSocketPath = "/gc-socket/socket";
|
|
|
|
static std::string gcRootsDir = "gcroots";
|
2005-02-01 00:01:55 +02:00
|
|
|
|
|
|
|
|
2006-12-05 01:29:16 +02:00
|
|
|
void LocalStore::addIndirectRoot(const Path & path)
|
|
|
|
{
|
2023-11-28 16:38:15 +02:00
|
|
|
std::string hash = hashString(HashAlgorithm::SHA1, path).to_string(HashFormat::Nix32, false);
|
2022-02-25 17:00:00 +02:00
|
|
|
Path realRoot = canonPath(fmt("%1%/%2%/auto/%3%", stateDir, gcRootsDir, hash));
|
2014-02-28 00:17:53 +02:00
|
|
|
makeSymlink(realRoot, path);
|
2006-12-05 01:29:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-03 15:51:23 +02:00
|
|
|
void LocalStore::createTempRootsFile()
|
2005-01-31 12:27:25 +02:00
|
|
|
{
|
2023-01-03 15:51:23 +02:00
|
|
|
auto fdTempRoots(_fdTempRoots.lock());
|
2016-04-08 19:07:13 +03:00
|
|
|
|
2005-01-31 12:27:25 +02:00
|
|
|
/* Create the temporary roots file for this process. */
|
2023-01-03 15:51:23 +02:00
|
|
|
if (*fdTempRoots) return;
|
2006-06-20 20:48:10 +03:00
|
|
|
|
2023-01-03 15:51:23 +02:00
|
|
|
while (1) {
|
|
|
|
if (pathExists(fnTempRoots))
|
|
|
|
/* It *must* be stale, since there can be no two
|
|
|
|
processes with the same pid. */
|
|
|
|
unlink(fnTempRoots.c_str());
|
2005-01-31 12:27:25 +02:00
|
|
|
|
2023-01-03 15:51:23 +02:00
|
|
|
*fdTempRoots = openLockFile(fnTempRoots, true);
|
2005-01-31 12:27:25 +02:00
|
|
|
|
2023-01-03 15:51:23 +02:00
|
|
|
debug("acquiring write lock on '%s'", fnTempRoots);
|
|
|
|
lockFile(fdTempRoots->get(), ltWrite, true);
|
2012-07-31 02:55:41 +03:00
|
|
|
|
2023-01-03 15:51:23 +02:00
|
|
|
/* Check whether the garbage collector didn't get in our
|
|
|
|
way. */
|
|
|
|
struct stat st;
|
|
|
|
if (fstat(fdTempRoots->get(), &st) == -1)
|
|
|
|
throw SysError("statting '%1%'", fnTempRoots);
|
|
|
|
if (st.st_size == 0) break;
|
2005-01-31 12:27:25 +02:00
|
|
|
|
2023-01-03 15:51:23 +02: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. */
|
2005-01-31 12:27:25 +02:00
|
|
|
}
|
2023-01-03 15:51:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void LocalStore::addTempRoot(const StorePath & path)
|
|
|
|
{
|
2023-05-17 21:48:59 +03:00
|
|
|
if (readOnly) {
|
|
|
|
debug("Read-only store doesn't support creating lock files for temp roots, but nothing can be deleted anyways.");
|
|
|
|
return;
|
|
|
|
}
|
2023-05-17 22:56:48 +03:00
|
|
|
|
2023-01-03 15:51:23 +02:00
|
|
|
createTempRootsFile();
|
|
|
|
|
2023-01-03 16:15:14 +02:00
|
|
|
/* Open/create the global GC lock file. */
|
|
|
|
{
|
|
|
|
auto fdGCLock(_fdGCLock.lock());
|
|
|
|
if (!*fdGCLock)
|
|
|
|
*fdGCLock = openGCLock();
|
|
|
|
}
|
2021-08-19 23:43:43 +03:00
|
|
|
|
2021-08-16 21:03:32 +03:00
|
|
|
restart:
|
2023-01-03 16:15:14 +02:00
|
|
|
/* Try to acquire a shared global GC lock (non-blocking). This
|
|
|
|
only succeeds if the garbage collector is not currently
|
|
|
|
running. */
|
|
|
|
FdLock gcLock(_fdGCLock.lock()->get(), ltRead, false, "");
|
2021-08-16 21:03:32 +03:00
|
|
|
|
|
|
|
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. */
|
2023-01-03 16:15:14 +02:00
|
|
|
auto fdRootsSocket(_fdRootsSocket.lock());
|
|
|
|
|
|
|
|
if (!*fdRootsSocket) {
|
2021-08-16 21:03:32 +03:00
|
|
|
auto socketPath = stateDir.get() + gcSocketPath;
|
|
|
|
debug("connecting to '%s'", socketPath);
|
2023-01-03 16:15:14 +02:00
|
|
|
*fdRootsSocket = createUnixDomainSocket();
|
2021-12-13 17:14:57 +02:00
|
|
|
try {
|
2023-01-03 16:15:14 +02:00
|
|
|
nix::connect(fdRootsSocket->get(), socketPath);
|
2021-12-13 17:14:57 +02:00
|
|
|
} catch (SysError & e) {
|
2023-06-19 17:54:05 +03:00
|
|
|
/* The garbage collector may have exited or not
|
|
|
|
created the socket yet, so we need to restart. */
|
|
|
|
if (e.errNo == ECONNREFUSED || e.errNo == ENOENT) {
|
2024-01-12 13:26:25 +02:00
|
|
|
debug("GC socket connection refused: %s", e.msg());
|
2023-01-03 16:15:14 +02:00
|
|
|
fdRootsSocket->close();
|
2024-01-16 16:23:22 +02:00
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
2021-12-13 17:14:57 +02:00
|
|
|
goto restart;
|
|
|
|
}
|
2022-06-10 20:49:38 +03:00
|
|
|
throw;
|
2021-12-13 17:14:57 +02:00
|
|
|
}
|
2021-08-16 21:03:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
debug("sending GC root '%s'", printStorePath(path));
|
2023-01-03 16:15:14 +02:00
|
|
|
writeFull(fdRootsSocket->get(), printStorePath(path) + "\n", false);
|
2021-08-16 21:03:32 +03:00
|
|
|
char c;
|
2023-01-03 16:15:14 +02:00
|
|
|
readFull(fdRootsSocket->get(), &c, 1);
|
2021-08-16 21:03:32 +03:00
|
|
|
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. */
|
2022-11-27 13:57:18 +02:00
|
|
|
if (e.errNo == EPIPE || e.errNo == ECONNRESET) {
|
2021-08-16 21:03:32 +03:00
|
|
|
debug("GC socket disconnected");
|
2023-01-03 16:15:14 +02:00
|
|
|
fdRootsSocket->close();
|
2021-08-16 21:03:32 +03:00
|
|
|
goto restart;
|
|
|
|
}
|
2022-06-10 20:49:38 +03:00
|
|
|
throw;
|
2021-08-16 21:03:32 +03:00
|
|
|
} catch (EndOfFile & e) {
|
|
|
|
debug("GC socket disconnected");
|
2023-01-03 16:15:14 +02:00
|
|
|
fdRootsSocket->close();
|
2021-08-16 21:03:32 +03:00
|
|
|
goto restart;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-03 16:15:14 +02:00
|
|
|
/* Record the store path in the temporary roots file so it will be
|
|
|
|
seen by a future run of the garbage collector. */
|
2022-02-25 17:00:00 +02:00
|
|
|
auto s = printStorePath(path) + '\0';
|
2023-01-03 15:51:23 +02:00
|
|
|
writeFull(_fdTempRoots.lock()->get(), s);
|
2005-01-31 12:27:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-14 14:50:07 +02:00
|
|
|
static std::string censored = "{censored}";
|
|
|
|
|
|
|
|
|
2021-08-16 21:03:32 +03:00
|
|
|
void LocalStore::findTempRoots(Roots & tempRoots, bool censor)
|
2005-01-31 12:27:25 +02:00
|
|
|
{
|
|
|
|
/* Read the `temproots' directory for per-process temporary root
|
|
|
|
files. */
|
2017-09-14 16:02:52 +03:00
|
|
|
for (auto & i : readDirectory(tempRootsDir)) {
|
2020-04-09 18:05:29 +03:00
|
|
|
if (i.name[0] == '.') {
|
|
|
|
// Ignore hidden files. Some package managers (notably portage) create
|
|
|
|
// those to keep the directory alive.
|
|
|
|
continue;
|
|
|
|
}
|
2017-09-05 21:39:57 +03:00
|
|
|
Path path = tempRootsDir + "/" + i.name;
|
2005-01-31 12:27:25 +02:00
|
|
|
|
2017-09-14 16:02:52 +03:00
|
|
|
pid_t pid = std::stoi(i.name);
|
|
|
|
|
2023-03-02 16:44:19 +02:00
|
|
|
debug("reading temporary root file '%1%'", path);
|
2021-08-16 21:03:32 +03:00
|
|
|
AutoCloseFD fd(open(path.c_str(), O_CLOEXEC | O_RDWR, 0666));
|
|
|
|
if (!fd) {
|
2005-01-31 12:27:25 +02:00
|
|
|
/* It's okay if the file has disappeared. */
|
|
|
|
if (errno == ENOENT) continue;
|
2020-04-22 02:07:07 +03:00
|
|
|
throw SysError("opening temporary roots file '%1%'", path);
|
2005-01-31 12:27:25 +02:00
|
|
|
}
|
|
|
|
|
2019-08-02 19:37:55 +03: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 21:03:32 +03:00
|
|
|
if (lockFile(fd.get(), ltWrite, false)) {
|
2020-05-13 18:52:36 +03:00
|
|
|
printInfo("removing stale temporary roots file '%1%'", path);
|
2019-08-02 19:37:55 +03:00
|
|
|
unlink(path.c_str());
|
2021-08-16 21:03:32 +03:00
|
|
|
writeFull(fd.get(), "d");
|
2019-08-02 19:37:55 +03:00
|
|
|
continue;
|
2017-09-05 21:39:57 +03:00
|
|
|
}
|
2005-01-31 12:27:25 +02:00
|
|
|
|
|
|
|
/* Read the entire file. */
|
2022-02-25 17:00:00 +02:00
|
|
|
auto contents = readFile(fd.get());
|
2005-01-31 12:27:25 +02:00
|
|
|
|
|
|
|
/* Extract the roots. */
|
2022-02-25 17:00:00 +02:00
|
|
|
std::string::size_type pos = 0, end;
|
2005-01-31 12:27:25 +02:00
|
|
|
|
2022-02-25 17:00:00 +02:00
|
|
|
while ((end = contents.find((char) 0, pos)) != std::string::npos) {
|
2005-01-31 12:27:25 +02:00
|
|
|
Path root(contents, pos, end - pos);
|
2017-09-14 16:02:52 +03:00
|
|
|
debug("got temporary root '%s'", root);
|
2019-12-05 20:11:09 +02:00
|
|
|
tempRoots[parseStorePath(root)].emplace(censor ? censored : fmt("{temp:%d}", pid));
|
2005-01-31 12:27:25 +02:00
|
|
|
pos = end + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-05-07 07:14:49 +03:00
|
|
|
void LocalStore::findRoots(const Path & path, std::filesystem::file_type type, Roots & roots)
|
2005-02-01 17:05:32 +02: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 15:28:26 +02:00
|
|
|
auto foundRoot = [&](const Path & path, const Path & target) {
|
2020-07-13 17:19:37 +03: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 15:28:26 +02:00
|
|
|
};
|
2006-12-05 03:31:45 +02:00
|
|
|
|
2013-07-12 15:01:25 +03:00
|
|
|
try {
|
|
|
|
|
2024-05-07 07:14:49 +03:00
|
|
|
if (type == std::filesystem::file_type::unknown)
|
2014-10-03 23:37:51 +03:00
|
|
|
type = getFileType(path);
|
2006-12-05 03:31:45 +02:00
|
|
|
|
2024-05-07 07:14:49 +03:00
|
|
|
if (type == std::filesystem::file_type::directory) {
|
2014-08-01 17:37:47 +03: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 15:28:26 +02:00
|
|
|
findRoots(path + "/" + i.name, i.type, roots);
|
2005-02-01 17:05:32 +02:00
|
|
|
}
|
|
|
|
|
2024-05-07 07:14:49 +03:00
|
|
|
else if (type == std::filesystem::file_type::symlink) {
|
2013-07-12 15:01:25 +03: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 15:28:26 +02:00
|
|
|
foundRoot(path, target);
|
2013-07-12 15:01:25 +03:00
|
|
|
|
|
|
|
/* Handle indirect roots. */
|
|
|
|
else {
|
|
|
|
target = absPath(target, dirOf(path));
|
|
|
|
if (!pathExists(target)) {
|
2016-06-02 14:33:49 +03:00
|
|
|
if (isInDir(path, stateDir + "/" + gcRootsDir + "/auto")) {
|
2023-03-02 16:44:19 +02:00
|
|
|
printInfo("removing stale link from '%1%' to '%2%'", path, target);
|
2013-07-12 15:01:25 +03: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 15:28:26 +02:00
|
|
|
if (isInStore(target2)) foundRoot(target, target2);
|
2006-12-05 03:31:45 +02:00
|
|
|
}
|
2005-02-01 17:05:32 +02:00
|
|
|
}
|
|
|
|
}
|
2006-12-05 03:31:45 +02:00
|
|
|
|
2024-05-07 07:14:49 +03:00
|
|
|
else if (type == std::filesystem::file_type::regular) {
|
2020-02-28 19:07:10 +02:00
|
|
|
auto storePath = maybeParseStorePath(storeDir + "/" + std::string(baseNameOf(path)));
|
|
|
|
if (storePath && isValidPath(*storePath))
|
|
|
|
roots[std::move(*storePath)].emplace(path);
|
2014-08-01 17:46:01 +03:00
|
|
|
}
|
|
|
|
|
2006-12-05 03:31:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
catch (SysError & e) {
|
|
|
|
/* We only ignore permanent failures. */
|
|
|
|
if (e.errNo == EACCES || e.errNo == ENOENT || e.errNo == ENOTDIR)
|
2019-12-05 20:11:09 +02:00
|
|
|
printInfo("cannot read potential root '%1%'", path);
|
2006-12-05 03:31:45 +02:00
|
|
|
else
|
|
|
|
throw;
|
2005-02-01 17:05:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-14 14:50:07 +02:00
|
|
|
void LocalStore::findRootsNoTemp(Roots & roots, bool censor)
|
2006-12-05 02:34:42 +02:00
|
|
|
{
|
2018-02-01 11:39:16 +02:00
|
|
|
/* Process direct roots in {gcroots,profiles}. */
|
2024-05-07 07:14:49 +03:00
|
|
|
findRoots(stateDir + "/" + gcRootsDir, std::filesystem::file_type::unknown, roots);
|
|
|
|
findRoots(stateDir + "/profiles", std::filesystem::file_type::unknown, roots);
|
2006-12-05 03:31:45 +02:00
|
|
|
|
2019-05-01 05:43:24 +03: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 14:50:07 +02:00
|
|
|
findRuntimeRoots(roots, censor);
|
2017-09-14 15:38:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-14 14:50:07 +02:00
|
|
|
Roots LocalStore::findRoots(bool censor)
|
2017-09-14 15:38:36 +03:00
|
|
|
{
|
2019-03-10 01:37:52 +02:00
|
|
|
Roots roots;
|
2019-03-14 14:50:07 +02:00
|
|
|
findRootsNoTemp(roots, censor);
|
2017-09-14 15:38:36 +03:00
|
|
|
|
2021-08-16 21:03:32 +03:00
|
|
|
findTempRoots(roots, censor);
|
2017-09-14 15:38:36 +03:00
|
|
|
|
2013-07-12 15:01:25 +03:00
|
|
|
return roots;
|
2006-12-05 02:34:42 +02:00
|
|
|
}
|
|
|
|
|
2019-12-05 20:11:09 +02:00
|
|
|
typedef std::unordered_map<Path, std::unordered_set<std::string>> UncheckedRoots;
|
|
|
|
|
2022-02-25 17:00:00 +02:00
|
|
|
static void readProcLink(const std::string & file, UncheckedRoots & roots)
|
2006-07-20 15:17:25 +03:00
|
|
|
{
|
2022-11-18 14:02:06 +02:00
|
|
|
constexpr auto bufsiz = PATH_MAX;
|
2016-07-20 21:00:36 +03:00
|
|
|
char buf[bufsiz];
|
|
|
|
auto res = readlink(file.c_str(), buf, bufsiz);
|
|
|
|
if (res == -1) {
|
2018-06-16 19:04:09 +03:00
|
|
|
if (errno == ENOENT || errno == EACCES || errno == ESRCH)
|
2016-07-20 21:00:36 +03:00
|
|
|
return;
|
|
|
|
throw SysError("reading symlink");
|
|
|
|
}
|
|
|
|
if (res == bufsiz) {
|
2023-11-16 12:33:10 +02:00
|
|
|
throw Error("overly long symlink starting with '%1%'", std::string_view(buf, bufsiz));
|
2016-07-20 21:00:36 +03:00
|
|
|
}
|
|
|
|
if (res > 0 && buf[0] == '/')
|
2019-03-01 01:54:52 +02:00
|
|
|
roots[std::string(static_cast<char *>(buf), res)]
|
2019-03-14 14:27:16 +02:00
|
|
|
.emplace(file);
|
2016-07-20 21:00:36 +03:00
|
|
|
}
|
2006-07-20 15:17:25 +03:00
|
|
|
|
2022-02-25 17:00:00 +02:00
|
|
|
static std::string quoteRegexChars(const std::string & raw)
|
2016-07-20 21:00:36 +03:00
|
|
|
{
|
|
|
|
static auto specialRegex = std::regex(R"([.^$\\*+?()\[\]{}|])");
|
|
|
|
return std::regex_replace(raw, specialRegex, R"(\$&)");
|
|
|
|
}
|
2012-07-31 02:55:41 +03:00
|
|
|
|
2021-11-22 14:57:56 +02:00
|
|
|
#if __linux__
|
2019-12-05 20:11:09 +02:00
|
|
|
static void readFileRoots(const char * path, UncheckedRoots & roots)
|
2016-07-20 21:00:36 +03:00
|
|
|
{
|
|
|
|
try {
|
2019-03-01 01:54:52 +02:00
|
|
|
roots[readFile(path)].emplace(path);
|
2016-07-20 21:00:36 +03:00
|
|
|
} catch (SysError & e) {
|
|
|
|
if (e.errNo != ENOENT && e.errNo != EACCES)
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
2021-11-19 16:22:31 +02:00
|
|
|
#endif
|
2006-07-20 15:17:25 +03:00
|
|
|
|
2019-03-14 14:50:07 +02:00
|
|
|
void LocalStore::findRuntimeRoots(Roots & roots, bool censor)
|
2016-07-20 21:00:36 +03:00
|
|
|
{
|
2019-12-05 20:11:09 +02:00
|
|
|
UncheckedRoots unchecked;
|
2019-02-28 00:32:12 +02:00
|
|
|
|
2016-07-20 21:00:36 +03: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 23:39:27 +02:00
|
|
|
while (errno = 0, ent = readdir(procDir.get())) {
|
2016-07-20 21:00:36 +03:00
|
|
|
checkInterrupt();
|
|
|
|
if (std::regex_match(ent->d_name, digitsRegex)) {
|
|
|
|
try {
|
2023-03-09 17:44:51 +02:00
|
|
|
readProcLink(fmt("/proc/%s/exe" ,ent->d_name), unchecked);
|
|
|
|
readProcLink(fmt("/proc/%s/cwd", ent->d_name), unchecked);
|
|
|
|
|
|
|
|
auto fdStr = fmt("/proc/%s/fd", ent->d_name);
|
|
|
|
auto fdDir = AutoCloseDir(opendir(fdStr.c_str()));
|
|
|
|
if (!fdDir) {
|
|
|
|
if (errno == ENOENT || errno == EACCES)
|
|
|
|
continue;
|
|
|
|
throw SysError("opening %1%", fdStr);
|
|
|
|
}
|
|
|
|
struct dirent * fd_ent;
|
|
|
|
while (errno = 0, fd_ent = readdir(fdDir.get())) {
|
|
|
|
if (fd_ent->d_name[0] != '.')
|
|
|
|
readProcLink(fmt("%s/%s", fdStr, fd_ent->d_name), unchecked);
|
|
|
|
}
|
|
|
|
if (errno) {
|
|
|
|
if (errno == ESRCH)
|
|
|
|
continue;
|
|
|
|
throw SysError("iterating /proc/%1%/fd", ent->d_name);
|
|
|
|
}
|
|
|
|
fdDir.reset();
|
|
|
|
|
2019-03-14 14:27:16 +02:00
|
|
|
auto mapFile = fmt("/proc/%s/maps", ent->d_name);
|
2022-02-25 17:00:00 +02:00
|
|
|
auto mapLines = tokenizeString<std::vector<std::string>>(readFile(mapFile), "\n");
|
2019-03-14 14:27:16 +02:00
|
|
|
for (const auto & line : mapLines) {
|
2018-06-11 16:59:32 +03:00
|
|
|
auto match = std::smatch{};
|
|
|
|
if (std::regex_match(line, match, mapRegex))
|
2019-03-14 14:27:16 +02:00
|
|
|
unchecked[match[1]].emplace(mapFile);
|
2018-06-11 16:59:32 +03:00
|
|
|
}
|
|
|
|
|
2019-03-14 14:27:16 +02:00
|
|
|
auto envFile = fmt("/proc/%s/environ", ent->d_name);
|
2020-04-29 19:42:19 +03:00
|
|
|
auto envString = readFile(envFile);
|
2016-07-20 21:00:36 +03: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 14:27:16 +02:00
|
|
|
unchecked[i->str()].emplace(envFile);
|
2023-12-02 00:03:28 +02:00
|
|
|
} catch (SystemError & e) {
|
2018-06-11 16:59:32 +03:00
|
|
|
if (errno == ENOENT || errno == EACCES || errno == ESRCH)
|
2016-07-20 21:00:36 +03:00
|
|
|
continue;
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (errno)
|
|
|
|
throw SysError("iterating /proc");
|
|
|
|
}
|
|
|
|
|
2017-04-20 20:11:45 +03:00
|
|
|
#if !defined(__linux__)
|
2019-07-30 12:29:03 +03: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-05 01:41:18 +02:00
|
|
|
if (getEnv("_NIX_TEST_NO_LSOF") != "1") {
|
2019-07-30 12:29:03 +03:00
|
|
|
try {
|
|
|
|
std::regex lsofRegex(R"(^n(/.*)$)");
|
|
|
|
auto lsofLines =
|
2022-02-28 16:21:03 +02:00
|
|
|
tokenizeString<std::vector<std::string>>(runProgram(LSOF, true, { "-n", "-w", "-F", "n" }), "\n");
|
2019-07-30 12:29:03 +03: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 21:00:36 +03:00
|
|
|
}
|
|
|
|
}
|
2017-04-20 20:11:45 +03:00
|
|
|
#endif
|
2006-07-20 15:17:25 +03:00
|
|
|
|
2021-11-22 14:57:56 +02:00
|
|
|
#if __linux__
|
2019-02-28 00:32:12 +02:00
|
|
|
readFileRoots("/proc/sys/kernel/modprobe", unchecked);
|
|
|
|
readFileRoots("/proc/sys/kernel/fbsplash", unchecked);
|
|
|
|
readFileRoots("/proc/sys/kernel/poweroff_cmd", unchecked);
|
2017-04-20 20:11:45 +03:00
|
|
|
#endif
|
2012-07-31 02:55:41 +03:00
|
|
|
|
2019-03-01 01:54:52 +02:00
|
|
|
for (auto & [target, links] : unchecked) {
|
2019-12-05 20:11:09 +02:00
|
|
|
if (!isInStore(target)) continue;
|
2020-07-13 17:19:37 +03: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-28 00:32:12 +02:00
|
|
|
}
|
2006-07-20 15:17:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-18 17:20:16 +03:00
|
|
|
struct GCLimitReached { };
|
|
|
|
|
|
|
|
|
2021-10-14 14:52:49 +03:00
|
|
|
void LocalStore::collectGarbage(const GCOptions & options, GCResults & results)
|
2008-06-13 21:25:24 +03:00
|
|
|
{
|
2021-10-14 14:52:49 +03: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 17:20:16 +03:00
|
|
|
|
2021-08-16 21:03:32 +03: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 18:34:24 +02:00
|
|
|
|
2021-08-16 21:03:32 +03:00
|
|
|
// Hash part of the store path currently being deleted, if
|
|
|
|
// any.
|
|
|
|
std::optional<std::string> pending;
|
|
|
|
};
|
2008-09-17 15:54:07 +03:00
|
|
|
|
2021-10-14 14:52:49 +03:00
|
|
|
Sync<Shared> _shared;
|
2012-03-26 21:00:02 +03:00
|
|
|
|
2021-08-16 21:03:32 +03:00
|
|
|
std::condition_variable wakeup;
|
|
|
|
|
2010-03-08 23:31:42 +02:00
|
|
|
/* Using `--ignore-liveness' with `--delete' can have unintended
|
2017-08-31 15:28:25 +03: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 23:31:42 +02:00
|
|
|
if (options.action == GCOptions::gcDeleteSpecific && options.ignoreLiveness) {
|
2021-10-14 14:52:49 +03:00
|
|
|
gcKeepOutputs = false;
|
|
|
|
gcKeepDerivations = false;
|
2010-03-08 23:31:42 +02:00
|
|
|
}
|
2012-07-31 02:55:41 +03:00
|
|
|
|
2021-10-14 14:52:49 +03:00
|
|
|
if (shouldDelete)
|
2016-02-24 18:33:53 +02:00
|
|
|
deletePath(reservedPath);
|
|
|
|
|
2021-10-14 14:52:49 +03:00
|
|
|
/* Acquire the global GC root. Note: we don't use fdGCLock
|
2021-08-19 23:43:43 +03: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 21:03:32 +03:00
|
|
|
|
2023-06-19 17:54:05 +03:00
|
|
|
/* Synchronisation point to test ENOENT handling in
|
|
|
|
addTempRoot(), see tests/gc-non-blocking.sh. */
|
2024-01-12 13:38:55 +02:00
|
|
|
if (auto p = getEnv("_NIX_TEST_GC_SYNC_1"))
|
2023-06-19 17:54:05 +03:00
|
|
|
readFile(*p);
|
|
|
|
|
2021-08-16 21:03:32 +03: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 12:18:35 +03:00
|
|
|
Sync<std::map<int, std::thread>> connections;
|
|
|
|
|
|
|
|
Finally cleanup([&]() {
|
|
|
|
debug("GC roots server shutting down");
|
2022-11-27 13:57:18 +02:00
|
|
|
fdServer.close();
|
2021-08-20 12:18:35 +03:00
|
|
|
while (true) {
|
|
|
|
auto item = remove_begin(*connections.lock());
|
|
|
|
if (!item) break;
|
|
|
|
auto & [fd, thread] = *item;
|
|
|
|
shutdown(fd, SHUT_RDWR);
|
|
|
|
thread.join();
|
|
|
|
}
|
|
|
|
});
|
2021-08-16 21:03:32 +03:00
|
|
|
|
2021-08-20 12:18:35 +03:00
|
|
|
while (true) {
|
2021-08-16 21:03:32 +03: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 12:18:35 +03: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 18:12:32 +02:00
|
|
|
debug("GC roots server accepted new client");
|
|
|
|
|
2021-08-20 12:18:35 +03:00
|
|
|
/* Process the connection in a separate thread. */
|
|
|
|
auto fdClient_ = fdClient.get();
|
|
|
|
std::thread clientThread([&, fdClient = std::move(fdClient)]() {
|
|
|
|
Finally cleanup([&]() {
|
2021-10-15 17:12:21 +03:00
|
|
|
auto conn(connections.lock());
|
|
|
|
auto i = conn->find(fdClient.get());
|
2021-10-15 17:36:48 +03:00
|
|
|
if (i != conn->end()) {
|
|
|
|
i->second.detach();
|
|
|
|
conn->erase(i);
|
|
|
|
}
|
2021-08-20 12:18:35 +03:00
|
|
|
});
|
|
|
|
|
2021-12-13 18:16:46 +02:00
|
|
|
/* On macOS, accepted sockets inherit the
|
|
|
|
non-blocking flag from the server socket, so
|
|
|
|
explicitly make it blocking. */
|
2023-06-20 12:19:14 +03:00
|
|
|
if (fcntl(fdClient.get(), F_SETFL, fcntl(fdClient.get(), F_GETFL) & ~O_NONBLOCK) == -1)
|
2021-12-13 18:16:46 +02:00
|
|
|
abort();
|
|
|
|
|
2021-08-20 12:18:35 +03: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 14:52:49 +03:00
|
|
|
auto shared(_shared.lock());
|
2021-08-20 12:18:35 +03: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 14:52:49 +03:00
|
|
|
shared.wait(wakeup);
|
2021-08-20 12:18:35 +03:00
|
|
|
}
|
|
|
|
} else
|
|
|
|
printError("received garbage instead of a root from client");
|
|
|
|
writeFull(fdClient.get(), "1", false);
|
2021-12-13 18:12:32 +02:00
|
|
|
} catch (Error & e) {
|
|
|
|
debug("reading GC root from client: %s", e.msg());
|
|
|
|
break;
|
|
|
|
}
|
2021-08-16 21:03:32 +03:00
|
|
|
}
|
2021-08-20 12:18:35 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
connections.lock()->insert({fdClient_, std::move(clientThread)});
|
2021-08-16 21:03:32 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Finally stopServer([&]() {
|
|
|
|
writeFull(shutdownPipe.writeSide.get(), "x", false);
|
2021-10-14 14:52:49 +03:00
|
|
|
wakeup.notify_all();
|
2021-08-16 21:03:32 +03:00
|
|
|
if (serverThread.joinable()) serverThread.join();
|
|
|
|
});
|
2005-01-31 12:27:25 +02:00
|
|
|
|
2005-02-01 17:05:32 +02:00
|
|
|
/* Find the roots. Since we've grabbed the GC lock, the set of
|
|
|
|
permanent roots cannot increase now. */
|
2020-05-13 18:52:36 +03:00
|
|
|
printInfo("finding garbage collector roots...");
|
2019-03-10 01:37:52 +02:00
|
|
|
Roots rootMap;
|
|
|
|
if (!options.ignoreLiveness)
|
2019-03-14 14:50:07 +02:00
|
|
|
findRootsNoTemp(rootMap, true);
|
2006-12-05 02:48:36 +02:00
|
|
|
|
2021-10-14 14:52:49 +03:00
|
|
|
for (auto & i : rootMap) roots.insert(i.first);
|
2005-02-01 17:05:32 +02:00
|
|
|
|
2021-08-16 21:03:32 +03:00
|
|
|
/* Read the temporary roots created before we acquired the global
|
|
|
|
GC root. Any new roots will be sent to our socket. */
|
2019-03-10 01:37:52 +02:00
|
|
|
Roots tempRoots;
|
2021-08-16 21:03:32 +03:00
|
|
|
findTempRoots(tempRoots, true);
|
2019-12-05 20:11:09 +02:00
|
|
|
for (auto & root : tempRoots) {
|
2021-10-14 14:52:49 +03:00
|
|
|
_shared.lock()->tempRoots.insert(std::string(root.first.hashPart()));
|
|
|
|
roots.insert(root.first);
|
2019-12-05 20:11:09 +02:00
|
|
|
}
|
2005-01-31 16:00:43 +02:00
|
|
|
|
2024-01-12 13:38:55 +02:00
|
|
|
/* Synchronisation point for testing, see tests/functional/gc-non-blocking.sh. */
|
|
|
|
if (auto p = getEnv("_NIX_TEST_GC_SYNC_2"))
|
|
|
|
readFile(*p);
|
|
|
|
|
2021-10-14 14:34:48 +03: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);
|
|
|
|
|
2022-09-07 01:48:00 +03:00
|
|
|
/* There may be temp directories in the store that are still in use
|
|
|
|
by another process. We need to be sure that we can acquire an
|
|
|
|
exclusive lock before deleting them. */
|
2022-09-12 19:33:23 +03:00
|
|
|
if (baseName.find("tmp-", 0) == 0) {
|
|
|
|
AutoCloseFD tmpDirFd = open(realPath.c_str(), O_RDONLY | O_DIRECTORY);
|
2022-09-07 01:48:00 +03:00
|
|
|
if (tmpDirFd.get() == -1 || !lockFile(tmpDirFd.get(), ltWrite, false)) {
|
|
|
|
debug("skipping locked tempdir '%s'", realPath);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-14 14:34:48 +03:00
|
|
|
printInfo("deleting '%1%'", path);
|
|
|
|
|
2021-10-14 14:52:49 +03:00
|
|
|
results.paths.insert(path);
|
2021-10-14 14:34:48 +03:00
|
|
|
|
|
|
|
uint64_t bytesFreed;
|
2023-07-26 15:29:31 +03:00
|
|
|
deleteStorePath(realPath, bytesFreed);
|
2023-05-24 13:24:07 +03:00
|
|
|
|
2021-10-14 14:52:49 +03:00
|
|
|
results.bytesFreed += bytesFreed;
|
2021-10-14 14:34:48 +03:00
|
|
|
|
2021-10-14 14:52:49 +03:00
|
|
|
if (results.bytesFreed > options.maxFreed) {
|
|
|
|
printInfo("deleted more than %d bytes; stopping", options.maxFreed);
|
2021-10-14 14:34:48 +03:00
|
|
|
throw GCLimitReached();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-10-15 13:20:23 +03:00
|
|
|
std::map<StorePath, StorePathSet> referrersCache;
|
|
|
|
|
2021-10-14 13:31:21 +03:00
|
|
|
/* Helper function that visits all paths reachable from `start`
|
|
|
|
via the referrers edges and optionally derivers and derivation
|
2021-10-14 14:34:48 +03:00
|
|
|
output edges. If none of those paths are roots, then all
|
|
|
|
visited paths are garbage and are deleted. */
|
2021-10-14 13:31:21 +03: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 14:52:49 +03:00
|
|
|
auto shared(_shared.lock());
|
2021-10-14 13:31:21 +03:00
|
|
|
shared->pending.reset();
|
2021-10-14 14:52:49 +03:00
|
|
|
wakeup.notify_all();
|
2021-10-14 13:31:21 +03: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 14:52:49 +03:00
|
|
|
if (alive.count(*path)) {
|
|
|
|
alive.insert(start);
|
2021-10-14 13:31:21 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we've previously deleted this path, we don't have to
|
|
|
|
handle it again. */
|
2021-10-14 14:52:49 +03:00
|
|
|
if (dead.count(*path)) continue;
|
2021-10-14 13:31:21 +03:00
|
|
|
|
2021-10-14 15:13:57 +03:00
|
|
|
auto markAlive = [&]()
|
|
|
|
{
|
|
|
|
alive.insert(*path);
|
|
|
|
alive.insert(start);
|
|
|
|
try {
|
|
|
|
StorePathSet closure;
|
2022-03-17 20:36:45 +02:00
|
|
|
computeFSClosure(*path, closure,
|
|
|
|
/* flipDirection */ false, gcKeepOutputs, gcKeepDerivations);
|
2021-10-14 15:13:57 +03:00
|
|
|
for (auto & p : closure)
|
|
|
|
alive.insert(p);
|
|
|
|
} catch (InvalidPath &) { }
|
|
|
|
};
|
|
|
|
|
2021-10-14 13:31:21 +03:00
|
|
|
/* If this is a root, bail out. */
|
2021-10-14 14:52:49 +03:00
|
|
|
if (roots.count(*path)) {
|
2021-10-14 13:31:21 +03:00
|
|
|
debug("cannot delete '%s' because it's a root", printStorePath(*path));
|
2021-10-14 15:13:57 +03:00
|
|
|
return markAlive();
|
2021-10-14 13:31:21 +03:00
|
|
|
}
|
2009-11-23 18:34:24 +02:00
|
|
|
|
2021-10-14 14:52:49 +03:00
|
|
|
if (options.action == GCOptions::gcDeleteSpecific
|
|
|
|
&& !options.pathsToDelete.count(*path))
|
2021-10-14 13:31:21 +03:00
|
|
|
return;
|
|
|
|
|
|
|
|
{
|
|
|
|
auto hashPart = std::string(path->hashPart());
|
2021-10-14 14:52:49 +03:00
|
|
|
auto shared(_shared.lock());
|
2021-10-14 13:31:21 +03:00
|
|
|
if (shared->tempRoots.count(hashPart)) {
|
|
|
|
debug("cannot delete '%s' because it's a temporary root", printStorePath(*path));
|
2021-10-14 15:13:57 +03:00
|
|
|
return markAlive();
|
2021-10-14 13:31:21 +03:00
|
|
|
}
|
|
|
|
shared->pending = hashPart;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isValidPath(*path)) {
|
|
|
|
|
|
|
|
/* Visit the referrers of this path. */
|
2021-10-15 13:20:23 +03:00
|
|
|
auto i = referrersCache.find(*path);
|
|
|
|
if (i == referrersCache.end()) {
|
|
|
|
StorePathSet referrers;
|
2023-07-26 00:09:23 +03:00
|
|
|
queryGCReferrers(*path, referrers);
|
2021-10-15 13:20:23 +03:00
|
|
|
referrersCache.emplace(*path, std::move(referrers));
|
|
|
|
i = referrersCache.find(*path);
|
|
|
|
}
|
|
|
|
for (auto & p : i->second)
|
2021-10-14 13:31:21 +03:00
|
|
|
enqueue(p);
|
|
|
|
|
|
|
|
/* If keep-derivations is set and this is a
|
|
|
|
derivation, then visit the derivation outputs. */
|
2021-10-14 14:52:49 +03:00
|
|
|
if (gcKeepDerivations && path->isDerivation()) {
|
2021-10-14 13:31:21 +03: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 14:52:49 +03:00
|
|
|
if (gcKeepOutputs) {
|
2021-10-14 13:31:21 +03:00
|
|
|
auto derivers = queryValidDerivers(*path);
|
|
|
|
for (auto & i : derivers)
|
|
|
|
enqueue(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto & path : topoSortPaths(visited)) {
|
2021-10-14 14:52:49 +03:00
|
|
|
if (!dead.insert(path).second) continue;
|
|
|
|
if (shouldDelete) {
|
2021-10-14 13:31:21 +03:00
|
|
|
invalidatePathChecked(path);
|
2021-10-14 14:34:48 +03:00
|
|
|
deleteFromStore(path.to_string());
|
2021-10-15 13:20:23 +03:00
|
|
|
referrersCache.erase(path);
|
2021-10-14 13:31:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Either delete all garbage paths, or just the specified
|
|
|
|
paths (for gcDeleteSpecific). */
|
2009-11-23 18:34:24 +02:00
|
|
|
if (options.action == GCOptions::gcDeleteSpecific) {
|
2008-09-17 13:02:55 +03:00
|
|
|
|
2015-07-17 20:24:28 +03:00
|
|
|
for (auto & i : options.pathsToDelete) {
|
2021-10-14 13:31:21 +03:00
|
|
|
deleteReferrersClosure(i);
|
2021-10-14 14:52:49 +03:00
|
|
|
if (!dead.count(i))
|
2019-12-05 20:11:09 +02:00
|
|
|
throw Error(
|
2021-10-14 13:31:21 +03:00
|
|
|
"Cannot delete path '%1%' since it is still alive. "
|
2021-08-16 21:03:32 +03:00
|
|
|
"To find out why, use: "
|
2019-12-05 20:11:09 +02:00
|
|
|
"nix-store --query --roots",
|
|
|
|
printStorePath(i));
|
2005-12-23 23:08:42 +02:00
|
|
|
}
|
2012-07-31 02:55:41 +03:00
|
|
|
|
2012-08-02 02:14:30 +03:00
|
|
|
} else if (options.maxFreed > 0) {
|
2012-07-31 02:55:41 +03:00
|
|
|
|
2021-10-14 14:52:49 +03:00
|
|
|
if (shouldDelete)
|
2020-05-13 18:52:36 +03:00
|
|
|
printInfo("deleting garbage...");
|
2009-11-23 18:34:24 +02:00
|
|
|
else
|
2020-05-13 18:52:36 +03:00
|
|
|
printInfo("determining live/dead paths...");
|
2012-07-31 02:55:41 +03:00
|
|
|
|
2009-11-23 18:34:24 +02:00
|
|
|
try {
|
2021-06-18 18:04:11 +03:00
|
|
|
AutoCloseDir dir(opendir(realStoreDir.get().c_str()));
|
2020-04-22 02:07:07 +03:00
|
|
|
if (!dir) throw SysError("opening directory '%1%'", realStoreDir);
|
2011-12-22 17:55:53 +02:00
|
|
|
|
2021-08-16 21:03:32 +03: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 15:58:35 +03:00
|
|
|
auto linksName = baseNameOf(linksDir);
|
2011-12-22 17:55:53 +02:00
|
|
|
Paths entries;
|
|
|
|
struct dirent * dirent;
|
2017-01-16 23:39:27 +02:00
|
|
|
while (errno = 0, dirent = readdir(dir.get())) {
|
2011-12-22 17:55:53 +02:00
|
|
|
checkInterrupt();
|
2022-02-25 17:00:00 +02:00
|
|
|
std::string name = dirent->d_name;
|
2021-10-08 15:58:35 +03:00
|
|
|
if (name == "." || name == ".." || name == linksName) continue;
|
|
|
|
|
2021-10-14 13:31:21 +03:00
|
|
|
if (auto storePath = maybeParseStorePath(storeDir + "/" + name))
|
|
|
|
deleteReferrersClosure(*storePath);
|
|
|
|
else
|
2021-10-14 14:34:48 +03:00
|
|
|
deleteFromStore(name);
|
2021-10-08 15:58:35 +03:00
|
|
|
|
2011-12-22 17:55:53 +02:00
|
|
|
}
|
2009-11-23 18:34:24 +02:00
|
|
|
} catch (GCLimitReached & e) {
|
|
|
|
}
|
2011-12-22 17:55:53 +02:00
|
|
|
}
|
2012-03-26 21:43:33 +03:00
|
|
|
|
2021-10-14 14:52:49 +03:00
|
|
|
if (options.action == GCOptions::gcReturnLive) {
|
|
|
|
for (auto & i : alive)
|
|
|
|
results.paths.insert(printStorePath(i));
|
2012-12-20 18:32:15 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-10-14 14:52:49 +03:00
|
|
|
if (options.action == GCOptions::gcReturnDead) {
|
|
|
|
for (auto & i : dead)
|
|
|
|
results.paths.insert(printStorePath(i));
|
2012-12-20 18:32:15 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-10-14 14:52:49 +03: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-13 01:49:35 +03:00
|
|
|
if (options.action == GCOptions::gcDeleteDead || options.action == GCOptions::gcDeleteSpecific) {
|
2020-05-13 18:52:36 +03:00
|
|
|
printInfo("deleting unused links...");
|
2021-10-14 14:52:49 +03: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 17:00:00 +02:00
|
|
|
std::string name = dirent->d_name;
|
2021-10-14 14:52:49 +03: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;
|
|
|
|
}
|
|
|
|
|
2023-03-02 16:44:19 +02:00
|
|
|
printMsg(lvlTalkative, "deleting unused link '%1%'", path);
|
2021-10-14 14:52:49 +03:00
|
|
|
|
|
|
|
if (unlink(path.c_str()) == -1)
|
|
|
|
throw SysError("deleting '%1%'", path);
|
|
|
|
|
2023-05-24 13:24:07 +03:00
|
|
|
/* Do not account for deleted file here. Rely on deletePath()
|
2022-03-12 23:56:08 +02:00
|
|
|
accounting. */
|
2021-10-14 14:52:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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-13 01:49:35 +03:00
|
|
|
}
|
2012-09-13 21:33:41 +03:00
|
|
|
|
|
|
|
/* While we're at it, vacuum the database. */
|
2014-11-19 19:02:39 +02:00
|
|
|
//if (options.action == GCOptions::gcDeleteDead) vacuumDB();
|
2004-08-25 14:43:49 +03:00
|
|
|
}
|
2006-09-05 00:06:23 +03:00
|
|
|
|
2008-09-17 13:02:55 +03:00
|
|
|
|
2017-09-05 21:43:42 +03:00
|
|
|
void LocalStore::autoGC(bool sync)
|
|
|
|
{
|
2019-11-22 17:06:44 +02:00
|
|
|
static auto fakeFreeSpaceFile = getEnv("_NIX_TEST_FREE_SPACE_FILE");
|
2019-08-02 18:07:33 +03:00
|
|
|
|
|
|
|
auto getAvail = [this]() -> uint64_t {
|
2019-11-22 17:06:44 +02:00
|
|
|
if (fakeFreeSpaceFile)
|
|
|
|
return std::stoll(readFile(*fakeFreeSpaceFile));
|
2019-08-02 18:07:33 +03:00
|
|
|
|
2017-09-05 21:43:42 +03:00
|
|
|
struct statvfs st;
|
2021-06-18 18:04:11 +03:00
|
|
|
if (statvfs(realStoreDir.get().c_str(), &st))
|
2017-09-05 21:43:42 +03:00
|
|
|
throw SysError("getting filesystem info about '%s'", realStoreDir);
|
|
|
|
|
2020-05-05 00:42:06 +03:00
|
|
|
return (uint64_t) st.f_bavail * st.f_frsize;
|
2017-09-05 21:43:42 +03: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 18:07:33 +03:00
|
|
|
if (now < state->lastGCCheck + std::chrono::seconds(settings.minFreeCheckInterval)) return;
|
2017-09-05 21:43:42 +03: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 12:37:58 +03:00
|
|
|
try {
|
2017-09-05 21:43:42 +03:00
|
|
|
|
2017-09-06 12:37:58 +03: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 21:43:42 +03:00
|
|
|
|
2017-09-06 12:37:58 +03:00
|
|
|
GCOptions options;
|
|
|
|
options.maxFreed = settings.maxFree - avail;
|
2017-09-05 21:43:42 +03:00
|
|
|
|
2019-08-29 13:09:58 +03:00
|
|
|
printInfo("running auto-GC to free %d bytes", options.maxFreed);
|
|
|
|
|
2017-09-06 12:37:58 +03:00
|
|
|
GCResults results;
|
2017-09-05 21:43:42 +03:00
|
|
|
|
2017-09-06 12:37:58 +03: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 21:43:42 +03:00
|
|
|
|
|
|
|
}).detach();
|
|
|
|
}
|
|
|
|
|
|
|
|
sync:
|
|
|
|
// Wait for the future outside of the state lock.
|
|
|
|
if (sync) future.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-05 00:06:23 +03:00
|
|
|
}
|