-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathServerSessionManager.cpp
More file actions
446 lines (370 loc) · 14.8 KB
/
Copy pathServerSessionManager.cpp
File metadata and controls
446 lines (370 loc) · 14.8 KB
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/*
* ServerSessionManager.cpp
*
* Copyright (C) 2021 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 <core/CrashHandler.hpp>
#include <server/ServerSessionManager.hpp>
#include <boost/format.hpp>
#include <shared_core/SafeConvert.hpp>
#include <core/system/Process.hpp>
#include <core/system/PosixUser.hpp>
#include <core/system/Environment.hpp>
#include <core/json/JsonRpc.hpp>
#include <monitor/MonitorClient.hpp>
#include <session/SessionConstants.hpp>
#include <server/ServerOptions.hpp>
#include <server/ServerPaths.hpp>
#include <server/ServerErrorCategory.hpp>
#include <server/auth/ServerValidateUser.hpp>
#include "ServerREnvironment.hpp"
#include "server-config.h"
using namespace rstudio::core;
namespace rstudio {
namespace server {
namespace {
static std::string s_launcherToken;
void readRequestArgs(const core::http::Request& request, core::system::Options *pArgs)
{
// we only do this when establishing new sessions via client_init
if (!boost::algorithm::ends_with(request.uri(), "client_init"))
return;
// parse the request (okay if it fails, none of the below is critical)
json::JsonRpcRequest clientInit;
Error error = json::parseJsonRpcRequest(request.body(), &clientInit);
if (error)
return;
// read parameters from the request if present
int restoreWorkspace = -1;
json::getOptionalParam<int>(clientInit.kwparams, "restore_workspace", -1, &restoreWorkspace);
if (restoreWorkspace != -1)
pArgs->push_back(std::make_pair("--r-restore-workspace",
safe_convert::numberToString(restoreWorkspace)));
int runRprofile = -1;
json::getOptionalParam<int>(clientInit.kwparams, "run_rprofile", -1, &runRprofile);
if (runRprofile != -1)
pArgs->push_back(std::make_pair("--r-run-rprofile",
safe_convert::numberToString(runRprofile)));
}
core::system::ProcessConfig sessionProcessConfig(
r_util::SessionContext context,
const core::system::Options& extraArgs = core::system::Options(),
bool requestIsSecure = false)
{
// 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,
context.username));
// pass the project if specified
if (!context.scope.project().empty())
{
args.push_back(std::make_pair("-" kProjectSessionOptionShort,
context.scope.project()));
}
// pass the scope id if specified
if (!context.scope.id().empty())
{
args.push_back(std::make_pair("-" kScopeSessionOptionShort,
context.scope.id()));
}
// ensure cookies are marked secure if applicable
bool useSecureCookies = options.authCookiesForceSecure() ||
options.getOverlayOption("ssl-enabled") == "1" ||
requestIsSecure;
args.push_back(std::make_pair("--" kUseSecureCookiesSessionOption,
useSecureCookies ? "1" : "0"));
args.push_back(std::make_pair("--" kRootPathSessionOption,
options.wwwRootPath()));
args.push_back(std::make_pair("--" kSameSiteSessionOption,
safe_convert::numberToString(static_cast<int>(options.wwwSameSite()))));
// create launch token if we haven't already
if (s_launcherToken.empty())
s_launcherToken = core::system::generateShortenedUuid();
args.push_back(std::make_pair("--launcher-token", s_launcherToken));
// allow session timeout to be overridden via environment variable
std::string timeout = core::system::getenv("RSTUDIO_SESSION_TIMEOUT");
if (!timeout.empty())
args.push_back(std::make_pair("--" kTimeoutSessionOption, timeout));
// 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,
safe_convert::numberToString(uid)));
// set session scope project if we have one
if (!context.scope.project().empty())
{
environment.push_back(std::make_pair(
kRStudioSessionScopeProject,
context.scope.project()));
}
// set session scope id if we have one
if (!context.scope.id().empty())
{
environment.push_back(std::make_pair(
kRStudioSessionScopeId,
context.scope.id()));
}
sessionProcessConfigOverlay(&args, &environment);
// pass extra params
std::copy(extraArgs.begin(), extraArgs.end(), std::back_inserter(args));
// append R environment variables
r_util::RVersion rVersion = r_environment::rVersion();
core::system::Options rEnvVars = rVersion.environment();
environment.insert(environment.end(), rEnvVars.begin(), rEnvVars.end());
// mark this as the system default R version
core::system::setenv(&environment,
kRStudioDefaultRVersion,
rVersion.number());
core::system::setenv(&environment,
kRStudioDefaultRVersionHome,
rVersion.homeDir().getAbsolutePath());
// forward the auth options
core::system::setenv(&environment,
kRStudioRequiredUserGroup,
options.authRequiredUserGroup());
core::system::setenv(&environment,
kRStudioMinimumUserId,
safe_convert::numberToString(
options.authMinimumUserId()));
// add monitor shared secret
environment.push_back(std::make_pair(kMonitorSharedSecretEnvVar,
options.monitorSharedSecret()));
// stamp the version number of the rserver process that is launching this session
// the session should log an error if its version does not match, as that is
// likely an unsupported configuration
environment.push_back({kRStudioVersion, RSTUDIO_VERSION});
// forward over crash handler environment if we have it (used for development mode)
if (!core::system::getenv(kCrashHandlerEnvVar).empty())
environment.push_back({kCrashHandlerEnvVar, core::system::getenv(kCrashHandlerEnvVar)});
if (!core::system::getenv(kCrashpadHandlerEnvVar).empty())
environment.push_back({kCrashpadHandlerEnvVar, core::system::getenv(kCrashpadHandlerEnvVar)});
// forward path for session temp dir (used for local stream path)
environment.push_back(
std::make_pair(kSessionTmpDirEnvVar, sessionTmpDir().getAbsolutePath()));
// build the config object and return it
core::system::ProcessConfig config;
config.args = args;
config.environment = environment;
config.stdStreamBehavior = core::system::StdStreamInherit;
return config;
}
void onProcessExit(const std::string& username, PidType pid)
{
}
} // anonymous namespace
SessionManager& sessionManager()
{
static SessionManager instance;
return instance;
}
SessionManager::SessionManager()
{
// set default session launcher
sessionLaunchFunction_ = boost::bind(&SessionManager::launchAndTrackSession,
this, _1, _2);
}
Error SessionManager::launchSession(boost::asio::io_service& ioService,
const r_util::SessionContext& context,
const http::Request& request,
const http::ResponseHandler& onLaunch,
const http::ErrorHandler& onError)
{
using namespace boost::posix_time;
LOCK_MUTEX(launchesMutex_)
{
// check whether we already have a launch pending
LaunchMap::const_iterator pos = pendingLaunches_.find(context);
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 " + context.username +" (aborting wait)");
pendingLaunches_.erase(context);
}
}
// record the launch
pendingLaunches_[context] = microsec_clock::universal_time();
}
END_LOCK_MUTEX
// translate querystring arguments into extra session args
core::system::Options args;
readRequestArgs(request, &args);
// determine launch options
r_util::SessionLaunchProfile profile;
profile.context = context;
profile.executablePath = server::options().rsessionPath();
profile.config = sessionProcessConfig(context, args, request.isSecure());
// pass the profile to any filters we have
for (SessionLaunchProfileFilter f : sessionLaunchProfileFilters_)
{
f(&profile);
}
// launch the session
Error error = sessionLaunchFunction_(ioService, profile, request, onLaunch, onError);
if (error)
{
removePendingLaunch(context);
return error;
}
return Success();
}
namespace {
boost::mutex s_configFilterMutex;
core::system::ProcessConfigFilter s_processConfigFilter;
} // anonymous namespace
void setProcessConfigFilter(const core::system::ProcessConfigFilter& filter)
{
LOCK_MUTEX(s_configFilterMutex)
{
s_processConfigFilter = filter;
}
END_LOCK_MUTEX
}
// default session launcher -- does the launch then tracks the pid
// for later reaping
Error SessionManager::launchAndTrackSession(
boost::asio::io_service&,
const core::r_util::SessionLaunchProfile& profile)
{
// if we are root then assume the identity of the user
using namespace rstudio::core::system;
std::string runAsUser = realUserIsRoot() ? profile.context.username : "";
core::system::ProcessConfigFilter configFilter;
LOCK_MUTEX(s_configFilterMutex)
{
configFilter = s_processConfigFilter;
}
END_LOCK_MUTEX
// retrieve profile config
auto config = profile.config;
// on macOS, we need to forward DYLD_INSERT_LIBRARIES
#if __APPLE__
auto rVersion = server::r_environment::rVersion();
auto rLibPath = rVersion.homeDir().completeChildPath("lib/libR.dylib");
core::system::setenv(
&config.environment,
"DYLD_INSERT_LIBRARIES",
rLibPath.getAbsolutePath());
#endif
// launch the session
PidType pid = 0;
Error error = launchChildProcess(profile.executablePath,
runAsUser,
config,
configFilter,
&pid);
if (error)
return error;
// track it for subsequent reaping
processTracker_.addProcess(pid, boost::bind(onProcessExit,
profile.context.username,
pid));
// return success
return Success();
}
void SessionManager::setSessionLaunchFunction(
const SessionLaunchFunction& launchFunction)
{
sessionLaunchFunction_ = launchFunction;
}
void SessionManager::addSessionLaunchProfileFilter(
const SessionLaunchProfileFilter& filter)
{
sessionLaunchProfileFilters_.push_back(filter);
}
void SessionManager::removePendingLaunch(const r_util::SessionContext& context)
{
LOCK_MUTEX(launchesMutex_)
{
pendingLaunches_.erase(context);
}
END_LOCK_MUTEX
}
void SessionManager::notifySIGCHLD()
{
processTracker_.notifySIGCHILD();
}
r_util::SessionLaunchProfile createSessionLaunchProfile(const r_util::SessionContext& context,
const core::system::Options& extraArgs)
{
r_util::SessionLaunchProfile profile;
profile.context = context;
profile.executablePath = server::options().rsessionPath();
profile.config = sessionProcessConfig(context, extraArgs);
// pass the profile to any filters we have
for (const SessionManager::SessionLaunchProfileFilter& f : sessionManager().getSessionLaunchProfileFilters())
{
f(&profile);
}
return profile;
}
// helper function for verify-installation
Error launchSession(const r_util::SessionContext& context,
const core::system::Options& extraArgs,
PidType* pPid)
{
// launch the session
// we use a modified configured home directory to provide a reliable temp dir that can be written to
// as the server (service) user most likely does not have a home directory configured
std::string username = context.username;
std::string rsessionPath = server::options().rsessionPath();
std::string runAsUser = core::system::realUserIsRoot() ? username : "";
core::system::ProcessConfig config = sessionProcessConfig(context,
extraArgs);
FilePath tmpDir;
Error error = FilePath::tempFilePath(tmpDir);
if (error)
{
LOG_ERROR(error);
}
else
{
error = tmpDir.ensureDirectory();
if (error)
{
LOG_ERROR(error);
}
else
{
FilePath userHome = tmpDir;
FilePath xdgConfigHome = tmpDir.completeChildPath(".config");
core::system::setenv(&config.environment, "HOME", userHome.getAbsolutePath());
core::system::setenv(&config.environment, "XDG_CONFIG_HOME", xdgConfigHome.getAbsolutePath());
}
}
*pPid = -1;
return core::system::launchChildProcess(rsessionPath,
runAsUser,
config,
core::system::ProcessConfigFilter(),
pPid);
}
} // namespace server
} // namespace rstudio