* to complete, returning the exit status.
*
* A thread-safe [1] version of popen() (type="r", to read from the child):
- * Subprocess proc(cmd, Subprocess::pipeStdout());
+ * Subprocess proc(cmd, Subprocess::Options().pipeStdout());
* // read from proc.stdoutFd()
* proc.wait();
*
* A thread-safe [1] version of popen() (type="w", to write to the child):
- * Subprocess proc(cmd, Subprocess::pipeStdin());
+ * Subprocess proc(cmd, Subprocess::Options().pipeStdin());
* // write to proc.stdinFd()
* proc.wait();
*
#pragma once
-#include <sys/types.h>
#include <signal.h>
+#include <sys/types.h>
+
#if __APPLE__
#include <sys/wait.h>
#else
#endif
#include <exception>
-#include <vector>
#include <string>
+#include <vector>
#include <boost/container/flat_map.hpp>
#include <folly/Range.h>
#include <folly/gen/String.h>
#include <folly/io/IOBufQueue.h>
+#include <folly/portability/SysResource.h>
namespace folly {
/**
* Base exception thrown by the Subprocess methods.
*/
-class SubprocessError : public std::exception {};
+class SubprocessError : public std::runtime_error {
+ public:
+ using std::runtime_error::runtime_error;
+};
/**
* Exception thrown by *Checked methods of Subprocess.
class CalledProcessError : public SubprocessError {
public:
explicit CalledProcessError(ProcessReturnCode rc);
- ~CalledProcessError() throw() = default;
- const char* what() const throw() override { return what_.c_str(); }
+ ~CalledProcessError() throw() override = default;
ProcessReturnCode returnCode() const { return returnCode_; }
private:
ProcessReturnCode returnCode_;
- std::string what_;
};
/**
class SubprocessSpawnError : public SubprocessError {
public:
SubprocessSpawnError(const char* executable, int errCode, int errnoValue);
- ~SubprocessSpawnError() throw() = default;
- const char* what() const throw() override { return what_.c_str(); }
+ ~SubprocessSpawnError() throw() override = default;
int errnoValue() const { return errnoValue_; }
private:
int errnoValue_;
- std::string what_;
};
/**
#endif
};
- static Options pipeStdin() { return Options().stdinFd(PIPE); }
- static Options pipeStdout() { return Options().stdoutFd(PIPE); }
- static Options pipeStderr() { return Options().stderrFd(PIPE); }
-
// Non-copiable, but movable
Subprocess(const Subprocess&) = delete;
Subprocess& operator=(const Subprocess&) = delete;
* e.g. if you wait for the underlying process without going through this
* Subprocess instance.
*/
- ProcessReturnCode poll();
+ ProcessReturnCode poll(struct rusage* ru = nullptr);
/**
* Poll the child's status. If the process is still running, return false.
std::vector<Pipe> pipes_;
};
-} // namespace folly
+} // namespace folly