FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
rstudio/src/cpp/server/ServerSessionManager.cpp at master · usersbin/rstudio · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
usersbin
/
rstudio
Public
forked from
rstudio/rstudio
Notifications
You must be signed in to change notification settings
Fork
0
Star
1
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
/
server
/
ServerSessionManager.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
281 lines (235 loc) · 7.58 KB
Breadcrumbs
rstudio
/
src
/
cpp
/
server
/
ServerSessionManager.cpp
Copy path
File metadata and controls
281 lines (235 loc) · 7.58 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
/*
* ServerSessionManager.cpp
*
* Copyright (C) 2009-11 by RStudio, Inc.
*
* 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
"
ServerSessionManager.hpp
"
#
include
<
sys/wait.h
>
#
include
<
vector
>
#
include
<
boost/foreach.hpp
>
#
include
<
boost/format.hpp
>
#
include
<
core/Error.hpp
>
#
include
<
core/Log.hpp
>
#
include
<
core/system/System.hpp
>
#
include
<
core/system/PosixUser.hpp
>
#
include
<
session/SessionConstants.hpp
>
#
include
<
server/ServerOptions.hpp
>
#
include
<
server/util/system/User.hpp
>
#
include
<
server/auth/ServerValidateUser.hpp
>
#
include
"
ServerREnvironment.hpp
"
using
namespace
core
;
namespace
server
{
SessionManager&
sessionManager
()
{
static
SessionManager instance;
return
instance;
}
Error
SessionManager::launchSession
(
const
std::string& username)
{
using
namespace
boost
::posix_time
;
LOCK_MUTEX
(launchesMutex_)
{
//
check whether we already have a launch pending
LaunchMap::const_iterator pos = pendingLaunches_.
find
(username);
if
(pos != pendingLaunches_.
end
())
{
//
if the launch is less than one minute old then return success
if
( (pos->
second
+
boost::posix_time::minutes
(
1
))
>
microsec_clock::universal_time
() )
{
return
Success
();
}
//
otherwise erase it from pending launches and then
//
re-launch (immediately below)
else
{
//
very unexpected condition
LOG_WARNING_MESSAGE
(
"
Very long session launch delay for
"
"
user
"
+ username +
"
(aborting wait)
"
);
pendingLaunches_.
erase
(username);
}
}
//
record the launch
pendingLaunches_[username] =
microsec_clock::universal_time
();
}
END_LOCK_MUTEX
//
launch the session
PidType pid;
Error error =
server::launchSession
(username, &pid);
if
(error)
{
removePendingLaunch
(username);
return
error;
}
else
{
//
add it to our active pids
addActivePid
(pid);
//
return success
return
Success
();
}
}
void
SessionManager::removePendingLaunch
(
const
std::string& username)
{
LOCK_MUTEX
(launchesMutex_)
{
pendingLaunches_.
erase
(username);
}
END_LOCK_MUTEX
}
namespace
{
//
wraper for waitPid which tries again for EINTR
int
waitPid
(PidType pid,
int
* pStatus)
{
for
(;;)
{
int
result = ::
waitpid
(pid, pStatus,
WNOHANG
);
if
(result == -
1
&& errno ==
EINTR
)
continue
;
return
result;
}
}
}
//
anonymous namespace
void
SessionManager::notifySIGCHLD
()
{
//
We make a copy of hte active pids so that we can do the reaping
//
outside of the pidsMutex_. This is an extra conservative precaution
//
in case there is ever an issue with waitpid blocking.
std::vector<PidType> pids =
activePids
();
//
reap all that we can
BOOST_FOREACH
(PidType pid, pids)
{
//
non-blocking wait for the child
int
status;
int
result =
waitPid
(pid, &status);
//
reaped the child
if
(result == pid)
{
//
confirm this was a real exit
bool
exited =
false
;
if
(
WIFEXITED
(status))
{
exited =
true
;
status =
WEXITSTATUS
(status);
}
else
if
(
WIFSIGNALED
(status))
{
exited =
true
;
}
//
if it was a real exit (as opposed to a SIGSTOP or SIGCONT)
//
then remove the pid from our table and fire the event
if
(exited)
{
//
all done with this pid
removeActivePid
(pid);
}
else
{
boost::format
fmt
(
"
Received SIGCHLD when child did not
"
"
actually exit (pid=%1%, status=%2%
"
);
LOG_WARNING_MESSAGE
(
boost::str
(fmt % pid % status));
}
}
//
error occured
else
if
(result == -
1
)
{
Error error =
systemError
(errno,
ERROR_LOCATION
);
error.
addProperty
(
"
pid
"
, pid);
LOG_ERROR
(error);
}
}
}
void
SessionManager::addActivePid
(PidType pid)
{
LOCK_MUTEX
(pidsMutex_)
{
activePids_.
push_back
(pid);
}
END_LOCK_MUTEX
}
void
SessionManager::removeActivePid
(PidType pid)
{
LOCK_MUTEX
(pidsMutex_)
{
activePids_.
erase
(
std::remove
(activePids_.
begin
(),
activePids_.
end
(),
pid),
activePids_.
end
());
}
END_LOCK_MUTEX
}
std::vector<PidType>
SessionManager::activePids
()
{
LOCK_MUTEX
(pidsMutex_)
{
return
activePids_;
}
END_LOCK_MUTEX
//
keep compiler happy
return
std::vector<PidType>();
}
Error
launchSession
(
const
std::string& username,
const
core::system::Options& extraArgs,
PidType* pPid)
{
//
last ditch user validation -- an invalid user should very rarely
//
get to this point since we pre-emptively validate on client_init
if
(!
server::auth::validateUser
(username))
{
Error error =
systemError
(boost::system::errc::permission_denied,
ERROR_LOCATION
);
error.
addProperty
(
"
username
"
, username);
return
error;
}
//
prepare command line arguments
server::Options& options =
server::options
();
core::system::Options args ;
//
check for options-specified config file and add to command
//
line if specified
std::string
rsessionConfigFile
(options.
rsessionConfigFile
());
if
(!rsessionConfigFile.
empty
())
args.
push_back
(
std::make_pair
(
"
--config-file
"
, rsessionConfigFile));
//
pass the user-identity
args.
push_back
(
std::make_pair
(
"
-
"
kUserIdentitySessionOptionShort
,
username));
//
pass our uid to instruct rsession to limit rpc clients to us and itself
core::system::Options environment;
uid_t
uid =
core::system::user::currentUserIdentity
().
userId
;
environment.
push_back
(
std::make_pair
(
kRStudioLimitRpcClientUid
,
boost::lexical_cast<std::string>(uid)));
//
pass extra params
std::copy
(extraArgs.
begin
(), extraArgs.
end
(),
std::back_inserter
(args));
//
append R environment variables
core::system::Options rEnvVars =
r_environment::variables
();
environment.
insert
(environment.
end
(), rEnvVars.
begin
(), rEnvVars.
end
());
//
launch the session
*pPid = -
1
;
std::string runAsUser =
util::system::realUserIsRoot
() ? username :
"
"
;
util::system::ProcessConfig config;
config.
args
= args;
config.
environment
= environment;
config.
stdStreamBehavior
= util::system::StdStreamInherit;
config.
memoryLimitBytes
=
static_cast
<RLimitType>(
options.
rsessionMemoryLimitMb
() *
1024L
*
1024L
);
config.
stackLimitBytes
=
static_cast
<RLimitType>(
options.
rsessionStackLimitMb
() *
1024L
*
1024L
);
config.
userProcessesLimit
=
static_cast
<RLimitType>(
options.
rsessionUserProcessLimit
());
return
util::system::launchChildProcess
(options.
rsessionPath
(),
runAsUser,
config,
pPid) ;
}
Error
launchSession
(
const
std::string& username, PidType* pPid)
{
return
launchSession
(username,
core::system::Options
(), pPid);
}
}
//
namespace server
Back
|
FazBrowse Home
|
New Git URL