Skip to content

Commit

Permalink
Merge pull request #2582 from barton2526/have-system
Browse files Browse the repository at this point in the history
build: Check std::system for -[alert|block|wallet]notify
  • Loading branch information
jamescowens committed Nov 6, 2022
2 parents 1929321 + 82197b3 commit df64b60
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 0 deletions.
25 changes: 25 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,31 @@ AC_LINK_IFELSE([AC_LANG_SOURCE([
]
)

have_any_system=no
AC_MSG_CHECKING([for std::system])
AC_LINK_IFELSE(
[ AC_LANG_PROGRAM(
[[ #include <cstdlib> ]],
[[ int nErr = std::system(""); ]]
)],
[ AC_MSG_RESULT(yes); have_any_system=yes],
[ AC_MSG_RESULT(no) ]
)

AC_MSG_CHECKING([for ::_wsystem])
AC_LINK_IFELSE(
[ AC_LANG_PROGRAM(
[[ ]],
[[ int nErr = ::_wsystem(""); ]]
)],
[ AC_MSG_RESULT(yes); have_any_system=yes],
[ AC_MSG_RESULT(no) ]
)

if test "x$have_any_system" != "xno"; then
AC_DEFINE(HAVE_SYSTEM, 1, Define to 1 if std::system or ::wsystem is available.)
fi

dnl Our minimum supported glibc is 2.17, however support for thread_local
dnl did not arrive in glibc until 2.18.
if test "$use_thread_local" = "yes" || test "$use_thread_local" = "auto"; then
Expand Down
2 changes: 2 additions & 0 deletions src/alert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ bool CAlert::ProcessAlert(bool fThread)
if(AppliesToMe())
{
uiInterface.NotifyAlertChanged(GetHash(), CT_NEW);
#if HAVE_SYSTEM
std::string strCmd = gArgs.GetArg("-alertnotify", "");
if (!strCmd.empty())
{
Expand All @@ -282,6 +283,7 @@ bool CAlert::ProcessAlert(bool fThread)
else
runCommand(strCmd);
}
#endif
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -337,17 +337,21 @@ void SetupServerArgs()
argsman.AddArg("-mininput=<amt>", "When creating transactions, ignore inputs with value less than this (default: 0.01)",
ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
argsman.AddArg("-testnet", "Use the test network", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
#if HAVE_SYSTEM
argsman.AddArg("-blocknotify=<cmd>", "Execute command when the best block changes (%s in cmd is replaced by block hash)",
ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
argsman.AddArg("-walletnotify=<cmd>", "Execute command when a wallet transaction changes (%s in cmd is replaced by TxID)",
ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
#endif
argsman.AddArg("-confchange", "Require confirmations for change (default: 0)",
ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
argsman.AddArg("-enforcecanonical", "Enforce transaction scripts to use canonical PUSH operators (default: 1)",
ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
#if HAVE_SYSTEM
argsman.AddArg("-alertnotify=<cmd>", "Execute command when a relevant alert is received or we see a really long fork"
" (%s in cmd is replaced by message)",
ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
#endif
argsman.AddArg("-blockminsize=<n>", "Set minimum block size in bytes (default: 0)",
ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
argsman.AddArg("-blockmaxsize=<n>", strprintf("Set maximum block size in bytes (default: %u)", MAX_BLOCK_SIZE_GEN/2),
Expand Down
2 changes: 2 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1277,12 +1277,14 @@ bool SetBestChain(CTxDB& txdb, CBlock &blockNew, CBlockIndex* pindexNew) EXCLUSI
else
LogPrintf("{SBC} new best {%s %d} ; ",hashBestChain.ToString(), nBestHeight);

#if HAVE_SYSTEM
std::string strCmd = gArgs.GetArg("-blocknotify", "");
if (!fIsInitialDownload && !strCmd.empty())
{
boost::replace_all(strCmd, "%s", hashBestChain.GetHex());
boost::thread t(runCommand, strCmd); // thread runs free
}
#endif

uiInterface.NotifyBlocksChanged(
fIsInitialDownload,
Expand Down
2 changes: 2 additions & 0 deletions src/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,7 @@ std::vector<std::string> split(const std::string& s, const std::string& delim)
return elems;
}

#if HAVE_SYSTEM
void runCommand(std::string strCommand)
{
#ifndef WIN32
Expand All @@ -378,6 +379,7 @@ void runCommand(std::string strCommand)
if (nErr)
LogPrintf("runCommand error: system(%s) returned %d", strCommand, nErr);
}
#endif

void RenameThread(const char* name)
{
Expand Down
2 changes: 2 additions & 0 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,9 @@ std::string GetFileContents(const fs::path filepath);
int64_t GetTimeOffset();
int64_t GetAdjustedTime();
void AddTimeData(const CNetAddr& ip, int64_t nOffsetSample);
#if HAVE_SYSTEM
void runCommand(std::string strCommand);
#endif

//!
//! \brief Round double value to N decimal places.
Expand Down
2 changes: 2 additions & 0 deletions src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -538,12 +538,14 @@ bool CWallet::AddToWallet(const CWalletTx& wtxIn, CWalletDB* pwalletdb)
NotifyTransactionChanged(this, hash, fInsertedNew ? CT_NEW : CT_UPDATED);

// notify an external script when a wallet transaction comes in or is updated
#if HAVE_SYSTEM
std::string strCmd = gArgs.GetArg("-walletnotify", "");
if (!strCmd.empty())
{
boost::replace_all(strCmd, "%s", hash.GetHex());
boost::thread t(runCommand, strCmd); // thread runs free
}
#endif

}
return true;
Expand Down

0 comments on commit df64b60

Please sign in to comment.