Debian Codegraph @

app/src/sys/unix/process.c

raw symbols SCIP
1 #include "util/process.h"
2
3 #include <assert.h>
4 #include <errno.h>
5 #include <fcntl.h>
6 #include <signal.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <sys/types.h>
10 #include <sys/wait.h>
11 #include <unistd.h>
12
13 #include "util/log.h"
14
15 enum sc_process_result
16 sc_process_execute_p(const char *const argv[], sc_pid *pid, unsigned flags,
17 int *pin, int *pout, int *perr) {
18 bool inherit_stdout = !pout && !(flags & SC_PROCESS_NO_STDOUT);
19 bool inherit_stderr = !perr && !(flags & SC_PROCESS_NO_STDERR);
20
21 int in[2];
22 int out[2];
23 int err[2];
24 int internal[2]; // communication between parent and children
25
26 if (pipe(internal) == -1) {
27 perror("pipe");
28 return SC_PROCESS_ERROR_GENERIC;
29 }
30 if (pin) {
31 if (pipe(in) == -1) {
32 perror("pipe");
33 close(internal[0]);
34 close(internal[1]);
35 return SC_PROCESS_ERROR_GENERIC;
36 }
37 }
38 if (pout) {
39 if (pipe(out) == -1) {
40 perror("pipe");
41 // clean up
42 if (pin) {
43 close(in[0]);
44 close(in[1]);
45 }
46 close(internal[0]);
47 close(internal[1]);
48 return SC_PROCESS_ERROR_GENERIC;
49 }
50 }
51 if (perr) {
52 if (pipe(err) == -1) {
53 perror("pipe");
54 // clean up
55 if (pout) {
56 close(out[0]);
57 close(out[1]);
58 }
59 if (pin) {
60 close(in[0]);
61 close(in[1]);
62 }
63 close(internal[0]);
64 close(internal[1]);
65 return SC_PROCESS_ERROR_GENERIC;
66 }
67 }
68
69 *pid = fork();
70 if (*pid == -1) {
71 perror("fork");
72 // clean up
73 if (perr) {
74 close(err[0]);
75 close(err[1]);
76 }
77 if (pout) {
78 close(out[0]);
79 close(out[1]);
80 }
81 if (pin) {
82 close(in[0]);
83 close(in[1]);
84 }
85 close(internal[0]);
86 close(internal[1]);
87 return SC_PROCESS_ERROR_GENERIC;
88 }
89
90 if (*pid == 0) {
91 if (pin) {
92 if (in[0] != STDIN_FILENO) {
93 dup2(in[0], STDIN_FILENO);
94 close(in[0]);
95 }
96 close(in[1]);
97 } else {
98 int devnull = open("/dev/null", O_RDONLY | O_CREAT, 0666);
99 if (devnull != -1) {
100 dup2(devnull, STDIN_FILENO);
101 } else {
102 LOGE("Could not open /dev/null for stdin");
103 }
104 }
105
106 if (pout) {
107 if (out[1] != STDOUT_FILENO) {
108 dup2(out[1], STDOUT_FILENO);
109 close(out[1]);
110 }
111 close(out[0]);
112 } else if (!inherit_stdout) {
113 int devnull = open("/dev/null", O_WRONLY | O_CREAT, 0666);
114 if (devnull != -1) {
115 dup2(devnull, STDOUT_FILENO);
116 } else {
117 LOGE("Could not open /dev/null for stdout");
118 }
119 }
120
121 if (perr) {
122 if (err[1] != STDERR_FILENO) {
123 dup2(err[1], STDERR_FILENO);
124 close(err[1]);
125 }
126 close(err[0]);
127 } else if (!inherit_stderr) {
128 int devnull = open("/dev/null", O_WRONLY | O_CREAT, 0666);
129 if (devnull != -1) {
130 dup2(devnull, STDERR_FILENO);
131 } else {
132 LOGE("Could not open /dev/null for stderr");
133 }
134 }
135
136 close(internal[0]);
137 enum sc_process_result err;
138
139 // Somehow SDL masks many signals - undo them for other processes
140 // https://github.com/libsdl-org/SDL/blob/release-2.0.18/src/thread/pthread/SDL_systhread.c#L167
141 sigset_t mask;
142 sigemptyset(&mask);
143 sigprocmask(SIG_SETMASK, &mask, NULL);
144
145 if (fcntl(internal[1], F_SETFD, FD_CLOEXEC) == 0) {
146 execvp(argv[0], (char *const *) argv);
147 perror("exec");
148 err = errno == ENOENT ? SC_PROCESS_ERROR_MISSING_BINARY
149 : SC_PROCESS_ERROR_GENERIC;
150 } else {
151 perror("fcntl");
152 err = SC_PROCESS_ERROR_GENERIC;
153 }
154 // send err to the parent
155 if (write(internal[1], &err, sizeof(err)) == -1) {
156 perror("write");
157 }
158 close(internal[1]);
159 _exit(1);
160 }
161
162 // parent
163 assert(*pid > 0);
164
165 close(internal[1]);
166
167 enum sc_process_result res = SC_PROCESS_SUCCESS;
168 // wait for EOF or receive err from child
169 if (read(internal[0], &res, sizeof(res)) == -1) {
170 perror("read");
171 res = SC_PROCESS_ERROR_GENERIC;
172 }
173
174 close(internal[0]);
175
176 if (pin) {
177 close(in[0]);
178 *pin = in[1];
179 }
180 if (pout) {
181 *pout = out[0];
182 close(out[1]);
183 }
184 if (perr) {
185 *perr = err[0];
186 close(err[1]);
187 }
188
189 return res;
190 }
191
192 bool
193 sc_process_terminate(pid_t pid) {
194 if (pid <= 0) {
195 LOGE("Requested to kill %d, this is an error. Please report the bug.\n",
196 (int) pid);
197 abort();
198 }
199 return kill(pid, SIGKILL) != -1;
200 }
201
202 sc_exit_code
203 sc_process_wait(pid_t pid, bool close) {
204 int code;
205 int options = WEXITED;
206 if (!close) {
207 options |= WNOWAIT;
208 }
209
210 siginfo_t info;
211 int r = waitid(P_PID, pid, &info, options);
212 if (r == -1 || info.si_code != CLD_EXITED) {
213 // could not wait, or exited unexpectedly, probably by a signal
214 code = SC_EXIT_CODE_NONE;
215 } else {
216 code = info.si_status;
217 }
218 return code;
219 }
220
221 void
222 sc_process_close(pid_t pid) {
223 sc_process_wait(pid, true); // ignore exit code
224 }
225
226 ssize_t
227 sc_pipe_read(int pipe, char *data, size_t len) {
228 return read(pipe, data, len);
229 }
230
231 void
232 sc_pipe_close(int pipe) {
233 if (close(pipe)) {
234 perror("close pipe");
235 }
236 }