aboutsummaryrefslogtreecommitdiff
path: root/src/libutil
diff options
context:
space:
mode:
authorBen Burdette <bburdette@gmail.com>2022-01-03 16:08:28 -0700
committerBen Burdette <bburdette@gmail.com>2022-01-03 16:08:28 -0700
commita47de1ac37841c29e1a4a7d3a9c50e96390ebaf6 (patch)
tree54819b34b9e090cf32c1ba6865ab9a0af60a9182 /src/libutil
parent5954cbf3e9dca0e3b84e4bf2def74abb3d6f80cd (diff)
parent96d08fcd66e2c38598bab4f39a37a98d58347467 (diff)
Merge branch 'master' into debug-exploratory-PR
Diffstat (limited to 'src/libutil')
-rw-r--r--src/libutil/abstract-setting-to-json.hh1
-rw-r--r--src/libutil/affinity.cc70
-rw-r--r--src/libutil/affinity.hh9
-rw-r--r--src/libutil/args.cc4
-rw-r--r--src/libutil/args.hh8
-rw-r--r--src/libutil/config.hh10
-rw-r--r--src/libutil/error.cc2
-rw-r--r--src/libutil/logging.hh5
-rw-r--r--src/libutil/tarfile.cc9
-rw-r--r--src/libutil/tests/config.cc2
-rw-r--r--src/libutil/thread-pool.cc3
-rw-r--r--src/libutil/util.cc13
-rw-r--r--src/libutil/util.hh8
13 files changed, 46 insertions, 98 deletions
diff --git a/src/libutil/abstract-setting-to-json.hh b/src/libutil/abstract-setting-to-json.hh
index b3fbc84f7..2d82b54e7 100644
--- a/src/libutil/abstract-setting-to-json.hh
+++ b/src/libutil/abstract-setting-to-json.hh
@@ -10,6 +10,7 @@ std::map<std::string, nlohmann::json> BaseSetting<T>::toJSONObject()
auto obj = AbstractSetting::toJSONObject();
obj.emplace("value", value);
obj.emplace("defaultValue", defaultValue);
+ obj.emplace("documentDefault", documentDefault);
return obj;
}
}
diff --git a/src/libutil/affinity.cc b/src/libutil/affinity.cc
deleted file mode 100644
index ac2295e4a..000000000
--- a/src/libutil/affinity.cc
+++ /dev/null
@@ -1,70 +0,0 @@
-#include "types.hh"
-#include "util.hh"
-#include "affinity.hh"
-
-#if __linux__
-#include <sched.h>
-#endif
-
-namespace nix {
-
-
-#if __linux__
-static bool didSaveAffinity = false;
-static cpu_set_t savedAffinity;
-
-std::ostream& operator<<(std::ostream &os, const cpu_set_t &cset)
-{
- auto count = CPU_COUNT(&cset);
- for (int i=0; i < count; ++i)
- {
- os << (CPU_ISSET(i,&cset) ? "1" : "0");
- }
-
- return os;
-}
-#endif
-
-
-void setAffinityTo(int cpu)
-{
-#if __linux__
- if (sched_getaffinity(0, sizeof(cpu_set_t), &savedAffinity) == -1) return;
- didSaveAffinity = true;
- debug(format("locking this thread to CPU %1%") % cpu);
- cpu_set_t newAffinity;
- CPU_ZERO(&newAffinity);
- CPU_SET(cpu, &newAffinity);
- if (sched_setaffinity(0, sizeof(cpu_set_t), &newAffinity) == -1)
- printError("failed to lock thread to CPU %1%", cpu);
-#endif
-}
-
-
-int lockToCurrentCPU()
-{
-#if __linux__
- int cpu = sched_getcpu();
- if (cpu != -1) setAffinityTo(cpu);
- return cpu;
-#else
- return -1;
-#endif
-}
-
-
-void restoreAffinity()
-{
-#if __linux__
- if (!didSaveAffinity) return;
- if (sched_setaffinity(0, sizeof(cpu_set_t), &savedAffinity) == -1)
- {
- std::ostringstream oss;
- oss << savedAffinity;
- printError("failed to restore CPU affinity %1%", oss.str());
- }
-#endif
-}
-
-
-}
diff --git a/src/libutil/affinity.hh b/src/libutil/affinity.hh
deleted file mode 100644
index c1bd28e13..000000000
--- a/src/libutil/affinity.hh
+++ /dev/null
@@ -1,9 +0,0 @@
-#pragma once
-
-namespace nix {
-
-void setAffinityTo(int cpu);
-int lockToCurrentCPU();
-void restoreAffinity();
-
-}
diff --git a/src/libutil/args.cc b/src/libutil/args.cc
index 9df279faf..a739d6b4e 100644
--- a/src/libutil/args.cc
+++ b/src/libutil/args.cc
@@ -39,7 +39,7 @@ void Completions::add(std::string completion, std::string description)
bool Completion::operator<(const Completion & other) const
{ return completion < other.completion || (completion == other.completion && description < other.description); }
-bool pathCompletions = false;
+CompletionType completionType = ctNormal;
std::shared_ptr<Completions> completions;
std::string completionMarker = "___COMPLETE___";
@@ -277,7 +277,7 @@ Args::Flag Args::Flag::mkHashTypeOptFlag(std::string && longName, std::optional<
static void _completePath(std::string_view prefix, bool onlyDirs)
{
- pathCompletions = true;
+ completionType = ctFilenames;
glob_t globbuf;
int flags = GLOB_NOESCAPE | GLOB_TILDE;
#ifdef GLOB_ONLYDIR
diff --git a/src/libutil/args.hh b/src/libutil/args.hh
index 7521b3065..76b1cfe92 100644
--- a/src/libutil/args.hh
+++ b/src/libutil/args.hh
@@ -237,7 +237,13 @@ public:
void add(std::string completion, std::string description = "");
};
extern std::shared_ptr<Completions> completions;
-extern bool pathCompletions;
+
+enum CompletionType {
+ ctNormal,
+ ctFilenames,
+ ctAttrs
+};
+extern CompletionType completionType;
std::optional<std::string> needsCompletion(std::string_view s);
diff --git a/src/libutil/config.hh b/src/libutil/config.hh
index 736810bf3..79ec0f9cf 100644
--- a/src/libutil/config.hh
+++ b/src/libutil/config.hh
@@ -232,16 +232,19 @@ protected:
T value;
const T defaultValue;
+ const bool documentDefault;
public:
BaseSetting(const T & def,
+ const bool documentDefault,
const std::string & name,
const std::string & description,
const std::set<std::string> & aliases = {})
: AbstractSetting(name, description, aliases)
, value(def)
, defaultValue(def)
+ , documentDefault(documentDefault)
{ }
operator const T &() const { return value; }
@@ -288,8 +291,9 @@ public:
const T & def,
const std::string & name,
const std::string & description,
- const std::set<std::string> & aliases = {})
- : BaseSetting<T>(def, name, description, aliases)
+ const std::set<std::string> & aliases = {},
+ const bool documentDefault = true)
+ : BaseSetting<T>(def, documentDefault, name, description, aliases)
{
options->addSetting(this);
}
@@ -311,7 +315,7 @@ public:
const std::string & name,
const std::string & description,
const std::set<std::string> & aliases = {})
- : BaseSetting<Path>(def, name, description, aliases)
+ : BaseSetting<Path>(def, true, name, description, aliases)
, allowEmpty(allowEmpty)
{
options->addSetting(this);
diff --git a/src/libutil/error.cc b/src/libutil/error.cc
index 203d79087..dd9678ee7 100644
--- a/src/libutil/error.cc
+++ b/src/libutil/error.cc
@@ -25,7 +25,7 @@ const string & BaseError::calcWhat() const
err.name = sname();
std::ostringstream oss;
- showErrorInfo(oss, err, false);
+ showErrorInfo(oss, err, loggerSettings.showTrace);
what_ = oss.str();
return *what_;
diff --git a/src/libutil/logging.hh b/src/libutil/logging.hh
index 96ad69790..ce9c3dfed 100644
--- a/src/libutil/logging.hh
+++ b/src/libutil/logging.hh
@@ -189,13 +189,14 @@ extern Verbosity verbosity; /* suppress msgs > this */
/* Print a string message if the current log level is at least the specified
level. Note that this has to be implemented as a macro to ensure that the
arguments are evaluated lazily. */
-#define printMsg(level, args...) \
+#define printMsgUsing(loggerParam, level, args...) \
do { \
auto __lvl = level; \
if (__lvl <= nix::verbosity) { \
- logger->log(__lvl, fmt(args)); \
+ loggerParam->log(__lvl, fmt(args)); \
} \
} while (0)
+#define printMsg(level, args...) printMsgUsing(logger, level, args)
#define printError(args...) printMsg(lvlError, args)
#define notice(args...) printMsg(lvlNotice, args)
diff --git a/src/libutil/tarfile.cc b/src/libutil/tarfile.cc
index 50e691a3d..790bc943a 100644
--- a/src/libutil/tarfile.cc
+++ b/src/libutil/tarfile.cc
@@ -93,9 +93,16 @@ static void extract_archive(TarArchive & archive, const Path & destDir)
else
archive.check(r);
- archive_entry_set_pathname(entry,
+ archive_entry_copy_pathname(entry,
(destDir + "/" + name).c_str());
+ // Patch hardlink path
+ const char *original_hardlink = archive_entry_hardlink(entry);
+ if (original_hardlink) {
+ archive_entry_copy_hardlink(entry,
+ (destDir + "/" + original_hardlink).c_str());
+ }
+
archive.check(archive_read_extract(archive.archive, entry, flags));
}
diff --git a/src/libutil/tests/config.cc b/src/libutil/tests/config.cc
index 0ebdaf3db..8be6730dd 100644
--- a/src/libutil/tests/config.cc
+++ b/src/libutil/tests/config.cc
@@ -161,7 +161,7 @@ namespace nix {
Setting<std::string> setting{&config, "", "name-of-the-setting", "description"};
setting.assign("value");
- ASSERT_EQ(config.toJSON().dump(), R"#({"name-of-the-setting":{"aliases":[],"defaultValue":"","description":"description\n","value":"value"}})#");
+ ASSERT_EQ(config.toJSON().dump(), R"#({"name-of-the-setting":{"aliases":[],"defaultValue":"","description":"description\n","documentDefault":true,"value":"value"}})#");
}
TEST(Config, setSettingAlias) {
diff --git a/src/libutil/thread-pool.cc b/src/libutil/thread-pool.cc
index 857ee91f8..dc4067f1b 100644
--- a/src/libutil/thread-pool.cc
+++ b/src/libutil/thread-pool.cc
@@ -1,13 +1,10 @@
#include "thread-pool.hh"
-#include "affinity.hh"
namespace nix {
ThreadPool::ThreadPool(size_t _maxThreads)
: maxThreads(_maxThreads)
{
- restoreAffinity(); // FIXME
-
if (!maxThreads) {
maxThreads = std::thread::hardware_concurrency();
if (!maxThreads) maxThreads = 1;
diff --git a/src/libutil/util.cc b/src/libutil/util.cc
index defb77a10..e18648557 100644
--- a/src/libutil/util.cc
+++ b/src/libutil/util.cc
@@ -1,5 +1,4 @@
#include "util.hh"
-#include "affinity.hh"
#include "sync.hh"
#include "finally.hh"
#include "serialise.hh"
@@ -512,6 +511,7 @@ std::pair<AutoCloseFD, Path> createTempFile(const Path & prefix)
AutoCloseFD fd(mkstemp((char *) tmpl.c_str()));
if (!fd)
throw SysError("creating temporary file '%s'", tmpl);
+ closeOnExec(fd.get());
return {std::move(fd), tmpl};
}
@@ -1003,7 +1003,6 @@ pid_t startProcess(std::function<void()> fun, const ProcessOptions & options)
if (options.dieWithParent && prctl(PR_SET_PDEATHSIG, SIGKILL) == -1)
throw SysError("setting death signal");
#endif
- restoreAffinity();
fun();
} catch (std::exception & e) {
try {
@@ -1659,6 +1658,14 @@ void restoreMountNamespace()
#endif
}
+void unshareFilesystem()
+{
+#ifdef __linux__
+ if (unshare(CLONE_FS) != 0 && errno != EPERM)
+ throw SysError("unsharing filesystem state in download thread");
+#endif
+}
+
void restoreProcessContext(bool restoreMounts)
{
restoreSignals();
@@ -1666,8 +1673,6 @@ void restoreProcessContext(bool restoreMounts)
restoreMountNamespace();
}
- restoreAffinity();
-
#if __linux__
if (savedStackSize) {
struct rlimit limit;
diff --git a/src/libutil/util.hh b/src/libutil/util.hh
index 0bdb37a79..d08f42826 100644
--- a/src/libutil/util.hh
+++ b/src/libutil/util.hh
@@ -11,6 +11,7 @@
#include <unistd.h>
#include <signal.h>
+#include <atomic>
#include <functional>
#include <map>
#include <sstream>
@@ -299,7 +300,7 @@ void setStackSize(size_t stackSize);
/* Restore the original inherited Unix process context (such as signal
- masks, stack size, CPU affinity). */
+ masks, stack size). */
void restoreProcessContext(bool restoreMounts = true);
/* Save the current mount namespace. Ignored if called more than
@@ -310,6 +311,11 @@ void saveMountNamespace();
if saveMountNamespace() was never called. */
void restoreMountNamespace();
+/* Cause this thread to not share any FS attributes with the main
+ thread, because this causes setns() in restoreMountNamespace() to
+ fail. */
+void unshareFilesystem();
+
class ExecError : public Error
{