FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
rstudio/src/cpp/server/ServerMain.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
/
ServerMain.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
417 lines (343 loc) · 12.8 KB
Breadcrumbs
rstudio
/
src
/
cpp
/
server
/
ServerMain.cpp
Copy path
File metadata and controls
417 lines (343 loc) · 12.8 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
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
/*
* ServerMain.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
<
pthread.h
>
#
include
<
signal.h
>
#
include
<
core/Error.hpp
>
#
include
<
core/ProgramStatus.hpp
>
#
include
<
core/ProgramOptions.hpp
>
#
include
<
core/text/TemplateFilter.hpp
>
#
include
<
core/system/System.hpp
>
#
include
<
core/system/Crypto.hpp
>
#
include
<
core/http/URL.hpp
>
#
include
<
core/http/AsyncUriHandler.hpp
>
#
include
<
core/http/TcpIpAsyncServer.hpp
>
#
include
<
core/gwt/GwtLogHandler.hpp
>
#
include
<
core/gwt/GwtFileHandler.hpp
>
#
include
<
session/SessionConstants.hpp
>
#
include
<
server/util/system/System.hpp
>
#
include
<
server/auth/ServerAuthHandler.hpp
>
#
include
<
server/auth/ServerValidateUser.hpp
>
#
include
<
server/auth/ServerSecureCookie.hpp
>
#
include
<
server/auth/ServerSecureUriHandler.hpp
>
#
include
<
server/ServerOptions.hpp
>
#
include
<
server/ServerUriHandlers.hpp
>
#
include
"
ServerAddins.hpp
"
#
include
"
ServerAppArmor.hpp
"
#
include
"
ServerBrowser.hpp
"
#
include
"
ServerOffline.hpp
"
#
include
"
ServerPAMAuth.hpp
"
#
include
"
ServerSessionProxy.hpp
"
#
include
"
ServerREnvironment.hpp
"
#
include
"
ServerSessionManager.hpp
"
using
namespace
core
;
using
namespace
server
;
namespace
{
bool
mainPageFilter
(
const
core::http::Request& request,
core::http::Response* pResponse)
{
return
server::browser::supportedBrowserFilter
(request, pResponse) &&
auth::handler::mainPageFilter
(request, pResponse);
}
http::UriHandlerFunction
blockingFileHandler
()
{
Options& options =
server::options
();
return
gwt::fileHandlerFunction
(options.
wwwLocalPath
(),
"
/
"
,
mainPageFilter);
}
//
//
some fancy footwork is required to take the standand blocking file handler
//
and make it work within a secure async context.
//
auth::SecureAsyncUriHandlerFunction
secureAsyncFileHandler
()
{
//
create a functor which can adapt a synchronous file handler into
//
an asynchronous handler
class
FileRequestHandler
{
public:
static
void
handleRequest
(
const
http::UriHandlerFunction& fileHandlerFunction,
boost::shared_ptr<http::AsyncConnection> pConnection)
{
fileHandlerFunction
(pConnection->
request
(), &(pConnection->
response
()));
pConnection->
writeResponse
();
}
};
//
use this functor to generate an async uri handler function from the
//
stock blockingFileHandler (defined above)
http::AsyncUriHandlerFunction asyncFileHandler =
boost::bind
(FileRequestHandler::handleRequest,
blockingFileHandler
(), _1);
//
finally, adapt this to be a secure async uri handler by binding out the
//
first parameter (username, which the gwt file handler knows nothing of)
return
boost::bind
(asyncFileHandler, _2);
}
//
http server
boost::scoped_ptr<http::TcpIpAsyncServer> s_pHttpServer;
Error
httpServerInit
()
{
//
create http server
s_pHttpServer.
reset
(
new
http::TcpIpAsyncServer
(
"
RStudio
"
));
//
set server options
s_pHttpServer->
setAbortOnResourceError
(
true
);
//
initialize the http server
Options& options =
server::options
();
return
s_pHttpServer->
init
(options.
wwwAddress
(), options.
wwwPort
());
}
void
httpServerAddHandlers
()
{
//
establish json-rpc handlers
using
namespace
server
::auth
;
using
namespace
server
::session_proxy
;
uri_handlers::add
(
"
/rpc
"
,
secureAsyncJsonRpcHandler
(proxyRpcRequest));
uri_handlers::add
(
"
/events
"
,
secureAsyncJsonRpcHandler
(proxyEventsRequest));
//
establish content handlers
uri_handlers::add
(
"
/graphics
"
,
secureAsyncHttpHandler
(proxyContentRequest));
uri_handlers::add
(
"
/upload
"
,
secureAsyncUploadHandler
(proxyContentRequest));
uri_handlers::add
(
"
/export
"
,
secureAsyncHttpHandler
(proxyContentRequest));
uri_handlers::add
(
"
/source
"
,
secureAsyncHttpHandler
(proxyContentRequest));
uri_handlers::add
(
"
/content
"
,
secureAsyncHttpHandler
(proxyContentRequest));
uri_handlers::add
(
"
/diff
"
,
secureAsyncHttpHandler
(proxyContentRequest));
uri_handlers::add
(
"
/file_show
"
,
secureAsyncHttpHandler
(proxyContentRequest));
uri_handlers::add
(
"
/agreement
"
,
secureAsyncHttpHandler
(proxyContentRequest));
//
content handlers which might be accessed outside the context of the
//
workbench get secure + authentication when required
uri_handlers::add
(
"
/help
"
,
secureAsyncHttpHandler
(proxyContentRequest,
true
));
uri_handlers::add
(
"
/files
"
,
secureAsyncHttpHandler
(proxyContentRequest,
true
));
uri_handlers::add
(
"
/custom
"
,
secureAsyncHttpHandler
(proxyContentRequest,
true
));
uri_handlers::add
(
"
/session
"
,
secureAsyncHttpHandler
(proxyContentRequest,
true
));
uri_handlers::add
(
"
/docs
"
,
secureAsyncHttpHandler
(
secureAsyncFileHandler
(),
true
));
//
establish logging handler
uri_handlers::addBlocking
(
"
/log
"
,
secureJsonRpcHandler
(gwt::handleLogRequest));
//
establish progress handler
FilePath
wwwLocalPath
(
server::options
().
wwwLocalPath
());
FilePath progressPagePath = wwwLocalPath.
complete
(
"
progress.htm
"
);
uri_handlers::addBlocking
(
"
/progress
"
,
secureHttpHandler
(
boost::bind
(
core::text::handleSecureTemplateRequest,
_1, progressPagePath, _2, _3)));
//
establish browser unsupported handler
using
namespace
server
::browser
;
uri_handlers::addBlocking
(
kBrowserUnsupported
,
handleBrowserUnsupportedRequest);
//
restrct access to templates directory
uri_handlers::addBlocking
(
"
/templates
"
, http::notFoundHandler);
//
add default handler for gwt app
uri_handlers::setBlockingDefault
(
blockingFileHandler
());
}
//
bogus SIGCHLD handler (never called)
void
handleSIGCHLD
(
int
)
{
}
//
wait for and handle child exit signals
Error
waitForChildExits
()
{
//
setup bogus handler for SIGCHLD (if we don't do this then
//
we can't successfully block/wait for the signal). This also
//
allows us to specify SA_NOCLDSTOP
struct
sigaction
sa;
::memset
(&sa,
0
,
sizeof
sa);
sa.
sa_handler
= handleSIGCHLD;
sigemptyset
(&sa.
sa_mask
);
sa.
sa_flags
=
SA_NOCLDSTOP
;
int
result = ::
sigaction
(
SIGCHLD
, &sa,
NULL
);
if
(result !=
0
)
return
systemError
(errno,
ERROR_LOCATION
);
//
block SIGCHLD (so we can sigwait on it below). note that on OSX
//
we also need to wait for termination related signals (otherwise
//
they are never delivered)
sigset_t
wait_mask;
sigemptyset
(&wait_mask);
sigaddset
(&wait_mask,
SIGCHLD
);
#
ifdef
__APPLE__
sigaddset
(&wait_mask,
SIGINT
);
sigaddset
(&wait_mask,
SIGQUIT
);
sigaddset
(&wait_mask,
SIGTERM
);
#
endif
result = ::
pthread_sigmask
(
SIG_BLOCK
, &wait_mask,
NULL
);
if
(result !=
0
)
return
systemError
(result,
ERROR_LOCATION
);
//
wait for child exits
for
(;;)
{
//
perform wait
int
sig =
0
;
int
result = ::
sigwait
(&wait_mask, &sig);
if
(result !=
0
)
return
systemError
(result,
ERROR_LOCATION
);
//
SIGCHLD
if
(sig ==
SIGCHLD
)
{
sessionManager
().
notifySIGCHLD
();
}
#
ifdef
__APPLE__
else
if
(sig ==
SIGINT
|| sig ==
SIGQUIT
|| sig ==
SIGTERM
)
{
::exit
(sig);
}
#
endif
//
Unexpected signal
else
{
LOG_WARNING_MESSAGE
(
"
Unexpected signal returned from sigwait:
"
+
boost::lexical_cast<std::string>(sig));
}
}
//
keep compiler happy (we never get here)
return
Success
();
}
}
//
anonymous namespace
//
provide global access to handlers
namespace
server
{
namespace
uri_handlers
{
void
add
(
const
std::string& prefix,
const
http::AsyncUriHandlerFunction& handler)
{
s_pHttpServer->
addHandler
(prefix, handler);
}
void
addBlocking
(
const
std::string& prefix,
const
http::UriHandlerFunction& handler)
{
s_pHttpServer->
addBlockingHandler
(prefix, handler);
}
void
setDefault
(
const
http::AsyncUriHandlerFunction& handler)
{
s_pHttpServer->
setDefaultHandler
(handler);
}
//
set blocking default handler
void
setBlockingDefault
(
const
http::UriHandlerFunction& handler)
{
s_pHttpServer->
setBlockingDefaultHandler
(handler);
}
}
//
namespace uri_handlers
}
//
namespace server
int
main
(
int
argc,
char
*
const
argv[])
{
try
{
//
initialize log
initializeSystemLog
(
"
rserver
"
, core::system::
kLogLevelWarning
);
//
read program options
Options& options =
server::options
();
ProgramStatus status = options.
read
(argc, argv);
if
( status.
exit
() )
return
status.
exitCode
() ;
//
daemonize if requested
if
(options.
serverDaemonize
())
{
Error error =
util::system::daemonize
();
if
(error)
return
core::system::exitFailure
(error,
ERROR_LOCATION
);
error =
core::system::ignoreTerminalSignals
();
if
(error)
return
core::system::exitFailure
(error,
ERROR_LOCATION
);
//
set file creation mask to 022 (might have inherted 0 from init)
setUMask
(util::system::OthersNoWriteMask);
}
//
detect R environment variables (calls R (and this forks) so must
//
happen after daemonize so that upstart script can correctly track us
std::string errMsg;
bool
detected =
r_environment::initialize
(&errMsg);
if
(!detected)
{
program_options::reportError
(errMsg,
ERROR_LOCATION
);
return
EXIT_FAILURE
;
}
//
increase the number of open files allowed (need more files
//
so we can supports lots of concurrent connectins)
if
(
util::system::realUserIsRoot
())
{
Error error =
setResourceLimit
(util::system::FilesLimit,
4096
);
if
(error)
return
core::system::exitFailure
(error,
ERROR_LOCATION
);
}
//
set working directory
Error error =
FilePath
(options.
serverWorkingDir
()).
makeCurrentPath
();
if
(error)
return
core::system::exitFailure
(error,
ERROR_LOCATION
);
//
initialize crypto utils
core::system::crypto::initialize
();
//
initialize secure cookie module
error =
auth::secure_cookie::initialize
();
if
(error)
return
core::system::exitFailure
(error,
ERROR_LOCATION
);
//
initialize the session proxy
error =
session_proxy::initialize
();
if
(error)
return
core::system::exitFailure
(error,
ERROR_LOCATION
);
//
initialize http server
error =
httpServerInit
();
if
(error)
return
core::system::exitFailure
(error,
ERROR_LOCATION
);
//
add handlers and initiliaze addins (offline has distinct behavior)
if
(
server::options
().
serverOffline
())
{
offline::httpServerAddHandlers
();
}
else
{
//
add handlers
httpServerAddHandlers
();
//
initialize addins
error =
addins::initialize
();
if
(error)
return
core::system::exitFailure
(error,
ERROR_LOCATION
);
//
initialize pam auth if we don't already have an auth handler
if
(!
auth::handler::isRegistered
())
{
error =
pam_auth::initialize
();
if
(error)
return
core::system::exitFailure
(error,
ERROR_LOCATION
);
}
}
//
enforce restricted mode if we are running under app armor
//
note that failure to do this (for whatever unanticipated reason)
//
is not considered fatal however it is logged as an error
//
so the sys-admin is informed
if
(options.
serverAppArmorEnabled
())
{
error =
app_armor::enforceRestricted
();
if
(error)
LOG_ERROR
(error);
}
//
give up root privilige if requested
std::string runAsUser = options.
serverUser
();
if
(!runAsUser.
empty
())
{
//
drop root priv
Error error =
util::system::temporarilyDropPriv
(runAsUser);
if
(error)
return
core::system::exitFailure
(error,
ERROR_LOCATION
);
}
//
run special verify installation mode if requested
if
(options.
verifyInstallation
())
{
Error error =
session_proxy::runVerifyInstallationSession
();
if
(error)
return
core::system::exitFailure
(error,
ERROR_LOCATION
);
return
EXIT_SUCCESS
;
}
//
run http server
error = s_pHttpServer->
run
(options.
wwwThreadPoolSize
());
if
(error)
return
core::system::exitFailure
(error,
ERROR_LOCATION
);
//
wait for child exits
error =
waitForChildExits
();
if
(error)
return
core::system::exitFailure
(error,
ERROR_LOCATION
);
//
NOTE: we never get here because waitForChildExits waits forever
return
EXIT_SUCCESS
;
}
CATCH_UNEXPECTED_EXCEPTION
//
if we got this far we had an unexpected exception
return
EXIT_FAILURE
;
}
Back
|
FazBrowse Home
|
New Git URL