Mirror of GNU Guix
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.

219 lines
5.9 KiB

  1. /* GNU Guix --- Functional package management for GNU
  2. Copyright (C) 2012 Ludovic Courtès <ludo@gnu.org>
  3. This file is part of GNU Guix.
  4. GNU Guix is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or (at
  7. your option) any later version.
  8. GNU Guix is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Guix. If not, see <http://www.gnu.org/licenses/>. */
  14. #include <config.h>
  15. #include <types.hh>
  16. #include "shared.hh"
  17. #include <globals.hh>
  18. #include <util.hh>
  19. #include <gcrypt.h>
  20. #include <stdlib.h>
  21. #include <argp.h>
  22. #include <unistd.h>
  23. #include <sys/types.h>
  24. #include <exception>
  25. /* Variables used by `nix-daemon.cc'. */
  26. volatile ::sig_atomic_t blockInt;
  27. char **argvSaved;
  28. using namespace nix;
  29. /* Entry point in `nix-daemon.cc'. */
  30. extern void run (Strings args);
  31. /* Command-line options. */
  32. const char *argp_program_version =
  33. "guix-daemon (" PACKAGE_NAME ") " PACKAGE_VERSION;
  34. const char *argp_program_bug_address = PACKAGE_BUGREPORT;
  35. static char doc[] =
  36. "guix-daemon -- perform derivation builds and store accesses\
  37. \v\
  38. This program is a daemon meant to run in the background. It serves \
  39. requests sent over a Unix-domain socket. It accesses the store, and \
  40. builds derivations on behalf of its clients.";
  41. #define GUIX_OPT_SYSTEM 1
  42. #define GUIX_OPT_DISABLE_CHROOT 2
  43. #define GUIX_OPT_BUILD_USERS_GROUP 3
  44. #define GUIX_OPT_CACHE_FAILURES 4
  45. #define GUIX_OPT_LOSE_LOGS 5
  46. #define GUIX_OPT_DISABLE_LOG_COMPRESSION 6
  47. #define GUIX_OPT_DISABLE_STORE_OPTIMIZATION 7
  48. #define GUIX_OPT_IMPERSONATE_LINUX_26 8
  49. #define GUIX_OPT_DEBUG 9
  50. #define GUIX_OPT_CHROOT_DIR 10
  51. static const struct argp_option options[] =
  52. {
  53. { "system", GUIX_OPT_SYSTEM, "SYSTEM", 0,
  54. "Assume SYSTEM as the current system type" },
  55. { "cores", 'c', "N", 0,
  56. "Use N CPU cores to build each derivation; 0 means as many as available" },
  57. { "max-jobs", 'M', "N", 0,
  58. "Allow at most N build jobs" },
  59. { "disable-chroot", GUIX_OPT_DISABLE_CHROOT, 0, 0,
  60. "Disable chroot builds"
  61. #ifndef HAVE_CHROOT
  62. " (chroots are not supported in this configuration, so "
  63. "this option has no effect)"
  64. #endif
  65. },
  66. { "chroot-directory", GUIX_OPT_CHROOT_DIR, "DIR", 0,
  67. "Add DIR to the build chroot"
  68. #ifndef HAVE_CHROOT
  69. " (chroots are not supported in this configuration, so "
  70. "this option has no effect)"
  71. #endif
  72. },
  73. { "build-users-group", GUIX_OPT_BUILD_USERS_GROUP, "GROUP", 0,
  74. "Perform builds as a user of GROUP" },
  75. { "cache-failures", GUIX_OPT_CACHE_FAILURES, 0, 0,
  76. "Cache build failures" },
  77. { "lose-logs", GUIX_OPT_LOSE_LOGS, 0, 0,
  78. "Do not keep build logs" },
  79. { "disable-log-compression", GUIX_OPT_DISABLE_LOG_COMPRESSION, 0, 0,
  80. "Disable compression of the build logs" },
  81. { "disable-store-optimization", GUIX_OPT_DISABLE_STORE_OPTIMIZATION, 0, 0,
  82. "Disable automatic file \"deduplication\" in the store" },
  83. { "impersonate-linux-2.6", GUIX_OPT_IMPERSONATE_LINUX_26, 0, 0,
  84. "Impersonate Linux 2.6"
  85. #ifndef HAVE_SYS_PERSONALITY_H
  86. " (this option has no effect in this configuration)"
  87. #endif
  88. },
  89. { "debug", GUIX_OPT_DEBUG, 0, 0,
  90. "Produce debugging output" },
  91. { 0, 0, 0, 0, 0 }
  92. };
  93. /* Parse a single option. */
  94. static error_t
  95. parse_opt (int key, char *arg, struct argp_state *state)
  96. {
  97. switch (key)
  98. {
  99. case GUIX_OPT_DISABLE_CHROOT:
  100. settings.useChroot = false;
  101. break;
  102. case GUIX_OPT_CHROOT_DIR:
  103. settings.dirsInChroot.insert (arg);
  104. break;
  105. case GUIX_OPT_DISABLE_LOG_COMPRESSION:
  106. settings.compressLog = false;
  107. break;
  108. case GUIX_OPT_BUILD_USERS_GROUP:
  109. settings.buildUsersGroup = arg;
  110. break;
  111. case GUIX_OPT_DISABLE_STORE_OPTIMIZATION:
  112. settings.autoOptimiseStore = false;
  113. break;
  114. case GUIX_OPT_CACHE_FAILURES:
  115. settings.cacheFailure = true;
  116. break;
  117. case GUIX_OPT_IMPERSONATE_LINUX_26:
  118. settings.impersonateLinux26 = true;
  119. break;
  120. case GUIX_OPT_LOSE_LOGS:
  121. settings.keepLog = false;
  122. break;
  123. case GUIX_OPT_DEBUG:
  124. verbosity = lvlDebug;
  125. break;
  126. case 'c':
  127. settings.buildCores = atoi (arg);
  128. break;
  129. case 'M':
  130. settings.maxBuildJobs = atoi (arg);
  131. break;
  132. case GUIX_OPT_SYSTEM:
  133. settings.thisSystem = arg;
  134. break;
  135. default:
  136. return ARGP_ERR_UNKNOWN;
  137. }
  138. return 0;
  139. }
  140. /* Argument parsing. */
  141. static struct argp argp = { options, parse_opt, 0, doc };
  142. int
  143. main (int argc, char *argv[])
  144. {
  145. Strings nothing;
  146. /* Initialize libgcrypt. */
  147. if (!gcry_check_version (GCRYPT_VERSION))
  148. {
  149. fprintf (stderr, "error: libgcrypt version mismatch\n");
  150. exit (EXIT_FAILURE);
  151. }
  152. #ifdef HAVE_CHROOT
  153. settings.useChroot = true;
  154. #else
  155. settings.useChroot = false;
  156. #endif
  157. argvSaved = argv;
  158. try
  159. {
  160. settings.processEnvironment ();
  161. /* FIXME: Disable substitutes until we have something that works. */
  162. settings.useSubstitutes = false;
  163. settings.substituters.clear ();
  164. argp_parse (&argp, argc, argv, 0, 0, 0);
  165. if (geteuid () == 0 && settings.buildUsersGroup.empty ())
  166. fprintf (stderr, "warning: daemon is running as root, so "
  167. "using `--build-users-group' is highly recommended\n");
  168. #ifdef HAVE_CHROOT
  169. if (settings.useChroot)
  170. {
  171. foreach (PathSet::iterator, i, settings.dirsInChroot)
  172. {
  173. printMsg (lvlDebug,
  174. format ("directory `%1%' added to the chroot") % *i);
  175. }
  176. }
  177. #endif
  178. run (nothing);
  179. }
  180. catch (std::exception &e)
  181. {
  182. fprintf (stderr, "error: %s\n", e.what ());
  183. return EXIT_FAILURE;
  184. }
  185. return EXIT_SUCCESS; /* never reached */
  186. }