Skip to content

Commit 92dcbe9

Browse files
author
MarcoFalke
committed
Merge bitcoin/bitcoin#23395: util: Add -shutdownnotify option
d96d97a doc: Add release note for shutdownnotify. (klementtan) 0bd73e2 util: Add -shutdownnotify option. (klementtan) Pull request description: **Description**: Similar to `-startupnotify`, this PR adds a new option to allow users to specify a command to be executed when Bitcoin Core shuts down. **Note**: The `shutdownnotify` commands will not be executed if bitcoind shut down due to *unexpected* reasons (ie `killall -9 bitcoind`). ### Testing: **Normal shutdown commands** ``` # start bitcoind with shutdownnotify optioin ./src/bitcoind -signet -shutdownnotify="touch foo.txt" # shutdown bitcoind ./src/bitcoin-cli -signet stop # check that foo.txt has been created ``` **Final RPC call** Commands: ``` $ ./src/bitcoind -signet -nolisten -noconnect -shutdownnotify="./src/bitcoin-cli -signet getblockchaininfo > tmp.txt" $ ./src/bitcoin-cli stop $ cat tmp.txt ``` <details> <summary>Screen Shot</summary> ![image](https://user-images.githubusercontent.com/49265907/141186183-cbc6f82c-400d-4a8b-baba-27c0346c2c8a.png) </details> ACKs for top commit: achow101: ACK d96d97a 1440000bytes: ACK bitcoin/bitcoin@d96d97a theStack: re-ACK d96d97a Tree-SHA512: 16f7406fd232e8b97aea5e58854c84755b0c35c88cb3ef9ee123b29a1475a376122b1e100da860cc336d4d657e6046a70e915fdb9b70c9fd097c6eef1b028161
2 parents 8ae2808 + d96d97a commit 92dcbe9

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

doc/release-notes-23395.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Notable changes
2+
===============
3+
4+
New settings
5+
------------
6+
7+
- The `shutdownnotify` option is used to specify a command to execute synchronously
8+
before Bitcoin Core has begun its shutdown sequence. (#23395)

src/init.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,24 @@ static fs::path GetPidFile(const ArgsManager& args)
191191
// shutdown thing.
192192
//
193193

194+
#if HAVE_SYSTEM
195+
static void ShutdownNotify(const ArgsManager& args)
196+
{
197+
std::vector<std::thread> threads;
198+
for (const auto& cmd : args.GetArgs("-shutdownnotify")) {
199+
threads.emplace_back(runCommand, cmd);
200+
}
201+
for (auto& t : threads) {
202+
t.join();
203+
}
204+
}
205+
#endif
206+
194207
void Interrupt(NodeContext& node)
195208
{
209+
#if HAVE_SYSTEM
210+
ShutdownNotify(*node.args);
211+
#endif
196212
InterruptHTTPServer();
197213
InterruptHTTPRPC();
198214
InterruptRPC();
@@ -441,6 +457,7 @@ void SetupServerArgs(ArgsManager& argsman)
441457
argsman.AddArg("-settings=<file>", strprintf("Specify path to dynamic settings data file. Can be disabled with -nosettings. File is written at runtime and not meant to be edited by users (use %s instead for custom settings). Relative paths will be prefixed by datadir location. (default: %s)", BITCOIN_CONF_FILENAME, BITCOIN_SETTINGS_FILENAME), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
442458
#if HAVE_SYSTEM
443459
argsman.AddArg("-startupnotify=<cmd>", "Execute command on startup.", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
460+
argsman.AddArg("-shutdownnotify=<cmd>", "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);
444461
#endif
445462
#ifndef WIN32
446463
argsman.AddArg("-sysperms", "Create new files with system default permissions, instead of umask 077 (only effective with disabled wallet functionality)", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);

test/functional/feature_notifications.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,26 @@ def set_test_params(self):
3131
self.num_nodes = 2
3232
self.setup_clean_chain = True
3333
# The experimental syscall sandbox feature (-sandbox) is not compatible with -alertnotify,
34-
# -blocknotify or -walletnotify (which all invoke execve).
34+
# -blocknotify, -walletnotify or -shutdownnotify (which all invoke execve).
3535
self.disable_syscall_sandbox = True
3636

3737
def setup_network(self):
3838
self.wallet = ''.join(chr(i) for i in range(FILE_CHAR_START, FILE_CHAR_END) if chr(i) not in FILE_CHARS_DISALLOWED)
3939
self.alertnotify_dir = os.path.join(self.options.tmpdir, "alertnotify")
4040
self.blocknotify_dir = os.path.join(self.options.tmpdir, "blocknotify")
4141
self.walletnotify_dir = os.path.join(self.options.tmpdir, "walletnotify")
42+
self.shutdownnotify_dir = os.path.join(self.options.tmpdir, "shutdownnotify")
43+
self.shutdownnotify_file = os.path.join(self.shutdownnotify_dir, "shutdownnotify.txt")
4244
os.mkdir(self.alertnotify_dir)
4345
os.mkdir(self.blocknotify_dir)
4446
os.mkdir(self.walletnotify_dir)
47+
os.mkdir(self.shutdownnotify_dir)
4548

4649
# -alertnotify and -blocknotify on node0, walletnotify on node1
4750
self.extra_args = [[
4851
f"-alertnotify=echo > {os.path.join(self.alertnotify_dir, '%s')}",
4952
f"-blocknotify=echo > {os.path.join(self.blocknotify_dir, '%s')}",
53+
f"-shutdownnotify=echo > {self.shutdownnotify_file}",
5054
], [
5155
f"-walletnotify=echo %h_%b > {os.path.join(self.walletnotify_dir, notify_outputname('%w', '%s'))}",
5256
]]
@@ -162,6 +166,10 @@ def run_test(self):
162166

163167
# TODO: add test for `-alertnotify` large fork notifications
164168

169+
self.log.info("test -shutdownnotify")
170+
self.stop_nodes()
171+
self.wait_until(lambda: os.path.isfile(self.shutdownnotify_file), timeout=10)
172+
165173
def expect_wallet_notify(self, tx_details):
166174
self.wait_until(lambda: len(os.listdir(self.walletnotify_dir)) >= len(tx_details), timeout=10)
167175
# Should have no more and no less files than expected

0 commit comments

Comments
 (0)