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/FileUtil.h"
37 #include "folly/ScopeGuard.h"
38 #include "folly/String.h"
39 #include "folly/io/Cursor.h"
41 extern char** environ;
43 constexpr int kExecFailure = 127;
44 constexpr int kChildFailure = 126;
48 ProcessReturnCode::State ProcessReturnCode::state() const {
49 if (rawStatus_ == RV_NOT_STARTED) return NOT_STARTED;
50 if (rawStatus_ == RV_RUNNING) return RUNNING;
51 if (WIFEXITED(rawStatus_)) return EXITED;
52 if (WIFSIGNALED(rawStatus_)) return KILLED;
53 throw std::runtime_error(to<std::string>(
54 "Invalid ProcessReturnCode: ", rawStatus_));
57 void ProcessReturnCode::enforce(State expected) const {
60 throw std::logic_error(to<std::string>("Invalid state ", s,
61 " expected ", expected));
65 int ProcessReturnCode::exitStatus() const {
67 return WEXITSTATUS(rawStatus_);
70 int ProcessReturnCode::killSignal() const {
72 return WTERMSIG(rawStatus_);
75 bool ProcessReturnCode::coreDumped() const {
77 return WCOREDUMP(rawStatus_);
80 std::string ProcessReturnCode::str() const {
87 return to<std::string>("exited with status ", exitStatus());
89 return to<std::string>("killed by signal ", killSignal(),
90 (coreDumped() ? " (core dumped)" : ""));
92 CHECK(false); // unreached
95 CalledProcessError::CalledProcessError(ProcessReturnCode rc)
97 what_(returnCode_.str()) {
100 SubprocessSpawnError::SubprocessSpawnError(const char* executable,
103 : errnoValue_(errnoValue),
104 what_(to<std::string>(errCode == kExecFailure ?
105 "failed to execute " :
106 "error preparing to execute ",
107 executable, ": ", errnoStr(errnoValue))) {
112 // Copy pointers to the given strings in a format suitable for posix_spawn
113 std::unique_ptr<const char*[]> cloneStrings(const std::vector<std::string>& s) {
114 std::unique_ptr<const char*[]> d(new const char*[s.size() + 1]);
115 for (int i = 0; i < s.size(); i++) {
118 d[s.size()] = nullptr;
122 // Check a wait() status, throw on non-successful
123 void checkStatus(ProcessReturnCode returnCode) {
124 if (returnCode.state() != ProcessReturnCode::EXITED ||
125 returnCode.exitStatus() != 0) {
126 throw CalledProcessError(returnCode);
132 Subprocess::Options& Subprocess::Options::fd(int fd, int action) {
133 if (action == Subprocess::PIPE) {
135 action = Subprocess::PIPE_IN;
136 } else if (fd == 1 || fd == 2) {
137 action = Subprocess::PIPE_OUT;
139 throw std::invalid_argument(
140 to<std::string>("Only fds 0, 1, 2 are valid for action=PIPE: ", fd));
143 fdActions_[fd] = action;
147 Subprocess::Subprocess(
148 const std::vector<std::string>& argv,
149 const Options& options,
150 const char* executable,
151 const std::vector<std::string>* env)
153 returnCode_(RV_NOT_STARTED) {
155 throw std::invalid_argument("argv must not be empty");
157 if (!executable) executable = argv[0].c_str();
158 spawn(cloneStrings(argv), executable, options, env);
161 Subprocess::Subprocess(
162 const std::string& cmd,
163 const Options& options,
164 const std::vector<std::string>* env)
166 returnCode_(RV_NOT_STARTED) {
167 if (options.usePath_) {
168 throw std::invalid_argument("usePath() not allowed when running in shell");
170 const char* shell = getenv("SHELL");
175 std::unique_ptr<const char*[]> argv(new const char*[4]);
178 argv[2] = cmd.c_str();
180 spawn(std::move(argv), shell, options, env);
183 Subprocess::~Subprocess() {
184 CHECK_NE(returnCode_.state(), ProcessReturnCode::RUNNING)
185 << "Subprocess destroyed without reaping child";
190 void closeChecked(int fd) {
191 checkUnixError(::close(fd), "close");
194 struct ChildErrorInfo {
199 void childError(int errFd, int errCode, int errnoValue) FOLLY_NORETURN;
200 void childError(int errFd, int errCode, int errnoValue) {
201 ChildErrorInfo info = {errCode, errnoValue};
202 // Write the error information over the pipe to our parent process.
203 // We can't really do anything else if this write call fails.
204 writeNoInt(errFd, &info, sizeof(info));
211 void Subprocess::closeAll() {
212 for (auto& p : pipes_) {
213 closeChecked(p.parentFd);
218 void Subprocess::setAllNonBlocking() {
219 for (auto& p : pipes_) {
221 int flags = ::fcntl(fd, F_GETFL);
222 checkUnixError(flags, "fcntl");
223 int r = ::fcntl(fd, F_SETFL, flags | O_NONBLOCK);
224 checkUnixError(r, "fcntl");
228 void Subprocess::spawn(
229 std::unique_ptr<const char*[]> argv,
230 const char* executable,
231 const Options& optionsIn,
232 const std::vector<std::string>* env) {
233 if (optionsIn.usePath_ && env) {
234 throw std::invalid_argument(
235 "usePath() not allowed when overriding environment");
238 // Make a copy, we'll mutate options
239 Options options(optionsIn);
241 // On error, close all of the pipes_
242 auto pipesGuard = makeGuard([&] {
243 for (auto& p : this->pipes_) {
244 CHECK_ERR(::close(p.parentFd));
248 // Create a pipe to use to receive error information from the child,
249 // in case it fails before calling exec()
251 int r = ::pipe(errFds);
252 checkUnixError(r, "pipe");
254 CHECK_ERR(::close(errFds[0]));
255 if (errFds[1] >= 0) {
256 CHECK_ERR(::close(errFds[1]));
259 // Ask the child to close the read end of the error pipe.
260 options.fdActions_[errFds[0]] = CLOSE;
261 // Set the close-on-exec flag on the write side of the pipe.
262 // This way the pipe will be closed automatically in the child if execve()
263 // succeeds. If the exec fails the child can write error information to the
265 r = fcntl(errFds[1], F_SETFD, FD_CLOEXEC);
266 checkUnixError(r, "set FD_CLOEXEC");
268 // Perform the actual work of setting up pipes then forking and
269 // executing the child.
270 spawnInternal(std::move(argv), executable, options, env, errFds[1]);
272 // After spawnInternal() returns the child is alive. We have to be very
273 // careful about throwing after this point. We are inside the constructor,
274 // so if we throw the Subprocess object will have never existed, and the
275 // destructor will never be called.
277 // We should only throw if we got an error via the errFd, and we know the
278 // child has exited and can be immediately waited for. In all other cases,
279 // we have no way of cleaning up the child.
281 // Close writable side of the errFd pipe in the parent process
282 CHECK_ERR(::close(errFds[1]));
285 // Read from the errFd pipe, to tell if the child ran into any errors before
287 readChildErrorPipe(errFds[0], executable);
289 // We have fully succeeded now, so release the guard on pipes_
290 pipesGuard.dismiss();
293 void Subprocess::spawnInternal(
294 std::unique_ptr<const char*[]> argv,
295 const char* executable,
297 const std::vector<std::string>* env,
299 // Parent work, pre-fork: create pipes
300 std::vector<int> childFds;
301 // Close all of the childFds as we leave this scope
303 // These are only pipes, closing them shouldn't fail
304 for (int cfd : childFds) {
305 CHECK_ERR(::close(cfd));
310 for (auto& p : options.fdActions_) {
311 if (p.second == PIPE_IN || p.second == PIPE_OUT) {
314 checkUnixError(r, "pipe");
316 pinfo.direction = p.second;
318 if (p.second == PIPE_IN) {
319 // Child gets reading end
320 pinfo.parentFd = fds[1];
323 pinfo.parentFd = fds[0];
326 p.second = cfd; // ensure it gets dup2()ed
327 pinfo.childFd = p.first;
328 childFds.push_back(cfd);
329 pipes_.push_back(pinfo);
333 // This should already be sorted, as options.fdActions_ is
334 DCHECK(std::is_sorted(pipes_.begin(), pipes_.end()));
336 // Note that the const casts below are legit, per
337 // http://pubs.opengroup.org/onlinepubs/009695399/functions/exec.html
339 char** argVec = const_cast<char**>(argv.get());
341 // Set up environment
342 std::unique_ptr<const char*[]> envHolder;
345 envHolder = cloneStrings(*env);
346 envVec = const_cast<char**>(envHolder.get());
351 // Block all signals around vfork; see http://ewontfix.com/7/.
353 // As the child may run in the same address space as the parent until
354 // the actual execve() system call, any (custom) signal handlers that
355 // the parent has might alter parent's memory if invoked in the child,
356 // with undefined results. So we block all signals in the parent before
357 // vfork(), which will cause them to be blocked in the child as well (we
358 // rely on the fact that Linux, just like all sane implementations, only
359 // clones the calling thread). Then, in the child, we reset all signals
360 // to their default dispositions (while still blocked), and unblock them
361 // (so the exec()ed process inherits the parent's signal mask)
363 // The parent also unblocks all signals as soon as vfork() returns.
365 r = ::sigfillset(&allBlocked);
366 checkUnixError(r, "sigfillset");
369 r = pthread_sigmask(SIG_SETMASK, &allBlocked, &oldSignals);
370 checkPosixError(r, "pthread_sigmask");
372 // Restore signal mask
373 r = pthread_sigmask(SIG_SETMASK, &oldSignals, nullptr);
374 CHECK_EQ(r, 0) << "pthread_sigmask: " << errnoStr(r); // shouldn't fail
379 int errnoValue = prepareChild(options, &oldSignals);
380 if (errnoValue != 0) {
381 childError(errFd, kChildFailure, errnoValue);
384 errnoValue = runChild(executable, argVec, envVec, options);
385 // If we get here, exec() failed.
386 childError(errFd, kExecFailure, errnoValue);
388 // In parent. Make sure vfork() succeeded.
389 checkUnixError(pid, errno, "vfork");
391 // Child is alive. We have to be very careful about throwing after this
392 // point. We are inside the constructor, so if we throw the Subprocess
393 // object will have never existed, and the destructor will never be called.
395 // We should only throw if we got an error via the errFd, and we know the
396 // child has exited and can be immediately waited for. In all other cases,
397 // we have no way of cleaning up the child.
399 returnCode_ = ProcessReturnCode(RV_RUNNING);
402 int Subprocess::prepareChild(const Options& options,
403 const sigset_t* sigmask) const {
404 // While all signals are blocked, we must reset their
405 // dispositions to default.
406 for (int sig = 1; sig < NSIG; ++sig) {
407 ::signal(sig, SIG_DFL);
409 // Unblock signals; restore signal mask.
410 int r = pthread_sigmask(SIG_SETMASK, sigmask, nullptr);
412 return r; // pthread_sigmask() returns an errno value
415 // Close parent's ends of all pipes
416 for (auto& p : pipes_) {
417 r = ::close(p.parentFd);
423 // Close all fds that we're supposed to close.
424 // Note that we're ignoring errors here, in case some of these
425 // fds were set to close on exec.
426 for (auto& p : options.fdActions_) {
427 if (p.second == CLOSE) {
430 r = ::dup2(p.second, p.first);
437 // If requested, close all other file descriptors. Don't close
438 // any fds in options.fdActions_, and don't touch stdin, stdout, stderr.
440 if (options.closeOtherFds_) {
441 for (int fd = getdtablesize() - 1; fd >= 3; --fd) {
442 if (options.fdActions_.count(fd) == 0) {
448 // Opt to receive signal on parent death, if requested
449 if (options.parentDeathSignal_ != 0) {
450 r = prctl(PR_SET_PDEATHSIG, options.parentDeathSignal_, 0, 0, 0);
459 int Subprocess::runChild(const char* executable,
460 char** argv, char** env,
461 const Options& options) const {
462 // Now, finally, exec.
464 if (options.usePath_) {
465 ::execvp(executable, argv);
467 ::execve(executable, argv, env);
472 void Subprocess::readChildErrorPipe(int pfd, const char* executable) {
474 auto rc = readNoInt(pfd, &info, sizeof(info));
476 // No data means the child executed successfully, and the pipe
477 // was closed due to the close-on-exec flag being set.
479 } else if (rc != sizeof(ChildErrorInfo)) {
480 // An error occurred trying to read from the pipe, or we got a partial read.
481 // Neither of these cases should really occur in practice.
483 // We can't get any error data from the child in this case, and we don't
484 // know if it is successfully running or not. All we can do is to return
485 // normally, as if the child executed successfully. If something bad
486 // happened the caller should at least get a non-normal exit status from
488 LOG(ERROR) << "unexpected error trying to read from child error pipe " <<
489 "rc=" << rc << ", errno=" << errno;
493 // We got error data from the child. The child should exit immediately in
494 // this case, so wait on it to clean up.
497 // Throw to signal the error
498 throw SubprocessSpawnError(executable, info.errCode, info.errnoValue);
501 ProcessReturnCode Subprocess::poll() {
502 returnCode_.enforce(ProcessReturnCode::RUNNING);
505 pid_t found = ::waitpid(pid_, &status, WNOHANG);
506 checkUnixError(found, "waitpid");
508 returnCode_ = ProcessReturnCode(status);
514 bool Subprocess::pollChecked() {
515 if (poll().state() == ProcessReturnCode::RUNNING) {
518 checkStatus(returnCode_);
522 ProcessReturnCode Subprocess::wait() {
523 returnCode_.enforce(ProcessReturnCode::RUNNING);
528 found = ::waitpid(pid_, &status, 0);
529 } while (found == -1 && errno == EINTR);
530 checkUnixError(found, "waitpid");
531 DCHECK_EQ(found, pid_);
532 returnCode_ = ProcessReturnCode(status);
537 void Subprocess::waitChecked() {
539 checkStatus(returnCode_);
542 void Subprocess::sendSignal(int signal) {
543 returnCode_.enforce(ProcessReturnCode::RUNNING);
544 int r = ::kill(pid_, signal);
545 checkUnixError(r, "kill");
548 pid_t Subprocess::pid() const {
554 std::pair<const uint8_t*, size_t> queueFront(const IOBufQueue& queue) {
555 auto* p = queue.front();
556 if (!p) return std::make_pair(nullptr, 0);
557 return io::Cursor(p).peek();
561 bool handleWrite(int fd, IOBufQueue& queue) {
563 auto p = queueFront(queue);
570 n = ::write(fd, p.first, p.second);
571 } while (n == -1 && errno == EINTR);
572 if (n == -1 && errno == EAGAIN) {
575 checkUnixError(n, "write");
581 bool handleRead(int fd, IOBufQueue& queue) {
583 auto p = queue.preallocate(100, 65000);
586 n = ::read(fd, p.first, p.second);
587 } while (n == -1 && errno == EINTR);
588 if (n == -1 && errno == EAGAIN) {
591 checkUnixError(n, "read");
595 queue.postallocate(n);
599 bool discardRead(int fd) {
600 static const size_t bufSize = 65000;
601 // Thread unsafe, but it doesn't matter.
602 static std::unique_ptr<char[]> buf(new char[bufSize]);
607 n = ::read(fd, buf.get(), bufSize);
608 } while (n == -1 && errno == EINTR);
609 if (n == -1 && errno == EAGAIN) {
612 checkUnixError(n, "read");
621 std::pair<std::string, std::string> Subprocess::communicate(
623 IOBufQueue inputQueue;
624 inputQueue.wrapBuffer(input.data(), input.size());
626 auto outQueues = communicateIOBuf(std::move(inputQueue));
627 auto outBufs = std::make_pair(outQueues.first.move(),
628 outQueues.second.move());
629 std::pair<std::string, std::string> out;
631 outBufs.first->coalesce();
632 out.first.assign(reinterpret_cast<const char*>(outBufs.first->data()),
633 outBufs.first->length());
635 if (outBufs.second) {
636 outBufs.second->coalesce();
637 out.second.assign(reinterpret_cast<const char*>(outBufs.second->data()),
638 outBufs.second->length());
643 std::pair<IOBufQueue, IOBufQueue> Subprocess::communicateIOBuf(
645 // If the user supplied a non-empty input buffer, make sure
646 // that stdin is a pipe so we can write the data.
647 if (!input.empty()) {
648 // findByChildFd() will throw std::invalid_argument if no pipe for
649 // STDIN_FILENO exists
650 findByChildFd(STDIN_FILENO);
653 std::pair<IOBufQueue, IOBufQueue> out;
655 auto readCallback = [&] (int pfd, int cfd) -> bool {
656 if (cfd == STDOUT_FILENO) {
657 return handleRead(pfd, out.first);
658 } else if (cfd == STDERR_FILENO) {
659 return handleRead(pfd, out.second);
661 // Don't close the file descriptor, the child might not like SIGPIPE,
662 // just read and throw the data away.
663 return discardRead(pfd);
667 auto writeCallback = [&] (int pfd, int cfd) -> bool {
668 if (cfd == STDIN_FILENO) {
669 return handleWrite(pfd, input);
671 // If we don't want to write to this fd, just close it.
676 communicate(std::move(readCallback), std::move(writeCallback));
681 void Subprocess::communicate(FdCallback readCallback,
682 FdCallback writeCallback) {
683 returnCode_.enforce(ProcessReturnCode::RUNNING);
686 std::vector<pollfd> fds;
687 fds.reserve(pipes_.size());
688 std::vector<int> toClose;
689 toClose.reserve(pipes_.size());
691 while (!pipes_.empty()) {
695 for (auto& p : pipes_) {
698 // Yes, backwards, PIPE_IN / PIPE_OUT are defined from the
699 // child's point of view.
700 pfd.events = (p.direction == PIPE_IN ? POLLOUT : POLLIN);
706 r = ::poll(fds.data(), fds.size(), -1);
707 } while (r == -1 && errno == EINTR);
708 checkUnixError(r, "poll");
710 for (int i = 0; i < pipes_.size(); ++i) {
712 DCHECK_EQ(fds[i].fd, p.parentFd);
713 short events = fds[i].revents;
716 if (events & POLLOUT) {
717 DCHECK(!(events & POLLIN));
718 if (writeCallback(p.parentFd, p.childFd)) {
719 toClose.push_back(i);
724 if (events & POLLIN) {
725 DCHECK(!(events & POLLOUT));
726 if (readCallback(p.parentFd, p.childFd)) {
727 toClose.push_back(i);
732 if ((events & (POLLHUP | POLLERR)) && !closed) {
733 toClose.push_back(i);
738 // Close the fds in reverse order so the indexes hold after erase()
739 for (int idx : boost::adaptors::reverse(toClose)) {
740 auto pos = pipes_.begin() + idx;
741 closeChecked(pos->parentFd);
747 int Subprocess::findByChildFd(int childFd) const {
748 auto pos = std::lower_bound(
749 pipes_.begin(), pipes_.end(), childFd,
750 [] (const PipeInfo& info, int fd) { return info.childFd < fd; });
751 if (pos == pipes_.end() || pos->childFd != childFd) {
752 throw std::invalid_argument(folly::to<std::string>(
753 "child fd not found ", childFd));
755 return pos - pipes_.begin();
758 void Subprocess::closeParentFd(int childFd) {
759 int idx = findByChildFd(childFd);
760 closeChecked(pipes_[idx].parentFd);
761 pipes_.erase(pipes_.begin() + idx);
769 // We like EPIPE, thanks.
770 ::signal(SIGPIPE, SIG_IGN);
774 Initializer initializer;