static void prepareHandler()

in src/UnixForkHandler.cpp [39:67]


static void prepareHandler() {
  globalStdoutPipeFd[0] = globalStdoutPipeFd[1] = -1;
  globalStderrPipeFd[0] = globalStderrPipeFd[1] = -1;
  if (rpiService == nullptr) return;
  if (pipe(globalStdoutPipeFd) || pipe(globalStderrPipeFd)) {
    if (globalStdoutPipeFd[0] != -1) {
      close(globalStdoutPipeFd[0]);
      close(globalStdoutPipeFd[1]);
    }
    perror("Failed to create pipe for child process");
    return;
  }
  auto outputHandler = rpiService->getOutputHandlerForChildProcess();
  for (int id = 0; id < 2; ++id) {
    int fd = (id == 0 ? globalStdoutPipeFd[0] : globalStderrPipeFd[0]);
    std::thread handlerThread([=] {
      CloseOnExit closeOnExit(fd);
      char buf[BUF_SIZE];
      while (true) {
        ssize_t size = read(fd, buf, BUF_SIZE);
        if (size <= 0) {
          break;
        }
        outputHandler(buf, size, (OutputType)id);
      }
    });
    handlerThread.detach();
  }
}