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