Hermes 0.7.0
Linux game streaming host
Loading...
Searching...
No Matches
clipboard_process.h
Go to the documentation of this file.
1
5#pragma once
6
7#include <cerrno>
8#include <csignal>
9#include <cstdio>
10#include <fcntl.h>
11#include <pthread.h>
12#include <string_view>
13#include <sys/syscall.h>
14#include <sys/wait.h>
15#include <unistd.h>
16
17namespace platf::clipboard {
18 class pipe_t {
19 public:
20 // Resolve the executable in the parent. After fork, the child uses only
21 // async-signal-safe operations before execv, even in a multithreaded host.
22 pipe_t(const char *executable, const char *const arguments[], bool writing) {
23 const long max_fd = sysconf(_SC_OPEN_MAX);
24 if (max_fd < 0) {
25 return;
26 }
27 int descriptors[2];
28 if (pipe2(descriptors, O_CLOEXEC) != 0) {
29 return;
30 }
31 // Keep the pipe out of stdin/stdout/stderr, including when one of the
32 // standard descriptors was closed before Hermes started.
33 for (int &fd : descriptors) {
34 if (fd < 3) {
35 const int replacement = fcntl(fd, F_DUPFD_CLOEXEC, 3);
36 if (replacement < 0) {
37 close(descriptors[0]);
38 close(descriptors[1]);
39 return;
40 }
41 close(fd);
42 fd = replacement;
43 }
44 }
45
46 child = fork();
47 if (child == 0) {
48 const int source = descriptors[writing ? 0 : 1];
49 const int target = writing ? STDIN_FILENO : STDOUT_FILENO;
50 if (dup2(source, target) < 0) {
51 _exit(127);
52 }
53 // popen() only closes its own pipes. wl-copy and xclip daemonize,
54 // which otherwise leaves every inherited stream socket alive (#71).
55 // Close in the child, so concurrent socket creation in the parent
56 // cannot escape a snapshot of its descriptor table.
57#ifdef SYS_close_range
58 if (syscall(SYS_close_range, 3U, ~0U, 0U) != 0)
59#endif
60 {
61 for (long fd = 3; fd < max_fd; ++fd) {
62 close(static_cast<int>(fd));
63 }
64 }
65 execv(executable, const_cast<char *const *>(arguments));
66 _exit(127);
67 }
68
69 close(descriptors[writing ? 0 : 1]);
70 const int parent_fd = descriptors[writing ? 1 : 0];
71 if (child < 0) {
72 close(parent_fd);
73 return;
74 }
75 stream = fdopen(parent_fd, writing ? "w" : "r");
76 if (!stream) {
77 close(parent_fd);
78 finish();
79 }
80 }
81
82 pipe_t(const pipe_t &) = delete;
83 pipe_t &operator=(const pipe_t &) = delete;
84
85 ~pipe_t() {
86 finish();
87 }
88
89 FILE *get() const {
90 return stream;
91 }
92
93 bool finish() {
94 const int close_status = stream ? fclose(stream) : 0;
95 stream = nullptr;
96 if (child < 0) {
97 return false;
98 }
99 int status = 0;
100 pid_t result;
101 do {
102 result = waitpid(child, &status, 0);
103 } while (result < 0 && errno == EINTR);
104 child = -1;
105 return close_status == 0 && result > 0 && WIFEXITED(status) && WEXITSTATUS(status) == 0;
106 }
107
108 bool write(std::string_view content) {
109 if (!stream) {
110 return false;
111 }
112 // A missing/crashing helper must not terminate Hermes with SIGPIPE.
113 // Keep the mask through fclose(), which can flush buffered bytes.
114 sigset_t mask;
115 sigemptyset(&mask);
116 sigaddset(&mask, SIGPIPE);
117 sigset_t previous_mask;
118 if (pthread_sigmask(SIG_BLOCK, &mask, &previous_mask) != 0) {
119 return false;
120 }
121 sigset_t pending;
122 const bool had_pending_sigpipe = sigpending(&pending) == 0 && sigismember(&pending, SIGPIPE) == 1;
123 const bool wrote_all = fwrite(content.data(), 1, content.size(), stream) == content.size();
124 const bool closed = finish();
125 if (!had_pending_sigpipe) {
126 const timespec no_wait {};
127 while (sigtimedwait(&mask, nullptr, &no_wait) < 0 && errno == EINTR) {}
128 }
129 pthread_sigmask(SIG_SETMASK, &previous_mask, nullptr);
130 return wrote_all && closed;
131 }
132
133 private:
134 FILE *stream = nullptr;
135 pid_t child = -1;
136 };
137} // namespace platf::clipboard