forked from ultmaster/runexe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInteraction.cpp
177 lines (140 loc) · 6.91 KB
/
Interaction.cpp
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
#include <cstdio>
#include <unistd.h>
#include "Interaction.h"
#include "Run.h"
#include "Configuration.h"
namespace interaction {
bool failed = false;
std::string failMessage = "";
pthread_mutex_t mutex_lock;
int pipeFds[8];
void error(const std::string &message) {
pthread_mutex_lock(&mutex_lock);
failed = true;
failMessage = message;
pthread_mutex_unlock(&mutex_lock);
}
StreamProxyConfiguration::StreamProxyConfiguration(const std::string &firstProcessName,
const std::string &secondProcessName,
const std::string &recordFileName, int inputStreamFd,
int outputStreamFd) : firstProcessName(firstProcessName),
secondProcessName(secondProcessName),
recordFileName(recordFileName),
inputStreamFd(inputStreamFd),
outputStreamFd(outputStreamFd) {}
void *streamProxyRun(void *arg) {
StreamProxyConfiguration *configuration = (StreamProxyConfiguration *) arg;
FILE* recordFile = NULL;
if (!configuration->recordFileName.empty())
recordFile = fopen(configuration->recordFileName.c_str(), "w");
const int bufferSize = 65536;
char buffer[bufferSize];
while (true) {
ssize_t len;
len = read(configuration->inputStreamFd, buffer, bufferSize);
if (len == -1) {
error("Unexpected exception while reading from the output stream of " +
configuration->firstProcessName);
break;
}
if (len == 0) break;
if (recordFile != NULL) {
if (fwrite(buffer, sizeof(char), len, recordFile) != len) {
error("Unexpected exception while recording the output stream to file from " +
configuration->firstProcessName);
}
}
if (write(configuration->outputStreamFd, buffer, len) != len) {
error("Unexpected exception while writing to the input stream of " + configuration->secondProcessName);
break;
}
}
fclose(recordFile);
close(configuration->inputStreamFd);
close(configuration->outputStreamFd);
return NULL;
}
void pipeHelper(int &writeEnd, int &readEnd) {
int fd[2];
if (pipe(fd) == -1) {
runexe::crash("Unexpected exception: pipe failed");
} else {
readEnd = fd[0];
writeEnd = fd[1];
}
}
struct InvocationThreadStruct {
runexe::InvocationParams params;
runexe::InvocationResult result;
InvocationThreadStruct(const runexe::InvocationParams ¶ms) : params(params) {}
};
void *runProcessHelper(void *arg) {
using namespace runexe;
InvocationThreadStruct *threadStruct = (InvocationThreadStruct *) arg;
InvocationParams *invocationParams = &(threadStruct->params);
ProcessParams params = invocationParams->asProcessParams();
int readFd = pipeFds[invocationParams->getProgramName() == "program" ? 7 : 3];
int writeFd = pipeFds[invocationParams->getProgramName() == "program" ? 0 : 4];
for (int i = 0; i < 8; ++i)
if (pipeFds[i] != readFd && pipeFds[i] != writeFd)
params.closing_fds.push_back(pipeFds[i]);
params.input_fd = readFd;
params.output_fd = writeFd;
ProcessOutcome outcome = run(params);
threadStruct->result = InvocationResult(outcome);
return NULL;
}
std::vector<runexe::InvocationResult> interact(runexe::InvocationParams paramProgram,
runexe::InvocationParams paramInteractor) {
pthread_mutex_init(&mutex_lock, NULL);
/* https://stackoverflow.com/questions/19020857/how-to-check-if-the-pipe-is-opend-before-writing */
if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
runexe::crash("Failed to ignore SIGPIPE");
pipeHelper(pipeFds[0], pipeFds[1]); // program process -> proxy
pipeHelper(pipeFds[2], pipeFds[3]); // proxy -> interactor process
pipeHelper(pipeFds[4], pipeFds[5]); // interactor process -> proxy
pipeHelper(pipeFds[6], pipeFds[7]); // proxy -> program process
runexe::Configuration& configuration = runexe::Configuration::getConfiguration();
StreamProxyConfiguration firstProxy = StreamProxyConfiguration("program", "interactor",
configuration.getInteractorRecordOutput(),
pipeFds[1], pipeFds[2]);
StreamProxyConfiguration secondProxy = StreamProxyConfiguration("interactor", "program",
configuration.getInteractorRecordInput(),
pipeFds[5], pipeFds[6]);
InvocationThreadStruct programStruct(paramProgram), interactorStruct(paramInteractor);
pthread_t proxy1, proxy2, local1, local2;
if (pthread_create(&local1, NULL, runProcessHelper, &programStruct) != 0) {
runexe::crash("Thread local 1 failed to create");
}
if (pthread_create(&local2, NULL, runProcessHelper, &interactorStruct) != 0) {
runexe::crash("Thread local 2 failed to create");
}
if (pthread_create(&proxy1, NULL, streamProxyRun, &firstProxy) != 0) {
runexe::crash("Thread proxy 1 failed to create");
}
if (pthread_create(&proxy2, NULL, streamProxyRun, &secondProxy) != 0) {
runexe::crash("Thread proxy 2 failed to create");
}
if (pthread_join(local1, NULL) != 0) {
runexe::crash("Thread local 1 failed to join");
}
if (pthread_join(local2, NULL) != 0) {
runexe::crash("Thread local 2 failed to join");
}
if (pthread_join(proxy1, NULL) != 0) {
runexe::crash("Thread proxy 1 failed to join");
}
if (pthread_join(proxy2, NULL) != 0) {
runexe::crash("Thread proxy 2 failed to join");
}
pthread_mutex_destroy(&mutex_lock);
interactorStruct.result.setProgramName("interactor");
std::vector<runexe::InvocationResult> result;
result.push_back(programStruct.result);
result.push_back(interactorStruct.result);
if (failed) {
runexe::crash(failMessage);
}
return result;
}
}