2022-05-08 17:01:47 +00:00
|
|
|
#include <csignal>
|
|
|
|
#include <csetjmp>
|
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>
|
2020-10-04 08:24:36 +00:00
|
|
|
#include <tuple>
|
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>
|
|
|
|
|
2021-12-11 18:25:23 +00:00
|
|
|
#include "config_tools.h"
|
2018-06-19 18:09:09 +00:00
|
|
|
|
2018-01-15 19:07:47 +00:00
|
|
|
#include <Common/StringUtils/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"},
|
|
|
|
};
|
|
|
|
|
2020-03-26 10:33:53 +00:00
|
|
|
|
|
|
|
enum class InstructionFail
|
|
|
|
{
|
|
|
|
NONE = 0,
|
|
|
|
SSE3 = 1,
|
|
|
|
SSSE3 = 2,
|
|
|
|
SSE4_1 = 3,
|
|
|
|
SSE4_2 = 4,
|
2020-05-24 16:39:31 +00:00
|
|
|
POPCNT = 5,
|
|
|
|
AVX = 6,
|
|
|
|
AVX2 = 7,
|
|
|
|
AVX512 = 8
|
2020-03-26 10:33:53 +00:00
|
|
|
};
|
|
|
|
|
2021-06-02 04:48:31 +00:00
|
|
|
auto instructionFailToString(InstructionFail fail)
|
2020-03-26 10:33:53 +00:00
|
|
|
{
|
|
|
|
switch (fail)
|
|
|
|
{
|
2022-04-16 22:19:36 +00:00
|
|
|
#define ret(x) return std::make_tuple(STDERR_FILENO, x, sizeof(x) - 1)
|
2020-03-26 10:33:53 +00:00
|
|
|
case InstructionFail::NONE:
|
2020-10-04 08:24:36 +00:00
|
|
|
ret("NONE");
|
2020-03-26 10:33:53 +00:00
|
|
|
case InstructionFail::SSE3:
|
2020-10-04 08:24:36 +00:00
|
|
|
ret("SSE3");
|
2020-03-26 10:33:53 +00:00
|
|
|
case InstructionFail::SSSE3:
|
2020-10-04 08:24:36 +00:00
|
|
|
ret("SSSE3");
|
2020-03-26 10:33:53 +00:00
|
|
|
case InstructionFail::SSE4_1:
|
2020-10-04 08:24:36 +00:00
|
|
|
ret("SSE4.1");
|
2020-03-26 10:33:53 +00:00
|
|
|
case InstructionFail::SSE4_2:
|
2020-10-04 08:24:36 +00:00
|
|
|
ret("SSE4.2");
|
2020-05-24 16:39:31 +00:00
|
|
|
case InstructionFail::POPCNT:
|
2020-10-04 08:24:36 +00:00
|
|
|
ret("POPCNT");
|
2020-03-26 10:33:53 +00:00
|
|
|
case InstructionFail::AVX:
|
2020-10-04 08:24:36 +00:00
|
|
|
ret("AVX");
|
2020-03-26 10:33:53 +00:00
|
|
|
case InstructionFail::AVX2:
|
2020-10-04 08:24:36 +00:00
|
|
|
ret("AVX2");
|
2020-03-26 10:33:53 +00:00
|
|
|
case InstructionFail::AVX512:
|
2020-10-04 08:24:36 +00:00
|
|
|
ret("AVX512");
|
2020-03-26 10:33:53 +00:00
|
|
|
}
|
2022-10-07 19:20:14 +00:00
|
|
|
UNREACHABLE();
|
2020-03-26 10:33:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sigjmp_buf jmpbuf;
|
|
|
|
|
2020-03-26 16:58:50 +00:00
|
|
|
[[noreturn]] void sigIllCheckHandler(int, siginfo_t *, void *)
|
2020-03-26 10:33:53 +00:00
|
|
|
{
|
|
|
|
siglongjmp(jmpbuf, 1);
|
|
|
|
}
|
|
|
|
|
2020-03-27 08:14:20 +00:00
|
|
|
/// Check if necessary SSE extensions are available by trying to execute some sse instructions.
|
2020-03-26 10:33:53 +00:00
|
|
|
/// If instruction is unavailable, SIGILL will be sent by kernel.
|
|
|
|
void checkRequiredInstructionsImpl(volatile InstructionFail & fail)
|
|
|
|
{
|
2020-03-26 16:58:50 +00:00
|
|
|
#if defined(__SSE3__)
|
2020-03-26 10:33:53 +00:00
|
|
|
fail = InstructionFail::SSE3;
|
|
|
|
__asm__ volatile ("addsubpd %%xmm0, %%xmm0" : : : "xmm0");
|
|
|
|
#endif
|
|
|
|
|
2020-03-26 16:58:50 +00:00
|
|
|
#if defined(__SSSE3__)
|
2020-03-26 10:33:53 +00:00
|
|
|
fail = InstructionFail::SSSE3;
|
|
|
|
__asm__ volatile ("pabsw %%xmm0, %%xmm0" : : : "xmm0");
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2020-03-26 16:58:50 +00:00
|
|
|
#if defined(__SSE4_1__)
|
2020-03-26 10:33:53 +00:00
|
|
|
fail = InstructionFail::SSE4_1;
|
|
|
|
__asm__ volatile ("pmaxud %%xmm0, %%xmm0" : : : "xmm0");
|
|
|
|
#endif
|
|
|
|
|
2020-03-26 16:58:50 +00:00
|
|
|
#if defined(__SSE4_2__)
|
2020-03-26 10:33:53 +00:00
|
|
|
fail = InstructionFail::SSE4_2;
|
|
|
|
__asm__ volatile ("pcmpgtq %%xmm0, %%xmm0" : : : "xmm0");
|
|
|
|
#endif
|
|
|
|
|
2020-05-24 16:39:31 +00:00
|
|
|
/// Defined by -msse4.2
|
|
|
|
#if defined(__POPCNT__)
|
|
|
|
fail = InstructionFail::POPCNT;
|
|
|
|
{
|
|
|
|
uint64_t a = 0;
|
|
|
|
uint64_t b = 0;
|
2020-05-27 17:23:09 +00:00
|
|
|
__asm__ volatile ("popcnt %1, %0" : "=r"(a) :"r"(b) :);
|
2020-05-24 16:39:31 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-03-26 16:58:50 +00:00
|
|
|
#if defined(__AVX__)
|
2020-03-26 10:33:53 +00:00
|
|
|
fail = InstructionFail::AVX;
|
|
|
|
__asm__ volatile ("vaddpd %%ymm0, %%ymm0, %%ymm0" : : : "ymm0");
|
|
|
|
#endif
|
|
|
|
|
2020-03-26 16:58:50 +00:00
|
|
|
#if defined(__AVX2__)
|
2020-03-26 10:33:53 +00:00
|
|
|
fail = InstructionFail::AVX2;
|
|
|
|
__asm__ volatile ("vpabsw %%ymm0, %%ymm0" : : : "ymm0");
|
|
|
|
#endif
|
|
|
|
|
2020-03-26 16:58:50 +00:00
|
|
|
#if defined(__AVX512__)
|
2020-03-26 10:33:53 +00:00
|
|
|
fail = InstructionFail::AVX512;
|
|
|
|
__asm__ volatile ("vpabsw %%zmm0, %%zmm0" : : : "zmm0");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
fail = InstructionFail::NONE;
|
|
|
|
}
|
|
|
|
|
2020-10-04 08:24:36 +00:00
|
|
|
/// Macros to avoid using strlen(), since it may fail if SSE is not supported.
|
2020-10-05 06:28:52 +00:00
|
|
|
#define writeError(data) do \
|
|
|
|
{ \
|
|
|
|
static_assert(__builtin_constant_p(data)); \
|
2022-04-16 22:19:36 +00:00
|
|
|
if (!writeRetry(STDERR_FILENO, data, sizeof(data) - 1)) \
|
2021-06-02 04:48:31 +00:00
|
|
|
_Exit(1); \
|
2020-10-05 06:28:52 +00:00
|
|
|
} while (false)
|
2020-03-26 17:53:57 +00:00
|
|
|
|
|
|
|
/// Check SSE and others instructions availability. Calls exit on fail.
|
|
|
|
/// This function must be called as early as possible, even before main, because static initializers may use unavailable instructions.
|
2020-03-26 10:33:53 +00:00
|
|
|
void checkRequiredInstructions()
|
|
|
|
{
|
|
|
|
struct sigaction sa{};
|
|
|
|
struct sigaction sa_old{};
|
|
|
|
sa.sa_sigaction = sigIllCheckHandler;
|
|
|
|
sa.sa_flags = SA_SIGINFO;
|
|
|
|
auto signal = SIGILL;
|
|
|
|
if (sigemptyset(&sa.sa_mask) != 0
|
|
|
|
|| sigaddset(&sa.sa_mask, signal) != 0
|
|
|
|
|| sigaction(signal, &sa, &sa_old) != 0)
|
|
|
|
{
|
2020-03-26 17:53:57 +00:00
|
|
|
/// You may wonder about strlen.
|
|
|
|
/// Typical implementation of strlen is using SSE4.2 or AVX2.
|
|
|
|
/// But this is not the case because it's compiler builtin and is executed at compile time.
|
|
|
|
|
2020-10-03 22:38:16 +00:00
|
|
|
writeError("Can not set signal handler\n");
|
2020-03-26 17:53:57 +00:00
|
|
|
_Exit(1);
|
2020-03-26 10:33:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
volatile InstructionFail fail = InstructionFail::NONE;
|
|
|
|
|
|
|
|
if (sigsetjmp(jmpbuf, 1))
|
|
|
|
{
|
2020-10-03 22:38:16 +00:00
|
|
|
writeError("Instruction check fail. The CPU does not support ");
|
2021-06-02 04:48:31 +00:00
|
|
|
if (!std::apply(writeRetry, instructionFailToString(fail)))
|
|
|
|
_Exit(1);
|
2020-10-03 22:38:16 +00:00
|
|
|
writeError(" instruction set.\n");
|
2020-03-26 17:53:57 +00:00
|
|
|
_Exit(1);
|
2020-03-26 10:33:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
checkRequiredInstructionsImpl(fail);
|
|
|
|
|
|
|
|
if (sigaction(signal, &sa_old, nullptr))
|
|
|
|
{
|
2020-10-03 22:38:16 +00:00
|
|
|
writeError("Can not set signal handler\n");
|
2020-03-26 17:53:57 +00:00
|
|
|
_Exit(1);
|
2020-03-26 10:33:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-03 22:38:16 +00:00
|
|
|
struct Checker
|
|
|
|
{
|
|
|
|
Checker()
|
|
|
|
{
|
2020-10-03 23:01:40 +00:00
|
|
|
checkRequiredInstructions();
|
2020-10-03 22:38:16 +00:00
|
|
|
}
|
2021-11-25 23:03:04 +00:00
|
|
|
} checker
|
2022-06-10 08:22:31 +00:00
|
|
|
#ifndef OS_DARWIN
|
2021-11-25 23:03:04 +00:00
|
|
|
__attribute__((init_priority(101))) /// Run before other static initializers.
|
|
|
|
#endif
|
|
|
|
;
|
2020-03-26 17:53:57 +00:00
|
|
|
|
2022-04-16 22:09:20 +00:00
|
|
|
|
2024-03-18 01:16:52 +00:00
|
|
|
#if !defined(USE_MUSL)
|
2022-04-18 05:00:18 +00:00
|
|
|
/// NOTE: We will migrate to full static linking or our own dynamic loader to make this code obsolete.
|
2022-05-14 07:42:32 +00:00
|
|
|
void checkHarmfulEnvironmentVariables(char ** argv)
|
2022-04-16 23:56:58 +00:00
|
|
|
{
|
2022-04-18 05:00:18 +00:00
|
|
|
std::initializer_list<const char *> harmful_env_variables = {
|
|
|
|
/// The list is a selection from "man ld-linux".
|
|
|
|
"LD_PRELOAD",
|
|
|
|
"LD_LIBRARY_PATH",
|
|
|
|
"LD_ORIGIN_PATH",
|
|
|
|
"LD_AUDIT",
|
|
|
|
"LD_DYNAMIC_WEAK",
|
|
|
|
/// The list is a selection from "man dyld" (osx).
|
|
|
|
"DYLD_LIBRARY_PATH",
|
|
|
|
"DYLD_FALLBACK_LIBRARY_PATH",
|
|
|
|
"DYLD_VERSIONED_LIBRARY_PATH",
|
|
|
|
"DYLD_INSERT_LIBRARIES",
|
|
|
|
};
|
|
|
|
|
2022-05-14 07:42:32 +00:00
|
|
|
bool require_reexec = false;
|
2022-04-18 05:00:18 +00:00
|
|
|
for (const auto * var : harmful_env_variables)
|
2022-04-16 23:56:58 +00:00
|
|
|
{
|
2022-08-21 18:24:17 +00:00
|
|
|
if (const char * value = getenv(var); value && value[0]) // NOLINT(concurrency-mt-unsafe)
|
2022-04-16 23:56:58 +00:00
|
|
|
{
|
2022-05-14 07:42:32 +00:00
|
|
|
/// NOTE: setenv() is used over unsetenv() since unsetenv() marked as harmful
|
2022-08-21 18:24:17 +00:00
|
|
|
if (setenv(var, "", true)) // NOLINT(concurrency-mt-unsafe) // this is safe if not called concurrently
|
2022-05-14 07:42:32 +00:00
|
|
|
{
|
|
|
|
fmt::print(stderr, "Cannot override {} environment variable", var);
|
|
|
|
_exit(1);
|
|
|
|
}
|
|
|
|
require_reexec = true;
|
2022-04-16 23:56:58 +00:00
|
|
|
}
|
|
|
|
}
|
2022-05-14 07:42:32 +00:00
|
|
|
|
|
|
|
if (require_reexec)
|
|
|
|
{
|
|
|
|
/// Use execvp() over execv() to search in PATH.
|
|
|
|
///
|
|
|
|
/// This should be safe, since:
|
|
|
|
/// - if argv[0] is relative path - it is OK
|
|
|
|
/// - if argv[0] has only basename, the it will search in PATH, like shell will do.
|
|
|
|
///
|
|
|
|
/// Also note, that this (search in PATH) because there is no easy and
|
|
|
|
/// portable way to get absolute path of argv[0].
|
|
|
|
/// - on linux there is /proc/self/exec and AT_EXECFN
|
|
|
|
/// - but on other OSes there is no such thing (especially on OSX).
|
|
|
|
///
|
|
|
|
/// And since static linking will be done someday anyway,
|
|
|
|
/// let's not pollute the code base with special cases.
|
|
|
|
int error = execvp(argv[0], argv);
|
|
|
|
_exit(error);
|
|
|
|
}
|
2022-04-16 23:56:58 +00:00
|
|
|
}
|
2022-07-31 08:51:17 +00:00
|
|
|
#endif
|
2022-04-16 23:56:58 +00:00
|
|
|
|
2024-01-16 08:44:52 +00:00
|
|
|
|
|
|
|
#if defined(SANITIZE_COVERAGE)
|
|
|
|
__attribute__((no_sanitize("coverage")))
|
|
|
|
void dumpCoverage()
|
|
|
|
{
|
|
|
|
/// A user can request to dump the coverage information into files at exit.
|
|
|
|
/// This is useful for non-server applications such as clickhouse-format or clickhouse-client,
|
|
|
|
/// that cannot introspect it with SQL functions at runtime.
|
|
|
|
|
|
|
|
/// The CLICKHOUSE_WRITE_COVERAGE environment variable defines a prefix for a filename 'prefix.pid'
|
|
|
|
/// containing the list of addresses of covered .
|
|
|
|
|
|
|
|
/// The format is even simpler than Clang's "sancov": an array of 64-bit addresses, native byte order, no header.
|
|
|
|
|
|
|
|
if (const char * coverage_filename_prefix = getenv("CLICKHOUSE_WRITE_COVERAGE")) // NOLINT(concurrency-mt-unsafe)
|
|
|
|
{
|
|
|
|
auto dump = [](const std::string & name, auto span)
|
|
|
|
{
|
|
|
|
/// Write only non-zeros.
|
|
|
|
std::vector<uintptr_t> data;
|
|
|
|
data.reserve(span.size());
|
|
|
|
for (auto addr : span)
|
|
|
|
if (addr)
|
|
|
|
data.push_back(addr);
|
|
|
|
|
|
|
|
int fd = ::open(name.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0400);
|
|
|
|
if (-1 == fd)
|
|
|
|
{
|
|
|
|
writeError("Cannot open a file to write the coverage data\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!writeRetry(fd, reinterpret_cast<const char *>(data.data()), data.size() * sizeof(data[0])))
|
|
|
|
writeError("Cannot write the coverage data to a file\n");
|
|
|
|
if (0 != ::close(fd))
|
|
|
|
writeError("Cannot close the file with coverage data\n");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
dump(fmt::format("{}.{}", coverage_filename_prefix, getpid()), getCumulativeCoverage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
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
|
|
|
|
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
|
|
|
|
///
|
|
|
|
if (main_func == printHelp && !argv.empty() && (argv.size() == 1 || argv[1][0] == '-'))
|
|
|
|
main_func = mainEntryClickHouseLocal;
|
|
|
|
|
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
|
|
|
}
|