1
0
Fork 0
arangodb/arangod/RestServer/arangod.cpp

118 lines
3.5 KiB
C++

////////////////////////////////////////////////////////////////////////////////
/// DISCLAIMER
///
/// Copyright 2014-2016 ArangoDB GmbH, Cologne, Germany
/// Copyright 2004-2014 triAGENS GmbH, Cologne, Germany
///
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///
/// http://www.apache.org/licenses/LICENSE-2.0
///
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
///
/// Copyright holder is ArangoDB GmbH, Cologne, Germany
///
/// @author Dr. Frank Celler
////////////////////////////////////////////////////////////////////////////////
#include "Basics/Common.h"
#include "Basics/ArangoGlobalContext.h"
#include "RestServer/ArangoServer.h"
#include <signal.h>
#ifdef ARANGODB_ENABLE_MAINTAINER_MODE
#include <iostream>
#endif
using namespace arangodb;
using namespace arangodb::rest;
////////////////////////////////////////////////////////////////////////////////
/// @brief ArangoDB server
////////////////////////////////////////////////////////////////////////////////
ArangoServer* ArangoInstance = nullptr;
////////////////////////////////////////////////////////////////////////////////
/// @brief Hooks for OS-Specific functions
////////////////////////////////////////////////////////////////////////////////
#ifdef _WIN32
extern bool TRI_ParseMoreArgs(int argc, char* argv[]);
extern void TRI_StartService(int argc, char* argv[]);
#else
bool TRI_ParseMoreArgs(int argc, char* argv[]) { return false; }
void TRI_StartService(int argc, char* argv[]) {}
#endif
////////////////////////////////////////////////////////////////////////////////
/// @brief handle fatal SIGNALs; print backtrace,
/// and rethrow signal for coredumps.
////////////////////////////////////////////////////////////////////////////////
static void AbortHandler(int signum) {
TRI_PrintBacktrace();
#ifdef _WIN32
exit(255 + signum);
#else
signal(signum, SIG_DFL);
kill(getpid(), signum);
#endif
}
////////////////////////////////////////////////////////////////////////////////
/// @brief creates an application server
////////////////////////////////////////////////////////////////////////////////
int main(int argc, char* argv[]) {
ArangoGlobalContext context(argc, argv);
int res = EXIT_SUCCESS;
// Note: NEVER start threads or create global objects in here. The server
// might enter enter a daemon mode, in which it leave the main function
// in the parent and only a forked child is running.
//
// Any startup handling MUST be done inside "startupServer".
signal(SIGSEGV, AbortHandler);
// windows only
bool const startAsService = TRI_ParseMoreArgs(argc, argv);
// initialize sub-systems
if (startAsService) {
TRI_StartService(argc, argv);
} else {
ArangoInstance = new ArangoServer(argc, argv);
res = ArangoInstance->start();
}
if (ArangoInstance != nullptr) {
try {
delete ArangoInstance;
} catch (...) {
// caught an error during shutdown
res = EXIT_FAILURE;
#ifdef ARANGODB_ENABLE_MAINTAINER_MODE
std::cerr << "Caught an exception during shutdown" << std::endl;
#endif
}
ArangoInstance = nullptr;
}
// shutdown sub-systems
return context.exit(res);
}