Merge pull request #9494 from sellout/nix-run-execv

Don’t use `execvp` when we know the path

(cherry picked from commit dfa219d03b2277e61dde1c1ddddddff7411ef112)
Change-Id: I2226340cebbe935e1f2fe10207daba69683c8cb3
This commit is contained in:
eldritch horrors 2024-03-04 06:28:55 +01:00
parent 8505f963a1
commit 94bf5a8018
4 changed files with 15 additions and 5 deletions

View file

@ -589,7 +589,7 @@ struct CmdDevelop : Common, MixEnvironment
} }
} }
runProgramInStore(store, shell, args, buildEnvironment.getSystem()); runProgramInStore(store, UseSearchPath::Use, shell, args, buildEnvironment.getSystem());
} }
}; };

View file

@ -49,7 +49,7 @@ struct CmdFmt : SourceExprCommand {
} }
} }
runProgramInStore(store, app.program, programArgs); runProgramInStore(store, UseSearchPath::DontUse, app.program, programArgs);
}; };
}; };

View file

@ -24,6 +24,7 @@ std::string chrootHelperName = "__run_in_chroot";
namespace nix { namespace nix {
void runProgramInStore(ref<Store> store, void runProgramInStore(ref<Store> store,
UseSearchPath useSearchPath,
const std::string & program, const std::string & program,
const Strings & args, const Strings & args,
std::optional<std::string_view> system) std::optional<std::string_view> system)
@ -57,7 +58,10 @@ void runProgramInStore(ref<Store> store,
if (system) if (system)
setPersonality(*system); setPersonality(*system);
if (useSearchPath == UseSearchPath::Use)
execvp(program.c_str(), stringsToCharPtrs(args).data()); execvp(program.c_str(), stringsToCharPtrs(args).data());
else
execv(program.c_str(), stringsToCharPtrs(args).data());
throw SysError("unable to execute '%s'", program); throw SysError("unable to execute '%s'", program);
} }
@ -131,7 +135,7 @@ struct CmdShell : InstallablesCommand, MixEnvironment
Strings args; Strings args;
for (auto & arg : command) args.push_back(arg); for (auto & arg : command) args.push_back(arg);
runProgramInStore(store, *command.begin(), args); runProgramInStore(store, UseSearchPath::Use, *command.begin(), args);
} }
}; };
@ -193,7 +197,7 @@ struct CmdRun : InstallableValueCommand
Strings allArgs{app.program}; Strings allArgs{app.program};
for (auto & i : args) allArgs.push_back(i); for (auto & i : args) allArgs.push_back(i);
runProgramInStore(store, app.program, allArgs); runProgramInStore(store, UseSearchPath::DontUse, app.program, allArgs);
} }
}; };

View file

@ -5,7 +5,13 @@
namespace nix { namespace nix {
enum struct UseSearchPath {
Use,
DontUse
};
void runProgramInStore(ref<Store> store, void runProgramInStore(ref<Store> store,
UseSearchPath useSearchPath,
const std::string & program, const std::string & program,
const Strings & args, const Strings & args,
std::optional<std::string_view> system = std::nullopt); std::optional<std::string_view> system = std::nullopt);