-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfork_exec.c
138 lines (114 loc) · 2.73 KB
/
fork_exec.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
* fork_exec.c - spawn a process and pipe its three standard streams
* Copyright (C) 2014 Vivien Didelot
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include "log.h"
static int
dup_and_close(int old, int new)
{
/* Defensive check */
if (old == new)
return 0;
if (dup2(old, new) == -1) {
ERRORX("dup2(%d, %d)", old, new);
return 1;
}
if (close(old) == -1) {
ERRORX("close(%d)", old);
return 1;
}
return 0;
}
/*
* Fork and exec a vector command line.
* Return -1 on error, the child pid otherwise.
*/
pid_t
fork_exec(char *const argv[], int io[3])
{
int in[2], out[2], err[2];
pid_t pid;
if (pipe(in) == -1) {
ERRORX("pipe stdin");
return -1;
}
if (pipe(out) == -1) {
ERRORX("pipe stdout");
return -1;
}
if (pipe(err) == -1) {
ERRORX("pipe stderr");
return -1;
}
pid = fork();
switch (pid) {
case -1:
ERRORX("fork");
return -1;
case 0:
/* Child, the filter process */
/* Close unused pipe ends */
if (close(in[1]) == -1) {
ERRORX("close stdin write end");
exit(1);
}
if (close(out[0]) == -1) {
ERRORX("close stdout read end");
exit(1);
}
if (close(err[0]) == -1) {
ERRORX("close stderr read end");
exit(1);
}
/* Connect stdin on read end of stdin pipe */
if (dup_and_close(in[0], STDIN_FILENO))
exit(1);
/* Connect stdout on write end of stdout pipe */
if (dup_and_close(out[1], STDOUT_FILENO))
exit(1);
/* Connect stderr on write end of stderr pipe */
if (dup_and_close(err[1], STDERR_FILENO))
exit(1);
/* Finally execute the program */
execv(argv[0], argv);
/* We should not reach this point */
ERRORX("execv %s", argv[0]);
exit(1);
default:
/* Parent */
/* Close unused pipe ends */
if (close(in[0]) == -1) {
ERRORX("close stdin read end");
return -1;
}
if (close(out[1]) == -1) {
ERRORX("close stdout write end");
return -1;
}
if (close(err[1]) == -1) {
ERRORX("close stderr write end");
return -1;
}
io[0] = in[1]; /* Filter's stdin */
io[1] = out[0]; /* Filter's stdout */
io[2] = err[0]; /* Filter's stderr */
break;
}
return pid;
}