-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExecuteBinary.cpp
More file actions
186 lines (152 loc) · 4.23 KB
/
ExecuteBinary.cpp
File metadata and controls
186 lines (152 loc) · 4.23 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/**
* \file ExecuteBinary.cpp
* \brief
*
* \review
*
* https://www.oreilly.com/library/view/secure-programming-cookbook/0596003943/ch01s07.html
*/
#include <StdStream/StdStream.h>
#include <StdTest/StdTest.h>
#include <Stl.h>
#include <sys/wait.h>
//-------------------------------------------------------------------------------------------------
struct SpcPipe
{
FILE *fdRead {};
FILE *fdWrite {};
pid_t pidChild {};
void clear()
{
fdRead = 0;
fdWrite = 0;
pidChild = -1;
};
};
//-------------------------------------------------------------------------------------------------
/**
* The following function, spc_fork( ) , is a wrapper around fork( ). As presented here,
* the code is incomplete when using an application-level random number generator;
* it will require the appropriate code to reseed whatever PRNG you’re using.
* It assumes that the new child process is the process that will be used to perform any work
* that does not require any extra privileges that the process may have.
* It is rare that when a process is forked, the original process is used to execute another
* program or the new process is used to continue primary execution of the program.
* In other words, the new process is most often the worker process.
*/
pid_t
spc_fork()
{
pid_t pidChild = ::fork();
if (pidChild == -1) {
return -1;
}
// Reseed PRNGs in both the parent and the child
// See Chapter 11 for examples
// If this is the parent process, there's nothing more to do
if (pidChild != 0) {
return pidChild;
}
// This is the child process
#if 0
spc_sanitize_files(); /* Close all open files. See Recipe 1.1 */
spc_drop_privileges(1); /* Permanently drop privileges. See Recipe 1.3 */
#endif
return 0;
}
//-------------------------------------------------------------------------------------------------
/**
* If spc_popen( ) is successful, the SpcPipe object it returns contains two FILE objects:
* fdRead can be used to read data written by the new program to its stdout file descriptor.
* fdWrite can be used to write data to the new program for reading from its stdin file descriptor.
* Unlike popen( ), which in its most portable form is unidirectional, spc_popen( ) is bidirectional
*/
bool
spc_popen(
SpcPipe &out_p, ///< [in,out]
const char *path,
char *const argv[],
char *const envp[]
)
{
out_p.clear();
int iRv {};
// StdIn
int pipeStdIn[2] {};
{
iRv = ::pipe(pipeStdIn);
STD_TEST_RET(iRv != -1, false);
out_p.fdWrite = ::fdopen(pipeStdIn[1], "w");
STD_TEST_RET(out_p.fdWrite != nullptr, false);
}
// StdOut
int pipeStdOut[2] {};
{
iRv = ::pipe(pipeStdOut);
STD_TEST_RET(iRv != -1, false);
out_p.fdRead = ::fdopen(pipeStdOut[0], "r");
STD_TEST_RET(out_p.fdRead != nullptr, false);
}
// fork
out_p.pidChild = ::spc_fork();
STD_TEST_RET(iRv != -1, false);
// child process
if (out_p.pidChild == 0) {
::close(pipeStdOut[0]);
if (pipeStdIn[0] != 0) {
::dup2(pipeStdIn[0], 0);
::close(pipeStdIn[0]);
}
if (pipeStdOut[1] != 1) {
::dup2(pipeStdOut[1], 1);
::close(pipeStdOut[1]);
}
// execute
::execve(path, argv, envp);
::exit(127);
}
::close(pipeStdOut[1]);
::close(pipeStdIn[0]);
return true;
}
//-------------------------------------------------------------------------------------------------
int
spc_pclose(
SpcPipe &out_p
)
{
int status {};
pid_t pid {};
if (out_p.pidChild != -1) {
do {
pid = ::waitpid(out_p.pidChild, &status, 0);
}
while (pid == -1 &&errno == EINTR);
}
if (out_p.fdRead != nullptr) {
::fclose(out_p.fdRead);
}
if (out_p.fdWrite != nullptr) {
::fclose(out_p.fdWrite);
}
if (pid != -1 && WIFEXITED(status)) {
return WEXITSTATUS(status);
}
return (pid == -1 ? -1 : 0);
}
//-------------------------------------------------------------------------------------------------
int main(int, char *const argv[], char *const envp[])
{
bool bRv {};
int iRv {};
SpcPipe p;
const std::string path = "/usr/bin/git";
bRv = ::spc_popen(p, path.c_str(), argv, envp);
iRv = ::spc_pclose(p);
std::cout << STD_TRACE_VAR3(path, bRv, iRv) << std::endl;
return EXIT_SUCCESS;
}
//-------------------------------------------------------------------------------------------------
#if OUTPUT
path: /usr/bin/git, bRv: 1, iRv: 1
#endif