aboutsummaryrefslogtreecommitdiff
path: root/src/src/process_helper_main.cpp
blob: 185111c8cc026b0e86180d872e9478004c5ae9f4 (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
// Standalone process helper for Flatpak game launching.
// Runs on the host via flatpak-spawn --host, keeping the flatpak-spawn
// proxy alive for MO2's PID polling while the game process tree is running.
//
// Protocol (stdin/stdout, line-oriented):
//   Config phase: MO2 writes key=value lines terminated by a blank line
//     program=<path>, arg=<value> (repeatable), env=KEY=VALUE (repeatable),
//     workdir=<path>
//   Helper responds: "started <pid>" or "error <message>"
//   Runtime commands (MO2→helper): "kill" (SIGTERM child tree), "quit"
//   Helper reports: "exited <code>" when game process tree exits

#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <poll.h>
#include <signal.h>
#include <sys/wait.h>
#include <unistd.h>

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <string>
#include <unordered_set>
#include <vector>

// ── Helpers ──

static void writeResponse(const std::string& msg)
{
  std::string line = msg + "\n";
  ::write(STDOUT_FILENO, line.data(), line.size());
}

static bool readLine(std::string& out, int timeoutMs)
{
  out.clear();

  struct pollfd pfd{};
  pfd.fd     = STDIN_FILENO;
  pfd.events = POLLIN;

  while (true) {
    int ret = ::poll(&pfd, 1, timeoutMs);
    if (ret < 0) {
      if (errno == EINTR)
        continue;
      return false;
    }
    if (ret == 0) {
      // timeout
      return false;
    }

    // Try reading if data is available, even if HUP is also set
    // (pipe can have buffered data when the writer closes).
    if (!(pfd.revents & POLLIN)) {
      // No data available — must be HUP or ERR only
      return false;
    }

    char ch = 0;
    ssize_t n = ::read(STDIN_FILENO, &ch, 1);
    if (n <= 0) {
      return false;
    }
    if (ch == '\n') {
      return true;
    }
    out.push_back(ch);
  }
}

// Collect all descendant PIDs of a given root by scanning /proc.
static std::unordered_set<pid_t> collectDescendants(pid_t root)
{
  std::unordered_set<pid_t> descendants;

  // Build parent→children map
  struct ProcEntry {
    pid_t pid;
    pid_t ppid;
  };
  std::vector<ProcEntry> entries;

  DIR* proc = opendir("/proc");
  if (!proc) {
    return descendants;
  }

  struct dirent* entry = nullptr;
  while ((entry = readdir(proc)) != nullptr) {
    if (entry->d_type != DT_DIR && entry->d_type != DT_UNKNOWN) {
      continue;
    }

    char* end = nullptr;
    long pidLong = strtol(entry->d_name, &end, 10);
    if (end == entry->d_name || *end != '\0' || pidLong <= 0) {
      continue;
    }

    pid_t pid = static_cast<pid_t>(pidLong);

    char statusPath[64];
    snprintf(statusPath, sizeof(statusPath), "/proc/%ld/status", pidLong);

    std::ifstream status(statusPath);
    if (!status.is_open()) {
      continue;
    }

    std::string line;
    pid_t ppid = 0;
    while (std::getline(status, line)) {
      if (line.rfind("PPid:", 0) == 0) {
        ppid = static_cast<pid_t>(strtol(line.c_str() + 5, nullptr, 10));
        break;
      }
    }

    if (ppid > 0) {
      entries.push_back({pid, ppid});
    }
  }
  closedir(proc);

  // BFS from root
  std::vector<pid_t> queue;
  queue.push_back(root);

  while (!queue.empty()) {
    pid_t cur = queue.back();
    queue.pop_back();

    for (const auto& e : entries) {
      if (e.ppid == cur && descendants.find(e.pid) == descendants.end()) {
        descendants.insert(e.pid);
        queue.push_back(e.pid);
      }
    }
  }

  return descendants;
}

// Check if any process in the given set is still alive.
static bool anyAlive(const std::unordered_set<pid_t>& pids)
{
  for (pid_t p : pids) {
    if (::kill(p, 0) == 0 || errno == EPERM) {
      return true;
    }
  }
  return false;
}

// ── Main ──

int main()
{
  // Make stdout line-buffered for reliable protocol messages.
  setvbuf(stdout, nullptr, _IOLBF, 0);

  // ── Config phase: read key=value lines until blank line ──
  std::string program;
  std::vector<std::string> args;
  std::vector<std::string> envVars;  // "KEY=VALUE"
  std::string workdir;

  while (true) {
    std::string line;
    if (!readLine(line, 30000)) {
      writeResponse("error stdin closed or timeout during config");
      return 1;
    }

    if (line.empty()) {
      break;  // blank line = end of config
    }

    auto eq = line.find('=');
    if (eq == std::string::npos) {
      continue;
    }

    std::string key = line.substr(0, eq);
    std::string val = line.substr(eq + 1);

    if (key == "program") {
      program = val;
    } else if (key == "arg") {
      args.push_back(val);
    } else if (key == "env") {
      envVars.push_back(val);
    } else if (key == "workdir") {
      workdir = val;
    }
  }

  if (program.empty()) {
    writeResponse("error no program specified");
    return 1;
  }

  // ── Pipe for exec error reporting ──
  // Child writes errno to this pipe if execvp fails; parent reads it.
  int errPipe[2];
  if (::pipe2(errPipe, O_CLOEXEC) != 0) {
    writeResponse("error pipe2 failed: " + std::string(strerror(errno)));
    return 1;
  }

  // ── Fork ──
  pid_t child = ::fork();
  if (child < 0) {
    writeResponse("error fork failed: " + std::string(strerror(errno)));
    return 1;
  }

  if (child == 0) {
    // ── Child ──
    ::close(errPipe[0]);  // close read end

    // New session so we can kill the whole process group later.
    ::setsid();

    // Set environment variables.
    for (const auto& ev : envVars) {
      ::putenv(const_cast<char*>(ev.c_str()));
    }

    // Change working directory.
    if (!workdir.empty()) {
      if (::chdir(workdir.c_str()) != 0) {
        int err = errno;
        (void)::write(errPipe[1], &err, sizeof(err));
        ::_exit(127);
      }
    }

    // Build argv for execvp.
    std::vector<const char*> argv;
    argv.push_back(program.c_str());
    for (const auto& a : args) {
      argv.push_back(a.c_str());
    }
    argv.push_back(nullptr);

    ::execvp(program.c_str(), const_cast<char* const*>(argv.data()));

    // If we get here, exec failed.
    int err = errno;
    (void)::write(errPipe[1], &err, sizeof(err));
    ::_exit(127);
  }

  // ── Parent ──
  ::close(errPipe[1]);  // close write end

  // Check if exec succeeded (pipe closes on successful exec due to O_CLOEXEC).
  int execErr = 0;
  ssize_t n   = ::read(errPipe[0], &execErr, sizeof(execErr));
  ::close(errPipe[0]);

  if (n > 0) {
    // exec failed in child
    ::waitpid(child, nullptr, 0);
    writeResponse("error exec failed: " + std::string(strerror(execErr)));
    return 1;
  }

  // Success - report PID.
  writeResponse("started " + std::to_string(child));

  // ── Monitor loop ──
  // Wait for the direct child and then monitor descendants (handles Proton
  // chain: proton → wine → game.exe).
  bool childExited    = false;
  int childStatus     = 0;
  bool quit           = false;

  while (!quit) {
    // Check for commands on stdin.
    std::string cmd;
    // Non-blocking: if readLine returns false due to timeout, that's fine.
    // If it returns false due to pipe close, MO2 crashed — kill child group.
    struct pollfd pfd{};
    pfd.fd     = STDIN_FILENO;
    pfd.events = POLLIN;

    int pollRet = ::poll(&pfd, 1, 200);

    if (pollRet > 0) {
      if (pfd.revents & (POLLHUP | POLLERR)) {
        // MO2 crashed or closed pipe — kill child group and exit.
        ::kill(-child, SIGTERM);
        break;
      }

      if (pfd.revents & POLLIN) {
        if (readLine(cmd, 0)) {
          if (cmd == "kill") {
            ::kill(-child, SIGTERM);
          } else if (cmd == "quit") {
            quit = true;
            break;
          }
        } else {
          // read failed = pipe closed
          ::kill(-child, SIGTERM);
          break;
        }
      }
    }

    // Check child status.
    if (!childExited) {
      int status  = 0;
      pid_t ret   = ::waitpid(child, &status, WNOHANG);
      if (ret == child) {
        childExited = true;
        childStatus = status;
      } else if (ret < 0 && errno != EINTR) {
        // Child somehow lost
        childExited = true;
        childStatus = 0;
      }
    }

    if (childExited) {
      // Check for surviving descendants (e.g., game.exe still running
      // after the proton wrapper exits).
      auto desc = collectDescendants(child);
      if (desc.empty() || !anyAlive(desc)) {
        // All done.
        int exitCode = 0;
        if (WIFEXITED(childStatus)) {
          exitCode = WEXITSTATUS(childStatus);
        } else if (WIFSIGNALED(childStatus)) {
          exitCode = 128 + WTERMSIG(childStatus);
        }
        writeResponse("exited " + std::to_string(exitCode));
        return 0;
      }
      // Descendants still alive, keep monitoring.
    }
  }

  // If we broke out of the loop, reap child if needed.
  if (!childExited) {
    ::waitpid(child, nullptr, 0);
  }

  writeResponse("exited 0");
  return 0;
}