2 * Copyright 2015 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 TEST(CommunicateSubprocessTest, SimpleRead) {
236 Subprocess proc(std::vector<std::string>{ "/bin/echo", "-n", "foo", "bar"},
237 Subprocess::pipeStdout());
238 auto p = proc.communicate();
239 EXPECT_EQ("foo bar", p.first);
243 TEST(CommunicateSubprocessTest, BigWrite) {
244 const int numLines = 1 << 20;
245 std::string line("hello\n");
247 data.reserve(numLines * line.size());
248 for (int i = 0; i < numLines; ++i) {
252 Subprocess proc("wc -l", Subprocess::pipeStdin() | Subprocess::pipeStdout());
253 auto p = proc.communicate(data);
254 EXPECT_EQ(folly::format("{}\n", numLines).str(), p.first);
258 TEST(CommunicateSubprocessTest, Duplex) {
259 // Take 10MB of data and pass them through a filter.
260 // One line, as tr is line-buffered
261 const int bytes = 10 << 20;
262 std::string line(bytes, 'x');
264 Subprocess proc("tr a-z A-Z",
265 Subprocess::pipeStdin() | Subprocess::pipeStdout());
266 auto p = proc.communicate(line);
267 EXPECT_EQ(bytes, p.first.size());
268 EXPECT_EQ(std::string::npos, p.first.find_first_not_of('X'));
272 TEST(CommunicateSubprocessTest, ProcessGroupLeader) {
273 const auto testIsLeader = "test $(cut -d ' ' -f 5 /proc/$$/stat) = $$";
274 Subprocess nonLeader(testIsLeader);
275 EXPECT_THROW(nonLeader.waitChecked(), CalledProcessError);
276 Subprocess leader(testIsLeader, Subprocess::Options().processGroupLeader());
277 leader.waitChecked();
280 TEST(CommunicateSubprocessTest, Duplex2) {
282 // Pipe 200,000 lines through sed
283 const size_t numCopies = 100000;
284 auto iobuf = IOBuf::copyBuffer("this is a test\nanother line\n");
286 for (size_t n = 0; n < numCopies; ++n) {
287 input.append(iobuf->clone());
290 std::vector<std::string> cmd({
292 "-e", "s/a test/a successful test/",
293 "-e", "/^another line/w/dev/stderr",
295 auto options = Subprocess::pipeStdin().pipeStdout().pipeStderr().usePath();
296 Subprocess proc(cmd, options);
297 auto out = proc.communicateIOBuf(std::move(input));
300 // Convert stdout and stderr to strings so we can call split() on them.
302 if (out.first.front()) {
303 stdoutStr = out.first.move()->moveToFbString();
306 if (out.second.front()) {
307 stderrStr = out.second.move()->moveToFbString();
310 // stdout should be a copy of stdin, with "a test" replaced by
311 // "a successful test"
312 std::vector<StringPiece> stdoutLines;
313 split('\n', stdoutStr, stdoutLines);
314 EXPECT_EQ(numCopies * 2 + 1, stdoutLines.size());
315 // Strip off the trailing empty line
316 if (!stdoutLines.empty()) {
317 EXPECT_EQ("", stdoutLines.back());
318 stdoutLines.pop_back();
321 for (const auto& line : stdoutLines) {
322 if ((linenum & 1) == 0) {
323 EXPECT_EQ("this is a successful test", line);
325 EXPECT_EQ("another line", line);
330 // stderr should only contain the lines containing "another line"
331 std::vector<StringPiece> stderrLines;
332 split('\n', stderrStr, stderrLines);
333 EXPECT_EQ(numCopies + 1, stderrLines.size());
334 // Strip off the trailing empty line
335 if (!stderrLines.empty()) {
336 EXPECT_EQ("", stderrLines.back());
337 stderrLines.pop_back();
339 for (const auto& line : stderrLines) {
340 EXPECT_EQ("another line", line);
347 bool readToString(int fd, std::string& buf, size_t maxSize) {
349 char* dest = &buf.front();
350 size_t remaining = maxSize;
354 n = ::read(fd, dest, remaining);
356 if (errno == EINTR) {
359 if (errno == EAGAIN) {
362 PCHECK("read failed");
370 buf.resize(dest - buf.data());
376 TEST(CommunicateSubprocessTest, Chatty) {
378 const int lineCount = 1000;
383 auto options = Subprocess::pipeStdin().pipeStdout().pipeStderr().usePath();
384 std::vector<std::string> cmd {
388 "s/a test/a successful test/",
391 Subprocess proc(cmd, options);
393 auto writeCallback = [&] (int pfd, int cfd) -> bool {
394 EXPECT_EQ(0, cfd); // child stdin
395 EXPECT_EQ(rcount, wcount); // chatty, one read for every write
397 auto msg = folly::to<std::string>("a test ", wcount, "\n");
399 // Not entirely kosher, we should handle partial writes, but this is
400 // fine for writes <= PIPE_BUF
401 EXPECT_EQ(msg.size(), writeFull(pfd, msg.data(), msg.size()));
404 proc.enableNotifications(0, false);
406 return (wcount == lineCount);
409 bool eofSeen = false;
411 auto readCallback = [&] (int pfd, int cfd) -> bool {
416 EXPECT_TRUE(readToString(pfd, lineBuf, 1));
417 EXPECT_EQ(0, lineBuf.size());
421 EXPECT_FALSE(eofSeen);
423 std::string expected;
425 if (rcount < lineCount) {
426 expected = folly::to<std::string>("a successful test ", rcount++, "\n");
429 EXPECT_EQ(wcount, rcount);
431 // Not entirely kosher, we should handle partial reads, but this is
432 // fine for reads <= PIPE_BUF
433 bool atEof = readToString(pfd, lineBuf, expected.size() + 1);
435 // EOF only expected after we finished reading
436 EXPECT_EQ(lineCount, rcount);
440 EXPECT_EQ(expected, lineBuf);
442 if (wcount != lineCount) { // still more to write...
443 proc.enableNotifications(0, true);
449 proc.communicate(readCallback, writeCallback);
451 EXPECT_EQ(lineCount, wcount);
452 EXPECT_EQ(lineCount, rcount);
453 EXPECT_TRUE(eofSeen);
455 EXPECT_EQ(0, proc.wait().exitStatus());
459 TEST(CommunicateSubprocessTest, TakeOwnershipOfPipes) {
460 std::vector<Subprocess::ChildPipe> pipes;
463 "echo $'oh\\nmy\\ncat' | wc -l &", Subprocess::pipeStdout()
465 pipes = proc.takeOwnershipOfPipes();
468 EXPECT_EQ(1, pipes.size());
469 EXPECT_EQ(1, pipes[0].childFd);
472 EXPECT_EQ(2, readFull(pipes[0].pipe.fd(), buf, 10));
474 EXPECT_EQ("3\n", std::string(buf));