Debian Codegraph @

tools/unix/process.c

raw symbols SCIP
1 // Copyright 2007 - 2021, Alan Antonuk and the rabbitmq-c contributors.
2 // SPDX-License-Identifier: mit
3
4 #include <errno.h>
5 #include <spawn.h>
6 #include <sys/wait.h>
7 #include <unistd.h>
8
9 #include "common.h"
10 #include "process.h"
11
12 extern char **environ;
13
14 void pipeline(const char *const *argv, struct pipeline *pl) {
15 posix_spawn_file_actions_t file_acts;
16
17 int pipefds[2];
18 if (pipe(pipefds)) {
19 die_errno(errno, "pipe");
20 }
21
22 die_errno(posix_spawn_file_actions_init(&file_acts),
23 "posix_spawn_file_actions_init");
24 die_errno(posix_spawn_file_actions_adddup2(&file_acts, pipefds[0], 0),
25 "posix_spawn_file_actions_adddup2");
26 die_errno(posix_spawn_file_actions_addclose(&file_acts, pipefds[0]),
27 "posix_spawn_file_actions_addclose");
28 die_errno(posix_spawn_file_actions_addclose(&file_acts, pipefds[1]),
29 "posix_spawn_file_actions_addclose");
30
31 die_errno(posix_spawnp(&pl->pid, argv[0], &file_acts, NULL,
32 (char *const *)argv, environ),
33 "posix_spawnp: %s", argv[0]);
34
35 die_errno(posix_spawn_file_actions_destroy(&file_acts),
36 "posix_spawn_file_actions_destroy");
37
38 if (close(pipefds[0])) {
39 die_errno(errno, "close");
40 }
41
42 pl->infd = pipefds[1];
43 }
44
45 int finish_pipeline(struct pipeline *pl) {
46 int status;
47
48 if (close(pl->infd)) {
49 die_errno(errno, "close");
50 }
51 if (waitpid(pl->pid, &status, 0) < 0) {
52 die_errno(errno, "waitpid");
53 }
54 return WIFEXITED(status) && WEXITSTATUS(status) == 0;
55 }