FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
rstudio/src/cpp/core/system/PosixOutputCapture.cpp at main · ssh352/rstudio · GitHub
ssh352
/
rstudio
Public
forked from
rstudio/rstudio
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
rstudio
/
src
/
cpp
/
core
/
system
/
PosixOutputCapture.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
278 lines (234 loc) · 7.65 KB
Breadcrumbs
rstudio
/
src
/
cpp
/
core
/
system
/
PosixOutputCapture.cpp
Copy path
File metadata and controls
278 lines (234 loc) · 7.65 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
* PosixOutputCapture.cpp
*
* Copyright (C) 2022 by Posit Software, PBC
*
* Unless you have received this program directly from Posit Software pursuant
* to the terms of a commercial license agreement with Posit Software, then
* this program is licensed to you under the terms of version 3 of the
* GNU Affero General Public License. This program is distributed WITHOUT
* ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
* MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
* AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
*
*/
#
include
<
core/system/OutputCapture.hpp
>
#
include
<
stdio.h
>
#
include
<
unistd.h
>
#
include
<
fcntl.h
>
#
include
<
iostream
>
#
include
<
core/Log.hpp
>
#
include
<
shared_core/Error.hpp
>
#
include
<
shared_core/SafeConvert.hpp
>
#
include
<
core/BoostThread.hpp
>
#
include
<
core/BoostErrors.hpp
>
#
include
<
core/system/System.hpp
>
#
include
<
boost/bind/bind.hpp
>
using
namespace
boost
::placeholders
;
namespace
rstudio
{
namespace
core
{
namespace
system
{
namespace
{
void
readFromPipe
(
int
pipeFd,
const
boost::function<
void
(
const
std::string&)>& outputFunction)
{
const
int
kBufferSize
=
40960
;
char
buffer[
kBufferSize
];
int
bytesRead =
0
;
while
( (bytesRead = ::
read
(pipeFd, buffer,
kBufferSize
)) >
0
)
{
std::string
output
(buffer, bytesRead);
outputFunction
(output);
}
//
log unexpected errors
if
(bytesRead == -
1
)
{
if
(errno !=
EAGAIN
&& errno !=
EINTR
)
LOG_ERROR
(
systemError
(errno,
ERROR_LOCATION
));
}
}
void
standardStreamCaptureThread
(
int
stdoutFd,
int
dupStdoutFd,
const
boost::function<
void
(
const
std::string&)>& stdoutHandler,
int stderrFd,
int dupStderrFd,
const
boost::function<void(
const
std::string&)>& stderrHandler)
{
boost::function<
void
(
const
std::string&)> outHandler = stdoutHandler;
boost::function<
void
(
const
std::string&)> errHandler = stderrHandler;
auto
wrapHandler =
[=](
const
boost::function<
void
(
const
std::string&)>& handler,
int
dupFd,
const
std::string& descriptorType,
boost::shared_ptr<
bool
> pSkipWrite,
const
std::string& output)
{
handler
(output);
if
(!*pSkipWrite)
{
if
(::
write
(dupFd, output.
c_str
(), output.
size
()) == -
1
)
{
if
(errno ==
EPIPE
|| errno ==
EBADF
)
{
//
the std stream was closed somehow, meaning this write call will never succeed again
//
mark that we should skip the write, and only log this error once
*pSkipWrite =
true
;
std::string cause = errno ==
EBADF
?
"
closed
"
:
"
's pipe read end closed
"
;
std::string description =
descriptorType +
"
descriptor
"
+
core::safe_convert::numberToString
(dupFd) +
cause +
"
. Output will no longer be redirected.
"
;
LOG_ERROR
(
systemError
(errno, description,
ERROR_LOCATION
));
}
else
if
(errno !=
EAGAIN
&& errno !=
EINTR
)
LOG_ERROR
(
systemError
(errno,
ERROR_LOCATION
));
}
}
};
if
(dupStdoutFd != -
1
)
{
boost::shared_ptr<
bool
> pSkipStdoutWrite = boost::make_shared<
bool
>(
false
);
outHandler = boost::bind<
void
>(wrapHandler, stdoutHandler, dupStdoutFd,
"
Stdout
"
, pSkipStdoutWrite, _1);
}
if
(dupStderrFd != -
1
)
{
boost::shared_ptr<
bool
> pSkipStderrWrite = boost::make_shared<
bool
>(
false
);
errHandler = boost::bind<
void
>(wrapHandler, stderrHandler, dupStderrFd,
"
Stderr
"
, pSkipStderrWrite, _1);
}
try
{
while
(
true
)
{
//
create fd set
fd_set fds;
FD_ZERO
(&fds);
FD_SET
(stdoutFd, &fds);
if
(stderrFd != -
1
)
FD_SET
(stderrFd, &fds);
//
wait
int
highFd =
std::max
(stdoutFd, stderrFd);
int
result = ::
select
(highFd+
1
, &fds,
nullptr
,
nullptr
,
nullptr
);
if
(result != -
1
)
{
if
(stdoutFd != -
1
)
{
if
(
FD_ISSET
(stdoutFd, &fds))
readFromPipe
(stdoutFd, outHandler);
}
if
(stderrFd != -
1
)
{
if
(
FD_ISSET
(stderrFd, &fds))
readFromPipe
(stderrFd, errHandler);
}
}
else
if
(errno !=
EINTR
)
{
LOG_ERROR
(
systemError
(errno,
ERROR_LOCATION
));
}
}
}
CATCH_UNEXPECTED_EXCEPTION
}
Error
redirectToPipe
(
int
fd,
int
* pPipeReadFd)
{
//
create pipe
int
pipe[
2
];
if
(::
pipe
(pipe) == -
1
)
return
systemError
(errno,
ERROR_LOCATION
);
//
redirect
if
(::
dup2
(pipe[
1
], fd) == -
1
)
return
systemError
(errno,
ERROR_LOCATION
);
//
set read fds to non-blocking
if
(::
fcntl
(pipe[
0
],
F_SETFL
,
::fcntl
(pipe[
0
],
F_GETFL
) | O_NONBLOCK) == -1)
{
return
systemError
(errno,
ERROR_LOCATION
);
}
//
return read fds
*pPipeReadFd = pipe[
0
];
return
Success
();
}
Error
redirectToPipe
(
int
fd,
int
*pPipeReadFd,
int
*pOriginalDup)
{
int
newFd = ::
dup
(fd);
if
(newFd == -
1
)
return
systemError
(errno,
ERROR_LOCATION
);
Error error =
redirectToPipe
(fd, pPipeReadFd);
if
(error)
return
error;
*pOriginalDup = newFd;
return
Success
();
}
}
//
anonymous namespace
Error
captureStandardStreams
(
const
boost::function<
void
(
const
std::string&)>& stdoutHandler,
const
boost::function<void(
const
std::string&)>& stderrHandler,
bool forwardOutputToOriginalDescriptors)
{
if
(!stdoutHandler && !stderrHandler)
{
return
systemError
(boost::system::errc::invalid_argument,
"
At least one of stdoutHandler and stderrHandler must be set
"
,
ERROR_LOCATION
);
}
Error error;
int
stdoutReadPipe = -
1
;
int
stderrReadPipe = -
1
;
int
dupStdoutFd = -
1
;
int
dupStderrFd = -
1
;
//
redirect stdout if handler was provided
if
(stdoutHandler)
{
if
(!forwardOutputToOriginalDescriptors)
error =
redirectToPipe
(
STDOUT_FILENO
, &stdoutReadPipe);
else
error =
redirectToPipe
(
STDOUT_FILENO
, &stdoutReadPipe, &dupStdoutFd);
if
(error)
return
error;
//
set stdout to use unbuffered io
::setvbuf
(stdout,
nullptr
, _IONBF,
0
);
}
//
redirect stderror if handler was provided
if
(stderrHandler)
{
if
(!forwardOutputToOriginalDescriptors)
error =
redirectToPipe
(
STDERR_FILENO
, &stderrReadPipe);
else
error =
redirectToPipe
(
STDERR_FILENO
, &stderrReadPipe, &dupStderrFd);
if
(error)
return
error;
//
set stderr to use unbuffered io
::setvbuf
(stderr,
nullptr
, _IONBF,
0
);
}
//
sync c++ iostreams
std::ios::sync_with_stdio
();
//
launch the monitor thread
try
{
//
block all signals for launch of background thread (will cause it
//
to never receive signals)
core::system::SignalBlocker signalBlocker;
Error error = signalBlocker.
blockAll
();
if
(error)
LOG_ERROR
(error);
boost::thread
t
(
boost::bind
(standardStreamCaptureThread,
stdoutReadPipe,
dupStdoutFd,
stdoutHandler,
stderrReadPipe,
dupStderrFd,
stderrHandler));
return
Success
();
}
catch
(
const
boost::thread_resource_error& e)
{
return
Error
(
boost::thread_error::ec_from_exception
(e),
ERROR_LOCATION
);
}
}
}
//
namespace system
}
//
namespace core
}
//
namespace rstudio
Back
|
FazBrowse Home
|
New Git URL