aboutsummaryrefslogtreecommitdiff
path: root/src/common/platform/unix/PosixSpawnSubProcess.cpp
blob: 8b9a9a478675bbd846862cb4a0f3c0bde9abcb73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include "cru/common/platform/unix/PosixSpawnSubProcess.h"
#include "cru/common/Exception.h"
#include "cru/common/Format.h"
#include "cru/common/Guard.h"
#include "cru/common/String.h"
#include "cru/common/SubProcess.h"

#include <spawn.h>
#include <unistd.h>
#include <memory>
#include <unordered_map>

namespace cru::platform::unix {
PosixSpawnSubProcess::PosixSpawnSubProcess(
    const PlatformSubProcessStartInfo& start_info)
    : PlatformSubProcessBase(start_info),
      pid_(0),
      exit_code_(0),
      stdin_pipe_(UnixPipe::Usage::Send),
      stdout_pipe_(UnixPipe::Usage::Receive),
      stderr_pipe_(UnixPipe::Usage::Receive) {
  stdin_stream_ = std::make_unique<UnixFileStream>(
      stdin_pipe_.GetSelfFileDescriptor(), false, false, true, false);
  stdout_stream_ = std::make_unique<UnixFileStream>(
      stdout_pipe_.GetSelfFileDescriptor(), false, true, false, false);
  stderr_stream_ = std::make_unique<UnixFileStream>(
      stderr_pipe_.GetSelfFileDescriptor(), false, true, false, false);

  stdout_buffer_stream_ =
      std::make_unique<io::AutoReadStream>(stdout_stream_.get(), false);
  stderr_buffer_stream_ =
      std::make_unique<io::AutoReadStream>(stdout_stream_.get(), false);
}

PosixSpawnSubProcess::~PosixSpawnSubProcess() {}

io::Stream* PosixSpawnSubProcess::GetStdinStream() {
  return stdin_stream_.get();
}

io::Stream* PosixSpawnSubProcess::GetStdoutStream() {
  return stdout_buffer_stream_.get();
}

io::Stream* PosixSpawnSubProcess::GetStderrStream() {
  return stderr_buffer_stream_.get();
}

namespace {
char** CreateCstrArray(const std::vector<String>& argv) {
  std::vector<Buffer> utf8_argv;
  for (const auto& arg : argv) {
    utf8_argv.push_back(arg.ToUtf8Buffer());
  }
  char** result = new char*[utf8_argv.size() + 1];
  for (int i = 0; i < utf8_argv.size(); i++) {
    result[i] = reinterpret_cast<char*>(utf8_argv[i].Detach());
  }
  result[utf8_argv.size()] = nullptr;
  return result;
}

char** CreateCstrArray(const std::unordered_map<String, String>& envp) {
  std::vector<String> str_array;
  for (auto& [key, value] : envp) {
    str_array.push_back(cru::Format(u"{}={}", key, value));
  }
  return CreateCstrArray(str_array);
}

void DestroyCstrArray(char** argv) {
  int index = 0;
  char* arg = argv[index];
  while (arg) {
    delete[] arg;
    index++;
    arg = argv[index];
  }
  delete[] argv;
}
}  // namespace

void PosixSpawnSubProcess::PlatformCreateProcess() {
  int error;
  auto check_error = [&error](String message) {
    if (error == 0) return;
    std::unique_ptr<ErrnoException> inner(new ErrnoException({}, error));
    throw SubProcessFailedToStartException(std::move(message),
                                           std::move(inner));
  };

  posix_spawn_file_actions_t file_actions;
  error = posix_spawn_file_actions_init(&file_actions);
  check_error(u"Failed to call posix_spawn_file_actions_init.");
  Guard file_actions_guard(
      [&file_actions] { posix_spawn_file_actions_destroy(&file_actions); });

  // dup2 stdin/stdout/stderr
  error = posix_spawn_file_actions_adddup2(
      &file_actions, stdin_pipe_.GetOtherFileDescriptor(), STDIN_FILENO);
  check_error(u"Failed to call posix_spawn_file_actions_adddup2 on stdin.");
  error = posix_spawn_file_actions_adddup2(
      &file_actions, stdout_pipe_.GetOtherFileDescriptor(), STDOUT_FILENO);
  check_error(u"Failed to call posix_spawn_file_actions_adddup2 on stdout.");
  error = posix_spawn_file_actions_adddup2(
      &file_actions, stderr_pipe_.GetOtherFileDescriptor(), STDERR_FILENO);
  check_error(u"Failed to call posix_spawn_file_actions_adddup2 on stderr.");

  posix_spawnattr_t attr;
  error = posix_spawnattr_init(&attr);
  check_error(u"Failed to call posix_spawnattr_init.");
  Guard attr_guard([&attr] { posix_spawnattr_destroy(&attr); });

#ifdef CRU_PLATFORM_OSX
  error = posix_spawnattr_setflags(&attr, POSIX_SPAWN_CLOEXEC_DEFAULT);
  check_error(u"Failed to set flag POSIX_SPAWN_CLOEXEC_DEFAULT (osx).");
#endif

  auto exe = start_info_.program.ToUtf8();

  auto argv = CreateCstrArray(start_info_.arguments);
  Guard argv_guard([argv] { DestroyCstrArray(argv); });

  auto envp = CreateCstrArray(start_info_.environments);
  Guard envp_guard([envp] { DestroyCstrArray(envp); });

  error = posix_spawnp(&pid_, exe.c_str(), &file_actions, &attr, argv, envp);
  check_error(u"Failed to call posix_spawnp.");
}

PlatformSubProcessExitResult PosixSpawnSubProcess::PlatformWaitForProcess() {}

void PosixSpawnSubProcess::PlatformKillProcess() {}
}  // namespace cru::platform::unix