Hermes 0.7.0
Linux game streaming host
Loading...
Searching...
No Matches
bounded_command.h
Go to the documentation of this file.
1
5#pragma once
6
7#include <array>
8#include <cerrno>
9#include <chrono>
10#include <csignal>
11#include <optional>
12#include <shared_mutex>
13#include <string>
14#include <thread>
15#include <fcntl.h>
16#include <poll.h>
17#include <sys/wait.h>
18#include <unistd.h>
19
20namespace platf::bounded_command {
28 inline std::shared_mutex &reap_mutex() {
29 static std::shared_mutex mutex;
30 return mutex;
31 }
32
33 inline std::optional<std::string> output(const std::string &command, std::chrono::milliseconds timeout, bool *timed_out) {
34 std::shared_lock reap_lock {reap_mutex()};
35 if (timed_out) {
36 *timed_out = false;
37 }
38 int out[2];
39 if (::pipe2(out, O_CLOEXEC) != 0) {
40 return std::nullopt;
41 }
42 const pid_t child = ::fork();
43 if (child < 0) {
44 ::close(out[0]);
45 ::close(out[1]);
46 return std::nullopt;
47 }
48 if (child == 0) {
49 // Its own process group, so that running over takes down whatever the
50 // shell started along with the shell.
51 ::setpgid(0, 0);
52 ::dup2(out[1], STDOUT_FILENO);
53 ::execl("/bin/sh", "sh", "-c", command.c_str(), static_cast<char *>(nullptr));
54 ::_exit(127);
55 }
56 ::setpgid(child, child); // Also close the fork-to-setpgid race from the parent.
57 ::close(out[1]);
58
59 std::string output;
60 std::array<char, 4096> buffer {};
61 const auto deadline = std::chrono::steady_clock::now() + timeout;
62 bool pipe_open = true;
63 bool exited = false;
64 bool expired = false;
65 int status = -1;
66 while (pipe_open || !exited) {
67 if (std::chrono::steady_clock::now() >= deadline) {
68 expired = true;
69 break;
70 }
71 if (pipe_open) {
72 // Once the command has exited, whatever is still in the pipe is all
73 // there will be: a descendant that kept it open is not waited for.
74 pollfd readable {out[0], POLLIN, 0};
75 const int ready = ::poll(&readable, 1, exited ? 0 : 20);
76 if (ready > 0) {
77 const ssize_t got = ::read(out[0], buffer.data(), buffer.size());
78 if (got > 0) {
79 output.append(buffer.data(), static_cast<size_t>(got));
80 } else if (got == 0 || errno != EINTR) {
81 pipe_open = false;
82 }
83 } else if (exited && ready == 0) {
84 pipe_open = false;
85 }
86 } else {
87 std::this_thread::sleep_for(std::chrono::milliseconds {10});
88 }
89 if (!exited) {
90 const pid_t reaped = ::waitpid(child, &status, WNOHANG);
91 if (reaped == child) {
92 exited = true;
93 } else if (reaped < 0 && errno != EINTR) {
94 exited = true; // Not ours to wait for any more; the status is unknown.
95 status = -1;
96 }
97 }
98 }
99 ::close(out[0]);
100
101 if (expired) {
102 ::kill(-child, SIGKILL);
103 ::kill(child, SIGKILL);
104 if (!exited) {
105 // SIGKILL cannot immediately finish a task waiting in the kernel.
106 // Never turn a command timeout into an unbounded waitpid under the
107 // display locks. The ordinary child reaper collects a late exit.
108 const auto reap_deadline = std::chrono::steady_clock::now() + std::chrono::milliseconds {100};
109 do {
110 const auto reaped = ::waitpid(child, &status, WNOHANG);
111 if (reaped == child || (reaped < 0 && errno != EINTR)) {
112 break;
113 }
114 std::this_thread::sleep_for(std::chrono::milliseconds {1});
115 } while (std::chrono::steady_clock::now() < reap_deadline);
116 }
117 if (timed_out) {
118 *timed_out = true;
119 }
120 return std::nullopt;
121 }
122 if (status == -1 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) {
123 return std::nullopt;
124 }
125 return output;
126 }
127
128} // namespace platf::bounded_command
std::shared_mutex & reap_mutex()
@ timeout
Timeout.
Definition common.h:438
int command(const char *name, int argc, char *argv[])
hermes --prelogin status|enable|disable.
Definition prelogin.cpp:632