FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
rstudio/src/cpp/core/system/Process.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
/
Process.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
421 lines (345 loc) · 12.1 KB
Breadcrumbs
rstudio
/
src
/
cpp
/
core
/
system
/
Process.cpp
Copy path
File metadata and controls
421 lines (345 loc) · 12.1 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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/*
* Process.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/Process.hpp
>
#
include
<
iostream
>
#
include
<
boost/algorithm/cxx11/any_of.hpp
>
#
include
<
boost/bind/bind.hpp
>
#
include
<
core/Scope.hpp
>
#
include
<
shared_core/Error.hpp
>
#
include
<
core/Log.hpp
>
#
include
<
core/BoostThread.hpp
>
#
include
<
core/PerformanceTimer.hpp
>
#
include
<
core/system/ChildProcess.hpp
>
#
include
<
core/Thread.hpp
>
using
namespace
boost
::placeholders
;
namespace
rstudio
{
namespace
core
{
namespace
system
{
bool
s_enableCallbacksRequireMainThread =
false
;
const
char
*
const
kSmartTerm
=
"
xterm-256color
"
;
const
char
*
const
kDumbTerm
=
"
dumb
"
;
//
default rows/columns for a pseudoterminal
const
int
kDefaultCols
=
80
;
const
int
kDefaultRows
=
25
;
Error
runProgram
(
const
std::string& executable,
const
std::vector<std::string>& args,
const
std::string& input,
const
ProcessOptions& options,
ProcessResult* pResult)
{
SyncChildProcess
child
(executable, args, options);
return
child.
run
(input, pResult);
}
Error
runProgram
(
const
std::string& executable,
const
std::vector<std::string>& args,
const
ProcessOptions& options,
ProcessResult* pResult)
{
SyncChildProcess
child
(executable, args, options);
return
child.
run
(
std::string
(), pResult);
}
Error
runCommand
(
const
std::string& command,
const
ProcessOptions& options,
ProcessResult* pResult)
{
return
runCommand
(command,
"
"
, options, pResult);
}
Error
runCommand
(
const
std::string& command,
const
std::string& input,
const
ProcessOptions& options,
ProcessResult* pResult)
{
SyncChildProcess
child
(command, options);
return
child.
run
(input, pResult);
}
struct
ProcessSupervisor
::Impl
{
Impl
() : isPolling(
false
) {}
bool
isPolling;
std::vector<boost::shared_ptr<AsyncChildProcess> > children;
};
ProcessSupervisor::ProcessSupervisor
()
: pImpl_(
new
Impl())
{
}
ProcessSupervisor::~ProcessSupervisor
()
{
}
namespace
{
Error
runChild
(boost::shared_ptr<AsyncChildProcess> pChild,
std::vector<boost::shared_ptr<AsyncChildProcess> >* pChildren,
boost::recursive_mutex* pMutex,
const
ProcessCallbacks& callbacks)
{
//
run the child
RECURSIVE_LOCK_MUTEX
(*pMutex)
{
Error error = pChild->
run
(callbacks);
if
(error)
return
error;
//
add to the list of children
pChildren->
push_back
(pChild);
}
END_LOCK_MUTEX
//
success
return
Success
();
}
}
//
anonymous namespace
Error
ProcessSupervisor::runProgram
(
const
std::string& executable,
const
std::vector<std::string>& args,
const
ProcessOptions& options,
const
ProcessCallbacks& callbacks)
{
//
create the child
boost::shared_ptr<AsyncChildProcess>
pChild
(
new
AsyncChildProcess
(executable,
args,
options));
//
run the child
return
runChild
(pChild, &(pImpl_->
children
), &mutex_, callbacks);
}
Error
ProcessSupervisor::runCommand
(
const
std::string& command,
const
ProcessOptions& options,
const
ProcessCallbacks& callbacks)
{
//
create the child
boost::shared_ptr<AsyncChildProcess>
pChild
(
new
AsyncChildProcess
(command, options));
//
run the child
return
runChild
(pChild, &(pImpl_->
children
), &mutex_, callbacks);
}
Error
ProcessSupervisor::runTerminal
(
const
ProcessOptions& options,
const
ProcessCallbacks& callbacks)
{
//
create the child
boost::shared_ptr<AsyncChildProcess>
pChild
(
new
AsyncChildProcess
(options));
//
run the child
return
runChild
(pChild, &(pImpl_->
children
), &mutex_, callbacks);
}
namespace
{
//
class which implements all of the callbacks
struct
ChildCallbacks
{
ChildCallbacks
(
const
std::string& input,
const
boost::function<
void
(
const
ProcessResult&)>& onCompleted,
const
boost::function<
void
(
const
Error&)>& onErrored)
: input(input), onCompleted(onCompleted), onErrored(onErrored)
{
}
void
onStarted
(ProcessOperations& operations)
{
if
(!input.
empty
())
{
Error error = operations.
writeToStdin
(input,
true
);
if
(error)
{
LOG_ERROR
(error);
error = operations.
terminate
();
if
(error)
LOG_ERROR
(error);
}
}
}
void
onStdout
(ProcessOperations&,
const
std::string& output)
{
stdOut.
append
(output);
}
void
onStderr
(ProcessOperations&,
const
std::string& output)
{
stdErr.
append
(output);
}
void
onError
(ProcessOperations&,
const
Error& error)
{
onErrored
(error);
}
void
onConsoleOutputSnapshot
(ProcessOperations&,
const
std::vector<
char
>& output)
{
consoleOutputSnapshot = output;
}
void
onExit
(
int
exitStatus)
{
ProcessResult result;
result.
exitStatus
= exitStatus;
result.
stdOut
= stdOut;
result.
stdErr
= stdErr;
onCompleted
(result);
}
void
onHasSubprocs
(
bool
hasSubprocs) {}
std::string input;
std::string stdOut;
std::string stdErr;
std::vector<
char
> consoleOutputSnapshot;
boost::function<
void
(
const
ProcessResult&)> onCompleted;
boost::function<
void
(
const
Error&)> onErrored;
};
}
//
anonymous namespace
ProcessCallbacks
createProcessCallbacks
(
const
std::string& input,
const
boost::function<
void
(
const
ProcessResult&)>& onCompleted,
const
boost::function<void(
const
Error&)>& onError)
{
//
create a shared_ptr to the ChildCallbacks. it will stay alive
//
as long as one of its members is referenced in a bind context
boost::shared_ptr<ChildCallbacks>
pCC
(
new
ChildCallbacks
(input,
onCompleted,
onError));
//
bind in the callbacks
using
boost::bind;
ProcessCallbacks cb;
cb.
onStarted
=
bind
(&ChildCallbacks::onStarted, pCC, _1);
cb.
onStdout
=
bind
(&ChildCallbacks::onStdout, pCC, _1, _2);
cb.
onStderr
=
bind
(&ChildCallbacks::onStderr, pCC, _1, _2);
cb.
onConsoleOutputSnapshot
=
bind
(&ChildCallbacks::onConsoleOutputSnapshot, pCC, _1, _2);
cb.
onExit
=
bind
(&ChildCallbacks::onExit, pCC, _1);
cb.
onError
=
bind
(&ChildCallbacks::onError, pCC, _1, _2);
//
Not implemented for generic processes
cb.
onHasSubprocs
.
clear
();
cb.
reportCwd
.
clear
();
//
return it
return
cb;
}
Error
ProcessSupervisor::runProgram
(
const
std::string& executable,
const
std::vector<std::string>& args,
const
std::string& input,
const
ProcessOptions& options,
const
boost::function<
void
(
const
ProcessResult&)>& onCompleted)
{
//
create process callbacks
ProcessCallbacks cb =
createProcessCallbacks
(
input, onCompleted, boost::function<
void
(
const
Error&)>());
//
run the child
return
runProgram
(executable, args, options, cb);
}
Error
ProcessSupervisor::runCommand
(
const
std::string& command,
const
ProcessOptions& options,
const
boost::function<
void
(
const
ProcessResult&)>& onCompleted)
{
return
runCommand
(command,
"
"
, options, onCompleted);
}
Error
ProcessSupervisor::runCommand
(
const
std::string& command,
const
std::string& input,
const
ProcessOptions& options,
const
boost::function<
void
(
const
ProcessResult&)>& onCompleted)
{
//
create process callbacks
ProcessCallbacks cb =
createProcessCallbacks
(input, onCompleted);
//
run the child
return
runCommand
(command, options, cb);
}
bool
ProcessSupervisor::hasRunningChildren
()
{
return
!pImpl_->
children
.
empty
();
}
namespace
{
bool
hasDurableActivity
(
const
boost::shared_ptr<AsyncChildProcess>& childProc)
{
return
!childProc->
allowParentSuspend
() && (
childProc->
hasNonIgnoredSubprocess
() ||
childProc->
hasIgnoredSubprocess
() ||
childProc->
hasRecentOutput
());
}
}
//
anonymous namespace
bool
ProcessSupervisor::hasDurableChildren
()
{
return
boost::algorithm::any_of
(pImpl_->
children
, hasDurableActivity);
}
bool
ProcessSupervisor::poll
()
{
RECURSIVE_LOCK_MUTEX
(mutex_)
{
//
bail immediately if we have no children
if
(!
hasRunningChildren
())
return
false
;
//
never allow re-entrancy (could occur if one of the output
//
handlers called from poll executes a waitForMethod which
//
results in additional polling during idle/wait time)
if
(pImpl_->
isPolling
)
return
true
;
//
set isPolling then clear it on exit
pImpl_->
isPolling
=
true
;
scope::SetOnExit<
bool
>
setOnExit
(&pImpl_->
isPolling
,
false
);
//
call poll on all of our children via a copy of the std::vector that
//
holds all of the children. we do this because 'poll' can end up
//
executing R code (e.g. via onContinue) which can in term end up
//
executing background tasks that result in a call to processSupervisor
//
runProgram or runCommand. This would then result in a push_back on
//
the children vector and if this required a realloc would invalidate
//
all of the iterators currently pointing into the container
std::vector<boost::shared_ptr<AsyncChildProcess> > children = pImpl_->
children
;
std::for_each
(children.
begin
(),
children.
end
(),
boost::bind
(&AsyncChildProcess::poll, _1));
//
remove any children who have exited from our list. note that it's safe
//
in this case to use pImpl_->children directly because the call to
//
AsyncChildProcess::exited just checks a member variable rather than
//
executing code that could cause re-entry
pImpl_->
children
.
erase
(
std::remove_if
(
pImpl_->
children
.
begin
(),
pImpl_->
children
.
end
(),
boost::bind
(&AsyncChildProcess::exited, _1)),
pImpl_->
children
.
end
());
//
return status
return
hasRunningChildren
();
}
END_LOCK_MUTEX
return
false
;
}
void
ProcessSupervisor::terminateAll
()
{
//
call terminate on all of our children
for
(boost::shared_ptr<AsyncChildProcess> pChild : pImpl_->
children
)
{
Error error = pChild->
terminate
();
if
(error)
LOG_ERROR
(error);
}
}
bool
ProcessSupervisor::wait
(
const
boost::posix_time::time_duration& pollingInterval,
const
boost::posix_time::time_duration& maxWait)
{
boost::posix_time::ptime
timeoutTime
(boost::posix_time::not_a_date_time);
if
(!maxWait.
is_not_a_date_time
())
timeoutTime =
boost::get_system_time
() + maxWait;
while
(
poll
())
{
//
wait the specified polling interval
boost::this_thread::sleep
(pollingInterval);
//
check for timeout if appropriate
if
(!timeoutTime.
is_not_a_date_time
())
{
if
(
boost::get_system_time
() > timeoutTime)
return
false
;
}
}
return
true
;
}
void
setEnableCallbacksRequireMainThread
(
bool
enabled)
{
s_enableCallbacksRequireMainThread = enabled;
}
bool
enableCallbacksRequireMainThread
()
{
return
s_enableCallbacksRequireMainThread;
}
}
//
namespace system
}
//
namespace core
}
//
namespace rstudio
Back
|
FazBrowse Home
|
New Git URL