Commit 8f27d856 authored by Dominik Gabi's avatar Dominik Gabi Committed by Facebook Github Bot 8

deprecate `folly::Subprocess(std::string, ...)`

Summary:
introducing `Subprocess::shellify` to get around this. Still thinking
about how to make sure people escape arguments passing into that function. I'd
like to have something along the lines of Phabricator's `csprintf` here.

Reviewed By: yfeldblum

Differential Revision: D3857827

fbshipit-source-id: 8afbc9f1c62c62e0fc91782e11b808145b370933
parent 3272dfdb
...@@ -182,17 +182,25 @@ Subprocess::Subprocess( ...@@ -182,17 +182,25 @@ Subprocess::Subprocess(
if (options.usePath_) { if (options.usePath_) {
throw std::invalid_argument("usePath() not allowed when running in shell"); throw std::invalid_argument("usePath() not allowed when running in shell");
} }
auto argv = Subprocess::shellify(cmd);
spawn(cloneStrings(argv), argv[0].c_str(), options, env);
}
/* static */ std::vector<std::string> Subprocess::shellify(
const std::string& cmd) {
std::vector<std::string> argv;
const char* shell = getenv("SHELL"); const char* shell = getenv("SHELL");
if (!shell) { if (!shell) {
shell = "/bin/sh"; shell = "/bin/sh";
} }
std::unique_ptr<const char*[]> argv(new const char*[4]); argv.push_back(shell);
argv[0] = shell; argv.push_back("-c");
argv[1] = "-c"; argv.push_back(cmd);
argv[2] = cmd.c_str();
argv[3] = nullptr; return argv;
spawn(std::move(argv), shell, options, env);
} }
Subprocess::~Subprocess() { Subprocess::~Subprocess() {
......
...@@ -456,11 +456,14 @@ class Subprocess { ...@@ -456,11 +456,14 @@ class Subprocess {
* The shell to use is taken from the environment variable $SHELL, * The shell to use is taken from the environment variable $SHELL,
* or /bin/sh if $SHELL is unset. * or /bin/sh if $SHELL is unset.
*/ */
FOLLY_DEPRECATED("Prefer not running in a shell or use `shellify`.")
explicit Subprocess( explicit Subprocess(
const std::string& cmd, const std::string& cmd,
const Options& options = Options(), const Options& options = Options(),
const std::vector<std::string>* env = nullptr); const std::vector<std::string>* env = nullptr);
static std::vector<std::string> shellify(const std::string& cmd);
//// ////
//// The methods below only manipulate the process state, and do not //// The methods below only manipulate the process state, and do not
//// affect its communication pipes. //// affect its communication pipes.
......
...@@ -531,3 +531,9 @@ TEST(CommunicateSubprocessTest, TakeOwnershipOfPipes) { ...@@ -531,3 +531,9 @@ TEST(CommunicateSubprocessTest, TakeOwnershipOfPipes) {
buf[2] = 0; buf[2] = 0;
EXPECT_EQ("3\n", std::string(buf)); EXPECT_EQ("3\n", std::string(buf));
} }
TEST(Subprocess, Shellify) {
auto argv = Subprocess::shellify("rm -rf /");
EXPECT_EQ(argv[1], "-c");
EXPECT_EQ(argv[2], "rm -rf /");
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment