kernel_notifications.cpp
1 // Copyright (c) 2023 The Bitcoin Core developers 2 // Distributed under the MIT software license, see the accompanying 3 // file COPYING or http://www.opensource.org/licenses/mit-license.php. 4 5 #include <node/kernel_notifications.h> 6 7 #if defined(HAVE_CONFIG_H) 8 #include <config/bitcoin-config.h> 9 #endif 10 11 #include <chain.h> 12 #include <common/args.h> 13 #include <common/system.h> 14 #include <kernel/context.h> 15 #include <logging.h> 16 #include <node/abort.h> 17 #include <node/interface_ui.h> 18 #include <util/check.h> 19 #include <util/strencodings.h> 20 #include <util/string.h> 21 #include <util/translation.h> 22 #include <warnings.h> 23 24 #include <cstdint> 25 #include <string> 26 #include <thread> 27 28 static void AlertNotify(const std::string& strMessage) 29 { 30 uiInterface.NotifyAlertChanged(); 31 #if HAVE_SYSTEM 32 std::string strCmd = gArgs.GetArg("-alertnotify", ""); 33 if (strCmd.empty()) return; 34 35 // Alert text should be plain ascii coming from a trusted source, but to 36 // be safe we first strip anything not in safeChars, then add single quotes around 37 // the whole string before passing it to the shell: 38 std::string singleQuote("'"); 39 std::string safeStatus = SanitizeString(strMessage); 40 safeStatus = singleQuote+safeStatus+singleQuote; 41 ReplaceAll(strCmd, "%s", safeStatus); 42 43 std::thread t(runCommand, strCmd); 44 t.detach(); // thread runs free 45 #endif 46 } 47 48 static void DoWarning(const bilingual_str& warning) 49 { 50 static bool fWarned = false; 51 SetMiscWarning(warning); 52 if (!fWarned) { 53 AlertNotify(warning.original); 54 fWarned = true; 55 } 56 } 57 58 namespace node { 59 60 kernel::InterruptResult KernelNotifications::blockTip(SynchronizationState state, CBlockIndex& index) 61 { 62 uiInterface.NotifyBlockTip(state, &index); 63 if (m_stop_at_height && index.nHeight >= m_stop_at_height) { 64 if (!m_shutdown()) { 65 LogPrintf("Error: failed to send shutdown signal after reaching stop height\n"); 66 } 67 return kernel::Interrupted{}; 68 } 69 return {}; 70 } 71 72 void KernelNotifications::headerTip(SynchronizationState state, int64_t height, int64_t timestamp, bool presync) 73 { 74 uiInterface.NotifyHeaderTip(state, height, timestamp, presync); 75 } 76 77 void KernelNotifications::progress(const bilingual_str& title, int progress_percent, bool resume_possible) 78 { 79 uiInterface.ShowProgress(title.translated, progress_percent, resume_possible); 80 } 81 82 void KernelNotifications::warning(const bilingual_str& warning) 83 { 84 DoWarning(warning); 85 } 86 87 void KernelNotifications::flushError(const bilingual_str& message) 88 { 89 AbortNode(&m_shutdown, m_exit_status, message); 90 } 91 92 void KernelNotifications::fatalError(const bilingual_str& message) 93 { 94 node::AbortNode(m_shutdown_on_fatal_error ? &m_shutdown : nullptr, 95 m_exit_status, message); 96 } 97 98 void ReadNotificationArgs(const ArgsManager& args, KernelNotifications& notifications) 99 { 100 if (auto value{args.GetIntArg("-stopatheight")}) notifications.m_stop_at_height = *value; 101 } 102 103 } // namespace node