2013-01-05 23:47:50 +00:00
|
|
|
|
/* GNU Guix --- Functional package management for GNU
|
2014-01-23 22:48:34 +00:00
|
|
|
|
Copyright (C) 2012, 2013, 2014 Ludovic Courtès <ludo@gnu.org>
|
2012-12-03 22:04:47 +00:00
|
|
|
|
|
2013-01-05 23:47:50 +00:00
|
|
|
|
This file is part of GNU Guix.
|
2012-12-03 22:04:47 +00:00
|
|
|
|
|
2013-01-05 23:47:50 +00:00
|
|
|
|
GNU Guix is free software; you can redistribute it and/or modify it
|
2012-12-03 22:04:47 +00:00
|
|
|
|
under the terms of the GNU General Public License as published by
|
|
|
|
|
the Free Software Foundation; either version 3 of the License, or (at
|
|
|
|
|
your option) any later version.
|
|
|
|
|
|
2013-01-05 23:47:50 +00:00
|
|
|
|
GNU Guix is distributed in the hope that it will be useful, but
|
2012-12-03 22:04:47 +00:00
|
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
2013-01-05 23:47:50 +00:00
|
|
|
|
along with GNU Guix. If not, see <http://www.gnu.org/licenses/>. */
|
2012-12-03 22:04:47 +00:00
|
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
|
|
#include <types.hh>
|
|
|
|
|
#include "shared.hh"
|
|
|
|
|
#include <globals.hh>
|
2012-12-14 23:06:09 +00:00
|
|
|
|
#include <util.hh>
|
2012-12-03 22:04:47 +00:00
|
|
|
|
|
2013-01-02 00:07:53 +00:00
|
|
|
|
#include <gcrypt.h>
|
|
|
|
|
|
2012-12-03 22:04:47 +00:00
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <argp.h>
|
2012-12-16 17:13:59 +00:00
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <sys/types.h>
|
2013-10-10 19:32:27 +00:00
|
|
|
|
#include <sys/stat.h>
|
2014-02-06 20:49:47 +00:00
|
|
|
|
#include <strings.h>
|
2012-12-16 17:28:00 +00:00
|
|
|
|
#include <exception>
|
2012-12-03 22:04:47 +00:00
|
|
|
|
|
|
|
|
|
/* Variables used by `nix-daemon.cc'. */
|
|
|
|
|
volatile ::sig_atomic_t blockInt;
|
|
|
|
|
char **argvSaved;
|
|
|
|
|
|
|
|
|
|
using namespace nix;
|
|
|
|
|
|
|
|
|
|
/* Entry point in `nix-daemon.cc'. */
|
|
|
|
|
extern void run (Strings args);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Command-line options. */
|
|
|
|
|
|
|
|
|
|
const char *argp_program_version =
|
|
|
|
|
"guix-daemon (" PACKAGE_NAME ") " PACKAGE_VERSION;
|
|
|
|
|
const char *argp_program_bug_address = PACKAGE_BUGREPORT;
|
|
|
|
|
|
|
|
|
|
static char doc[] =
|
|
|
|
|
"guix-daemon -- perform derivation builds and store accesses\
|
|
|
|
|
\v\
|
|
|
|
|
This program is a daemon meant to run in the background. It serves \
|
|
|
|
|
requests sent over a Unix-domain socket. It accesses the store, and \
|
|
|
|
|
builds derivations on behalf of its clients.";
|
|
|
|
|
|
|
|
|
|
#define GUIX_OPT_SYSTEM 1
|
|
|
|
|
#define GUIX_OPT_DISABLE_CHROOT 2
|
2012-12-06 21:21:45 +00:00
|
|
|
|
#define GUIX_OPT_BUILD_USERS_GROUP 3
|
|
|
|
|
#define GUIX_OPT_CACHE_FAILURES 4
|
|
|
|
|
#define GUIX_OPT_LOSE_LOGS 5
|
|
|
|
|
#define GUIX_OPT_DISABLE_LOG_COMPRESSION 6
|
|
|
|
|
#define GUIX_OPT_DISABLE_STORE_OPTIMIZATION 7
|
|
|
|
|
#define GUIX_OPT_IMPERSONATE_LINUX_26 8
|
2012-12-14 23:06:09 +00:00
|
|
|
|
#define GUIX_OPT_DEBUG 9
|
2012-12-16 17:09:27 +00:00
|
|
|
|
#define GUIX_OPT_CHROOT_DIR 10
|
2013-02-19 21:48:13 +00:00
|
|
|
|
#define GUIX_OPT_LISTEN 11
|
2013-04-18 20:46:34 +00:00
|
|
|
|
#define GUIX_OPT_NO_SUBSTITUTES 12
|
2014-01-23 22:48:34 +00:00
|
|
|
|
#define GUIX_OPT_NO_BUILD_HOOK 13
|
2014-02-06 20:49:47 +00:00
|
|
|
|
#define GUIX_OPT_GC_KEEP_OUTPUTS 14
|
|
|
|
|
#define GUIX_OPT_GC_KEEP_DERIVATIONS 15
|
2012-12-03 22:04:47 +00:00
|
|
|
|
|
|
|
|
|
static const struct argp_option options[] =
|
|
|
|
|
{
|
|
|
|
|
{ "system", GUIX_OPT_SYSTEM, "SYSTEM", 0,
|
|
|
|
|
"Assume SYSTEM as the current system type" },
|
2013-01-09 11:14:23 +00:00
|
|
|
|
{ "cores", 'c', "N", 0,
|
2012-12-03 22:04:47 +00:00
|
|
|
|
"Use N CPU cores to build each derivation; 0 means as many as available" },
|
|
|
|
|
{ "max-jobs", 'M', "N", 0,
|
|
|
|
|
"Allow at most N build jobs" },
|
|
|
|
|
{ "disable-chroot", GUIX_OPT_DISABLE_CHROOT, 0, 0,
|
2012-12-05 14:22:28 +00:00
|
|
|
|
"Disable chroot builds"
|
|
|
|
|
#ifndef HAVE_CHROOT
|
|
|
|
|
" (chroots are not supported in this configuration, so "
|
|
|
|
|
"this option has no effect)"
|
2012-12-16 17:09:27 +00:00
|
|
|
|
#endif
|
|
|
|
|
},
|
|
|
|
|
{ "chroot-directory", GUIX_OPT_CHROOT_DIR, "DIR", 0,
|
|
|
|
|
"Add DIR to the build chroot"
|
|
|
|
|
#ifndef HAVE_CHROOT
|
|
|
|
|
" (chroots are not supported in this configuration, so "
|
|
|
|
|
"this option has no effect)"
|
2012-12-05 14:22:28 +00:00
|
|
|
|
#endif
|
|
|
|
|
},
|
2012-12-06 21:21:45 +00:00
|
|
|
|
{ "build-users-group", GUIX_OPT_BUILD_USERS_GROUP, "GROUP", 0,
|
|
|
|
|
"Perform builds as a user of GROUP" },
|
2013-04-18 20:46:34 +00:00
|
|
|
|
{ "no-substitutes", GUIX_OPT_NO_SUBSTITUTES, 0, 0,
|
|
|
|
|
"Do not use substitutes" },
|
2014-01-23 22:48:34 +00:00
|
|
|
|
{ "no-build-hook", GUIX_OPT_NO_BUILD_HOOK, 0, 0,
|
|
|
|
|
"Do not use the 'build hook'" },
|
2012-12-06 21:21:45 +00:00
|
|
|
|
{ "cache-failures", GUIX_OPT_CACHE_FAILURES, 0, 0,
|
|
|
|
|
"Cache build failures" },
|
|
|
|
|
{ "lose-logs", GUIX_OPT_LOSE_LOGS, 0, 0,
|
|
|
|
|
"Do not keep build logs" },
|
2012-12-03 22:04:47 +00:00
|
|
|
|
{ "disable-log-compression", GUIX_OPT_DISABLE_LOG_COMPRESSION, 0, 0,
|
|
|
|
|
"Disable compression of the build logs" },
|
2012-12-06 21:21:45 +00:00
|
|
|
|
{ "disable-store-optimization", GUIX_OPT_DISABLE_STORE_OPTIMIZATION, 0, 0,
|
|
|
|
|
"Disable automatic file \"deduplication\" in the store" },
|
|
|
|
|
{ "impersonate-linux-2.6", GUIX_OPT_IMPERSONATE_LINUX_26, 0, 0,
|
|
|
|
|
"Impersonate Linux 2.6"
|
|
|
|
|
#ifndef HAVE_SYS_PERSONALITY_H
|
|
|
|
|
" (this option has no effect in this configuration)"
|
|
|
|
|
#endif
|
|
|
|
|
},
|
2014-02-06 20:49:47 +00:00
|
|
|
|
{ "gc-keep-outputs", GUIX_OPT_GC_KEEP_OUTPUTS,
|
|
|
|
|
"yes/no", OPTION_ARG_OPTIONAL,
|
|
|
|
|
"Tell whether the GC must keep outputs of live derivations" },
|
|
|
|
|
{ "gc-keep-derivations", GUIX_OPT_GC_KEEP_DERIVATIONS,
|
|
|
|
|
"yes/no", OPTION_ARG_OPTIONAL,
|
|
|
|
|
"Tell whether the GC must keep derivations corresponding \
|
|
|
|
|
to live outputs" },
|
|
|
|
|
|
2013-02-19 21:48:13 +00:00
|
|
|
|
{ "listen", GUIX_OPT_LISTEN, "SOCKET", 0,
|
|
|
|
|
"Listen for connections on SOCKET" },
|
2012-12-14 23:06:09 +00:00
|
|
|
|
{ "debug", GUIX_OPT_DEBUG, 0, 0,
|
|
|
|
|
"Produce debugging output" },
|
2012-12-03 22:04:47 +00:00
|
|
|
|
{ 0, 0, 0, 0, 0 }
|
|
|
|
|
};
|
|
|
|
|
|
2014-02-06 20:49:47 +00:00
|
|
|
|
|
|
|
|
|
/* Convert ARG to a Boolean value, or throw an error if it does not denote a
|
|
|
|
|
Boolean. */
|
|
|
|
|
static bool
|
|
|
|
|
string_to_bool (const char *arg, bool dflt = true)
|
|
|
|
|
{
|
|
|
|
|
if (arg == NULL)
|
|
|
|
|
return dflt;
|
|
|
|
|
else if (strcasecmp (arg, "yes") == 0)
|
|
|
|
|
return true;
|
|
|
|
|
else if (strcasecmp (arg, "no") == 0)
|
|
|
|
|
return false;
|
|
|
|
|
else
|
|
|
|
|
throw nix::Error (format ("'%1%': invalid Boolean value") % arg);
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-03 22:04:47 +00:00
|
|
|
|
/* Parse a single option. */
|
|
|
|
|
static error_t
|
|
|
|
|
parse_opt (int key, char *arg, struct argp_state *state)
|
|
|
|
|
{
|
|
|
|
|
switch (key)
|
|
|
|
|
{
|
|
|
|
|
case GUIX_OPT_DISABLE_CHROOT:
|
|
|
|
|
settings.useChroot = false;
|
|
|
|
|
break;
|
2012-12-16 17:09:27 +00:00
|
|
|
|
case GUIX_OPT_CHROOT_DIR:
|
|
|
|
|
settings.dirsInChroot.insert (arg);
|
|
|
|
|
break;
|
2012-12-03 22:04:47 +00:00
|
|
|
|
case GUIX_OPT_DISABLE_LOG_COMPRESSION:
|
|
|
|
|
settings.compressLog = false;
|
|
|
|
|
break;
|
2012-12-06 21:21:45 +00:00
|
|
|
|
case GUIX_OPT_BUILD_USERS_GROUP:
|
|
|
|
|
settings.buildUsersGroup = arg;
|
|
|
|
|
break;
|
|
|
|
|
case GUIX_OPT_DISABLE_STORE_OPTIMIZATION:
|
|
|
|
|
settings.autoOptimiseStore = false;
|
|
|
|
|
break;
|
|
|
|
|
case GUIX_OPT_CACHE_FAILURES:
|
|
|
|
|
settings.cacheFailure = true;
|
|
|
|
|
break;
|
|
|
|
|
case GUIX_OPT_IMPERSONATE_LINUX_26:
|
|
|
|
|
settings.impersonateLinux26 = true;
|
|
|
|
|
break;
|
|
|
|
|
case GUIX_OPT_LOSE_LOGS:
|
|
|
|
|
settings.keepLog = false;
|
|
|
|
|
break;
|
2013-02-19 21:48:13 +00:00
|
|
|
|
case GUIX_OPT_LISTEN:
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
settings.nixDaemonSocketFile = canonPath (arg);
|
|
|
|
|
}
|
|
|
|
|
catch (std::exception &e)
|
|
|
|
|
{
|
|
|
|
|
fprintf (stderr, "error: %s\n", e.what ());
|
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
|
}
|
|
|
|
|
break;
|
2013-04-18 20:46:34 +00:00
|
|
|
|
case GUIX_OPT_NO_SUBSTITUTES:
|
2014-03-27 21:08:53 +00:00
|
|
|
|
settings.set ("build-use-substitutes", "false");
|
2013-04-18 20:46:34 +00:00
|
|
|
|
break;
|
2014-01-23 22:48:34 +00:00
|
|
|
|
case GUIX_OPT_NO_BUILD_HOOK:
|
|
|
|
|
settings.useBuildHook = false;
|
|
|
|
|
break;
|
2012-12-14 23:06:09 +00:00
|
|
|
|
case GUIX_OPT_DEBUG:
|
|
|
|
|
verbosity = lvlDebug;
|
|
|
|
|
break;
|
2014-02-06 20:49:47 +00:00
|
|
|
|
case GUIX_OPT_GC_KEEP_OUTPUTS:
|
|
|
|
|
settings.gcKeepOutputs = string_to_bool (arg);
|
|
|
|
|
break;
|
|
|
|
|
case GUIX_OPT_GC_KEEP_DERIVATIONS:
|
|
|
|
|
settings.gcKeepDerivations = string_to_bool (arg);
|
|
|
|
|
break;
|
2013-01-09 11:14:23 +00:00
|
|
|
|
case 'c':
|
2014-03-27 21:08:53 +00:00
|
|
|
|
settings.set ("build-cores", arg);
|
2012-12-03 22:04:47 +00:00
|
|
|
|
break;
|
|
|
|
|
case 'M':
|
2014-03-27 21:08:53 +00:00
|
|
|
|
settings.set ("build-max-jobs", arg);
|
2012-12-03 22:04:47 +00:00
|
|
|
|
break;
|
|
|
|
|
case GUIX_OPT_SYSTEM:
|
|
|
|
|
settings.thisSystem = arg;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
2013-07-04 20:46:26 +00:00
|
|
|
|
return (error_t) ARGP_ERR_UNKNOWN;
|
2012-12-03 22:04:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-07-04 20:46:26 +00:00
|
|
|
|
return (error_t) 0;
|
2012-12-03 22:04:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Argument parsing. */
|
|
|
|
|
static struct argp argp = { options, parse_opt, 0, doc };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
main (int argc, char *argv[])
|
|
|
|
|
{
|
|
|
|
|
Strings nothing;
|
|
|
|
|
|
2013-01-02 00:07:53 +00:00
|
|
|
|
/* Initialize libgcrypt. */
|
|
|
|
|
if (!gcry_check_version (GCRYPT_VERSION))
|
|
|
|
|
{
|
|
|
|
|
fprintf (stderr, "error: libgcrypt version mismatch\n");
|
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-18 20:29:00 +00:00
|
|
|
|
/* Tell Libgcrypt that initialization has completed, as per the Libgcrypt
|
|
|
|
|
1.6.0 manual (although this does not appear to be strictly needed.) */
|
|
|
|
|
gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
|
|
|
|
|
|
2013-10-10 19:32:27 +00:00
|
|
|
|
/* Set the umask so that the daemon does not end up creating group-writable
|
|
|
|
|
files, which would lead to "suspicious ownership or permission" errors.
|
|
|
|
|
See <http://lists.gnu.org/archive/html/bug-guix/2013-07/msg00033.html>. */
|
|
|
|
|
umask (S_IWGRP | S_IWOTH);
|
|
|
|
|
|
2012-12-05 14:22:28 +00:00
|
|
|
|
#ifdef HAVE_CHROOT
|
2012-12-03 22:04:47 +00:00
|
|
|
|
settings.useChroot = true;
|
2012-12-05 14:22:28 +00:00
|
|
|
|
#else
|
|
|
|
|
settings.useChroot = false;
|
|
|
|
|
#endif
|
|
|
|
|
|
2012-12-16 17:28:00 +00:00
|
|
|
|
argvSaved = argv;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
settings.processEnvironment ();
|
2012-12-03 22:04:47 +00:00
|
|
|
|
|
2013-12-20 16:17:42 +00:00
|
|
|
|
/* Hackily help 'local-store.cc' find our 'guix-authenticate' program, which
|
|
|
|
|
is known as 'OPENSSL_PATH' here. */
|
2014-04-09 21:05:42 +00:00
|
|
|
|
std::string search_path;
|
|
|
|
|
search_path = settings.nixLibexecDir;
|
|
|
|
|
if (getenv ("PATH") != NULL)
|
|
|
|
|
{
|
|
|
|
|
search_path += ":";
|
|
|
|
|
search_path += getenv ("PATH");
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-20 16:17:42 +00:00
|
|
|
|
setenv ("PATH", search_path.c_str (), 1);
|
|
|
|
|
|
2013-04-02 08:44:20 +00:00
|
|
|
|
/* Use our substituter by default. */
|
2012-12-16 17:28:00 +00:00
|
|
|
|
settings.substituters.clear ();
|
2014-03-27 21:08:53 +00:00
|
|
|
|
settings.set ("build-use-substitutes", "true");
|
2012-12-14 22:51:20 +00:00
|
|
|
|
|
2014-01-23 22:48:34 +00:00
|
|
|
|
#ifdef HAVE_DAEMON_OFFLOAD_HOOK
|
|
|
|
|
/* Use our build hook for distributed builds by default. */
|
|
|
|
|
settings.useBuildHook = true;
|
|
|
|
|
if (getenv ("NIX_BUILD_HOOK") == NULL)
|
|
|
|
|
{
|
|
|
|
|
std::string build_hook;
|
|
|
|
|
|
|
|
|
|
build_hook = settings.nixLibexecDir + "/guix/offload";
|
|
|
|
|
setenv ("NIX_BUILD_HOOK", build_hook.c_str (), 1);
|
|
|
|
|
}
|
|
|
|
|
#else
|
|
|
|
|
/* We are not installing any build hook, so disable it. */
|
|
|
|
|
settings.useBuildHook = false;
|
|
|
|
|
#endif
|
|
|
|
|
|
2012-12-16 17:28:00 +00:00
|
|
|
|
argp_parse (&argp, argc, argv, 0, 0, 0);
|
2012-12-03 22:04:47 +00:00
|
|
|
|
|
2014-03-27 21:08:53 +00:00
|
|
|
|
/* Effect all the changes made via 'settings.set'. */
|
|
|
|
|
settings.update ();
|
|
|
|
|
|
2013-04-18 20:46:34 +00:00
|
|
|
|
if (settings.useSubstitutes)
|
|
|
|
|
{
|
|
|
|
|
string subs = getEnv ("NIX_SUBSTITUTERS", "default");
|
|
|
|
|
|
|
|
|
|
if (subs == "default")
|
2014-03-20 22:05:59 +00:00
|
|
|
|
{
|
|
|
|
|
string subst =
|
|
|
|
|
settings.nixLibexecDir + "/guix/substitute-binary";
|
|
|
|
|
setenv ("NIX_SUBSTITUTERS", subst.c_str (), 1);
|
|
|
|
|
}
|
2013-04-18 20:46:34 +00:00
|
|
|
|
}
|
2014-03-27 21:35:34 +00:00
|
|
|
|
else
|
|
|
|
|
/* Clear the substituter list to make sure nothing ever gets
|
|
|
|
|
substituted, regardless of the client's settings. */
|
|
|
|
|
setenv ("NIX_SUBSTITUTERS", "", 1);
|
|
|
|
|
|
|
|
|
|
/* Effect the $NIX_SUBSTITUTERS change. */
|
|
|
|
|
settings.update ();
|
2013-04-18 20:46:34 +00:00
|
|
|
|
|
2012-12-16 17:28:00 +00:00
|
|
|
|
if (geteuid () == 0 && settings.buildUsersGroup.empty ())
|
2013-01-09 11:16:26 +00:00
|
|
|
|
fprintf (stderr, "warning: daemon is running as root, so "
|
|
|
|
|
"using `--build-users-group' is highly recommended\n");
|
2012-12-16 17:13:59 +00:00
|
|
|
|
|
2013-01-02 17:31:40 +00:00
|
|
|
|
#ifdef HAVE_CHROOT
|
|
|
|
|
if (settings.useChroot)
|
|
|
|
|
{
|
|
|
|
|
foreach (PathSet::iterator, i, settings.dirsInChroot)
|
|
|
|
|
{
|
|
|
|
|
printMsg (lvlDebug,
|
|
|
|
|
format ("directory `%1%' added to the chroot") % *i);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2013-02-19 21:48:13 +00:00
|
|
|
|
printMsg (lvlDebug,
|
|
|
|
|
format ("listening on `%1%'") % settings.nixDaemonSocketFile);
|
|
|
|
|
|
2012-12-16 17:28:00 +00:00
|
|
|
|
run (nothing);
|
|
|
|
|
}
|
|
|
|
|
catch (std::exception &e)
|
|
|
|
|
{
|
|
|
|
|
fprintf (stderr, "error: %s\n", e.what ());
|
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return EXIT_SUCCESS; /* never reached */
|
2012-12-03 22:04:47 +00:00
|
|
|
|
}
|