2 * Copyright 2016 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <folly/Subprocess.h>
20 #include <sys/types.h>
23 #include <boost/container/flat_set.hpp>
24 #include <glog/logging.h>
25 #include <gtest/gtest.h>
27 #include <folly/Exception.h>
28 #include <folly/Format.h>
29 #include <folly/FileUtil.h>
30 #include <folly/String.h>
31 #include <folly/gen/Base.h>
32 #include <folly/gen/File.h>
33 #include <folly/gen/String.h>
34 #include <folly/experimental/TestUtil.h>
35 #include <folly/experimental/io/FsUtil.h>
37 using namespace folly;
39 TEST(SimpleSubprocessTest, ExitsSuccessfully) {
40 Subprocess proc(std::vector<std::string>{ "/bin/true" });
41 EXPECT_EQ(0, proc.wait().exitStatus());
44 TEST(SimpleSubprocessTest, ExitsSuccessfullyChecked) {
45 Subprocess proc(std::vector<std::string>{ "/bin/true" });
49 TEST(SimpleSubprocessTest, ExitsWithError) {
50 Subprocess proc(std::vector<std::string>{ "/bin/false" });
51 EXPECT_EQ(1, proc.wait().exitStatus());
54 TEST(SimpleSubprocessTest, ExitsWithErrorChecked) {
55 Subprocess proc(std::vector<std::string>{ "/bin/false" });
56 EXPECT_THROW(proc.waitChecked(), CalledProcessError);
59 TEST(SimpleSubprocessTest, DefaultConstructibleProcessReturnCode) {
60 ProcessReturnCode retcode;
61 EXPECT_TRUE(retcode.notStarted());
64 TEST(SimpleSubprocessTest, MoveSubprocess) {
65 Subprocess old_proc(std::vector<std::string>{ "/bin/true" });
66 EXPECT_TRUE(old_proc.returnCode().running());
67 auto new_proc = std::move(old_proc);
68 EXPECT_TRUE(old_proc.returnCode().notStarted());
69 EXPECT_TRUE(new_proc.returnCode().running());
70 EXPECT_EQ(0, new_proc.wait().exitStatus());
71 // Now old_proc is destroyed, but we don't crash.
74 #define EXPECT_SPAWN_ERROR(err, errMsg, cmd, ...) \
77 Subprocess proc(std::vector<std::string>{ (cmd), ## __VA_ARGS__ }); \
78 ADD_FAILURE() << "expected an error when running " << (cmd); \
79 } catch (const SubprocessSpawnError& ex) { \
80 EXPECT_EQ((err), ex.errnoValue()); \
81 if (StringPiece(ex.what()).find(errMsg) == StringPiece::npos) { \
82 ADD_FAILURE() << "failed to find \"" << (errMsg) << \
83 "\" in exception: \"" << ex.what() << "\""; \
88 TEST(SimpleSubprocessTest, ExecFails) {
89 EXPECT_SPAWN_ERROR(ENOENT, "failed to execute /no/such/file:",
91 EXPECT_SPAWN_ERROR(EACCES, "failed to execute /etc/passwd:",
93 EXPECT_SPAWN_ERROR(ENOTDIR, "failed to execute /etc/passwd/not/a/file:",
94 "/etc/passwd/not/a/file");
97 TEST(SimpleSubprocessTest, ShellExitsSuccesssfully) {
98 Subprocess proc("true");
99 EXPECT_EQ(0, proc.wait().exitStatus());
102 TEST(SimpleSubprocessTest, ShellExitsWithError) {
103 Subprocess proc("false");
104 EXPECT_EQ(1, proc.wait().exitStatus());
107 TEST(SimpleSubprocessTest, ChangeChildDirectorySuccessfully) {
108 // The filesystem root normally lacks a 'true' binary
109 EXPECT_EQ(0, chdir("/"));
110 EXPECT_SPAWN_ERROR(ENOENT, "failed to execute ./true", "./true");
111 // The child can fix that by moving to /bin before exec().
112 Subprocess proc("./true", Subprocess::Options().chdir("/bin"));
113 EXPECT_EQ(0, proc.wait().exitStatus());
116 TEST(SimpleSubprocessTest, ChangeChildDirectoryWithError) {
119 std::vector<std::string>{"/bin/true"},
120 Subprocess::Options().chdir("/usually/this/is/not/a/valid/directory/")
122 ADD_FAILURE() << "expected to fail when changing the child's directory";
123 } catch (const SubprocessSpawnError& ex) {
124 EXPECT_EQ(ENOENT, ex.errnoValue());
125 const std::string expectedError =
126 "error preparing to execute /bin/true: No such file or directory";
127 if (StringPiece(ex.what()).find(expectedError) == StringPiece::npos) {
128 ADD_FAILURE() << "failed to find \"" << expectedError <<
129 "\" in exception: \"" << ex.what() << "\"";
135 boost::container::flat_set<int> getOpenFds() {
137 auto dirname = to<std::string>("/proc/", pid, "/fd");
139 boost::container::flat_set<int> fds;
140 for (fs::directory_iterator it(dirname);
141 it != fs::directory_iterator();
143 int fd = to<int>(it->path().filename().native());
149 template<class Runnable>
150 void checkFdLeak(const Runnable& r) {
151 // Get the currently open fds. Check that they are the same both before and
152 // after calling the specified function. We read the open fds from /proc.
153 // (If we wanted to work even on systems that don't have /proc, we could
154 // perhaps create and immediately close a socket both before and after
155 // running the function, and make sure we got the same fd number both times.)
156 auto fdsBefore = getOpenFds();
158 auto fdsAfter = getOpenFds();
159 EXPECT_EQ(fdsAfter.size(), fdsBefore.size());
163 // Make sure Subprocess doesn't leak any file descriptors
164 TEST(SimpleSubprocessTest, FdLeakTest) {
167 Subprocess proc("true");
168 EXPECT_EQ(0, proc.wait().exitStatus());
170 // Normal execution with pipes
172 Subprocess proc("echo foo; echo bar >&2",
173 Subprocess::pipeStdout() | Subprocess::pipeStderr());
174 auto p = proc.communicate();
175 EXPECT_EQ("foo\n", p.first);
176 EXPECT_EQ("bar\n", p.second);
180 // Test where the exec call fails()
182 EXPECT_SPAWN_ERROR(ENOENT, "failed to execute", "/no/such/file");
184 // Test where the exec call fails() with pipes
187 Subprocess proc(std::vector<std::string>({"/no/such/file"}),
188 Subprocess::pipeStdout().stderr(Subprocess::PIPE));
189 ADD_FAILURE() << "expected an error when running /no/such/file";
190 } catch (const SubprocessSpawnError& ex) {
191 EXPECT_EQ(ENOENT, ex.errnoValue());
196 TEST(ParentDeathSubprocessTest, ParentDeathSignal) {
197 // Find out where we are.
198 const auto basename = "subprocess_test_parent_death_helper";
199 auto helper = fs::executable_path();
200 helper.remove_filename() /= basename;
201 if (!fs::exists(helper)) {
202 helper = helper.parent_path().parent_path() / basename / basename;
205 fs::path tempFile(fs::temp_directory_path() / fs::unique_path());
207 std::vector<std::string> args {helper.string(), tempFile.string()};
208 Subprocess proc(args);
209 // The helper gets killed by its child, see details in
210 // SubprocessTestParentDeathHelper.cpp
211 ASSERT_EQ(SIGKILL, proc.wait().killSignal());
213 // Now wait for the file to be created, see details in
214 // SubprocessTestParentDeathHelper.cpp
215 while (!fs::exists(tempFile)) {
216 usleep(20000); // 20ms
219 fs::remove(tempFile);
222 TEST(PopenSubprocessTest, PopenRead) {
223 Subprocess proc("ls /", Subprocess::pipeStdout());
225 gen::byLine(File(proc.stdout())) |
226 [&] (StringPiece line) {
227 if (line == "etc" || line == "bin" || line == "usr") {
235 // DANGER: This class runs after fork in a child processes. Be fast, the
236 // parent thread is waiting, but remember that other parent threads are
237 // running and may mutate your state. Avoid mutating any data belonging to
238 // the parent. Avoid interacting with non-POD data that originated in the
239 // parent. Avoid any libraries that may internally reference non-POD data.
240 // Especially beware parent mutexes -- for example, glog's LOG() uses one.
241 struct WriteFileAfterFork
242 : public Subprocess::DangerousPostForkPreExecCallback {
243 explicit WriteFileAfterFork(std::string filename)
244 : filename_(std::move(filename)) {}
245 virtual ~WriteFileAfterFork() {}
246 int operator()() override {
247 return writeFile(std::string("ok"), filename_.c_str()) ? 0 : errno;
249 const std::string filename_;
252 TEST(AfterForkCallbackSubprocessTest, TestAfterForkCallbackSuccess) {
253 test::ChangeToTempDir td;
254 // Trigger a file write from the child.
255 WriteFileAfterFork write_cob("good_file");
257 std::vector<std::string>{"/bin/echo"},
258 Subprocess::Options().dangerousPostForkPreExecCallback(&write_cob)
260 // The file gets written immediately.
262 EXPECT_TRUE(readFile(write_cob.filename_.c_str(), s));
267 TEST(AfterForkCallbackSubprocessTest, TestAfterForkCallbackError) {
268 test::ChangeToTempDir td;
269 // The child will try to write to a file, whose directory does not exist.
270 WriteFileAfterFork write_cob("bad/file");
273 std::vector<std::string>{"/bin/echo"},
274 Subprocess::Options().dangerousPostForkPreExecCallback(&write_cob)
278 EXPECT_FALSE(fs::exists(write_cob.filename_));
281 TEST(CommunicateSubprocessTest, SimpleRead) {
282 Subprocess proc(std::vector<std::string>{ "/bin/echo", "-n", "foo", "bar"},
283 Subprocess::pipeStdout());
284 auto p = proc.communicate();
285 EXPECT_EQ("foo bar", p.first);
289 TEST(CommunicateSubprocessTest, BigWrite) {
290 const int numLines = 1 << 20;
291 std::string line("hello\n");
293 data.reserve(numLines * line.size());
294 for (int i = 0; i < numLines; ++i) {
298 Subprocess proc("wc -l", Subprocess::pipeStdin() | Subprocess::pipeStdout());
299 auto p = proc.communicate(data);
300 EXPECT_EQ(folly::format("{}\n", numLines).str(), p.first);
304 TEST(CommunicateSubprocessTest, Duplex) {
305 // Take 10MB of data and pass them through a filter.
306 // One line, as tr is line-buffered
307 const int bytes = 10 << 20;
308 std::string line(bytes, 'x');
310 Subprocess proc("tr a-z A-Z",
311 Subprocess::pipeStdin() | Subprocess::pipeStdout());
312 auto p = proc.communicate(line);
313 EXPECT_EQ(bytes, p.first.size());
314 EXPECT_EQ(std::string::npos, p.first.find_first_not_of('X'));
318 TEST(CommunicateSubprocessTest, ProcessGroupLeader) {
319 const auto testIsLeader = "test $(cut -d ' ' -f 5 /proc/$$/stat) = $$";
320 Subprocess nonLeader(testIsLeader);
321 EXPECT_THROW(nonLeader.waitChecked(), CalledProcessError);
322 Subprocess leader(testIsLeader, Subprocess::Options().processGroupLeader());
323 leader.waitChecked();
326 TEST(CommunicateSubprocessTest, Duplex2) {
328 // Pipe 200,000 lines through sed
329 const size_t numCopies = 100000;
330 auto iobuf = IOBuf::copyBuffer("this is a test\nanother line\n");
332 for (size_t n = 0; n < numCopies; ++n) {
333 input.append(iobuf->clone());
336 std::vector<std::string> cmd({
338 "-e", "s/a test/a successful test/",
339 "-e", "/^another line/w/dev/stderr",
341 auto options = Subprocess::pipeStdin().pipeStdout().pipeStderr().usePath();
342 Subprocess proc(cmd, options);
343 auto out = proc.communicateIOBuf(std::move(input));
346 // Convert stdout and stderr to strings so we can call split() on them.
348 if (out.first.front()) {
349 stdoutStr = out.first.move()->moveToFbString();
352 if (out.second.front()) {
353 stderrStr = out.second.move()->moveToFbString();
356 // stdout should be a copy of stdin, with "a test" replaced by
357 // "a successful test"
358 std::vector<StringPiece> stdoutLines;
359 split('\n', stdoutStr, stdoutLines);
360 EXPECT_EQ(numCopies * 2 + 1, stdoutLines.size());
361 // Strip off the trailing empty line
362 if (!stdoutLines.empty()) {
363 EXPECT_EQ("", stdoutLines.back());
364 stdoutLines.pop_back();
367 for (const auto& line : stdoutLines) {
368 if ((linenum & 1) == 0) {
369 EXPECT_EQ("this is a successful test", line);
371 EXPECT_EQ("another line", line);
376 // stderr should only contain the lines containing "another line"
377 std::vector<StringPiece> stderrLines;
378 split('\n', stderrStr, stderrLines);
379 EXPECT_EQ(numCopies + 1, stderrLines.size());
380 // Strip off the trailing empty line
381 if (!stderrLines.empty()) {
382 EXPECT_EQ("", stderrLines.back());
383 stderrLines.pop_back();
385 for (const auto& line : stderrLines) {
386 EXPECT_EQ("another line", line);
393 bool readToString(int fd, std::string& buf, size_t maxSize) {
395 char* dest = &buf.front();
396 size_t remaining = maxSize;
400 n = ::read(fd, dest, remaining);
402 if (errno == EINTR) {
405 if (errno == EAGAIN) {
408 PCHECK("read failed");
416 buf.resize(dest - buf.data());
422 TEST(CommunicateSubprocessTest, Chatty) {
424 const int lineCount = 1000;
429 auto options = Subprocess::pipeStdin().pipeStdout().pipeStderr().usePath();
430 std::vector<std::string> cmd {
434 "s/a test/a successful test/",
437 Subprocess proc(cmd, options);
439 auto writeCallback = [&] (int pfd, int cfd) -> bool {
440 EXPECT_EQ(0, cfd); // child stdin
441 EXPECT_EQ(rcount, wcount); // chatty, one read for every write
443 auto msg = folly::to<std::string>("a test ", wcount, "\n");
445 // Not entirely kosher, we should handle partial writes, but this is
446 // fine for writes <= PIPE_BUF
447 EXPECT_EQ(msg.size(), writeFull(pfd, msg.data(), msg.size()));
450 proc.enableNotifications(0, false);
452 return (wcount == lineCount);
455 bool eofSeen = false;
457 auto readCallback = [&] (int pfd, int cfd) -> bool {
462 EXPECT_TRUE(readToString(pfd, lineBuf, 1));
463 EXPECT_EQ(0, lineBuf.size());
467 EXPECT_FALSE(eofSeen);
469 std::string expected;
471 if (rcount < lineCount) {
472 expected = folly::to<std::string>("a successful test ", rcount++, "\n");
475 EXPECT_EQ(wcount, rcount);
477 // Not entirely kosher, we should handle partial reads, but this is
478 // fine for reads <= PIPE_BUF
479 bool atEof = readToString(pfd, lineBuf, expected.size() + 1);
481 // EOF only expected after we finished reading
482 EXPECT_EQ(lineCount, rcount);
486 EXPECT_EQ(expected, lineBuf);
488 if (wcount != lineCount) { // still more to write...
489 proc.enableNotifications(0, true);
495 proc.communicate(readCallback, writeCallback);
497 EXPECT_EQ(lineCount, wcount);
498 EXPECT_EQ(lineCount, rcount);
499 EXPECT_TRUE(eofSeen);
501 EXPECT_EQ(0, proc.wait().exitStatus());
505 TEST(CommunicateSubprocessTest, TakeOwnershipOfPipes) {
506 std::vector<Subprocess::ChildPipe> pipes;
509 "echo $'oh\\nmy\\ncat' | wc -l &", Subprocess::pipeStdout()
511 pipes = proc.takeOwnershipOfPipes();
514 EXPECT_EQ(1, pipes.size());
515 EXPECT_EQ(1, pipes[0].childFd);
518 EXPECT_EQ(2, readFull(pipes[0].pipe.fd(), buf, 10));
520 EXPECT_EQ("3\n", std::string(buf));