FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
rstudio/src/cpp/core/system/ChildProcessSubprocPoll.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
/
core
/
system
/
ChildProcessSubprocPoll.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
193 lines (165 loc) · 5.29 KB
Breadcrumbs
rstudio
/
src
/
cpp
/
core
/
system
/
ChildProcessSubprocPoll.cpp
Copy path
File metadata and controls
193 lines (165 loc) · 5.29 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
/*
* ChildProcessSubprocPoll.cpp
*
* Copyright (C) 2009-18 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
"
ChildProcessSubprocPoll.hpp
"
namespace
rstudio
{
namespace
core
{
namespace
system
{
namespace
{
boost::posix_time::ptime
now
()
{
return
boost::posix_time::microsec_clock::universal_time
();
}
const
int
kThrottleSubProcFactor
=
4
;
}
//
anonymous namespace
ChildProcessSubprocPoll::ChildProcessSubprocPoll
(
PidType pid,
boost::posix_time::milliseconds resetRecentDelay,
boost::posix_time::milliseconds checkSubprocDelay,
boost::posix_time::milliseconds checkCwdDelay,
boost::function<std::vector<SubprocInfo> (PidType pid)> subProcCheck,
const
std::vector<std::string>& subProcWhitelist,
boost::function<core::FilePath (PidType pid)> cwdCheck)
:
pid_
(pid),
checkSubProcAfter_(boost::posix_time::not_a_date_time),
resetRecentOutputAfter_(boost::posix_time::not_a_date_time),
checkCwdAfter_(boost::posix_time::not_a_date_time),
hasSubprocess_(
true
),
hasWhitelistSubprocess_(
false
),
hasRecentOutput_(
true
),
didThrottleSubprocCheck_(
false
),
stopped_(
false
),
resetRecentDelay_(resetRecentDelay),
checkSubprocDelay_(checkSubprocDelay),
checkCwdDelay_(checkCwdDelay),
subProcCheck_(subProcCheck),
subProcWhitelist_(subProcWhitelist),
cwdCheck_(cwdCheck)
{
}
bool
ChildProcessSubprocPoll::poll
(
bool
hadOutput)
{
if
(stopped_)
return
false
;
boost::posix_time::ptime currentTime =
now
();
//
Update state of "hasRecentOutput". We remember that we saw output for
//
up to "resetRecentDelay_" milliseconds.
if
(hadOutput)
{
hasRecentOutput_ =
true
;
resetRecentOutputAfter_ = boost::posix_time::not_a_date_time;
}
if
(resetRecentOutputAfter_.
is_not_a_date_time
())
{
resetRecentOutputAfter_ = currentTime + resetRecentDelay_;
}
else
if
(currentTime > resetRecentOutputAfter_)
{
hasRecentOutput_ =
false
;
resetRecentOutputAfter_ = currentTime + resetRecentDelay_;
}
bool
didCheckSubProc =
pollSubproc
(currentTime);
bool
didCheckCwd =
pollCwd
(currentTime);
return
didCheckSubProc || didCheckCwd;
}
bool
ChildProcessSubprocPoll::pollSubproc
(boost::posix_time::ptime currentTime)
{
if
(!subProcCheck_)
return
false
;
//
Update state of "hasSubprocesses". We do this no more often than every
//
"checkSubprocDelay_" milliseconds, and less often if we haven't seen any
//
recent output. The latter is to reduce load when nothing is happening,
//
under the assumption that if all child processes are terminated, we
//
will always see output in the form of the command prompt.
if
(!
hasRecentOutput
() && !didThrottleSubprocCheck_)
{
checkSubProcAfter_ = currentTime + checkSubprocDelay_ *
kThrottleSubProcFactor
;
didThrottleSubprocCheck_ =
true
;
return
false
;
}
if
(checkSubProcAfter_.
is_not_a_date_time
())
{
checkSubProcAfter_ = currentTime + checkSubprocDelay_;
return
false
;
}
if
(currentTime <= checkSubProcAfter_)
return
false
;
//
Enough time has passed, update whether "pid" has subprocesses
//
and restart the timer.
hasSubprocess_ =
false
;
hasWhitelistSubprocess_ =
false
;
std::vector<SubprocInfo> children =
subProcCheck_
(pid_);
for
(
const
SubprocInfo& proc : children)
{
bool
isWhitelistItem =
false
;
for
(
const
auto
& whitelistItem : subProcWhitelist_)
{
if
(proc.
exe
== whitelistItem)
{
isWhitelistItem =
true
;
hasWhitelistSubprocess_ =
true
;
break
;
}
}
if
(!isWhitelistItem)
{
hasSubprocess_ =
true
;
}
}
checkSubProcAfter_ = currentTime + checkSubprocDelay_;
didThrottleSubprocCheck_ =
false
;
return
true
;
}
bool
ChildProcessSubprocPoll::pollCwd
(boost::posix_time::ptime currentTime)
{
if
(!cwdCheck_)
return
false
;
//
Update state of "getCwd". We do this no more often than every
//
"checkCwdDelay_" milliseconds.
if
(checkCwdAfter_.
is_not_a_date_time
())
{
checkCwdAfter_ = currentTime + checkCwdDelay_;
return
false
;
}
if
(currentTime <= checkCwdAfter_)
return
false
;
//
Enough time has passed, query current working directory and restart timer
cwd_ =
cwdCheck_
(pid_);
checkCwdAfter_ = currentTime + checkCwdDelay_;
return
true
;
}
void
ChildProcessSubprocPoll::stop
()
{
stopped_ =
true
;
}
bool
ChildProcessSubprocPoll::hasNonWhitelistSubprocess
()
const
{
return
hasSubprocess_;
}
bool
ChildProcessSubprocPoll::hasWhitelistSubprocess
()
const
{
return
hasWhitelistSubprocess_;
}
bool
ChildProcessSubprocPoll::hasRecentOutput
()
const
{
return
hasRecentOutput_;
}
core::FilePath
ChildProcessSubprocPoll::getCwd
()
const
{
return
cwd_;
}
}
//
namespace system
}
//
namespace core
}
//
namespace rstudio
Back
|
FazBrowse Home
|
New Git URL