You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

3563 lines
115 KiB

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