proxygen
folly::ProcessReturnCode Class Reference

#include <Subprocess.h>

Public Types

enum  State { NOT_STARTED, RUNNING, EXITED, KILLED }
 

Public Member Functions

 ProcessReturnCode ()
 
 ProcessReturnCode (const ProcessReturnCode &p)=default
 
ProcessReturnCodeoperator= (const ProcessReturnCode &p)=default
 
 ProcessReturnCode (ProcessReturnCode &&p) noexcept
 
ProcessReturnCodeoperator= (ProcessReturnCode &&p) noexcept
 
State state () const
 
bool notStarted () const
 
bool running () const
 
bool exited () const
 
bool killed () const
 
int exitStatus () const
 
int killSignal () const
 
bool coreDumped () const
 
std::string str () const
 
void enforce (State state) const
 

Static Public Member Functions

static ProcessReturnCode makeNotStarted ()
 
static ProcessReturnCode makeRunning ()
 
static ProcessReturnCode make (int status)
 

Private Member Functions

 ProcessReturnCode (int rv)
 

Private Attributes

int rawStatus_
 

Static Private Attributes

static constexpr int RV_NOT_STARTED = -2
 
static constexpr int RV_RUNNING = -1
 

Detailed Description

Definition at line 129 of file Subprocess.h.

Member Enumeration Documentation

Enumerator
NOT_STARTED 
RUNNING 
EXITED 
KILLED 

Definition at line 131 of file Subprocess.h.

131  {
132  // Subprocess starts in the constructor, so this state designates only
133  // default-initialized or moved-out ProcessReturnCodes.
134  NOT_STARTED,
135  RUNNING,
136  EXITED,
137  KILLED,
138  };

Constructor & Destructor Documentation

folly::ProcessReturnCode::ProcessReturnCode ( )
inline

Definition at line 152 of file Subprocess.h.

References folly::pushmi::__adl::noexcept(), operator=(), and state().

Referenced by make(), makeNotStarted(), and makeRunning().

static constexpr int RV_NOT_STARTED
Definition: Subprocess.h:222
folly::ProcessReturnCode::ProcessReturnCode ( const ProcessReturnCode p)
default
folly::ProcessReturnCode::ProcessReturnCode ( ProcessReturnCode &&  p)
noexcept

Definition at line 62 of file Subprocess.cpp.

References RV_NOT_STARTED.

63  : rawStatus_(p.rawStatus_) {
64  p.rawStatus_ = ProcessReturnCode::RV_NOT_STARTED;
65 }
static constexpr int RV_NOT_STARTED
Definition: Subprocess.h:222
folly::ProcessReturnCode::ProcessReturnCode ( int  rv)
inlineexplicitprivate

Definition at line 221 of file Subprocess.h.

221 : rawStatus_(rv) {}

Member Function Documentation

bool folly::ProcessReturnCode::coreDumped ( ) const

Was a core file generated? Only valid if state() == KILLED; throws otherwise.

Definition at line 108 of file Subprocess.cpp.

References enforce(), KILLED, and rawStatus_.

Referenced by killed(), and str().

108  {
109  enforce(KILLED);
110  return WCOREDUMP(rawStatus_);
111 }
void enforce(State state) const
Definition: Subprocess.cpp:90
void folly::ProcessReturnCode::enforce ( State  state) const

Helper function to enforce a precondition based on this. Throws std::logic_error if in an unexpected state.

Definition at line 90 of file Subprocess.cpp.

References s, and state().

Referenced by coreDumped(), exitStatus(), killed(), killSignal(), folly::Subprocess::poll(), and folly::Subprocess::wait().

90  {
91  State s = state();
92  if (s != expected) {
93  throw std::logic_error(to<std::string>(
94  "Bad use of ProcessReturnCode; state is ", s, " expected ", expected));
95  }
96 }
State state() const
Definition: Subprocess.cpp:74
static set< string > s
bool folly::ProcessReturnCode::exited ( ) const
inline

Definition at line 180 of file Subprocess.h.

References EXITED, and state().

180  {
181  return state() == EXITED;
182  }
State state() const
Definition: Subprocess.cpp:74
int folly::ProcessReturnCode::exitStatus ( ) const

Exit status. Only valid if state() == EXITED; throws otherwise.

Definition at line 98 of file Subprocess.cpp.

References enforce(), EXITED, and rawStatus_.

Referenced by killed(), str(), folly::SubprocessSpawnError::SubprocessSpawnError(), and TEST().

98  {
99  enforce(EXITED);
100  return WEXITSTATUS(rawStatus_);
101 }
void enforce(State state) const
Definition: Subprocess.cpp:90
bool folly::ProcessReturnCode::killed ( ) const
inline

Definition at line 183 of file Subprocess.h.

References coreDumped(), enforce(), exitStatus(), KILLED, killSignal(), state(), str(), and string.

183  {
184  return state() == KILLED;
185  }
State state() const
Definition: Subprocess.cpp:74
int folly::ProcessReturnCode::killSignal ( ) const

Signal that caused the process's termination. Only valid if state() == KILLED; throws otherwise.

Definition at line 103 of file Subprocess.cpp.

References enforce(), KILLED, and rawStatus_.

Referenced by killed(), str(), and TEST().

103  {
104  enforce(KILLED);
105  return WTERMSIG(rawStatus_);
106 }
void enforce(State state) const
Definition: Subprocess.cpp:90
ProcessReturnCode folly::ProcessReturnCode::make ( int  status)
static

