int main(int argc, char * const argv[]) { try { // initialize log initializeSystemLog("rpostback", core::system::kLogLevelWarning); // ignore SIGPIPE Error error = core::system::ignoreSignal(core::system::SigPipe); if (error) LOG_ERROR(error); // read program options Options& options = session::postback::options(); ProgramStatus status = options.read(argc, argv); if ( status.exit() ) return status.exitCode() ; // determine postback uri std::string uri = std::string(kLocalUriLocationPrefix kPostbackUriScope) + options.command(); // build postback request http::Request request; request.setMethod("POST"); request.setUri(uri); request.setHeader("Accept", "*/*"); request.setHeader("Connection", "close"); request.setBody(options.argument()); // send it http::Response response; error = sendRequest(&request, &response); if (error) return exitFailure(error); std::string exitCode = response.headerValue(kPostbackExitCodeHeader); std::cout << response.body(); return safe_convert::stringTo<int>(exitCode, EXIT_FAILURE); } CATCH_UNEXPECTED_EXCEPTION // if we got this far we had an unexpected exception return EXIT_FAILURE ; }
int main(int argc, char * const argv[]) { try { // initialize log initializeSystemLog("rpostback", core::system::kLogLevelWarning); // read program options Options& options = session::postback::options(); ProgramStatus status = options.read(argc, argv); if ( status.exit() ) return status.exitCode() ; // determine postback uri std::string uri = std::string(kLocalUriLocationPrefix kPostbackUriScope) + options.command(); // determine stream path std::string userIdentity = core::system::getenv(kRStudioUserIdentity); FilePath streamPath = session::local_streams::streamPath(userIdentity); // build postback request http::Request request; request.setMethod("POST"); request.setUri(uri); request.setHeader("Accept", "*/*"); request.setHeader("Connection", "close"); request.setBody(options.argument()); // send it http::Response response; Error error = http::sendRequest(streamPath, request, &response); if (error) return exitFailure(error); else return EXIT_SUCCESS; } CATCH_UNEXPECTED_EXCEPTION // if we got this far we had an unexpected exception return EXIT_FAILURE ; }
int main(int argc, char** argv) { try { // initialize log initializeSystemLog("rsinverse", core::system::kLogLevelWarning); // ignore SIGPIPE Error error = core::system::ignoreSignal(core::system::SigPipe); if (error) LOG_ERROR(error); // read options using namespace boost::program_options ; options_description rsinverseOptions("rsinverse"); unsigned int windowHandle; std::string port, sharedSecret, sourceFile; int line; rsinverseOptions.add_options() ("hwnd", value<unsigned int>(&windowHandle), "hwnd of rstudio instance") ("port", value<std::string>(&port), "port of rstudio instance") ("secret", value<std::string>(&sharedSecret), "rstudio shared secret") ("source-file", value<std::string>(&sourceFile), "source file to navigate to") ("line", value<int>(&line), "line of code to navigate to"); // define program options (allow positional specification) core::program_options::OptionsDescription optDesc("rsinverse"); optDesc.commandLine.add(rsinverseOptions); optDesc.positionalOptions.add("hwnd", 1); optDesc.positionalOptions.add("port", 1); optDesc.positionalOptions.add("secret", 1); optDesc.positionalOptions.add("source-file", 1); optDesc.positionalOptions.add("line", 1); // read options ProgramStatus status = core::program_options::read(optDesc, argc, argv); if (status.exit()) return status.exitCode(); // activate the window HWND hRStudioWnd = reinterpret_cast<HWND>(windowHandle); if (::IsWindow(hRStudioWnd)) { HWND hwndPopup = ::GetLastActivePopup(hRStudioWnd); if (::IsWindow(hwndPopup)) hRStudioWnd = hwndPopup; ::SetForegroundWindow(hRStudioWnd); if (::IsIconic(hRStudioWnd)) ::ShowWindow(hRStudioWnd, SW_RESTORE); } // we presume that the path is passed to us in the system encoding sourceFile = string_utils::systemToUtf8(sourceFile); // enocde the source file and line as a query string std::string requestBody; core::http::Fields args; args.push_back(std::make_pair("source-file", sourceFile)); args.push_back(std::make_pair("line", safe_convert::numberToString(line))); http::util::buildQueryString(args, &requestBody); // determine postback uri std::string uri = std::string(kLocalUriLocationPrefix kPostbackUriScope) + "rsinverse"; // build postback request http::Request request; request.setMethod("POST"); request.setUri(uri); request.setHeader("Accept", "*/*"); request.setHeader("X-Shared-Secret", sharedSecret); request.setHeader("Connection", "close"); request.setBody(requestBody); // send it http::Response response; std::string pipeName = core::system::getenv("RS_LOCAL_PEER"); error = http::sendRequest(pipeName, request, http::ConnectionRetryProfile( boost::posix_time::seconds(10), boost::posix_time::milliseconds(50)), &response); std::string exitCode = response.headerValue(kPostbackExitCodeHeader); return safe_convert::stringTo<int>(exitCode, EXIT_FAILURE); } CATCH_UNEXPECTED_EXCEPTION // if we got this far we had an unexpected exception return EXIT_FAILURE ; }
int main(int argc, char * const argv[]) { try { // initialize log initializeSystemLog(kProgramIdentity, core::system::kLogLevelWarning); // ignore SIGPIPE Error error = core::system::ignoreSignal(core::system::SigPipe); if (error) LOG_ERROR(error); // 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 = core::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(core::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 (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 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); // register the monitor log writer (needs to happen after // http server init so the io service is available) core::system::addLogWriter(monitorAsyncLogWriter()); // call overlay initialize error = overlay::initialize(); 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 = 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) return core::system::exitFailure(error, ERROR_LOCATION); return EXIT_SUCCESS; } // call overlay startup error = overlay::startup(); if (error) return core::system::exitFailure(error, ERROR_LOCATION); // 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 ; }
int main(int argc, char * const argv[]) { try { // initialize log const char * const kProgramIdentity = "rserver"; initializeSystemLog(kProgramIdentity, core::system::kLogLevelWarning); // ignore SIGPIPE (don't log error because we should never call // syslog prior to daemonizing) core::system::ignoreSignal(core::system::SigPipe); // 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()) { Error error = core::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) if (options.serverSetUmask()) setUMask(core::system::OthersNoWriteMask); } // wait until now to output options warnings (we need to wait for our // first call to logging functions until after daemonization) if (!optionsWarnings.empty()) program_options::reportWarnings(optionsWarnings, ERROR_LOCATION); // 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 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); // 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(kMonitorSocketPath, server::options().monitorSharedSecret(), s_pHttpServer->ioService()); // add a monitor log writer core::system::addLogWriter( monitor::client().createLogWriter(kProgramIdentity)); // 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) { program_options::reportError(errMsg, ERROR_LOCATION); return EXIT_FAILURE; } // 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(); // log error unless it's path not found (which indicates that // libapparmor isn't installed on this system) if (error && !isPathNotFoundError(error)) LOG_ERROR(error); } // give up root privilige if requested std::string runAsUser = options.serverUser(); if (!runAsUser.empty()) { // drop root priv Error 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) return core::system::exitFailure(error, ERROR_LOCATION); return EXIT_SUCCESS; } // call overlay startup error = overlay::startup(); if (error) return core::system::exitFailure(error, ERROR_LOCATION); // 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 ; }