| 1 | // Copyright (c) 2014-2022, The Monero Project |
| 2 | // |
| 3 | // All rights reserved. |
| 4 | // |
| 5 | // Redistribution and use in source and binary forms, with or without modification, are |
| 6 | // permitted provided that the following conditions are met: |
| 7 | // |
| 8 | // 1. Redistributions of source code must retain the above copyright notice, this list of |
| 9 | // conditions and the following disclaimer. |
| 10 | // |
| 11 | // 2. Redistributions in binary form must reproduce the above copyright notice, this list |
| 12 | // of conditions and the following disclaimer in the documentation and/or other |
| 13 | // materials provided with the distribution. |
| 14 | // |
| 15 | // 3. Neither the name of the copyright holder nor the names of its contributors may be |
| 16 | // used to endorse or promote products derived from this software without specific |
| 17 | // prior written permission. |
| 18 | // |
| 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY |
| 20 | // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 21 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
| 22 | // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 24 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 26 | // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF |
| 27 | // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | #include "wallet/wallet_args.h" |
| 29 | |
| 30 | #include <boost/filesystem/path.hpp> |
| 31 | #include <boost/filesystem/operations.hpp> |
| 32 | #include <boost/format.hpp> |
| 33 | #include "common/i18n.h" |
| 34 | #include "common/util.h" |
| 35 | #include "misc_log_ex.h" |
| 36 | #include "string_tools.h" |
| 37 | #include "version.h" |
| 38 | |
| 39 | #if defined(WIN32) |
| 40 | #include <crtdbg.h> |
| 41 | #endif |
| 42 | |
| 43 | #undef MONERO_DEFAULT_LOG_CATEGORY |
| 44 | #define MONERO_DEFAULT_LOG_CATEGORY "wallet.wallet2" |
| 45 | |
| 46 | // workaround for a suspected bug in pthread/kernel on MacOS X |
| 47 | #ifdef __APPLE__ |
| 48 | #define DEFAULT_MAX_CONCURRENCY 1 |
| 49 | #else |
| 50 | #define DEFAULT_MAX_CONCURRENCY 0 |
| 51 | #endif |
| 52 | |
| 53 | namespace |
| 54 | { |
| 55 | class Print |
| 56 | { |
| 57 | public: |
| 58 | Print(const std::function<void(const std::string&, bool)> &p, bool em = false): print(p), emphasis(em) {} |
| 59 | ~Print() { print(ss.str(), emphasis); } |
| 60 | template<typename T> std::ostream &operator<<(const T &t) { ss << t; return ss; } |
| 61 | private: |
| 62 | const std::function<void(const std::string&, bool)> &print; |
| 63 | std::stringstream ss; |
| 64 | bool emphasis; |
| 65 | }; |
| 66 | } |
| 67 | |
| 68 | namespace wallet_args |
| 69 | { |
| 70 | // Create on-demand to prevent static initialization order fiasco issues. |
| 71 | command_line::arg_descriptor<std::string> arg_generate_from_json() |
| 72 | { |
| 73 | return {"generate-from-json", wallet_args::tr("Generate wallet from JSON format file"), ""}; |
| 74 | } |
| 75 | command_line::arg_descriptor<std::string> arg_wallet_file() |
| 76 | { |
| 77 | return {"wallet-file", wallet_args::tr("Use wallet <arg>"), ""}; |
| 78 | } |
| 79 | command_line::arg_descriptor<std::string> arg_rpc_client_secret_key() |
| 80 | { |
| 81 | return {"rpc-client-secret-key", wallet_args::tr("Set RPC client secret key for RPC payments"), ""}; |
| 82 | } |
| 83 | command_line::arg_descriptor<std::string> arg_password_file() |
| 84 | { |
| 85 | return {"password-file", wallet_args::tr("Wallet password file"), ""}; |
| 86 | } |
| 87 | |
| 88 | const char* tr(const char* str) |
| 89 | { |
| 90 | return i18n_translate(str, "wallet_args"); |
| 91 | } |
| 92 | |
| 93 | std::pair<boost::optional<boost::program_options::variables_map>, bool> main( |
| 94 | int argc, char** argv, |
| 95 | const char* const usage, |
| 96 | const char* const notice, |
| 97 | boost::program_options::options_description desc_params, |
| 98 | const boost::program_options::positional_options_description& positional_options, |
| 99 | const std::function<void(const std::string&, bool)> &print, |
| 100 | const char *default_log_name, |
| 101 | bool log_to_console) |
| 102 | |
| 103 | { |
| 104 | namespace bf = boost::filesystem; |
| 105 | namespace po = boost::program_options; |
| 106 | #ifdef WIN32 |
| 107 | _CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF ); |
| 108 | #endif |
| 109 | |
| 110 | const command_line::arg_descriptor<std::string> arg_log_level = {"log-level", "0-4 or categories", ""}; |
| 111 | const command_line::arg_descriptor<std::size_t> arg_max_log_file_size = {"max-log-file-size", "Specify maximum log file size [B]", MAX_LOG_FILE_SIZE}; |
| 112 | const command_line::arg_descriptor<std::size_t> arg_max_log_files = {"max-log-files", "Specify maximum number of rotated log files to be saved (no limit by setting to 0)", MAX_LOG_FILES}; |
| 113 | const command_line::arg_descriptor<uint32_t> arg_max_concurrency = {"max-concurrency", wallet_args::tr("Max number of threads to use for a parallel job"), DEFAULT_MAX_CONCURRENCY}; |
| 114 | const command_line::arg_descriptor<std::string> arg_log_file = {"log-file", wallet_args::tr("Specify log file"), ""}; |
| 115 | const command_line::arg_descriptor<std::string> arg_config_file = {"config-file", wallet_args::tr("Config file"), "", true}; |
| 116 | |
| 117 | |
| 118 | std::string lang = i18n_get_language(); |
| 119 | tools::on_startup(); |
| 120 | #ifdef NDEBUG |
| 121 | tools::disable_core_dumps(); |
| 122 | #endif |
| 123 | tools::set_strict_default_file_permissions(true); |
| 124 | |
| 125 | epee::string_tools::set_module_name_and_folder(argv[0]); |
| 126 | |
| 127 | po::options_description desc_general(wallet_args::tr("General options")); |
| 128 | command_line::add_arg(desc_general, command_line::arg_help); |
| 129 | command_line::add_arg(desc_general, command_line::arg_version); |
| 130 | |
| 131 | command_line::add_arg(desc_params, arg_log_file); |
| 132 | command_line::add_arg(desc_params, arg_log_level); |
| 133 | command_line::add_arg(desc_params, arg_max_log_file_size); |
| 134 | command_line::add_arg(desc_params, arg_max_log_files); |
| 135 | command_line::add_arg(desc_params, arg_max_concurrency); |
| 136 | command_line::add_arg(desc_params, arg_config_file); |
| 137 | |
| 138 | i18n_set_language("translations", "monero", lang); |
| 139 | |
| 140 | po::options_description desc_all; |
| 141 | desc_all.add(desc_general).add(desc_params); |
| 142 | po::variables_map vm; |
| 143 | bool should_terminate = false; |
| 144 | bool r = command_line::handle_error_helper(desc_all, [&]() |
| 145 | { |
| 146 | auto parser = po::command_line_parser(argc, argv).options(desc_all).positional(positional_options); |
| 147 | po::store(parser.run(), vm); |
| 148 | |
| 149 | if (command_line::get_arg(vm, command_line::arg_help)) |
| 150 | { |
| 151 | Print(print) << "Monero '" << MONERO_RELEASE_NAME << "' (v" << MONERO_VERSION_FULL << ")" << ENDL; |
| 152 | Print(print) << wallet_args::tr("This is the command line monero wallet. It needs to connect to a monero\n" |
| 153 | "daemon to work correctly.") << ENDL; |
| 154 | Print(print) << wallet_args::tr("Usage:") << ENDL << " " << usage; |
| 155 | Print(print) << desc_all; |
| 156 | should_terminate = true; |
| 157 | return true; |
| 158 | } |
| 159 | else if (command_line::get_arg(vm, command_line::arg_version)) |
| 160 | { |
| 161 | Print(print) << "Monero '" << MONERO_RELEASE_NAME << "' (v" << MONERO_VERSION_FULL << ")"; |
| 162 | should_terminate = true; |
| 163 | return true; |
| 164 | } |
| 165 | |
| 166 | if(command_line::has_arg(vm, arg_config_file)) |
| 167 | { |
| 168 | std::string config = command_line::get_arg(vm, arg_config_file); |
| 169 | bf::path config_path(config); |
| 170 | boost::system::error_code ec; |
| 171 | if (bf::exists(config_path, ec)) |
| 172 | { |
| 173 | po::store(po::parse_config_file<char>(config_path.string<std::string>().c_str(), desc_params), vm); |
| 174 | } |
| 175 | else |
| 176 | { |
| 177 | MERROR(wallet_args::tr("Can't find config file ") << config); |
| 178 | return false; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | po::notify(vm); |
| 183 | return true; |
| 184 | }); |
| 185 | if (!r) |
| 186 | return {boost::none, true}; |
| 187 | |
| 188 | if (should_terminate) |
| 189 | return {std::move(vm), should_terminate}; |
| 190 | |
| 191 | std::string log_path; |
| 192 | if (!command_line::is_arg_defaulted(vm, arg_log_file)) |
| 193 | log_path = command_line::get_arg(vm, arg_log_file); |
| 194 | else |
| 195 | log_path = mlog_get_default_log_path(default_log_name); |
| 196 | mlog_configure(log_path, log_to_console, command_line::get_arg(vm, arg_max_log_file_size), command_line::get_arg(vm, arg_max_log_files)); |
| 197 | if (!command_line::is_arg_defaulted(vm, arg_log_level)) |
| 198 | { |
| 199 | mlog_set_log(command_line::get_arg(vm, arg_log_level).c_str()); |
| 200 | } |
| 201 | else if (!log_to_console) |
| 202 | { |
| 203 | mlog_set_categories(""); |
| 204 | } |
| 205 | |
| 206 | if (notice) |
| 207 | Print(print) << notice << ENDL; |
| 208 | |
| 209 | if (!command_line::is_arg_defaulted(vm, arg_max_concurrency)) |
| 210 | tools::set_max_concurrency(command_line::get_arg(vm, arg_max_concurrency)); |
| 211 | |
| 212 | Print(print) << "Monero '" << MONERO_RELEASE_NAME << "' (v" << MONERO_VERSION_FULL << ")"; |
| 213 | |
| 214 | if (!command_line::is_arg_defaulted(vm, arg_log_level)) |
| 215 | MINFO("Setting log level = " << command_line::get_arg(vm, arg_log_level)); |
| 216 | else |
| 217 | { |
| 218 | const char *logs = getenv("MONERO_LOGS"); |
| 219 | MINFO("Setting log levels = " << (logs ? logs : "<default>")); |
| 220 | } |
| 221 | MINFO(wallet_args::tr("Logging to: ") << log_path); |
| 222 | |
| 223 | Print(print) << boost::format(wallet_args::tr("Logging to %s")) % log_path; |
| 224 | |
| 225 | const ssize_t lockable_memory = tools::get_lockable_memory(); |
| 226 | if (lockable_memory >= 0 && lockable_memory < 256 * 4096) // 256 pages -> at least 256 secret keys and other such small/medium objects |
| 227 | Print(print) << tr("WARNING: You may not have a high enough lockable memory limit") |
| 228 | #ifdef ELPP_OS_UNIX |
| 229 | << ", " << tr("see ulimit -l") |
| 230 | #endif |
| 231 | ; |
| 232 | |
| 233 | return {std::move(vm), should_terminate}; |
| 234 | } |
| 235 | } |