Subprocess.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  1. /*
  2. * Copyright 2012-present Facebook, Inc.
  3. *
  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
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  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.
  15. */
  16. #ifndef _GNU_SOURCE
  17. #define _GNU_SOURCE
  18. #endif
  19. #include <folly/Subprocess.h>
  20. #if __linux__
  21. #include <sys/prctl.h>
  22. #endif
  23. #include <fcntl.h>
  24. #include <algorithm>
  25. #include <array>
  26. #include <system_error>
  27. #include <boost/container/flat_set.hpp>
  28. #include <boost/range/adaptors.hpp>
  29. #include <glog/logging.h>
  30. #include <folly/Conv.h>
  31. #include <folly/Exception.h>
  32. #include <folly/ScopeGuard.h>
  33. #include <folly/String.h>
  34. #include <folly/io/Cursor.h>
  35. #include <folly/lang/Assume.h>
  36. #include <folly/portability/Sockets.h>
  37. #include <folly/portability/Stdlib.h>
  38. #include <folly/portability/SysSyscall.h>
  39. #include <folly/portability/Unistd.h>
  40. #include <folly/system/Shell.h>
  41. constexpr int kExecFailure = 127;
  42. constexpr int kChildFailure = 126;
  43. namespace folly {
  44. ProcessReturnCode ProcessReturnCode::make(int status) {
  45. if (!WIFEXITED(status) && !WIFSIGNALED(status)) {
  46. throw std::runtime_error(
  47. to<std::string>("Invalid ProcessReturnCode: ", status));
  48. }
  49. return ProcessReturnCode(status);
  50. }
  51. ProcessReturnCode::ProcessReturnCode(ProcessReturnCode&& p) noexcept
  52. : rawStatus_(p.rawStatus_) {
  53. p.rawStatus_ = ProcessReturnCode::RV_NOT_STARTED;
  54. }
  55. ProcessReturnCode& ProcessReturnCode::operator=(
  56. ProcessReturnCode&& p) noexcept {
  57. rawStatus_ = p.rawStatus_;
  58. p.rawStatus_ = ProcessReturnCode::RV_NOT_STARTED;
  59. return *this;
  60. }
  61. ProcessReturnCode::State ProcessReturnCode::state() const {
  62. if (rawStatus_ == RV_NOT_STARTED) {
  63. return NOT_STARTED;
  64. }
  65. if (rawStatus_ == RV_RUNNING) {
  66. return RUNNING;
  67. }
  68. if (WIFEXITED(rawStatus_)) {
  69. return EXITED;
  70. }
  71. if (WIFSIGNALED(rawStatus_)) {
  72. return KILLED;
  73. }
  74. assume_unreachable();
  75. }
  76. void ProcessReturnCode::enforce(State expected) const {
  77. State s = state();
  78. if (s != expected) {
  79. throw std::logic_error(to<std::string>(
  80. "Bad use of ProcessReturnCode; state is ", s, " expected ", expected));
  81. }
  82. }
  83. int ProcessReturnCode::exitStatus() const {
  84. enforce(EXITED);
  85. return WEXITSTATUS(rawStatus_);
  86. }
  87. int ProcessReturnCode::killSignal() const {
  88. enforce(KILLED);
  89. return WTERMSIG(rawStatus_);
  90. }
  91. bool ProcessReturnCode::coreDumped() const {
  92. enforce(KILLED);
  93. return WCOREDUMP(rawStatus_);
  94. }
  95. std::string ProcessReturnCode::str() const {
  96. switch (state()) {
  97. case NOT_STARTED:
  98. return "not started";
  99. case RUNNING:
  100. return "running";
  101. case EXITED:
  102. return to<std::string>("exited with status ", exitStatus());
  103. case KILLED:
  104. return to<std::string>(
  105. "killed by signal ",
  106. killSignal(),
  107. (coreDumped() ? " (core dumped)" : ""));
  108. }
  109. assume_unreachable();
  110. }
  111. CalledProcessError::CalledProcessError(ProcessReturnCode rc)
  112. : SubprocessError(rc.str()), returnCode_(rc) {}
  113. static inline std::string toSubprocessSpawnErrorMessage(
  114. char const* executable,
  115. int errCode,
  116. int errnoValue) {
  117. auto prefix = errCode == kExecFailure ? "failed to execute "
  118. : "error preparing to execute ";
  119. return to<std::string>(prefix, executable, ": ", errnoStr(errnoValue));
  120. }
  121. SubprocessSpawnError::SubprocessSpawnError(
  122. const char* executable,
  123. int errCode,
  124. int errnoValue)
  125. : SubprocessError(
  126. toSubprocessSpawnErrorMessage(executable, errCode, errnoValue)),
  127. errnoValue_(errnoValue) {}
  128. namespace {
  129. // Copy pointers to the given strings in a format suitable for posix_spawn
  130. std::unique_ptr<const char* []> cloneStrings(
  131. const std::vector<std::string>& s) {
  132. std::unique_ptr<const char*[]> d(new const char*[s.size() + 1]);
  133. for (size_t i = 0; i < s.size(); i++) {
  134. d[i] = s[i].c_str();
  135. }
  136. d[s.size()] = nullptr;
  137. return d;
  138. }
  139. // Check a wait() status, throw on non-successful
  140. void checkStatus(ProcessReturnCode returnCode) {
  141. if (returnCode.state() != ProcessReturnCode::EXITED ||
  142. returnCode.exitStatus() != 0) {
  143. throw CalledProcessError(returnCode);
  144. }
  145. }
  146. } // namespace
  147. Subprocess::Options& Subprocess::Options::fd(int fd, int action) {
  148. if (action == Subprocess::PIPE) {
  149. if (fd == 0) {
  150. action = Subprocess::PIPE_IN;
  151. } else if (fd == 1 || fd == 2) {
  152. action = Subprocess::PIPE_OUT;
  153. } else {
  154. throw std::invalid_argument(
  155. to<std::string>("Only fds 0, 1, 2 are valid for action=PIPE: ", fd));
  156. }
  157. }
  158. fdActions_[fd] = action;
  159. return *this;
  160. }
  161. Subprocess::Subprocess() {}
  162. Subprocess::Subprocess(
  163. const std::vector<std::string>& argv,
  164. const Options& options,
  165. const char* executable,
  166. const std::vector<std::string>* env) {
  167. if (argv.empty()) {
  168. throw std::invalid_argument("argv must not be empty");
  169. }
  170. if (!executable) {
  171. executable = argv[0].c_str();
  172. }
  173. spawn(cloneStrings(argv), executable, options, env);
  174. }
  175. Subprocess::Subprocess(
  176. const std::string& cmd,
  177. const Options& options,
  178. const std::vector<std::string>* env) {
  179. if (options.usePath_) {
  180. throw std::invalid_argument("usePath() not allowed when running in shell");
  181. }
  182. std::vector<std::string> argv = {"/bin/sh", "-c", cmd};
  183. spawn(cloneStrings(argv), argv[0].c_str(), options, env);
  184. }
  185. Subprocess::~Subprocess() {
  186. CHECK_NE(returnCode_.state(), ProcessReturnCode::RUNNING)
  187. << "Subprocess destroyed without reaping child";
  188. }
  189. namespace {
  190. struct ChildErrorInfo {
  191. int errCode;
  192. int errnoValue;
  193. };
  194. [[noreturn]] void childError(int errFd, int errCode, int errnoValue) {
  195. ChildErrorInfo info = {errCode, errnoValue};
  196. // Write the error information over the pipe to our parent process.
  197. // We can't really do anything else if this write call fails.
  198. writeNoInt(errFd, &info, sizeof(info));
  199. // exit
  200. _exit(errCode);
  201. }
  202. } // namespace
  203. void Subprocess::setAllNonBlocking() {
  204. for (auto& p : pipes_) {
  205. int fd = p.pipe.fd();
  206. int flags = ::fcntl(fd, F_GETFL);
  207. checkUnixError(flags, "fcntl");
  208. int r = ::fcntl(fd, F_SETFL, flags | O_NONBLOCK);
  209. checkUnixError(r, "fcntl");
  210. }
  211. }
  212. void Subprocess::spawn(
  213. std::unique_ptr<const char*[]> argv,
  214. const char* executable,
  215. const Options& optionsIn,
  216. const std::vector<std::string>* env) {
  217. if (optionsIn.usePath_ && env) {
  218. throw std::invalid_argument(
  219. "usePath() not allowed when overriding environment");
  220. }
  221. // Make a copy, we'll mutate options
  222. Options options(optionsIn);
  223. // On error, close all pipes_ (ignoring errors, but that seems fine here).
  224. auto pipesGuard = makeGuard([this] { pipes_.clear(); });
  225. // Create a pipe to use to receive error information from the child,
  226. // in case it fails before calling exec()
  227. int errFds[2];
  228. #if FOLLY_HAVE_PIPE2
  229. checkUnixError(::pipe2(errFds, O_CLOEXEC), "pipe2");
  230. #else
  231. checkUnixError(::pipe(errFds), "pipe");
  232. #endif
  233. SCOPE_EXIT {
  234. CHECK_ERR(::close(errFds[0]));
  235. if (errFds[1] >= 0) {
  236. CHECK_ERR(::close(errFds[1]));
  237. }
  238. };
  239. #if !FOLLY_HAVE_PIPE2
  240. // Ask the child to close the read end of the error pipe.
  241. checkUnixError(fcntl(errFds[0], F_SETFD, FD_CLOEXEC), "set FD_CLOEXEC");
  242. // Set the close-on-exec flag on the write side of the pipe.
  243. // This way the pipe will be closed automatically in the child if execve()
  244. // succeeds. If the exec fails the child can write error information to the
  245. // pipe.
  246. checkUnixError(fcntl(errFds[1], F_SETFD, FD_CLOEXEC), "set FD_CLOEXEC");
  247. #endif
  248. // Perform the actual work of setting up pipes then forking and
  249. // executing the child.
  250. spawnInternal(std::move(argv), executable, options, env, errFds[1]);
  251. // After spawnInternal() returns the child is alive. We have to be very
  252. // careful about throwing after this point. We are inside the constructor,
  253. // so if we throw the Subprocess object will have never existed, and the
  254. // destructor will never be called.
  255. //
  256. // We should only throw if we got an error via the errFd, and we know the
  257. // child has exited and can be immediately waited for. In all other cases,
  258. // we have no way of cleaning up the child.
  259. // Close writable side of the errFd pipe in the parent process
  260. CHECK_ERR(::close(errFds[1]));
  261. errFds[1] = -1;
  262. // Read from the errFd pipe, to tell if the child ran into any errors before
  263. // calling exec()
  264. readChildErrorPipe(errFds[0], executable);
  265. // We have fully succeeded now, so release the guard on pipes_
  266. pipesGuard.dismiss();
  267. }
  268. // With -Wclobbered, gcc complains about vfork potentially cloberring the
  269. // childDir variable, even though we only use it on the child side of the
  270. // vfork.
  271. FOLLY_PUSH_WARNING
  272. FOLLY_GCC_DISABLE_WARNING("-Wclobbered")
  273. void Subprocess::spawnInternal(
  274. std::unique_ptr<const char*[]> argv,
  275. const char* executable,
  276. Options& options,
  277. const std::vector<std::string>* env,
  278. int errFd) {
  279. // Parent work, pre-fork: create pipes
  280. std::vector<int> childFds;
  281. // Close all of the childFds as we leave this scope
  282. SCOPE_EXIT {
  283. // These are only pipes, closing them shouldn't fail
  284. for (int cfd : childFds) {
  285. CHECK_ERR(::close(cfd));
  286. }
  287. };
  288. int r;
  289. for (auto& p : options.fdActions_) {
  290. if (p.second == PIPE_IN || p.second == PIPE_OUT) {
  291. int fds[2];
  292. // We're setting both ends of the pipe as close-on-exec. The child
  293. // doesn't need to reset the flag on its end, as we always dup2() the fd,
  294. // and dup2() fds don't share the close-on-exec flag.
  295. #if FOLLY_HAVE_PIPE2
  296. // If possible, set close-on-exec atomically. Otherwise, a concurrent
  297. // Subprocess invocation can fork() between "pipe" and "fnctl",
  298. // causing FDs to leak.
  299. r = ::pipe2(fds, O_CLOEXEC);
  300. checkUnixError(r, "pipe2");
  301. #else
  302. r = ::pipe(fds);
  303. checkUnixError(r, "pipe");
  304. r = fcntl(fds[0], F_SETFD, FD_CLOEXEC);
  305. checkUnixError(r, "set FD_CLOEXEC");
  306. r = fcntl(fds[1], F_SETFD, FD_CLOEXEC);
  307. checkUnixError(r, "set FD_CLOEXEC");
  308. #endif
  309. pipes_.emplace_back();
  310. Pipe& pipe = pipes_.back();
  311. pipe.direction = p.second;
  312. int cfd;
  313. if (p.second == PIPE_IN) {
  314. // Child gets reading end
  315. pipe.pipe = folly::File(fds[1], /*ownsFd=*/true);
  316. cfd = fds[0];
  317. } else {
  318. pipe.pipe = folly::File(fds[0], /*ownsFd=*/true);
  319. cfd = fds[1];
  320. }
  321. p.second = cfd; // ensure it gets dup2()ed
  322. pipe.childFd = p.first;
  323. childFds.push_back(cfd);
  324. }
  325. }
  326. // This should already be sorted, as options.fdActions_ is
  327. DCHECK(std::is_sorted(pipes_.begin(), pipes_.end()));
  328. // Note that the const casts below are legit, per
  329. // http://pubs.opengroup.org/onlinepubs/009695399/functions/exec.html
  330. char** argVec = const_cast<char**>(argv.get());
  331. // Set up environment
  332. std::unique_ptr<const char*[]> envHolder;
  333. char** envVec;
  334. if (env) {
  335. envHolder = cloneStrings(*env);
  336. envVec = const_cast<char**>(envHolder.get());
  337. } else {
  338. envVec = environ;
  339. }
  340. // Block all signals around vfork; see http://ewontfix.com/7/.
  341. //
  342. // As the child may run in the same address space as the parent until
  343. // the actual execve() system call, any (custom) signal handlers that
  344. // the parent has might alter parent's memory if invoked in the child,
  345. // with undefined results. So we block all signals in the parent before
  346. // vfork(), which will cause them to be blocked in the child as well (we
  347. // rely on the fact that Linux, just like all sane implementations, only
  348. // clones the calling thread). Then, in the child, we reset all signals
  349. // to their default dispositions (while still blocked), and unblock them
  350. // (so the exec()ed process inherits the parent's signal mask)
  351. //
  352. // The parent also unblocks all signals as soon as vfork() returns.
  353. sigset_t allBlocked;
  354. r = sigfillset(&allBlocked);
  355. checkUnixError(r, "sigfillset");
  356. sigset_t oldSignals;
  357. r = pthread_sigmask(SIG_SETMASK, &allBlocked, &oldSignals);
  358. checkPosixError(r, "pthread_sigmask");
  359. SCOPE_EXIT {
  360. // Restore signal mask
  361. r = pthread_sigmask(SIG_SETMASK, &oldSignals, nullptr);
  362. CHECK_EQ(r, 0) << "pthread_sigmask: " << errnoStr(r); // shouldn't fail
  363. };
  364. // Call c_str() here, as it's not necessarily safe after fork.
  365. const char* childDir =
  366. options.childDir_.empty() ? nullptr : options.childDir_.c_str();
  367. pid_t pid;
  368. #ifdef __linux__
  369. if (options.cloneFlags_) {
  370. pid = syscall(SYS_clone, *options.cloneFlags_, 0, nullptr, nullptr);
  371. checkUnixError(pid, errno, "clone");
  372. } else {
  373. #endif
  374. pid = vfork();
  375. checkUnixError(pid, errno, "vfork");
  376. #ifdef __linux__
  377. }
  378. #endif
  379. if (pid == 0) {
  380. int errnoValue = prepareChild(options, &oldSignals, childDir);
  381. if (errnoValue != 0) {
  382. childError(errFd, kChildFailure, errnoValue);
  383. }
  384. errnoValue = runChild(executable, argVec, envVec, options);
  385. // If we get here, exec() failed.
  386. childError(errFd, kExecFailure, errnoValue);
  387. }
  388. // Child is alive. We have to be very careful about throwing after this
  389. // point. We are inside the constructor, so if we throw the Subprocess
  390. // object will have never existed, and the destructor will never be called.
  391. //
  392. // We should only throw if we got an error via the errFd, and we know the
  393. // child has exited and can be immediately waited for. In all other cases,
  394. // we have no way of cleaning up the child.
  395. pid_ = pid;
  396. returnCode_ = ProcessReturnCode::makeRunning();
  397. }
  398. FOLLY_POP_WARNING
  399. int Subprocess::prepareChild(
  400. const Options& options,
  401. const sigset_t* sigmask,
  402. const char* childDir) const {
  403. // While all signals are blocked, we must reset their
  404. // dispositions to default.
  405. for (int sig = 1; sig < NSIG; ++sig) {
  406. ::signal(sig, SIG_DFL);
  407. }
  408. {
  409. // Unblock signals; restore signal mask.
  410. int r = pthread_sigmask(SIG_SETMASK, sigmask, nullptr);
  411. if (r != 0) {
  412. return r; // pthread_sigmask() returns an errno value
  413. }
  414. }
  415. // Change the working directory, if one is given
  416. if (childDir) {
  417. if (::chdir(childDir) == -1) {
  418. return errno;
  419. }
  420. }
  421. // We don't have to explicitly close the parent's end of all pipes,
  422. // as they all have the FD_CLOEXEC flag set and will be closed at
  423. // exec time.
  424. // Close all fds that we're supposed to close.
  425. for (auto& p : options.fdActions_) {
  426. if (p.second == CLOSE) {
  427. if (::close(p.first) == -1) {
  428. return errno;
  429. }
  430. } else if (p.second != p.first) {
  431. if (::dup2(p.second, p.first) == -1) {
  432. return errno;
  433. }
  434. }
  435. }
  436. // If requested, close all other file descriptors. Don't close
  437. // any fds in options.fdActions_, and don't touch stdin, stdout, stderr.
  438. // Ignore errors.
  439. if (options.closeOtherFds_) {
  440. for (int fd = getdtablesize() - 1; fd >= 3; --fd) {
  441. if (options.fdActions_.count(fd) == 0) {
  442. ::close(fd);
  443. }
  444. }
  445. }
  446. #if __linux__
  447. // Opt to receive signal on parent death, if requested
  448. if (options.parentDeathSignal_ != 0) {
  449. const auto parentDeathSignal =
  450. static_cast<unsigned long>(options.parentDeathSignal_);
  451. if (prctl(PR_SET_PDEATHSIG, parentDeathSignal, 0, 0, 0) == -1) {
  452. return errno;
  453. }
  454. }
  455. #endif
  456. if (options.processGroupLeader_) {
  457. if (setpgrp() == -1) {
  458. return errno;
  459. }
  460. }
  461. // The user callback comes last, so that the child is otherwise all set up.
  462. if (options.dangerousPostForkPreExecCallback_) {
  463. if (int error = (*options.dangerousPostForkPreExecCallback_)()) {
  464. return error;
  465. }
  466. }
  467. return 0;
  468. }
  469. int Subprocess::runChild(
  470. const char* executable,
  471. char** argv,
  472. char** env,
  473. const Options& options) const {
  474. // Now, finally, exec.
  475. if (options.usePath_) {
  476. ::execvp(executable, argv);
  477. } else {
  478. ::execve(executable, argv, env);
  479. }
  480. return errno;
  481. }
  482. void Subprocess::readChildErrorPipe(int pfd, const char* executable) {
  483. ChildErrorInfo info;
  484. auto rc = readNoInt(pfd, &info, sizeof(info));
  485. if (rc == 0) {
  486. // No data means the child executed successfully, and the pipe
  487. // was closed due to the close-on-exec flag being set.
  488. return;
  489. } else if (rc != sizeof(ChildErrorInfo)) {
  490. // An error occurred trying to read from the pipe, or we got a partial read.
  491. // Neither of these cases should really occur in practice.
  492. //
  493. // We can't get any error data from the child in this case, and we don't
  494. // know if it is successfully running or not. All we can do is to return
  495. // normally, as if the child executed successfully. If something bad
  496. // happened the caller should at least get a non-normal exit status from
  497. // the child.
  498. LOG(ERROR) << "unexpected error trying to read from child error pipe "
  499. << "rc=" << rc << ", errno=" << errno;
  500. return;
  501. }
  502. // We got error data from the child. The child should exit immediately in
  503. // this case, so wait on it to clean up.
  504. wait();
  505. // Throw to signal the error
  506. throw SubprocessSpawnError(executable, info.errCode, info.errnoValue);
  507. }
  508. ProcessReturnCode Subprocess::poll(struct rusage* ru) {
  509. returnCode_.enforce(ProcessReturnCode::RUNNING);
  510. DCHECK_GT(pid_, 0);
  511. int status;
  512. pid_t found = ::wait4(pid_, &status, WNOHANG, ru);
  513. // The spec guarantees that EINTR does not occur with WNOHANG, so the only
  514. // two remaining errors are ECHILD (other code reaped the child?), or
  515. // EINVAL (cosmic rays?), both of which merit an abort:
  516. PCHECK(found != -1) << "waitpid(" << pid_ << ", &status, WNOHANG)";
  517. if (found != 0) {
  518. // Though the child process had quit, this call does not close the pipes
  519. // since its descendants may still be using them.
  520. returnCode_ = ProcessReturnCode::make(status);
  521. pid_ = -1;
  522. }
  523. return returnCode_;
  524. }
  525. bool Subprocess::pollChecked() {
  526. if (poll().state() == ProcessReturnCode::RUNNING) {
  527. return false;
  528. }
  529. checkStatus(returnCode_);
  530. return true;
  531. }
  532. ProcessReturnCode Subprocess::wait() {
  533. returnCode_.enforce(ProcessReturnCode::RUNNING);
  534. DCHECK_GT(pid_, 0);
  535. int status;
  536. pid_t found;
  537. do {
  538. found = ::waitpid(pid_, &status, 0);
  539. } while (found == -1 && errno == EINTR);
  540. // The only two remaining errors are ECHILD (other code reaped the
  541. // child?), or EINVAL (cosmic rays?), and both merit an abort:
  542. PCHECK(found != -1) << "waitpid(" << pid_ << ", &status, WNOHANG)";
  543. // Though the child process had quit, this call does not close the pipes
  544. // since its descendants may still be using them.
  545. DCHECK_EQ(found, pid_);
  546. returnCode_ = ProcessReturnCode::make(status);
  547. pid_ = -1;
  548. return returnCode_;
  549. }
  550. void Subprocess::waitChecked() {
  551. wait();
  552. checkStatus(returnCode_);
  553. }
  554. void Subprocess::sendSignal(int signal) {
  555. returnCode_.enforce(ProcessReturnCode::RUNNING);
  556. int r = ::kill(pid_, signal);
  557. checkUnixError(r, "kill");
  558. }
  559. pid_t Subprocess::pid() const {
  560. return pid_;
  561. }
  562. namespace {
  563. ByteRange queueFront(const IOBufQueue& queue) {
  564. auto* p = queue.front();
  565. if (!p) {
  566. return ByteRange{};
  567. }
  568. return io::Cursor(p).peekBytes();
  569. }
  570. // fd write
  571. bool handleWrite(int fd, IOBufQueue& queue) {
  572. for (;;) {
  573. auto b = queueFront(queue);
  574. if (b.empty()) {
  575. return true; // EOF
  576. }
  577. ssize_t n = writeNoInt(fd, b.data(), b.size());
  578. if (n == -1 && errno == EAGAIN) {
  579. return false;
  580. }
  581. checkUnixError(n, "write");
  582. queue.trimStart(n);
  583. }
  584. }
  585. // fd read
  586. bool handleRead(int fd, IOBufQueue& queue) {
  587. for (;;) {
  588. auto p = queue.preallocate(100, 65000);
  589. ssize_t n = readNoInt(fd, p.first, p.second);
  590. if (n == -1 && errno == EAGAIN) {
  591. return false;
  592. }
  593. checkUnixError(n, "read");
  594. if (n == 0) {
  595. return true;
  596. }
  597. queue.postallocate(n);
  598. }
  599. }
  600. bool discardRead(int fd) {
  601. static const size_t bufSize = 65000;
  602. // Thread unsafe, but it doesn't matter.
  603. static std::unique_ptr<char[]> buf(new char[bufSize]);
  604. for (;;) {
  605. ssize_t n = readNoInt(fd, buf.get(), bufSize);
  606. if (n == -1 && errno == EAGAIN) {
  607. return false;
  608. }
  609. checkUnixError(n, "read");
  610. if (n == 0) {
  611. return true;
  612. }
  613. }
  614. }
  615. } // namespace
  616. std::pair<std::string, std::string> Subprocess::communicate(StringPiece input) {
  617. IOBufQueue inputQueue;
  618. inputQueue.wrapBuffer(input.data(), input.size());
  619. auto outQueues = communicateIOBuf(std::move(inputQueue));
  620. auto outBufs =
  621. std::make_pair(outQueues.first.move(), outQueues.second.move());
  622. std::pair<std::string, std::string> out;
  623. if (outBufs.first) {
  624. outBufs.first->coalesce();
  625. out.first.assign(
  626. reinterpret_cast<const char*>(outBufs.first->data()),
  627. outBufs.first->length());
  628. }
  629. if (outBufs.second) {
  630. outBufs.second->coalesce();
  631. out.second.assign(
  632. reinterpret_cast<const char*>(outBufs.second->data()),
  633. outBufs.second->length());
  634. }
  635. return out;
  636. }
  637. std::pair<IOBufQueue, IOBufQueue> Subprocess::communicateIOBuf(
  638. IOBufQueue input) {
  639. // If the user supplied a non-empty input buffer, make sure
  640. // that stdin is a pipe so we can write the data.
  641. if (!input.empty()) {
  642. // findByChildFd() will throw std::invalid_argument if no pipe for
  643. // STDIN_FILENO exists
  644. findByChildFd(STDIN_FILENO);
  645. }
  646. std::pair<IOBufQueue, IOBufQueue> out;
  647. auto readCallback = [&](int pfd, int cfd) -> bool {
  648. if (cfd == STDOUT_FILENO) {
  649. return handleRead(pfd, out.first);
  650. } else if (cfd == STDERR_FILENO) {
  651. return handleRead(pfd, out.second);
  652. } else {
  653. // Don't close the file descriptor, the child might not like SIGPIPE,
  654. // just read and throw the data away.
  655. return discardRead(pfd);
  656. }
  657. };
  658. auto writeCallback = [&](int pfd, int cfd) -> bool {
  659. if (cfd == STDIN_FILENO) {
  660. return handleWrite(pfd, input);
  661. } else {
  662. // If we don't want to write to this fd, just close it.
  663. return true;
  664. }
  665. };
  666. communicate(std::move(readCallback), std::move(writeCallback));
  667. return out;
  668. }
  669. void Subprocess::communicate(
  670. FdCallback readCallback,
  671. FdCallback writeCallback) {
  672. // This serves to prevent wait() followed by communicate(), but if you
  673. // legitimately need that, send a patch to delete this line.
  674. returnCode_.enforce(ProcessReturnCode::RUNNING);
  675. setAllNonBlocking();
  676. std::vector<pollfd> fds;
  677. fds.reserve(pipes_.size());
  678. std::vector<size_t> toClose; // indexes into pipes_
  679. toClose.reserve(pipes_.size());
  680. while (!pipes_.empty()) {
  681. fds.clear();
  682. toClose.clear();
  683. for (auto& p : pipes_) {
  684. pollfd pfd;
  685. pfd.fd = p.pipe.fd();
  686. // Yes, backwards, PIPE_IN / PIPE_OUT are defined from the
  687. // child's point of view.
  688. if (!p.enabled) {
  689. // Still keeping fd in watched set so we get notified of POLLHUP /
  690. // POLLERR
  691. pfd.events = 0;
  692. } else if (p.direction == PIPE_IN) {
  693. pfd.events = POLLOUT;
  694. } else {
  695. pfd.events = POLLIN;
  696. }
  697. fds.push_back(pfd);
  698. }
  699. int r;
  700. do {
  701. r = ::poll(fds.data(), fds.size(), -1);
  702. } while (r == -1 && errno == EINTR);
  703. checkUnixError(r, "poll");
  704. for (size_t i = 0; i < pipes_.size(); ++i) {
  705. auto& p = pipes_[i];
  706. auto parentFd = p.pipe.fd();
  707. DCHECK_EQ(fds[i].fd, parentFd);
  708. short events = fds[i].revents;
  709. bool closed = false;
  710. if (events & POLLOUT) {
  711. DCHECK(!(events & POLLIN));
  712. if (writeCallback(parentFd, p.childFd)) {
  713. toClose.push_back(i);
  714. closed = true;
  715. }
  716. }
  717. // Call read callback on POLLHUP, to give it a chance to read (and act
  718. // on) end of file
  719. if (events & (POLLIN | POLLHUP)) {
  720. DCHECK(!(events & POLLOUT));
  721. if (readCallback(parentFd, p.childFd)) {
  722. toClose.push_back(i);
  723. closed = true;
  724. }
  725. }
  726. if ((events & (POLLHUP | POLLERR)) && !closed) {
  727. toClose.push_back(i);
  728. closed = true;
  729. }
  730. }
  731. // Close the fds in reverse order so the indexes hold after erase()
  732. for (int idx : boost::adaptors::reverse(toClose)) {
  733. auto pos = pipes_.begin() + idx;
  734. pos->pipe.close(); // Throws on error
  735. pipes_.erase(pos);
  736. }
  737. }
  738. }
  739. void Subprocess::enableNotifications(int childFd, bool enabled) {
  740. pipes_[findByChildFd(childFd)].enabled = enabled;
  741. }
  742. bool Subprocess::notificationsEnabled(int childFd) const {
  743. return pipes_[findByChildFd(childFd)].enabled;
  744. }
  745. size_t Subprocess::findByChildFd(int childFd) const {
  746. auto pos = std::lower_bound(
  747. pipes_.begin(), pipes_.end(), childFd, [](const Pipe& pipe, int fd) {
  748. return pipe.childFd < fd;
  749. });
  750. if (pos == pipes_.end() || pos->childFd != childFd) {
  751. throw std::invalid_argument(
  752. folly::to<std::string>("child fd not found ", childFd));
  753. }
  754. return pos - pipes_.begin();
  755. }
  756. void Subprocess::closeParentFd(int childFd) {
  757. int idx = findByChildFd(childFd);
  758. pipes_[idx].pipe.close(); // May throw
  759. pipes_.erase(pipes_.begin() + idx);
  760. }
  761. std::vector<Subprocess::ChildPipe> Subprocess::takeOwnershipOfPipes() {
  762. std::vector<Subprocess::ChildPipe> pipes;
  763. for (auto& p : pipes_) {
  764. pipes.emplace_back(p.childFd, std::move(p.pipe));
  765. }
  766. // release memory
  767. std::vector<Pipe>().swap(pipes_);
  768. return pipes;
  769. }
  770. namespace {
  771. class Initializer {
  772. public:
  773. Initializer() {
  774. // We like EPIPE, thanks.
  775. ::signal(SIGPIPE, SIG_IGN);
  776. }
  777. };
  778. Initializer initializer;
  779. } // namespace
  780. } // namespace folly