Branch data Line data Source code
1 : : #include <base/phdr_cache.h>
2 : : #include <base/scope_guard.h>
3 : : #include <base/defines.h>
4 : :
5 : : #include <Common/EnvironmentChecks.h>
6 : : #include <Common/Exception.h>
7 : : #include <Common/StringUtils.h>
8 : : #include <Common/getHashOfLoadedBinary.h>
9 : : #include <Common/Crypto/OpenSSLInitializer.h>
10 : :
11 : :
12 : : #include "config.h"
13 : : #include "config_tools.h"
14 : :
15 : : #include <unistd.h>
16 : :
17 : : #include <filesystem>
18 : : #include <iostream>
19 : : #include <new>
20 : : #include <string>
21 : : #include <string_view>
22 : : #include <utility> /// pair
23 : : #include <vector>
24 : :
25 : : #ifdef SANITIZER
26 : : #pragma clang diagnostic push
27 : : #pragma clang diagnostic ignored "-Wreserved-identifier"
28 : : extern "C" {
29 : : #ifdef ADDRESS_SANITIZER
30 : : const char * __asan_default_options()
31 : : {
32 : : return "halt_on_error=1 abort_on_error=1";
33 : : }
34 : : const char * __lsan_default_options()
35 : : {
36 : : return "max_allocation_size_mb=32768";
37 : : }
38 : : const char * __lsan_default_suppressions()
39 : : {
40 : : /// OpenSSL intentionally does not free all global state at exit.
41 : : /// These are known false positives from OpenSSL provider and EVP initialization.
42 : : return "leak:ossl_provider_new\n"
43 : : "leak:OSSL_PROVIDER_try_load_ex\n"
44 : : "leak:ossl_rand_ctx_new\n"
45 : : "leak:OSSL_LIB_CTX_new\n"
46 : : "leak:ossl_legacy_provider_init\n"
47 : : /// OpenSSL EVP method objects are cached globally and never freed at exit.
48 : : /// Triggered when S3 client initializes HMAC (AWS SDK -> OpenSSL HMAC_Init_ex).
49 : : "leak:evp_md_new\n"
50 : : "leak:construct_evp_method\n"
51 : : "leak:CRYPTO_THREAD_lock_new\n";
52 : : }
53 : : #endif
54 : :
55 : : #ifdef MEMORY_SANITIZER
56 : : const char * __msan_default_options()
57 : : {
58 : : return "abort_on_error=1 poison_in_dtor=1 max_allocation_size_mb=32768";
59 : : }
60 : : #endif
61 : :
62 : : #ifdef THREAD_SANITIZER
63 : : const char * __tsan_default_options()
64 : : {
65 : : return "halt_on_error=1 abort_on_error=1 history_size=7 second_deadlock_stack=1 max_allocation_size_mb=32768";
66 : : }
67 : : #endif
68 : :
69 : : #ifdef UNDEFINED_BEHAVIOR_SANITIZER
70 : : const char * __ubsan_default_options()
71 : : {
72 : : return "print_stacktrace=1 max_allocation_size_mb=32768";
73 : : }
74 : : #endif
75 : : }
76 : : #pragma clang diagnostic pop
77 : : #endif
78 : :
79 : : /// Universal executable for various clickhouse applications
80 : : int mainEntryClickHouseBenchmark(int argc, char ** argv);
81 : : int mainEntryClickHouseCheckMarks(int argc, char ** argv);
82 : : int mainEntryClickHouseChecksumForCompressedBlock(int, char **);
83 : : int mainEntryClickHouseClient(int argc, char ** argv);
84 : : int mainEntryClickHouseCompressor(int argc, char ** argv);
85 : : int mainEntryClickHouseDisks(int argc, char ** argv);
86 : : int mainEntryClickHouseExtractFromConfig(int argc, char ** argv);
87 : : int mainEntryClickHouseFormat(int argc, char ** argv);
88 : : int mainEntryClickHouseFstDumpTree(int argc, char ** argv);
89 : : int mainEntryClickHouseGitImport(int argc, char ** argv);
90 : : int mainEntryClickHouseLocal(int argc, char ** argv);
91 : : int mainEntryClickHouseObfuscator(int argc, char ** argv);
92 : : int mainEntryClickHouseSU(int argc, char ** argv);
93 : : int mainEntryClickHouseDockerInit(int argc, char ** argv);
94 : : int mainEntryClickHouseServer(int argc, char ** argv);
95 : : int mainEntryClickHouseStaticFilesDiskUploader(int argc, char ** argv);
96 : : int mainEntryClickHouseZooKeeperDumpTree(int argc, char ** argv);
97 : : int mainEntryClickHouseZooKeeperRemoveByList(int argc, char ** argv);
98 : :
99 : : int mainEntryClickHouseHashBinary(int argc, char ** argv)
100 : 4 : {
101 [ + - ][ + - ]: 4 : if (argc > 1 && (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-h") == 0))
[ # # ]
102 : 4 : {
103 : 4 : std::cout << "Usage: clickhouse hash-binary\n"
104 : 4 : "Prints hash of ClickHouse binary.\n"
105 : 4 : " -h, --help Print this message\n"
106 : 4 : "Result is intentionally without newline. So you can run:\n"
107 : 4 : "objcopy --add-section .clickhouse.hash=<(./clickhouse hash-binary) clickhouse\n\n"
108 : 4 : "Current binary hash: ";
109 : 4 : }
110 : 4 : std::cout << getHashOfLoadedBinaryHex();
111 : 4 : return 0;
112 : 4 : }
113 : :
114 : : #if ENABLE_CLICKHOUSE_KEEPER
115 : : int mainEntryClickHouseKeeper(int argc, char ** argv);
116 : : #endif
117 : : #if ENABLE_CLICKHOUSE_KEEPER_CONVERTER
118 : : int mainEntryClickHouseKeeperConverter(int argc, char ** argv);
119 : : #endif
120 : : #if ENABLE_CLICKHOUSE_KEEPER_CLIENT
121 : : int mainEntryClickHouseKeeperClient(int argc, char ** argv);
122 : : #endif
123 : : #if USE_RAPIDJSON && USE_NURAFT
124 : : int mainEntryClickHouseKeeperBench(int argc, char ** argv);
125 : : #endif
126 : : #if USE_NURAFT
127 : : int mainEntryClickHouseKeeperDataDumper(int argc, char ** argv);
128 : : int mainEntryClickHouseKeeperUtils(int argc, char ** argv);
129 : : #endif
130 : :
131 : : #if USE_CHDIG
132 : : extern "C" int chdig_main(int argc, char ** argv);
133 : : int mainEntryClickHouseChdig(int argc, char ** argv)
134 : 4 : {
135 : 4 : return chdig_main(argc, argv);
136 : 4 : }
137 : : #endif
138 : :
139 : : // install
140 : : int mainEntryClickHouseInstall(int argc, char ** argv);
141 : : int mainEntryClickHouseStart(int argc, char ** argv);
142 : : int mainEntryClickHouseStop(int argc, char ** argv);
143 : : int mainEntryClickHouseStatus(int argc, char ** argv);
144 : : int mainEntryClickHouseRestart(int argc, char ** argv);
145 : :
146 : : namespace
147 : : {
148 : :
149 : : using MainFunc = int (*)(int, char**);
150 : :
151 : : /// Forward declaration, since clickhouse_applications is defined after this function.
152 : : void printHelp(std::ostream & out);
153 : :
154 : : int mainEntryHelp(int, char **)
155 : 28 : {
156 : 28 : printHelp(std::cout);
157 : 28 : return 0;
158 : 28 : }
159 : :
160 : : int printHelpOnError(int, char **)
161 : 12 : {
162 : 12 : printHelp(std::cerr);
163 : 12 : return -1;
164 : 12 : }
165 : :
166 : : /// Add an item here to register new application.
167 : : /// This list has a "priority" - e.g. we need to disambiguate clickhouse --format being
168 : : /// either clickouse-format or clickhouse-{local, client} --format.
169 : : /// Currently we will prefer the latter option.
170 : : std::pair<std::string_view, MainFunc> clickhouse_applications[] =
171 : : {
172 : : {"local", mainEntryClickHouseLocal},
173 : : {"client", mainEntryClickHouseClient},
174 : : #if USE_CHDIG
175 : : {"chdig", mainEntryClickHouseChdig},
176 : : {"dig", mainEntryClickHouseChdig},
177 : : #endif
178 : : {"benchmark", mainEntryClickHouseBenchmark},
179 : : {"server", mainEntryClickHouseServer},
180 : : {"extract-from-config", mainEntryClickHouseExtractFromConfig},
181 : : {"compressor", mainEntryClickHouseCompressor},
182 : : {"format", mainEntryClickHouseFormat},
183 : : {"obfuscator", mainEntryClickHouseObfuscator},
184 : : {"git-import", mainEntryClickHouseGitImport},
185 : : {"static-files-disk-uploader", mainEntryClickHouseStaticFilesDiskUploader},
186 : : {"su", mainEntryClickHouseSU},
187 : : {"docker-init", mainEntryClickHouseDockerInit},
188 : : {"hash-binary", mainEntryClickHouseHashBinary},
189 : : {"disks", mainEntryClickHouseDisks},
190 : : {"check-marks", mainEntryClickHouseCheckMarks},
191 : : {"checksum-for-compressed-block", mainEntryClickHouseChecksumForCompressedBlock},
192 : : {"zookeeper-dump-tree", mainEntryClickHouseZooKeeperDumpTree},
193 : : {"zookeeper-remove-by-list", mainEntryClickHouseZooKeeperRemoveByList},
194 : :
195 : : // keeper
196 : : #if ENABLE_CLICKHOUSE_KEEPER
197 : : {"keeper", mainEntryClickHouseKeeper},
198 : : #endif
199 : : #if ENABLE_CLICKHOUSE_KEEPER_CONVERTER
200 : : {"keeper-converter", mainEntryClickHouseKeeperConverter},
201 : : #endif
202 : : #if ENABLE_CLICKHOUSE_KEEPER_CLIENT
203 : : {"keeper-client", mainEntryClickHouseKeeperClient},
204 : : #endif
205 : : #if USE_RAPIDJSON && USE_NURAFT
206 : : {"keeper-bench", mainEntryClickHouseKeeperBench},
207 : : #endif
208 : : #if USE_NURAFT
209 : : {"keeper-data-dumper", mainEntryClickHouseKeeperDataDumper},
210 : : {"keeper-utils", mainEntryClickHouseKeeperUtils},
211 : : #endif
212 : : // install
213 : : {"install", mainEntryClickHouseInstall},
214 : : {"start", mainEntryClickHouseStart},
215 : : {"stop", mainEntryClickHouseStop},
216 : : {"status", mainEntryClickHouseStatus},
217 : : {"restart", mainEntryClickHouseRestart},
218 : : // help
219 : : {"help", mainEntryHelp},
220 : : };
221 : :
222 : : void printHelp(std::ostream & out)
223 : 40 : {
224 : 40 : out << "Use one of the following commands:" << std::endl;
225 [ + + ]: 40 : for (const auto & application : clickhouse_applications)
226 : 1280 : out << "clickhouse " << application.first << " [args] " << std::endl;
227 : 40 : }
228 : :
229 : : /// Add an item here to register a new short name
230 : : std::pair<std::string_view, std::string_view> clickhouse_short_names[] =
231 : : {
232 : : {"chl", "local"},
233 : : {"chc", "client"},
234 : : #if USE_CHDIG
235 : : {"chdig", "chdig"},
236 : : #endif
237 : : };
238 : :
239 : : }
240 : :
241 : : bool isClickhouseApp(std::string_view app_suffix, std::vector<char *> & argv)
242 : 876765 : {
243 [ + + ]: 876765 : for (const auto & [alias, name] : clickhouse_short_names)
244 [ + + ]: 2630295 : if (app_suffix == name
245 [ + - ][ - + ]: 2630295 : && !argv.empty() && (alias == argv[0] || endsWith(argv[0], "/" + std::string(alias))))
[ - + ]
246 : 0 : return true;
247 : :
248 : : /// Use app if the first arg 'app' is passed (the arg should be quietly removed)
249 [ + + ]: 876765 : if (argv.size() >= 2)
250 : 874407 : {
251 : 874407 : auto first_arg = argv.begin() + 1;
252 : :
253 : : /// 'clickhouse --client ...' and 'clickhouse client ...' are Ok
254 [ + + ]: 874407 : if (*first_arg == app_suffix
255 [ + + ][ + + ]: 874407 : || (std::string_view(*first_arg).starts_with("--") && std::string_view(*first_arg).substr(2) == app_suffix))
256 : 89132 : {
257 : 89132 : argv.erase(first_arg);
258 : 89132 : return true;
259 : 89132 : }
260 : 874407 : }
261 : :
262 : : /// Use app if clickhouse binary is run through symbolic link with name clickhouse-app
263 : 787633 : std::string app_name = "clickhouse-" + std::string(app_suffix);
264 [ + - ][ + + ]: 787633 : return !argv.empty() && (app_name == argv[0] || endsWith(argv[0], "/" + app_name));
[ + + ]
265 : 876765 : }
266 : :
267 : : /// Don't allow dlopen in the main ClickHouse binary, because it is harmful and insecure.
268 : : /// We don't use it. But it can be used by some libraries for implementation of "plugins".
269 : : /// We absolutely discourage the ancient technique of loading
270 : : /// 3rd-party uncontrolled dangerous libraries into the process address space,
271 : : /// because it is insane.
272 : :
273 : : #if !defined(USE_MUSL)
274 : : extern "C"
275 : : {
276 : : void * dlopen(const char *, int)
277 : 4327 : {
278 : 4327 : return nullptr;
279 : 4327 : }
280 : :
281 : : void * dlmopen(long, const char *, int) // NOLINT
282 : 0 : {
283 : 0 : return nullptr;
284 : 0 : }
285 : :
286 : : int dlclose(void *)
287 : 0 : {
288 : 0 : return 0;
289 : 0 : }
290 : :
291 : : const char * dlerror()
292 : 36 : {
293 : 36 : return "ClickHouse does not allow dynamic library loading";
294 : 36 : }
295 : : }
296 : : #endif
297 : :
298 : : /// Prevent messages from JeMalloc in the release build.
299 : : /// Some of these messages are non-actionable for the users, such as:
300 : : /// <jemalloc>: Number of CPUs detected is not deterministic. Per-CPU arena disabled.
301 : : #if USE_JEMALLOC && defined(NDEBUG) && !defined(SANITIZER)
302 : : extern "C" void (*je_malloc_message)(void *, const char *s);
303 : 201275 : __attribute__((constructor(0))) void init_je_malloc_message() { je_malloc_message = [](void *, const char *){}; }
304 : : #elif USE_JEMALLOC
305 : : #include <unordered_set>
306 : : /// Ignore messages which can be safely ignored, e.g. EAGAIN on pthread_create
307 : : extern "C" void (*je_malloc_message)(void *, const char * s);
308 : : __attribute__((constructor(0))) void init_je_malloc_message()
309 : : {
310 : : je_malloc_message = [](void *, const char * str)
311 : : {
312 : : using namespace std::literals;
313 : : static const std::unordered_set<std::string_view> ignore_messages{
314 : : "<jemalloc>: background thread creation failed (11)\n"sv};
315 : :
316 : : std::string_view message_view{str};
317 : : if (ignore_messages.contains(message_view))
318 : : return;
319 : :
320 : : # if defined(SYS_write)
321 : : syscall(SYS_write, 2 /*stderr*/, message_view.data(), message_view.size());
322 : : # else
323 : : write(STDERR_FILENO, message_view.data(), message_view.size());
324 : : # endif
325 : : };
326 : : }
327 : : #endif
328 : :
329 : : /// OpenSSL early initialization.
330 : : /// See also EnvironmentChecks.cpp for other static initializers.
331 : : /// Must be ran after EnvironmentChecks.cpp, as OpenSSL uses SSE4.1 and POPCNT.
332 : : __attribute__((constructor(202))) void init_ssl()
333 : 201275 : {
334 : 201275 : DB::OpenSSLInitializer::instance();
335 : 201275 : }
336 : :
337 : : /// This allows to implement assert to forbid initialization of a class in static constructors.
338 : : /// Usage:
339 : : ///
340 : : /// extern bool inside_main;
341 : : /// class C { C() { assert(inside_main); } };
342 : : bool inside_main = false;
343 : :
344 : : int main(int argc_, char ** argv_)
345 : 201275 : {
346 : 201275 : inside_main = true;
347 : 201275 : SCOPE_EXIT({ inside_main = false; });
348 : :
349 : : /// PHDR cache is required for query profiler to work reliably
350 : : /// It also speed up exception handling, but exceptions from dynamically loaded libraries (dlopen)
351 : : /// will work only after additional call of this function.
352 : : /// Note: we forbid dlopen in our code.
353 : 201275 : updatePHDRCache();
354 : :
355 : 201275 : #if !defined(USE_MUSL)
356 : 201275 : checkHarmfulEnvironmentVariables(argv_);
357 : 201275 : #endif
358 : :
359 : : /// This is used for testing. For example,
360 : : /// clickhouse-local should be able to run a simple query without throw/catch.
361 [ + + ]: 201275 : if (getenv("CLICKHOUSE_TERMINATE_ON_ANY_EXCEPTION")) // NOLINT(concurrency-mt-unsafe)
362 : 36 : DB::terminate_on_any_exception = true;
363 : :
364 : : /// Reset new handler to default (that throws std::bad_alloc)
365 : : /// It is needed because LLVM library clobbers it.
366 : 201275 : std::set_new_handler(nullptr);
367 : :
368 : 201275 : std::vector<char *> argv(argv_, argv_ + argc_);
369 : :
370 : : /// Print a basic help if nothing was matched
371 : 201275 : MainFunc main_func = printHelpOnError;
372 : :
373 [ + + ]: 201275 : for (auto & application : clickhouse_applications)
374 : 876765 : {
375 [ + + ]: 876765 : if (isClickhouseApp(application.first, argv))
376 : 201146 : {
377 : 201146 : main_func = application.second;
378 : 201146 : break;
379 : 201146 : }
380 : 876765 : }
381 : :
382 : : /// Top-level --help / -h / -? (as the sole argument) should show the dispatcher
383 : : /// help listing all subcommands and exit with code 0. Without this carve-out,
384 : : /// `--help` would match the `startsWith(argv[i], "-h")` rule below and be routed
385 : : /// into clickhouse-client, which treats anything starting with "-h" as a --host
386 : : /// specification and fails.
387 [ + + ][ + + ]: 201275 : if (main_func == printHelpOnError && argv.size() == 2)
388 : 77 : {
389 : 77 : std::string_view arg(argv[1]);
390 [ - + ][ + + ]: 77 : if (arg == "--help" || arg == "-h" || arg == "-?")
[ + + ]
391 : 8 : main_func = mainEntryHelp;
392 : 77 : }
393 : :
394 : : /// If host/port arguments are passed to clickhouse/ch shortcuts,
395 : : /// interpret it as clickhouse-client invocation for usability.
396 [ + + ][ + + ]: 201275 : if (main_func == printHelpOnError && argv.size() >= 2)
397 : 117 : {
398 [ + + ]: 290 : for (size_t i = 1, num_args = argv.size(); i < num_args; ++i)
399 : 181 : {
400 [ + + ][ + + ]: 181 : if ((i + 1 < num_args && argv[i] == std::string_view("--host")) || startsWith(argv[i], "--host=")
[ + + ][ - + ]
401 [ + + ][ - + ]: 181 : || (i + 1 < num_args && argv[i] == std::string_view("--port")) || startsWith(argv[i], "--port=")
[ - + ]
402 [ + + ]: 181 : || startsWith(argv[i], "-h"))
403 : 8 : {
404 : 8 : main_func = mainEntryClickHouseClient;
405 : 8 : break;
406 : 8 : }
407 : 181 : }
408 : 117 : }
409 : :
410 : : /// Interpret binary without argument or with arguments starts with dash
411 : : /// ('-') as clickhouse-local for better usability:
412 : : ///
413 : : /// clickhouse help # dumps help
414 : : /// clickhouse -q 'select 1' # use local
415 : : /// clickhouse # spawn local
416 : : /// clickhouse local # spawn local
417 : : /// clickhouse "select ..." # spawn local
418 : : /// clickhouse /tmp/repro --enable-analyzer
419 : : ///
420 : 201275 : std::error_code ec;
421 [ + + ][ + - ]: 201275 : if (main_func == printHelpOnError && !argv.empty()
422 [ + + ][ + - ]: 201275 : && (argv.size() < 2 || argv[1] != std::string_view("--help"))
423 [ + + ][ + + ]: 201275 : && (argv.size() == 1 || argv[1][0] == '-' || std::string_view(argv[1]).contains(' ')
[ + + ]
424 [ + + ]: 113 : || std::filesystem::is_regular_file(std::filesystem::path{argv[1]}, ec)))
425 : 93 : {
426 : 93 : main_func = mainEntryClickHouseLocal;
427 : 93 : }
428 : :
429 : : /// If the argument looks like a file path but doesn't exist, provide a helpful error
430 : : /// instead of the generic "Use one of the following commands" message.
431 : : /// The check above routes existing files to clickhouse-local, but when the file
432 : : /// doesn't exist, we fall through to `printHelp` which is confusing:
433 : : /// $ clickhouse tests/queries/0_stateless/my_test.sql
434 : : /// Use one of the following commands: ...
435 : : /// We detect file-like arguments by the presence of `/` (path separator)
436 : : /// or `.` (file extension), which distinguishes them from mistyped subcommand
437 : : /// names like "clickhouse sever" where the generic help is appropriate.
438 [ + + ][ + - ]: 201275 : if (main_func == printHelpOnError && argv.size() >= 2)
439 : 20 : {
440 : 20 : std::string_view arg(argv[1]);
441 [ + + ][ + + ]: 20 : if (arg.contains('/') || arg.contains('.'))
442 : 8 : {
443 : 8 : std::cerr << "Error: no such file: " << arg << std::endl;
444 : 8 : std::cerr << "If you intended to run a script, please check the path." << std::endl;
445 : 8 : return 1;
446 : 8 : }
447 : 20 : }
448 : :
449 : 201267 : int exit_code = main_func(static_cast<int>(argv.size()), argv.data());
450 : :
451 : 201267 : return exit_code;
452 : 201275 : }
|