MCPcopy Create free account
hub / github.com/USBGuard/usbguard / runExecutable

Method runExecutable

src/CLI/IPCSignalWatcher.cpp:206–270  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

204 }
205
206 void IPCSignalWatcher::runExecutable(const std::map<std::string, std::string>& environment)
207 {
208 /*
209 * Fork child process, that will fork again and exec.
210 */
211 const ::pid_t pid_fork1 = fork();
212
213 if (pid_fork1 == -1) {
214 std::cerr << "ERROR: runExecutable: " << strerror(errno) << std::endl;
215 return;
216 }
217
218 if (pid_fork1 > 0) {
219 /*
220 * Wait for the intermediary process to fork and exit. In case something
221 * fails in the intermediary process, we can retrieve the error code from
222 * the return status.
223 */
224 int fork1_status = -1;
225 const ::pid_t pid_waited = ::waitpid(pid_fork1, &fork1_status, 0);
226
227 if (pid_waited == -1) {
228 std::cerr << "ERROR: runExecutable: " << strerror(errno) << std::endl;
229 }
230 else if (WIFEXITED(fork1_status)) {
231 const int fork1_errno = WEXITSTATUS(fork1_status);
232
233 if (fork1_errno != 0) {
234 std::cerr << "ERROR: runExecutable: " << strerror(errno) << std::endl;
235 }
236 }
237 else if (WIFSIGNALED(fork1_status)) {
238 const int fork1_signal = WTERMSIG(fork1_status);
239 std::cerr << "ERROR: runExecutable: terminated by signal " << fork1_signal << std::endl;
240 }
241
242 return;
243 }
244
245 /*
246 * Intermediary process.
247 */
248 const ::pid_t sid = ::setsid();
249
250 if (sid == (::pid_t)-1) {
251 ::_exit(errno);
252 }
253
254 const ::pid_t pid_fork2 = fork();
255
256 if (pid_fork2 == (::pid_t)-1) {
257 ::_exit(errno);
258 }
259
260 if (pid_fork2 > 0) {
261 ::_exit(0);
262 }
263

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected