2020-03-26 17:53:57 +00:00
|
|
|
#include <unistd.h>
|
2024-01-14 23:36:44 +00:00
|
|
|
#include <fcntl.h>
|
2020-03-26 16:58:50 +00:00
|
|
|
|
2019-03-12 19:30:59 +00:00
|
|
|
#include <new>
|
2018-04-05 19:28:07 +00:00
|
|
|
#include <iostream>
|
2018-04-05 14:27:39 +00:00
|
|
|
#include <vector>
|
2018-04-05 19:28:07 +00:00
|
|
|
#include <string>
|
2023-11-12 01:42:51 +00:00
|
|
|
#include <string_view>
|
2018-11-26 00:56:50 +00:00
|
|
|
#include <utility> /// pair
|
2017-11-12 12:58:40 +00:00
|
|
|
|
2022-04-16 23:56:58 +00:00
|
|
|
#include <fmt/format.h>
|
|
|
|
|
2024-06-21 13:13:56 +00:00
|
|
|
#include "config.h"
|
2021-12-11 18:25:23 +00:00
|
|
|
#include "config_tools.h"
|
2018-06-19 18:09:09 +00:00
|
|
|
|
2024-07-03 14:54:09 +00:00
|
|
|
#include <Common/EnvironmentChecks.h>
|
|
|
|
#include <Common/Coverage.h>
|
2024-05-19 08:02:06 +00:00
|
|
|
#include <Common/StringUtils.h>
|
2021-01-07 02:56:57 +00:00
|
|
|
#include <Common/getHashOfLoadedBinary.h>
|
2021-06-02 04:48:31 +00:00
|
|
|
#include <Common/IO.h>
|
2016-10-25 12:14:27 +00:00
|
|
|
|
2021-10-02 07:13:14 +00:00
|
|
|
#include <base/phdr_cache.h>
|
2024-01-14 23:36:44 +00:00
|
|
|
#include <base/coverage.h>
|
2019-07-24 15:26:23 +00:00
|
|
|
|
|
|
|
|
2016-10-31 19:54:49 +00:00
|
|
|
/// Universal executable for various clickhouse applications
|
2016-11-11 17:01:02 +00:00
|
|
|
int mainEntryClickHouseServer(int argc, char ** argv);
|
2017-03-24 15:05:54 +00:00
|
|
|
int mainEntryClickHouseClient(int argc, char ** argv);
|
|
|
|
int mainEntryClickHouseLocal(int argc, char ** argv);
|
|
|
|
int mainEntryClickHouseBenchmark(int argc, char ** argv);
|
|
|
|
int mainEntryClickHouseExtractFromConfig(int argc, char ** argv);
|
2017-09-20 14:12:12 +00:00
|
|
|
int mainEntryClickHouseCompressor(int argc, char ** argv);
|
2017-11-11 01:04:14 +00:00
|
|
|
int mainEntryClickHouseFormat(int argc, char ** argv);
|
2018-06-16 00:27:59 +00:00
|
|
|
int mainEntryClickHouseObfuscator(int argc, char ** argv);
|
2020-09-07 03:22:47 +00:00
|
|
|
int mainEntryClickHouseGitImport(int argc, char ** argv);
|
2024-02-11 19:00:37 +00:00
|
|
|
int mainEntryClickHouseStaticFilesDiskUploader(int argc, char ** argv);
|
|
|
|
int mainEntryClickHouseSU(int argc, char ** argv);
|
|
|
|
int mainEntryClickHouseDisks(int argc, char ** argv);
|
|
|
|
|
|
|
|
int mainEntryClickHouseHashBinary(int, char **)
|
|
|
|
{
|
|
|
|
/// Intentionally without newline. So you can run:
|
|
|
|
/// objcopy --add-section .clickhouse.hash=<(./clickhouse hash-binary) clickhouse
|
|
|
|
std::cout << getHashOfLoadedBinaryHex();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-05-12 10:39:07 +00:00
|
|
|
#if ENABLE_CLICKHOUSE_KEEPER
|
|
|
|
int mainEntryClickHouseKeeper(int argc, char ** argv);
|
|
|
|
#endif
|
2022-04-19 15:44:11 +00:00
|
|
|
#if ENABLE_CLICKHOUSE_KEEPER_CONVERTER
|
2021-06-17 16:32:50 +00:00
|
|
|
int mainEntryClickHouseKeeperConverter(int argc, char ** argv);
|
|
|
|
#endif
|
2023-03-10 02:23:57 +00:00
|
|
|
#if ENABLE_CLICKHOUSE_KEEPER_CLIENT
|
|
|
|
int mainEntryClickHouseKeeperClient(int argc, char ** argv);
|
|
|
|
#endif
|
2024-02-11 19:00:37 +00:00
|
|
|
|
|
|
|
// install
|
2020-08-08 18:38:34 +00:00
|
|
|
int mainEntryClickHouseInstall(int argc, char ** argv);
|
2020-08-08 20:42:10 +00:00
|
|
|
int mainEntryClickHouseStart(int argc, char ** argv);
|
|
|
|
int mainEntryClickHouseStop(int argc, char ** argv);
|
|
|
|
int mainEntryClickHouseStatus(int argc, char ** argv);
|
|
|
|
int mainEntryClickHouseRestart(int argc, char ** argv);
|
2021-01-07 02:56:57 +00:00
|
|
|
|
2017-09-20 13:41:13 +00:00
|
|
|
namespace
|
2016-10-25 12:14:27 +00:00
|
|
|
{
|
2016-10-31 19:54:49 +00:00
|
|
|
|
2017-09-20 13:41:13 +00:00
|
|
|
using MainFunc = int (*)(int, char**);
|
|
|
|
|
|
|
|
/// Add an item here to register new application
|
2023-11-12 01:42:51 +00:00
|
|
|
std::pair<std::string_view, MainFunc> clickhouse_applications[] =
|
2017-09-20 13:41:13 +00:00
|
|
|
{
|
|
|
|
{"local", mainEntryClickHouseLocal},
|
|
|
|
{"client", mainEntryClickHouseClient},
|
|
|
|
{"benchmark", mainEntryClickHouseBenchmark},
|
|
|
|
{"server", mainEntryClickHouseServer},
|
|
|
|
{"extract-from-config", mainEntryClickHouseExtractFromConfig},
|
2017-11-11 01:04:14 +00:00
|
|
|
{"compressor", mainEntryClickHouseCompressor},
|
2017-11-12 12:58:40 +00:00
|
|
|
{"format", mainEntryClickHouseFormat},
|
2018-06-16 00:27:59 +00:00
|
|
|
{"obfuscator", mainEntryClickHouseObfuscator},
|
2020-09-07 03:22:47 +00:00
|
|
|
{"git-import", mainEntryClickHouseGitImport},
|
2024-02-11 19:00:37 +00:00
|
|
|
{"static-files-disk-uploader", mainEntryClickHouseStaticFilesDiskUploader},
|
|
|
|
{"su", mainEntryClickHouseSU},
|
|
|
|
{"hash-binary", mainEntryClickHouseHashBinary},
|
|
|
|
{"disks", mainEntryClickHouseDisks},
|
|
|
|
|
|
|
|
// keeper
|
2021-05-12 10:39:07 +00:00
|
|
|
#if ENABLE_CLICKHOUSE_KEEPER
|
|
|
|
{"keeper", mainEntryClickHouseKeeper},
|
|
|
|
#endif
|
2021-06-17 16:32:50 +00:00
|
|
|
#if ENABLE_CLICKHOUSE_KEEPER_CONVERTER
|
|
|
|
{"keeper-converter", mainEntryClickHouseKeeperConverter},
|
|
|
|
#endif
|
2023-03-10 02:23:57 +00:00
|
|
|
#if ENABLE_CLICKHOUSE_KEEPER_CLIENT
|
|
|
|
{"keeper-client", mainEntryClickHouseKeeperClient},
|
|
|
|
#endif
|
2024-02-11 19:00:37 +00:00
|
|
|
|
|
|
|
// install
|
2020-08-08 18:38:34 +00:00
|
|
|
{"install", mainEntryClickHouseInstall},
|
2020-08-08 20:42:10 +00:00
|
|
|
{"start", mainEntryClickHouseStart},
|
|
|
|
{"stop", mainEntryClickHouseStop},
|
|
|
|
{"status", mainEntryClickHouseStatus},
|
|
|
|
{"restart", mainEntryClickHouseRestart},
|
2017-09-20 13:41:13 +00:00
|
|
|
};
|
|
|
|
|
2017-12-02 02:47:12 +00:00
|
|
|
int printHelp(int, char **)
|
2017-09-20 13:41:13 +00:00
|
|
|
{
|
|
|
|
std::cerr << "Use one of the following commands:" << std::endl;
|
|
|
|
for (auto & application : clickhouse_applications)
|
|
|
|
std::cerr << "clickhouse " << application.first << " [args] " << std::endl;
|
|
|
|
return -1;
|
2018-08-26 01:14:00 +00:00
|
|
|
}
|
2016-10-30 18:02:56 +00:00
|
|
|
|
2024-03-17 15:22:33 +00:00
|
|
|
/// Add an item here to register a new short name
|
|
|
|
std::pair<std::string_view, std::string_view> clickhouse_short_names[] =
|
|
|
|
{
|
|
|
|
{"chl", "local"},
|
|
|
|
{"chc", "client"},
|
|
|
|
};
|
|
|
|
|
2016-10-31 19:54:49 +00:00
|
|
|
}
|
|
|
|
|
2023-11-12 01:42:51 +00:00
|
|
|
bool isClickhouseApp(std::string_view app_suffix, std::vector<char *> & argv)
|
2023-09-01 14:20:50 +00:00
|
|
|
{
|
2023-11-12 01:42:51 +00:00
|
|
|
for (const auto & [alias, name] : clickhouse_short_names)
|
|
|
|
if (app_suffix == name
|
|
|
|
&& !argv.empty() && (alias == argv[0] || endsWith(argv[0], "/" + std::string(alias))))
|
|
|
|
return true;
|
|
|
|
|
2023-09-01 14:20:50 +00:00
|
|
|
/// 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;
|
|
|
|
|
|
|
|
/// 'clickhouse --client ...' and 'clickhouse client ...' are Ok
|
2023-11-12 01:42:51 +00:00
|
|
|
if (*first_arg == app_suffix
|
|
|
|
|| (std::string_view(*first_arg).starts_with("--") && std::string_view(*first_arg).substr(2) == app_suffix))
|
2023-09-01 14:20:50 +00:00
|
|
|
{
|
|
|
|
argv.erase(first_arg);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Use app if clickhouse binary is run through symbolic link with name clickhouse-app
|
2023-11-12 01:42:51 +00:00
|
|
|
std::string app_name = "clickhouse-" + std::string(app_suffix);
|
2023-09-01 14:20:50 +00:00
|
|
|
return !argv.empty() && (app_name == argv[0] || endsWith(argv[0], "/" + app_name));
|
|
|
|
}
|
2016-10-31 19:54:49 +00:00
|
|
|
|
2022-09-17 01:55:39 +00:00
|
|
|
/// Don't allow dlopen in the main ClickHouse 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.
|
|
|
|
|
2022-10-01 14:29:41 +00:00
|
|
|
#if !defined(USE_MUSL)
|
2022-09-17 01:55:39 +00:00
|
|
|
extern "C"
|
|
|
|
{
|
|
|
|
void * dlopen(const char *, int)
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2022-09-17 16:40:22 +00:00
|
|
|
void * dlmopen(long, const char *, int) // NOLINT
|
2022-09-17 01:55:39 +00:00
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
int dlclose(void *)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char * dlerror()
|
|
|
|
{
|
|
|
|
return "ClickHouse does not allow dynamic library loading";
|
|
|
|
}
|
|
|
|
}
|
2022-10-01 14:29:41 +00:00
|
|
|
#endif
|
2022-09-17 01:55:39 +00:00
|
|
|
|
2024-06-21 13:13:56 +00:00
|
|
|
/// Prevent messages from JeMalloc in the release build.
|
|
|
|
/// Some of these messages are non-actionable for the users, such as:
|
|
|
|
/// <jemalloc>: Number of CPUs detected is not deterministic. Per-CPU arena disabled.
|
|
|
|
#if USE_JEMALLOC && defined(NDEBUG) && !defined(SANITIZER)
|
2024-06-21 14:00:20 +00:00
|
|
|
extern "C" void (*malloc_message)(void *, const char *s);
|
2024-06-21 13:13:56 +00:00
|
|
|
__attribute__((constructor(0))) void init_je_malloc_message() { malloc_message = [](void *, const char *){}; }
|
|
|
|
#endif
|
|
|
|
|
2019-08-28 17:13:29 +00:00
|
|
|
/// 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;
|
|
|
|
|
2016-10-31 19:54:49 +00:00
|
|
|
int main(int argc_, char ** argv_)
|
|
|
|
{
|
2019-08-28 17:13:29 +00:00
|
|
|
inside_main = true;
|
|
|
|
SCOPE_EXIT({ inside_main = false; });
|
|
|
|
|
2022-05-11 01:16:10 +00:00
|
|
|
/// 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.
|
2022-09-17 01:02:34 +00:00
|
|
|
/// Note: we forbid dlopen in our code.
|
2022-05-11 01:16:10 +00:00
|
|
|
updatePHDRCache();
|
|
|
|
|
2023-01-03 18:35:24 +00:00
|
|
|
#if !defined(USE_MUSL)
|
2022-05-14 07:42:32 +00:00
|
|
|
checkHarmfulEnvironmentVariables(argv_);
|
2022-07-31 08:51:17 +00:00
|
|
|
#endif
|
2022-04-16 23:56:58 +00:00
|
|
|
|
2023-07-16 22:06:58 +00:00
|
|
|
/// This is used for testing. For example,
|
|
|
|
/// clickhouse-local should be able to run a simple query without throw/catch.
|
|
|
|
if (getenv("CLICKHOUSE_TERMINATE_ON_ANY_EXCEPTION")) // NOLINT(concurrency-mt-unsafe)
|
|
|
|
DB::terminate_on_any_exception = true;
|
|
|
|
|
2019-03-12 19:30:59 +00:00
|
|
|
/// Reset new handler to default (that throws std::bad_alloc)
|
|
|
|
/// It is needed because LLVM library clobbers it.
|
|
|
|
std::set_new_handler(nullptr);
|
|
|
|
|
2016-10-31 19:54:49 +00:00
|
|
|
std::vector<char *> argv(argv_, argv_ + argc_);
|
|
|
|
|
2017-09-20 13:41:13 +00:00
|
|
|
/// Print a basic help if nothing was matched
|
|
|
|
MainFunc main_func = printHelp;
|
|
|
|
|
|
|
|
for (auto & application : clickhouse_applications)
|
|
|
|
{
|
|
|
|
if (isClickhouseApp(application.first, argv))
|
|
|
|
{
|
|
|
|
main_func = application.second;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-10-28 17:38:32 +00:00
|
|
|
|
2023-12-29 11:33:27 +00:00
|
|
|
/// Interpret binary without argument or with arguments starts with dash
|
|
|
|
/// ('-') as clickhouse-local for better usability:
|
|
|
|
///
|
2024-04-09 13:36:34 +00:00
|
|
|
/// clickhouse help # dumps help
|
2023-12-29 11:33:27 +00:00
|
|
|
/// clickhouse -q 'select 1' # use local
|
|
|
|
/// clickhouse # spawn local
|
|
|
|
/// clickhouse local # spawn local
|
2024-04-28 02:39:44 +00:00
|
|
|
/// clickhouse "select ..." # spawn local
|
2023-12-29 11:33:27 +00:00
|
|
|
///
|
2024-04-28 02:39:44 +00:00
|
|
|
if (main_func == printHelp && !argv.empty() && (argv.size() == 1 || argv[1][0] == '-'
|
2024-04-30 02:21:44 +00:00
|
|
|
|| std::string_view(argv[1]).contains(' ')))
|
2024-04-28 02:39:44 +00:00
|
|
|
{
|
2023-12-29 11:33:27 +00:00
|
|
|
main_func = mainEntryClickHouseLocal;
|
2024-04-28 02:39:44 +00:00
|
|
|
}
|
2023-12-29 11:33:27 +00:00
|
|
|
|
2024-01-14 23:36:44 +00:00
|
|
|
int exit_code = main_func(static_cast<int>(argv.size()), argv.data());
|
|
|
|
|
|
|
|
#if defined(SANITIZE_COVERAGE)
|
2024-01-16 08:44:52 +00:00
|
|
|
dumpCoverage();
|
2024-01-14 23:36:44 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return exit_code;
|
2016-10-25 12:14:27 +00:00
|
|
|
}
|