/*
- * Copyright 2015 Facebook, Inc.
+ * Copyright 2017 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
#include <sys/prctl.h>
#endif
#include <fcntl.h>
-#include <poll.h>
-
-#include <unistd.h>
#include <array>
#include <algorithm>
#include <glog/logging.h>
+#include <folly/Assume.h>
#include <folly/Conv.h>
#include <folly/Exception.h>
#include <folly/ScopeGuard.h>
+#include <folly/Shell.h>
#include <folly/String.h>
#include <folly/io/Cursor.h>
-
-extern char** environ;
+#include <folly/portability/Sockets.h>
+#include <folly/portability/Stdlib.h>
+#include <folly/portability/SysSyscall.h>
+#include <folly/portability/Unistd.h>
constexpr int kExecFailure = 127;
constexpr int kChildFailure = 126;
namespace folly {
+ProcessReturnCode::ProcessReturnCode(ProcessReturnCode&& p) noexcept
+ : rawStatus_(p.rawStatus_) {
+ p.rawStatus_ = ProcessReturnCode::RV_NOT_STARTED;
+}
+
+ProcessReturnCode& ProcessReturnCode::operator=(ProcessReturnCode&& p)
+ noexcept {
+ rawStatus_ = p.rawStatus_;
+ p.rawStatus_ = ProcessReturnCode::RV_NOT_STARTED;
+ return *this;
+}
+
ProcessReturnCode::State ProcessReturnCode::state() const {
if (rawStatus_ == RV_NOT_STARTED) return NOT_STARTED;
if (rawStatus_ == RV_RUNNING) return RUNNING;
return to<std::string>("killed by signal ", killSignal(),
(coreDumped() ? " (core dumped)" : ""));
}
- CHECK(false); // unreached
- return ""; // silence GCC warning
+ assume_unreachable();
}
CalledProcessError::CalledProcessError(ProcessReturnCode rc)
return *this;
}
+Subprocess::Subprocess() {}
+
Subprocess::Subprocess(
const std::vector<std::string>& argv,
const Options& options,
const char* executable,
- const std::vector<std::string>* env)
- : pid_(-1),
- returnCode_(RV_NOT_STARTED) {
+ const std::vector<std::string>* env) {
if (argv.empty()) {
throw std::invalid_argument("argv must not be empty");
}
Subprocess::Subprocess(
const std::string& cmd,
const Options& options,
- const std::vector<std::string>* env)
- : pid_(-1),
- returnCode_(RV_NOT_STARTED) {
+ const std::vector<std::string>* env) {
if (options.usePath_) {
throw std::invalid_argument("usePath() not allowed when running in shell");
}
- const char* shell = getenv("SHELL");
- if (!shell) {
- shell = "/bin/sh";
- }
- std::unique_ptr<const char*[]> argv(new const char*[4]);
- argv[0] = shell;
- argv[1] = "-c";
- argv[2] = cmd.c_str();
- argv[3] = nullptr;
- spawn(std::move(argv), shell, options, env);
+ std::vector<std::string> argv = {"/bin/sh", "-c", cmd};
+ spawn(cloneStrings(argv), argv[0].c_str(), options, env);
}
Subprocess::~Subprocess() {
int errnoValue;
};
-FOLLY_NORETURN void childError(int errFd, int errCode, int errnoValue);
-void childError(int errFd, int errCode, int errnoValue) {
+[[noreturn]] void childError(int errFd, int errCode, int errnoValue) {
ChildErrorInfo info = {errCode, errnoValue};
// Write the error information over the pipe to our parent process.
// We can't really do anything else if this write call fails.
// Call c_str() here, as it's not necessarily safe after fork.
const char* childDir =
options.childDir_.empty() ? nullptr : options.childDir_.c_str();
- pid_t pid = vfork();
+
+ pid_t pid;
+#ifdef __linux__
+ if (options.cloneFlags_) {
+ pid = syscall(SYS_clone, *options.cloneFlags_, 0, nullptr, nullptr);
+ checkUnixError(pid, errno, "clone");
+ } else {
+#endif
+ pid = vfork();
+ checkUnixError(pid, errno, "vfork");
+#ifdef __linux__
+ }
+#endif
if (pid == 0) {
int errnoValue = prepareChild(options, &oldSignals, childDir);
if (errnoValue != 0) {
// If we get here, exec() failed.
childError(errFd, kExecFailure, errnoValue);
}
- // In parent. Make sure vfork() succeeded.
- checkUnixError(pid, errno, "vfork");
// Child is alive. We have to be very careful about throwing after this
// point. We are inside the constructor, so if we throw the Subprocess
#if __linux__
// Opt to receive signal on parent death, if requested
if (options.parentDeathSignal_ != 0) {
- if (prctl(PR_SET_PDEATHSIG, options.parentDeathSignal_, 0, 0, 0) == -1) {
+ const auto parentDeathSignal =
+ static_cast<unsigned long>(options.parentDeathSignal_);
+ if (prctl(PR_SET_PDEATHSIG, parentDeathSignal, 0, 0, 0) == -1) {
return errno;
}
}
}
}
+ // The user callback comes last, so that the child is otherwise all set up.
+ if (options.dangerousPostForkPreExecCallback_) {
+ if (int error = (*options.dangerousPostForkPreExecCallback_)()) {
+ return error;
+ }
+ }
+
return 0;
}
throw SubprocessSpawnError(executable, info.errCode, info.errnoValue);
}
-ProcessReturnCode Subprocess::poll() {
+ProcessReturnCode Subprocess::poll(struct rusage* ru) {
returnCode_.enforce(ProcessReturnCode::RUNNING);
DCHECK_GT(pid_, 0);
int status;
- pid_t found = ::waitpid(pid_, &status, WNOHANG);
- checkUnixError(found, "waitpid");
+ pid_t found = ::wait4(pid_, &status, WNOHANG, ru);
+ // The spec guarantees that EINTR does not occur with WNOHANG, so the only
+ // two remaining errors are ECHILD (other code reaped the child?), or
+ // EINVAL (cosmic rays?), both of which merit an abort:
+ PCHECK(found != -1) << "waitpid(" << pid_ << ", &status, WNOHANG)";
if (found != 0) {
// Though the child process had quit, this call does not close the pipes
// since its descendants may still be using them.
do {
found = ::waitpid(pid_, &status, 0);
} while (found == -1 && errno == EINTR);
- checkUnixError(found, "waitpid");
+ // The only two remaining errors are ECHILD (other code reaped the
+ // child?), or EINVAL (cosmic rays?), and both merit an abort:
+ PCHECK(found != -1) << "waitpid(" << pid_ << ", &status, WNOHANG)";
// Though the child process had quit, this call does not close the pipes
// since its descendants may still be using them.
DCHECK_EQ(found, pid_);
namespace {
-std::pair<const uint8_t*, size_t> queueFront(const IOBufQueue& queue) {
+ByteRange queueFront(const IOBufQueue& queue) {
auto* p = queue.front();
- if (!p) return std::make_pair(nullptr, 0);
- return io::Cursor(p).peek();
+ if (!p) {
+ return ByteRange{};
+ }
+ return io::Cursor(p).peekBytes();
}
// fd write
bool handleWrite(int fd, IOBufQueue& queue) {
for (;;) {
- auto p = queueFront(queue);
- if (p.second == 0) {
+ auto b = queueFront(queue);
+ if (b.empty()) {
return true; // EOF
}
- ssize_t n = writeNoInt(fd, p.first, p.second);
+ ssize_t n = writeNoInt(fd, b.data(), b.size());
if (n == -1 && errno == EAGAIN) {
return false;
}
std::vector<Subprocess::ChildPipe> Subprocess::takeOwnershipOfPipes() {
std::vector<Subprocess::ChildPipe> pipes;
for (auto& p : pipes_) {
- pipes.emplace_back(ChildPipe{p.childFd, std::move(p.pipe)});
+ pipes.emplace_back(p.childFd, std::move(p.pipe));
}
- pipes_.clear();
+ // release memory
+ std::vector<Pipe>().swap(pipes_);
return pipes;
}