nix-daemon.cc 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049
  1. #include "config.h"
  2. #include "shared.hh"
  3. #include "local-store.hh"
  4. #include "util.hh"
  5. #include "serialise.hh"
  6. #include "worker-protocol.hh"
  7. #include "archive.hh"
  8. #include "affinity.hh"
  9. #include "globals.hh"
  10. #include "builtins.hh"
  11. #include <algorithm>
  12. #include <cstring>
  13. #include <unistd.h>
  14. #include <signal.h>
  15. #include <sys/types.h>
  16. #include <sys/wait.h>
  17. #include <sys/stat.h>
  18. #include <sys/socket.h>
  19. #include <sys/un.h>
  20. #include <arpa/inet.h>
  21. #include <netinet/in.h>
  22. #include <netinet/tcp.h>
  23. #include <fcntl.h>
  24. #include <errno.h>
  25. #include <pwd.h>
  26. #include <grp.h>
  27. using namespace nix;
  28. /* On platforms that have O_ASYNC, we can detect when a client
  29. disconnects and immediately kill any ongoing builds. On platforms
  30. that lack it, we only notice the disconnection the next time we try
  31. to write to the client. So if you have a builder that never
  32. generates output on stdout/stderr, the daemon will never notice
  33. that the client has disconnected until the builder terminates.
  34. GNU/Hurd does have O_ASYNC, but its Unix-domain socket translator
  35. (pflocal) does not implement F_SETOWN. See
  36. <http://lists.gnu.org/archive/html/bug-guix/2013-07/msg00021.html> for
  37. details.*/
  38. #if defined O_ASYNC && !defined __GNU__
  39. #define HAVE_HUP_NOTIFICATION
  40. #ifndef SIGPOLL
  41. #define SIGPOLL SIGIO
  42. #endif
  43. #endif
  44. static FdSource from(STDIN_FILENO);
  45. static FdSink to(STDOUT_FILENO);
  46. bool canSendStderr;
  47. /* This variable is used to keep track of whether a connection
  48. comes from a host other than the host running guix-daemon. */
  49. static bool isRemoteConnection;
  50. /* This function is called anytime we want to write something to
  51. stderr. If we're in a state where the protocol allows it (i.e.,
  52. when canSendStderr), send the message to the client over the
  53. socket. */
  54. static void tunnelStderr(const unsigned char * buf, size_t count)
  55. {
  56. if (canSendStderr) {
  57. try {
  58. writeInt(STDERR_NEXT, to);
  59. writeString(buf, count, to);
  60. to.flush();
  61. } catch (...) {
  62. /* Write failed; that means that the other side is
  63. gone. */
  64. canSendStderr = false;
  65. throw;
  66. }
  67. } else
  68. writeFull(STDERR_FILENO, buf, count);
  69. }
  70. /* Return true if the remote side has closed its end of the
  71. connection, false otherwise. Should not be called on any socket on
  72. which we expect input! */
  73. static bool isFarSideClosed(int socket)
  74. {
  75. struct timeval timeout;
  76. timeout.tv_sec = timeout.tv_usec = 0;
  77. fd_set fds;
  78. FD_ZERO(&fds);
  79. FD_SET(socket, &fds);
  80. while (select(socket + 1, &fds, 0, 0, &timeout) == -1)
  81. if (errno != EINTR) throw SysError("select()");
  82. if (!FD_ISSET(socket, &fds)) return false;
  83. /* Destructive read to determine whether the select() marked the
  84. socket as readable because there is actual input or because
  85. we've reached EOF (i.e., a read of size 0 is available). */
  86. char c;
  87. int rd;
  88. if ((rd = read(socket, &c, 1)) > 0)
  89. throw Error("EOF expected (protocol error?)");
  90. else if (rd == -1 && errno != ECONNRESET)
  91. throw SysError("expected connection reset or EOF");
  92. return true;
  93. }
  94. /* A SIGPOLL signal is received when data is available on the client
  95. communication socket, or when the client has closed its side of the
  96. socket. This handler is enabled at precisely those moments in the
  97. protocol when we're doing work and the client is supposed to be
  98. quiet. Thus, if we get a SIGPOLL signal, it means that the client
  99. has quit. So we should quit as well.
  100. Too bad most operating systems don't support the POLL_HUP value for
  101. si_code in siginfo_t. That would make most of the SIGPOLL
  102. complexity unnecessary, i.e., we could just enable SIGPOLL all the
  103. time and wouldn't have to worry about races. */
  104. static void sigPollHandler(int sigNo)
  105. {
  106. using namespace std;
  107. try {
  108. /* Check that the far side actually closed. We're still
  109. getting spurious signals every once in a while. I.e.,
  110. there is no input available, but we get a signal with
  111. POLL_IN set. Maybe it's delayed or something. */
  112. if (isFarSideClosed(from.fd)) {
  113. if (!blockInt) {
  114. _isInterrupted = 1;
  115. blockInt = 1;
  116. canSendStderr = false;
  117. const char * s = "SIGPOLL\n";
  118. write(STDERR_FILENO, s, strlen(s));
  119. }
  120. } else {
  121. const char * s = "spurious SIGPOLL\n";
  122. write(STDERR_FILENO, s, strlen(s));
  123. }
  124. }
  125. catch (Error & e) {
  126. /* Shouldn't happen. */
  127. string s = "impossible: " + e.msg() + '\n';
  128. write(STDERR_FILENO, s.data(), s.size());
  129. throw;
  130. }
  131. }
  132. static void setSigPollAction(bool enable)
  133. {
  134. #ifdef HAVE_HUP_NOTIFICATION
  135. struct sigaction act, oact;
  136. act.sa_handler = enable ? sigPollHandler : SIG_IGN;
  137. sigfillset(&act.sa_mask);
  138. act.sa_flags = 0;
  139. if (sigaction(SIGPOLL, &act, &oact))
  140. throw SysError("setting handler for SIGPOLL");
  141. #endif
  142. }
  143. /* startWork() means that we're starting an operation for which we
  144. want to send out stderr to the client. */
  145. static void startWork()
  146. {
  147. canSendStderr = true;
  148. /* Handle client death asynchronously. */
  149. setSigPollAction(true);
  150. /* Of course, there is a race condition here: the socket could
  151. have closed between when we last read from / wrote to it, and
  152. between the time we set the handler for SIGPOLL. In that case
  153. we won't get the signal. So do a non-blocking select() to find
  154. out if any input is available on the socket. If there is, it
  155. has to be the 0-byte read that indicates that the socket has
  156. closed. */
  157. if (isFarSideClosed(from.fd)) {
  158. _isInterrupted = 1;
  159. checkInterrupt();
  160. }
  161. }
  162. /* stopWork() means that we're done; stop sending stderr to the
  163. client. */
  164. static void stopWork(bool success = true, const string & msg = "", unsigned int status = 0)
  165. {
  166. /* Stop handling async client death; we're going to a state where
  167. we're either sending or receiving from the client, so we'll be
  168. notified of client death anyway. */
  169. setSigPollAction(false);
  170. canSendStderr = false;
  171. if (success)
  172. writeInt(STDERR_LAST, to);
  173. else {
  174. writeInt(STDERR_ERROR, to);
  175. writeString(msg, to);
  176. if (status != 0) writeInt(status, to);
  177. }
  178. }
  179. struct TunnelSink : BufferedSink
  180. {
  181. Sink & to;
  182. TunnelSink(Sink & to) : BufferedSink(64 * 1024), to(to) { }
  183. virtual void write(const unsigned char * data, size_t len)
  184. {
  185. writeInt(STDERR_WRITE, to);
  186. writeString(data, len, to);
  187. }
  188. };
  189. struct TunnelSource : BufferedSource
  190. {
  191. Source & from;
  192. TunnelSource(Source & from) : from(from) { }
  193. size_t readUnbuffered(unsigned char * data, size_t len)
  194. {
  195. /* Careful: we're going to receive data from the client now,
  196. so we have to disable the SIGPOLL handler. */
  197. setSigPollAction(false);
  198. canSendStderr = false;
  199. writeInt(STDERR_READ, to);
  200. writeInt(len, to);
  201. to.flush();
  202. size_t n = readString(data, len, from);
  203. startWork();
  204. if (n == 0) throw EndOfFile("unexpected end-of-file");
  205. return n;
  206. }
  207. };
  208. /* If the NAR archive contains a single file at top-level, then save
  209. the contents of the file to `s'. Otherwise barf. */
  210. struct RetrieveRegularNARSink : ParseSink
  211. {
  212. bool regular;
  213. string s;
  214. RetrieveRegularNARSink() : regular(true) { }
  215. void createDirectory(const Path & path)
  216. {
  217. regular = false;
  218. }
  219. void receiveContents(unsigned char * data, unsigned int len)
  220. {
  221. s.append((const char *) data, len);
  222. }
  223. void createSymlink(const Path & path, const string & target)
  224. {
  225. regular = false;
  226. }
  227. };
  228. /* Adapter class of a Source that saves all data read to `s'. */
  229. struct SavingSourceAdapter : Source
  230. {
  231. Source & orig;
  232. string s;
  233. SavingSourceAdapter(Source & orig) : orig(orig) { }
  234. size_t read(unsigned char * data, size_t len)
  235. {
  236. size_t n = orig.read(data, len);
  237. s.append((const char *) data, n);
  238. return n;
  239. }
  240. };
  241. static void performOp(bool trusted, unsigned int clientVersion,
  242. Source & from, Sink & to, unsigned int op)
  243. {
  244. switch (op) {
  245. case wopIsValidPath: {
  246. /* 'readStorePath' could raise an error leading to the connection
  247. being closed. To be able to recover from an invalid path error,
  248. call 'startWork' early, and do 'assertStorePath' afterwards so
  249. that the 'Error' exception handler doesn't close the
  250. connection. */
  251. Path path = readString(from);
  252. startWork();
  253. assertStorePath(path);
  254. bool result = store->isValidPath(path);
  255. stopWork();
  256. writeInt(result, to);
  257. break;
  258. }
  259. case wopQueryValidPaths: {
  260. PathSet paths = readStorePaths<PathSet>(from);
  261. startWork();
  262. PathSet res = store->queryValidPaths(paths);
  263. stopWork();
  264. writeStrings(res, to);
  265. break;
  266. }
  267. case wopHasSubstitutes: {
  268. Path path = readStorePath(from);
  269. startWork();
  270. PathSet res = store->querySubstitutablePaths(singleton<PathSet>(path));
  271. stopWork();
  272. writeInt(res.find(path) != res.end(), to);
  273. break;
  274. }
  275. case wopQuerySubstitutablePaths: {
  276. PathSet paths = readStorePaths<PathSet>(from);
  277. startWork();
  278. PathSet res = store->querySubstitutablePaths(paths);
  279. stopWork();
  280. writeStrings(res, to);
  281. break;
  282. }
  283. case wopQueryPathHash: {
  284. Path path = readStorePath(from);
  285. startWork();
  286. Hash hash = store->queryPathHash(path);
  287. stopWork();
  288. writeString(printHash(hash), to);
  289. break;
  290. }
  291. case wopQueryReferences:
  292. case wopQueryReferrers:
  293. case wopQueryValidDerivers:
  294. case wopQueryDerivationOutputs: {
  295. Path path = readStorePath(from);
  296. startWork();
  297. PathSet paths;
  298. if (op == wopQueryReferences)
  299. store->queryReferences(path, paths);
  300. else if (op == wopQueryReferrers)
  301. store->queryReferrers(path, paths);
  302. else if (op == wopQueryValidDerivers)
  303. paths = store->queryValidDerivers(path);
  304. else paths = store->queryDerivationOutputs(path);
  305. stopWork();
  306. writeStrings(paths, to);
  307. break;
  308. }
  309. case wopQueryDerivationOutputNames: {
  310. Path path = readStorePath(from);
  311. startWork();
  312. StringSet names;
  313. names = store->queryDerivationOutputNames(path);
  314. stopWork();
  315. writeStrings(names, to);
  316. break;
  317. }
  318. case wopQueryDeriver: {
  319. Path path = readStorePath(from);
  320. startWork();
  321. Path deriver = store->queryDeriver(path);
  322. stopWork();
  323. writeString(deriver, to);
  324. break;
  325. }
  326. case wopQueryPathFromHashPart: {
  327. string hashPart = readString(from);
  328. startWork();
  329. Path path = store->queryPathFromHashPart(hashPart);
  330. stopWork();
  331. writeString(path, to);
  332. break;
  333. }
  334. case wopAddToStore: {
  335. string baseName = readString(from);
  336. bool fixed = readInt(from) == 1; /* obsolete */
  337. bool recursive = readInt(from) == 1;
  338. string s = readString(from);
  339. /* Compatibility hack. */
  340. if (!fixed) {
  341. s = "sha256";
  342. recursive = true;
  343. }
  344. HashType hashAlgo = parseHashType(s);
  345. SavingSourceAdapter savedNAR(from);
  346. RetrieveRegularNARSink savedRegular;
  347. if (recursive) {
  348. /* Get the entire NAR dump from the client and save it to
  349. a string so that we can pass it to
  350. addToStoreFromDump(). */
  351. ParseSink sink; /* null sink; just parse the NAR */
  352. parseDump(sink, savedNAR);
  353. } else
  354. parseDump(savedRegular, from);
  355. startWork();
  356. if (!savedRegular.regular) throw Error("regular file expected");
  357. Path path = dynamic_cast<LocalStore *>(store.get())
  358. ->addToStoreFromDump(recursive ? savedNAR.s : savedRegular.s, baseName, recursive, hashAlgo);
  359. stopWork();
  360. writeString(path, to);
  361. break;
  362. }
  363. case wopAddTextToStore: {
  364. string suffix = readString(from);
  365. string s = readString(from);
  366. PathSet refs = readStorePaths<PathSet>(from);
  367. startWork();
  368. Path path = store->addTextToStore(suffix, s, refs);
  369. stopWork();
  370. writeString(path, to);
  371. break;
  372. }
  373. case wopExportPath: {
  374. Path path = readStorePath(from);
  375. bool sign = readInt(from) == 1;
  376. startWork();
  377. TunnelSink sink(to);
  378. try {
  379. store->exportPath(path, sign, sink);
  380. }
  381. catch (Error &e) {
  382. /* Flush SINK beforehand or its destructor will rightfully trigger
  383. an assertion failure. */
  384. sink.flush();
  385. throw e;
  386. }
  387. sink.flush();
  388. stopWork();
  389. writeInt(1, to);
  390. break;
  391. }
  392. case wopImportPaths: {
  393. startWork();
  394. TunnelSource source(from);
  395. /* Unlike Nix, always require a signature, even for "trusted"
  396. users. */
  397. Paths paths = store->importPaths(true, source);
  398. stopWork();
  399. writeStrings(paths, to);
  400. break;
  401. }
  402. case wopBuildPaths: {
  403. PathSet drvs = readStorePaths<PathSet>(from);
  404. BuildMode mode = bmNormal;
  405. if (GET_PROTOCOL_MINOR(clientVersion) >= 15) {
  406. mode = (BuildMode)readInt(from);
  407. /* Repairing is not atomic, so disallowed for "untrusted"
  408. clients. */
  409. if (mode == bmRepair && !trusted)
  410. throw Error("repairing is a privileged operation");
  411. }
  412. startWork();
  413. store->buildPaths(drvs, mode);
  414. stopWork();
  415. writeInt(1, to);
  416. break;
  417. }
  418. case wopEnsurePath: {
  419. Path path = readStorePath(from);
  420. startWork();
  421. store->ensurePath(path);
  422. stopWork();
  423. writeInt(1, to);
  424. break;
  425. }
  426. case wopAddTempRoot: {
  427. Path path = readStorePath(from);
  428. startWork();
  429. store->addTempRoot(path);
  430. stopWork();
  431. writeInt(1, to);
  432. break;
  433. }
  434. case wopAddIndirectRoot: {
  435. Path path = absPath(readString(from));
  436. startWork();
  437. store->addIndirectRoot(path);
  438. stopWork();
  439. writeInt(1, to);
  440. break;
  441. }
  442. case wopSyncWithGC: {
  443. startWork();
  444. store->syncWithGC();
  445. stopWork();
  446. writeInt(1, to);
  447. break;
  448. }
  449. case wopFindRoots: {
  450. startWork();
  451. Roots roots = store->findRoots();
  452. stopWork();
  453. writeInt(roots.size(), to);
  454. for (Roots::iterator i = roots.begin(); i != roots.end(); ++i) {
  455. writeString(i->first, to);
  456. writeString(i->second, to);
  457. }
  458. break;
  459. }
  460. case wopCollectGarbage: {
  461. if (isRemoteConnection) {
  462. throw Error("Garbage collection is disabled for remote hosts.");
  463. break;
  464. }
  465. GCOptions options;
  466. options.action = (GCOptions::GCAction) readInt(from);
  467. options.pathsToDelete = readStorePaths<PathSet>(from);
  468. options.ignoreLiveness = readInt(from);
  469. options.maxFreed = readLongLong(from);
  470. readInt(from); // obsolete field
  471. if (GET_PROTOCOL_MINOR(clientVersion) >= 5) {
  472. /* removed options */
  473. readInt(from);
  474. readInt(from);
  475. }
  476. GCResults results;
  477. startWork();
  478. if (options.ignoreLiveness)
  479. throw Error("you are not allowed to ignore liveness");
  480. store->collectGarbage(options, results);
  481. stopWork();
  482. writeStrings(results.paths, to);
  483. writeLongLong(results.bytesFreed, to);
  484. writeLongLong(0, to); // obsolete
  485. break;
  486. }
  487. case wopSetOptions: {
  488. settings.keepFailed = readInt(from) != 0;
  489. if (isRemoteConnection)
  490. /* When the client is remote, don't keep the failed build tree as
  491. it is presumably inaccessible to the client and could fill up
  492. our disk. */
  493. settings.keepFailed = 0;
  494. settings.keepGoing = readInt(from) != 0;
  495. settings.set("build-fallback", readInt(from) ? "true" : "false");
  496. verbosity = (Verbosity) readInt(from);
  497. if (GET_PROTOCOL_MINOR(clientVersion) < 0x61) {
  498. settings.set("build-max-jobs", std::to_string(readInt(from)));
  499. settings.set("build-max-silent-time", std::to_string(readInt(from)));
  500. }
  501. if (GET_PROTOCOL_MINOR(clientVersion) >= 2) {
  502. #ifdef HAVE_DAEMON_OFFLOAD_HOOK
  503. settings.useBuildHook = readInt(from) != 0;
  504. #else
  505. readInt(from); // ignore the user's setting
  506. #endif
  507. }
  508. if (GET_PROTOCOL_MINOR(clientVersion) >= 4) {
  509. settings.buildVerbosity = (Verbosity) readInt(from);
  510. logType = (LogType) readInt(from);
  511. settings.printBuildTrace = readInt(from) != 0;
  512. }
  513. if (GET_PROTOCOL_MINOR(clientVersion) >= 6
  514. && GET_PROTOCOL_MINOR(clientVersion) < 0x61)
  515. settings.set("build-cores", std::to_string(readInt(from)));
  516. if (GET_PROTOCOL_MINOR(clientVersion) >= 10) {
  517. if (settings.useSubstitutes)
  518. settings.set("build-use-substitutes", readInt(from) ? "true" : "false");
  519. else
  520. readInt(from); // substitutes remain disabled
  521. }
  522. if (GET_PROTOCOL_MINOR(clientVersion) >= 12) {
  523. unsigned int n = readInt(from);
  524. for (unsigned int i = 0; i < n; i++) {
  525. string name = readString(from);
  526. string value = readString(from);
  527. if (name == "build-timeout" || name == "build-max-silent-time"
  528. || name == "build-max-jobs" || name == "build-cores"
  529. || name == "build-repeat"
  530. || name == "multiplexed-build-output")
  531. settings.set(name, value);
  532. else if (name == "user-name"
  533. && settings.clientUid == (uid_t) -1) {
  534. /* Create the user profile. This is necessary if
  535. clientUid = -1, for instance because the client
  536. connected over TCP. */
  537. struct passwd *pw = getpwnam(value.c_str());
  538. if (pw != NULL)
  539. store->createUser(value, pw->pw_uid);
  540. else
  541. printMsg(lvlInfo, format("user name %1% not found") % value);
  542. }
  543. else
  544. settings.set(trusted ? name : "untrusted-" + name, value);
  545. }
  546. }
  547. settings.update();
  548. startWork();
  549. stopWork();
  550. break;
  551. }
  552. case wopQuerySubstitutablePathInfo: {
  553. Path path = absPath(readString(from));
  554. startWork();
  555. SubstitutablePathInfos infos;
  556. store->querySubstitutablePathInfos(singleton<PathSet>(path), infos);
  557. stopWork();
  558. SubstitutablePathInfos::iterator i = infos.find(path);
  559. if (i == infos.end())
  560. writeInt(0, to);
  561. else {
  562. writeInt(1, to);
  563. writeString(i->second.deriver, to);
  564. writeStrings(i->second.references, to);
  565. writeLongLong(i->second.downloadSize, to);
  566. if (GET_PROTOCOL_MINOR(clientVersion) >= 7)
  567. writeLongLong(i->second.narSize, to);
  568. }
  569. break;
  570. }
  571. case wopQuerySubstitutablePathInfos: {
  572. PathSet paths = readStorePaths<PathSet>(from);
  573. startWork();
  574. SubstitutablePathInfos infos;
  575. store->querySubstitutablePathInfos(paths, infos);
  576. stopWork();
  577. writeInt(infos.size(), to);
  578. foreach (SubstitutablePathInfos::iterator, i, infos) {
  579. writeString(i->first, to);
  580. writeString(i->second.deriver, to);
  581. writeStrings(i->second.references, to);
  582. writeLongLong(i->second.downloadSize, to);
  583. writeLongLong(i->second.narSize, to);
  584. }
  585. break;
  586. }
  587. case wopQueryAllValidPaths: {
  588. startWork();
  589. PathSet paths = store->queryAllValidPaths();
  590. stopWork();
  591. writeStrings(paths, to);
  592. break;
  593. }
  594. case wopQueryFailedPaths: {
  595. startWork();
  596. PathSet paths = store->queryFailedPaths();
  597. stopWork();
  598. writeStrings(paths, to);
  599. break;
  600. }
  601. case wopClearFailedPaths: {
  602. PathSet paths = readStrings<PathSet>(from);
  603. startWork();
  604. store->clearFailedPaths(paths);
  605. stopWork();
  606. writeInt(1, to);
  607. break;
  608. }
  609. case wopQueryPathInfo: {
  610. Path path = readStorePath(from);
  611. startWork();
  612. ValidPathInfo info = store->queryPathInfo(path);
  613. stopWork();
  614. writeString(info.deriver, to);
  615. writeString(printHash(info.hash), to);
  616. writeStrings(info.references, to);
  617. writeInt(info.registrationTime, to);
  618. writeLongLong(info.narSize, to);
  619. break;
  620. }
  621. case wopOptimiseStore:
  622. startWork();
  623. store->optimiseStore();
  624. stopWork();
  625. writeInt(1, to);
  626. break;
  627. case wopVerifyStore: {
  628. bool checkContents = readInt(from) != 0;
  629. bool repair = readInt(from) != 0;
  630. startWork();
  631. if (repair && !trusted)
  632. throw Error("you are not privileged to repair paths");
  633. bool errors = store->verifyStore(checkContents, repair);
  634. stopWork();
  635. writeInt(errors, to);
  636. break;
  637. }
  638. case wopBuiltinBuilders: {
  639. startWork();
  640. auto names = builtinBuilderNames();
  641. stopWork();
  642. writeStrings(names, to);
  643. break;
  644. }
  645. default:
  646. throw Error(format("invalid operation %1%") % op);
  647. }
  648. }
  649. static void processConnection(bool trusted, uid_t userId)
  650. {
  651. canSendStderr = false;
  652. _writeToStderr = tunnelStderr;
  653. #ifdef HAVE_HUP_NOTIFICATION
  654. /* Allow us to receive SIGPOLL for events on the client socket. */
  655. setSigPollAction(false);
  656. if (fcntl(from.fd, F_SETOWN, getpid()) == -1)
  657. throw SysError("F_SETOWN");
  658. if (fcntl(from.fd, F_SETFL, fcntl(from.fd, F_GETFL, 0) | O_ASYNC) == -1)
  659. throw SysError("F_SETFL");
  660. #endif
  661. /* Exchange the greeting. */
  662. unsigned int magic = readInt(from);
  663. if (magic != WORKER_MAGIC_1) throw Error("protocol mismatch");
  664. writeInt(WORKER_MAGIC_2, to);
  665. writeInt(PROTOCOL_VERSION, to);
  666. to.flush();
  667. unsigned int clientVersion = readInt(from);
  668. if (GET_PROTOCOL_MINOR(clientVersion) >= 14 && readInt(from))
  669. setAffinityTo(readInt(from));
  670. bool reserveSpace = true;
  671. if (GET_PROTOCOL_MINOR(clientVersion) >= 11)
  672. reserveSpace = readInt(from) != 0;
  673. /* Send startup error messages to the client. */
  674. startWork();
  675. try {
  676. /* If we can't accept clientVersion, then throw an error
  677. *here* (not above). */
  678. #if 0
  679. /* Prevent users from doing something very dangerous. */
  680. if (geteuid() == 0 &&
  681. querySetting("build-users-group", "") == "")
  682. throw Error("if you run `nix-daemon' as root, then you MUST set `build-users-group'!");
  683. #endif
  684. /* Open the store. */
  685. store = std::shared_ptr<StoreAPI>(new LocalStore(reserveSpace));
  686. if (userId != (uid_t) -1) {
  687. /* Create the user profile. */
  688. struct passwd *pw = getpwuid(userId);
  689. if (pw != NULL && pw->pw_name != NULL)
  690. store->createUser(pw->pw_name, userId);
  691. else
  692. printMsg(lvlInfo, format("user with UID %1% not found") % userId);
  693. }
  694. stopWork();
  695. to.flush();
  696. } catch (Error & e) {
  697. stopWork(false, e.msg(), GET_PROTOCOL_MINOR(clientVersion) >= 8 ? 1 : 0);
  698. to.flush();
  699. return;
  700. }
  701. /* Process client requests. */
  702. unsigned int opCount = 0;
  703. while (true) {
  704. WorkerOp op;
  705. try {
  706. op = (WorkerOp) readInt(from);
  707. } catch (EndOfFile & e) {
  708. break;
  709. }
  710. opCount++;
  711. try {
  712. performOp(trusted, clientVersion, from, to, op);
  713. } catch (Error & e) {
  714. /* If we're not in a state where we can send replies, then
  715. something went wrong processing the input of the
  716. client. This can happen especially if I/O errors occur
  717. during addTextToStore() / importPath(). If that
  718. happens, just send the error message and exit. */
  719. bool errorAllowed = canSendStderr;
  720. stopWork(false, e.msg(), GET_PROTOCOL_MINOR(clientVersion) >= 8 ? e.status : 0);
  721. if (!errorAllowed) throw;
  722. } catch (std::bad_alloc & e) {
  723. stopWork(false, "build daemon out of memory", GET_PROTOCOL_MINOR(clientVersion) >= 8 ? 1 : 0);
  724. throw;
  725. }
  726. to.flush();
  727. assert(!canSendStderr);
  728. };
  729. canSendStderr = false;
  730. _isInterrupted = false;
  731. printMsg(lvlDebug, format("%1% operations") % opCount);
  732. }
  733. static void sigChldHandler(int sigNo)
  734. {
  735. /* Reap all dead children. */
  736. while (waitpid(-1, 0, WNOHANG) > 0) ;
  737. }
  738. static void setSigChldAction(bool autoReap)
  739. {
  740. struct sigaction act, oact;
  741. act.sa_handler = autoReap ? sigChldHandler : SIG_DFL;
  742. sigfillset(&act.sa_mask);
  743. act.sa_flags = 0;
  744. if (sigaction(SIGCHLD, &act, &oact))
  745. throw SysError("setting SIGCHLD handler");
  746. }
  747. /* Accept a connection on FDSOCKET and fork a server process to process the
  748. new connection. */
  749. static void acceptConnection(int fdSocket)
  750. {
  751. uid_t clientUid = (uid_t) -1;
  752. gid_t clientGid = (gid_t) -1;
  753. try {
  754. /* Important: the server process *cannot* open the SQLite
  755. database, because it doesn't like forks very much. */
  756. assert(!store);
  757. /* Accept a connection. */
  758. struct sockaddr_storage remoteAddr;
  759. socklen_t remoteAddrLen = sizeof(remoteAddr);
  760. try_again:
  761. AutoCloseFD remote = accept(fdSocket,
  762. (struct sockaddr *) &remoteAddr, &remoteAddrLen);
  763. checkInterrupt();
  764. if (remote == -1) {
  765. if (errno == EINTR)
  766. goto try_again;
  767. else
  768. throw SysError("accepting connection");
  769. }
  770. closeOnExec(remote);
  771. {
  772. int enabled = 1;
  773. /* If we're on a TCP connection, disable Nagle's algorithm so that
  774. data is sent as soon as possible. */
  775. (void) setsockopt(remote, SOL_TCP, TCP_NODELAY,
  776. &enabled, sizeof enabled);
  777. #if defined(TCP_QUICKACK)
  778. /* Enable TCP quick-ack if applicable; this might help a little. */
  779. (void) setsockopt(remote, SOL_TCP, TCP_QUICKACK,
  780. &enabled, sizeof enabled);
  781. #endif
  782. }
  783. pid_t clientPid = -1;
  784. bool trusted = false;
  785. /* Get the identity of the caller, if possible. */
  786. if (remoteAddr.ss_family == AF_UNIX) {
  787. #if defined(SO_PEERCRED)
  788. ucred cred;
  789. socklen_t credLen = sizeof(cred);
  790. if (getsockopt(remote, SOL_SOCKET, SO_PEERCRED,
  791. &cred, &credLen) == -1)
  792. throw SysError("getting peer credentials");
  793. clientPid = cred.pid;
  794. clientUid = cred.uid;
  795. clientGid = cred.gid;
  796. trusted = clientUid == 0;
  797. struct passwd * pw = getpwuid(cred.uid);
  798. string user = pw ? pw->pw_name : std::to_string(cred.uid);
  799. printMsg(lvlInfo,
  800. format((string) "accepted connection from pid %1%, user %2%")
  801. % clientPid % user);
  802. #endif
  803. } else {
  804. char address_str[128];
  805. const char *result;
  806. if (remoteAddr.ss_family == AF_INET) {
  807. struct sockaddr_in *addr = (struct sockaddr_in *) &remoteAddr;
  808. result = inet_ntop(AF_INET, &addr->sin_addr,
  809. address_str, sizeof address_str);
  810. } else if (remoteAddr.ss_family == AF_INET6) {
  811. struct sockaddr_in6 *addr = (struct sockaddr_in6 *) &remoteAddr;
  812. result = inet_ntop(AF_INET6, &addr->sin6_addr,
  813. address_str, sizeof address_str);
  814. } else {
  815. result = NULL;
  816. }
  817. if (result != NULL) {
  818. printMsg(lvlInfo,
  819. format("accepted connection from %1%")
  820. % address_str);
  821. }
  822. }
  823. /* Fork a child to handle the connection. */
  824. startProcess([&]() {
  825. close(fdSocket);
  826. /* Background the daemon. */
  827. if (setsid() == -1)
  828. throw SysError(format("creating a new session"));
  829. /* Restore normal handling of SIGCHLD. */
  830. setSigChldAction(false);
  831. /* For debugging, stuff the pid into argv[1]. */
  832. if (clientPid != -1 && argvSaved[1]) {
  833. string processName = std::to_string(clientPid);
  834. strncpy(argvSaved[1], processName.c_str(), strlen(argvSaved[1]));
  835. }
  836. /* Store the client's user and group for this connection. This
  837. has to be done in the forked process since it is per
  838. connection. Setting these to -1 means: do not change. */
  839. settings.clientUid = clientUid;
  840. settings.clientGid = clientGid;
  841. isRemoteConnection = (remoteAddr.ss_family != AF_UNIX);
  842. /* Handle the connection. */
  843. from.fd = remote;
  844. to.fd = remote;
  845. processConnection(trusted, clientUid);
  846. exit(0);
  847. }, false, "unexpected build daemon error: ", true);
  848. } catch (Interrupted & e) {
  849. throw;
  850. } catch (Error & e) {
  851. printMsg(lvlError, format("error processing connection: %1%") % e.msg());
  852. }
  853. }
  854. static void daemonLoop(const std::vector<int>& sockets)
  855. {
  856. if (chdir("/") == -1)
  857. throw SysError("cannot change current directory");
  858. /* Get rid of children automatically; don't let them become
  859. zombies. */
  860. setSigChldAction(true);
  861. /* Mark sockets as close-on-exec. */
  862. for(int fd: sockets) {
  863. closeOnExec(fd);
  864. }
  865. /* Prepare the FD set corresponding to SOCKETS. */
  866. auto initializeFDSet = [&](fd_set *set) {
  867. FD_ZERO(set);
  868. for (int fd: sockets) {
  869. FD_SET(fd, set);
  870. }
  871. };
  872. /* Loop accepting connections. */
  873. while (1) {
  874. fd_set readfds;
  875. initializeFDSet(&readfds);
  876. int count =
  877. select(*std::max_element(sockets.begin(), sockets.end()) + 1,
  878. &readfds, NULL, NULL,
  879. NULL);
  880. if (count < 0) {
  881. int err = errno;
  882. if (err == EINTR)
  883. continue;
  884. throw SysError(format("select error: %1%") % strerror(err));
  885. }
  886. for (unsigned int i = 0; i < sockets.size(); i++) {
  887. if (FD_ISSET(sockets[i], &readfds)) {
  888. acceptConnection(sockets[i]);
  889. }
  890. }
  891. }
  892. }
  893. void run(const std::vector<int>& sockets)
  894. {
  895. daemonLoop(sockets);
  896. }