From fb890f4161d0b014586959a4c810d470026f4846 Mon Sep 17 00:00:00 2001 From: Jouzo <15011228+Jouzo@users.noreply.github.com> Date: Thu, 16 Jun 2022 13:07:28 +0100 Subject: [PATCH] util: Add -shutdownnotify option. (#1345) Co-authored-by: klementtan Co-authored-by: Prasanna Loganathar --- src/init.cpp | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/init.cpp b/src/init.cpp index 992763efd8..4afc01890c 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -163,8 +163,24 @@ static std::unique_ptr globalVerifyHandle; static boost::thread_group threadGroup; static CScheduler scheduler; +#if HAVE_SYSTEM +static void ShutdownNotify() +{ + std::vector threads; + for (const auto& cmd : gArgs.GetArgs("-shutdownnotify")) { + threads.emplace_back(runCommand, cmd); + } + for (auto& t : threads) { + t.join(); + } +} +#endif + void Interrupt() { +#if HAVE_SYSTEM + ShutdownNotify(); +#endif InterruptHTTPServer(); InterruptHTTPRPC(); InterruptRPC(); @@ -376,7 +392,7 @@ void SetupServerArgs() // Hidden Options std::vector hidden_args = { - "-dbcrashratio", "-forcecompactdb", + "-dbcrashratio", "-forcecompactdb", "-interrupt-block=", "-stop-block=", "-mocknet", "-mocknet-blocktime=", "-mocknet-key=" // GUI args. These will be overwritten by SetupUIArgs for the GUI @@ -416,6 +432,9 @@ void SetupServerArgs() "(default: 0 = disable pruning blocks, 1 = allow manual pruning via RPC, >=%u = automatically prune block files to stay under the specified target size in MiB)", MIN_DISK_SPACE_FOR_BLOCK_FILES / 1024 / 1024), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS); gArgs.AddArg("-reindex", "Rebuild chain state and block index from the blk*.dat files on disk", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS); gArgs.AddArg("-reindex-chainstate", "Rebuild chain state from the currently indexed blocks. When in pruning mode or if blocks on disk might be corrupted, use full -reindex instead.", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS); +#if HAVE_SYSTEM + gArgs.AddArg("-shutdownnotify=", "Execute command immediately before beginning shutdown. The need for shutdown may be urgent, so be careful not to delay it long (if the command doesn't require interaction with the server, consider having it fork into the background).", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS); +#endif #ifndef WIN32 gArgs.AddArg("-sysperms", "Create new files with system default permissions, instead of umask 077 (only effective with disabled wallet functionality)", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS); #else @@ -1241,7 +1260,7 @@ bool AppInitParameterInteraction() nMaxTipAge = gArgs.GetArg("-maxtipage", DEFAULT_MAX_TIP_AGE); fIsFakeNet = Params().NetworkIDString() == "regtest" && gArgs.GetArg("-dummypos", false); CTxOut::SERIALIZE_FORCED_TO_OLD_IN_TESTS = Params().NetworkIDString() == "regtest" && gArgs.GetArg("-txnotokens", false); - + return true; }