| 1021 | namespace |
| 1022 | { |
| 1023 | int start(const std::string & user, const std::string & group, const fs::path & binary, const fs::path & executable, const fs::path & config, const fs::path & pid_file, unsigned max_tries, bool no_sudo) |
| 1024 | { |
| 1025 | if (fs::exists(pid_file)) |
| 1026 | { |
| 1027 | ReadBufferFromFile in(pid_file.string()); |
| 1028 | Int32 pid = {}; |
| 1029 | if (tryReadIntText(pid, in)) |
| 1030 | { |
| 1031 | fmt::print("{} file exists and contains pid = {}.\n", pid_file.string(), pid); |
| 1032 | |
| 1033 | if (0 == kill(pid, 0)) |
| 1034 | { |
| 1035 | fmt::print("The process with pid = {} is already running.\n", pid); |
| 1036 | return 2; |
| 1037 | } |
| 1038 | } |
| 1039 | else |
| 1040 | { |
| 1041 | fmt::print("{} file exists but damaged, ignoring.\n", pid_file.string()); |
| 1042 | (void)fs::remove(pid_file); |
| 1043 | } |
| 1044 | } |
| 1045 | else |
| 1046 | { |
| 1047 | /// Create a directory for pid file. |
| 1048 | /// It's created by "install" but we also support cases when ClickHouse is already installed different way. |
| 1049 | fs::path pid_path = pid_file; |
| 1050 | pid_path = pid_path.remove_filename(); |
| 1051 | fs::create_directories(pid_path); |
| 1052 | /// All users are allowed to read pid file (for clickhouse status command). |
| 1053 | fs::permissions(pid_path, fs::perms::owner_all | fs::perms::group_read | fs::perms::others_read, fs::perm_options::replace); |
| 1054 | |
| 1055 | changeOwnership(pid_path, user); |
| 1056 | } |
| 1057 | |
| 1058 | std::string command = fmt::format("{} --config-file {} --pid-file {} --daemon", |
| 1059 | shellQuote(executable.string()), shellQuote(config.string()), shellQuote(pid_file.string())); |
| 1060 | |
| 1061 | if (!user.empty()) |
| 1062 | { |
| 1063 | if (no_sudo) |
| 1064 | { |
| 1065 | /// Sometimes there is no sudo available like in some Docker images. |
| 1066 | /// We will use clickhouse su instead. |
| 1067 | command = fmt::format("{} su {} {}", |
| 1068 | shellQuote(binary.string()), shellQuote(user + ":" + group), command); |
| 1069 | } |
| 1070 | else |
| 1071 | { |
| 1072 | /// sudo respects limits in /etc/security/limits.conf e.g. open files, |
| 1073 | /// that's why we are using it instead of the 'clickhouse su' tool. |
| 1074 | /// by default, sudo resets all the ENV variables, but we should preserve |
| 1075 | /// the values /etc/default/clickhouse in /etc/init.d/clickhouse file |
| 1076 | if (!group.empty()) |
| 1077 | command = fmt::format("sudo --preserve-env -u {} -g {} {}", shellQuote(user), shellQuote(group), command); |
| 1078 | else |
| 1079 | command = fmt::format("sudo --preserve-env -u {} {}", shellQuote(user), command); |
| 1080 | } |
no test coverage detected