From ea89df2b76811505239b508a570ac9c0ea591038 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Fri, 9 Nov 2012 18:00:33 +0100 Subject: Use vfork() instead of fork() if available MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Hopefully this reduces the chance of hitting ‘unable to fork: Cannot allocate memory’ errors. vfork() is used for everything except starting builders. --- src/libstore/build.cc | 27 +++++++++++++-------------- src/libstore/local-store.cc | 21 ++++++++++++++++----- src/libstore/local-store.hh | 4 ++++ 3 files changed, 33 insertions(+), 19 deletions(-) (limited to 'src/libstore') diff --git a/src/libstore/build.cc b/src/libstore/build.cc index 6def0c1c54..3e67e55d4a 100644 --- a/src/libstore/build.cc +++ b/src/libstore/build.cc @@ -665,7 +665,7 @@ HookInstance::HookInstance() builderOut.create(); /* Fork the hook. */ - pid = fork(); + pid = maybeVfork(); switch (pid) { case -1: @@ -2662,8 +2662,19 @@ void SubstitutionGoal::tryToRun() if (pathExists(destPath)) deletePathWrapped(destPath); + worker.store.setSubstituterEnv(); + + /* Fill in the arguments. */ + Strings args; + args.push_back(baseNameOf(sub)); + args.push_back("--substitute"); + args.push_back(storePath); + args.push_back(destPath); + const char * * argArr = strings2CharPtrs(args); + /* Fork the substitute program. */ - pid = fork(); + pid = maybeVfork(); + switch (pid) { case -1: @@ -2677,18 +2688,6 @@ void SubstitutionGoal::tryToRun() if (dup2(outPipe.writeSide, STDOUT_FILENO) == -1) throw SysError("cannot dup output pipe into stdout"); - /* Pass configuration options (including those overriden - with --option) to the substituter. */ - setenv("_NIX_OPTIONS", settings.pack().c_str(), 1); - - /* Fill in the arguments. */ - Strings args; - args.push_back(baseNameOf(sub)); - args.push_back("--substitute"); - args.push_back(storePath); - args.push_back(destPath); - const char * * argArr = strings2CharPtrs(args); - execv(sub.c_str(), (char * *) argArr); throw SysError(format("executing `%1%'") % sub); diff --git a/src/libstore/local-store.cc b/src/libstore/local-store.cc index d6cdd10d6f..b4fc64d712 100644 --- a/src/libstore/local-store.cc +++ b/src/libstore/local-store.cc @@ -202,6 +202,7 @@ void checkStoreNotSymlink() LocalStore::LocalStore(bool reserveSpace) + : didSetSubstituterEnv(false) { schemaPath = settings.nixDBPath + "/schema"; @@ -943,6 +944,18 @@ Path LocalStore::queryPathFromHashPart(const string & hashPart) } +void LocalStore::setSubstituterEnv() +{ + if (didSetSubstituterEnv) return; + + /* Pass configuration options (including those overriden with + --option) to substituters. */ + setenv("_NIX_OPTIONS", settings.pack().c_str(), 1); + + didSetSubstituterEnv = true; +} + + void LocalStore::startSubstituter(const Path & substituter, RunningSubstituter & run) { if (run.pid != -1) return; @@ -955,7 +968,9 @@ void LocalStore::startSubstituter(const Path & substituter, RunningSubstituter & fromPipe.create(); errorPipe.create(); - run.pid = fork(); + setSubstituterEnv(); + + run.pid = maybeVfork(); switch (run.pid) { @@ -964,10 +979,6 @@ void LocalStore::startSubstituter(const Path & substituter, RunningSubstituter & case 0: /* child */ try { - /* Pass configuration options (including those overriden - with --option) to the substituter. */ - setenv("_NIX_OPTIONS", settings.pack().c_str(), 1); - if (dup2(toPipe.readSide, STDIN_FILENO) == -1) throw SysError("dupping stdin"); if (dup2(fromPipe.writeSide, STDOUT_FILENO) == -1) diff --git a/src/libstore/local-store.hh b/src/libstore/local-store.hh index 8d745cfb83..ebf3f6e2b4 100644 --- a/src/libstore/local-store.hh +++ b/src/libstore/local-store.hh @@ -208,6 +208,8 @@ public: void markContentsGood(const Path & path); + void setSubstituterEnv(); + private: Path schemaPath; @@ -238,6 +240,8 @@ private: /* Cache for pathContentsGood(). */ std::map pathContentsGoodCache; + bool didSetSubstituterEnv; + int getSchema(); void openDB(bool create); -- cgit 1.4.1