CBMC
piped_process.h
Go to the documentation of this file.
1 
5 #ifndef CPROVER_UTIL_PIPED_PROCESS_H
6 #define CPROVER_UTIL_PIPED_PROCESS_H
7 
8 #ifdef _WIN32
9 # include <memory>
10 // The below are forward declarations for Windows APIs
11 struct _PROCESS_INFORMATION; // NOLINT
12 typedef struct _PROCESS_INFORMATION PROCESS_INFORMATION; // NOLINT
13 typedef void *HANDLE; // NOLINT
14 #endif
15 
16 #include "message.h"
17 
18 #include <vector>
19 
20 #define PIPED_PROCESS_INFINITE_TIMEOUT \
21  std::optional<std::size_t> \
22  { \
23  }
24 
26 {
27 public:
29  enum class statet
30  {
31  RUNNING,
32  ERRORED
33  };
34 
36  enum class send_responset
37  {
38  SUCCEEDED,
39  FAILED,
40  ERRORED
41  };
42 
46  [[nodiscard]] send_responset send(const std::string &message);
49  std::string receive();
53  std::string wait_receive();
54 
58 
65  bool can_receive(std::optional<std::size_t> wait_time);
66 
70  bool can_receive();
71 
76  // of can_receive(0)
77  void wait_receivable(int wait_time);
78 
83  explicit piped_processt(
84  const std::vector<std::string> &commandvec,
85  message_handlert &message_handler);
86 
87  // Deleted due to declaring an explicit destructor and not wanting copy
88  // constructors to be implemented.
89  piped_processt(const piped_processt &) = delete;
92 
93 protected:
94 #ifdef _WIN32
95  // Process information handle for Windows
96  std::unique_ptr<PROCESS_INFORMATION> proc_info;
97  // Handles for communication with child process
98  HANDLE child_std_IN_Rd;
99  HANDLE child_std_IN_Wr;
100  HANDLE child_std_OUT_Rd;
101  HANDLE child_std_OUT_Wr;
102 #else
103  // Child process ID.
106  // The member fields below are so named from the perspective of the
107  // parent -> child process. So `pipe_input` is where we are feeding
108  // commands to the child process, and `pipe_output` is where we read
109  // the results of execution from.
110  int pipe_input[2];
111  int pipe_output[2];
112 #endif
115 };
116 
117 #endif // endifndef CPROVER_UTIL_PIPED_PROCESS_H
Class that provides messages with a built-in verbosity 'level'.
Definition: message.h:155
pid_t child_process_id
piped_processt & operator=(const piped_processt &)=delete
statet process_state
bool can_receive()
See if this process can receive data from the other process.
void wait_receivable(int wait_time)
Wait for the pipe to be ready, waiting specified time between checks.
piped_processt(const piped_processt &)=delete
std::string receive()
Read a string from the child process' output.
send_responset send(const std::string &message)
Send a string message (command) to the child process.
FILE * command_stream
statet
Enumeration to keep track of child process state.
Definition: piped_process.h:30
piped_processt(const std::vector< std::string > &commandvec, message_handlert &message_handler)
Initiate a new subprocess with pipes supporting communication between the parent (this process) and t...
statet get_status()
Get child process status.
send_responset
Enumeration for send response.
Definition: piped_process.h:37
std::string wait_receive()
Wait until a string is available and read a string from the child process' output.
unsigned int statet