102 # define BUFSIZE 2048
111 prepare_windows_command_line(
const std::vector<std::string> &commandvec)
113 std::wstring result =
widen(commandvec[0]);
114 for(
int i = 1; i < commandvec.size(); i++)
117 result.append(quote_windows_arg(
widen(commandvec[i])));
127 SECURITY_ATTRIBUTES sec_attr;
128 sec_attr.nLength =
sizeof(SECURITY_ATTRIBUTES);
130 sec_attr.bInheritHandle =
TRUE;
134 sec_attr.lpSecurityDescriptor = NULL;
137 std::string base_name =
"\\\\.\\pipe\\cbmc\\child\\";
140 const std::string in_name = base_name +
"\\IN";
141 child_std_IN_Wr = CreateNamedPipe(
143 PIPE_ACCESS_OUTBOUND,
144 PIPE_TYPE_BYTE | PIPE_NOWAIT,
145 PIPE_UNLIMITED_INSTANCES,
154 if(child_std_IN_Rd == INVALID_HANDLE_VALUE)
159 child_std_IN_Rd = CreateFile(
162 FILE_SHARE_READ | FILE_SHARE_WRITE,
165 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING,
167 if(child_std_IN_Wr == INVALID_HANDLE_VALUE)
171 if(!SetHandleInformation(
172 child_std_IN_Rd, HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT))
175 "Input pipe creation failed on SetHandleInformation");
177 const std::string out_name = base_name +
"\\OUT";
178 child_std_OUT_Rd = CreateNamedPipe(
181 PIPE_TYPE_BYTE | PIPE_NOWAIT,
182 PIPE_UNLIMITED_INSTANCES,
187 if(child_std_OUT_Rd == INVALID_HANDLE_VALUE)
191 child_std_OUT_Wr = CreateFile(
194 FILE_SHARE_READ | FILE_SHARE_WRITE,
197 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING,
199 if(child_std_OUT_Wr == INVALID_HANDLE_VALUE)
203 if(!SetHandleInformation(child_std_OUT_Rd, HANDLE_FLAG_INHERIT, 0))
206 "Output pipe creation failed on SetHandleInformation");
209 STARTUPINFOW start_info;
210 proc_info = util_make_unique<PROCESS_INFORMATION>();
211 ZeroMemory(proc_info.get(),
sizeof(PROCESS_INFORMATION));
212 ZeroMemory(&start_info,
sizeof(STARTUPINFOW));
213 start_info.cb =
sizeof(STARTUPINFOW);
214 start_info.hStdError = child_std_OUT_Wr;
215 start_info.hStdOutput = child_std_OUT_Wr;
216 start_info.hStdInput = child_std_IN_Rd;
217 start_info.dwFlags |= STARTF_USESTDHANDLES;
218 const std::wstring cmdline = prepare_windows_command_line(commandvec);
221 const BOOL success = CreateProcessW(
223 _wcsdup(cmdline.c_str()),
235 CloseHandle(child_std_OUT_Wr);
236 CloseHandle(child_std_IN_Rd);
277 reinterpret_cast<char **
>(malloc((commandvec.size()) *
sizeof(
char *)));
280 while(i < commandvec.size())
282 args[i] = strdup(commandvec[i].c_str());
286 execvp(commandvec[0].c_str(), args);
299 std::cerr <<
"Launching " << commandvec[0]
300 <<
" failed with error: " << std::strerror(errno) << std::endl;
320 TerminateProcess(proc_info->hProcess, 0);
325 DisconnectNamedPipe(child_std_OUT_Rd);
326 DisconnectNamedPipe(child_std_IN_Wr);
327 CloseHandle(child_std_OUT_Rd);
328 CloseHandle(child_std_IN_Wr);
329 CloseHandle(proc_info->hProcess);
330 CloseHandle(proc_info->hThread);
349 const auto message_size = narrow<DWORD>(message.size());
350 DWORD bytes_written = 0;
352 child_std_IN_Wr, message.c_str(), message_size, &bytes_written, NULL))
358 message_size == bytes_written,
359 "Number of bytes written to sub process must match message size.");
365 if(send_status == EOF)
377 "Can only receive() from a fully initialised process");
378 std::string response = std::string(
"");
389 success = ReadFile(child_std_OUT_Rd, buff,
BUFSIZE, &nbytes, NULL);
396 success = nbytes > 0;
400 "More bytes cannot be read at a time, than the size of the buffer");
403 response.append(buff, nbytes);
425 const int timeout = wait_time ? narrow<int>(*wait_time) : -1;
428 DWORD total_bytes_available = 0;
429 while(timeout < 0 || waited_time >= timeout)
431 const LPVOID lpBuffer =
nullptr;
432 const DWORD nBufferSize = 0;
433 const LPDWORD lpBytesRead =
nullptr;
434 const LPDWORD lpTotalBytesAvail = &total_bytes_available;
435 const LPDWORD lpBytesLeftThisMessage =
nullptr;
442 lpBytesLeftThisMessage);
443 if(total_bytes_available > 0)
448 # define WIN_POLL_WAIT 10
449 Sleep(WIN_POLL_WAIT);
450 waited_time += WIN_POLL_WAIT;
457 nfds_t nfds = POLLIN;
458 const int ready = poll(&fds, nfds, timeout);
474 if(fds.revents & POLLIN)
bool can_receive()
See if this process can receive data from the other process.
piped_processt(const std::vector< std::string > &commandvec)
Initiate a new subprocess with pipes supporting communication between the parent (this process) and t...
void wait_receivable(int wait_time)
Wait for the pipe to be ready, waiting specified time between checks.
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.
statet
Enumeration to keep track of child process state.
statet get_status()
Get child process status.
send_responset
Enumeration for send response.
std::string wait_receive()
Wait until a string is available and read a string from the child process' output.
Thrown when some external system fails unexpectedly.
nonstd::optional< T > optionalt
Subprocess communication with pipes.
#define PIPED_PROCESS_INFINITE_TIMEOUT
std::string to_string(const string_not_contains_constraintt &expr)
Used for debug printing.
std::wstring widen(const char *s)