BoosterSeat  0.1
A C++ library that includes common utilities that are used in other projects.
process.hpp
Go to the documentation of this file.
1 
10 #ifndef BOOSTER_SEAT_PROCESS_HPP_
11 #define BOOSTER_SEAT_PROCESS_HPP_
12 
13 #include <iostream>
14 #include <sstream>
15 #include <string>
16 #include <vector>
17 
18 #include <ext/stdio_filebuf.h>
19 #include <sys/types.h>
20 #include <sys/wait.h>
21 #include <unistd.h>
22 
24 #include <BoosterSeat/sleep.hpp>
25 
26 namespace bst {
27 
33 class Process {
34 public:
35  Process(const std::string &cmd, std::vector<std::string> argv);
36  ~Process();
37 
38  void writeToInStream(const std::string &s);
39  void closeInStream();
40  const std::string &getStdout();
41  const std::string &getStderr();
42  int getExitCode();
43 
51  void waitToComplete(unsigned int timeout = 1000);
52 
53 private:
54  void assertComplete();
55  void run(const std::string &cmd, std::vector<std::string> argv);
56  void child(const std::vector<std::string> &argv);
57 
59  pid_t pid_;
60 
62  int in_pipe_[2];
64  int out_pipe_[2];
66  int err_pipe_[2];
67 
69  __gnu_cxx::stdio_filebuf<char> *p_in_filebuf_;
71  __gnu_cxx::stdio_filebuf<char> *p_out_filebuf_;
73  __gnu_cxx::stdio_filebuf<char> *p_err_filebuf_;
74 
76  std::ostream *in_stream_;
78  std::istream *out_stream_;
80  std::istream *err_stream_;
81 
86  bool have_exit_code_ = false;
87 
89  int exit_code_ = 0;
91  std::string stdout_string_;
93  std::string stderr_string_;
94 };
95 
96 } // namespace bst
97 
98 #endif /* BOOSTER_SEAT_PROCESS_HPP_ */
A class to handle linux process calls.
Definition: process.hpp:33
pid_t pid_
The process id of the child process.
Definition: process.hpp:59
~Process()
Definition: process.cpp:23
int getExitCode()
Definition: process.cpp:52
std::string stderr_string_
The stderr of the process.
Definition: process.hpp:93
void writeToInStream(const std::string &s)
Definition: process.cpp:34
Process(const std::string &cmd, std::vector< std::string > argv)
Definition: process.cpp:13
std::istream * out_stream_
The output stream for the child process.
Definition: process.hpp:78
std::istream * err_stream_
The error stream for the child process.
Definition: process.hpp:80
const std::string & getStdout()
Definition: process.cpp:42
__gnu_cxx::stdio_filebuf< char > * p_err_filebuf_
The filebuf for error pipe.
Definition: process.hpp:73
void run(const std::string &cmd, std::vector< std::string > argv)
Definition: process.cpp:106
void child(const std::vector< std::string > &argv)
Definition: process.cpp:142
int in_pipe_[2]
The file descriptors for input pipe.
Definition: process.hpp:62
void waitToComplete(unsigned int timeout=1000)
Waits for the process to complete. If the process does not complete within the timeout,...
Definition: process.cpp:64
int out_pipe_[2]
The file descriptors for output pipe.
Definition: process.hpp:64
void closeInStream()
Definition: process.cpp:38
void assertComplete()
Definition: process.cpp:95
int exit_code_
The exit code of the process.
Definition: process.hpp:89
__gnu_cxx::stdio_filebuf< char > * p_in_filebuf_
The filebuf for input pipe.
Definition: process.hpp:69
int err_pipe_[2]
The file descriptors for error pipe.
Definition: process.hpp:66
std::string stdout_string_
The stdout of the process.
Definition: process.hpp:91
__gnu_cxx::stdio_filebuf< char > * p_out_filebuf_
The filebuf for output pipe.
Definition: process.hpp:71
bool have_exit_code_
Set to true when the process has completed and an exit code has been retrieved. This is used internal...
Definition: process.hpp:86
const std::string & getStderr()
Definition: process.cpp:47
std::ostream * in_stream_
The input stream for the child process.
Definition: process.hpp:76
Definition: filesystem.cpp:34