125 const std::vector<std::string> &commandvec,
127 : log{message_handler}
131 SECURITY_ATTRIBUTES sec_attr;
132 sec_attr.nLength =
sizeof(SECURITY_ATTRIBUTES);
134 sec_attr.bInheritHandle =
TRUE;
138 sec_attr.lpSecurityDescriptor = NULL;
141 std::string base_name =
"\\\\.\\pipe\\cbmc\\child\\";
143 base_name.append(std::to_string(GetCurrentProcessId()));
144 const std::string in_name = base_name +
"\\IN";
145 child_std_IN_Wr = CreateNamedPipe(
147 PIPE_ACCESS_OUTBOUND,
148 PIPE_TYPE_BYTE | PIPE_NOWAIT,
149 PIPE_UNLIMITED_INSTANCES,
158 if(child_std_IN_Rd == INVALID_HANDLE_VALUE)
163 child_std_IN_Rd = CreateFile(
166 FILE_SHARE_READ | FILE_SHARE_WRITE,
169 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING,
171 if(child_std_IN_Wr == INVALID_HANDLE_VALUE)
175 if(!SetHandleInformation(
176 child_std_IN_Rd, HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT))
179 "Input pipe creation failed on SetHandleInformation");
181 const std::string out_name = base_name +
"\\OUT";
182 child_std_OUT_Rd = CreateNamedPipe(
185 PIPE_TYPE_BYTE | PIPE_NOWAIT,
186 PIPE_UNLIMITED_INSTANCES,
191 if(child_std_OUT_Rd == INVALID_HANDLE_VALUE)
195 child_std_OUT_Wr = CreateFile(
198 FILE_SHARE_READ | FILE_SHARE_WRITE,
201 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING,
203 if(child_std_OUT_Wr == INVALID_HANDLE_VALUE)
207 if(!SetHandleInformation(child_std_OUT_Rd, HANDLE_FLAG_INHERIT, 0))
210 "Output pipe creation failed on SetHandleInformation");
213 STARTUPINFOW start_info;
214 proc_info = std::make_unique<PROCESS_INFORMATION>();
215 ZeroMemory(proc_info.get(),
sizeof(PROCESS_INFORMATION));
216 ZeroMemory(&start_info,
sizeof(STARTUPINFOW));
217 start_info.cb =
sizeof(STARTUPINFOW);
218 start_info.hStdError = child_std_OUT_Wr;
219 start_info.hStdOutput = child_std_OUT_Wr;
220 start_info.hStdInput = child_std_IN_Rd;
221 start_info.dwFlags |= STARTF_USESTDHANDLES;
222 const std::wstring cmdline = prepare_windows_command_line(commandvec);
225 const BOOL success = CreateProcessW(
227 _wcsdup(cmdline.c_str()),
239 CloseHandle(child_std_OUT_Wr);
240 CloseHandle(child_std_IN_Rd);
280 char **args =
reinterpret_cast<char **
>(
281 malloc((commandvec.size() + 1) *
sizeof(
char *)));
284 while(i < commandvec.size())
286 args[i] = strdup(commandvec[i].c_str());
290 execvp(commandvec[0].c_str(), args);
303 std::cerr <<
"Launching " << commandvec[0]
304 <<
" failed with error: " << std::strerror(errno) << std::endl;
324 TerminateProcess(proc_info->hProcess, 0);
329 DisconnectNamedPipe(child_std_OUT_Rd);
330 DisconnectNamedPipe(child_std_IN_Wr);
331 CloseHandle(child_std_OUT_Rd);
332 CloseHandle(child_std_IN_Wr);
333 CloseHandle(proc_info->hProcess);
334 CloseHandle(proc_info->hThread);
355 DWORD bytes_written = 0;
356 const int retry_limit = 10;
357 for(
int send_attempts = 0; send_attempts < retry_limit; ++send_attempts)
366 child_std_IN_Wr, message.c_str(), message_size, &bytes_written, NULL))
368 const DWORD error_code = GetLastError();
369 log.
debug() <<
"Last error code is " + std::to_string(error_code)
373 if(bytes_written != 0)
376 const auto wait_milliseconds =
narrow<DWORD>(1 << send_attempts);
377 log.
debug() <<
"Zero bytes send to sub process. Retrying in "
379 FlushFileBuffers(child_std_IN_Wr);
380 Sleep(wait_milliseconds);
383 message_size == bytes_written,
384 "Number of bytes written to sub process must match message size."
386 std::to_string(message_size) +
" but " + std::to_string(bytes_written) +
387 " bytes were written.");
393 if(send_status == EOF)
453 const int timeout = wait_time ?
narrow<int>(*wait_time) : -1;
456 DWORD total_bytes_available = 0;
457 while(timeout < 0 || waited_time >= timeout)
459 const LPVOID lpBuffer =
nullptr;
460 const DWORD nBufferSize = 0;
461 const LPDWORD lpBytesRead =
nullptr;
462 const LPDWORD lpTotalBytesAvail = &total_bytes_available;
463 const LPDWORD lpBytesLeftThisMessage =
nullptr;
470 lpBytesLeftThisMessage);
471 if(total_bytes_available > 0)
476# define WIN_POLL_WAIT 10
477 Sleep(WIN_POLL_WAIT);
478 waited_time += WIN_POLL_WAIT;
483 pipe_output[0], POLLIN, 0
485 nfds_t nfds = POLLIN;
486 const int ready = poll(&fds, nfds, timeout);
502 if(fds.revents & POLLIN)