128 const std::vector<std::string> &commandvec,
130 :
log{message_handler}
134 SECURITY_ATTRIBUTES sec_attr;
135 sec_attr.nLength =
sizeof(SECURITY_ATTRIBUTES);
137 sec_attr.bInheritHandle =
TRUE;
141 sec_attr.lpSecurityDescriptor = NULL;
144 std::string base_name =
"\\\\.\\pipe\\cbmc\\child\\";
146 base_name.append(std::to_string(GetCurrentProcessId()));
147 const std::string in_name = base_name +
"\\IN";
148 child_std_IN_Wr = CreateNamedPipe(
150 PIPE_ACCESS_OUTBOUND,
151 PIPE_TYPE_BYTE | PIPE_NOWAIT,
152 PIPE_UNLIMITED_INSTANCES,
161 if(child_std_IN_Rd == INVALID_HANDLE_VALUE)
166 child_std_IN_Rd = CreateFile(
169 FILE_SHARE_READ | FILE_SHARE_WRITE,
172 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING,
174 if(child_std_IN_Wr == INVALID_HANDLE_VALUE)
178 if(!SetHandleInformation(
179 child_std_IN_Rd, HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT))
182 "Input pipe creation failed on SetHandleInformation");
184 const std::string out_name = base_name +
"\\OUT";
185 child_std_OUT_Rd = CreateNamedPipe(
188 PIPE_TYPE_BYTE | PIPE_NOWAIT,
189 PIPE_UNLIMITED_INSTANCES,
194 if(child_std_OUT_Rd == INVALID_HANDLE_VALUE)
198 child_std_OUT_Wr = CreateFile(
201 FILE_SHARE_READ | FILE_SHARE_WRITE,
204 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING,
206 if(child_std_OUT_Wr == INVALID_HANDLE_VALUE)
210 if(!SetHandleInformation(child_std_OUT_Rd, HANDLE_FLAG_INHERIT, 0))
213 "Output pipe creation failed on SetHandleInformation");
216 STARTUPINFOW start_info;
217 proc_info = std::make_unique<PROCESS_INFORMATION>();
218 ZeroMemory(proc_info.get(),
sizeof(PROCESS_INFORMATION));
219 ZeroMemory(&start_info,
sizeof(STARTUPINFOW));
220 start_info.cb =
sizeof(STARTUPINFOW);
221 start_info.hStdError = child_std_OUT_Wr;
222 start_info.hStdOutput = child_std_OUT_Wr;
223 start_info.hStdInput = child_std_IN_Rd;
224 start_info.dwFlags |= STARTF_USESTDHANDLES;
225 const std::wstring cmdline = prepare_windows_command_line(commandvec);
228 const BOOL success = CreateProcessW(
230 _wcsdup(cmdline.c_str()),
242 CloseHandle(child_std_OUT_Wr);
243 CloseHandle(child_std_IN_Rd);
275 prctl(PR_SET_PDEATHSIG, SIGTERM);
294 char **args =
reinterpret_cast<char **
>(
295 malloc((commandvec.size() + 1) *
sizeof(
char *)));
298 while(i < commandvec.size())
300 args[i] = strdup(commandvec[i].c_str());
304 execvp(commandvec[0].c_str(), args);
317 std::cerr <<
"Launching " << commandvec[0]
318 <<
" failed with error: " << std::strerror(errno) << std::endl;
369 DWORD bytes_written = 0;
370 const int retry_limit = 10;
371 for(
int send_attempts = 0; send_attempts < retry_limit; ++send_attempts)
380 child_std_IN_Wr, message.c_str(), message_size, &bytes_written, NULL))
382 const DWORD error_code = GetLastError();
383 log.debug() <<
"Last error code is " + std::to_string(error_code)
387 if(bytes_written != 0)
390 const auto wait_milliseconds =
narrow<DWORD>(1 << send_attempts);
391 log.debug() <<
"Zero bytes send to sub process. Retrying in "
393 FlushFileBuffers(child_std_IN_Wr);
394 Sleep(wait_milliseconds);
397 message_size == bytes_written,
398 "Number of bytes written to sub process must match message size."
400 std::to_string(message_size) +
" but " + std::to_string(bytes_written) +
401 " bytes were written.");
407 if(send_status == EOF)
467 const int timeout = wait_time ?
narrow<int>(*wait_time) : -1;
470 DWORD total_bytes_available = 0;
471 while(timeout < 0 || waited_time >= timeout)
473 const LPVOID lpBuffer =
nullptr;
474 const DWORD nBufferSize = 0;
475 const LPDWORD lpBytesRead =
nullptr;
476 const LPDWORD lpTotalBytesAvail = &total_bytes_available;
477 const LPDWORD lpBytesLeftThisMessage =
nullptr;
484 lpBytesLeftThisMessage);
485 if(total_bytes_available > 0)
490# define WIN_POLL_WAIT 10
491 Sleep(WIN_POLL_WAIT);
492 waited_time += WIN_POLL_WAIT;
499 nfds_t nfds = POLLIN;
500 const int ready = poll(&fds, nfds, timeout);
516 if(fds.revents & POLLIN)