FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
rstudio/src/cpp/session/SessionConsoleProcessTable.cpp at master · pearsonca/rstudio · GitHub
pearsonca
/
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
/
session
/
SessionConsoleProcessTable.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
370 lines (301 loc) · 9.6 KB
Breadcrumbs
rstudio
/
src
/
cpp
/
session
/
SessionConsoleProcessTable.cpp
Copy path
File metadata and controls
370 lines (301 loc) · 9.6 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
/*
* SessionConsoleProcessTable.cpp
*
* Copyright (C) 2009-19 by RStudio, PBC
*
* Unless you have received this program directly from RStudio pursuant
* to the terms of a commercial license agreement with RStudio, 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
"
SessionConsoleProcessTable.hpp
"
#
include
<
boost/range/adaptor/map.hpp
>
#
include
<
shared_core/SafeConvert.hpp
>
#
include
<
session/SessionModuleContext.hpp
>
#
include
"
SessionConsoleProcessApi.hpp
"
#
include
"
modules/SessionVCS.hpp
"
using
namespace
rstudio
::core
;
namespace
rstudio
{
namespace
session
{
namespace
console_process
{
namespace
{
//
terminal currently visible in the client
std::string s_visibleTerminalHandle;
typedef
std::map<std::string, ConsoleProcessPtr> ProcTable;
ProcTable s_procs;
std::string
serializeConsoleProcs
(SerializationMode serialMode)
{
json::Array array;
for
(ProcTable::const_iterator it = s_procs.
begin
();
it != s_procs.
end
();
it++)
{
array.
push_back
(it->
second
->
toJson
(serialMode));
}
std::ostringstream ostr;
array.
write
(ostr);
return
ostr.
str
();
}
void
deserializeConsoleProcs
(
const
std::string& jsonStr)
{
if
(jsonStr.
empty
())
return
;
json::Value value;
if
(value.
parse
(jsonStr))
{
LOG_WARNING_MESSAGE
(
"
invalid console process json:
"
+ jsonStr);
return
;
}
const
json::Array& procs = value.
getArray
();
for
(json::Array::Iterator it = procs.
begin
();
it != procs.
end
();
it++)
{
ConsoleProcessPtr proc =
ConsoleProcess::fromJson
((*it).
getObject
());
//
Deserializing consoleprocs list only happens during session
//
initialization, therefore they do not represent an actual running
//
async process, therefore are not busy. Mark as such, otherwise we
//
can get false "busy" indications on the client after a restart, for
//
example if a session was closed with busy terminal(s), then
//
restarted. This is not hit if reconnecting to a still-running
//
session.
proc->
setNotBusy
();
s_procs[proc->
handle
()] = proc;
}
}
bool
isKnownProcHandle
(
const
std::string& handle)
{
return
findProcByHandle
(handle) !=
nullptr
;
}
void
onSuspend
(core::Settings*
/*
pSettings
*/
)
{
serializeConsoleProcs
(PersistentSerialization);
s_visibleTerminalHandle.
clear
();
}
void
onResume
(
const
core::Settings&
/*
settings
*/
)
{
}
void
loadConsoleProcesses
()
{
std::string contents =
ConsoleProcessInfo::loadConsoleProcessMetadata
();
if
(contents.
empty
())
return
;
deserializeConsoleProcs
(contents);
ConsoleProcessInfo::deleteOrphanedLogs
(isKnownProcHandle);
}
}
//
anonymous namespace--------------
ConsoleProcessPtr
findProcByHandle
(
const
std::string& handle)
{
ProcTable::const_iterator pos = s_procs.
find
(handle);
if
(pos != s_procs.
end
())
return
pos->
second
;
else
return
ConsoleProcessPtr
();
}
ConsoleProcessPtr
findProcByCaption
(
const
std::string& caption)
{
for
(ConsoleProcessPtr& proc : s_procs | boost::adaptors::map_values)
{
if
(proc->
getCaption
() == caption)
return
proc;
}
return
ConsoleProcessPtr
();
}
ConsoleProcessPtr
getVisibleProc
()
{
return
findProcByHandle
(s_visibleTerminalHandle);
}
void
clearVisibleProc
()
{
s_visibleTerminalHandle.
clear
();
}
void
setVisibleProc
(
const
std::string& handle)
{
s_visibleTerminalHandle = handle;
}
std::vector<std::string>
getAllHandles
()
{
std::vector<std::string> allHandles;
for
(ProcTable::const_iterator it = s_procs.
begin
(); it != s_procs.
end
(); it++)
{
allHandles.
push_back
(it->
second
->
handle
());
}
return
allHandles;
}
//
Determine next terminal sequence and name
std::pair<
int
, std::string>
nextTerminalName
()
{
int
maxNum =
kNoTerminal
;
for
(ConsoleProcessPtr& proc : s_procs | boost::adaptors::map_values)
{
maxNum =
std::max
(maxNum, proc->
getTerminalSequence
());
}
maxNum++;
return
std::pair<
int
, std::string>(
maxNum,
std::string
(
"
Terminal
"
) +
core::safe_convert::numberToString
(maxNum));
}
void
saveConsoleProcesses
()
{
ConsoleProcessInfo::saveConsoleProcesses
(
serializeConsoleProcs
(PersistentSerialization));
}
void
saveConsoleProcessesAtShutdown
(
bool
terminatedNormally)
{
if
(!terminatedNormally)
return
;
//
When shutting down, only preserve ConsoleProcesses that are marked
//
with allow_restart. Others should not survive a shutdown/restart.
ProcTable::const_iterator nextIt;
for
(ProcTable::const_iterator it = s_procs.
begin
();
it != s_procs.
end
();
it = nextIt)
{
nextIt = it;
++nextIt;
if
(!it->
second
->
getAllowRestart
())
{
s_procs.
erase
(it->
second
->
handle
());
}
}
s_visibleTerminalHandle.
clear
();
saveConsoleProcesses
();
}
void
addConsoleProcess
(
const
ConsoleProcessPtr& proc)
{
s_procs[proc->
handle
()] = proc;
}
Error
reapConsoleProcess
(
const
ConsoleProcess& proc)
{
proc.
deleteLogFile
();
proc.
deleteEnvFile
();
if
(s_procs.
erase
(proc.
handle
()))
{
saveConsoleProcesses
();
}
//
don't report errors if tried to reap something that isn't in the
//
table; there are cases where we do reaping on the server-side and
//
the client may also try to reap the same thing after-the-fact
return
Success
();
}
core::json::Array
allProcessesAsJson
(SerializationMode serialMode)
{
json::Array procInfos;
for
(ProcTable::const_iterator it = s_procs.
begin
();
it != s_procs.
end
();
it++)
{
procInfos.
push_back
(it->
second
->
toJson
(serialMode));
}
return
procInfos;
}
Error
internalInitialize
()
{
using
boost::bind;
using
namespace
module_context
;
events
().
onShutdown
.
connect
(saveConsoleProcessesAtShutdown);
addSuspendHandler
(
SuspendHandler
(
boost::bind
(onSuspend, _2), onResume));
loadConsoleProcesses
();
return
initializeApi
();
}
Error
createTerminalConsoleProc
(boost::shared_ptr<ConsoleProcessInfo> cpi,
std::string* pHandle)
{
using
namespace
session
::module_context
;
using
namespace
session
::console_process
;
#
if
defined(_WIN32)
cpi->
setTrackEnv
(
false
);
#
endif
std::string computedCaption = cpi->
getCaption
();
if
(cpi->
getTerminalSequence
() ==
kNewTerminal
)
{
std::pair<
int
, std::string> sequenceInfo =
nextTerminalName
();
cpi->
setTerminalSequence
(sequenceInfo.
first
);
if
(computedCaption.
empty
())
computedCaption = sequenceInfo.
second
;
}
if
(cpi->
getTerminalSequence
() ==
kNoTerminal
)
{
return
systemError
(boost::system::errc::invalid_argument,
"
Unsupported terminal sequence
"
,
ERROR_LOCATION
);
}
if
(computedCaption.
empty
())
{
return
systemError
(boost::system::errc::invalid_argument,
"
Empty terminal caption not supported
"
,
ERROR_LOCATION
);
}
cpi->
setCaption
(computedCaption);
TerminalShell::ShellType actualShellType;
core::system::ProcessOptions options =
ConsoleProcess::createTerminalProcOptions
(
*cpi, &actualShellType);
cpi->
setShellType
(actualShellType);
boost::shared_ptr<ConsoleProcess> ptrProc =
ConsoleProcess::createTerminalProcess
(options, cpi);
ptrProc->
onExit
().
connect
(
boost::bind
(
&modules::source_control::enqueueRefreshEvent));
*pHandle = ptrProc->
handle
();
return
Success
();
}
Error
createTerminalExecuteConsoleProc
(
const
std::string& title,
const
std::string& command,
const
std::string& currentDir,
const
core::system::Options& env,
std::string* pHandle)
{
using
namespace
session
::module_context
;
using
namespace
session
::console_process
;
std::pair<
int
, std::string> sequenceInfo =
nextTerminalName
();
int
termSequence = sequenceInfo.
first
;
std::string caption = sequenceInfo.
second
;
FilePath cwd;
if
(!currentDir.
empty
())
{
cwd =
module_context::resolveAliasedPath
(currentDir);
}
core::system::ProcessOptions options;
core::system::Options childEnv;
core::system::environment
(&childEnv);
core::system::getModifiedEnv
(env, &childEnv);
#
ifdef
_WIN32
core::system::setHomeToUserProfile
(&childEnv);
#
endif
options.
environment
= childEnv;
options.
smartTerminal
=
true
;
#
ifdef
_WIN32
options.
detachProcess
=
true
;
#
else
options.
detachSession
=
true
;
#
endif
options.
reportHasSubprocs
=
false
;
options.
trackCwd
=
false
;
options.
workingDir
= cwd;
boost::shared_ptr<ConsoleProcessInfo> ptrProcInfo =
boost::shared_ptr<ConsoleProcessInfo>(
new
ConsoleProcessInfo
(
caption,
title,
std::string
()
/*
handle
*/
,
termSequence,
TerminalShell::ShellType::NoShell,
false
/*
altBuffer
*/
,
cwd,
core::system::
kDefaultCols
, core::system::
kDefaultRows
,
false
/*
zombie
*/
,
false
/*
trackEnv
*/
));
ptrProcInfo->
setInteractionMode
(InteractionNever);
ptrProcInfo->
setAutoClose
(NeverAutoClose);
boost::shared_ptr<ConsoleProcess> ptrProc =
ConsoleProcess::createTerminalProcess
(command, options, ptrProcInfo,
ConsoleProcess::useWebsockets
());
*pHandle = ptrProc->
handle
();
return
Success
();
}
}
//
namespace console_process
}
//
namespace session
}
//
namespace rstudio
Back
|
FazBrowse Home
|
New Git URL