#include #include #include #include #include #include #include /// pair #include #include "config.h" #include "config_tools.h" #include #include #include #include #include #include #include #include #include int mainEntryDatastoreKeeper(int argc, char ** argv); #if ENABLE_DATASTORE_KEEPER_CONVERTER int mainEntryDatastoreKeeperConverter(int argc, char ** argv); #endif #if ENABLE_DATASTORE_KEEPER_CLIENT int mainEntryDatastoreKeeperClient(int argc, char ** argv); #endif namespace { using MainFunc = int (*)(int, char**); /// Add an item here to register new application std::pair datastore_applications[] = { // keeper {"keeper", mainEntryDatastoreKeeper}, #if ENABLE_DATASTORE_KEEPER_CONVERTER {"converter", mainEntryDatastoreKeeperConverter}, {"keeper-converter", mainEntryDatastoreKeeperConverter}, #endif #if ENABLE_DATASTORE_KEEPER_CLIENT {"client", mainEntryDatastoreKeeperClient}, {"keeper-client", mainEntryDatastoreKeeperClient}, #endif }; int printHelp(int, char **) { std::cerr << "Use one of the following commands:" << std::endl; for (auto & application : datastore_applications) std::cerr << "datastore " << application.first << " [args] " << std::endl; return -1; } } static bool isDatastoreApp(std::string_view app_suffix, std::vector & argv) { /// Use app if the first arg 'app' is passed (the arg should be quietly removed) if (argv.size() >= 2) { auto first_arg = argv.begin() + 1; /// 'datastore --client ...' and 'datastore client ...' are Ok if (*first_arg == app_suffix || (std::string_view(*first_arg).starts_with("--") && std::string_view(*first_arg).substr(2) == app_suffix)) { argv.erase(first_arg); return true; } } /// keeper suffix is default which will be used if no other app is detected if (app_suffix == "keeper") return false; /// Use app if datastore binary is run through symbolic link with name datastore-app std::string app_name = "datastore-" + std::string(app_suffix); return !argv.empty() && (app_name == argv[0] || endsWith(argv[0], "/" + app_name)); } /// Don't allow dlopen in the main Datastore binary, because it is harmful and insecure. /// We don't use it. But it can be used by some libraries for implementation of "plugins". /// We absolutely discourage the ancient technique of loading /// 3rd-party uncontrolled dangerous libraries into the process address space, /// because it is insane. #if !defined(USE_MUSL) extern "C" { void * dlopen(const char *, int); void * dlmopen(long, const char *, int); // NOLINT int dlclose(void *); const char * dlerror(); void * dlopen(const char *, int) { return nullptr; } void * dlmopen(long, const char *, int) // NOLINT { return nullptr; } int dlclose(void *) { return 0; } const char * dlerror() { return "Datastore does not allow dynamic library loading"; } } #endif /// Prevent messages from JeMalloc in the release build. /// Some of these messages are non-actionable for the users, such as: /// : Number of CPUs detected is not deterministic. Per-CPU arena disabled. #if USE_JEMALLOC && defined(NDEBUG) && !defined(SANITIZER) extern "C" void (*je_malloc_message)(void *, const char *s); static __attribute__((constructor(0))) void init_je_malloc_message() { je_malloc_message = [](void *, const char *){}; } #elif USE_JEMALLOC #include /// Ignore messages which can be safely ignored, e.g. EAGAIN on pthread_create extern "C" void (*je_malloc_message)(void *, const char * s); static __attribute__((constructor(0))) void init_je_malloc_message() { je_malloc_message = [](void *, const char * str) { using namespace std::literals; static const std::unordered_set ignore_messages{ ": background thread creation failed (11)\n"sv}; std::string_view message_view{str}; if (ignore_messages.contains(message_view)) return; # if defined(SYS_write) syscall(SYS_write, 2 /*stderr*/, message_view.data(), message_view.size()); # else write(STDERR_FILENO, message_view.data(), message_view.size()); # endif }; } #endif /// OpenSSL early initialization. /// See also EnvironmentChecks.cpp for other static initializers. /// Must be ran after EnvironmentChecks.cpp, as OpenSSL uses SSE4.1 and POPCNT. static __attribute__((constructor(202))) void init_ssl() { DB::OpenSSLInitializer::instance(); } /// This allows to implement assert to forbid initialization of a class in static constructors. /// Usage: /// /// extern bool inside_main; /// class C { C() { assert(inside_main); } }; bool inside_main = false; int main(int argc_, char ** argv_) { inside_main = true; SCOPE_EXIT({ inside_main = false; }); /// PHDR cache is required for query profiler to work reliably /// It also speed up exception handling, but exceptions from dynamically loaded libraries (dlopen) /// will work only after additional call of this function. /// Note: we forbid dlopen in our code. updatePHDRCache(); #if !defined(USE_MUSL) checkHarmfulEnvironmentVariables(argv_); #endif /// This is used for testing. For example, /// datastore-local should be able to run a simple query without throw/catch. if (getenv("DATASTORE_TERMINATE_ON_ANY_EXCEPTION")) // NOLINT(concurrency-mt-unsafe) DB::terminate_on_any_exception = true; /// Reset new handler to default (that throws std::bad_alloc) /// It is needed because LLVM library clobbers it. std::set_new_handler(nullptr); std::vector argv(argv_, argv_ + argc_); /// Print a basic help if nothing was matched MainFunc main_func = mainEntryDatastoreKeeper; if (isDatastoreApp("help", argv)) { main_func = printHelp; } else { for (auto & application : datastore_applications) { if (isDatastoreApp(application.first, argv)) { main_func = application.second; break; } } } int exit_code = main_func(static_cast(argv.size()), argv.data()); return exit_code; }