-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathServerMain.cpp
More file actions
842 lines (701 loc) · 27.5 KB
/
Copy pathServerMain.cpp
File metadata and controls
842 lines (701 loc) · 27.5 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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
/*
* ServerMain.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 <pthread.h>
#include <signal.h>
#include <core/CrashHandler.hpp>
#include <core/FileLock.hpp>
#include <core/Log.hpp>
#include <core/ProgramStatus.hpp>
#include <core/ProgramOptions.hpp>
#include <core/text/TemplateFilter.hpp>
#include <core/system/PosixSystem.hpp>
#include <core/system/Crypto.hpp>
#include <core/http/URL.hpp>
#include <core/http/AsyncUriHandler.hpp>
#include <server_core/http/SecureCookie.hpp>
#include <core/http/TcpIpAsyncServer.hpp>
#include <core/gwt/GwtLogHandler.hpp>
#include <core/gwt/GwtFileHandler.hpp>
#include <server_core/ServerDatabase.hpp>
#include <monitor/MonitorClient.hpp>
#include <session/SessionConstants.hpp>
#include <server/auth/ServerAuthHandler.hpp>
#include <server/auth/ServerValidateUser.hpp>
#include <server/auth/ServerSecureUriHandler.hpp>
#include <server/ServerOptions.hpp>
#include <server/ServerUriHandlers.hpp>
#include <server/ServerScheduler.hpp>
#include <server/ServerSessionProxy.hpp>
#include <server/ServerSessionManager.hpp>
#include <server/ServerProcessSupervisor.hpp>
#include <server/ServerPaths.hpp>
#include <shared_core/Error.hpp>
#include <shared_core/system/User.hpp>
#include "ServerAddins.hpp"
#include "ServerBrowser.hpp"
#include "ServerEval.hpp"
#include "ServerEnvVars.hpp"
#include "ServerInit.hpp"
#include "ServerMeta.hpp"
#include "ServerOffline.hpp"
#include "ServerPAMAuth.hpp"
#include "ServerREnvironment.hpp"
#include "ServerXdgVars.hpp"
using namespace rstudio;
using namespace rstudio::core;
using namespace rstudio::server;
// forward-declare overlay methods
namespace rstudio {
namespace server {
namespace overlay {
Error initialize();
Error startup();
bool reloadConfiguration();
void shutdown();
bool requireLocalR();
} // namespace overlay
} // namespace server
} // namespace rstudio
namespace {
const char * const kProgramIdentity = "rserver";
bool mainPageFilter(const core::http::Request& request,
core::http::Response* pResponse)
{
return server::eval::expirationFilter(request, pResponse) &&
server::browser::supportedBrowserFilter(request, pResponse) &&
auth::handler::mainPageFilter(request, pResponse);
}
http::UriHandlerFunction blockingFileHandler()
{
Options& options = server::options();
// determine initJs (none for now)
std::string initJs;
// return file
return gwt::fileHandlerFunction(options.wwwLocalPath(),
"/",
mainPageFilter,
initJs,
options.gwtPrefix(),
options.wwwUseEmulatedStack(),
"", // no server homepage in open source
options.wwwFrameOrigin());
}
//
// 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::shared_ptr<http::AsyncServer> s_pHttpServer;
Error httpServerInit()
{
http::Headers additionalHeaders;
for (const std::string& headerStr : options().serverAddHeaders())
{
size_t pos = headerStr.find(':');
if (pos == std::string::npos)
{
LOG_WARNING_MESSAGE("Invalid header " + headerStr +
" will be skipped and not be written to outgoing requests");
continue;
}
additionalHeaders.emplace_back(string_utils::trimWhitespace(headerStr.substr(0, pos)),
string_utils::trimWhitespace(headerStr.substr(pos+1)));
}
s_pHttpServer.reset(server::httpServerCreate(additionalHeaders));
// set server options
s_pHttpServer->setAbortOnResourceError(true);
s_pHttpServer->setScheduledCommandInterval(
boost::posix_time::milliseconds(500));
// initialize
return rstudio::server::httpServerInit(s_pHttpServer.get());
}
void pageNotFoundHandler(const http::Request& request,
http::Response* pResponse)
{
std::ostringstream os;
std::map<std::string, std::string> vars;
vars["request_uri"] = string_utils::jsLiteralEscape(request.uri());
vars["base_uri"] = string_utils::jsLiteralEscape(request.baseUri(core::http::BaseUriUse::External));
FilePath notFoundTemplate = FilePath(options().wwwLocalPath()).completeChildPath("404.htm");
core::Error err = core::text::renderTemplate(notFoundTemplate, vars, os);
if (err)
{
// if we cannot display the 404 page log the error
// note: this should never happen in a proper deployment
LOG_ERROR(err);
}
else
{
std::string body = os.str();
pResponse->setContentType("text/html");
pResponse->setBodyUnencoded(body);
}
// set 404 status even if there was an error showing the proper not found page
pResponse->setStatusCode(core::http::status::NotFound);
}
void rootPathRequestFilter(
boost::asio::io_service& ioService,
http::Request* pRequest,
http::RequestFilterContinuation continuation)
{
// for all requests, be sure to inject the configured root path
// this way proxied requests will redirect correctly and cookies
// will have the correct path
pRequest->setRootPath(options().wwwRootPath());
continuation(boost::shared_ptr<http::Response>());
}
void httpServerAddHandlers()
{
// establish json-rpc handlers
using namespace server::auth;
using namespace server::session_proxy;
uri_handlers::add("/rpc", secureAsyncJsonRpcHandlerEx(proxyRpcRequest));
uri_handlers::add("/events", secureAsyncJsonRpcHandler(proxyEventsRequest));
// establish content handlers
uri_handlers::add("/graphics", secureAsyncHttpHandler(proxyContentRequest));
uri_handlers::addUploadHandler("/upload", secureAsyncUploadHandler(proxyUploadRequest));
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("/view_pdf", secureAsyncHttpHandler(proxyContentRequest));
uri_handlers::add("/agreement", secureAsyncHttpHandler(proxyContentRequest));
uri_handlers::add("/presentation", secureAsyncHttpHandler(proxyContentRequest));
uri_handlers::add("/pdf_js", secureAsyncHttpHandler(proxyContentRequest));
uri_handlers::add("/mathjax", secureAsyncHttpHandler(proxyContentRequest));
uri_handlers::add("/connections", secureAsyncHttpHandler(proxyContentRequest));
uri_handlers::add("/theme", secureAsyncHttpHandler(proxyContentRequest));
uri_handlers::add("/fonts", secureAsyncHttpHandler(proxyContentRequest));
uri_handlers::add("/python", secureAsyncHttpHandler(proxyContentRequest));
uri_handlers::add("/tutorial", 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));
uri_handlers::add("/html_preview", secureAsyncHttpHandler(proxyContentRequest, true));
uri_handlers::add("/rmd_output", secureAsyncHttpHandler(proxyContentRequest, true));
uri_handlers::add("/grid_data", secureAsyncHttpHandler(proxyContentRequest, true));
uri_handlers::add("/grid_resource", secureAsyncHttpHandler(proxyContentRequest, true));
uri_handlers::add("/chunk_output", secureAsyncHttpHandler(proxyContentRequest, true));
uri_handlers::add("/profiles", secureAsyncHttpHandler(proxyContentRequest, true));
uri_handlers::add("/rmd_data", secureAsyncHttpHandler(proxyContentRequest, true));
uri_handlers::add("/profiler_resource", secureAsyncHttpHandler(proxyContentRequest, true));
uri_handlers::add("/dictionaries", secureAsyncHttpHandler(proxyContentRequest, true));
// proxy localhost if requested
if (server::options().wwwProxyLocalhost())
{
uri_handlers::addProxyHandler("/p/", secureAsyncHttpHandler(
boost::bind(proxyLocalhostRequest, false, _1, _2), true));
uri_handlers::addProxyHandler("/p6/", secureAsyncHttpHandler(
boost::bind(proxyLocalhostRequest, true, _1, _2), true));
}
// establish logging handler
uri_handlers::addBlocking("/log", secureJsonRpcHandler(gwt::handleLogRequest));
// establish meta
uri_handlers::addBlocking("/meta", secureJsonRpcHandler(meta::handleMetaRequest));
// establish progress handler
FilePath wwwPath(server::options().wwwLocalPath());
FilePath progressPagePath = wwwPath.completePath("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", pageNotFoundHandler);
// initialize gwt symbol maps
gwt::initializeSymbolMaps(server::options().wwwSymbolMapsPath());
// add default handler for gwt app
uri_handlers::setBlockingDefault(blockingFileHandler());
}
Error initLog()
{
return core::system::initializeSystemLog(kProgramIdentity, core::log::LogLevel::WARN, false);
}
bool reloadLoggingConfiguration()
{
LOG_INFO_MESSAGE("Reloading logging configuration...");
Error error = initLog();
if (error)
{
LOG_ERROR_MESSAGE("Failed to reload logging configuration");
LOG_ERROR(error);
}
else
{
LOG_INFO_MESSAGE("Successfully reloaded logging configuration");
}
return !static_cast<bool>(error);
}
bool reloadEnvConfiguration()
{
LOG_INFO_MESSAGE("Reloading environment configuration...");
Error error = env_vars::initialize();
if (error)
{
LOG_ERROR_MESSAGE("Failed to reload environment configuration");
LOG_ERROR(error);
}
else
{
LOG_INFO_MESSAGE("Successfully reloaded environment configuration");
}
return !static_cast<bool>(error);
}
void reloadConfiguration()
{
bool success = reloadLoggingConfiguration();
success = reloadEnvConfiguration() && success;
success = overlay::reloadConfiguration() && success;
if (success)
{
LOG_INFO_MESSAGE("Successfully reloaded all configuration");
}
else
{
LOG_ERROR_MESSAGE("Configuration reload unsuccessful");
}
}
// bogus SIGCHLD handler (never called)
void handleSIGCHLD(int)
{
}
// wait for and handle signals
Error waitForSignals()
{
// 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, nullptr);
if (result != 0)
return systemError(errno, ERROR_LOCATION);
// block signals that we want to sigwait on
sigset_t wait_mask;
sigemptyset(&wait_mask);
sigaddset(&wait_mask, SIGCHLD);
sigaddset(&wait_mask, SIGINT);
sigaddset(&wait_mask, SIGQUIT);
sigaddset(&wait_mask, SIGTERM);
sigaddset(&wait_mask, SIGHUP);
result = ::pthread_sigmask(SIG_BLOCK, &wait_mask, nullptr);
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();
}
// SIGINT, SIGQUIT, SIGTERM
else if (sig == SIGINT || sig == SIGQUIT || sig == SIGTERM)
{
//
// Here is where we can perform server cleanup e.g.
// closing pam sessions
//
// call overlay shutdown
overlay::shutdown();
// clear the signal mask
Error error = core::system::clearSignalMask();
if (error)
LOG_ERROR(error);
// reset the signal to its default
struct sigaction sa;
::memset(&sa, 0, sizeof sa);
sa.sa_handler = SIG_DFL;
int result = ::sigaction(sig, &sa, nullptr);
if (result != 0)
LOG_ERROR(systemError(result, ERROR_LOCATION));
// re-raise the signal
::kill(::getpid(), sig);
}
// SIGHUP
else if (sig == SIGHUP)
{
reloadConfiguration();
}
// Unexpected signal
else
{
LOG_WARNING_MESSAGE("Unexpected signal returned from sigwait: " +
safe_convert::numberToString(sig));
}
}
// keep compiler happy (we never get here)
return Success();
}
} // anonymous namespace
// provide global access to handlers
namespace rstudio {
namespace server {
namespace uri_handlers {
void add(const std::string& prefix,
const http::AsyncUriHandlerFunction& handler)
{
s_pHttpServer->addHandler(prefix, handler);
}
void addUploadHandler(const std::string& prefix,
const http::AsyncUriUploadHandlerFunction& handler)
{
s_pHttpServer->addUploadHandler(prefix, handler);
}
void addProxyHandler(const std::string& prefix,
const http::AsyncUriHandlerFunction& handler)
{
s_pHttpServer->addProxyHandler(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);
}
void setRequestFilter(const core::http::RequestFilter& filter)
{
s_pHttpServer->setRequestFilter(filter);
}
void setResponseFilter(const core::http::ResponseFilter& filter)
{
s_pHttpServer->setResponseFilter(filter);
}
} // namespace uri_handlers
boost::shared_ptr<http::AsyncServer> server()
{
return s_pHttpServer;
}
namespace scheduler {
void addCommand(boost::shared_ptr<ScheduledCommand> pCmd)
{
s_pHttpServer->addScheduledCommand(pCmd);
}
} // namespace scheduler
} // namespace server
} // namespace rstudio
int main(int argc, char * const argv[])
{
try
{
Error error = initLog();
if (error)
{
core::log::writeError(error, std::cerr);
return EXIT_FAILURE;
}
// ignore SIGPIPE (don't log error because we should never call
// syslog prior to daemonizing)
core::system::ignoreSignal(core::system::SigPipe);
#ifdef __APPLE__
// warn if the rstudio pam profile does not exist
// (note that this only effects macOS development configurations)
FilePath pamProfilePath("/etc/pam.d/rstudio");
if (!pamProfilePath.exists())
{
std::cerr << "WARNING: /etc/pam.d/rstudio does not exist; authentication may fail!" << std::endl;
std::cerr << "Run 'sudo cp /etc/pam.d/cups /etc/pam.d/rstudio' to set a default PAM profile for RStudio." << std::endl;
}
#endif
// read program options
std::ostringstream osWarnings;
Options& options = server::options();
ProgramStatus status = options.read(argc, argv, osWarnings);
std::string optionsWarnings = osWarnings.str();
if ( status.exit() )
{
if (!optionsWarnings.empty())
program_options::reportWarnings(optionsWarnings, ERROR_LOCATION);
return status.exitCode();
}
// daemonize if requested
if (options.serverDaemonize() && options.dbCommand().empty())
{
Error error = core::system::daemonize(options.serverPidFile());
if (error)
return core::system::exitFailure(error, ERROR_LOCATION);
error = core::system::ignoreTerminalSignals();
if (error)
return core::system::exitFailure(error, ERROR_LOCATION);
// Reload the loggers after succesful daemonize to clear out old FDs.
core::log::reloadAllLogDestinations();
// set file creation mask to 022 (might have inherted 0 from init)
if (options.serverSetUmask())
setUMask(core::system::OthersNoWriteMask);
}
// increase the number of open files allowed (need more files
// so we can supports lots of concurrent connectins)
if (core::system::realUserIsRoot())
{
Error error = setResourceLimit(core::system::FilesLimit, 4096);
if (error)
return core::system::exitFailure(error, ERROR_LOCATION);
}
// set working directory
error = FilePath(options.serverWorkingDir()).makeCurrentPath();
if (error)
return core::system::exitFailure(error, ERROR_LOCATION);
// initialize server data directory
FilePath serverDataDir = options.serverDataDir();
error = serverDataDir.ensureDirectory();
if (error)
return core::system::exitFailure(error, ERROR_LOCATION);
boost::optional<system::User> serverUser;
if (core::system::effectiveUserIsRoot())
{
auto shouldChown = [&](int depth, const FilePath& file)
{
// don't chown user sockets - they belong to the user
if (depth == 3 &&
boost::ends_with(file.getParent().getParent().getFilename(), "-ds"))
return false;
if (depth == 1 &&
(boost::ends_with(file.getFilename(), "-d") ||
boost::ends_with(file.getFilename(), "-d.pid")))
return false;
return true;
};
system::User serverUserObj;
error = system::User::getUserFromIdentifier(options.serverUser(), serverUserObj);
if (error)
return core::system::exitFailure(error, ERROR_LOCATION);
serverUser = serverUserObj;
error = serverDataDir.changeOwnership(serverUserObj, true, shouldChown);
if (error)
{
error.addProperty("description",
"Could not change owner for path " + serverDataDir.getAbsolutePath() +
". Is root squash enabled?");
LOG_ERROR(error);
}
}
// ensure permissions - the folder needs to be readable and writeable
// by all users of the system, and the sticky bit must be set to ensure
// that users do not delete each others' sockets
struct stat st;
if (::stat(serverDataDir.getAbsolutePath().c_str(), &st) == -1)
{
Error error = systemError(errno,
"Could not determine permissions on specified 'server-data-dir' "
"directory (" + serverDataDir.getAbsolutePath() + ")",
ERROR_LOCATION);
return core::system::exitFailure(error, ERROR_LOCATION);
}
unsigned desiredMode = S_IRWXU | S_IRWXG | S_IRWXO | S_ISVTX;
if ((st.st_mode & desiredMode) != desiredMode)
{
// permissions aren't correct - attempt to fix them
Error error = serverDataDir.changeFileMode(core::FileMode::ALL_READ_WRITE_EXECUTE, true);
if (error)
{
LOG_ERROR_MESSAGE("Could not change permissions for specified 'server-data-dir' - "
"the directory (" + serverDataDir.getAbsolutePath() + ") must be "
"writeable by all users and have the sticky bit set");
return core::system::exitFailure(error, ERROR_LOCATION);
}
}
// export important environment variables
core::system::setenv(kServerDataDirEnvVar, serverDataDir.getAbsolutePath());
core::system::setenv(kSessionTmpDirEnvVar, sessionTmpDir().getAbsolutePath());
// initialize File Lock
FileLock::initialize();
// initialize crypto utils
core::system::crypto::initialize();
// initialize secure cookie module
error = core::http::secure_cookie::initialize(options.secureCookieKeyFile());
if (error)
return core::system::exitFailure(error, ERROR_LOCATION);
// execute any database commands if passed
if (!options.dbCommand().empty())
{
Error error = server_core::database::execute(options.databaseConfigFile(), serverUser, options.dbCommand());
if (error)
return core::system::exitFailure(error, ERROR_LOCATION);
return EXIT_SUCCESS;
}
// initialize database connectivity
error = server_core::database::initialize(options.databaseConfigFile(), true, serverUser);
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);
// initialize the process supervisor (needs to happen post http server
// init for access to the scheduled command list)
error = process_supervisor::initialize();
if (error)
return core::system::exitFailure(error, ERROR_LOCATION);
// initialize monitor (needs to happen post http server init for access
// to the server's io service)
monitor::initializeMonitorClient(monitorSocketPath().getAbsolutePath(),
server::options().monitorSharedSecret(),
s_pHttpServer->ioService());
if (!options.verifyInstallation())
{
// add a monitor log writer
core::log::addLogDestination(
monitor::client().createLogDestination(core::log::LogLevel::WARN, kProgramIdentity));
}
// initialize XDG var insertion
error = xdg_vars::initialize();
if (error)
return core::system::exitFailure(error, ERROR_LOCATION);
// initialize environment variables
error = env_vars::initialize();
if (error)
{
// error loading env vars is non-fatal
LOG_ERROR(error);
}
// overlay may replace this
if (server::options().wwwRootPath() != kRequestDefaultRootPath)
{
// inject the path prefix as the root path for all requests
uri_handlers::setRequestFilter(rootPathRequestFilter);
}
// call overlay initialize
error = overlay::initialize();
if (error)
return core::system::exitFailure(error, ERROR_LOCATION);
// 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 && overlay::requireLocalR())
{
program_options::reportError(errMsg, ERROR_LOCATION);
return EXIT_FAILURE;
}
// initialize base authorization routines
error = auth::handler::initialize();
if (error)
return core::system::exitFailure(error, ERROR_LOCATION);
// add handlers and initialize 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);
}
}
// give up root privilige if requested
std::string runAsUser = options.serverUser();
if (!runAsUser.empty())
{
// drop root priv
error = core::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)
{
std::cerr << "Verify Installation Failed: " << error << std::endl;
return core::system::exitFailure(error, ERROR_LOCATION);
}
return EXIT_SUCCESS;
}
// catch unhandled exceptions
error = core::crash_handler::initialize();
if (error)
LOG_ERROR(error);
// call overlay startup
error = overlay::startup();
if (error)
return core::system::exitFailure(error, ERROR_LOCATION);
// add http server not found handler
s_pHttpServer->setNotFoundHandler(pageNotFoundHandler);
// run http server
error = s_pHttpServer->run(options.wwwThreadPoolSize());
if (error)
return core::system::exitFailure(error, ERROR_LOCATION);
// wait for signals
error = waitForSignals();
if (error)
return core::system::exitFailure(error, ERROR_LOCATION);
// NOTE: we never get here because waitForSignals waits forever
return EXIT_SUCCESS;
}
CATCH_UNEXPECTED_EXCEPTION
// if we got this far we had an unexpected exception
return EXIT_FAILURE;
}