| 1 |
#include "config.h" |
| 2 |
|
| 3 |
#include "references.hh" |
| 4 |
#include "pathlocks.hh" |
| 5 |
#include "misc.hh" |
| 6 |
#include "globals.hh" |
| 7 |
#include "local-store.hh" |
| 8 |
#include "util.hh" |
| 9 |
#include "archive.hh" |
| 10 |
#include "affinity.hh" |
| 11 |
#include "builtins.hh" |
| 12 |
|
| 13 |
#include <map> |
| 14 |
#include <sstream> |
| 15 |
#include <algorithm> |
| 16 |
|
| 17 |
#include <limits.h> |
| 18 |
#include <time.h> |
| 19 |
#include <sys/time.h> |
| 20 |
#include <sys/wait.h> |
| 21 |
#include <sys/types.h> |
| 22 |
#include <sys/stat.h> |
| 23 |
#include <sys/utsname.h> |
| 24 |
#include <fcntl.h> |
| 25 |
#include <unistd.h> |
| 26 |
#include <errno.h> |
| 27 |
#include <stdio.h> |
| 28 |
#include <cstring> |
| 29 |
#include <stdint.h> |
| 30 |
|
| 31 |
#include <pwd.h> |
| 32 |
#include <grp.h> |
| 33 |
|
| 34 |
#include <zlib.h> |
| 35 |
|
| 36 |
#if HAVE_BZLIB_H |
| 37 |
# include <bzlib.h> |
| 38 |
#endif |
| 39 |
|
| 40 |
/* Includes required for chroot support. */ |
| 41 |
#if HAVE_SYS_PARAM_H |
| 42 |
#include <sys/param.h> |
| 43 |
#endif |
| 44 |
#if HAVE_SYS_MOUNT_H |
| 45 |
#include <sys/mount.h> |
| 46 |
#endif |
| 47 |
#if HAVE_SYS_SYSCALL_H |
| 48 |
#include <sys/syscall.h> |
| 49 |
#endif |
| 50 |
#if HAVE_SCHED_H |
| 51 |
#include <sched.h> |
| 52 |
#endif |
| 53 |
|
| 54 |
|
| 55 |
#define CHROOT_ENABLED HAVE_CHROOT && HAVE_SYS_MOUNT_H && defined(MS_BIND) && defined(MS_PRIVATE) |
| 56 |
#define CLONE_ENABLED defined(CLONE_NEWNS) |
| 57 |
|
| 58 |
#if defined(SYS_pivot_root) |
| 59 |
#define pivot_root(new_root, put_old) (syscall(SYS_pivot_root, new_root,put_old)) |
| 60 |
#endif |
| 61 |
|
| 62 |
#if CHROOT_ENABLED |
| 63 |
#include <sys/socket.h> |
| 64 |
#include <sys/ioctl.h> |
| 65 |
#include <net/if.h> |
| 66 |
#include <netinet/ip.h> |
| 67 |
#endif |
| 68 |
|
| 69 |
#if __linux__ |
| 70 |
#include <sys/personality.h> |
| 71 |
#endif |
| 72 |
|
| 73 |
#if HAVE_STATVFS |
| 74 |
#include <sys/statvfs.h> |
| 75 |
#endif |
| 76 |
|
| 77 |
|
| 78 |
namespace nix { |
| 79 |
|
| 80 |
using std::map; |
| 81 |
|
| 82 |
|
| 83 |
/* Forward definition. */ |
| 84 |
class Worker; |
| 85 |
|
| 86 |
|
| 87 |
/* A pointer to a goal. */ |
| 88 |
class Goal; |
| 89 |
class DerivationGoal; |
| 90 |
typedef std::shared_ptr<Goal> GoalPtr; |
| 91 |
typedef std::weak_ptr<Goal> WeakGoalPtr; |
| 92 |
|
| 93 |
struct CompareGoalPtrs { |
| 94 |
bool operator() (const GoalPtr & a, const GoalPtr & b); |
| 95 |
}; |
| 96 |
|
| 97 |
/* Set of goals. */ |
| 98 |
typedef set<GoalPtr, CompareGoalPtrs> Goals; |
| 99 |
typedef list<WeakGoalPtr> WeakGoals; |
| 100 |
|
| 101 |
/* A map of paths to goals (and the other way around). */ |
| 102 |
typedef map<Path, WeakGoalPtr> WeakGoalMap; |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
class Goal : public std::enable_shared_from_this<Goal> |
| 107 |
{ |
| 108 |
public: |
| 109 |
typedef enum {ecBusy, ecSuccess, ecFailed, ecNoSubstituters, ecIncompleteClosure} ExitCode; |
| 110 |
|
| 111 |
protected: |
| 112 |
|
| 113 |
/* Backlink to the worker. */ |
| 114 |
Worker & worker; |
| 115 |
|
| 116 |
/* Goals that this goal is waiting for. */ |
| 117 |
Goals waitees; |
| 118 |
|
| 119 |
/* Goals waiting for this one to finish. Must use weak pointers |
| 120 |
here to prevent cycles. */ |
| 121 |
WeakGoals waiters; |
| 122 |
|
| 123 |
/* Number of goals we are/were waiting for that have failed. */ |
| 124 |
unsigned int nrFailed; |
| 125 |
|
| 126 |
/* Number of substitution goals we are/were waiting for that |
| 127 |
failed because there are no substituters. */ |
| 128 |
unsigned int nrNoSubstituters; |
| 129 |
|
| 130 |
/* Number of substitution goals we are/were waiting for that |
| 131 |
failed because othey had unsubstitutable references. */ |
| 132 |
unsigned int nrIncompleteClosure; |
| 133 |
|
| 134 |
/* Name of this goal for debugging purposes. */ |
| 135 |
string name; |
| 136 |
|
| 137 |
/* Whether the goal is finished. */ |
| 138 |
ExitCode exitCode; |
| 139 |
|
| 140 |
Goal(Worker & worker) : worker(worker) |
| 141 |
{ |
| 142 |
nrFailed = nrNoSubstituters = nrIncompleteClosure = 0; |
| 143 |
exitCode = ecBusy; |
| 144 |
} |
| 145 |
|
| 146 |
virtual ~Goal() |
| 147 |
{ |
| 148 |
trace("goal destroyed"); |
| 149 |
} |
| 150 |
|
| 151 |
public: |
| 152 |
virtual void work() = 0; |
| 153 |
|
| 154 |
void addWaitee(GoalPtr waitee); |
| 155 |
|
| 156 |
virtual void waiteeDone(GoalPtr waitee, ExitCode result); |
| 157 |
|
| 158 |
virtual void handleChildOutput(int fd, const string & data) |
| 159 |
{ |
| 160 |
abort(); |
| 161 |
} |
| 162 |
|
| 163 |
virtual void handleEOF(int fd) |
| 164 |
{ |
| 165 |
abort(); |
| 166 |
} |
| 167 |
|
| 168 |
void trace(const format & f); |
| 169 |
|
| 170 |
string getName() |
| 171 |
{ |
| 172 |
return name; |
| 173 |
} |
| 174 |
|
| 175 |
ExitCode getExitCode() |
| 176 |
{ |
| 177 |
return exitCode; |
| 178 |
} |
| 179 |
|
| 180 |
/* Callback in case of a timeout. It should wake up its waiters, |
| 181 |
get rid of any running child processes that are being monitored |
| 182 |
by the worker (important!), etc. */ |
| 183 |
virtual void timedOut() = 0; |
| 184 |
|
| 185 |
virtual string key() = 0; |
| 186 |
|
| 187 |
protected: |
| 188 |
void amDone(ExitCode result); |
| 189 |
}; |
| 190 |
|
| 191 |
|
| 192 |
bool CompareGoalPtrs::operator() (const GoalPtr & a, const GoalPtr & b) { |
| 193 |
string s1 = a->key(); |
| 194 |
string s2 = b->key(); |
| 195 |
return s1 < s2; |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
/* A mapping used to remember for each child process to what goal it |
| 200 |
belongs, and file descriptors for receiving log data and output |
| 201 |
path creation commands. */ |
| 202 |
struct Child |
| 203 |
{ |
| 204 |
WeakGoalPtr goal; |
| 205 |
set<int> fds; |
| 206 |
bool respectTimeouts; |
| 207 |
bool inBuildSlot; |
| 208 |
time_t lastOutput; /* time we last got output on stdout/stderr */ |
| 209 |
time_t timeStarted; |
| 210 |
}; |
| 211 |
|
| 212 |
typedef map<pid_t, Child> Children; |
| 213 |
|
| 214 |
|
| 215 |
/* The worker class. */ |
| 216 |
class Worker |
| 217 |
{ |
| 218 |
private: |
| 219 |
|
| 220 |
/* Note: the worker should only have strong pointers to the |
| 221 |
top-level goals. */ |
| 222 |
|
| 223 |
/* The top-level goals of the worker. */ |
| 224 |
Goals topGoals; |
| 225 |
|
| 226 |
/* Goals that are ready to do some work. */ |
| 227 |
WeakGoals awake; |
| 228 |
|
| 229 |
/* Goals waiting for a build slot. */ |
| 230 |
WeakGoals wantingToBuild; |
| 231 |
|
| 232 |
/* Child processes currently running. */ |
| 233 |
Children children; |
| 234 |
|
| 235 |
/* Number of build slots occupied. This includes local builds and |
| 236 |
substitutions but not remote builds via the build hook. */ |
| 237 |
unsigned int nrLocalBuilds; |
| 238 |
|
| 239 |
/* Maps used to prevent multiple instantiations of a goal for the |
| 240 |
same derivation / path. */ |
| 241 |
WeakGoalMap derivationGoals; |
| 242 |
WeakGoalMap substitutionGoals; |
| 243 |
|
| 244 |
/* Goals waiting for busy paths to be unlocked. */ |
| 245 |
WeakGoals waitingForAnyGoal; |
| 246 |
|
| 247 |
/* Goals sleeping for a few seconds (polling a lock). */ |
| 248 |
WeakGoals waitingForAWhile; |
| 249 |
|
| 250 |
/* Last time the goals in `waitingForAWhile' where woken up. */ |
| 251 |
time_t lastWokenUp; |
| 252 |
|
| 253 |
public: |
| 254 |
|
| 255 |
/* Set if at least one derivation had a BuildError (i.e. permanent |
| 256 |
failure). */ |
| 257 |
bool permanentFailure; |
| 258 |
|
| 259 |
/* Set if at least one derivation had a timeout. */ |
| 260 |
bool timedOut; |
| 261 |
|
| 262 |
LocalStore & store; |
| 263 |
|
| 264 |
std::shared_ptr<Agent> hook; |
| 265 |
std::shared_ptr<Agent> substituter; |
| 266 |
|
| 267 |
Worker(LocalStore & store); |
| 268 |
~Worker(); |
| 269 |
|
| 270 |
/* Make a goal (with caching). */ |
| 271 |
GoalPtr makeDerivationGoal(const Path & drvPath, const StringSet & wantedOutputs, BuildMode buildMode = bmNormal); |
| 272 |
GoalPtr makeSubstitutionGoal(const Path & storePath, bool repair = false); |
| 273 |
|
| 274 |
/* Remove a dead goal. */ |
| 275 |
void removeGoal(GoalPtr goal); |
| 276 |
|
| 277 |
/* Wake up a goal (i.e., there is something for it to do). */ |
| 278 |
void wakeUp(GoalPtr goal); |
| 279 |
|
| 280 |
/* Return the number of local build and substitution processes |
| 281 |
currently running (but not remote builds via the build |
| 282 |
hook). */ |
| 283 |
unsigned int getNrLocalBuilds(); |
| 284 |
|
| 285 |
/* Registers a running child process. `inBuildSlot' means that |
| 286 |
the process counts towards the jobs limit. */ |
| 287 |
void childStarted(GoalPtr goal, pid_t pid, |
| 288 |
const set<int> & fds, bool inBuildSlot, bool respectTimeouts); |
| 289 |
|
| 290 |
/* Unregisters a running child process. `wakeSleepers' should be |
| 291 |
false if there is no sense in waking up goals that are sleeping |
| 292 |
because they can't run yet (e.g., there is no free build slot, |
| 293 |
or the hook would still say `postpone'). */ |
| 294 |
void childTerminated(pid_t pid, bool wakeSleepers = true); |
| 295 |
|
| 296 |
/* Put `goal' to sleep until a build slot becomes available (which |
| 297 |
might be right away). */ |
| 298 |
void waitForBuildSlot(GoalPtr goal); |
| 299 |
|
| 300 |
/* Wait for any goal to finish. Pretty indiscriminate way to |
| 301 |
wait for some resource that some other goal is holding. */ |
| 302 |
void waitForAnyGoal(GoalPtr goal); |
| 303 |
|
| 304 |
/* Wait for a few seconds and then retry this goal. Used when |
| 305 |
waiting for a lock held by another process. This kind of |
| 306 |
polling is inefficient, but POSIX doesn't really provide a way |
| 307 |
to wait for multiple locks in the main select() loop. */ |
| 308 |
void waitForAWhile(GoalPtr goal); |
| 309 |
|
| 310 |
/* Loop until the specified top-level goals have finished. */ |
| 311 |
void run(const Goals & topGoals); |
| 312 |
|
| 313 |
/* Wait for input to become available. */ |
| 314 |
void waitForInput(); |
| 315 |
|
| 316 |
unsigned int exitStatus(); |
| 317 |
}; |
| 318 |
|
| 319 |
|
| 320 |
////////////////////////////////////////////////////////////////////// |
| 321 |
|
| 322 |
|
| 323 |
void addToWeakGoals(WeakGoals & goals, GoalPtr p) |
| 324 |
{ |
| 325 |
// FIXME: necessary? |
| 326 |
// FIXME: O(n) |
| 327 |
foreach (WeakGoals::iterator, i, goals) |
| 328 |
if (i->lock() == p) return; |
| 329 |
goals.push_back(p); |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
void Goal::addWaitee(GoalPtr waitee) |
| 334 |
{ |
| 335 |
waitees.insert(waitee); |
| 336 |
addToWeakGoals(waitee->waiters, shared_from_this()); |
| 337 |
} |
| 338 |
|
| 339 |
|
| 340 |
void Goal::waiteeDone(GoalPtr waitee, ExitCode result) |
| 341 |
{ |
| 342 |
assert(waitees.find(waitee) != waitees.end()); |
| 343 |
waitees.erase(waitee); |
| 344 |
|
| 345 |
trace(format("waitee `%1%' done; %2% left") % |
| 346 |
waitee->name % waitees.size()); |
| 347 |
|
| 348 |
if (result == ecFailed || result == ecNoSubstituters || result == ecIncompleteClosure) ++nrFailed; |
| 349 |
|
| 350 |
if (result == ecNoSubstituters) ++nrNoSubstituters; |
| 351 |
|
| 352 |
if (result == ecIncompleteClosure) ++nrIncompleteClosure; |
| 353 |
|
| 354 |
if (waitees.empty() || (result == ecFailed && !settings.keepGoing)) { |
| 355 |
|
| 356 |
/* If we failed and keepGoing is not set, we remove all |
| 357 |
remaining waitees. */ |
| 358 |
foreach (Goals::iterator, i, waitees) { |
| 359 |
GoalPtr goal = *i; |
| 360 |
WeakGoals waiters2; |
| 361 |
foreach (WeakGoals::iterator, j, goal->waiters) |
| 362 |
if (j->lock() != shared_from_this()) waiters2.push_back(*j); |
| 363 |
goal->waiters = waiters2; |
| 364 |
} |
| 365 |
waitees.clear(); |
| 366 |
|
| 367 |
worker.wakeUp(shared_from_this()); |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
|
| 372 |
void Goal::amDone(ExitCode result) |
| 373 |
{ |
| 374 |
trace("done"); |
| 375 |
assert(exitCode == ecBusy); |
| 376 |
assert(result == ecSuccess || result == ecFailed || result == ecNoSubstituters || result == ecIncompleteClosure); |
| 377 |
exitCode = result; |
| 378 |
foreach (WeakGoals::iterator, i, waiters) { |
| 379 |
GoalPtr goal = i->lock(); |
| 380 |
if (goal) goal->waiteeDone(shared_from_this(), result); |
| 381 |
} |
| 382 |
waiters.clear(); |
| 383 |
worker.removeGoal(shared_from_this()); |
| 384 |
} |
| 385 |
|
| 386 |
|
| 387 |
void Goal::trace(const format & f) |
| 388 |
{ |
| 389 |
debug(format("%1%: %2%") % name % f); |
| 390 |
} |
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
////////////////////////////////////////////////////////////////////// |
| 395 |
|
| 396 |
|
| 397 |
/* Restore default handling of SIGPIPE, otherwise some programs will |
| 398 |
randomly say "Broken pipe". */ |
| 399 |
static void restoreSIGPIPE() |
| 400 |
{ |
| 401 |
struct sigaction act, oact; |
| 402 |
act.sa_handler = SIG_DFL; |
| 403 |
act.sa_flags = 0; |
| 404 |
sigemptyset(&act.sa_mask); |
| 405 |
if (sigaction(SIGPIPE, &act, &oact)) throw SysError("resetting SIGPIPE"); |
| 406 |
} |
| 407 |
|
| 408 |
|
| 409 |
////////////////////////////////////////////////////////////////////// |
| 410 |
|
| 411 |
|
| 412 |
class UserLock |
| 413 |
{ |
| 414 |
private: |
| 415 |
/* POSIX locks suck. If we have a lock on a file, and we open and |
| 416 |
close that file again (without closing the original file |
| 417 |
descriptor), we lose the lock. So we have to be *very* careful |
| 418 |
not to open a lock file on which we are holding a lock. */ |
| 419 |
static PathSet lockedPaths; /* !!! not thread-safe */ |
| 420 |
|
| 421 |
Path fnUserLock; |
| 422 |
AutoCloseFD fdUserLock; |
| 423 |
|
| 424 |
string user; |
| 425 |
uid_t uid; |
| 426 |
gid_t gid; |
| 427 |
std::vector<gid_t> supplementaryGIDs; |
| 428 |
|
| 429 |
public: |
| 430 |
UserLock(); |
| 431 |
~UserLock(); |
| 432 |
|
| 433 |
void acquire(); |
| 434 |
void release(); |
| 435 |
|
| 436 |
void kill(); |
| 437 |
|
| 438 |
string getUser() { return user; } |
| 439 |
uid_t getUID() { return uid; } |
| 440 |
uid_t getGID() { return gid; } |
| 441 |
std::vector<gid_t> getSupplementaryGIDs() { return supplementaryGIDs; } |
| 442 |
|
| 443 |
bool enabled() { return uid != 0; } |
| 444 |
|
| 445 |
}; |
| 446 |
|
| 447 |
|
| 448 |
PathSet UserLock::lockedPaths; |
| 449 |
|
| 450 |
|
| 451 |
UserLock::UserLock() |
| 452 |
{ |
| 453 |
uid = gid = 0; |
| 454 |
} |
| 455 |
|
| 456 |
|
| 457 |
UserLock::~UserLock() |
| 458 |
{ |
| 459 |
release(); |
| 460 |
} |
| 461 |
|
| 462 |
|
| 463 |
void UserLock::acquire() |
| 464 |
{ |
| 465 |
assert(uid == 0); |
| 466 |
|
| 467 |
assert(settings.buildUsersGroup != ""); |
| 468 |
|
| 469 |
/* Get the members of the build-users-group. */ |
| 470 |
struct group * gr = getgrnam(settings.buildUsersGroup.c_str()); |
| 471 |
if (!gr) |
| 472 |
throw Error(format("the group `%1%' specified in `build-users-group' does not exist") |
| 473 |
% settings.buildUsersGroup); |
| 474 |
gid = gr->gr_gid; |
| 475 |
|
| 476 |
/* Copy the result of getgrnam. */ |
| 477 |
Strings users; |
| 478 |
for (char * * p = gr->gr_mem; *p; ++p) { |
| 479 |
debug(format("found build user `%1%'") % *p); |
| 480 |
users.push_back(*p); |
| 481 |
} |
| 482 |
|
| 483 |
if (users.empty()) |
| 484 |
throw Error(format("the build users group `%1%' has no members") |
| 485 |
% settings.buildUsersGroup); |
| 486 |
|
| 487 |
/* Find a user account that isn't currently in use for another |
| 488 |
build. */ |
| 489 |
foreach (Strings::iterator, i, users) { |
| 490 |
debug(format("trying user `%1%'") % *i); |
| 491 |
|
| 492 |
struct passwd * pw = getpwnam(i->c_str()); |
| 493 |
if (!pw) |
| 494 |
throw Error(format("the user `%1%' in the group `%2%' does not exist") |
| 495 |
% *i % settings.buildUsersGroup); |
| 496 |
|
| 497 |
createDirs(settings.nixStateDir + "/userpool"); |
| 498 |
|
| 499 |
fnUserLock = (format("%1%/userpool/%2%") % settings.nixStateDir % pw->pw_uid).str(); |
| 500 |
|
| 501 |
if (lockedPaths.find(fnUserLock) != lockedPaths.end()) |
| 502 |
/* We already have a lock on this one. */ |
| 503 |
continue; |
| 504 |
|
| 505 |
AutoCloseFD fd = open(fnUserLock.c_str(), O_RDWR | O_CREAT, 0600); |
| 506 |
if (fd == -1) |
| 507 |
throw SysError(format("opening user lock `%1%'") % fnUserLock); |
| 508 |
closeOnExec(fd); |
| 509 |
|
| 510 |
if (lockFile(fd, ltWrite, false)) { |
| 511 |
fdUserLock = fd.borrow(); |
| 512 |
lockedPaths.insert(fnUserLock); |
| 513 |
user = *i; |
| 514 |
uid = pw->pw_uid; |
| 515 |
|
| 516 |
/* Sanity check... */ |
| 517 |
if (uid == getuid() || uid == geteuid()) |
| 518 |
throw Error(format("the build user should not be a member of `%1%'") |
| 519 |
% settings.buildUsersGroup); |
| 520 |
|
| 521 |
/* Get the list of supplementary groups of this build user. This |
| 522 |
is usually either empty or contains a group such as "kvm". */ |
| 523 |
supplementaryGIDs.resize(10); |
| 524 |
int ngroups = supplementaryGIDs.size(); |
| 525 |
int err = getgrouplist(pw->pw_name, pw->pw_gid, |
| 526 |
supplementaryGIDs.data(), &ngroups); |
| 527 |
if (err == -1) |
| 528 |
throw Error(format("failed to get list of supplementary groups for ‘%1%’") % pw->pw_name); |
| 529 |
|
| 530 |
supplementaryGIDs.resize(ngroups); |
| 531 |
|
| 532 |
return; |
| 533 |
} |
| 534 |
} |
| 535 |
|
| 536 |
throw Error(format("all build users are currently in use; " |
| 537 |
"consider creating additional users and adding them to the `%1%' group") |
| 538 |
% settings.buildUsersGroup); |
| 539 |
} |
| 540 |
|
| 541 |
|
| 542 |
void UserLock::release() |
| 543 |
{ |
| 544 |
if (uid == 0) return; |
| 545 |
fdUserLock.close(); /* releases lock */ |
| 546 |
assert(lockedPaths.find(fnUserLock) != lockedPaths.end()); |
| 547 |
lockedPaths.erase(fnUserLock); |
| 548 |
fnUserLock = ""; |
| 549 |
uid = 0; |
| 550 |
} |
| 551 |
|
| 552 |
|
| 553 |
void UserLock::kill() |
| 554 |
{ |
| 555 |
assert(enabled()); |
| 556 |
killUser(uid); |
| 557 |
} |
| 558 |
|
| 559 |
////////////////////////////////////////////////////////////////////// |
| 560 |
|
| 561 |
|
| 562 |
typedef map<string, string> HashRewrites; |
| 563 |
|
| 564 |
|
| 565 |
string rewriteHashes(string s, const HashRewrites & rewrites) |
| 566 |
{ |
| 567 |
foreach (HashRewrites::const_iterator, i, rewrites) { |
| 568 |
assert(i->first.size() == i->second.size()); |
| 569 |
size_t j = 0; |
| 570 |
while ((j = s.find(i->first, j)) != string::npos) { |
| 571 |
debug(format("rewriting @ %1%") % j); |
| 572 |
s.replace(j, i->second.size(), i->second); |
| 573 |
} |
| 574 |
} |
| 575 |
return s; |
| 576 |
} |
| 577 |
|
| 578 |
|
| 579 |
////////////////////////////////////////////////////////////////////// |
| 580 |
|
| 581 |
|
| 582 |
typedef enum {rpAccept, rpDecline, rpPostpone} HookReply; |
| 583 |
|
| 584 |
class SubstitutionGoal; |
| 585 |
|
| 586 |
class DerivationGoal : public Goal |
| 587 |
{ |
| 588 |
private: |
| 589 |
/* The path of the derivation. */ |
| 590 |
Path drvPath; |
| 591 |
|
| 592 |
/* The specific outputs that we need to build. Empty means all of |
| 593 |
them. */ |
| 594 |
StringSet wantedOutputs; |
| 595 |
|
| 596 |
/* Whether additional wanted outputs have been added. */ |
| 597 |
bool needRestart; |
| 598 |
|
| 599 |
/* Whether to retry substituting the outputs after building the |
| 600 |
inputs. */ |
| 601 |
bool retrySubstitution; |
| 602 |
|
| 603 |
/* The derivation stored at drvPath. */ |
| 604 |
Derivation drv; |
| 605 |
|
| 606 |
/* The remainder is state held during the build. */ |
| 607 |
|
| 608 |
/* Locks on the output paths. */ |
| 609 |
PathLocks outputLocks; |
| 610 |
|
| 611 |
/* All input paths (that is, the union of FS closures of the |
| 612 |
immediate input paths). */ |
| 613 |
PathSet inputPaths; |
| 614 |
|
| 615 |
/* Referenceable paths (i.e., input and output paths). */ |
| 616 |
PathSet allPaths; |
| 617 |
|
| 618 |
/* Outputs that are already valid. If we're repairing, these are |
| 619 |
the outputs that are valid *and* not corrupt. */ |
| 620 |
PathSet validPaths; |
| 621 |
|
| 622 |
/* Outputs that are corrupt or not valid. */ |
| 623 |
PathSet missingPaths; |
| 624 |
|
| 625 |
/* User selected for running the builder. */ |
| 626 |
UserLock buildUser; |
| 627 |
|
| 628 |
/* The process ID of the builder. */ |
| 629 |
Pid pid; |
| 630 |
|
| 631 |
/* The temporary directory. */ |
| 632 |
Path tmpDir; |
| 633 |
|
| 634 |
/* The path of the temporary directory in the sandbox. */ |
| 635 |
Path tmpDirInSandbox; |
| 636 |
|
| 637 |
/* File descriptor for the log file. */ |
| 638 |
FILE * fLogFile; |
| 639 |
gzFile gzLogFile; |
| 640 |
#if HAVE_BZLIB_H |
| 641 |
BZFILE * bzLogFile; |
| 642 |
#endif |
| 643 |
AutoCloseFD fdLogFile; |
| 644 |
|
| 645 |
/* Number of bytes received from the builder's stdout/stderr. */ |
| 646 |
unsigned long logSize; |
| 647 |
|
| 648 |
/* Pipe for the builder's standard output/error. */ |
| 649 |
Pipe builderOut; |
| 650 |
|
| 651 |
/* The build hook. */ |
| 652 |
std::shared_ptr<Agent> hook; |
| 653 |
|
| 654 |
/* Whether we're currently doing a chroot build. */ |
| 655 |
bool useChroot; |
| 656 |
|
| 657 |
Path chrootRootDir; |
| 658 |
|
| 659 |
/* RAII object to delete the chroot directory. */ |
| 660 |
std::shared_ptr<AutoDelete> autoDelChroot; |
| 661 |
|
| 662 |
/* All inputs that are regular files. */ |
| 663 |
PathSet regularInputPaths; |
| 664 |
|
| 665 |
/* Whether this is a fixed-output derivation. */ |
| 666 |
bool fixedOutput; |
| 667 |
|
| 668 |
typedef void (DerivationGoal::*GoalState)(); |
| 669 |
GoalState state; |
| 670 |
|
| 671 |
/* Stuff we need to pass to runChild(). */ |
| 672 |
typedef map<Path, Path> DirsInChroot; // maps target path to source path |
| 673 |
DirsInChroot dirsInChroot; |
| 674 |
typedef map<string, string> Environment; |
| 675 |
Environment env; |
| 676 |
|
| 677 |
/* Hash rewriting. */ |
| 678 |
HashRewrites rewritesToTmp, rewritesFromTmp; |
| 679 |
typedef map<Path, Path> RedirectedOutputs; |
| 680 |
RedirectedOutputs redirectedOutputs; |
| 681 |
|
| 682 |
BuildMode buildMode; |
| 683 |
|
| 684 |
/* If we're repairing without a chroot, there may be outputs that |
| 685 |
are valid but corrupt. So we redirect these outputs to |
| 686 |
temporary paths. */ |
| 687 |
PathSet redirectedBadOutputs; |
| 688 |
|
| 689 |
/* The current round, if we're building multiple times. */ |
| 690 |
unsigned int curRound = 1; |
| 691 |
|
| 692 |
unsigned int nrRounds; |
| 693 |
|
| 694 |
/* Path registration info from the previous round, if we're |
| 695 |
building multiple times. Since this contains the hash, it |
| 696 |
allows us to compare whether two rounds produced the same |
| 697 |
result. */ |
| 698 |
ValidPathInfos prevInfos; |
| 699 |
|
| 700 |
BuildResult result; |
| 701 |
|
| 702 |
public: |
| 703 |
DerivationGoal(const Path & drvPath, const StringSet & wantedOutputs, Worker & worker, BuildMode buildMode = bmNormal); |
| 704 |
~DerivationGoal(); |
| 705 |
|
| 706 |
void timedOut() override; |
| 707 |
|
| 708 |
string key() |
| 709 |
{ |
| 710 |
/* Ensure that derivations get built in order of their name, |
| 711 |
i.e. a derivation named "aardvark" always comes before |
| 712 |
"baboon". And substitution goals always happen before |
| 713 |
derivation goals (due to "b$"). */ |
| 714 |
return "b$" + storePathToName(drvPath) + "$" + drvPath; |
| 715 |
} |
| 716 |
|
| 717 |
void work(); |
| 718 |
|
| 719 |
Path getDrvPath() |
| 720 |
{ |
| 721 |
return drvPath; |
| 722 |
} |
| 723 |
|
| 724 |
/* Add wanted outputs to an already existing derivation goal. */ |
| 725 |
void addWantedOutputs(const StringSet & outputs); |
| 726 |
|
| 727 |
BuildResult getResult() { return result; } |
| 728 |
|
| 729 |
private: |
| 730 |
/* The states. */ |
| 731 |
void init(); |
| 732 |
void haveDerivation(); |
| 733 |
void outputsSubstituted(); |
| 734 |
void closureRepaired(); |
| 735 |
void inputsRealised(); |
| 736 |
void tryToBuild(); |
| 737 |
void buildDone(); |
| 738 |
|
| 739 |
/* Is the build hook willing to perform the build? */ |
| 740 |
HookReply tryBuildHook(); |
| 741 |
|
| 742 |
/* Start building a derivation. */ |
| 743 |
void startBuilder(); |
| 744 |
|
| 745 |
/* Run the builder's process. */ |
| 746 |
void runChild(); |
| 747 |
|
| 748 |
friend int childEntry(void *); |
| 749 |
|
| 750 |
/* Check that the derivation outputs all exist and register them |
| 751 |
as valid. */ |
| 752 |
void registerOutputs(); |
| 753 |
|
| 754 |
/* Open a log file and a pipe to it. */ |
| 755 |
Path openLogFile(); |
| 756 |
|
| 757 |
/* Close the log file. */ |
| 758 |
void closeLogFile(); |
| 759 |
|
| 760 |
/* Delete the temporary directory, if we have one. */ |
| 761 |
void deleteTmpDir(bool force); |
| 762 |
|
| 763 |
/* Callback used by the worker to write to the log. */ |
| 764 |
void handleChildOutput(int fd, const string & data); |
| 765 |
void handleEOF(int fd); |
| 766 |
|
| 767 |
/* Return the set of (in)valid paths. */ |
| 768 |
PathSet checkPathValidity(bool returnValid, bool checkHash); |
| 769 |
|
| 770 |
/* Abort the goal if `path' failed to build. */ |
| 771 |
bool pathFailed(const Path & path); |
| 772 |
|
| 773 |
/* Forcibly kill the child process, if any. */ |
| 774 |
void killChild(); |
| 775 |
|
| 776 |
Path addHashRewrite(const Path & path); |
| 777 |
|
| 778 |
void repairClosure(); |
| 779 |
|
| 780 |
void done(BuildResult::Status status, const string & msg = ""); |
| 781 |
}; |
| 782 |
|
| 783 |
|
| 784 |
DerivationGoal::DerivationGoal(const Path & drvPath, const StringSet & wantedOutputs, Worker & worker, BuildMode buildMode) |
| 785 |
: Goal(worker) |
| 786 |
, wantedOutputs(wantedOutputs) |
| 787 |
, needRestart(false) |
| 788 |
, retrySubstitution(false) |
| 789 |
, fLogFile(0) |
| 790 |
, gzLogFile(0) |
| 791 |
#if HAVE_BZLIB_H |
| 792 |
, bzLogFile(0) |
| 793 |
#endif |
| 794 |
, useChroot(false) |
| 795 |
, buildMode(buildMode) |
| 796 |
{ |
| 797 |
this->drvPath = drvPath; |
| 798 |
state = &DerivationGoal::init; |
| 799 |
name = (format("building of `%1%'") % drvPath).str(); |
| 800 |
trace("created"); |
| 801 |
|
| 802 |
/* Prevent the .chroot directory from being |
| 803 |
garbage-collected. (See isActiveTempFile() in gc.cc.) */ |
| 804 |
worker.store.addTempRoot(drvPath); |
| 805 |
} |
| 806 |
|
| 807 |
|
| 808 |
DerivationGoal::~DerivationGoal() |
| 809 |
{ |
| 810 |
/* Careful: we should never ever throw an exception from a |
| 811 |
destructor. */ |
| 812 |
try { killChild(); } catch (...) { ignoreException(); } |
| 813 |
try { deleteTmpDir(false); } catch (...) { ignoreException(); } |
| 814 |
try { closeLogFile(); } catch (...) { ignoreException(); } |
| 815 |
} |
| 816 |
|
| 817 |
|
| 818 |
void DerivationGoal::killChild() |
| 819 |
{ |
| 820 |
if (pid != -1) { |
| 821 |
worker.childTerminated(pid); |
| 822 |
|
| 823 |
if (buildUser.enabled()) { |
| 824 |
/* If we're using a build user, then there is a tricky |
| 825 |
race condition: if we kill the build user before the |
| 826 |
child has done its setuid() to the build user uid, then |
| 827 |
it won't be killed, and we'll potentially lock up in |
| 828 |
pid.wait(). So also send a conventional kill to the |
| 829 |
child. */ |
| 830 |
::kill(-pid, SIGKILL); /* ignore the result */ |
| 831 |
buildUser.kill(); |
| 832 |
pid.wait(true); |
| 833 |
} else |
| 834 |
pid.kill(); |
| 835 |
|
| 836 |
assert(pid == -1); |
| 837 |
} |
| 838 |
|
| 839 |
/* If there was a build hook involved, remove it from the worker's |
| 840 |
children. */ |
| 841 |
if (hook && hook->pid != -1) { |
| 842 |
worker.childTerminated(hook->pid); |
| 843 |
} |
| 844 |
hook.reset(); |
| 845 |
} |
| 846 |
|
| 847 |
|
| 848 |
void DerivationGoal::timedOut() |
| 849 |
{ |
| 850 |
if (settings.printBuildTrace) |
| 851 |
printMsg(lvlError, format("@ build-failed %1% - timeout") % drvPath); |
| 852 |
killChild(); |
| 853 |
done(BuildResult::TimedOut); |
| 854 |
} |
| 855 |
|
| 856 |
|
| 857 |
void DerivationGoal::work() |
| 858 |
{ |
| 859 |
(this->*state)(); |
| 860 |
} |
| 861 |
|
| 862 |
|
| 863 |
void DerivationGoal::addWantedOutputs(const StringSet & outputs) |
| 864 |
{ |
| 865 |
/* If we already want all outputs, there is nothing to do. */ |
| 866 |
if (wantedOutputs.empty()) return; |
| 867 |
|
| 868 |
if (outputs.empty()) { |
| 869 |
wantedOutputs.clear(); |
| 870 |
needRestart = true; |
| 871 |
} else |
| 872 |
foreach (StringSet::const_iterator, i, outputs) |
| 873 |
if (wantedOutputs.find(*i) == wantedOutputs.end()) { |
| 874 |
wantedOutputs.insert(*i); |
| 875 |
needRestart = true; |
| 876 |
} |
| 877 |
} |
| 878 |
|
| 879 |
|
| 880 |
void DerivationGoal::init() |
| 881 |
{ |
| 882 |
trace("init"); |
| 883 |
|
| 884 |
if (settings.readOnlyMode) |
| 885 |
throw Error(format("cannot build derivation `%1%' - no write access to the store") % drvPath); |
| 886 |
|
| 887 |
/* The first thing to do is to make sure that the derivation |
| 888 |
exists. If it doesn't, it may be created through a |
| 889 |
substitute. */ |
| 890 |
if (buildMode == bmNormal && worker.store.isValidPath(drvPath)) { |
| 891 |
haveDerivation(); |
| 892 |
return; |
| 893 |
} |
| 894 |
|
| 895 |
addWaitee(worker.makeSubstitutionGoal(drvPath)); |
| 896 |
|
| 897 |
state = &DerivationGoal::haveDerivation; |
| 898 |
} |
| 899 |
|
| 900 |
|
| 901 |
void DerivationGoal::haveDerivation() |
| 902 |
{ |
| 903 |
trace("loading derivation"); |
| 904 |
|
| 905 |
if (nrFailed != 0) { |
| 906 |
printMsg(lvlError, format("cannot build missing derivation ‘%1%’") % drvPath); |
| 907 |
done(BuildResult::MiscFailure); |
| 908 |
return; |
| 909 |
} |
| 910 |
|
| 911 |
/* `drvPath' should already be a root, but let's be on the safe |
| 912 |
side: if the user forgot to make it a root, we wouldn't want |
| 913 |
things being garbage collected while we're busy. */ |
| 914 |
worker.store.addTempRoot(drvPath); |
| 915 |
|
| 916 |
assert(worker.store.isValidPath(drvPath)); |
| 917 |
|
| 918 |
/* Get the derivation. */ |
| 919 |
drv = derivationFromPath(worker.store, drvPath); |
| 920 |
|
| 921 |
foreach (DerivationOutputs::iterator, i, drv.outputs) |
| 922 |
worker.store.addTempRoot(i->second.path); |
| 923 |
|
| 924 |
/* Check what outputs paths are not already valid. */ |
| 925 |
PathSet invalidOutputs = checkPathValidity(false, buildMode == bmRepair); |
| 926 |
|
| 927 |
/* If they are all valid, then we're done. */ |
| 928 |
if (invalidOutputs.size() == 0 && buildMode == bmNormal) { |
| 929 |
done(BuildResult::AlreadyValid); |
| 930 |
return; |
| 931 |
} |
| 932 |
|
| 933 |
/* Check whether any output previously failed to build. If so, |
| 934 |
don't bother. */ |
| 935 |
foreach (PathSet::iterator, i, invalidOutputs) |
| 936 |
if (pathFailed(*i)) return; |
| 937 |
|
| 938 |
/* We are first going to try to create the invalid output paths |
| 939 |
through substitutes. If that doesn't work, we'll build |
| 940 |
them. */ |
| 941 |
if (settings.useSubstitutes && substitutesAllowed(drv)) |
| 942 |
foreach (PathSet::iterator, i, invalidOutputs) |
| 943 |
addWaitee(worker.makeSubstitutionGoal(*i, buildMode == bmRepair)); |
| 944 |
|
| 945 |
if (waitees.empty()) /* to prevent hang (no wake-up event) */ |
| 946 |
outputsSubstituted(); |
| 947 |
else |
| 948 |
state = &DerivationGoal::outputsSubstituted; |
| 949 |
} |
| 950 |
|
| 951 |
|
| 952 |
void DerivationGoal::outputsSubstituted() |
| 953 |
{ |
| 954 |
trace("all outputs substituted (maybe)"); |
| 955 |
|
| 956 |
if (nrFailed > 0 && nrFailed > nrNoSubstituters + nrIncompleteClosure && !settings.tryFallback) |
| 957 |
throw Error(format("some substitutes for the outputs of derivation `%1%' failed (usually happens due to networking issues); try `--fallback' to build derivation from source ") % drvPath); |
| 958 |
|
| 959 |
/* If the substitutes form an incomplete closure, then we should |
| 960 |
build the dependencies of this derivation, but after that, we |
| 961 |
can still use the substitutes for this derivation itself. */ |
| 962 |
if (nrIncompleteClosure > 0 && !retrySubstitution) retrySubstitution = true; |
| 963 |
|
| 964 |
nrFailed = nrNoSubstituters = nrIncompleteClosure = 0; |
| 965 |
|
| 966 |
if (needRestart) { |
| 967 |
needRestart = false; |
| 968 |
haveDerivation(); |
| 969 |
return; |
| 970 |
} |
| 971 |
|
| 972 |
unsigned int nrInvalid = checkPathValidity(false, buildMode == bmRepair).size(); |
| 973 |
if (buildMode == bmNormal && nrInvalid == 0) { |
| 974 |
done(BuildResult::Substituted); |
| 975 |
return; |
| 976 |
} |
| 977 |
if (buildMode == bmRepair && nrInvalid == 0) { |
| 978 |
repairClosure(); |
| 979 |
return; |
| 980 |
} |
| 981 |
if (buildMode == bmCheck && nrInvalid > 0) |
| 982 |
throw Error(format("`%1%' is missing outputs; build it normally before using `--check'") % drvPath); |
| 983 |
|
| 984 |
/* Otherwise, at least one of the output paths could not be |
| 985 |
produced using a substitute. So we have to build instead. */ |
| 986 |
|
| 987 |
/* Make sure checkPathValidity() from now on checks all |
| 988 |
outputs. */ |
| 989 |
wantedOutputs = PathSet(); |
| 990 |
|
| 991 |
/* The inputs must be built before we can build this goal. */ |
| 992 |
foreach (DerivationInputs::iterator, i, drv.inputDrvs) |
| 993 |
addWaitee(worker.makeDerivationGoal(i->first, i->second, buildMode == bmRepair ? bmRepair : bmNormal)); |
| 994 |
|
| 995 |
foreach (PathSet::iterator, i, drv.inputSrcs) |
| 996 |
addWaitee(worker.makeSubstitutionGoal(*i)); |
| 997 |
|
| 998 |
if (waitees.empty()) /* to prevent hang (no wake-up event) */ |
| 999 |
inputsRealised(); |
| 1000 |
else |
| 1001 |
state = &DerivationGoal::inputsRealised; |
| 1002 |
} |
| 1003 |
|
| 1004 |
|
| 1005 |
void DerivationGoal::repairClosure() |
| 1006 |
{ |
| 1007 |
/* If we're repairing, we now know that our own outputs are valid. |
| 1008 |
Now check whether the other paths in the outputs closure are |
| 1009 |
good. If not, then start derivation goals for the derivations |
| 1010 |
that produced those outputs. */ |
| 1011 |
|
| 1012 |
/* Get the output closure. */ |
| 1013 |
PathSet outputClosure; |
| 1014 |
foreach (DerivationOutputs::iterator, i, drv.outputs) { |
| 1015 |
if (!wantOutput(i->first, wantedOutputs)) continue; |
| 1016 |
computeFSClosure(worker.store, i->second.path, outputClosure); |
| 1017 |
} |
| 1018 |
|
| 1019 |
/* Filter out our own outputs (which we have already checked). */ |
| 1020 |
foreach (DerivationOutputs::iterator, i, drv.outputs) |
| 1021 |
outputClosure.erase(i->second.path); |
| 1022 |
|
| 1023 |
/* Get all dependencies of this derivation so that we know which |
| 1024 |
derivation is responsible for which path in the output |
| 1025 |
closure. */ |
| 1026 |
PathSet inputClosure; |
| 1027 |
computeFSClosure(worker.store, drvPath, inputClosure); |
| 1028 |
std::map<Path, Path> outputsToDrv; |
| 1029 |
foreach (PathSet::iterator, i, inputClosure) |
| 1030 |
if (isDerivation(*i)) { |
| 1031 |
Derivation drv = derivationFromPath(worker.store, *i); |
| 1032 |
foreach (DerivationOutputs::iterator, j, drv.outputs) |
| 1033 |
outputsToDrv[j->second.path] = *i; |
| 1034 |
} |
| 1035 |
|
| 1036 |
/* Check each path (slow!). */ |
| 1037 |
PathSet broken; |
| 1038 |
foreach (PathSet::iterator, i, outputClosure) { |
| 1039 |
if (worker.store.pathContentsGood(*i)) continue; |
| 1040 |
printMsg(lvlError, format("found corrupted or missing path `%1%' in the output closure of `%2%'") % *i % drvPath); |
| 1041 |
Path drvPath2 = outputsToDrv[*i]; |
| 1042 |
if (drvPath2 == "") |
| 1043 |
addWaitee(worker.makeSubstitutionGoal(*i, true)); |
| 1044 |
else |
| 1045 |
addWaitee(worker.makeDerivationGoal(drvPath2, PathSet(), bmRepair)); |
| 1046 |
} |
| 1047 |
|
| 1048 |
if (waitees.empty()) { |
| 1049 |
done(BuildResult::AlreadyValid); |
| 1050 |
return; |
| 1051 |
} |
| 1052 |
|
| 1053 |
state = &DerivationGoal::closureRepaired; |
| 1054 |
} |
| 1055 |
|
| 1056 |
|
| 1057 |
void DerivationGoal::closureRepaired() |
| 1058 |
{ |
| 1059 |
trace("closure repaired"); |
| 1060 |
if (nrFailed > 0) |
| 1061 |
throw Error(format("some paths in the output closure of derivation ‘%1%’ could not be repaired") % drvPath); |
| 1062 |
done(BuildResult::AlreadyValid); |
| 1063 |
} |
| 1064 |
|
| 1065 |
|
| 1066 |
void DerivationGoal::inputsRealised() |
| 1067 |
{ |
| 1068 |
trace("all inputs realised"); |
| 1069 |
|
| 1070 |
if (nrFailed != 0) { |
| 1071 |
printMsg(lvlError, |
| 1072 |
format("cannot build derivation `%1%': %2% dependencies couldn't be built") |
| 1073 |
% drvPath % nrFailed); |
| 1074 |
done(BuildResult::DependencyFailed); |
| 1075 |
return; |
| 1076 |
} |
| 1077 |
|
| 1078 |
if (retrySubstitution) { |
| 1079 |
haveDerivation(); |
| 1080 |
return; |
| 1081 |
} |
| 1082 |
|
| 1083 |
/* Gather information necessary for computing the closure and/or |
| 1084 |
running the build hook. */ |
| 1085 |
|
| 1086 |
/* The outputs are referenceable paths. */ |
| 1087 |
foreach (DerivationOutputs::iterator, i, drv.outputs) { |
| 1088 |
debug(format("building path `%1%'") % i->second.path); |
| 1089 |
allPaths.insert(i->second.path); |
| 1090 |
} |
| 1091 |
|
| 1092 |
/* Determine the full set of input paths. */ |
| 1093 |
|
| 1094 |
/* First, the input derivations. */ |
| 1095 |
foreach (DerivationInputs::iterator, i, drv.inputDrvs) { |
| 1096 |
/* Add the relevant output closures of the input derivation |
| 1097 |
`*i' as input paths. Only add the closures of output paths |
| 1098 |
that are specified as inputs. */ |
| 1099 |
assert(worker.store.isValidPath(i->first)); |
| 1100 |
Derivation inDrv = derivationFromPath(worker.store, i->first); |
| 1101 |
foreach (StringSet::iterator, j, i->second) |
| 1102 |
if (inDrv.outputs.find(*j) != inDrv.outputs.end()) |
| 1103 |
computeFSClosure(worker.store, inDrv.outputs[*j].path, inputPaths); |
| 1104 |
else |
| 1105 |
throw Error( |
| 1106 |
format("derivation `%1%' requires non-existent output `%2%' from input derivation `%3%'") |
| 1107 |
% drvPath % *j % i->first); |
| 1108 |
} |
| 1109 |
|
| 1110 |
/* Second, the input sources. */ |
| 1111 |
foreach (PathSet::iterator, i, drv.inputSrcs) |
| 1112 |
computeFSClosure(worker.store, *i, inputPaths); |
| 1113 |
|
| 1114 |
debug(format("added input paths %1%") % showPaths(inputPaths)); |
| 1115 |
|
| 1116 |
allPaths.insert(inputPaths.begin(), inputPaths.end()); |
| 1117 |
|
| 1118 |
/* Is this a fixed-output derivation? */ |
| 1119 |
fixedOutput = true; |
| 1120 |
for (auto & i : drv.outputs) |
| 1121 |
if (i.second.hash == "") fixedOutput = false; |
| 1122 |
|
| 1123 |
/* Don't repeat fixed-output derivations since they're already |
| 1124 |
verified by their output hash.*/ |
| 1125 |
nrRounds = fixedOutput ? 1 : settings.get("build-repeat", 0) + 1; |
| 1126 |
|
| 1127 |
/* Okay, try to build. Note that here we don't wait for a build |
| 1128 |
slot to become available, since we don't need one if there is a |
| 1129 |
build hook. */ |
| 1130 |
state = &DerivationGoal::tryToBuild; |
| 1131 |
worker.wakeUp(shared_from_this()); |
| 1132 |
} |
| 1133 |
|
| 1134 |
|
| 1135 |
static bool canBuildLocally(const string & platform) |
| 1136 |
{ |
| 1137 |
return platform == settings.thisSystem |
| 1138 |
#if __linux__ |
| 1139 |
|| (platform == "i686-linux" && settings.thisSystem == "x86_64-linux") |
| 1140 |
|| (platform == "armhf-linux" && settings.thisSystem == "aarch64-linux") |
| 1141 |
#endif |
| 1142 |
; |
| 1143 |
} |
| 1144 |
|
| 1145 |
|
| 1146 |
static string get(const StringPairs & map, const string & key, const string & def = "") |
| 1147 |
{ |
| 1148 |
StringPairs::const_iterator i = map.find(key); |
| 1149 |
return i == map.end() ? def : i->second; |
| 1150 |
} |
| 1151 |
|
| 1152 |
|
| 1153 |
bool willBuildLocally(const Derivation & drv) |
| 1154 |
{ |
| 1155 |
return get(drv.env, "preferLocalBuild") == "1" && canBuildLocally(drv.platform); |
| 1156 |
} |
| 1157 |
|
| 1158 |
|
| 1159 |
bool substitutesAllowed(const Derivation & drv) |
| 1160 |
{ |
| 1161 |
return get(drv.env, "allowSubstitutes", "1") == "1"; |
| 1162 |
} |
| 1163 |
|
| 1164 |
|
| 1165 |
void DerivationGoal::tryToBuild() |
| 1166 |
{ |
| 1167 |
trace("trying to build"); |
| 1168 |
|
| 1169 |
/* Check for the possibility that some other goal in this process |
| 1170 |
has locked the output since we checked in haveDerivation(). |
| 1171 |
(It can't happen between here and the lockPaths() call below |
| 1172 |
because we're not allowing multi-threading.) If so, put this |
| 1173 |
goal to sleep until another goal finishes, then try again. */ |
| 1174 |
foreach (DerivationOutputs::iterator, i, drv.outputs) |
| 1175 |
if (pathIsLockedByMe(i->second.path)) { |
| 1176 |
debug(format("putting derivation `%1%' to sleep because `%2%' is locked by another goal") |
| 1177 |
% drvPath % i->second.path); |
| 1178 |
worker.waitForAnyGoal(shared_from_this()); |
| 1179 |
return; |
| 1180 |
} |
| 1181 |
|
| 1182 |
/* Obtain locks on all output paths. The locks are automatically |
| 1183 |
released when we exit this function or the client crashes. If we |
| 1184 |
can't acquire the lock, then continue; hopefully some other |
| 1185 |
goal can start a build, and if not, the main loop will sleep a |
| 1186 |
few seconds and then retry this goal. */ |
| 1187 |
if (!outputLocks.lockPaths(outputPaths(drv), "", false)) { |
| 1188 |
worker.waitForAWhile(shared_from_this()); |
| 1189 |
return; |
| 1190 |
} |
| 1191 |
|
| 1192 |
/* Now check again whether the outputs are valid. This is because |
| 1193 |
another process may have started building in parallel. After |
| 1194 |
it has finished and released the locks, we can (and should) |
| 1195 |
reuse its results. (Strictly speaking the first check can be |
| 1196 |
omitted, but that would be less efficient.) Note that since we |
| 1197 |
now hold the locks on the output paths, no other process can |
| 1198 |
build this derivation, so no further checks are necessary. */ |
| 1199 |
validPaths = checkPathValidity(true, buildMode == bmRepair); |
| 1200 |
if (buildMode != bmCheck && validPaths.size() == drv.outputs.size()) { |
| 1201 |
debug(format("skipping build of derivation `%1%', someone beat us to it") % drvPath); |
| 1202 |
outputLocks.setDeletion(true); |
| 1203 |
done(BuildResult::AlreadyValid); |
| 1204 |
return; |
| 1205 |
} |
| 1206 |
|
| 1207 |
missingPaths = outputPaths(drv); |
| 1208 |
if (buildMode != bmCheck) |
| 1209 |
foreach (PathSet::iterator, i, validPaths) missingPaths.erase(*i); |
| 1210 |
|
| 1211 |
/* If any of the outputs already exist but are not valid, delete |
| 1212 |
them. */ |
| 1213 |
foreach (DerivationOutputs::iterator, i, drv.outputs) { |
| 1214 |
Path path = i->second.path; |
| 1215 |
if (worker.store.isValidPath(path)) continue; |
| 1216 |
if (!pathExists(path)) continue; |
| 1217 |
debug(format("removing invalid path `%1%'") % path); |
| 1218 |
deletePath(path); |
| 1219 |
} |
| 1220 |
|
| 1221 |
/* Check again whether any output previously failed to build, |
| 1222 |
because some other process may have tried and failed before we |
| 1223 |
acquired the lock. */ |
| 1224 |
foreach (DerivationOutputs::iterator, i, drv.outputs) |
| 1225 |
if (pathFailed(i->second.path)) return; |
| 1226 |
|
| 1227 |
/* Don't do a remote build if the derivation has the attribute |
| 1228 |
`preferLocalBuild' set. Also, check and repair modes are only |
| 1229 |
supported for local builds. */ |
| 1230 |
bool buildLocally = buildMode != bmNormal || willBuildLocally(drv); |
| 1231 |
|
| 1232 |
/* Is the build hook willing to accept this job? */ |
| 1233 |
if (!buildLocally) { |
| 1234 |
switch (tryBuildHook()) { |
| 1235 |
case rpAccept: |
| 1236 |
/* Yes, it has started doing so. Wait until we get |
| 1237 |
EOF from the hook. */ |
| 1238 |
state = &DerivationGoal::buildDone; |
| 1239 |
return; |
| 1240 |
case rpPostpone: |
| 1241 |
/* Not now; wait until at least one child finishes or |
| 1242 |
the wake-up timeout expires. */ |
| 1243 |
worker.waitForAWhile(shared_from_this()); |
| 1244 |
outputLocks.unlock(); |
| 1245 |
return; |
| 1246 |
case rpDecline: |
| 1247 |
/* We should do it ourselves. */ |
| 1248 |
break; |
| 1249 |
} |
| 1250 |
} |
| 1251 |
|
| 1252 |
/* Make sure that we are allowed to start a build. If this |
| 1253 |
derivation prefers to be done locally, do it even if |
| 1254 |
maxBuildJobs is 0. */ |
| 1255 |
unsigned int curBuilds = worker.getNrLocalBuilds(); |
| 1256 |
if (curBuilds >= settings.maxBuildJobs && !(buildLocally && curBuilds == 0)) { |
| 1257 |
worker.waitForBuildSlot(shared_from_this()); |
| 1258 |
outputLocks.unlock(); |
| 1259 |
return; |
| 1260 |
} |
| 1261 |
|
| 1262 |
try { |
| 1263 |
|
| 1264 |
/* Okay, we have to build. */ |
| 1265 |
startBuilder(); |
| 1266 |
|
| 1267 |
} catch (BuildError & e) { |
| 1268 |
printMsg(lvlError, e.msg()); |
| 1269 |
outputLocks.unlock(); |
| 1270 |
buildUser.release(); |
| 1271 |
if (settings.printBuildTrace) |
| 1272 |
printMsg(lvlError, format("@ build-failed %1% - %2% %3%") |
| 1273 |
% drvPath % 0 % e.msg()); |
| 1274 |
worker.permanentFailure = true; |
| 1275 |
done(BuildResult::InputRejected, e.msg()); |
| 1276 |
return; |
| 1277 |
} |
| 1278 |
|
| 1279 |
/* This state will be reached when we get EOF on the child's |
| 1280 |
log pipe. */ |
| 1281 |
state = &DerivationGoal::buildDone; |
| 1282 |
} |
| 1283 |
|
| 1284 |
|
| 1285 |
void replaceValidPath(const Path & storePath, const Path tmpPath) |
| 1286 |
{ |
| 1287 |
/* We can't atomically replace storePath (the original) with |
| 1288 |
tmpPath (the replacement), so we have to move it out of the |
| 1289 |
way first. We'd better not be interrupted here, because if |
| 1290 |
we're repairing (say) Glibc, we end up with a broken system. */ |
| 1291 |
Path oldPath = (format("%1%.old-%2%-%3%") % storePath % getpid() % rand()).str(); |
| 1292 |
if (pathExists(storePath)) |
| 1293 |
rename(storePath.c_str(), oldPath.c_str()); |
| 1294 |
if (rename(tmpPath.c_str(), storePath.c_str()) == -1) |
| 1295 |
throw SysError(format("moving `%1%' to `%2%'") % tmpPath % storePath); |
| 1296 |
if (pathExists(oldPath)) |
| 1297 |
deletePath(oldPath); |
| 1298 |
} |
| 1299 |
|
| 1300 |
|
| 1301 |
MakeError(NotDeterministic, BuildError) |
| 1302 |
|
| 1303 |
|
| 1304 |
/* Recursively make the file permissions of a path safe for exposure to |
| 1305 |
arbitrary users, but without canonicalising its permissions, timestamp, and |
| 1306 |
user. Throw an exception if a file type that isn't explicitly known to be |
| 1307 |
safe is found. */ |
| 1308 |
static void secureFilePerms(Path path) |
| 1309 |
{ |
| 1310 |
struct stat st; |
| 1311 |
if (lstat(path.c_str(), &st)) return; |
| 1312 |
|
| 1313 |
switch(st.st_mode & S_IFMT) { |
| 1314 |
case S_IFLNK: |
| 1315 |
return; |
| 1316 |
|
| 1317 |
case S_IFDIR: |
| 1318 |
for (auto & i : readDirectory(path)) { |
| 1319 |
secureFilePerms(path + "/" + i.name); |
| 1320 |
} |
| 1321 |
/* FALLTHROUGH */ |
| 1322 |
|
| 1323 |
case S_IFREG: |
| 1324 |
chmod(path.c_str(), (st.st_mode & ~S_IFMT) & ~(S_ISUID | S_ISGID | S_IWOTH)); |
| 1325 |
break; |
| 1326 |
|
| 1327 |
default: |
| 1328 |
throw Error(format("file `%1%' has an unsupported type") % path); |
| 1329 |
} |
| 1330 |
} |
| 1331 |
|
| 1332 |
void DerivationGoal::buildDone() |
| 1333 |
{ |
| 1334 |
trace("build done"); |
| 1335 |
|
| 1336 |
/* Since we got an EOF on the logger pipe, the builder is presumed |
| 1337 |
to have terminated. In fact, the builder could also have |
| 1338 |
simply have closed its end of the pipe --- just don't do that |
| 1339 |
:-) */ |
| 1340 |
int status; |
| 1341 |
pid_t savedPid; |
| 1342 |
if (hook) { |
| 1343 |
savedPid = hook->pid; |
| 1344 |
status = hook->pid.wait(true); |
| 1345 |
} else { |
| 1346 |
/* !!! this could block! security problem! solution: kill the |
| 1347 |
child */ |
| 1348 |
savedPid = pid; |
| 1349 |
status = pid.wait(true); |
| 1350 |
} |
| 1351 |
|
| 1352 |
debug(format("builder process for `%1%' finished") % drvPath); |
| 1353 |
|
| 1354 |
/* So the child is gone now. */ |
| 1355 |
worker.childTerminated(savedPid); |
| 1356 |
|
| 1357 |
/* Close the read side of the logger pipe. */ |
| 1358 |
if (hook) { |
| 1359 |
hook->builderOut.readSide.close(); |
| 1360 |
hook->fromAgent.readSide.close(); |
| 1361 |
} |
| 1362 |
else builderOut.readSide.close(); |
| 1363 |
|
| 1364 |
/* Close the log file. */ |
| 1365 |
closeLogFile(); |
| 1366 |
|
| 1367 |
/* When running under a build user, make sure that all processes |
| 1368 |
running under that uid are gone. This is to prevent a |
| 1369 |
malicious user from leaving behind a process that keeps files |
| 1370 |
open and modifies them after they have been chown'ed to |
| 1371 |
root. */ |
| 1372 |
if (buildUser.enabled()) buildUser.kill(); |
| 1373 |
|
| 1374 |
bool diskFull = false; |
| 1375 |
|
| 1376 |
try { |
| 1377 |
|
| 1378 |
/* Check the exit status. */ |
| 1379 |
if (!statusOk(status)) { |
| 1380 |
|
| 1381 |
/* Heuristically check whether the build failure may have |
| 1382 |
been caused by a disk full condition. We have no way |
| 1383 |
of knowing whether the build actually got an ENOSPC. |
| 1384 |
So instead, check if the disk is (nearly) full now. If |
| 1385 |
so, we don't mark this build as a permanent failure. */ |
| 1386 |
#if HAVE_STATVFS |
| 1387 |
unsigned long long required = 8ULL * 1024 * 1024; // FIXME: make configurable |
| 1388 |
struct statvfs st; |
| 1389 |
if (statvfs(settings.nixStore.c_str(), &st) == 0 && |
| 1390 |
(unsigned long long) st.f_bavail * st.f_bsize < required) |
| 1391 |
diskFull = true; |
| 1392 |
if (statvfs(tmpDir.c_str(), &st) == 0 && |
| 1393 |
(unsigned long long) st.f_bavail * st.f_bsize < required) |
| 1394 |
diskFull = true; |
| 1395 |
#endif |
| 1396 |
|
| 1397 |
deleteTmpDir(false); |
| 1398 |
|
| 1399 |
/* Move paths out of the chroot for easier debugging of |
| 1400 |
build failures. */ |
| 1401 |
if (useChroot && buildMode == bmNormal) |
| 1402 |
foreach (PathSet::iterator, i, missingPaths) |
| 1403 |
if (pathExists(chrootRootDir + *i)) { |
| 1404 |
try { |
| 1405 |
secureFilePerms(chrootRootDir + *i); |
| 1406 |
rename((chrootRootDir + *i).c_str(), i->c_str()); |
| 1407 |
} catch(Error & e) { |
| 1408 |
printMsg(lvlError, e.msg()); |
| 1409 |
} |
| 1410 |
} |
| 1411 |
|
| 1412 |
if (diskFull) |
| 1413 |
printMsg(lvlError, "note: build failure may have been caused by lack of free disk space"); |
| 1414 |
|
| 1415 |
throw BuildError(format("builder for `%1%' %2%") |
| 1416 |
% drvPath % statusToString(status)); |
| 1417 |
} |
| 1418 |
|
| 1419 |
if (fixedOutput) { |
| 1420 |
/* Replace the output, if it exists, by a fresh copy of itself to |
| 1421 |
make sure that there's no stale file descriptor pointing to it |
| 1422 |
(CVE-2024-27297). */ |
| 1423 |
foreach (DerivationOutputs::iterator, i, drv.outputs) { |
| 1424 |
Path output = chrootRootDir + i->second.path; |
| 1425 |
if (pathExists(output)) { |
| 1426 |
Path pivot = output + ".tmp"; |
| 1427 |
copyFileRecursively(output, pivot, true); |
| 1428 |
int err = rename(pivot.c_str(), output.c_str()); |
| 1429 |
if (err != 0) |
| 1430 |
throw SysError(format("renaming `%1%' to `%2%'") |
| 1431 |
% pivot % output); |
| 1432 |
} |
| 1433 |
} |
| 1434 |
} |
| 1435 |
|
| 1436 |
/* Compute the FS closure of the outputs and register them as |
| 1437 |
being valid. */ |
| 1438 |
registerOutputs(); |
| 1439 |
|
| 1440 |
/* Delete unused redirected outputs (when doing hash rewriting). */ |
| 1441 |
foreach (RedirectedOutputs::iterator, i, redirectedOutputs) |
| 1442 |
if (pathExists(i->second)) deletePath(i->second); |
| 1443 |
|
| 1444 |
/* Delete the chroot (if we were using one). */ |
| 1445 |
autoDelChroot.reset(); /* this runs the destructor */ |
| 1446 |
|
| 1447 |
deleteTmpDir(true); |
| 1448 |
|
| 1449 |
/* Repeat the build if necessary. */ |
| 1450 |
if (curRound++ < nrRounds) { |
| 1451 |
outputLocks.unlock(); |
| 1452 |
buildUser.release(); |
| 1453 |
state = &DerivationGoal::tryToBuild; |
| 1454 |
worker.wakeUp(shared_from_this()); |
| 1455 |
return; |
| 1456 |
} |
| 1457 |
|
| 1458 |
/* It is now safe to delete the lock files, since all future |
| 1459 |
lockers will see that the output paths are valid; they will |
| 1460 |
not create new lock files with the same names as the old |
| 1461 |
(unlinked) lock files. */ |
| 1462 |
outputLocks.setDeletion(true); |
| 1463 |
outputLocks.unlock(); |
| 1464 |
|
| 1465 |
} catch (BuildError & e) { |
| 1466 |
if (!hook) |
| 1467 |
printMsg(lvlError, e.msg()); |
| 1468 |
outputLocks.unlock(); |
| 1469 |
buildUser.release(); |
| 1470 |
|
| 1471 |
BuildResult::Status st = BuildResult::MiscFailure; |
| 1472 |
|
| 1473 |
if (hook && WIFEXITED(status) && WEXITSTATUS(status) == 101) { |
| 1474 |
if (settings.printBuildTrace) |
| 1475 |
printMsg(lvlError, format("@ build-failed %1% - timeout") % drvPath); |
| 1476 |
st = BuildResult::TimedOut; |
| 1477 |
} |
| 1478 |
|
| 1479 |
else if (hook && (!WIFEXITED(status) || WEXITSTATUS(status) != 100)) { |
| 1480 |
if (settings.printBuildTrace) |
| 1481 |
printMsg(lvlError, format("@ hook-failed %1% - %2% %3%") |
| 1482 |
% drvPath % status % e.msg()); |
| 1483 |
} |
| 1484 |
|
| 1485 |
else { |
| 1486 |
if (settings.printBuildTrace) |
| 1487 |
printMsg(lvlError, format("@ build-failed %1% - %2% %3%") |
| 1488 |
% drvPath % 1 % e.msg()); |
| 1489 |
|
| 1490 |
st = |
| 1491 |
statusOk(status) ? BuildResult::OutputRejected : |
| 1492 |
fixedOutput || diskFull ? BuildResult::TransientFailure : |
| 1493 |
BuildResult::PermanentFailure; |
| 1494 |
|
| 1495 |
/* Register the outputs of this build as "failed" so we |
| 1496 |
won't try to build them again (negative caching). |
| 1497 |
However, don't do this for fixed-output derivations, |
| 1498 |
since they're likely to fail for transient reasons |
| 1499 |
(e.g., fetchurl not being able to access the network). |
| 1500 |
Hook errors (like communication problems with the |
| 1501 |
remote machine) shouldn't be cached either. */ |
| 1502 |
if (settings.cacheFailure && !fixedOutput && !diskFull) |
| 1503 |
foreach (DerivationOutputs::iterator, i, drv.outputs) |
| 1504 |
worker.store.registerFailedPath(i->second.path); |
| 1505 |
} |
| 1506 |
|
| 1507 |
done(st, e.msg()); |
| 1508 |
return; |
| 1509 |
} |
| 1510 |
|
| 1511 |
/* Release the build user, if applicable. */ |
| 1512 |
buildUser.release(); |
| 1513 |
|
| 1514 |
if (settings.printBuildTrace) |
| 1515 |
printMsg(lvlError, format("@ build-succeeded %1% -") % drvPath); |
| 1516 |
|
| 1517 |
done(BuildResult::Built); |
| 1518 |
} |
| 1519 |
|
| 1520 |
|
| 1521 |
HookReply DerivationGoal::tryBuildHook() |
| 1522 |
{ |
| 1523 |
if (!settings.useBuildHook) return rpDecline; |
| 1524 |
|
| 1525 |
if (!worker.hook) { |
| 1526 |
Strings args = { |
| 1527 |
"offload", |
| 1528 |
settings.thisSystem.c_str(), |
| 1529 |
(format("%1%") % settings.maxSilentTime).str().c_str(), |
| 1530 |
(format("%1%") % settings.printBuildTrace).str().c_str(), |
| 1531 |
(format("%1%") % settings.buildTimeout).str().c_str() |
| 1532 |
}; |
| 1533 |
|
| 1534 |
worker.hook = std::make_shared<Agent>(settings.guixProgram, args); |
| 1535 |
} |
| 1536 |
|
| 1537 |
/* Tell the hook about system features (beyond the system type) |
| 1538 |
required from the build machine. (The hook could parse the |
| 1539 |
drv file itself, but this is easier.) */ |
| 1540 |
Strings features = tokenizeString<Strings>(get(drv.env, "requiredSystemFeatures")); |
| 1541 |
foreach (Strings::iterator, i, features) checkStoreName(*i); /* !!! abuse */ |
| 1542 |
|
| 1543 |
/* Send the request to the hook. */ |
| 1544 |
writeLine(worker.hook->toAgent.writeSide, (format("%1% %2% %3% %4%") |
| 1545 |
% (worker.getNrLocalBuilds() < settings.maxBuildJobs ? "1" : "0") |
| 1546 |
% drv.platform % drvPath % concatStringsSep(",", features)).str()); |
| 1547 |
|
| 1548 |
/* Read the first line of input, which should be a word indicating |
| 1549 |
whether the hook wishes to perform the build. */ |
| 1550 |
string reply; |
| 1551 |
while (true) { |
| 1552 |
string s = readLine(worker.hook->fromAgent.readSide); |
| 1553 |
if (string(s, 0, 2) == "# ") { |
| 1554 |
reply = string(s, 2); |
| 1555 |
break; |
| 1556 |
} |
| 1557 |
s += "\n"; |
| 1558 |
writeToStderr(s); |
| 1559 |
} |
| 1560 |
|
| 1561 |
debug(format("hook reply is `%1%'") % reply); |
| 1562 |
|
| 1563 |
if (reply == "decline" || reply == "postpone") |
| 1564 |
return reply == "decline" ? rpDecline : rpPostpone; |
| 1565 |
else if (reply != "accept") |
| 1566 |
throw Error(format("bad hook reply `%1%'") % reply); |
| 1567 |
|
| 1568 |
printMsg(lvlTalkative, format("using hook to build path(s) %1%") % showPaths(missingPaths)); |
| 1569 |
|
| 1570 |
hook = worker.hook; |
| 1571 |
worker.hook.reset(); |
| 1572 |
|
| 1573 |
/* Tell the hook all the inputs that have to be copied to the |
| 1574 |
remote system. This unfortunately has to contain the entire |
| 1575 |
derivation closure to ensure that the validity invariant holds |
| 1576 |
on the remote system. (I.e., it's unfortunate that we have to |
| 1577 |
list it since the remote system *probably* already has it.) */ |
| 1578 |
PathSet allInputs; |
| 1579 |
allInputs.insert(inputPaths.begin(), inputPaths.end()); |
| 1580 |
computeFSClosure(worker.store, drvPath, allInputs); |
| 1581 |
|
| 1582 |
string s; |
| 1583 |
foreach (PathSet::iterator, i, allInputs) { s += *i; s += ' '; } |
| 1584 |
writeLine(hook->toAgent.writeSide, s); |
| 1585 |
|
| 1586 |
/* Tell the hooks the missing outputs that have to be copied back |
| 1587 |
from the remote system. */ |
| 1588 |
s = ""; |
| 1589 |
foreach (PathSet::iterator, i, missingPaths) { s += *i; s += ' '; } |
| 1590 |
writeLine(hook->toAgent.writeSide, s); |
| 1591 |
|
| 1592 |
hook->toAgent.writeSide.close(); |
| 1593 |
|
| 1594 |
/* Create the log file and pipe. */ |
| 1595 |
Path logFile = openLogFile(); |
| 1596 |
|
| 1597 |
set<int> fds; |
| 1598 |
fds.insert(hook->fromAgent.readSide); |
| 1599 |
fds.insert(hook->builderOut.readSide); |
| 1600 |
worker.childStarted(shared_from_this(), hook->pid, fds, false, true); |
| 1601 |
|
| 1602 |
if (settings.printBuildTrace) |
| 1603 |
printMsg(lvlError, format("@ build-started %1% - %2% %3% %4%") |
| 1604 |
% drvPath % drv.platform % logFile % hook->pid); |
| 1605 |
|
| 1606 |
return rpAccept; |
| 1607 |
} |
| 1608 |
|
| 1609 |
|
| 1610 |
void chmod_(const Path & path, mode_t mode) |
| 1611 |
{ |
| 1612 |
if (chmod(path.c_str(), mode) == -1) |
| 1613 |
throw SysError(format("setting permissions on `%1%'") % path); |
| 1614 |
} |
| 1615 |
|
| 1616 |
|
| 1617 |
int childEntry(void * arg) |
| 1618 |
{ |
| 1619 |
((DerivationGoal *) arg)->runChild(); |
| 1620 |
return 1; |
| 1621 |
} |
| 1622 |
|
| 1623 |
|
| 1624 |
void DerivationGoal::startBuilder() |
| 1625 |
{ |
| 1626 |
auto f = format( |
| 1627 |
buildMode == bmRepair ? "repairing path(s) %1%" : |
| 1628 |
buildMode == bmCheck ? "checking path(s) %1%" : |
| 1629 |
nrRounds > 1 ? "building path(s) %1% (round %2%/%3%)" : |
| 1630 |
"building path(s) %1%"); |
| 1631 |
f.exceptions(boost::io::all_error_bits ^ boost::io::too_many_args_bit); |
| 1632 |
startNest(nest, lvlInfo, f % showPaths(missingPaths) % curRound % nrRounds); |
| 1633 |
|
| 1634 |
/* Note: built-in builders are *not* running in a chroot environment so |
| 1635 |
that we can easily implement them in Guile without having it as a |
| 1636 |
derivation input (they are running under a separate build user, |
| 1637 |
though). */ |
| 1638 |
useChroot = settings.useChroot && !isBuiltin(drv); |
| 1639 |
|
| 1640 |
/* Construct the environment passed to the builder. */ |
| 1641 |
env.clear(); |
| 1642 |
|
| 1643 |
/* Most shells initialise PATH to some default (/bin:/usr/bin:...) when |
| 1644 |
PATH is not set. We don't want this, so we fill it in with some dummy |
| 1645 |
value. */ |
| 1646 |
env["PATH"] = "/path-not-set"; |
| 1647 |
|
| 1648 |
/* Set HOME to a non-existing path to prevent certain programs from using |
| 1649 |
/etc/passwd (or NIS, or whatever) to locate the home directory (for |
| 1650 |
example, wget looks for ~/.wgetrc). I.e., these tools use /etc/passwd |
| 1651 |
if HOME is not set, but they will just assume that the settings file |
| 1652 |
they are looking for does not exist if HOME is set but points to some |
| 1653 |
non-existing path. */ |
| 1654 |
Path homeDir = "/homeless-shelter"; |
| 1655 |
env["HOME"] = homeDir; |
| 1656 |
|
| 1657 |
/* Tell the builder where the store is. Usually they |
| 1658 |
shouldn't care, but this is useful for purity checking (e.g., |
| 1659 |
the compiler or linker might only want to accept paths to files |
| 1660 |
in the store or in the build directory). */ |
| 1661 |
env["NIX_STORE"] = settings.nixStore; |
| 1662 |
|
| 1663 |
/* The maximum number of cores to utilize for parallel building. */ |
| 1664 |
env["NIX_BUILD_CORES"] = (format("%d") % settings.buildCores).str(); |
| 1665 |
|
| 1666 |
/* Add all bindings specified in the derivation. */ |
| 1667 |
foreach (StringPairs::iterator, i, drv.env) |
| 1668 |
env[i->first] = i->second; |
| 1669 |
|
| 1670 |
/* Create a temporary directory where the build will take |
| 1671 |
place. */ |
| 1672 |
auto drvName = storePathToName(drvPath); |
| 1673 |
tmpDir = createTempDir("", "guix-build-" + drvName, false, false, 0700); |
| 1674 |
|
| 1675 |
if (useChroot) { |
| 1676 |
/* Make the build directory seen by the build process a sub-directory. |
| 1677 |
That way, "/tmp/guix-build-foo.drv-0" is root-owned, and thus its |
| 1678 |
permissions cannot be changed by the build process, while |
| 1679 |
"/tmp/guix-build-foo.drv-0/top" is owned by the build user. This |
| 1680 |
cannot be done when !useChroot because then $NIX_BUILD_TOP would |
| 1681 |
be inaccessible to the build user by its full file name. |
| 1682 |
|
| 1683 |
If the build user could make the build directory world-writable, |
| 1684 |
then an attacker could create in it a hardlink to a root-owned file |
| 1685 |
such as /etc/shadow. If 'keepFailed' is true, the daemon would |
| 1686 |
then chown that hardlink to the user, giving them write access to |
| 1687 |
that file. */ |
| 1688 |
tmpDir += "/top"; |
| 1689 |
if (mkdir(tmpDir.c_str(), 0700) == 1) |
| 1690 |
throw SysError("creating top-level build directory"); |
| 1691 |
} |
| 1692 |
|
| 1693 |
/* In a sandbox, for determinism, always use the same temporary |
| 1694 |
directory. */ |
| 1695 |
tmpDirInSandbox = useChroot ? canonPath("/tmp", true) + "/guix-build-" + drvName + "-0" : tmpDir; |
| 1696 |
|
| 1697 |
/* For convenience, set an environment pointing to the top build |
| 1698 |
directory. */ |
| 1699 |
env["NIX_BUILD_TOP"] = tmpDirInSandbox; |
| 1700 |
|
| 1701 |
/* Also set TMPDIR and variants to point to this directory. */ |
| 1702 |
env["TMPDIR"] = env["TEMPDIR"] = env["TMP"] = env["TEMP"] = tmpDirInSandbox; |
| 1703 |
|
| 1704 |
/* Explicitly set PWD to prevent problems with chroot builds. In |
| 1705 |
particular, dietlibc cannot figure out the cwd because the |
| 1706 |
inode of the current directory doesn't appear in .. (because |
| 1707 |
getdents returns the inode of the mount point). */ |
| 1708 |
env["PWD"] = tmpDirInSandbox; |
| 1709 |
|
| 1710 |
/* *Only* if this is a fixed-output derivation, propagate the |
| 1711 |
values of the environment variables specified in the |
| 1712 |
`impureEnvVars' attribute to the builder. This allows for |
| 1713 |
instance environment variables for proxy configuration such as |
| 1714 |
`http_proxy' to be easily passed to downloaders like |
| 1715 |
`fetchurl'. Passing such environment variables from the caller |
| 1716 |
to the builder is generally impure, but the output of |
| 1717 |
fixed-output derivations is by definition pure (since we |
| 1718 |
already know the cryptographic hash of the output). */ |
| 1719 |
if (fixedOutput) { |
| 1720 |
Strings varNames = tokenizeString<Strings>(get(drv.env, "impureEnvVars")); |
| 1721 |
foreach (Strings::iterator, i, varNames) env[*i] = getEnv(*i); |
| 1722 |
} |
| 1723 |
|
| 1724 |
/* The `exportReferencesGraph' feature allows the references graph |
| 1725 |
to be passed to a builder. This attribute should be a list of |
| 1726 |
pairs [name1 path1 name2 path2 ...]. The references graph of |
| 1727 |
each `pathN' will be stored in a text file `nameN' in the |
| 1728 |
temporary build directory. The text files have the format used |
| 1729 |
by `nix-store --register-validity'. However, the deriver |
| 1730 |
fields are left empty. */ |
| 1731 |
string s = get(drv.env, "exportReferencesGraph"); |
| 1732 |
Strings ss = tokenizeString<Strings>(s); |
| 1733 |
if (ss.size() % 2 != 0) |
| 1734 |
throw BuildError(format("odd number of tokens in `exportReferencesGraph': `%1%'") % s); |
| 1735 |
for (Strings::iterator i = ss.begin(); i != ss.end(); ) { |
| 1736 |
string fileName = *i++; |
| 1737 |
checkStoreName(fileName); /* !!! abuse of this function */ |
| 1738 |
|
| 1739 |
/* Check that the store path is valid. */ |
| 1740 |
Path storePath = *i++; |
| 1741 |
if (!isInStore(storePath)) |
| 1742 |
throw BuildError(format("`exportReferencesGraph' contains a non-store path `%1%'") |
| 1743 |
% storePath); |
| 1744 |
storePath = toStorePath(storePath); |
| 1745 |
if (!worker.store.isValidPath(storePath)) |
| 1746 |
throw BuildError(format("`exportReferencesGraph' contains an invalid path `%1%'") |
| 1747 |
% storePath); |
| 1748 |
|
| 1749 |
/* If there are derivations in the graph, then include their |
| 1750 |
outputs as well. This is useful if you want to do things |
| 1751 |
like passing all build-time dependencies of some path to a |
| 1752 |
derivation that builds a NixOS DVD image. */ |
| 1753 |
PathSet paths, paths2; |
| 1754 |
computeFSClosure(worker.store, storePath, paths); |
| 1755 |
paths2 = paths; |
| 1756 |
|
| 1757 |
foreach (PathSet::iterator, j, paths2) { |
| 1758 |
if (isDerivation(*j)) { |
| 1759 |
Derivation drv = derivationFromPath(worker.store, *j); |
| 1760 |
foreach (DerivationOutputs::iterator, k, drv.outputs) |
| 1761 |
computeFSClosure(worker.store, k->second.path, paths); |
| 1762 |
} |
| 1763 |
} |
| 1764 |
|
| 1765 |
/* Write closure info to `fileName'. */ |
| 1766 |
writeFile(tmpDir + "/" + fileName, |
| 1767 |
worker.store.makeValidityRegistration(paths, false, false)); |
| 1768 |
} |
| 1769 |
|
| 1770 |
|
| 1771 |
/* If `build-users-group' is not empty, then we have to build as |
| 1772 |
one of the members of that group. */ |
| 1773 |
if (settings.buildUsersGroup != "") { |
| 1774 |
buildUser.acquire(); |
| 1775 |
assert(buildUser.getUID() != 0); |
| 1776 |
assert(buildUser.getGID() != 0); |
| 1777 |
|
| 1778 |
/* Make sure that no other processes are executing under this |
| 1779 |
uid. */ |
| 1780 |
buildUser.kill(); |
| 1781 |
|
| 1782 |
/* Change ownership of the temporary build directory. */ |
| 1783 |
if (chown(tmpDir.c_str(), buildUser.getUID(), buildUser.getGID()) == -1) |
| 1784 |
throw SysError(format("cannot change ownership of '%1%'") % tmpDir); |
| 1785 |
} |
| 1786 |
|
| 1787 |
if (useChroot) { |
| 1788 |
#if CHROOT_ENABLED |
| 1789 |
/* Create a temporary directory in which we set up the chroot |
| 1790 |
environment using bind-mounts. We put it in the store |
| 1791 |
to ensure that we can create hard-links to non-directory |
| 1792 |
inputs in the fake store in the chroot (see below). */ |
| 1793 |
chrootRootDir = drvPath + ".chroot"; |
| 1794 |
if (pathExists(chrootRootDir)) deletePath(chrootRootDir); |
| 1795 |
|
| 1796 |
/* Clean up the chroot directory automatically. */ |
| 1797 |
autoDelChroot = std::shared_ptr<AutoDelete>(new AutoDelete(chrootRootDir)); |
| 1798 |
|
| 1799 |
printMsg(lvlChatty, format("setting up chroot environment in `%1%'") % chrootRootDir); |
| 1800 |
|
| 1801 |
if (mkdir(chrootRootDir.c_str(), 0750) == -1) |
| 1802 |
throw SysError(format("cannot create ‘%1%’") % chrootRootDir); |
| 1803 |
|
| 1804 |
if (chown(chrootRootDir.c_str(), 0, buildUser.getGID()) == -1) |
| 1805 |
throw SysError(format("cannot change ownership of ‘%1%’") % chrootRootDir); |
| 1806 |
|
| 1807 |
/* Create a writable /tmp in the chroot. Many builders need |
| 1808 |
this. (Of course they should really respect $TMPDIR |
| 1809 |
instead.) */ |
| 1810 |
Path chrootTmpDir = chrootRootDir + "/tmp"; |
| 1811 |
createDirs(chrootTmpDir); |
| 1812 |
chmod_(chrootTmpDir, 01777); |
| 1813 |
|
| 1814 |
/* Create a /etc/passwd with entries for the build user and the |
| 1815 |
nobody account. The latter is kind of a hack to support |
| 1816 |
Samba-in-QEMU. */ |
| 1817 |
createDirs(chrootRootDir + "/etc"); |
| 1818 |
|
| 1819 |
writeFile(chrootRootDir + "/etc/passwd", |
| 1820 |
(format( |
| 1821 |
"nixbld:x:%1%:%2%:Nix build user:/:/noshell\n" |
| 1822 |
"nobody:x:65534:65534:Nobody:/:/noshell\n") |
| 1823 |
% (buildUser.enabled() ? buildUser.getUID() : getuid()) |
| 1824 |
% (buildUser.enabled() ? buildUser.getGID() : getgid())).str()); |
| 1825 |
|
| 1826 |
/* Declare the build user's group so that programs get a consistent |
| 1827 |
view of the system (e.g., "id -gn"). */ |
| 1828 |
writeFile(chrootRootDir + "/etc/group", |
| 1829 |
(format("nixbld:!:%1%:\n") |
| 1830 |
% (buildUser.enabled() ? buildUser.getGID() : getgid())).str()); |
| 1831 |
|
| 1832 |
/* Create /etc/hosts with localhost entry. */ |
| 1833 |
if (!fixedOutput) |
| 1834 |
writeFile(chrootRootDir + "/etc/hosts", "127.0.0.1 localhost\n"); |
| 1835 |
|
| 1836 |
/* Bind-mount a user-configurable set of directories from the |
| 1837 |
host file system. */ |
| 1838 |
PathSet dirs = tokenizeString<StringSet>(settings.get("build-chroot-dirs", string(DEFAULT_CHROOT_DIRS))); |
| 1839 |
PathSet dirs2 = tokenizeString<StringSet>(settings.get("build-extra-chroot-dirs", string(""))); |
| 1840 |
dirs.insert(dirs2.begin(), dirs2.end()); |
| 1841 |
for (auto & i : dirs) { |
| 1842 |
size_t p = i.find('='); |
| 1843 |
if (p == string::npos) |
| 1844 |
dirsInChroot[i] = i; |
| 1845 |
else |
| 1846 |
dirsInChroot[string(i, 0, p)] = string(i, p + 1); |
| 1847 |
} |
| 1848 |
dirsInChroot[tmpDirInSandbox] = tmpDir; |
| 1849 |
|
| 1850 |
/* Make the closure of the inputs available in the chroot, |
| 1851 |
rather than the whole store. This prevents any access |
| 1852 |
to undeclared dependencies. Directories are bind-mounted, |
| 1853 |
while other inputs are hard-linked (since only directories |
| 1854 |
can be bind-mounted). !!! As an extra security |
| 1855 |
precaution, make the fake store only writable by the |
| 1856 |
build user. */ |
| 1857 |
Path chrootStoreDir = chrootRootDir + settings.nixStore; |
| 1858 |
createDirs(chrootStoreDir); |
| 1859 |
chmod_(chrootStoreDir, 01775); |
| 1860 |
|
| 1861 |
if (chown(chrootStoreDir.c_str(), 0, buildUser.getGID()) == -1) |
| 1862 |
throw SysError(format("cannot change ownership of ‘%1%’") % chrootStoreDir); |
| 1863 |
|
| 1864 |
foreach (PathSet::iterator, i, inputPaths) { |
| 1865 |
struct stat st; |
| 1866 |
if (lstat(i->c_str(), &st)) |
| 1867 |
throw SysError(format("getting attributes of path `%1%'") % *i); |
| 1868 |
if (S_ISDIR(st.st_mode)) |
| 1869 |
dirsInChroot[*i] = *i; |
| 1870 |
else { |
| 1871 |
Path p = chrootRootDir + *i; |
| 1872 |
if (link(i->c_str(), p.c_str()) == -1) { |
| 1873 |
/* Hard-linking fails if we exceed the maximum |
| 1874 |
link count on a file (e.g. 32000 of ext3), |
| 1875 |
which is quite possible after a `nix-store |
| 1876 |
--optimise'. */ |
| 1877 |
if (errno != EMLINK) |
| 1878 |
throw SysError(format("linking `%1%' to `%2%'") % p % *i); |
| 1879 |
StringSink sink; |
| 1880 |
dumpPath(*i, sink); |
| 1881 |
StringSource source(sink.s); |
| 1882 |
restorePath(p, source); |
| 1883 |
} |
| 1884 |
|
| 1885 |
regularInputPaths.insert(*i); |
| 1886 |
} |
| 1887 |
} |
| 1888 |
|
| 1889 |
/* If we're repairing, checking or rebuilding part of a |
| 1890 |
multiple-outputs derivation, it's possible that we're |
| 1891 |
rebuilding a path that is in settings.dirsInChroot |
| 1892 |
(typically the dependencies of /bin/sh). Throw them |
| 1893 |
out. */ |
| 1894 |
for (auto & i : drv.outputs) |
| 1895 |
dirsInChroot.erase(i.second.path); |
| 1896 |
|
| 1897 |
#else |
| 1898 |
throw Error("chroot builds are not supported on this platform"); |
| 1899 |
#endif |
| 1900 |
} |
| 1901 |
|
| 1902 |
else { |
| 1903 |
|
| 1904 |
if (pathExists(homeDir)) |
| 1905 |
throw Error(format("directory `%1%' exists; please remove it") % homeDir); |
| 1906 |
|
| 1907 |
/* We're not doing a chroot build, but we have some valid |
| 1908 |
output paths. Since we can't just overwrite or delete |
| 1909 |
them, we have to do hash rewriting: i.e. in the |
| 1910 |
environment/arguments passed to the build, we replace the |
| 1911 |
hashes of the valid outputs with unique dummy strings; |
| 1912 |
after the build, we discard the redirected outputs |
| 1913 |
corresponding to the valid outputs, and rewrite the |
| 1914 |
contents of the new outputs to replace the dummy strings |
| 1915 |
with the actual hashes. */ |
| 1916 |
if (validPaths.size() > 0) |
| 1917 |
foreach (PathSet::iterator, i, validPaths) |
| 1918 |
addHashRewrite(*i); |
| 1919 |
|
| 1920 |
/* If we're repairing, then we don't want to delete the |
| 1921 |
corrupt outputs in advance. So rewrite them as well. */ |
| 1922 |
if (buildMode == bmRepair) |
| 1923 |
foreach (PathSet::iterator, i, missingPaths) |
| 1924 |
if (worker.store.isValidPath(*i) && pathExists(*i)) { |
| 1925 |
addHashRewrite(*i); |
| 1926 |
redirectedBadOutputs.insert(*i); |
| 1927 |
} |
| 1928 |
} |
| 1929 |
|
| 1930 |
|
| 1931 |
/* Run the builder. */ |
| 1932 |
printMsg(lvlChatty, format("executing builder `%1%'") % drv.builder); |
| 1933 |
|
| 1934 |
/* Create the log file. */ |
| 1935 |
Path logFile = openLogFile(); |
| 1936 |
|
| 1937 |
/* Create a pipe to get the output of the builder. */ |
| 1938 |
builderOut.create(); |
| 1939 |
|
| 1940 |
/* Fork a child to build the package. Note that while we |
| 1941 |
currently use forks to run and wait for the children, it |
| 1942 |
shouldn't be hard to use threads for this on systems where |
| 1943 |
fork() is unavailable or inefficient. |
| 1944 |
|
| 1945 |
If we're building in a chroot, then also set up private |
| 1946 |
namespaces for the build: |
| 1947 |
|
| 1948 |
- The PID namespace causes the build to start as PID 1. |
| 1949 |
Processes outside of the chroot are not visible to those on |
| 1950 |
the inside, but processes inside the chroot are visible from |
| 1951 |
the outside (though with different PIDs). |
| 1952 |
|
| 1953 |
- The private mount namespace ensures that all the bind mounts |
| 1954 |
we do will only show up in this process and its children, and |
| 1955 |
will disappear automatically when we're done. |
| 1956 |
|
| 1957 |
- The private network namespace ensures that the builder cannot |
| 1958 |
talk to the outside world (or vice versa). It only has a |
| 1959 |
private loopback interface. |
| 1960 |
|
| 1961 |
- The IPC namespace prevents the builder from communicating |
| 1962 |
with outside processes using SysV IPC mechanisms (shared |
| 1963 |
memory, message queues, semaphores). It also ensures that |
| 1964 |
all IPC objects are destroyed when the builder exits. |
| 1965 |
|
| 1966 |
- The UTS namespace ensures that builders see a hostname of |
| 1967 |
localhost rather than the actual hostname. |
| 1968 |
*/ |
| 1969 |
#if __linux__ |
| 1970 |
if (useChroot) { |
| 1971 |
char stack[32 * 1024]; |
| 1972 |
int flags = CLONE_NEWPID | CLONE_NEWNS | CLONE_NEWIPC | CLONE_NEWUTS | SIGCHLD; |
| 1973 |
if (!fixedOutput) flags |= CLONE_NEWNET; |
| 1974 |
/* Ensure proper alignment on the stack. On aarch64, it has to be 16 |
| 1975 |
bytes. */ |
| 1976 |
pid = clone(childEntry, |
| 1977 |
(char *)(((uintptr_t)stack + sizeof(stack) - 8) & ~(uintptr_t)0xf), |
| 1978 |
flags, this); |
| 1979 |
if (pid == -1) |
| 1980 |
throw SysError("cloning builder process"); |
| 1981 |
} else |
| 1982 |
#endif |
| 1983 |
{ |
| 1984 |
pid = fork(); |
| 1985 |
if (pid == 0) runChild(); |
| 1986 |
} |
| 1987 |
|
| 1988 |
if (pid == -1) throw SysError("unable to fork"); |
| 1989 |
|
| 1990 |
/* parent */ |
| 1991 |
pid.setSeparatePG(true); |
| 1992 |
builderOut.writeSide.close(); |
| 1993 |
worker.childStarted(shared_from_this(), pid, |
| 1994 |
singleton<set<int> >(builderOut.readSide), true, true); |
| 1995 |
|
| 1996 |
/* Check if setting up the build environment failed. */ |
| 1997 |
string msg = readLine(builderOut.readSide); |
| 1998 |
if (!msg.empty()) throw Error(msg); |
| 1999 |
|
| 2000 |
if (settings.printBuildTrace) { |
| 2001 |
printMsg(lvlError, format("@ build-started %1% - %2% %3% %4%") |
| 2002 |
% drvPath % drv.platform % logFile % pid); |
| 2003 |
} |
| 2004 |
|
| 2005 |
} |
| 2006 |
|
| 2007 |
/* Return true if the operating system kernel part of SYSTEM1 and SYSTEM2 (the |
| 2008 |
bit that comes after the hyphen in system types such as "i686-linux") is |
| 2009 |
the same. */ |
| 2010 |
static bool sameOperatingSystemKernel(const std::string& system1, const std::string& system2) |
| 2011 |
{ |
| 2012 |
auto os1 = system1.substr(system1.find("-")); |
| 2013 |
auto os2 = system2.substr(system2.find("-")); |
| 2014 |
return os1 == os2; |
| 2015 |
} |
| 2016 |
|
| 2017 |
void DerivationGoal::runChild() |
| 2018 |
{ |
| 2019 |
/* Warning: in the child we should absolutely not make any SQLite |
| 2020 |
calls! */ |
| 2021 |
|
| 2022 |
try { /* child */ |
| 2023 |
|
| 2024 |
_writeToStderr = 0; |
| 2025 |
|
| 2026 |
restoreAffinity(); |
| 2027 |
|
| 2028 |
commonChildInit(builderOut); |
| 2029 |
|
| 2030 |
#if CHROOT_ENABLED |
| 2031 |
if (useChroot) { |
| 2032 |
/* Initialise the loopback interface. */ |
| 2033 |
AutoCloseFD fd(socket(PF_INET, SOCK_DGRAM, IPPROTO_IP)); |
| 2034 |
if (fd == -1) throw SysError("cannot open IP socket"); |
| 2035 |
|
| 2036 |
struct ifreq ifr; |
| 2037 |
strcpy(ifr.ifr_name, "lo"); |
| 2038 |
ifr.ifr_flags = IFF_UP | IFF_LOOPBACK | IFF_RUNNING; |
| 2039 |
if (ioctl(fd, SIOCSIFFLAGS, &ifr) == -1) |
| 2040 |
throw SysError("cannot set loopback interface flags"); |
| 2041 |
|
| 2042 |
fd.close(); |
| 2043 |
|
| 2044 |
/* Set the hostname etc. to fixed values. */ |
| 2045 |
char hostname[] = "localhost"; |
| 2046 |
if (sethostname(hostname, sizeof(hostname)) == -1) |
| 2047 |
throw SysError("cannot set host name"); |
| 2048 |
char domainname[] = "(none)"; // kernel default |
| 2049 |
if (setdomainname(domainname, sizeof(domainname)) == -1) |
| 2050 |
throw SysError("cannot set domain name"); |
| 2051 |
|
| 2052 |
/* Make all filesystems private. This is necessary |
| 2053 |
because subtrees may have been mounted as "shared" |
| 2054 |
(MS_SHARED). (Systemd does this, for instance.) Even |
| 2055 |
though we have a private mount namespace, mounting |
| 2056 |
filesystems on top of a shared subtree still propagates |
| 2057 |
outside of the namespace. Making a subtree private is |
| 2058 |
local to the namespace, though, so setting MS_PRIVATE |
| 2059 |
does not affect the outside world. */ |
| 2060 |
if (mount(0, "/", 0, MS_REC|MS_PRIVATE, 0) == -1) { |
| 2061 |
throw SysError("unable to make ‘/’ private mount"); |
| 2062 |
} |
| 2063 |
|
| 2064 |
/* Bind-mount chroot directory to itself, to treat it as a |
| 2065 |
different filesystem from /, as needed for pivot_root. */ |
| 2066 |
if (mount(chrootRootDir.c_str(), chrootRootDir.c_str(), 0, MS_BIND, 0) == -1) |
| 2067 |
throw SysError(format("unable to bind mount ‘%1%’") % chrootRootDir); |
| 2068 |
|
| 2069 |
/* Set up a nearly empty /dev, unless the user asked to |
| 2070 |
bind-mount the host /dev. */ |
| 2071 |
Strings ss; |
| 2072 |
if (dirsInChroot.find("/dev") == dirsInChroot.end()) { |
| 2073 |
createDirs(chrootRootDir + "/dev/shm"); |
| 2074 |
createDirs(chrootRootDir + "/dev/pts"); |
| 2075 |
ss.push_back("/dev/full"); |
| 2076 |
#ifdef __linux__ |
| 2077 |
if (pathExists("/dev/kvm")) |
| 2078 |
ss.push_back("/dev/kvm"); |
| 2079 |
#endif |
| 2080 |
ss.push_back("/dev/null"); |
| 2081 |
ss.push_back("/dev/random"); |
| 2082 |
ss.push_back("/dev/tty"); |
| 2083 |
ss.push_back("/dev/urandom"); |
| 2084 |
ss.push_back("/dev/zero"); |
| 2085 |
createSymlink("/proc/self/fd", chrootRootDir + "/dev/fd"); |
| 2086 |
createSymlink("/proc/self/fd/0", chrootRootDir + "/dev/stdin"); |
| 2087 |
createSymlink("/proc/self/fd/1", chrootRootDir + "/dev/stdout"); |
| 2088 |
createSymlink("/proc/self/fd/2", chrootRootDir + "/dev/stderr"); |
| 2089 |
} |
| 2090 |
|
| 2091 |
/* Fixed-output derivations typically need to access the |
| 2092 |
network, so give them access to /etc/resolv.conf and so |
| 2093 |
on. */ |
| 2094 |
if (fixedOutput) { |
| 2095 |
ss.push_back("/etc/resolv.conf"); |
| 2096 |
ss.push_back("/etc/nsswitch.conf"); |
| 2097 |
ss.push_back("/etc/services"); |
| 2098 |
ss.push_back("/etc/hosts"); |
| 2099 |
} |
| 2100 |
|
| 2101 |
for (auto & i : ss) dirsInChroot[i] = i; |
| 2102 |
|
| 2103 |
/* Bind-mount all the directories from the "host" |
| 2104 |
filesystem that we want in the chroot |
| 2105 |
environment. */ |
| 2106 |
foreach (DirsInChroot::iterator, i, dirsInChroot) { |
| 2107 |
struct stat st; |
| 2108 |
Path source = i->second; |
| 2109 |
Path target = chrootRootDir + i->first; |
| 2110 |
if (source == "/proc") continue; // backwards compatibility |
| 2111 |
if (stat(source.c_str(), &st) == -1) |
| 2112 |
throw SysError(format("getting attributes of path `%1%'") % source); |
| 2113 |
if (S_ISDIR(st.st_mode)) |
| 2114 |
createDirs(target); |
| 2115 |
else { |
| 2116 |
createDirs(dirOf(target)); |
| 2117 |
writeFile(target, ""); |
| 2118 |
} |
| 2119 |
if (mount(source.c_str(), target.c_str(), "", MS_BIND, 0) == -1) |
| 2120 |
throw SysError(format("bind mount from `%1%' to `%2%' failed") % source % target); |
| 2121 |
} |
| 2122 |
|
| 2123 |
/* Bind a new instance of procfs on /proc to reflect our |
| 2124 |
private PID namespace. */ |
| 2125 |
createDirs(chrootRootDir + "/proc"); |
| 2126 |
if (mount("none", (chrootRootDir + "/proc").c_str(), "proc", 0, 0) == -1) |
| 2127 |
throw SysError("mounting /proc"); |
| 2128 |
|
| 2129 |
/* Mount a new tmpfs on /dev/shm to ensure that whatever |
| 2130 |
the builder puts in /dev/shm is cleaned up automatically. */ |
| 2131 |
if (pathExists("/dev/shm") && mount("none", (chrootRootDir + "/dev/shm").c_str(), "tmpfs", 0, 0) == -1) |
| 2132 |
throw SysError("mounting /dev/shm"); |
| 2133 |
|
| 2134 |
/* Mount a new devpts on /dev/pts. Note that this |
| 2135 |
requires the kernel to be compiled with |
| 2136 |
CONFIG_DEVPTS_MULTIPLE_INSTANCES=y (which is the case |
| 2137 |
if /dev/ptx/ptmx exists). */ |
| 2138 |
if (pathExists("/dev/pts/ptmx") && |
| 2139 |
!pathExists(chrootRootDir + "/dev/ptmx") |
| 2140 |
&& dirsInChroot.find("/dev/pts") == dirsInChroot.end()) |
| 2141 |
{ |
| 2142 |
if (mount("none", (chrootRootDir + "/dev/pts").c_str(), "devpts", 0, "newinstance,mode=0620") == -1) |
| 2143 |
throw SysError("mounting /dev/pts"); |
| 2144 |
createSymlink("/dev/pts/ptmx", chrootRootDir + "/dev/ptmx"); |
| 2145 |
|
| 2146 |
/* Make sure /dev/pts/ptmx is world-writable. With some |
| 2147 |
Linux versions, it is created with permissions 0. */ |
| 2148 |
chmod_(chrootRootDir + "/dev/pts/ptmx", 0666); |
| 2149 |
} |
| 2150 |
|
| 2151 |
/* Do the chroot(). */ |
| 2152 |
if (chdir(chrootRootDir.c_str()) == -1) |
| 2153 |
throw SysError(format("cannot change directory to '%1%'") % chrootRootDir); |
| 2154 |
|
| 2155 |
if (mkdir("real-root", 0) == -1) |
| 2156 |
throw SysError("cannot create real-root directory"); |
| 2157 |
|
| 2158 |
if (pivot_root(".", "real-root") == -1) |
| 2159 |
throw SysError(format("cannot pivot old root directory onto '%1%'") % (chrootRootDir + "/real-root")); |
| 2160 |
|
| 2161 |
if (chroot(".") == -1) |
| 2162 |
throw SysError(format("cannot change root directory to '%1%'") % chrootRootDir); |
| 2163 |
|
| 2164 |
if (umount2("real-root", MNT_DETACH) == -1) |
| 2165 |
throw SysError("cannot unmount real root filesystem"); |
| 2166 |
|
| 2167 |
if (rmdir("real-root") == -1) |
| 2168 |
throw SysError("cannot remove real-root directory"); |
| 2169 |
} |
| 2170 |
#endif |
| 2171 |
|
| 2172 |
if (chdir(tmpDirInSandbox.c_str()) == -1) |
| 2173 |
throw SysError(format("changing into `%1%'") % tmpDir); |
| 2174 |
|
| 2175 |
/* Close all other file descriptors. */ |
| 2176 |
closeMostFDs(set<int>()); |
| 2177 |
|
| 2178 |
#if __linux__ |
| 2179 |
/* Change the personality to 32-bit if we're doing an |
| 2180 |
i686-linux build on an x86_64-linux machine. */ |
| 2181 |
struct utsname utsbuf; |
| 2182 |
uname(&utsbuf); |
| 2183 |
if (drv.platform == "i686-linux" && |
| 2184 |
(settings.thisSystem == "x86_64-linux" || |
| 2185 |
(!strcmp(utsbuf.sysname, "Linux") && !strcmp(utsbuf.machine, "x86_64")))) { |
| 2186 |
if (personality(PER_LINUX32) == -1) |
| 2187 |
throw SysError("cannot set i686-linux personality"); |
| 2188 |
} |
| 2189 |
|
| 2190 |
if (drv.platform == "armhf-linux" && |
| 2191 |
(settings.thisSystem == "aarch64-linux" || |
| 2192 |
(!strcmp(utsbuf.sysname, "Linux") && !strcmp(utsbuf.machine, "aarch64")))) { |
| 2193 |
if (personality(PER_LINUX32) == -1) |
| 2194 |
throw SysError("cannot set armhf-linux personality"); |
| 2195 |
} |
| 2196 |
|
| 2197 |
/* Impersonate a Linux 2.6 machine to get some determinism in |
| 2198 |
builds that depend on the kernel version. */ |
| 2199 |
if ((drv.platform == "i686-linux" || drv.platform == "x86_64-linux") && settings.impersonateLinux26) { |
| 2200 |
int cur = personality(0xffffffff); |
| 2201 |
if (cur != -1) personality(cur | 0x0020000 /* == UNAME26 */); |
| 2202 |
} |
| 2203 |
|
| 2204 |
/* Disable address space randomization for improved |
| 2205 |
determinism. */ |
| 2206 |
int cur = personality(0xffffffff); |
| 2207 |
if (cur != -1) personality(cur | ADDR_NO_RANDOMIZE); |
| 2208 |
#endif |
| 2209 |
|
| 2210 |
/* Fill in the environment. */ |
| 2211 |
Strings envStrs; |
| 2212 |
foreach (Environment::const_iterator, i, env) |
| 2213 |
envStrs.push_back(rewriteHashes(i->first + "=" + i->second, rewritesToTmp)); |
| 2214 |
|
| 2215 |
/* If we are running in `build-users' mode, then switch to the |
| 2216 |
user we allocated above. Make sure that we drop all root |
| 2217 |
privileges. Note that above we have closed all file |
| 2218 |
descriptors except std*, so that's safe. Also note that |
| 2219 |
setuid() when run as root sets the real, effective and |
| 2220 |
saved UIDs. */ |
| 2221 |
if (buildUser.enabled()) { |
| 2222 |
/* Preserve supplementary groups of the build user, to allow |
| 2223 |
admins to specify groups such as "kvm". */ |
| 2224 |
if (setgroups(buildUser.getSupplementaryGIDs().size(), |
| 2225 |
buildUser.getSupplementaryGIDs().data()) == -1) |
| 2226 |
throw SysError("cannot set supplementary groups of build user"); |
| 2227 |
|
| 2228 |
if (setgid(buildUser.getGID()) == -1 || |
| 2229 |
getgid() != buildUser.getGID() || |
| 2230 |
getegid() != buildUser.getGID()) |
| 2231 |
throw SysError("setgid failed"); |
| 2232 |
|
| 2233 |
if (setuid(buildUser.getUID()) == -1 || |
| 2234 |
getuid() != buildUser.getUID() || |
| 2235 |
geteuid() != buildUser.getUID()) |
| 2236 |
throw SysError("setuid failed"); |
| 2237 |
} |
| 2238 |
|
| 2239 |
restoreSIGPIPE(); |
| 2240 |
|
| 2241 |
/* Indicate that we managed to set up the build environment. */ |
| 2242 |
writeFull(STDERR_FILENO, "\n"); |
| 2243 |
|
| 2244 |
/* Execute the program. This should not return. */ |
| 2245 |
if (isBuiltin(drv)) { |
| 2246 |
try { |
| 2247 |
logType = ltFlat; |
| 2248 |
|
| 2249 |
auto buildDrv = lookupBuiltinBuilder(drv.builder); |
| 2250 |
if (buildDrv != NULL) { |
| 2251 |
/* Check what the output file name is. When doing a |
| 2252 |
'bmCheck' build, the output file name is different from |
| 2253 |
that specified in DRV due to hash rewriting. */ |
| 2254 |
Path output = drv.outputs["out"].path; |
| 2255 |
auto redirected = redirectedOutputs.find(output); |
| 2256 |
if (redirected != redirectedOutputs.end()) |
| 2257 |
output = redirected->second; |
| 2258 |
|
| 2259 |
buildDrv(drv, drvPath, output); |
| 2260 |
} |
| 2261 |
else |
| 2262 |
throw Error(format("unsupported builtin function '%1%'") % string(drv.builder, 8)); |
| 2263 |
_exit(0); |
| 2264 |
} catch (std::exception & e) { |
| 2265 |
writeFull(STDERR_FILENO, "error: " + string(e.what()) + "\n"); |
| 2266 |
_exit(1); |
| 2267 |
} |
| 2268 |
} |
| 2269 |
|
| 2270 |
/* Fill in the arguments. */ |
| 2271 |
Strings args; |
| 2272 |
string builderBasename = baseNameOf(drv.builder); |
| 2273 |
args.push_back(builderBasename); |
| 2274 |
foreach (Strings::iterator, i, drv.args) |
| 2275 |
args.push_back(rewriteHashes(*i, rewritesToTmp)); |
| 2276 |
|
| 2277 |
/* If DRV targets the same operating system kernel, try to execute it: |
| 2278 |
there might be binfmt_misc set up for user-land emulation of other |
| 2279 |
architectures. However, if it targets a different operating |
| 2280 |
system--e.g., "i586-gnu" vs. "x86_64-linux"--do not try executing |
| 2281 |
it: the ELF file for that OS is likely indistinguishable from a |
| 2282 |
native ELF binary and it would just crash at run time. */ |
| 2283 |
int error; |
| 2284 |
if (sameOperatingSystemKernel(drv.platform, settings.thisSystem)) { |
| 2285 |
execve(drv.builder.c_str(), stringsToCharPtrs(args).data(), |
| 2286 |
stringsToCharPtrs(envStrs).data()); |
| 2287 |
error = errno; |
| 2288 |
} else { |
| 2289 |
error = ENOEXEC; |
| 2290 |
} |
| 2291 |
|
| 2292 |
/* Right platform? Check this after we've tried 'execve' to allow for |
| 2293 |
transparent emulation of different platforms with binfmt_misc |
| 2294 |
handlers that invoke QEMU. */ |
| 2295 |
if (error == ENOEXEC && !canBuildLocally(drv.platform)) { |
| 2296 |
if (settings.printBuildTrace) |
| 2297 |
printMsg(lvlError, format("@ unsupported-platform %1% %2%") % drvPath % drv.platform); |
| 2298 |
throw Error( |
| 2299 |
format("a `%1%' is required to build `%3%', but I am a `%2%'") |
| 2300 |
% drv.platform % settings.thisSystem % drvPath); |
| 2301 |
} |
| 2302 |
|
| 2303 |
errno = error; |
| 2304 |
throw SysError(format("executing `%1%'") % drv.builder); |
| 2305 |
|
| 2306 |
} catch (std::exception & e) { |
| 2307 |
writeFull(STDERR_FILENO, "while setting up the build environment: " + string(e.what()) + "\n"); |
| 2308 |
_exit(1); |
| 2309 |
} |
| 2310 |
|
| 2311 |
abort(); /* never reached */ |
| 2312 |
} |
| 2313 |
|
| 2314 |
|
| 2315 |
/* Parse a list of reference specifiers. Each element must either be |
| 2316 |
a store path, or the symbolic name of the output of the derivation |
| 2317 |
(such as `out'). */ |
| 2318 |
PathSet parseReferenceSpecifiers(const Derivation & drv, string attr) |
| 2319 |
{ |
| 2320 |
PathSet result; |
| 2321 |
Paths paths = tokenizeString<Paths>(attr); |
| 2322 |
foreach (Strings::iterator, i, paths) { |
| 2323 |
if (isStorePath(*i)) |
| 2324 |
result.insert(*i); |
| 2325 |
else if (drv.outputs.find(*i) != drv.outputs.end()) |
| 2326 |
result.insert(drv.outputs.find(*i)->second.path); |
| 2327 |
else throw BuildError( |
| 2328 |
format("derivation contains an invalid reference specifier `%1%'") |
| 2329 |
% *i); |
| 2330 |
} |
| 2331 |
return result; |
| 2332 |
} |
| 2333 |
|
| 2334 |
|
| 2335 |
void DerivationGoal::registerOutputs() |
| 2336 |
{ |
| 2337 |
/* When using a build hook, the build hook can register the output |
| 2338 |
as valid (by doing `nix-store --import'). If so we don't have |
| 2339 |
to do anything here. */ |
| 2340 |
if (hook) { |
| 2341 |
bool allValid = true; |
| 2342 |
foreach (DerivationOutputs::iterator, i, drv.outputs) |
| 2343 |
if (!worker.store.isValidPath(i->second.path)) allValid = false; |
| 2344 |
if (allValid) return; |
| 2345 |
} |
| 2346 |
|
| 2347 |
ValidPathInfos infos; |
| 2348 |
|
| 2349 |
/* Set of inodes seen during calls to canonicalisePathMetaData() |
| 2350 |
for this build's outputs. This needs to be shared between |
| 2351 |
outputs to allow hard links between outputs. */ |
| 2352 |
InodesSeen inodesSeen; |
| 2353 |
|
| 2354 |
Path checkSuffix = "-check"; |
| 2355 |
|
| 2356 |
/* Check whether the output paths were created, and grep each |
| 2357 |
output path to determine what other paths it references. Also make all |
| 2358 |
output paths read-only. */ |
| 2359 |
foreach (DerivationOutputs::iterator, i, drv.outputs) { |
| 2360 |
Path path = i->second.path; |
| 2361 |
if (missingPaths.find(path) == missingPaths.end()) continue; |
| 2362 |
|
| 2363 |
Path actualPath = path; |
| 2364 |
if (useChroot) { |
| 2365 |
actualPath = chrootRootDir + path; |
| 2366 |
} else { |
| 2367 |
Path redirected = redirectedOutputs[path]; |
| 2368 |
if (buildMode == bmRepair |
| 2369 |
&& redirectedBadOutputs.find(path) != redirectedBadOutputs.end() |
| 2370 |
&& pathExists(redirected)) |
| 2371 |
replaceValidPath(path, redirected); |
| 2372 |
if (buildMode == bmCheck && redirected != "") |
| 2373 |
actualPath = redirected; |
| 2374 |
} |
| 2375 |
|
| 2376 |
struct stat st; |
| 2377 |
if (lstat(actualPath.c_str(), &st) == -1) { |
| 2378 |
if (errno == ENOENT) |
| 2379 |
throw BuildError( |
| 2380 |
format("builder for `%1%' failed to produce output path `%2%'") |
| 2381 |
% drvPath % path); |
| 2382 |
throw SysError(format("getting attributes of path `%1%'") % actualPath); |
| 2383 |
} |
| 2384 |
|
| 2385 |
#ifndef __CYGWIN__ |
| 2386 |
/* Check that the output is not group or world writable, as |
| 2387 |
that means that someone else can have interfered with the |
| 2388 |
build. Also, the output should be owned by the build |
| 2389 |
user. */ |
| 2390 |
if ((!S_ISLNK(st.st_mode) && (st.st_mode & (S_IWGRP | S_IWOTH))) || |
| 2391 |
(buildUser.enabled() && st.st_uid != buildUser.getUID())) |
| 2392 |
throw BuildError(format("suspicious ownership or permission on `%1%'; rejecting this build output") % path); |
| 2393 |
#endif |
| 2394 |
|
| 2395 |
/* Apply hash rewriting if necessary. */ |
| 2396 |
bool rewritten = false; |
| 2397 |
if (!rewritesFromTmp.empty()) { |
| 2398 |
printMsg(lvlError, format("warning: rewriting hashes in `%1%'; cross fingers") % path); |
| 2399 |
|
| 2400 |
/* Canonicalise first. This ensures that the path we're |
| 2401 |
rewriting doesn't contain a hard link to /etc/shadow or |
| 2402 |
something like that. */ |
| 2403 |
canonicalisePathMetaData(actualPath, buildUser.enabled() ? buildUser.getUID() : -1, inodesSeen); |
| 2404 |
|
| 2405 |
/* FIXME: this is in-memory. */ |
| 2406 |
StringSink sink; |
| 2407 |
dumpPath(actualPath, sink); |
| 2408 |
deletePath(actualPath); |
| 2409 |
sink.s = rewriteHashes(sink.s, rewritesFromTmp); |
| 2410 |
StringSource source(sink.s); |
| 2411 |
restorePath(actualPath, source); |
| 2412 |
|
| 2413 |
rewritten = true; |
| 2414 |
} |
| 2415 |
|
| 2416 |
startNest(nest, lvlTalkative, |
| 2417 |
format("scanning for references inside `%1%'") % path); |
| 2418 |
|
| 2419 |
/* Check that fixed-output derivations produced the right |
| 2420 |
outputs (i.e., the content hash should match the specified |
| 2421 |
hash). */ |
| 2422 |
if (i->second.hash != "") { |
| 2423 |
|
| 2424 |
bool recursive; HashType ht; Hash h; |
| 2425 |
i->second.parseHashInfo(recursive, ht, h); |
| 2426 |
|
| 2427 |
if (!recursive) { |
| 2428 |
/* The output path should be a regular file without |
| 2429 |
execute permission. */ |
| 2430 |
if (!S_ISREG(st.st_mode) || (st.st_mode & S_IXUSR) != 0) |
| 2431 |
throw BuildError( |
| 2432 |
format("output path `%1% should be a non-executable regular file") % path); |
| 2433 |
} |
| 2434 |
|
| 2435 |
/* Check the hash. */ |
| 2436 |
Hash h2 = recursive ? hashPath(ht, actualPath).first : hashFile(ht, actualPath); |
| 2437 |
if (h != h2) { |
| 2438 |
if (settings.printBuildTrace) |
| 2439 |
printMsg(lvlError, format("@ hash-mismatch %1% %2% %3% %4%") |
| 2440 |
% path % i->second.hashAlgo |
| 2441 |
% printHash16or32(h) % printHash16or32(h2)); |
| 2442 |
throw BuildError(format("hash mismatch for store item '%1%'") % path); |
| 2443 |
} |
| 2444 |
} |
| 2445 |
|
| 2446 |
/* Get rid of all weird permissions. This also checks that |
| 2447 |
all files are owned by the build user, if applicable. */ |
| 2448 |
canonicalisePathMetaData(actualPath, |
| 2449 |
buildUser.enabled() && !rewritten ? buildUser.getUID() : -1, inodesSeen); |
| 2450 |
|
| 2451 |
if (useChroot) { |
| 2452 |
if (pathExists(actualPath)) { |
| 2453 |
/* Now that output paths have been canonicalized (in particular |
| 2454 |
there are no setuid files left), move them outside of the |
| 2455 |
chroot and to the store. */ |
| 2456 |
if (buildMode == bmRepair) |
| 2457 |
replaceValidPath(path, actualPath); |
| 2458 |
else |
| 2459 |
if (buildMode != bmCheck && rename(actualPath.c_str(), path.c_str()) == -1) |
| 2460 |
throw SysError(format("moving build output `%1%' from the chroot to the store") % path); |
| 2461 |
} |
| 2462 |
if (buildMode != bmCheck) actualPath = path; |
| 2463 |
} |
| 2464 |
|
| 2465 |
/* For this output path, find the references to other paths |
| 2466 |
contained in it. Compute the SHA-256 NAR hash at the same |
| 2467 |
time. The hash is stored in the database so that we can |
| 2468 |
verify later on whether nobody has messed with the store. */ |
| 2469 |
HashResult hash; |
| 2470 |
PathSet references = scanForReferences(actualPath, allPaths, hash); |
| 2471 |
|
| 2472 |
if (buildMode == bmCheck) { |
| 2473 |
if (!store->isValidPath(path)) continue; |
| 2474 |
ValidPathInfo info = worker.store.queryPathInfo(path); |
| 2475 |
if (hash.first != info.hash) { |
| 2476 |
if (settings.keepFailed) { |
| 2477 |
Path dst = path + checkSuffix; |
| 2478 |
if (pathExists(dst)) deletePath(dst); |
| 2479 |
if (rename(actualPath.c_str(), dst.c_str())) |
| 2480 |
throw SysError(format("renaming `%1%' to `%2%'") % actualPath % dst); |
| 2481 |
throw Error(format("derivation `%1%' may not be deterministic: output `%2%' differs from `%3%'") |
| 2482 |
% drvPath % path % dst); |
| 2483 |
} else |
| 2484 |
throw Error(format("derivation `%1%' may not be deterministic: output `%2%' differs") |
| 2485 |
% drvPath % path); |
| 2486 |
} |
| 2487 |
|
| 2488 |
if (settings.printBuildTrace) |
| 2489 |
printMsg(lvlError, format("@ build-succeeded %1% -") % drvPath); |
| 2490 |
|
| 2491 |
continue; |
| 2492 |
} |
| 2493 |
|
| 2494 |
/* For debugging, print out the referenced and unreferenced |
| 2495 |
paths. */ |
| 2496 |
foreach (PathSet::iterator, i, inputPaths) { |
| 2497 |
PathSet::iterator j = references.find(*i); |
| 2498 |
if (j == references.end()) |
| 2499 |
debug(format("unreferenced input: `%1%'") % *i); |
| 2500 |
else |
| 2501 |
debug(format("referenced input: `%1%'") % *i); |
| 2502 |
} |
| 2503 |
|
| 2504 |
/* Enforce `allowedReferences' and friends. */ |
| 2505 |
auto checkRefs = [&](const string & attrName, bool allowed, bool recursive) { |
| 2506 |
if (drv.env.find(attrName) == drv.env.end()) return; |
| 2507 |
|
| 2508 |
PathSet spec = parseReferenceSpecifiers(drv, get(drv.env, attrName)); |
| 2509 |
|
| 2510 |
PathSet used; |
| 2511 |
if (recursive) { |
| 2512 |
/* Our requisites are the union of the closures of our references. */ |
| 2513 |
for (auto & i : references) |
| 2514 |
/* Don't call computeFSClosure on ourselves. */ |
| 2515 |
if (actualPath != i) |
| 2516 |
computeFSClosure(worker.store, i, used); |
| 2517 |
} else |
| 2518 |
used = references; |
| 2519 |
|
| 2520 |
for (auto & i : used) |
| 2521 |
if (allowed) { |
| 2522 |
if (spec.find(i) == spec.end()) |
| 2523 |
throw BuildError(format("output (`%1%') is not allowed to refer to path `%2%'") % actualPath % i); |
| 2524 |
} else { |
| 2525 |
if (spec.find(i) != spec.end()) |
| 2526 |
throw BuildError(format("output (`%1%') is not allowed to refer to path `%2%'") % actualPath % i); |
| 2527 |
} |
| 2528 |
}; |
| 2529 |
|
| 2530 |
checkRefs("allowedReferences", true, false); |
| 2531 |
checkRefs("allowedRequisites", true, true); |
| 2532 |
checkRefs("disallowedReferences", false, false); |
| 2533 |
checkRefs("disallowedRequisites", false, true); |
| 2534 |
|
| 2535 |
if (curRound == nrRounds) { |
| 2536 |
worker.store.optimisePath(path); // FIXME: combine with scanForReferences() |
| 2537 |
|
| 2538 |
worker.store.markContentsGood(path); |
| 2539 |
} |
| 2540 |
|
| 2541 |
ValidPathInfo info; |
| 2542 |
info.path = path; |
| 2543 |
info.hash = hash.first; |
| 2544 |
info.narSize = hash.second; |
| 2545 |
info.references = references; |
| 2546 |
info.deriver = drvPath; |
| 2547 |
infos.push_back(info); |
| 2548 |
} |
| 2549 |
|
| 2550 |
/* Compare the result with the previous round, and report which |
| 2551 |
path is different, if any.*/ |
| 2552 |
if (curRound > 1 && prevInfos != infos) { |
| 2553 |
assert(prevInfos.size() == infos.size()); |
| 2554 |
for (auto i = prevInfos.begin(), j = infos.begin(); i != prevInfos.end(); ++i, ++j) |
| 2555 |
if (!(*i == *j)) { |
| 2556 |
Path prev = i->path + checkSuffix; |
| 2557 |
if (pathExists(prev)) |
| 2558 |
throw NotDeterministic( |
| 2559 |
format("output ‘%1%’ of ‘%2%’ differs from ‘%3%’ from previous round") |
| 2560 |
% i->path % drvPath % prev); |
| 2561 |
else |
| 2562 |
throw NotDeterministic( |
| 2563 |
format("output ‘%1%’ of ‘%2%’ differs from previous round") |
| 2564 |
% i->path % drvPath); |
| 2565 |
} |
| 2566 |
assert(false); // shouldn't happen |
| 2567 |
} |
| 2568 |
|
| 2569 |
if (settings.keepFailed) { |
| 2570 |
for (auto & i : drv.outputs) { |
| 2571 |
Path prev = i.second.path + checkSuffix; |
| 2572 |
if (pathExists(prev)) deletePath(prev); |
| 2573 |
if (curRound < nrRounds) { |
| 2574 |
Path dst = i.second.path + checkSuffix; |
| 2575 |
if (rename(i.second.path.c_str(), dst.c_str())) |
| 2576 |
throw SysError(format("renaming ‘%1%’ to ‘%2%’") % i.second.path % dst); |
| 2577 |
} |
| 2578 |
} |
| 2579 |
|
| 2580 |
} |
| 2581 |
|
| 2582 |
if (curRound < nrRounds) { |
| 2583 |
prevInfos = infos; |
| 2584 |
return; |
| 2585 |
} |
| 2586 |
|
| 2587 |
/* Register each output path as valid, and register the sets of |
| 2588 |
paths referenced by each of them. If there are cycles in the |
| 2589 |
outputs, this will fail. */ |
| 2590 |
worker.store.registerValidPaths(infos); |
| 2591 |
} |
| 2592 |
|
| 2593 |
|
| 2594 |
string drvsLogDir = "drvs"; |
| 2595 |
|
| 2596 |
|
| 2597 |
Path DerivationGoal::openLogFile() |
| 2598 |
{ |
| 2599 |
logSize = 0; |
| 2600 |
|
| 2601 |
if (!settings.keepLog) return ""; |
| 2602 |
|
| 2603 |
string baseName = baseNameOf(drvPath); |
| 2604 |
|
| 2605 |
/* Create a log file. */ |
| 2606 |
Path dir = (format("%1%/%2%/%3%/") % settings.nixLogDir % drvsLogDir % string(baseName, 0, 2)).str(); |
| 2607 |
createDirs(dir); |
| 2608 |
|
| 2609 |
switch (settings.logCompression) |
| 2610 |
{ |
| 2611 |
case COMPRESSION_GZIP: { |
| 2612 |
Path logFileName = (format("%1%/%2%.gz") % dir % string(baseName, 2)).str(); |
| 2613 |
AutoCloseFD fd = open(logFileName.c_str(), O_CREAT | O_WRONLY | O_TRUNC, 0666); |
| 2614 |
if (fd == -1) throw SysError(format("creating log file `%1%'") % logFileName); |
| 2615 |
closeOnExec(fd); |
| 2616 |
|
| 2617 |
/* Note: FD will be closed by 'gzclose'. */ |
| 2618 |
if (!(gzLogFile = gzdopen(fd.borrow(), "w"))) |
| 2619 |
throw Error(format("cannot open compressed log file `%1%'") % logFileName); |
| 2620 |
|
| 2621 |
gzbuffer(gzLogFile, 32768); |
| 2622 |
gzsetparams(gzLogFile, Z_BEST_COMPRESSION, Z_DEFAULT_STRATEGY); |
| 2623 |
|
| 2624 |
return logFileName; |
| 2625 |
} |
| 2626 |
|
| 2627 |
#if HAVE_BZLIB_H |
| 2628 |
case COMPRESSION_BZIP2: { |
| 2629 |
Path logFileName = (format("%1%/%2%.bz2") % dir % string(baseName, 2)).str(); |
| 2630 |
AutoCloseFD fd = open(logFileName.c_str(), O_CREAT | O_WRONLY | O_TRUNC, 0666); |
| 2631 |
if (fd == -1) throw SysError(format("creating log file `%1%'") % logFileName); |
| 2632 |
closeOnExec(fd); |
| 2633 |
|
| 2634 |
if (!(fLogFile = fdopen(fd.borrow(), "w"))) |
| 2635 |
throw SysError(format("opening log file `%1%'") % logFileName); |
| 2636 |
|
| 2637 |
int err; |
| 2638 |
if (!(bzLogFile = BZ2_bzWriteOpen(&err, fLogFile, 9, 0, 0))) |
| 2639 |
throw Error(format("cannot open compressed log file `%1%'") % logFileName); |
| 2640 |
|
| 2641 |
return logFileName; |
| 2642 |
} |
| 2643 |
#endif |
| 2644 |
|
| 2645 |
case COMPRESSION_NONE: { |
| 2646 |
Path logFileName = (format("%1%/%2%") % dir % string(baseName, 2)).str(); |
| 2647 |
fdLogFile = open(logFileName.c_str(), O_CREAT | O_WRONLY | O_TRUNC, 0666); |
| 2648 |
if (fdLogFile == -1) throw SysError(format("creating log file `%1%'") % logFileName); |
| 2649 |
closeOnExec(fdLogFile); |
| 2650 |
return logFileName; |
| 2651 |
} |
| 2652 |
} |
| 2653 |
|
| 2654 |
abort(); |
| 2655 |
} |
| 2656 |
|
| 2657 |
|
| 2658 |
void DerivationGoal::closeLogFile() |
| 2659 |
{ |
| 2660 |
if (gzLogFile) { |
| 2661 |
int err; |
| 2662 |
err = gzclose(gzLogFile); |
| 2663 |
gzLogFile = NULL; |
| 2664 |
if (err != Z_OK) throw Error(format("cannot close compressed log file (gzip error = %1%)") % err); |
| 2665 |
} |
| 2666 |
#if HAVE_BZLIB_H |
| 2667 |
else if (bzLogFile) { |
| 2668 |
int err; |
| 2669 |
BZ2_bzWriteClose(&err, bzLogFile, 0, 0, 0); |
| 2670 |
bzLogFile = 0; |
| 2671 |
if (err != BZ_OK) throw Error(format("cannot close compressed log file (BZip2 error = %1%)") % err); |
| 2672 |
} |
| 2673 |
#endif |
| 2674 |
|
| 2675 |
if (fLogFile) { |
| 2676 |
fclose(fLogFile); |
| 2677 |
fLogFile = 0; |
| 2678 |
} |
| 2679 |
|
| 2680 |
fdLogFile.close(); |
| 2681 |
} |
| 2682 |
|
| 2683 |
|
| 2684 |
static void _chown(const Path & path, uid_t uid, gid_t gid) |
| 2685 |
{ |
| 2686 |
checkInterrupt(); |
| 2687 |
|
| 2688 |
if (lchown(path.c_str(), uid, gid) == -1) { |
| 2689 |
throw SysError(format("change owner and group of `%1%'") % path); |
| 2690 |
} |
| 2691 |
struct stat st = lstat(path); |
| 2692 |
if (S_ISDIR(st.st_mode)) { |
| 2693 |
for (auto & i : readDirectory(path)) |
| 2694 |
_chown(path + "/" + i.name, uid, gid); |
| 2695 |
} |
| 2696 |
} |
| 2697 |
|
| 2698 |
|
| 2699 |
void DerivationGoal::deleteTmpDir(bool force) |
| 2700 |
{ |
| 2701 |
if (tmpDir != "") { |
| 2702 |
// When useChroot is true, tmpDir looks like |
| 2703 |
// "/tmp/guix-build-foo.drv-0/top". Its parent is root-owned. |
| 2704 |
string top; |
| 2705 |
if (useChroot) { |
| 2706 |
if (baseNameOf(tmpDir) != "top") abort(); |
| 2707 |
top = dirOf(tmpDir); |
| 2708 |
} else top = tmpDir; |
| 2709 |
|
| 2710 |
if (settings.keepFailed && !force) { |
| 2711 |
printMsg(lvlError, |
| 2712 |
format("note: keeping build directory `%2%'") |
| 2713 |
% drvPath % top); |
| 2714 |
chmod(tmpDir.c_str(), 0755); |
| 2715 |
|
| 2716 |
// Change the ownership if clientUid is set. Never change the |
| 2717 |
// ownership or the group to "root" for security reasons. |
| 2718 |
if (settings.clientUid != (uid_t) -1 && settings.clientUid != 0) { |
| 2719 |
_chown(tmpDir, settings.clientUid, |
| 2720 |
settings.clientGid != 0 ? settings.clientGid : -1); |
| 2721 |
|
| 2722 |
if (top != tmpDir) { |
| 2723 |
// Rename tmpDir to its parent, with an intermediate step. |
| 2724 |
string pivot = top + ".pivot"; |
| 2725 |
if (rename(top.c_str(), pivot.c_str()) == -1) |
| 2726 |
throw SysError("pivoting failed build tree"); |
| 2727 |
if (rename((pivot + "/top").c_str(), top.c_str()) == -1) |
| 2728 |
throw SysError("renaming failed build tree"); |
| 2729 |
rmdir(pivot.c_str()); |
| 2730 |
} |
| 2731 |
} |
| 2732 |
} |
| 2733 |
else { |
| 2734 |
deletePath(tmpDir); |
| 2735 |
if (top != tmpDir) rmdir(dirOf(tmpDir).c_str()); |
| 2736 |
} |
| 2737 |
tmpDir = ""; |
| 2738 |
} |
| 2739 |
} |
| 2740 |
|
| 2741 |
|
| 2742 |
void DerivationGoal::handleChildOutput(int fd, const string & data) |
| 2743 |
{ |
| 2744 |
string prefix; |
| 2745 |
|
| 2746 |
if (settings.multiplexedBuildOutput) { |
| 2747 |
/* Print a prefix that allows clients to determine whether a message |
| 2748 |
comes from the daemon or from a build process, and in the latter |
| 2749 |
case, which build process it comes from. The PID here matches the |
| 2750 |
one given in "@ build-started" traces; it's shorter that the |
| 2751 |
derivation file name, hence this choice. */ |
| 2752 |
prefix = "@ build-log " |
| 2753 |
+ std::to_string(pid < 0 ? hook->pid : pid) |
| 2754 |
+ " " + std::to_string(data.size()) + "\n"; |
| 2755 |
} |
| 2756 |
|
| 2757 |
if ((hook && fd == hook->builderOut.readSide) || |
| 2758 |
(!hook && fd == builderOut.readSide)) |
| 2759 |
{ |
| 2760 |
logSize += data.size(); |
| 2761 |
if (settings.maxLogSize && logSize > settings.maxLogSize) { |
| 2762 |
printMsg(lvlError, |
| 2763 |
format("%1% killed after writing more than %2% bytes of log output") |
| 2764 |
% getName() % settings.maxLogSize); |
| 2765 |
timedOut(); // not really a timeout, but close enough |
| 2766 |
return; |
| 2767 |
} |
| 2768 |
if (verbosity >= settings.buildVerbosity) |
| 2769 |
writeToStderr(prefix + data); |
| 2770 |
|
| 2771 |
if (gzLogFile) { |
| 2772 |
if (data.size() > 0) { |
| 2773 |
int count, err; |
| 2774 |
count = gzwrite(gzLogFile, data.data(), data.size()); |
| 2775 |
if (count == 0) throw Error(format("cannot write to compressed log file (gzip error = %1%)") % gzerror(gzLogFile, &err)); |
| 2776 |
} |
| 2777 |
#if HAVE_BZLIB_H |
| 2778 |
} else if (bzLogFile) { |
| 2779 |
int err; |
| 2780 |
BZ2_bzWrite(&err, bzLogFile, (unsigned char *) data.data(), data.size()); |
| 2781 |
if (err != BZ_OK) throw Error(format("cannot write to compressed log file (BZip2 error = %1%)") % err); |
| 2782 |
#endif |
| 2783 |
} else if (fdLogFile != -1) |
| 2784 |
writeFull(fdLogFile, data); |
| 2785 |
} |
| 2786 |
|
| 2787 |
if (hook && fd == hook->fromAgent.readSide) |
| 2788 |
writeToStderr(prefix + data); |
| 2789 |
} |
| 2790 |
|
| 2791 |
|
| 2792 |
void DerivationGoal::handleEOF(int fd) |
| 2793 |
{ |
| 2794 |
worker.wakeUp(shared_from_this()); |
| 2795 |
} |
| 2796 |
|
| 2797 |
|
| 2798 |
PathSet DerivationGoal::checkPathValidity(bool returnValid, bool checkHash) |
| 2799 |
{ |
| 2800 |
PathSet result; |
| 2801 |
foreach (DerivationOutputs::iterator, i, drv.outputs) { |
| 2802 |
if (!wantOutput(i->first, wantedOutputs)) continue; |
| 2803 |
bool good = |
| 2804 |
worker.store.isValidPath(i->second.path) && |
| 2805 |
(!checkHash || worker.store.pathContentsGood(i->second.path)); |
| 2806 |
if (good == returnValid) result.insert(i->second.path); |
| 2807 |
} |
| 2808 |
return result; |
| 2809 |
} |
| 2810 |
|
| 2811 |
|
| 2812 |
bool DerivationGoal::pathFailed(const Path & path) |
| 2813 |
{ |
| 2814 |
if (!settings.cacheFailure) return false; |
| 2815 |
|
| 2816 |
if (!worker.store.hasPathFailed(path)) return false; |
| 2817 |
|
| 2818 |
printMsg(lvlError, format("builder for `%1%' failed previously (cached)") % path); |
| 2819 |
|
| 2820 |
if (settings.printBuildTrace) |
| 2821 |
printMsg(lvlError, format("@ build-failed %1% - cached") % drvPath); |
| 2822 |
|
| 2823 |
done(BuildResult::CachedFailure); |
| 2824 |
|
| 2825 |
return true; |
| 2826 |
} |
| 2827 |
|
| 2828 |
|
| 2829 |
Path DerivationGoal::addHashRewrite(const Path & path) |
| 2830 |
{ |
| 2831 |
string h1 = string(path, settings.nixStore.size() + 1, 32); |
| 2832 |
string h2 = string(printHash32(hashString(htSHA256, "rewrite:" + drvPath + ":" + path)), 0, 32); |
| 2833 |
Path p = settings.nixStore + "/" + h2 + string(path, settings.nixStore.size() + 33); |
| 2834 |
if (pathExists(p)) deletePath(p); |
| 2835 |
assert(path.size() == p.size()); |
| 2836 |
rewritesToTmp[h1] = h2; |
| 2837 |
rewritesFromTmp[h2] = h1; |
| 2838 |
redirectedOutputs[path] = p; |
| 2839 |
printMsg(lvlChatty, format("output '%1%' redirected to '%2%'") |
| 2840 |
% path % p); |
| 2841 |
return p; |
| 2842 |
} |
| 2843 |
|
| 2844 |
|
| 2845 |
void DerivationGoal::done(BuildResult::Status status, const string & msg) |
| 2846 |
{ |
| 2847 |
result.status = status; |
| 2848 |
result.errorMsg = msg; |
| 2849 |
amDone(result.success() ? ecSuccess : ecFailed); |
| 2850 |
if (result.status == BuildResult::TimedOut) |
| 2851 |
worker.timedOut = true; |
| 2852 |
if (result.status == BuildResult::PermanentFailure || result.status == BuildResult::CachedFailure) |
| 2853 |
worker.permanentFailure = true; |
| 2854 |
} |
| 2855 |
|
| 2856 |
|
| 2857 |
////////////////////////////////////////////////////////////////////// |
| 2858 |
|
| 2859 |
|
| 2860 |
class SubstitutionGoal : public Goal |
| 2861 |
{ |
| 2862 |
friend class Worker; |
| 2863 |
|
| 2864 |
private: |
| 2865 |
/* The store path that should be realised through a substitute. */ |
| 2866 |
Path storePath; |
| 2867 |
|
| 2868 |
/* Path info returned by the substituter's query info operation. */ |
| 2869 |
SubstitutablePathInfo info; |
| 2870 |
|
| 2871 |
/* Lock on the store path. */ |
| 2872 |
std::shared_ptr<PathLocks> outputLock; |
| 2873 |
|
| 2874 |
/* Whether to try to repair a valid path. */ |
| 2875 |
bool repair; |
| 2876 |
|
| 2877 |
/* Location where we're downloading the substitute. Differs from |
| 2878 |
storePath when doing a repair. */ |
| 2879 |
Path destPath; |
| 2880 |
|
| 2881 |
typedef void (SubstitutionGoal::*GoalState)(); |
| 2882 |
GoalState state; |
| 2883 |
|
| 2884 |
/* The substituter. */ |
| 2885 |
std::shared_ptr<Agent> substituter; |
| 2886 |
|
| 2887 |
/* Either the empty string, or the status phrase returned by the |
| 2888 |
substituter. */ |
| 2889 |
string status; |
| 2890 |
|
| 2891 |
void tryNext(); |
| 2892 |
|
| 2893 |
public: |
| 2894 |
SubstitutionGoal(const Path & storePath, Worker & worker, bool repair = false); |
| 2895 |
~SubstitutionGoal(); |
| 2896 |
|
| 2897 |
void timedOut(); |
| 2898 |
|
| 2899 |
string key() |
| 2900 |
{ |
| 2901 |
/* "a$" ensures substitution goals happen before derivation |
| 2902 |
goals. */ |
| 2903 |
return "a$" + storePathToName(storePath) + "$" + storePath; |
| 2904 |
} |
| 2905 |
|
| 2906 |
void work(); |
| 2907 |
|
| 2908 |
/* The states. */ |
| 2909 |
void init(); |
| 2910 |
void gotInfo(); |
| 2911 |
void referencesValid(); |
| 2912 |
void tryToRun(); |
| 2913 |
void finished(); |
| 2914 |
|
| 2915 |
/* Callback used by the worker to write to the log. */ |
| 2916 |
void handleChildOutput(int fd, const string & data); |
| 2917 |
void handleEOF(int fd); |
| 2918 |
|
| 2919 |
Path getStorePath() { return storePath; } |
| 2920 |
}; |
| 2921 |
|
| 2922 |
|
| 2923 |
SubstitutionGoal::SubstitutionGoal(const Path & storePath, Worker & worker, bool repair) |
| 2924 |
: Goal(worker) |
| 2925 |
, repair(repair) |
| 2926 |
{ |
| 2927 |
this->storePath = storePath; |
| 2928 |
state = &SubstitutionGoal::init; |
| 2929 |
name = (format("substitution of `%1%'") % storePath).str(); |
| 2930 |
trace("created"); |
| 2931 |
} |
| 2932 |
|
| 2933 |
|
| 2934 |
SubstitutionGoal::~SubstitutionGoal() |
| 2935 |
{ |
| 2936 |
if (substituter) worker.childTerminated(substituter->pid); |
| 2937 |
} |
| 2938 |
|
| 2939 |
|
| 2940 |
void SubstitutionGoal::timedOut() |
| 2941 |
{ |
| 2942 |
if (settings.printBuildTrace) |
| 2943 |
printMsg(lvlError, format("@ substituter-failed %1% timeout") % storePath); |
| 2944 |
if (substituter) { |
| 2945 |
pid_t savedPid = substituter->pid; |
| 2946 |
substituter.reset(); |
| 2947 |
worker.childTerminated(savedPid); |
| 2948 |
} |
| 2949 |
amDone(ecFailed); |
| 2950 |
} |
| 2951 |
|
| 2952 |
|
| 2953 |
void SubstitutionGoal::work() |
| 2954 |
{ |
| 2955 |
(this->*state)(); |
| 2956 |
} |
| 2957 |
|
| 2958 |
|
| 2959 |
void SubstitutionGoal::init() |
| 2960 |
{ |
| 2961 |
trace("init"); |
| 2962 |
|
| 2963 |
worker.store.addTempRoot(storePath); |
| 2964 |
|
| 2965 |
/* If the path already exists we're done. */ |
| 2966 |
if (!repair && worker.store.isValidPath(storePath)) { |
| 2967 |
amDone(ecSuccess); |
| 2968 |
return; |
| 2969 |
} |
| 2970 |
|
| 2971 |
if (settings.readOnlyMode) |
| 2972 |
throw Error(format("cannot substitute path `%1%' - no write access to the store") % storePath); |
| 2973 |
|
| 2974 |
tryNext(); |
| 2975 |
} |
| 2976 |
|
| 2977 |
|
| 2978 |
void SubstitutionGoal::tryNext() |
| 2979 |
{ |
| 2980 |
trace("trying substituter"); |
| 2981 |
|
| 2982 |
SubstitutablePathInfos infos; |
| 2983 |
PathSet dummy(singleton<PathSet>(storePath)); |
| 2984 |
worker.store.querySubstitutablePathInfos(dummy, infos); |
| 2985 |
SubstitutablePathInfos::iterator k = infos.find(storePath); |
| 2986 |
if (k == infos.end()) { |
| 2987 |
/* None left. Terminate this goal and let someone else deal |
| 2988 |
with it. */ |
| 2989 |
debug(format("path `%1%' is required, but there is no substituter that can build it") % storePath); |
| 2990 |
/* Hack: don't indicate failure if there were no substituters. |
| 2991 |
In that case the calling derivation should just do a |
| 2992 |
build. */ |
| 2993 |
amDone(ecNoSubstituters); |
| 2994 |
return; |
| 2995 |
} |
| 2996 |
|
| 2997 |
/* Found a substitute. */ |
| 2998 |
info = k->second; |
| 2999 |
|
| 3000 |
/* To maintain the closure invariant, we first have to realise the |
| 3001 |
paths referenced by this one. */ |
| 3002 |
foreach (PathSet::iterator, i, info.references) |
| 3003 |
if (*i != storePath) /* ignore self-references */ |
| 3004 |
addWaitee(worker.makeSubstitutionGoal(*i)); |
| 3005 |
|
| 3006 |
if (waitees.empty()) /* to prevent hang (no wake-up event) */ |
| 3007 |
referencesValid(); |
| 3008 |
else |
| 3009 |
state = &SubstitutionGoal::referencesValid; |
| 3010 |
} |
| 3011 |
|
| 3012 |
|
| 3013 |
void SubstitutionGoal::referencesValid() |
| 3014 |
{ |
| 3015 |
trace("all references realised"); |
| 3016 |
|
| 3017 |
if (nrFailed > 0) { |
| 3018 |
debug(format("some references of path `%1%' could not be realised") % storePath); |
| 3019 |
amDone(nrNoSubstituters > 0 || nrIncompleteClosure > 0 ? ecIncompleteClosure : ecFailed); |
| 3020 |
return; |
| 3021 |
} |
| 3022 |
|
| 3023 |
foreach (PathSet::iterator, i, info.references) |
| 3024 |
if (*i != storePath) /* ignore self-references */ |
| 3025 |
assert(worker.store.isValidPath(*i)); |
| 3026 |
|
| 3027 |
state = &SubstitutionGoal::tryToRun; |
| 3028 |
worker.wakeUp(shared_from_this()); |
| 3029 |
} |
| 3030 |
|
| 3031 |
|
| 3032 |
void SubstitutionGoal::tryToRun() |
| 3033 |
{ |
| 3034 |
trace("trying to run"); |
| 3035 |
|
| 3036 |
/* Make sure that we are allowed to start a build. Note that even |
| 3037 |
is maxBuildJobs == 0 (no local builds allowed), we still allow |
| 3038 |
a substituter to run. This is because substitutions cannot be |
| 3039 |
distributed to another machine via the build hook. */ |
| 3040 |
if (worker.getNrLocalBuilds() >= (settings.maxBuildJobs == 0 ? 1 : settings.maxBuildJobs)) { |
| 3041 |
worker.waitForBuildSlot(shared_from_this()); |
| 3042 |
return; |
| 3043 |
} |
| 3044 |
|
| 3045 |
/* Maybe a derivation goal has already locked this path |
| 3046 |
(exceedingly unlikely, since it should have used a substitute |
| 3047 |
first, but let's be defensive). */ |
| 3048 |
outputLock.reset(); // make sure this goal's lock is gone |
| 3049 |
if (pathIsLockedByMe(storePath)) { |
| 3050 |
debug(format("restarting substitution of `%1%' because it's locked by another goal") |
| 3051 |
% storePath); |
| 3052 |
worker.waitForAnyGoal(shared_from_this()); |
| 3053 |
return; /* restart in the tryToRun() state when another goal finishes */ |
| 3054 |
} |
| 3055 |
|
| 3056 |
/* Acquire a lock on the output path. */ |
| 3057 |
outputLock = std::shared_ptr<PathLocks>(new PathLocks); |
| 3058 |
if (!outputLock->lockPaths(singleton<PathSet>(storePath), "", false)) { |
| 3059 |
worker.waitForAWhile(shared_from_this()); |
| 3060 |
return; |
| 3061 |
} |
| 3062 |
|
| 3063 |
/* Check again whether the path is invalid. */ |
| 3064 |
if (!repair && worker.store.isValidPath(storePath)) { |
| 3065 |
debug(format("store path `%1%' has become valid") % storePath); |
| 3066 |
outputLock->setDeletion(true); |
| 3067 |
amDone(ecSuccess); |
| 3068 |
return; |
| 3069 |
} |
| 3070 |
|
| 3071 |
printMsg(lvlInfo, format("fetching path `%1%'...") % storePath); |
| 3072 |
|
| 3073 |
destPath = repair ? storePath + ".tmp" : storePath; |
| 3074 |
|
| 3075 |
/* Remove the (stale) output path if it exists. */ |
| 3076 |
if (pathExists(destPath)) |
| 3077 |
deletePath(destPath); |
| 3078 |
|
| 3079 |
if (!worker.substituter) { |
| 3080 |
const Strings args = { "substitute", "--substitute" }; |
| 3081 |
const std::map<string, string> env = { |
| 3082 |
{ "_NIX_OPTIONS", |
| 3083 |
settings.pack() + "deduplicate=" |
| 3084 |
+ (settings.autoOptimiseStore ? "yes" : "no") |
| 3085 |
} |
| 3086 |
}; |
| 3087 |
worker.substituter = std::make_shared<Agent>(settings.guixProgram, args, env); |
| 3088 |
} |
| 3089 |
|
| 3090 |
/* Borrow the worker's substituter. */ |
| 3091 |
if (!substituter) substituter.swap(worker.substituter); |
| 3092 |
|
| 3093 |
/* Send the request to the substituter. */ |
| 3094 |
writeLine(substituter->toAgent.writeSide, |
| 3095 |
(format("substitute %1% %2%") % storePath % destPath).str()); |
| 3096 |
|
| 3097 |
set<int> fds; |
| 3098 |
fds.insert(substituter->fromAgent.readSide); |
| 3099 |
fds.insert(substituter->builderOut.readSide); |
| 3100 |
worker.childStarted(shared_from_this(), substituter->pid, fds, true, true); |
| 3101 |
|
| 3102 |
state = &SubstitutionGoal::finished; |
| 3103 |
|
| 3104 |
if (settings.printBuildTrace) |
| 3105 |
/* The second element in the message used to be the name of the |
| 3106 |
substituter but we're left with only one. */ |
| 3107 |
printMsg(lvlError, format("@ substituter-started %1% substitute") % storePath); |
| 3108 |
} |
| 3109 |
|
| 3110 |
|
| 3111 |
void SubstitutionGoal::finished() |
| 3112 |
{ |
| 3113 |
trace("substitute finished"); |
| 3114 |
|
| 3115 |
/* Remove the 'guix substitute' process from the list of children. */ |
| 3116 |
worker.childTerminated(substituter->pid); |
| 3117 |
|
| 3118 |
/* If max-jobs > 1, the worker might have created a new 'substitute' |
| 3119 |
process in the meantime. If that is the case, terminate ours; |
| 3120 |
otherwise, give it back to the worker. */ |
| 3121 |
if (worker.substituter) { |
| 3122 |
substituter.reset (); |
| 3123 |
} else { |
| 3124 |
worker.substituter.swap(substituter); |
| 3125 |
} |
| 3126 |
|
| 3127 |
/* Check the exit status and the build result. */ |
| 3128 |
HashResult hash; |
| 3129 |
try { |
| 3130 |
auto statusList = tokenizeString<vector<string> >(status); |
| 3131 |
|
| 3132 |
if (statusList.empty()) { |
| 3133 |
throw SubstError(format("fetching path `%1%' (empty status: '%2%')") |
| 3134 |
% storePath % status); |
| 3135 |
} else if (statusList[0] == "hash-mismatch") { |
| 3136 |
if (settings.printBuildTrace) { |
| 3137 |
auto hashType = statusList[1]; |
| 3138 |
auto expectedHash = statusList[2]; |
| 3139 |
auto actualHash = statusList[3]; |
| 3140 |
printMsg(lvlError, format("@ hash-mismatch %1% %2% %3% %4%") |
| 3141 |
% storePath |
| 3142 |
% hashType % expectedHash % actualHash); |
| 3143 |
} |
| 3144 |
throw SubstError(format("hash mismatch for substituted item `%1%'") % storePath); |
| 3145 |
} else if (statusList[0] == "success") { |
| 3146 |
if (!pathExists(destPath)) |
| 3147 |
throw SubstError(format("substitute did not produce path `%1%'") % destPath); |
| 3148 |
|
| 3149 |
std::string hashStr = statusList[1]; |
| 3150 |
size_t n = hashStr.find(':'); |
| 3151 |
if (n == string::npos) |
| 3152 |
throw Error(format("bad hash from substituter: %1%") % hashStr); |
| 3153 |
|
| 3154 |
HashType hashType = parseHashType(string(hashStr, 0, n)); |
| 3155 |
switch (hashType) { |
| 3156 |
case htUnknown: |
| 3157 |
throw Error(format("unknown hash algorithm in `%1%'") % hashStr); |
| 3158 |
case htSHA256: |
| 3159 |
hash.first = parseHash16or32(hashType, string(hashStr, n + 1)); |
| 3160 |
if (!string2Int(statusList[2], hash.second)) |
| 3161 |
throw Error(format("invalid nar size for '%1%' substitute") % storePath); |
| 3162 |
break; |
| 3163 |
default: |
| 3164 |
/* The database only stores SHA256 hashes, so compute it. */ |
| 3165 |
hash = hashPath(htSHA256, destPath); |
| 3166 |
break; |
| 3167 |
} |
| 3168 |
} |
| 3169 |
else |
| 3170 |
throw SubstError(format("fetching path `%1%' (status: '%2%')") |
| 3171 |
% storePath % status); |
| 3172 |
|
| 3173 |
} catch (SubstError & e) { |
| 3174 |
|
| 3175 |
printMsg(lvlInfo, e.msg()); |
| 3176 |
|
| 3177 |
if (settings.printBuildTrace) { |
| 3178 |
printMsg(lvlError, format("@ substituter-failed %1% %2% %3%") |
| 3179 |
% storePath % status % e.msg()); |
| 3180 |
} |
| 3181 |
|
| 3182 |
amDone(ecFailed); |
| 3183 |
return; |
| 3184 |
} |
| 3185 |
|
| 3186 |
if (repair) replaceValidPath(storePath, destPath); |
| 3187 |
|
| 3188 |
/* Note: 'guix substitute' takes care of resetting timestamps and of |
| 3189 |
deduplicating 'destPath', so no need to do it here. */ |
| 3190 |
|
| 3191 |
ValidPathInfo info2; |
| 3192 |
info2.path = storePath; |
| 3193 |
info2.hash = hash.first; |
| 3194 |
info2.narSize = hash.second; |
| 3195 |
info2.references = info.references; |
| 3196 |
info2.deriver = info.deriver; |
| 3197 |
worker.store.registerValidPath(info2); |
| 3198 |
|
| 3199 |
outputLock->setDeletion(true); |
| 3200 |
outputLock.reset(); |
| 3201 |
|
| 3202 |
worker.store.markContentsGood(storePath); |
| 3203 |
|
| 3204 |
printMsg(lvlChatty, |
| 3205 |
format("substitution of path `%1%' succeeded") % storePath); |
| 3206 |
|
| 3207 |
if (settings.printBuildTrace) |
| 3208 |
printMsg(lvlError, format("@ substituter-succeeded %1%") % storePath); |
| 3209 |
|
| 3210 |
amDone(ecSuccess); |
| 3211 |
} |
| 3212 |
|
| 3213 |
|
| 3214 |
void SubstitutionGoal::handleChildOutput(int fd, const string & data) |
| 3215 |
{ |
| 3216 |
if (verbosity >= settings.buildVerbosity |
| 3217 |
&& fd == substituter->fromAgent.readSide) { |
| 3218 |
writeToStderr(data); |
| 3219 |
/* Don't write substitution output to a log file for now. We |
| 3220 |
probably should, though. */ |
| 3221 |
} |
| 3222 |
|
| 3223 |
if (fd == substituter->builderOut.readSide) { |
| 3224 |
/* DATA may consist of several lines. Process them one by one. */ |
| 3225 |
string input = data; |
| 3226 |
while (!input.empty()) { |
| 3227 |
/* Process up to the first newline. */ |
| 3228 |
size_t end = input.find_first_of("\n"); |
| 3229 |
string trimmed = (end != string::npos) ? input.substr(0, end) : input; |
| 3230 |
|
| 3231 |
/* Update the goal's state accordingly. */ |
| 3232 |
if (status == "") { |
| 3233 |
status = trimmed; |
| 3234 |
worker.wakeUp(shared_from_this()); |
| 3235 |
} else { |
| 3236 |
printMsg(lvlError, format("unexpected substituter message '%1%'") % input); |
| 3237 |
} |
| 3238 |
|
| 3239 |
input = (end != string::npos) ? input.substr(end + 1) : ""; |
| 3240 |
} |
| 3241 |
} |
| 3242 |
} |
| 3243 |
|
| 3244 |
|
| 3245 |
void SubstitutionGoal::handleEOF(int fd) |
| 3246 |
{ |
| 3247 |
worker.wakeUp(shared_from_this()); |
| 3248 |
} |
| 3249 |
|
| 3250 |
|
| 3251 |
|
| 3252 |
////////////////////////////////////////////////////////////////////// |
| 3253 |
|
| 3254 |
|
| 3255 |
static bool working = false; |
| 3256 |
|
| 3257 |
|
| 3258 |
Worker::Worker(LocalStore & store) |
| 3259 |
: store(store) |
| 3260 |
{ |
| 3261 |
/* Debugging: prevent recursive workers. */ |
| 3262 |
if (working) abort(); |
| 3263 |
working = true; |
| 3264 |
nrLocalBuilds = 0; |
| 3265 |
lastWokenUp = 0; |
| 3266 |
permanentFailure = false; |
| 3267 |
timedOut = false; |
| 3268 |
} |
| 3269 |
|
| 3270 |
|
| 3271 |
Worker::~Worker() |
| 3272 |
{ |
| 3273 |
working = false; |
| 3274 |
|
| 3275 |
/* Explicitly get rid of all strong pointers now. After this all |
| 3276 |
goals that refer to this worker should be gone. (Otherwise we |
| 3277 |
are in trouble, since goals may call childTerminated() etc. in |
| 3278 |
their destructors). */ |
| 3279 |
topGoals.clear(); |
| 3280 |
} |
| 3281 |
|
| 3282 |
|
| 3283 |
GoalPtr Worker::makeDerivationGoal(const Path & path, |
| 3284 |
const StringSet & wantedOutputs, BuildMode buildMode) |
| 3285 |
{ |
| 3286 |
GoalPtr goal = derivationGoals[path].lock(); |
| 3287 |
if (!goal) { |
| 3288 |
goal = GoalPtr(new DerivationGoal(path, wantedOutputs, *this, buildMode)); |
| 3289 |
derivationGoals[path] = goal; |
| 3290 |
wakeUp(goal); |
| 3291 |
} else |
| 3292 |
(dynamic_cast<DerivationGoal *>(goal.get()))->addWantedOutputs(wantedOutputs); |
| 3293 |
return goal; |
| 3294 |
} |
| 3295 |
|
| 3296 |
|
| 3297 |
GoalPtr Worker::makeSubstitutionGoal(const Path & path, bool repair) |
| 3298 |
{ |
| 3299 |
GoalPtr goal = substitutionGoals[path].lock(); |
| 3300 |
if (!goal) { |
| 3301 |
goal = GoalPtr(new SubstitutionGoal(path, *this, repair)); |
| 3302 |
substitutionGoals[path] = goal; |
| 3303 |
wakeUp(goal); |
| 3304 |
} |
| 3305 |
return goal; |
| 3306 |
} |
| 3307 |
|
| 3308 |
|
| 3309 |
static void removeGoal(GoalPtr goal, WeakGoalMap & goalMap) |
| 3310 |
{ |
| 3311 |
/* !!! inefficient */ |
| 3312 |
for (WeakGoalMap::iterator i = goalMap.begin(); |
| 3313 |
i != goalMap.end(); ) |
| 3314 |
if (i->second.lock() == goal) { |
| 3315 |
WeakGoalMap::iterator j = i; ++j; |
| 3316 |
goalMap.erase(i); |
| 3317 |
i = j; |
| 3318 |
} |
| 3319 |
else ++i; |
| 3320 |
} |
| 3321 |
|
| 3322 |
|
| 3323 |
void Worker::removeGoal(GoalPtr goal) |
| 3324 |
{ |
| 3325 |
nix::removeGoal(goal, derivationGoals); |
| 3326 |
nix::removeGoal(goal, substitutionGoals); |
| 3327 |
if (topGoals.find(goal) != topGoals.end()) { |
| 3328 |
topGoals.erase(goal); |
| 3329 |
/* If a top-level goal failed, then kill all other goals |
| 3330 |
(unless keepGoing was set). */ |
| 3331 |
if (goal->getExitCode() == Goal::ecFailed && !settings.keepGoing) |
| 3332 |
topGoals.clear(); |
| 3333 |
} |
| 3334 |
|
| 3335 |
/* Wake up goals waiting for any goal to finish. */ |
| 3336 |
foreach (WeakGoals::iterator, i, waitingForAnyGoal) { |
| 3337 |
GoalPtr goal = i->lock(); |
| 3338 |
if (goal) wakeUp(goal); |
| 3339 |
} |
| 3340 |
|
| 3341 |
waitingForAnyGoal.clear(); |
| 3342 |
} |
| 3343 |
|
| 3344 |
|
| 3345 |
void Worker::wakeUp(GoalPtr goal) |
| 3346 |
{ |
| 3347 |
goal->trace("woken up"); |
| 3348 |
addToWeakGoals(awake, goal); |
| 3349 |
} |
| 3350 |
|
| 3351 |
|
| 3352 |
unsigned Worker::getNrLocalBuilds() |
| 3353 |
{ |
| 3354 |
return nrLocalBuilds; |
| 3355 |
} |
| 3356 |
|
| 3357 |
|
| 3358 |
void Worker::childStarted(GoalPtr goal, |
| 3359 |
pid_t pid, const set<int> & fds, bool inBuildSlot, |
| 3360 |
bool respectTimeouts) |
| 3361 |
{ |
| 3362 |
Child child; |
| 3363 |
child.goal = goal; |
| 3364 |
child.fds = fds; |
| 3365 |
child.timeStarted = child.lastOutput = time(0); |
| 3366 |
child.inBuildSlot = inBuildSlot; |
| 3367 |
child.respectTimeouts = respectTimeouts; |
| 3368 |
children[pid] = child; |
| 3369 |
if (inBuildSlot) nrLocalBuilds++; |
| 3370 |
} |
| 3371 |
|
| 3372 |
|
| 3373 |
void Worker::childTerminated(pid_t pid, bool wakeSleepers) |
| 3374 |
{ |
| 3375 |
assert(pid != -1); /* common mistake */ |
| 3376 |
|
| 3377 |
Children::iterator i = children.find(pid); |
| 3378 |
assert(i != children.end()); |
| 3379 |
|
| 3380 |
if (i->second.inBuildSlot) { |
| 3381 |
assert(nrLocalBuilds > 0); |
| 3382 |
nrLocalBuilds--; |
| 3383 |
} |
| 3384 |
|
| 3385 |
children.erase(pid); |
| 3386 |
|
| 3387 |
if (wakeSleepers) { |
| 3388 |
|
| 3389 |
/* Wake up goals waiting for a build slot. */ |
| 3390 |
foreach (WeakGoals::iterator, i, wantingToBuild) { |
| 3391 |
GoalPtr goal = i->lock(); |
| 3392 |
if (goal) wakeUp(goal); |
| 3393 |
} |
| 3394 |
|
| 3395 |
wantingToBuild.clear(); |
| 3396 |
} |
| 3397 |
} |
| 3398 |
|
| 3399 |
|
| 3400 |
void Worker::waitForBuildSlot(GoalPtr goal) |
| 3401 |
{ |
| 3402 |
debug("wait for build slot"); |
| 3403 |
if (getNrLocalBuilds() < settings.maxBuildJobs) |
| 3404 |
wakeUp(goal); /* we can do it right away */ |
| 3405 |
else |
| 3406 |
addToWeakGoals(wantingToBuild, goal); |
| 3407 |
} |
| 3408 |
|
| 3409 |
|
| 3410 |
void Worker::waitForAnyGoal(GoalPtr goal) |
| 3411 |
{ |
| 3412 |
debug("wait for any goal"); |
| 3413 |
addToWeakGoals(waitingForAnyGoal, goal); |
| 3414 |
} |
| 3415 |
|
| 3416 |
|
| 3417 |
void Worker::waitForAWhile(GoalPtr goal) |
| 3418 |
{ |
| 3419 |
debug("wait for a while"); |
| 3420 |
addToWeakGoals(waitingForAWhile, goal); |
| 3421 |
} |
| 3422 |
|
| 3423 |
|
| 3424 |
void Worker::run(const Goals & _topGoals) |
| 3425 |
{ |
| 3426 |
foreach (Goals::iterator, i, _topGoals) topGoals.insert(*i); |
| 3427 |
|
| 3428 |
startNest(nest, lvlDebug, format("entered goal loop")); |
| 3429 |
|
| 3430 |
while (1) { |
| 3431 |
|
| 3432 |
checkInterrupt(); |
| 3433 |
|
| 3434 |
/* Call every wake goal (in the ordering established by |
| 3435 |
CompareGoalPtrs). */ |
| 3436 |
while (!awake.empty() && !topGoals.empty()) { |
| 3437 |
Goals awake2; |
| 3438 |
for (auto & i : awake) { |
| 3439 |
GoalPtr goal = i.lock(); |
| 3440 |
if (goal) awake2.insert(goal); |
| 3441 |
} |
| 3442 |
awake.clear(); |
| 3443 |
for (auto & goal : awake2) { |
| 3444 |
checkInterrupt(); |
| 3445 |
goal->work(); |
| 3446 |
if (topGoals.empty()) break; // stuff may have been cancelled |
| 3447 |
} |
| 3448 |
} |
| 3449 |
|
| 3450 |
if (topGoals.empty()) break; |
| 3451 |
|
| 3452 |
/* Wait for input. */ |
| 3453 |
if (!children.empty() || !waitingForAWhile.empty()) |
| 3454 |
waitForInput(); |
| 3455 |
else { |
| 3456 |
if (awake.empty() && settings.maxBuildJobs == 0) throw Error( |
| 3457 |
"unable to start any build; either increase `--max-jobs' " |
| 3458 |
"or enable distributed builds"); |
| 3459 |
assert(!awake.empty()); |
| 3460 |
} |
| 3461 |
} |
| 3462 |
|
| 3463 |
/* If --keep-going is not set, it's possible that the main goal |
| 3464 |
exited while some of its subgoals were still active. But if |
| 3465 |
--keep-going *is* set, then they must all be finished now. */ |
| 3466 |
assert(!settings.keepGoing || awake.empty()); |
| 3467 |
assert(!settings.keepGoing || wantingToBuild.empty()); |
| 3468 |
assert(!settings.keepGoing || children.empty()); |
| 3469 |
} |
| 3470 |
|
| 3471 |
|
| 3472 |
void Worker::waitForInput() |
| 3473 |
{ |
| 3474 |
printMsg(lvlVomit, "waiting for children"); |
| 3475 |
|
| 3476 |
/* Process output from the file descriptors attached to the |
| 3477 |
children, namely log output and output path creation commands. |
| 3478 |
We also use this to detect child termination: if we get EOF on |
| 3479 |
the logger pipe of a build, we assume that the builder has |
| 3480 |
terminated. */ |
| 3481 |
|
| 3482 |
bool useTimeout = false; |
| 3483 |
struct timeval timeout; |
| 3484 |
timeout.tv_usec = 0; |
| 3485 |
time_t before = time(0); |
| 3486 |
|
| 3487 |
/* If we're monitoring for silence on stdout/stderr, or if there |
| 3488 |
is a build timeout, then wait for input until the first |
| 3489 |
deadline for any child. */ |
| 3490 |
assert(sizeof(time_t) >= sizeof(long)); |
| 3491 |
time_t nearest = LONG_MAX; // nearest deadline |
| 3492 |
foreach (Children::iterator, i, children) { |
| 3493 |
if (!i->second.respectTimeouts) continue; |
| 3494 |
if (settings.maxSilentTime != 0) |
| 3495 |
nearest = std::min(nearest, i->second.lastOutput + settings.maxSilentTime); |
| 3496 |
if (settings.buildTimeout != 0) |
| 3497 |
nearest = std::min(nearest, i->second.timeStarted + settings.buildTimeout); |
| 3498 |
} |
| 3499 |
if (nearest != LONG_MAX) { |
| 3500 |
timeout.tv_sec = std::max((time_t) 1, nearest - before); |
| 3501 |
useTimeout = true; |
| 3502 |
printMsg(lvlVomit, format("sleeping %1% seconds") % timeout.tv_sec); |
| 3503 |
} |
| 3504 |
|
| 3505 |
/* If we are polling goals that are waiting for a lock, then wake |
| 3506 |
up after a few seconds at most. */ |
| 3507 |
if (!waitingForAWhile.empty()) { |
| 3508 |
useTimeout = true; |
| 3509 |
if (lastWokenUp == 0) |
| 3510 |
printMsg(lvlError, "waiting for locks or build slots..."); |
| 3511 |
if (lastWokenUp == 0 || lastWokenUp > before) lastWokenUp = before; |
| 3512 |
timeout.tv_sec = std::max((time_t) 1, (time_t) (lastWokenUp + settings.pollInterval - before)); |
| 3513 |
} else lastWokenUp = 0; |
| 3514 |
|
| 3515 |
using namespace std; |
| 3516 |
/* Use select() to wait for the input side of any logger pipe to |
| 3517 |
become `available'. Note that `available' (i.e., non-blocking) |
| 3518 |
includes EOF. */ |
| 3519 |
fd_set fds; |
| 3520 |
FD_ZERO(&fds); |
| 3521 |
int fdMax = 0; |
| 3522 |
foreach (Children::iterator, i, children) { |
| 3523 |
foreach (set<int>::iterator, j, i->second.fds) { |
| 3524 |
FD_SET(*j, &fds); |
| 3525 |
if (*j >= fdMax) fdMax = *j + 1; |
| 3526 |
} |
| 3527 |
} |
| 3528 |
|
| 3529 |
if (select(fdMax, &fds, 0, 0, useTimeout ? &timeout : 0) == -1) { |
| 3530 |
if (errno == EINTR) return; |
| 3531 |
throw SysError("waiting for input"); |
| 3532 |
} |
| 3533 |
|
| 3534 |
time_t after = time(0); |
| 3535 |
|
| 3536 |
/* Process all available file descriptors. */ |
| 3537 |
|
| 3538 |
/* Since goals may be canceled from inside the loop below (causing |
| 3539 |
them go be erased from the `children' map), we have to be |
| 3540 |
careful that we don't keep iterators alive across calls to |
| 3541 |
timedOut(). */ |
| 3542 |
set<pid_t> pids; |
| 3543 |
foreach (Children::iterator, i, children) pids.insert(i->first); |
| 3544 |
|
| 3545 |
foreach (set<pid_t>::iterator, i, pids) { |
| 3546 |
checkInterrupt(); |
| 3547 |
Children::iterator j = children.find(*i); |
| 3548 |
if (j == children.end()) continue; // child destroyed |
| 3549 |
GoalPtr goal = j->second.goal.lock(); |
| 3550 |
assert(goal); |
| 3551 |
|
| 3552 |
set<int> fds2(j->second.fds); |
| 3553 |
foreach (set<int>::iterator, k, fds2) { |
| 3554 |
if (FD_ISSET(*k, &fds)) { |
| 3555 |
unsigned char buffer[4096]; |
| 3556 |
ssize_t rd = read(*k, buffer, sizeof(buffer)); |
| 3557 |
if (rd == -1) { |
| 3558 |
if (errno != EINTR) |
| 3559 |
throw SysError(format("reading from %1%") |
| 3560 |
% goal->getName()); |
| 3561 |
} else if (rd == 0) { |
| 3562 |
debug(format("%1%: got EOF") % goal->getName()); |
| 3563 |
goal->handleEOF(*k); |
| 3564 |
j->second.fds.erase(*k); |
| 3565 |
} else { |
| 3566 |
printMsg(lvlVomit, format("%1%: read %2% bytes") |
| 3567 |
% goal->getName() % rd); |
| 3568 |
string data((char *) buffer, rd); |
| 3569 |
j->second.lastOutput = after; |
| 3570 |
goal->handleChildOutput(*k, data); |
| 3571 |
} |
| 3572 |
} |
| 3573 |
} |
| 3574 |
|
| 3575 |
if (goal->getExitCode() == Goal::ecBusy && |
| 3576 |
settings.maxSilentTime != 0 && |
| 3577 |
j->second.respectTimeouts && |
| 3578 |
after - j->second.lastOutput >= (time_t) settings.maxSilentTime) |
| 3579 |
{ |
| 3580 |
printMsg(lvlError, |
| 3581 |
format("%1% timed out after %2% seconds of silence") |
| 3582 |
% goal->getName() % settings.maxSilentTime); |
| 3583 |
goal->timedOut(); |
| 3584 |
} |
| 3585 |
|
| 3586 |
else if (goal->getExitCode() == Goal::ecBusy && |
| 3587 |
settings.buildTimeout != 0 && |
| 3588 |
j->second.respectTimeouts && |
| 3589 |
after - j->second.timeStarted >= (time_t) settings.buildTimeout) |
| 3590 |
{ |
| 3591 |
printMsg(lvlError, |
| 3592 |
format("%1% timed out after %2% seconds") |
| 3593 |
% goal->getName() % settings.buildTimeout); |
| 3594 |
goal->timedOut(); |
| 3595 |
} |
| 3596 |
} |
| 3597 |
|
| 3598 |
if (!waitingForAWhile.empty() && lastWokenUp + settings.pollInterval <= after) { |
| 3599 |
lastWokenUp = after; |
| 3600 |
foreach (WeakGoals::iterator, i, waitingForAWhile) { |
| 3601 |
GoalPtr goal = i->lock(); |
| 3602 |
if (goal) wakeUp(goal); |
| 3603 |
} |
| 3604 |
waitingForAWhile.clear(); |
| 3605 |
} |
| 3606 |
} |
| 3607 |
|
| 3608 |
|
| 3609 |
unsigned int Worker::exitStatus() |
| 3610 |
{ |
| 3611 |
return timedOut ? 101 : (permanentFailure ? 100 : 1); |
| 3612 |
} |
| 3613 |
|
| 3614 |
|
| 3615 |
////////////////////////////////////////////////////////////////////// |
| 3616 |
|
| 3617 |
|
| 3618 |
void LocalStore::buildPaths(const PathSet & drvPaths, BuildMode buildMode) |
| 3619 |
{ |
| 3620 |
startNest(nest, lvlDebug, |
| 3621 |
format("building %1%") % showPaths(drvPaths)); |
| 3622 |
|
| 3623 |
Worker worker(*this); |
| 3624 |
|
| 3625 |
Goals goals; |
| 3626 |
foreach (PathSet::const_iterator, i, drvPaths) { |
| 3627 |
DrvPathWithOutputs i2 = parseDrvPathWithOutputs(*i); |
| 3628 |
if (isDerivation(i2.first)) |
| 3629 |
goals.insert(worker.makeDerivationGoal(i2.first, i2.second, buildMode)); |
| 3630 |
else |
| 3631 |
goals.insert(worker.makeSubstitutionGoal(*i, buildMode)); |
| 3632 |
} |
| 3633 |
|
| 3634 |
worker.run(goals); |
| 3635 |
|
| 3636 |
PathSet failed; |
| 3637 |
foreach (Goals::iterator, i, goals) |
| 3638 |
if ((*i)->getExitCode() == Goal::ecFailed) { |
| 3639 |
DerivationGoal * i2 = dynamic_cast<DerivationGoal *>(i->get()); |
| 3640 |
if (i2) failed.insert(i2->getDrvPath()); |
| 3641 |
else failed.insert(dynamic_cast<SubstitutionGoal *>(i->get())->getStorePath()); |
| 3642 |
} |
| 3643 |
|
| 3644 |
if (!failed.empty()) |
| 3645 |
throw Error(format("build of %1% failed") % showPaths(failed), worker.exitStatus()); |
| 3646 |
} |
| 3647 |
|
| 3648 |
|
| 3649 |
void LocalStore::ensurePath(const Path & path) |
| 3650 |
{ |
| 3651 |
/* If the path is already valid, we're done. */ |
| 3652 |
if (isValidPath(path)) return; |
| 3653 |
|
| 3654 |
Worker worker(*this); |
| 3655 |
GoalPtr goal = worker.makeSubstitutionGoal(path); |
| 3656 |
Goals goals = singleton<Goals>(goal); |
| 3657 |
|
| 3658 |
worker.run(goals); |
| 3659 |
|
| 3660 |
if (goal->getExitCode() != Goal::ecSuccess) |
| 3661 |
throw Error(format("path `%1%' does not exist and cannot be created") % path, worker.exitStatus()); |
| 3662 |
} |
| 3663 |
|
| 3664 |
|
| 3665 |
void LocalStore::repairPath(const Path & path) |
| 3666 |
{ |
| 3667 |
Worker worker(*this); |
| 3668 |
GoalPtr goal = worker.makeSubstitutionGoal(path, true); |
| 3669 |
Goals goals = singleton<Goals>(goal); |
| 3670 |
|
| 3671 |
worker.run(goals); |
| 3672 |
|
| 3673 |
if (goal->getExitCode() != Goal::ecSuccess) { |
| 3674 |
/* Since substituting the path didn't work, if we have a valid |
| 3675 |
deriver, then rebuild the deriver. */ |
| 3676 |
Path deriver = queryDeriver(path); |
| 3677 |
if (deriver != "" && isValidPath(deriver)) { |
| 3678 |
goals.clear(); |
| 3679 |
goals.insert(worker.makeDerivationGoal(deriver, StringSet(), bmRepair)); |
| 3680 |
worker.run(goals); |
| 3681 |
} else |
| 3682 |
throw Error(format("cannot repair path `%1%'") % path, worker.exitStatus()); |
| 3683 |
} |
| 3684 |
} |
| 3685 |
|
| 3686 |
|
| 3687 |
} |