guix-daemon.cc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. /* GNU Guix --- Functional package management for GNU
  2. Copyright (C) 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019 Ludovic Courtès <ludo@gnu.org>
  3. Copyright (C) 2006, 2010, 2012, 2014 Eelco Dolstra <e.dolstra@tudelft.nl>
  4. Copyright (C) 2021 Maxime Devos <maximedevos@telenet.be>
  5. This file is part of GNU Guix.
  6. GNU Guix is free software; you can redistribute it and/or modify it
  7. under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 3 of the License, or (at
  9. your option) any later version.
  10. GNU Guix is distributed in the hope that it will be useful, but
  11. WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with GNU Guix. If not, see <http://www.gnu.org/licenses/>. */
  16. #include <config.h>
  17. #include <types.hh>
  18. #include "shared.hh"
  19. #include <globals.hh>
  20. #include <util.hh>
  21. #include <gcrypt.h>
  22. #include <stdlib.h>
  23. #include <argp.h>
  24. #include <unistd.h>
  25. #include <sys/types.h>
  26. #include <sys/stat.h>
  27. #include <sys/socket.h>
  28. #include <sys/un.h>
  29. #include <netdb.h>
  30. #include <strings.h>
  31. #include <exception>
  32. #include <iostream>
  33. #include <libintl.h>
  34. #include <locale.h>
  35. /* Variables used by `nix-daemon.cc'. */
  36. volatile ::sig_atomic_t blockInt;
  37. char **argvSaved;
  38. using namespace nix;
  39. /* Entry point in `nix-daemon.cc'. */
  40. extern void run (const std::vector<int> &);
  41. /* Command-line options. */
  42. #define n_(str) str
  43. #define _(str) gettext (str)
  44. static const char guix_textdomain[] = "guix";
  45. const char *argp_program_version =
  46. "guix-daemon (" PACKAGE_NAME ") " PACKAGE_VERSION;
  47. const char *argp_program_bug_address = PACKAGE_BUGREPORT;
  48. static char doc[] =
  49. n_("guix-daemon -- perform derivation builds and store accesses")
  50. "\v\n"
  51. n_("This program is a daemon meant to run in the background. It serves \
  52. requests sent over a Unix-domain socket. It accesses the store, and \
  53. builds derivations on behalf of its clients.");
  54. #define GUIX_OPT_SYSTEM 1
  55. #define GUIX_OPT_DISABLE_CHROOT 2
  56. #define GUIX_OPT_BUILD_USERS_GROUP 3
  57. #define GUIX_OPT_CACHE_FAILURES 4
  58. #define GUIX_OPT_LOSE_LOGS 5
  59. #define GUIX_OPT_DISABLE_LOG_COMPRESSION 6
  60. #define GUIX_OPT_DISABLE_DEDUPLICATION 7
  61. #define GUIX_OPT_IMPERSONATE_LINUX_26 8
  62. #define GUIX_OPT_DEBUG 9
  63. #define GUIX_OPT_CHROOT_DIR 10
  64. #define GUIX_OPT_LISTEN 11
  65. #define GUIX_OPT_NO_SUBSTITUTES 12
  66. #define GUIX_OPT_SUBSTITUTE_URLS 13
  67. #define GUIX_OPT_NO_BUILD_HOOK 14
  68. #define GUIX_OPT_GC_KEEP_OUTPUTS 15
  69. #define GUIX_OPT_GC_KEEP_DERIVATIONS 16
  70. #define GUIX_OPT_BUILD_ROUNDS 17
  71. #define GUIX_OPT_TIMEOUT 18
  72. #define GUIX_OPT_MAX_SILENT_TIME 19
  73. #define GUIX_OPT_LOG_COMPRESSION 20
  74. #define GUIX_OPT_DISCOVER 21
  75. #define GUIX_OPT_SUBSTITUTE_METHODS 22
  76. static const struct argp_option options[] =
  77. {
  78. { "system", GUIX_OPT_SYSTEM, n_("SYSTEM"), 0,
  79. n_("assume SYSTEM as the current system type") },
  80. { "cores", 'c', n_("N"), 0,
  81. n_("use N CPU cores to build each derivation; 0 means as many as available")
  82. },
  83. { "max-jobs", 'M', n_("N"), 0,
  84. n_("allow at most N build jobs") },
  85. { "timeout", GUIX_OPT_TIMEOUT, n_("SECONDS"), 0,
  86. n_("mark builds as failed after SECONDS of activity") },
  87. { "max-silent-time", GUIX_OPT_MAX_SILENT_TIME, n_("SECONDS"), 0,
  88. n_("mark builds as failed after SECONDS of silence") },
  89. { "disable-chroot", GUIX_OPT_DISABLE_CHROOT, 0, 0,
  90. n_("disable chroot builds") },
  91. { "chroot-directory", GUIX_OPT_CHROOT_DIR, n_("DIR"), 0,
  92. n_("add DIR to the build chroot") },
  93. { "build-users-group", GUIX_OPT_BUILD_USERS_GROUP, n_("GROUP"), 0,
  94. n_("perform builds as a user of GROUP") },
  95. { "no-substitutes", GUIX_OPT_NO_SUBSTITUTES, 0, 0,
  96. n_("do not use substitutes") },
  97. { "substitute-urls", GUIX_OPT_SUBSTITUTE_URLS, n_("URLS"), 0,
  98. n_("use URLS as the default list of substitute providers") },
  99. { "substitute-methods", GUIX_OPT_SUBSTITUTE_METHODS, n_("METHODS"), 0,
  100. n_("use METHODS as the list of substitute methods") },
  101. { "no-offload", GUIX_OPT_NO_BUILD_HOOK, 0, 0,
  102. n_("do not attempt to offload builds") },
  103. { "no-build-hook", GUIX_OPT_NO_BUILD_HOOK, 0,
  104. OPTION_HIDDEN, // deprecated
  105. n_("do not attempt to offload builds") },
  106. { "cache-failures", GUIX_OPT_CACHE_FAILURES, 0, 0,
  107. n_("cache build failures") },
  108. { "rounds", GUIX_OPT_BUILD_ROUNDS, "N", 0,
  109. n_("build each derivation N times in a row") },
  110. { "lose-logs", GUIX_OPT_LOSE_LOGS, 0, 0,
  111. n_("do not keep build logs") },
  112. { "disable-log-compression", GUIX_OPT_DISABLE_LOG_COMPRESSION, 0,
  113. OPTION_HIDDEN, // deprecated
  114. n_("disable compression of the build logs") },
  115. { "log-compression", GUIX_OPT_LOG_COMPRESSION, "TYPE", 0,
  116. n_("use the specified compression type for build logs") },
  117. { "discover", GUIX_OPT_DISCOVER, "yes/no", OPTION_ARG_OPTIONAL,
  118. n_("use substitute servers discovered on the local network") },
  119. /* '--disable-deduplication' was known as '--disable-store-optimization'
  120. up to Guix 0.7 included, so keep the alias around. */
  121. { "disable-deduplication", GUIX_OPT_DISABLE_DEDUPLICATION, 0, 0,
  122. n_("disable automatic file \"deduplication\" in the store") },
  123. { "disable-store-optimization", GUIX_OPT_DISABLE_DEDUPLICATION, 0,
  124. OPTION_ALIAS | OPTION_HIDDEN, NULL },
  125. { "impersonate-linux-2.6", GUIX_OPT_IMPERSONATE_LINUX_26, 0,
  126. #ifdef HAVE_SYS_PERSONALITY_H
  127. 0,
  128. #else
  129. OPTION_HIDDEN,
  130. #endif
  131. n_("impersonate Linux 2.6")
  132. },
  133. { "gc-keep-outputs", GUIX_OPT_GC_KEEP_OUTPUTS,
  134. "yes/no", OPTION_ARG_OPTIONAL,
  135. n_("tell whether the GC must keep outputs of live derivations") },
  136. { "gc-keep-derivations", GUIX_OPT_GC_KEEP_DERIVATIONS,
  137. "yes/no", OPTION_ARG_OPTIONAL,
  138. n_("tell whether the GC must keep derivations corresponding \
  139. to live outputs") },
  140. { "listen", GUIX_OPT_LISTEN, n_("SOCKET"), 0,
  141. n_("listen for connections on SOCKET") },
  142. { "debug", GUIX_OPT_DEBUG, 0, 0,
  143. n_("produce debugging output") },
  144. { 0, 0, 0, 0, 0 }
  145. };
  146. /* Default port for '--listen' on TCP/IP. */
  147. #define DEFAULT_GUIX_PORT "44146"
  148. /* List of '--listen' options. */
  149. static std::list<std::string> listen_options;
  150. static bool useDiscover = false;
  151. /* Convert ARG to a Boolean value, or throw an error if it does not denote a
  152. Boolean. */
  153. static bool
  154. string_to_bool (const char *arg, bool dflt = true)
  155. {
  156. if (arg == NULL)
  157. return dflt;
  158. else if (strcasecmp (arg, "yes") == 0)
  159. return true;
  160. else if (strcasecmp (arg, "no") == 0)
  161. return false;
  162. else
  163. throw nix::Error (format ("'%1%': invalid Boolean value") % arg);
  164. }
  165. /* Parse a single option. */
  166. static error_t
  167. parse_opt (int key, char *arg, struct argp_state *state)
  168. {
  169. switch (key)
  170. {
  171. case GUIX_OPT_DISABLE_CHROOT:
  172. settings.useChroot = false;
  173. break;
  174. case GUIX_OPT_CHROOT_DIR:
  175. {
  176. std::string chroot_dirs;
  177. chroot_dirs = settings.get ("build-extra-chroot-dirs",
  178. (std::string) "");
  179. if (chroot_dirs == "")
  180. chroot_dirs = arg;
  181. else
  182. chroot_dirs = chroot_dirs + " " + arg;
  183. settings.set("build-extra-chroot-dirs", chroot_dirs);
  184. break;
  185. }
  186. case GUIX_OPT_LOG_COMPRESSION:
  187. if (strcmp (arg, "none") == 0)
  188. settings.logCompression = COMPRESSION_NONE;
  189. else if (strcmp (arg, "gzip") == 0)
  190. settings.logCompression = COMPRESSION_GZIP;
  191. #if HAVE_BZLIB_H
  192. else if (strcmp (arg, "bzip2") == 0)
  193. settings.logCompression = COMPRESSION_BZIP2;
  194. #endif
  195. else
  196. {
  197. fprintf (stderr, _("error: %s: unknown compression type\n"), arg);
  198. exit (EXIT_FAILURE);
  199. }
  200. break;
  201. case GUIX_OPT_DISABLE_LOG_COMPRESSION:
  202. settings.logCompression = COMPRESSION_NONE;
  203. break;
  204. case GUIX_OPT_BUILD_USERS_GROUP:
  205. settings.buildUsersGroup = arg;
  206. break;
  207. case GUIX_OPT_DISABLE_DEDUPLICATION:
  208. settings.autoOptimiseStore = false;
  209. break;
  210. case GUIX_OPT_CACHE_FAILURES:
  211. settings.cacheFailure = true;
  212. break;
  213. case GUIX_OPT_BUILD_ROUNDS:
  214. {
  215. char *end;
  216. unsigned long n = strtoul (arg, &end, 10);
  217. if (end != arg + strlen (arg))
  218. {
  219. fprintf (stderr, _("error: %s: invalid number of rounds\n"), arg);
  220. exit (EXIT_FAILURE);
  221. }
  222. settings.set ("build-repeat", std::to_string (std::max (0UL, n - 1)));
  223. break;
  224. }
  225. case GUIX_OPT_IMPERSONATE_LINUX_26:
  226. settings.impersonateLinux26 = true;
  227. break;
  228. case GUIX_OPT_LOSE_LOGS:
  229. settings.keepLog = false;
  230. break;
  231. case GUIX_OPT_LISTEN:
  232. listen_options.push_back (arg);
  233. break;
  234. case GUIX_OPT_SUBSTITUTE_URLS:
  235. settings.set ("substitute-urls", arg);
  236. break;
  237. case GUIX_OPT_NO_SUBSTITUTES:
  238. settings.set ("build-use-substitutes", "false");
  239. break;
  240. case GUIX_OPT_SUBSTITUTE_METHODS:
  241. settings.set ("substitute-methods", arg);
  242. break;
  243. case GUIX_OPT_NO_BUILD_HOOK:
  244. settings.useBuildHook = false;
  245. break;
  246. case GUIX_OPT_DISCOVER:
  247. useDiscover = string_to_bool (arg);
  248. settings.set("discover", arg);
  249. break;
  250. case GUIX_OPT_DEBUG:
  251. verbosity = lvlDebug;
  252. break;
  253. case GUIX_OPT_GC_KEEP_OUTPUTS:
  254. settings.gcKeepOutputs = string_to_bool (arg);
  255. break;
  256. case GUIX_OPT_GC_KEEP_DERIVATIONS:
  257. settings.gcKeepDerivations = string_to_bool (arg);
  258. break;
  259. case 'c':
  260. settings.set ("build-cores", arg);
  261. break;
  262. case 'M':
  263. settings.set ("build-max-jobs", arg);
  264. break;
  265. case GUIX_OPT_TIMEOUT:
  266. settings.set ("build-timeout", arg);
  267. break;
  268. case GUIX_OPT_MAX_SILENT_TIME:
  269. settings.set ("build-max-silent-time", arg);
  270. break;
  271. case GUIX_OPT_SYSTEM:
  272. settings.thisSystem = arg;
  273. break;
  274. default:
  275. return (error_t) ARGP_ERR_UNKNOWN;
  276. }
  277. return (error_t) 0;
  278. }
  279. /* Argument parsing. */
  280. static const struct argp argp =
  281. {
  282. options, parse_opt,
  283. NULL, doc,
  284. NULL, NULL, // children and help_filter
  285. guix_textdomain
  286. };
  287. static int
  288. open_unix_domain_socket (const char *file)
  289. {
  290. /* Create and bind to a Unix domain socket. */
  291. AutoCloseFD fdSocket = socket (PF_UNIX, SOCK_STREAM, 0);
  292. if (fdSocket == -1)
  293. throw SysError (_("cannot create Unix domain socket"));
  294. createDirs (dirOf (file));
  295. /* Urgh, sockaddr_un allows path names of only 108 characters.
  296. So chdir to the socket directory so that we can pass a
  297. relative path name. */
  298. if (chdir (dirOf (file).c_str ()) == -1)
  299. throw SysError (_("cannot change current directory"));
  300. Path fileRel = "./" + baseNameOf (file);
  301. struct sockaddr_un addr;
  302. addr.sun_family = AF_UNIX;
  303. if (fileRel.size () >= sizeof (addr.sun_path))
  304. throw Error (format (_("socket file name '%1%' is too long")) % fileRel);
  305. strcpy (addr.sun_path, fileRel.c_str ());
  306. unlink (file);
  307. /* Make sure that the socket is created with 0666 permission
  308. (everybody can connect --- provided they have access to the
  309. directory containing the socket). */
  310. mode_t oldMode = umask (0111);
  311. int res = bind (fdSocket, (struct sockaddr *) &addr, sizeof addr);
  312. umask (oldMode);
  313. if (res == -1)
  314. throw SysError (format (_("cannot bind to socket '%1%'")) % file);
  315. if (chdir ("/") == -1) /* back to the root */
  316. throw SysError (_("cannot change current directory"));
  317. if (listen (fdSocket, 5) == -1)
  318. throw SysError (format (_("cannot listen on socket '%1%'")) % file);
  319. return fdSocket.borrow ();
  320. }
  321. /* Return a listening socket for ADDRESS, which has the given LENGTH. */
  322. static int
  323. open_inet_socket (const struct sockaddr *address, socklen_t length)
  324. {
  325. AutoCloseFD fd = socket (address->sa_family, SOCK_STREAM, 0);
  326. if (fd == -1)
  327. throw SysError (_("cannot create TCP socket"));
  328. int res = bind (fd, address, length);
  329. if (res == -1)
  330. throw SysError (_("cannot bind TCP socket"));
  331. if (listen (fd, 5) == -1)
  332. throw SysError (format (_("cannot listen on TCP socket")));
  333. return fd.borrow ();
  334. }
  335. /* Return a list of file descriptors of listening sockets. */
  336. static std::vector<int>
  337. listening_sockets (const std::list<std::string> &options)
  338. {
  339. std::vector<int> result;
  340. if (options.empty ())
  341. {
  342. /* Open the default Unix-domain socket. */
  343. auto fd = open_unix_domain_socket (settings.nixDaemonSocketFile.c_str ());
  344. result.push_back (fd);
  345. return result;
  346. }
  347. /* Open the user-specified sockets. */
  348. for (const std::string& option: options)
  349. {
  350. if (option[0] == '/')
  351. {
  352. /* Assume OPTION is the file name of a Unix-domain socket. */
  353. settings.nixDaemonSocketFile = canonPath (option);
  354. int fd =
  355. open_unix_domain_socket (settings.nixDaemonSocketFile.c_str ());
  356. result.push_back (fd);
  357. }
  358. else
  359. {
  360. /* Assume OPTIONS has the form "HOST" or "HOST:PORT". */
  361. auto colon = option.find_last_of (":");
  362. auto host = colon == std::string::npos
  363. ? option : option.substr (0, colon);
  364. auto port = colon == std::string::npos
  365. ? DEFAULT_GUIX_PORT
  366. : option.substr (colon + 1, option.size () - colon - 1);
  367. struct addrinfo *res, hints;
  368. memset (&hints, '\0', sizeof hints);
  369. hints.ai_socktype = SOCK_STREAM;
  370. hints.ai_flags = AI_NUMERICSERV | AI_ADDRCONFIG;
  371. int err = getaddrinfo (host.c_str(), port.c_str (),
  372. &hints, &res);
  373. if (err != 0)
  374. throw Error(format ("failed to look up '%1%': %2%")
  375. % option % gai_strerror (err));
  376. printMsg (lvlDebug, format ("listening on '%1%', port '%2%'")
  377. % host % port);
  378. /* XXX: Pick the first result, RES. */
  379. result.push_back (open_inet_socket (res->ai_addr,
  380. res->ai_addrlen));
  381. freeaddrinfo (res);
  382. }
  383. }
  384. return result;
  385. }
  386. int
  387. main (int argc, char *argv[])
  388. {
  389. setlocale (LC_ALL, "");
  390. bindtextdomain (guix_textdomain, LOCALEDIR);
  391. textdomain (guix_textdomain);
  392. /* Initialize libgcrypt. */
  393. if (!gcry_check_version (GCRYPT_VERSION))
  394. {
  395. fprintf (stderr, _("error: libgcrypt version mismatch\n"));
  396. exit (EXIT_FAILURE);
  397. }
  398. /* Tell Libgcrypt that initialization has completed, as per the Libgcrypt
  399. 1.6.0 manual (although this does not appear to be strictly needed.) */
  400. gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
  401. /* Set the umask so that the daemon does not end up creating group-writable
  402. files, which would lead to "suspicious ownership or permission" errors.
  403. See <http://lists.gnu.org/archive/html/bug-guix/2013-07/msg00033.html>. */
  404. umask (S_IWGRP | S_IWOTH);
  405. #ifndef HAVE_CHROOT
  406. # error chroot is assumed to be available
  407. #endif
  408. /* Always use chroots by default. */
  409. settings.useChroot = true;
  410. /* Turn automatic deduplication on by default. */
  411. settings.autoOptimiseStore = true;
  412. /* Default to using as many cores as possible and one job at a time. */
  413. settings.buildCores = 0;
  414. settings.maxBuildJobs = 1;
  415. argvSaved = argv;
  416. try
  417. {
  418. settings.processEnvironment ();
  419. /* Enable substitutes by default. */
  420. settings.set ("build-use-substitutes", "true");
  421. /* Use our substitute server by default. */
  422. settings.set ("substitute-urls", GUIX_SUBSTITUTE_URLS);
  423. #ifdef HAVE_DAEMON_OFFLOAD_HOOK
  424. /* Use 'guix offload' for distributed builds by default. */
  425. settings.useBuildHook = true;
  426. #else
  427. /* We are not installing any build hook, so disable it. */
  428. settings.useBuildHook = false;
  429. #endif
  430. argp_parse (&argp, argc, argv, 0, 0, 0);
  431. auto sockets = listening_sockets (listen_options);
  432. /* Effect all the changes made via 'settings.set'. */
  433. settings.update ();
  434. printMsg(lvlDebug,
  435. format ("build log compression: %1%") % settings.logCompression);
  436. if (geteuid () == 0 && settings.buildUsersGroup.empty ())
  437. fprintf (stderr, _("warning: daemon is running as root, so \
  438. using `--build-users-group' is highly recommended\n"));
  439. if (settings.useChroot)
  440. {
  441. std::string chroot_dirs;
  442. chroot_dirs = settings.get ("build-extra-chroot-dirs",
  443. (std::string) "");
  444. printMsg (lvlDebug,
  445. format ("extra chroot directories: '%1%'") % chroot_dirs);
  446. }
  447. if (useDiscover)
  448. {
  449. Strings args;
  450. args.push_back("guix");
  451. args.push_back("discover");
  452. startProcess([&]() {
  453. execv(settings.guixProgram.c_str(), stringsToCharPtrs(args).data());
  454. });
  455. }
  456. printMsg (lvlDebug,
  457. format ("automatic deduplication set to %1%")
  458. % settings.autoOptimiseStore);
  459. printMsg (lvlDebug,
  460. format ("listening on `%1%'") % settings.nixDaemonSocketFile);
  461. run (sockets);
  462. }
  463. catch (std::exception &e)
  464. {
  465. fprintf (stderr, _("error: %s\n"), e.what ());
  466. return EXIT_FAILURE;
  467. }
  468. return EXIT_SUCCESS; /* never reached */
  469. }