Definition at line 54 of file Subprocess.cpp.

References ProcessReturnCode().

Referenced by makeRunning(), folly::Subprocess::poll(), and folly::Subprocess::wait().

54  {
55  if (!WIFEXITED(status) && !WIFSIGNALED(status)) {
56  throw std::runtime_error(
57  to<std::string>("Invalid ProcessReturnCode: ", status));
58  }
59  return ProcessReturnCode(status);
60 }
static ProcessReturnCode folly::ProcessReturnCode::makeNotStarted ( )
inlinestatic

Definition at line 140 of file Subprocess.h.

References ProcessReturnCode(), and RV_NOT_STARTED.

140  {
142  }
static constexpr int RV_NOT_STARTED
Definition: Subprocess.h:222
static ProcessReturnCode folly::ProcessReturnCode::makeRunning ( )
inlinestatic

Definition at line 144 of file Subprocess.h.

References make(), ProcessReturnCode(), and RV_RUNNING.

Referenced by folly::Subprocess::spawnInternal().

144  {
146  }
static constexpr int RV_RUNNING
Definition: Subprocess.h:223
bool folly::ProcessReturnCode::notStarted ( ) const
inline

Helper wrappers around state().

Definition at line 174 of file Subprocess.h.

References NOT_STARTED, and state().

Referenced by TEST().

174  {
175  return state() == NOT_STARTED;
176  }
State state() const
Definition: Subprocess.cpp:74
ProcessReturnCode& folly::ProcessReturnCode::operator= ( const ProcessReturnCode p)
default

Referenced by ProcessReturnCode().

ProcessReturnCode & folly::ProcessReturnCode::operator= ( ProcessReturnCode &&  p)
noexcept

Definition at line 67 of file Subprocess.cpp.

References rawStatus_, and RV_NOT_STARTED.

68  {
69  rawStatus_ = p.rawStatus_;
70  p.rawStatus_ = ProcessReturnCode::RV_NOT_STARTED;
71  return *this;
72 }
static constexpr int RV_NOT_STARTED
Definition: Subprocess.h:222
bool folly::ProcessReturnCode::running ( ) const
inline

Definition at line 177 of file Subprocess.h.

References RUNNING, and state().

Referenced by runParent(), and TEST().

177  {
178  return state() == RUNNING;
179  }
State state() const
Definition: Subprocess.cpp:74
ProcessReturnCode::State folly::ProcessReturnCode::state ( ) const

Process state. One of: NOT_STARTED: process hasn't been started successfully RUNNING: process is currently running EXITED: process exited (successfully or not) KILLED: process was killed by a signal.

Definition at line 74 of file Subprocess.cpp.

References folly::assume_unreachable(), EXITED, KILLED, NOT_STARTED, rawStatus_, RUNNING, RV_NOT_STARTED, and RV_RUNNING.

Referenced by enforce(), exited(), killed(), notStarted(), ProcessReturnCode(), running(), str(), and folly::SubprocessSpawnError::SubprocessSpawnError().

74  {
75  if (rawStatus_ == RV_NOT_STARTED) {
76  return NOT_STARTED;
77  }
78  if (rawStatus_ == RV_RUNNING) {
79  return RUNNING;
80  }
81  if (WIFEXITED(rawStatus_)) {
82  return EXITED;
83  }
84  if (WIFSIGNALED(rawStatus_)) {
85  return KILLED;
86  }
88 }
static constexpr int RV_RUNNING
Definition: Subprocess.h:223
static constexpr int RV_NOT_STARTED
Definition: Subprocess.h:222
FOLLY_ALWAYS_INLINE void assume_unreachable()
Definition: Assume.h:59
std::string folly::ProcessReturnCode::str ( ) const

String representation; one of "not started" "running" "exited with status <status>" "killed by signal <signal>" "killed by signal <signal> (core dumped)"

Definition at line 113 of file Subprocess.cpp.

References folly::assume_unreachable(), coreDumped(), EXITED, exitStatus(), KILLED, killSignal(), NOT_STARTED, RUNNING, and state().

Referenced by killed().

113  {
114  switch (state()) {
115  case NOT_STARTED:
116  return "not started";
117  case RUNNING:
118  return "running";
119  case EXITED:
120  return to<std::string>("exited with status ", exitStatus());
121  case KILLED:
122  return to<std::string>(
123  "killed by signal ",
124  killSignal(),
125  (coreDumped() ? " (core dumped)" : ""));
126  }
128 }
State state() const
Definition: Subprocess.cpp:74
FOLLY_ALWAYS_INLINE void assume_unreachable()
Definition: Assume.h:59
bool coreDumped() const
Definition: Subprocess.cpp:108

Member Data Documentation

int folly::ProcessReturnCode::rawStatus_
private

Definition at line 225 of file Subprocess.h.

Referenced by coreDumped(), exitStatus(), killSignal(), operator=(), and state().

constexpr int folly::ProcessReturnCode::RV_NOT_STARTED = -2
staticprivate

Definition at line 222 of file Subprocess.h.

Referenced by makeNotStarted(), operator=(), ProcessReturnCode(), and state().

constexpr int folly::ProcessReturnCode::RV_RUNNING = -1
staticprivate

Definition at line 223 of file Subprocess.h.

Referenced by makeRunning(), and state().


The documentation for this class was generated from the following files: