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
|
From faf9b22bc1caa538009356a3be1f0ee3ac8205f0 Mon Sep 17 00:00:00 2001
From: Ricardo Wurmus <rekado@elephly.net>
Date: Fri, 18 Dec 2020 10:32:01 +0100
Subject: [PATCH 2/7] launchAndTrackSession: Configure R version from active
session.
When switching projects the R version is recorded in the active
session. Before launching a new rsession process R environment
variables need to be set to select the variant of libR.so that should
be used to launch the embedded R.
This needs to happen *after* the process is forked and has changed the
UID to that of the target user account, so that we can read the user's
session files. We accomplish this by introducing a custom filter
function that modifies the process configuration just before the child
process replaces the fork.
* src/cpp/server/ServerSessionManager.cpp (loadRVersion): New
procedure to configure the R environment using the R version that has
been recorded in the active session.
(launchAndTrackSession): Register loadRVersion as a config filter.
---
.../server/session/ServerSessionManager.cpp | 49 +++++++++++++++++++
1 file changed, 49 insertions(+)
diff --git a/src/cpp/server/session/ServerSessionManager.cpp b/src/cpp/server/session/ServerSessionManager.cpp
index 17bcd10b56..e107b89dff 100644
--- a/src/cpp/server/session/ServerSessionManager.cpp
+++ b/src/cpp/server/session/ServerSessionManager.cpp
@@ -2,6 +2,7 @@
* ServerSessionManager.cpp
*
* Copyright (C) 2022 by Posit Software, PBC
+ * Copyright (C) 2020-2026 Ricardo Wurmus
*
* Unless you have received this program directly from Posit Software pursuant
* to the terms of a commercial license agreement with Posit Software, then
@@ -14,6 +15,7 @@
*/
#include <server/session/ServerSessionManager.hpp>
+#include <session/SessionScopes.hpp>
#include <boost/format.hpp>
@@ -22,10 +24,14 @@
#include <core/SocketRpc.hpp>
#include <core/system/System.hpp>
#include <core/system/Process.hpp>
+#include <shared_core/system/User.hpp>
#include <core/system/PosixUser.hpp>
#include <core/system/Environment.hpp>
#include <core/json/JsonRpc.hpp>
+#include <core/r_util/RActiveSessions.hpp>
+#include <core/r_util/RActiveSessionsStorage.hpp>
+
#include <session/SessionConstants.hpp>
#include <monitor/MonitorClient.hpp>
@@ -380,6 +386,46 @@ void setProcessConfigFilter(const core::system::ProcessConfigFilter& filter)
END_LOCK_MUTEX
}
+void loadRVersion(const core::system::User& user, core::system::ProcessConfig* config) {
+ // Get the active session (e.g. after switching projects) and set R
+ // variables according to the configured R version.
+ FilePath storageDir =
+ system::xdg::userDataDir(user.getUsername(), user.getHomePath());
+ std::shared_ptr<r_util::IActiveSessionsStorage> sessionStorage =
+ std::shared_ptr<r_util::IActiveSessionsStorage>(new r_util::FileActiveSessionsStorage(storageDir, rstudio::session::filePathToProjectId(storageDir, FilePath(server::options().getOverlayOption(kSessionSharedStoragePath)))));
+
+ std::unique_ptr<r_util::ActiveSessions> activeSessions =
+ std::unique_ptr<r_util::ActiveSessions>(new r_util::ActiveSessions(sessionStorage, storageDir));
+ std::vector<boost::shared_ptr<r_util::ActiveSession> > sessions =
+ activeSessions->list(false);
+
+ r_util::RVersion rVersion;
+ if (sessions.size() > 0) {
+ // Get the R version from the active user session. This will
+ // have been set earlier when the user switched to a project.
+ std::string errorMsg;
+ bool success = r_environment::detectRVersion(FilePath((*sessions.front()).rVersionHome()).completePath("bin/R"),
+ &rVersion, &errorMsg);
+
+ if (!success) {
+ rVersion = r_environment::rVersion();
+ }
+ } else {
+ rVersion = r_environment::rVersion();
+ }
+ core::system::Options rEnvVars = rVersion.environment();
+ config->environment.insert(config->environment.end(), rEnvVars.begin(), rEnvVars.end());
+
+ // mark this as the system default R version
+ core::system::setenv(&(config->environment),
+ kRStudioDefaultRVersion,
+ rVersion.number());
+ core::system::setenv(&(config->environment),
+ kRStudioDefaultRVersionHome,
+ rVersion.homeDir().getAbsolutePath());
+ return;
+}
+
// default session launcher -- does the launch then tracks the pid
// for later reaping
Error SessionManager::launchAndTrackSession(
@@ -390,6 +436,9 @@ Error SessionManager::launchAndTrackSession(
using namespace rstudio::core::system;
std::string runAsUser = realUserIsRoot() ? profile.context.username : "";
+ LOG_DEBUG_MESSAGE("REKADO loading R version");
+ setProcessConfigFilter(loadRVersion);
+
core::system::ProcessConfigFilter configFilter;
LOCK_MUTEX(s_configFilterMutex)
{
--
2.54.0
|