X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=folly%2FSubprocess.cpp;h=7fb038b2dd198f4f1a197d9385b1dda704291168;hb=c98de6b2ecb4b4855fe6ce152329dc74eaae4710;hp=52dad4350bc91eec3f15eb0a21be3af18e3f5ca6;hpb=ee87051bba16d2c98a1871450ff390924d66a4df;p=folly.git diff --git a/folly/Subprocess.cpp b/folly/Subprocess.cpp index 52dad435..7fb038b2 100644 --- a/folly/Subprocess.cpp +++ b/folly/Subprocess.cpp @@ -25,8 +25,8 @@ #endif #include -#include #include +#include #include #include @@ -34,23 +34,31 @@ #include -#include #include #include #include -#include #include #include +#include #include #include #include #include +#include constexpr int kExecFailure = 127; constexpr int kChildFailure = 126; namespace folly { +ProcessReturnCode ProcessReturnCode::make(int status) { + if (!WIFEXITED(status) && !WIFSIGNALED(status)) { + throw std::runtime_error( + to("Invalid ProcessReturnCode: ", status)); + } + return ProcessReturnCode(status); +} + ProcessReturnCode::ProcessReturnCode(ProcessReturnCode&& p) noexcept : rawStatus_(p.rawStatus_) { p.rawStatus_ = ProcessReturnCode::RV_NOT_STARTED; @@ -64,12 +72,19 @@ ProcessReturnCode& ProcessReturnCode::operator=(ProcessReturnCode&& p) } ProcessReturnCode::State ProcessReturnCode::state() const { - if (rawStatus_ == RV_NOT_STARTED) return NOT_STARTED; - if (rawStatus_ == RV_RUNNING) return RUNNING; - if (WIFEXITED(rawStatus_)) return EXITED; - if (WIFSIGNALED(rawStatus_)) return KILLED; - throw std::runtime_error(to( - "Invalid ProcessReturnCode: ", rawStatus_)); + if (rawStatus_ == RV_NOT_STARTED) { + return NOT_STARTED; + } + if (rawStatus_ == RV_RUNNING) { + return RUNNING; + } + if (WIFEXITED(rawStatus_)) { + return EXITED; + } + if (WIFSIGNALED(rawStatus_)) { + return KILLED; + } + assume_unreachable(); } void ProcessReturnCode::enforce(State expected) const { @@ -151,7 +166,7 @@ void checkStatus(ProcessReturnCode returnCode) { } } -} // namespace +} // namespace Subprocess::Options& Subprocess::Options::fd(int fd, int action) { if (action == Subprocess::PIPE) { @@ -178,7 +193,9 @@ Subprocess::Subprocess( if (argv.empty()) { throw std::invalid_argument("argv must not be empty"); } - if (!executable) executable = argv[0].c_str(); + if (!executable) { + executable = argv[0].c_str(); + } spawn(cloneStrings(argv), executable, options, env); } @@ -215,7 +232,7 @@ struct ChildErrorInfo { _exit(errCode); } -} // namespace +} // namespace void Subprocess::setAllNonBlocking() { for (auto& p : pipes_) { @@ -336,10 +353,10 @@ void Subprocess::spawnInternal( int cfd; if (p.second == PIPE_IN) { // Child gets reading end - pipe.pipe = folly::File(fds[1], /*owns_fd=*/ true); + pipe.pipe = folly::File(fds[1], /*ownsFd=*/true); cfd = fds[0]; } else { - pipe.pipe = folly::File(fds[0], /*owns_fd=*/ true); + pipe.pipe = folly::File(fds[0], /*ownsFd=*/true); cfd = fds[1]; } p.second = cfd; // ensure it gets dup2()ed @@ -427,7 +444,7 @@ void Subprocess::spawnInternal( // child has exited and can be immediately waited for. In all other cases, // we have no way of cleaning up the child. pid_ = pid; - returnCode_ = ProcessReturnCode(RV_RUNNING); + returnCode_ = ProcessReturnCode::makeRunning(); } int Subprocess::prepareChild(const Options& options, @@ -562,7 +579,7 @@ ProcessReturnCode Subprocess::poll(struct rusage* ru) { if (found != 0) { // Though the child process had quit, this call does not close the pipes // since its descendants may still be using them. - returnCode_ = ProcessReturnCode(status); + returnCode_ = ProcessReturnCode::make(status); pid_ = -1; } return returnCode_; @@ -590,7 +607,7 @@ ProcessReturnCode Subprocess::wait() { // 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_); - returnCode_ = ProcessReturnCode(status); + returnCode_ = ProcessReturnCode::make(status); pid_ = -1; return returnCode_; } @@ -670,7 +687,7 @@ bool discardRead(int fd) { } } -} // namespace +} // namespace std::pair Subprocess::communicate( StringPiece input) { @@ -858,6 +875,6 @@ class Initializer { Initializer initializer; -} // namespace +} // namespace -} // namespace folly +} // namespace folly