2007-03-30 16:24:35 +03:00
|
|
|
|
#include "config.h"
|
|
|
|
|
|
2016-02-09 22:07:48 +02:00
|
|
|
|
#include "common-args.hh"
|
2006-09-05 00:06:23 +03:00
|
|
|
|
#include "globals.hh"
|
2016-02-09 22:07:48 +02:00
|
|
|
|
#include "shared.hh"
|
2006-11-30 19:43:04 +02:00
|
|
|
|
#include "store-api.hh"
|
2006-09-05 00:06:23 +03:00
|
|
|
|
#include "util.hh"
|
|
|
|
|
|
2016-02-22 15:49:15 +02:00
|
|
|
|
#include <algorithm>
|
2003-07-24 11:53:43 +03:00
|
|
|
|
#include <cctype>
|
2007-05-01 18:16:17 +03:00
|
|
|
|
#include <exception>
|
2016-02-22 15:49:15 +02:00
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <mutex>
|
2003-07-04 18:42:03 +03:00
|
|
|
|
|
2014-12-14 02:51:14 +02:00
|
|
|
|
#include <cstdlib>
|
2012-12-11 12:49:42 +02:00
|
|
|
|
#include <sys/time.h>
|
2004-03-27 19:58:04 +02:00
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
#include <unistd.h>
|
2013-07-31 00:25:37 +03:00
|
|
|
|
#include <signal.h>
|
2004-03-27 19:58:04 +02:00
|
|
|
|
|
2016-02-22 15:49:15 +02:00
|
|
|
|
#include <openssl/crypto.h>
|
2014-12-05 22:05:24 +02:00
|
|
|
|
|
2003-07-04 18:42:03 +03:00
|
|
|
|
|
2006-09-05 00:06:23 +03:00
|
|
|
|
namespace nix {
|
2003-07-04 18:42:03 +03:00
|
|
|
|
|
|
|
|
|
|
2004-05-11 21:05:44 +03:00
|
|
|
|
volatile sig_atomic_t blockInt = 0;
|
|
|
|
|
|
|
|
|
|
|
2006-03-11 00:27:26 +02:00
|
|
|
|
static void sigintHandler(int signo)
|
2004-01-15 22:23:55 +02:00
|
|
|
|
{
|
2004-05-11 21:05:44 +03:00
|
|
|
|
if (!blockInt) {
|
|
|
|
|
_isInterrupted = 1;
|
|
|
|
|
blockInt = 1;
|
|
|
|
|
}
|
2004-01-15 22:23:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2013-12-20 14:19:10 +02:00
|
|
|
|
static bool gcWarning = true;
|
|
|
|
|
|
2005-02-01 14:36:25 +02:00
|
|
|
|
void printGCWarning()
|
|
|
|
|
{
|
2013-12-20 14:19:10 +02:00
|
|
|
|
if (!gcWarning) return;
|
2006-08-29 18:40:49 +03:00
|
|
|
|
static bool haveWarned = false;
|
2012-07-31 00:13:25 +03:00
|
|
|
|
warnOnce(haveWarned,
|
2014-08-20 18:00:17 +03:00
|
|
|
|
"you did not specify ‘--add-root’; "
|
2006-08-29 18:40:49 +03:00
|
|
|
|
"the result might be removed by the garbage collector");
|
2005-02-01 14:36:25 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2016-02-04 15:48:42 +02:00
|
|
|
|
void printMissing(ref<Store> store, const PathSet & paths)
|
2008-08-04 16:44:46 +03:00
|
|
|
|
{
|
2010-11-17 16:31:42 +02:00
|
|
|
|
unsigned long long downloadSize, narSize;
|
2008-08-04 16:44:46 +03:00
|
|
|
|
PathSet willBuild, willSubstitute, unknown;
|
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
|
|
|
|
store->queryMissing(paths, willBuild, willSubstitute, unknown, downloadSize, narSize);
|
2016-01-28 16:16:06 +02:00
|
|
|
|
printMissing(store, willBuild, willSubstitute, unknown, downloadSize, narSize);
|
2012-11-20 01:27:25 +02:00
|
|
|
|
}
|
|
|
|
|
|
2008-08-04 16:44:46 +03:00
|
|
|
|
|
2016-02-04 15:48:42 +02:00
|
|
|
|
void printMissing(ref<Store> store, const PathSet & willBuild,
|
2012-11-20 01:27:25 +02:00
|
|
|
|
const PathSet & willSubstitute, const PathSet & unknown,
|
|
|
|
|
unsigned long long downloadSize, unsigned long long narSize)
|
|
|
|
|
{
|
2008-08-04 16:44:46 +03:00
|
|
|
|
if (!willBuild.empty()) {
|
2010-11-17 16:31:42 +02:00
|
|
|
|
printMsg(lvlInfo, format("these derivations will be built:"));
|
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
|
|
|
|
Paths sorted = store->topoSortPaths(willBuild);
|
2014-09-26 15:09:20 +03:00
|
|
|
|
reverse(sorted.begin(), sorted.end());
|
|
|
|
|
for (auto & i : sorted)
|
|
|
|
|
printMsg(lvlInfo, format(" %1%") % i);
|
2008-08-04 16:44:46 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!willSubstitute.empty()) {
|
2011-11-29 15:00:41 +02:00
|
|
|
|
printMsg(lvlInfo, format("these paths will be fetched (%.2f MiB download, %.2f MiB unpacked):")
|
2010-11-17 16:31:42 +02:00
|
|
|
|
% (downloadSize / (1024.0 * 1024.0))
|
|
|
|
|
% (narSize / (1024.0 * 1024.0)));
|
2014-09-26 15:09:20 +03:00
|
|
|
|
for (auto & i : willSubstitute)
|
|
|
|
|
printMsg(lvlInfo, format(" %1%") % i);
|
2008-08-04 16:44:46 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!unknown.empty()) {
|
2010-11-17 16:31:42 +02:00
|
|
|
|
printMsg(lvlInfo, format("don't know how to build these paths%1%:")
|
2012-07-31 02:55:41 +03:00
|
|
|
|
% (settings.readOnlyMode ? " (may be caused by read-only store access)" : ""));
|
2014-09-26 15:09:20 +03:00
|
|
|
|
for (auto & i : unknown)
|
|
|
|
|
printMsg(lvlInfo, format(" %1%") % i);
|
2008-08-04 16:44:46 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2012-07-31 02:55:41 +03:00
|
|
|
|
string getArg(const string & opt,
|
|
|
|
|
Strings::iterator & i, const Strings::iterator & end)
|
|
|
|
|
{
|
|
|
|
|
++i;
|
2014-08-20 18:00:17 +03:00
|
|
|
|
if (i == end) throw UsageError(format("‘%1%’ requires an argument") % opt);
|
2012-07-31 02:55:41 +03:00
|
|
|
|
return *i;
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-03 23:37:06 +03:00
|
|
|
|
|
2016-02-22 15:49:15 +02:00
|
|
|
|
/* OpenSSL is not thread-safe by default - it will randomly crash
|
|
|
|
|
unless the user supplies a mutex locking function. So let's do
|
|
|
|
|
that. */
|
|
|
|
|
static std::vector<std::mutex> opensslLocks;
|
|
|
|
|
|
|
|
|
|
static void opensslLockCallback(int mode, int type, const char * file, int line)
|
|
|
|
|
{
|
|
|
|
|
if (mode & CRYPTO_LOCK)
|
|
|
|
|
opensslLocks[type].lock();
|
|
|
|
|
else
|
|
|
|
|
opensslLocks[type].unlock();
|
|
|
|
|
}
|
2013-07-31 00:25:37 +03:00
|
|
|
|
|
|
|
|
|
|
2014-08-13 04:50:44 +03:00
|
|
|
|
void initNix()
|
2003-07-04 18:42:03 +03:00
|
|
|
|
{
|
2014-08-13 04:50:44 +03:00
|
|
|
|
/* Turn on buffering for cerr. */
|
|
|
|
|
#if HAVE_PUBSETBUF
|
|
|
|
|
static char buf[1024];
|
|
|
|
|
std::cerr.rdbuf()->pubsetbuf(buf, sizeof(buf));
|
|
|
|
|
#endif
|
|
|
|
|
|
2015-07-20 02:39:48 +03:00
|
|
|
|
if (getEnv("IN_SYSTEMD") == "1")
|
|
|
|
|
logType = ltSystemd;
|
|
|
|
|
|
2016-02-22 15:49:15 +02:00
|
|
|
|
/* Initialise OpenSSL locking. */
|
|
|
|
|
opensslLocks = std::vector<std::mutex>(CRYPTO_num_locks());
|
|
|
|
|
CRYPTO_set_locking_callback(opensslLockCallback);
|
2015-07-20 02:39:48 +03:00
|
|
|
|
|
2012-07-31 02:55:41 +03:00
|
|
|
|
settings.processEnvironment();
|
|
|
|
|
settings.loadConfFile();
|
2012-07-31 00:13:25 +03:00
|
|
|
|
|
2004-01-15 22:23:55 +02:00
|
|
|
|
/* Catch SIGINT. */
|
2010-01-12 14:22:38 +02:00
|
|
|
|
struct sigaction act;
|
2004-01-15 22:23:55 +02:00
|
|
|
|
act.sa_handler = sigintHandler;
|
2014-03-29 21:20:14 +02:00
|
|
|
|
sigemptyset(&act.sa_mask);
|
2004-01-15 22:23:55 +02:00
|
|
|
|
act.sa_flags = 0;
|
2010-01-12 14:22:38 +02:00
|
|
|
|
if (sigaction(SIGINT, &act, 0))
|
2004-01-15 22:23:55 +02:00
|
|
|
|
throw SysError("installing handler for SIGINT");
|
2010-01-12 14:22:38 +02:00
|
|
|
|
if (sigaction(SIGTERM, &act, 0))
|
2005-11-04 17:34:09 +02:00
|
|
|
|
throw SysError("installing handler for SIGTERM");
|
2010-01-12 14:22:38 +02:00
|
|
|
|
if (sigaction(SIGHUP, &act, 0))
|
2005-11-04 17:34:09 +02:00
|
|
|
|
throw SysError("installing handler for SIGHUP");
|
2004-01-15 22:23:55 +02:00
|
|
|
|
|
2004-05-13 22:14:49 +03:00
|
|
|
|
/* Ignore SIGPIPE. */
|
|
|
|
|
act.sa_handler = SIG_IGN;
|
|
|
|
|
act.sa_flags = 0;
|
2010-01-12 14:22:38 +02:00
|
|
|
|
if (sigaction(SIGPIPE, &act, 0))
|
2004-05-13 22:14:49 +03:00
|
|
|
|
throw SysError("ignoring SIGPIPE");
|
|
|
|
|
|
2010-01-12 14:22:38 +02:00
|
|
|
|
/* Reset SIGCHLD to its default. */
|
|
|
|
|
act.sa_handler = SIG_DFL;
|
|
|
|
|
act.sa_flags = 0;
|
|
|
|
|
if (sigaction(SIGCHLD, &act, 0))
|
|
|
|
|
throw SysError("resetting SIGCHLD");
|
|
|
|
|
|
2013-07-31 00:25:37 +03:00
|
|
|
|
/* Register a SIGSEGV handler to detect stack overflows. */
|
|
|
|
|
detectStackOverflow();
|
|
|
|
|
|
2004-09-09 17:16:02 +03:00
|
|
|
|
/* There is no privacy in the Nix system ;-) At least not for
|
|
|
|
|
now. In particular, store objects should be readable by
|
2012-10-04 00:30:45 +03:00
|
|
|
|
everybody. */
|
2004-09-09 17:16:02 +03:00
|
|
|
|
umask(0022);
|
|
|
|
|
|
2012-12-11 12:49:42 +02:00
|
|
|
|
/* Initialise the PRNG. */
|
|
|
|
|
struct timeval tv;
|
|
|
|
|
gettimeofday(&tv, 0);
|
|
|
|
|
srandom(tv.tv_usec);
|
|
|
|
|
|
2014-08-13 04:50:44 +03:00
|
|
|
|
if (char *pack = getenv("_NIX_OPTIONS"))
|
|
|
|
|
settings.unpack(pack);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2016-02-09 22:07:48 +02:00
|
|
|
|
struct LegacyArgs : public MixCommonArgs
|
2014-08-13 04:50:44 +03:00
|
|
|
|
{
|
2016-02-09 22:07:48 +02:00
|
|
|
|
std::function<bool(Strings::iterator & arg, const Strings::iterator & end)> parseArg;
|
|
|
|
|
|
|
|
|
|
LegacyArgs(const std::string & programName,
|
|
|
|
|
std::function<bool(Strings::iterator & arg, const Strings::iterator & end)> parseArg)
|
|
|
|
|
: MixCommonArgs(programName), parseArg(parseArg)
|
|
|
|
|
{
|
|
|
|
|
mkFlag('Q', "no-build-output", "do not show build output",
|
|
|
|
|
&settings.buildVerbosity, lvlVomit);
|
|
|
|
|
|
|
|
|
|
mkFlag(0, "print-build-trace", "emit special build trace message",
|
|
|
|
|
&settings.printBuildTrace);
|
|
|
|
|
|
|
|
|
|
mkFlag('K', "keep-failed", "keep temporary directories of failed builds",
|
|
|
|
|
&settings.keepFailed);
|
|
|
|
|
|
|
|
|
|
mkFlag('k', "keep-going", "keep going after a build fails",
|
|
|
|
|
&settings.keepGoing);
|
|
|
|
|
|
|
|
|
|
mkFlag(0, "fallback", "build from source if substitution fails", []() {
|
2012-08-01 00:56:02 +03:00
|
|
|
|
settings.set("build-fallback", "true");
|
2016-02-09 22:07:48 +02:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
auto intSettingAlias = [&](char shortName, const std::string & longName,
|
|
|
|
|
const std::string & description, const std::string & dest) {
|
|
|
|
|
mkFlag<unsigned int>(shortName, longName, description, [=](unsigned int n) {
|
|
|
|
|
settings.set(dest, std::to_string(n));
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
intSettingAlias('j', "max-jobs", "maximum number of parallel builds", "build-max-jobs");
|
|
|
|
|
intSettingAlias(0, "cores", "maximum number of CPU cores to use inside a build", "build-cores");
|
|
|
|
|
intSettingAlias(0, "max-silent-time", "number of seconds of silence before a build is killed", "build-max-silent-time");
|
|
|
|
|
intSettingAlias(0, "timeout", "number of seconds before a build is killed", "build-timeout");
|
|
|
|
|
|
|
|
|
|
mkFlag(0, "readonly-mode", "do not write to the Nix store",
|
|
|
|
|
&settings.readOnlyMode);
|
|
|
|
|
|
|
|
|
|
mkFlag(0, "no-build-hook", "disable use of the build hook mechanism",
|
|
|
|
|
&settings.useBuildHook, false);
|
|
|
|
|
|
|
|
|
|
mkFlag(0, "show-trace", "show Nix expression stack trace in evaluation errors",
|
|
|
|
|
&settings.showTrace);
|
|
|
|
|
|
|
|
|
|
mkFlag(0, "no-gc-warning", "disable warning about not using ‘--add-root’",
|
|
|
|
|
&gcWarning, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool processFlag(Strings::iterator & pos, Strings::iterator end) override
|
|
|
|
|
{
|
|
|
|
|
if (MixCommonArgs::processFlag(pos, end)) return true;
|
|
|
|
|
bool res = parseArg(pos, end);
|
|
|
|
|
if (res) ++pos;
|
|
|
|
|
return res;
|
2003-12-01 17:55:05 +02:00
|
|
|
|
}
|
|
|
|
|
|
2016-02-09 22:07:48 +02:00
|
|
|
|
bool processArgs(const Strings & args, bool finish) override
|
|
|
|
|
{
|
|
|
|
|
if (args.empty()) return true;
|
|
|
|
|
assert(args.size() == 1);
|
|
|
|
|
Strings ss(args);
|
|
|
|
|
auto pos = ss.begin();
|
|
|
|
|
if (!parseArg(pos, ss.end()))
|
|
|
|
|
throw UsageError(format("unexpected argument ‘%1%’") % args.front());
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void parseCmdLine(int argc, char * * argv,
|
|
|
|
|
std::function<bool(Strings::iterator & arg, const Strings::iterator & end)> parseArg)
|
|
|
|
|
{
|
|
|
|
|
LegacyArgs(baseNameOf(argv[0]), parseArg).parseCmdline(argvToStrings(argc, argv));
|
2012-07-31 02:55:41 +03:00
|
|
|
|
settings.update();
|
2014-08-13 04:50:44 +03:00
|
|
|
|
}
|
2012-07-31 02:55:41 +03:00
|
|
|
|
|
2006-03-01 18:36:35 +02:00
|
|
|
|
|
2014-08-13 04:50:44 +03:00
|
|
|
|
void printVersion(const string & programName)
|
|
|
|
|
{
|
|
|
|
|
std::cout << format("%1% (Nix) %2%") % programName % nixVersion << std::endl;
|
2015-07-23 15:19:49 +03:00
|
|
|
|
if (verbosity > lvlInfo) {
|
|
|
|
|
Strings cfg;
|
|
|
|
|
#if HAVE_BOEHMGC
|
|
|
|
|
cfg.push_back("gc");
|
|
|
|
|
#endif
|
|
|
|
|
#if HAVE_SODIUM
|
|
|
|
|
cfg.push_back("signed-caches");
|
|
|
|
|
#endif
|
|
|
|
|
std::cout << "Features: " << concatStringsSep(", ", cfg) << "\n";
|
|
|
|
|
std::cout << "Configuration file: " << settings.nixConfDir + "/nix.conf" << "\n";
|
|
|
|
|
std::cout << "Store directory: " << settings.nixStore << "\n";
|
|
|
|
|
std::cout << "State directory: " << settings.nixStateDir << "\n";
|
|
|
|
|
std::cout << "Database directory: " << settings.nixDBPath << "\n";
|
|
|
|
|
}
|
2014-08-13 04:50:44 +03:00
|
|
|
|
throw Exit();
|
2003-07-04 18:42:03 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2012-10-03 23:37:06 +03:00
|
|
|
|
void showManPage(const string & name)
|
|
|
|
|
{
|
2014-07-31 11:31:17 +03:00
|
|
|
|
restoreSIGPIPE();
|
|
|
|
|
execlp("man", "man", name.c_str(), NULL);
|
2014-08-20 18:00:17 +03:00
|
|
|
|
throw SysError(format("command ‘man %1%’ failed") % name.c_str());
|
2012-10-03 23:37:06 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2014-08-13 04:50:44 +03:00
|
|
|
|
int handleExceptions(const string & programName, std::function<void()> fun)
|
2003-07-04 18:42:03 +03:00
|
|
|
|
{
|
2014-08-20 17:01:16 +03:00
|
|
|
|
string error = ANSI_RED "error:" ANSI_NORMAL " ";
|
2003-07-04 18:42:03 +03:00
|
|
|
|
try {
|
2004-05-11 21:05:44 +03:00
|
|
|
|
try {
|
2014-08-13 04:50:44 +03:00
|
|
|
|
fun();
|
2004-05-11 21:05:44 +03:00
|
|
|
|
} catch (...) {
|
|
|
|
|
/* Subtle: we have to make sure that any `interrupted'
|
|
|
|
|
condition is discharged before we reach printMsg()
|
|
|
|
|
below, since otherwise it will throw an (uncaught)
|
|
|
|
|
exception. */
|
|
|
|
|
blockInt = 1; /* ignore further SIGINTs */
|
|
|
|
|
_isInterrupted = 0;
|
|
|
|
|
throw;
|
|
|
|
|
}
|
2014-08-13 04:50:44 +03:00
|
|
|
|
} catch (Exit & e) {
|
|
|
|
|
return e.status;
|
2003-07-04 18:42:03 +03:00
|
|
|
|
} catch (UsageError & e) {
|
2012-07-31 00:13:25 +03:00
|
|
|
|
printMsg(lvlError,
|
2014-10-14 12:41:04 +03:00
|
|
|
|
format(error + "%1%\nTry ‘%2% --help’ for more information.")
|
2014-08-13 04:50:44 +03:00
|
|
|
|
% e.what() % programName);
|
2003-07-04 18:42:03 +03:00
|
|
|
|
return 1;
|
2007-08-12 03:29:28 +03:00
|
|
|
|
} catch (BaseError & e) {
|
2014-08-20 17:01:16 +03:00
|
|
|
|
printMsg(lvlError, format(error + "%1%%2%") % (settings.showTrace ? e.prefix() : "") % e.msg());
|
2013-11-12 13:51:59 +02:00
|
|
|
|
if (e.prefix() != "" && !settings.showTrace)
|
2014-08-20 18:00:17 +03:00
|
|
|
|
printMsg(lvlError, "(use ‘--show-trace’ to show detailed location information)");
|
2010-12-13 18:53:23 +02:00
|
|
|
|
return e.status;
|
2013-10-02 15:34:36 +03:00
|
|
|
|
} catch (std::bad_alloc & e) {
|
2014-08-20 17:01:16 +03:00
|
|
|
|
printMsg(lvlError, error + "out of memory");
|
2013-10-02 15:34:36 +03:00
|
|
|
|
return 1;
|
2006-09-05 00:06:23 +03:00
|
|
|
|
} catch (std::exception & e) {
|
2014-08-20 17:01:16 +03:00
|
|
|
|
printMsg(lvlError, error + e.what());
|
2003-07-04 18:42:03 +03:00
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-13 04:50:44 +03:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2014-08-20 16:12:58 +03:00
|
|
|
|
RunPager::RunPager()
|
|
|
|
|
{
|
2014-12-10 19:16:05 +02:00
|
|
|
|
if (!isatty(STDOUT_FILENO)) return;
|
2015-01-02 16:26:56 +02:00
|
|
|
|
char * pager = getenv("NIX_PAGER");
|
|
|
|
|
if (!pager) pager = getenv("PAGER");
|
|
|
|
|
if (pager && ((string) pager == "" || (string) pager == "cat")) return;
|
2014-08-20 16:12:58 +03:00
|
|
|
|
|
|
|
|
|
/* Ignore SIGINT. The pager will handle it (and we'll get
|
|
|
|
|
SIGPIPE). */
|
|
|
|
|
struct sigaction act;
|
|
|
|
|
act.sa_handler = SIG_IGN;
|
|
|
|
|
act.sa_flags = 0;
|
|
|
|
|
sigemptyset(&act.sa_mask);
|
|
|
|
|
if (sigaction(SIGINT, &act, 0)) throw SysError("ignoring SIGINT");
|
|
|
|
|
|
|
|
|
|
restoreSIGPIPE();
|
|
|
|
|
|
|
|
|
|
Pipe toPager;
|
|
|
|
|
toPager.create();
|
|
|
|
|
|
2014-12-10 14:48:50 +02:00
|
|
|
|
pid = startProcess([&]() {
|
|
|
|
|
if (dup2(toPager.readSide, STDIN_FILENO) == -1)
|
|
|
|
|
throw SysError("dupping stdin");
|
|
|
|
|
if (!getenv("LESS"))
|
|
|
|
|
setenv("LESS", "FRSXMK", 1);
|
2015-01-02 16:26:56 +02:00
|
|
|
|
if (pager)
|
|
|
|
|
execl("/bin/sh", "sh", "-c", pager, NULL);
|
2014-12-10 19:16:05 +02:00
|
|
|
|
execlp("pager", "pager", NULL);
|
|
|
|
|
execlp("less", "less", NULL);
|
|
|
|
|
execlp("more", "more", NULL);
|
2014-12-10 14:48:50 +02:00
|
|
|
|
throw SysError(format("executing ‘%1%’") % pager);
|
|
|
|
|
});
|
2014-08-20 16:12:58 +03:00
|
|
|
|
|
|
|
|
|
if (dup2(toPager.writeSide, STDOUT_FILENO) == -1)
|
|
|
|
|
throw SysError("dupping stdout");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
RunPager::~RunPager()
|
|
|
|
|
{
|
2014-12-12 15:05:23 +02:00
|
|
|
|
try {
|
|
|
|
|
if (pid != -1) {
|
|
|
|
|
std::cout.flush();
|
|
|
|
|
close(STDOUT_FILENO);
|
|
|
|
|
pid.wait(true);
|
|
|
|
|
}
|
|
|
|
|
} catch (...) {
|
|
|
|
|
ignoreException();
|
2014-08-20 16:12:58 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2015-05-21 16:21:38 +03:00
|
|
|
|
string showBytes(unsigned long long bytes)
|
|
|
|
|
{
|
|
|
|
|
return (format("%.2f MiB") % (bytes / (1024.0 * 1024.0))).str();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
PrintFreed::~PrintFreed()
|
|
|
|
|
{
|
|
|
|
|
if (show)
|
|
|
|
|
std::cout << format("%1% store paths deleted, %2% freed\n")
|
|
|
|
|
% results.paths.size()
|
|
|
|
|
% showBytes(results.bytesFreed);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2003-07-04 18:42:03 +03:00
|
|
|
|
}
|