2 * Copyright 2013 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"
19 #include <sys/prctl.h>
27 #include <system_error>
29 #include <boost/container/flat_set.hpp>
30 #include <boost/range/adaptors.hpp>
32 #include <glog/logging.h>
34 #include "folly/Conv.h"
35 #include "folly/Exception.h"
36 #include "folly/ScopeGuard.h"
37 #include "folly/String.h"
38 #include "folly/io/Cursor.h"
40 extern char** environ;
42 constexpr int kExecFailure = 127;
43 constexpr int kChildFailure = 126;
47 ProcessReturnCode::State ProcessReturnCode::state() const {
48 if (rawStatus_ == RV_NOT_STARTED) return NOT_STARTED;
49 if (rawStatus_ == RV_RUNNING) return RUNNING;
50 if (WIFEXITED(rawStatus_)) return EXITED;
51 if (WIFSIGNALED(rawStatus_)) return KILLED;
52 throw std::runtime_error(to<std::string>(
53 "Invalid ProcessReturnCode: ", rawStatus_));
56 void ProcessReturnCode::enforce(State s) const {
58 throw std::logic_error(to<std::string>("Invalid state ", s));
62 int ProcessReturnCode::exitStatus() const {
64 return WEXITSTATUS(rawStatus_);
67 int ProcessReturnCode::killSignal() const {
69 return WTERMSIG(rawStatus_);
72 bool ProcessReturnCode::coreDumped() const {
74 return WCOREDUMP(rawStatus_);
77 std::string ProcessReturnCode::str() const {
84 return to<std::string>("exited with status ", exitStatus());
86 return to<std::string>("killed by signal ", killSignal(),
87 (coreDumped() ? " (core dumped)" : ""));
89 CHECK(false); // unreached
92 CalledProcessError::CalledProcessError(ProcessReturnCode rc)
94 what_(returnCode_.str()) {
99 // Copy pointers to the given strings in a format suitable for posix_spawn
100 std::unique_ptr<const char*[]> cloneStrings(const std::vector<std::string>& s) {
101 std::unique_ptr<const char*[]> d(new const char*[s.size() + 1]);
102 for (int i = 0; i < s.size(); i++) {
105 d[s.size()] = nullptr;
109 // Check a wait() status, throw on non-successful
110 void checkStatus(ProcessReturnCode returnCode) {
111 if (returnCode.state() != ProcessReturnCode::EXITED ||
112 returnCode.exitStatus() != 0) {
113 throw CalledProcessError(returnCode);
119 Subprocess::Options& Subprocess::Options::fd(int fd, int action) {
120 if (action == Subprocess::PIPE) {
122 action = Subprocess::PIPE_IN;
123 } else if (fd == 1 || fd == 2) {
124 action = Subprocess::PIPE_OUT;
126 throw std::invalid_argument(
127 to<std::string>("Only fds 0, 1, 2 are valid for action=PIPE: ", fd));
130 fdActions_[fd] = action;
134 Subprocess::Subprocess(
135 const std::vector<std::string>& argv,
136 const Options& options,
137 const char* executable,
138 const std::vector<std::string>* env)
140 returnCode_(RV_NOT_STARTED) {
142 throw std::invalid_argument("argv must not be empty");
144 if (!executable) executable = argv[0].c_str();
145 spawn(cloneStrings(argv), executable, options, env);
148 Subprocess::Subprocess(
149 const std::string& cmd,
150 const Options& options,
151 const std::vector<std::string>* env)
153 returnCode_(RV_NOT_STARTED) {
154 if (options.usePath_) {
155 throw std::invalid_argument("usePath() not allowed when running in shell");
157 const char* shell = getenv("SHELL");
162 std::unique_ptr<const char*[]> argv(new const char*[4]);
165 argv[2] = cmd.c_str();
167 spawn(std::move(argv), shell, options, env);
170 Subprocess::~Subprocess() {
171 CHECK_NE(returnCode_.state(), ProcessReturnCode::RUNNING)
172 << "Subprocess destroyed without reaping child";
176 void closeChecked(int fd) {
177 checkUnixError(::close(fd), "close");
181 void Subprocess::closeAll() {
182 for (auto& p : pipes_) {
183 closeChecked(p.parentFd);
188 void Subprocess::setAllNonBlocking() {
189 for (auto& p : pipes_) {
191 int flags = ::fcntl(fd, F_GETFL);
192 checkUnixError(flags, "fcntl");
193 int r = ::fcntl(fd, F_SETFL, flags | O_NONBLOCK);
194 checkUnixError(r, "fcntl");
198 void Subprocess::spawn(
199 std::unique_ptr<const char*[]> argv,
200 const char* executable,
201 const Options& optionsIn,
202 const std::vector<std::string>* env) {
203 if (optionsIn.usePath_ && env) {
204 throw std::invalid_argument(
205 "usePath() not allowed when overriding environment");
208 // Make a copy, we'll mutate options
209 Options options(optionsIn);
211 // Parent work, pre-fork: create pipes
212 std::vector<int> childFds;
214 // If we throw, don't leak file descriptors
215 auto guard = makeGuard([&] {
216 // These are only pipes, closing them shouldn't fail
217 for (int cfd : childFds) {
218 CHECK_ERR(::close(cfd));
220 for (auto& p : this->pipes_) {
221 CHECK_ERR(::close(p.parentFd));
225 for (auto& p : options.fdActions_) {
226 if (p.second == PIPE_IN || p.second == PIPE_OUT) {
229 checkUnixError(r, "pipe");
231 pinfo.direction = p.second;
233 if (p.second == PIPE_IN) {
234 // Child gets reading end
235 pinfo.parentFd = fds[1];
238 pinfo.parentFd = fds[0];
241 p.second = cfd; // ensure it gets dup2()ed
242 pinfo.childFd = p.first;
243 childFds.push_back(cfd);
244 pipes_.push_back(pinfo);
248 // This should already be sorted, as options.fdActions_ is
249 DCHECK(std::is_sorted(pipes_.begin(), pipes_.end()));
251 // Note that the const casts below are legit, per
252 // http://pubs.opengroup.org/onlinepubs/009695399/functions/exec.html
254 char** argVec = const_cast<char**>(argv.get());
256 // Set up environment
257 std::unique_ptr<const char*[]> envHolder;
260 envHolder = cloneStrings(*env);
261 envVec = const_cast<char**>(envHolder.get());
266 // Block all signals around vfork; see http://ewontfix.com/7/.
268 // As the child may run in the same address space as the parent until
269 // the actual execve() system call, any (custom) signal handlers that
270 // the parent has might alter parent's memory if invoked in the child,
271 // with undefined results. So we block all signals in the parent before
272 // vfork(), which will cause them to be blocked in the child as well (we
273 // rely on the fact that Linux, just like all sane implementations, only
274 // clones the calling thread). Then, in the child, we reset all signals
275 // to their default dispositions (while still blocked), and unblock them
276 // (so the exec()ed process inherits the parent's signal mask)
278 // The parent also unblocks all signals as soon as vfork() returns.
280 int r = ::sigfillset(&allBlocked);
281 checkUnixError(r, "sigfillset");
283 r = pthread_sigmask(SIG_SETMASK, &allBlocked, &oldSignals);
284 checkPosixError(r, "pthread_sigmask");
288 // While all signals are blocked, we must reset their
289 // dispositions to default.
290 for (int sig = 1; sig < NSIG; ++sig) {
291 ::signal(sig, SIG_DFL);
293 // Unblock signals; restore signal mask.
294 int r = pthread_sigmask(SIG_SETMASK, &oldSignals, nullptr);
295 if (r != 0) _exit(kChildFailure);
297 runChild(executable, argVec, envVec, options);
298 // This should never return, but there's nothing else we can do here.
301 // In parent. We want to restore the signal mask even if vfork fails,
302 // so we'll save errno here, restore the signal mask, and only then
304 int savedErrno = errno;
306 // Restore signal mask; do this even if vfork fails!
307 r = pthread_sigmask(SIG_SETMASK, &oldSignals, nullptr);
308 CHECK_EQ(r, 0) << "pthread_sigmask: " << errnoStr(r); // shouldn't fail
309 checkUnixError(pid, savedErrno, "vfork");
311 // Child is alive. We can't throw any more, as we can't figure out
312 // what to do with the child.
315 returnCode_ = ProcessReturnCode(RV_RUNNING);
317 // Parent work, post-fork: close child's ends of pipes; closing them
319 for (int f : childFds) {
320 CHECK_ERR(::close(f));
326 // Checked version of close() to use in the child: _exit(126) on error
327 void childClose(int fd) {
329 if (r == -1) _exit(kChildFailure);
332 // Checked version of dup2() to use in the child: _exit(126) on error
333 void childDup2(int oldfd, int newfd) {
334 int r = ::dup2(oldfd, newfd);
335 if (r == -1) _exit(kChildFailure);
340 void Subprocess::runChild(const char* executable,
341 char** argv, char** env,
342 const Options& options) const {
343 // Close parent's ends of all pipes
344 for (auto& p : pipes_) {
345 childClose(p.parentFd);
348 // Close all fds that we're supposed to close.
349 // Note that we're ignoring errors here, in case some of these
350 // fds were set to close on exec.
351 for (auto& p : options.fdActions_) {
352 if (p.second == CLOSE) {
355 childDup2(p.second, p.first);
359 // If requested, close all other file descriptors. Don't close
360 // any fds in options.fdActions_, and don't touch stdin, stdout, stderr.
362 if (options.closeOtherFds_) {
363 for (int fd = getdtablesize() - 1; fd >= 3; --fd) {
364 if (options.fdActions_.count(fd) == 0) {
370 // Opt to receive signal on parent death, if requested
371 if (options.parentDeathSignal_ != 0) {
372 int r = prctl(PR_SET_PDEATHSIG, options.parentDeathSignal_, 0, 0, 0);
374 _exit(kChildFailure);
378 // Now, finally, exec.
380 if (options.usePath_) {
381 ::execvp(executable, argv);
383 ::execve(executable, argv, env);
387 ProcessReturnCode Subprocess::poll() {
388 returnCode_.enforce(ProcessReturnCode::RUNNING);
391 pid_t found = ::waitpid(pid_, &status, WNOHANG);
392 checkUnixError(found, "waitpid");
394 returnCode_ = ProcessReturnCode(status);
400 bool Subprocess::pollChecked() {
401 if (poll().state() == ProcessReturnCode::RUNNING) {
404 checkStatus(returnCode_);
408 ProcessReturnCode Subprocess::wait() {
409 returnCode_.enforce(ProcessReturnCode::RUNNING);
414 found = ::waitpid(pid_, &status, 0);
415 } while (found == -1 && errno == EINTR);
416 checkUnixError(found, "waitpid");
417 DCHECK_EQ(found, pid_);
418 returnCode_ = ProcessReturnCode(status);
423 void Subprocess::waitChecked() {
425 checkStatus(returnCode_);
428 void Subprocess::sendSignal(int signal) {
429 returnCode_.enforce(ProcessReturnCode::RUNNING);
430 int r = ::kill(pid_, signal);
431 checkUnixError(r, "kill");
434 pid_t Subprocess::pid() const {
440 std::pair<const uint8_t*, size_t> queueFront(const IOBufQueue& queue) {
441 auto* p = queue.front();
442 if (!p) return std::make_pair(nullptr, 0);
443 return io::Cursor(p).peek();
447 bool handleWrite(int fd, IOBufQueue& queue) {
449 auto p = queueFront(queue);
456 n = ::write(fd, p.first, p.second);
457 } while (n == -1 && errno == EINTR);
458 if (n == -1 && errno == EAGAIN) {
461 checkUnixError(n, "write");
467 bool handleRead(int fd, IOBufQueue& queue) {
469 auto p = queue.preallocate(100, 65000);
472 n = ::read(fd, p.first, p.second);
473 } while (n == -1 && errno == EINTR);
474 if (n == -1 && errno == EAGAIN) {
477 checkUnixError(n, "read");
481 queue.postallocate(n);
485 bool discardRead(int fd) {
486 static const size_t bufSize = 65000;
487 // Thread unsafe, but it doesn't matter.
488 static std::unique_ptr<char[]> buf(new char[bufSize]);
493 n = ::read(fd, buf.get(), bufSize);
494 } while (n == -1 && errno == EINTR);
495 if (n == -1 && errno == EAGAIN) {
498 checkUnixError(n, "read");
507 std::pair<std::string, std::string> Subprocess::communicate(
508 const CommunicateFlags& flags,
510 IOBufQueue dataQueue;
511 dataQueue.wrapBuffer(data.data(), data.size());
513 auto outQueues = communicateIOBuf(flags, std::move(dataQueue));
514 auto outBufs = std::make_pair(outQueues.first.move(),
515 outQueues.second.move());
516 std::pair<std::string, std::string> out;
518 outBufs.first->coalesce();
519 out.first.assign(reinterpret_cast<const char*>(outBufs.first->data()),
520 outBufs.first->length());
522 if (outBufs.second) {
523 outBufs.second->coalesce();
524 out.second.assign(reinterpret_cast<const char*>(outBufs.second->data()),
525 outBufs.second->length());
530 std::pair<IOBufQueue, IOBufQueue> Subprocess::communicateIOBuf(
531 const CommunicateFlags& flags,
533 std::pair<IOBufQueue, IOBufQueue> out;
535 auto readCallback = [&] (int pfd, int cfd) -> bool {
536 if (cfd == 1 && flags.readStdout_) {
537 return handleRead(pfd, out.first);
538 } else if (cfd == 2 && flags.readStderr_) {
539 return handleRead(pfd, out.second);
541 // Don't close the file descriptor, the child might not like SIGPIPE,
542 // just read and throw the data away.
543 return discardRead(pfd);
547 auto writeCallback = [&] (int pfd, int cfd) -> bool {
548 if (cfd == 0 && flags.writeStdin_) {
549 return handleWrite(pfd, data);
551 // If we don't want to write to this fd, just close it.
556 communicate(std::move(readCallback), std::move(writeCallback));
561 void Subprocess::communicate(FdCallback readCallback,
562 FdCallback writeCallback) {
563 returnCode_.enforce(ProcessReturnCode::RUNNING);
566 std::vector<pollfd> fds;
567 fds.reserve(pipes_.size());
568 std::vector<int> toClose;
569 toClose.reserve(pipes_.size());
571 while (!pipes_.empty()) {
575 for (auto& p : pipes_) {
578 // Yes, backwards, PIPE_IN / PIPE_OUT are defined from the
579 // child's point of view.
580 pfd.events = (p.direction == PIPE_IN ? POLLOUT : POLLIN);
586 r = ::poll(fds.data(), fds.size(), -1);
587 } while (r == -1 && errno == EINTR);
588 checkUnixError(r, "poll");
590 for (int i = 0; i < pipes_.size(); ++i) {
592 DCHECK_EQ(fds[i].fd, p.parentFd);
593 short events = fds[i].revents;
596 if (events & POLLOUT) {
597 DCHECK(!(events & POLLIN));
598 if (writeCallback(p.parentFd, p.childFd)) {
599 toClose.push_back(i);
604 if (events & POLLIN) {
605 DCHECK(!(events & POLLOUT));
606 if (readCallback(p.parentFd, p.childFd)) {
607 toClose.push_back(i);
612 if ((events & (POLLHUP | POLLERR)) && !closed) {
613 toClose.push_back(i);
618 // Close the fds in reverse order so the indexes hold after erase()
619 for (int idx : boost::adaptors::reverse(toClose)) {
620 auto pos = pipes_.begin() + idx;
621 closeChecked(pos->parentFd);
627 int Subprocess::findByChildFd(int childFd) const {
628 auto pos = std::lower_bound(
629 pipes_.begin(), pipes_.end(), childFd,
630 [] (const PipeInfo& info, int fd) { return info.childFd < fd; });
631 if (pos == pipes_.end() || pos->childFd != childFd) {
632 throw std::invalid_argument(folly::to<std::string>(
633 "child fd not found ", childFd));
635 return pos - pipes_.begin();
638 void Subprocess::closeParentFd(int childFd) {
639 int idx = findByChildFd(childFd);
640 closeChecked(pipes_[idx].parentFd);
641 pipes_.erase(pipes_.begin() + idx);
649 // We like EPIPE, thanks.
650 ::signal(SIGPIPE, SIG_IGN);
654 Initializer initializer;