Skip to content

Commit 192325a

Browse files
committed
kernel: move RunCommandParseJSON to its own file
Because libbitcoinkernel does not include this new object, this has the side-effect of eliminating the unnecessary boost::process dependency.
1 parent b2da6dd commit 192325a

File tree

7 files changed

+89
-61
lines changed

7 files changed

+89
-61
lines changed

src/Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@ BITCOIN_CORE_H = \
284284
util/rbf.h \
285285
util/readwritefile.h \
286286
util/result.h \
287+
util/run_command.h \
287288
util/serfloat.h \
288289
util/settings.h \
289290
util/sock.h \
@@ -680,6 +681,7 @@ libbitcoin_util_a_SOURCES = \
680681
util/fees.cpp \
681682
util/getuniquepath.cpp \
682683
util/hasher.cpp \
684+
util/run_command.cpp \
683685
util/sock.cpp \
684686
util/syserror.cpp \
685687
util/system.cpp \

src/external_signer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
#include <chainparams.h>
66
#include <core_io.h>
77
#include <psbt.h>
8+
#include <util/run_command.h>
89
#include <util/strencodings.h>
9-
#include <util/system.h>
1010
#include <external_signer.h>
1111

1212
#include <algorithm>

src/test/system_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44
//
55
#include <test/util/setup_common.h>
6-
#include <util/system.h>
6+
#include <util/run_command.h>
77
#include <univalue.h>
88

99
#ifdef ENABLE_EXTERNAL_SIGNER

src/util/run_command.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright (c) 2022 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+
#if defined(HAVE_CONFIG_H)
6+
#include <config/bitcoin-config.h>
7+
#endif
8+
9+
#include <util/run_command.h>
10+
11+
#include <tinyformat.h>
12+
#include <univalue.h>
13+
14+
#ifdef ENABLE_EXTERNAL_SIGNER
15+
#if defined(__GNUC__)
16+
// Boost 1.78 requires the following workaround.
17+
// See: https://github.com/boostorg/process/issues/235
18+
#pragma GCC diagnostic push
19+
#pragma GCC diagnostic ignored "-Wnarrowing"
20+
#endif
21+
#include <boost/process.hpp>
22+
#if defined(__GNUC__)
23+
#pragma GCC diagnostic pop
24+
#endif
25+
#endif // ENABLE_EXTERNAL_SIGNER
26+
27+
UniValue RunCommandParseJSON(const std::string& str_command, const std::string& str_std_in)
28+
{
29+
#ifdef ENABLE_EXTERNAL_SIGNER
30+
namespace bp = boost::process;
31+
32+
UniValue result_json;
33+
bp::opstream stdin_stream;
34+
bp::ipstream stdout_stream;
35+
bp::ipstream stderr_stream;
36+
37+
if (str_command.empty()) return UniValue::VNULL;
38+
39+
bp::child c(
40+
str_command,
41+
bp::std_out > stdout_stream,
42+
bp::std_err > stderr_stream,
43+
bp::std_in < stdin_stream
44+
);
45+
if (!str_std_in.empty()) {
46+
stdin_stream << str_std_in << std::endl;
47+
}
48+
stdin_stream.pipe().close();
49+
50+
std::string result;
51+
std::string error;
52+
std::getline(stdout_stream, result);
53+
std::getline(stderr_stream, error);
54+
55+
c.wait();
56+
const int n_error = c.exit_code();
57+
if (n_error) throw std::runtime_error(strprintf("RunCommandParseJSON error: process(%s) returned %d: %s\n", str_command, n_error, error));
58+
if (!result_json.read(result)) throw std::runtime_error("Unable to parse JSON: " + result);
59+
60+
return result_json;
61+
#else
62+
throw std::runtime_error("Compiled without external signing support (required for external signing).");
63+
#endif // ENABLE_EXTERNAL_SIGNER
64+
}

src/util/run_command.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright (c) 2022 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+
#ifndef BITCOIN_UTIL_RUN_COMMAND_H
6+
#define BITCOIN_UTIL_RUN_COMMAND_H
7+
8+
#include <string>
9+
10+
class UniValue;
11+
12+
/**
13+
* Execute a command which returns JSON, and parse the result.
14+
*
15+
* @param str_command The command to execute, including any arguments
16+
* @param str_std_in string to pass to stdin
17+
* @return parsed JSON
18+
*/
19+
UniValue RunCommandParseJSON(const std::string& str_command, const std::string& str_std_in="");
20+
21+
#endif // BITCOIN_UTIL_RUN_COMMAND_H

src/util/system.cpp

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,6 @@
55

66
#include <util/system.h>
77

8-
#ifdef ENABLE_EXTERNAL_SIGNER
9-
#if defined(__GNUC__)
10-
// Boost 1.78 requires the following workaround.
11-
// See: https://github.com/boostorg/process/issues/235
12-
#pragma GCC diagnostic push
13-
#pragma GCC diagnostic ignored "-Wnarrowing"
14-
#endif
15-
#include <boost/process.hpp>
16-
#if defined(__GNUC__)
17-
#pragma GCC diagnostic pop
18-
#endif
19-
#endif // ENABLE_EXTERNAL_SIGNER
20-
218
#include <chainparamsbase.h>
229
#include <fs.h>
2310
#include <sync.h>
@@ -1332,44 +1319,6 @@ void runCommand(const std::string& strCommand)
13321319
}
13331320
#endif
13341321

1335-
UniValue RunCommandParseJSON(const std::string& str_command, const std::string& str_std_in)
1336-
{
1337-
#ifdef ENABLE_EXTERNAL_SIGNER
1338-
namespace bp = boost::process;
1339-
1340-
UniValue result_json;
1341-
bp::opstream stdin_stream;
1342-
bp::ipstream stdout_stream;
1343-
bp::ipstream stderr_stream;
1344-
1345-
if (str_command.empty()) return UniValue::VNULL;
1346-
1347-
bp::child c(
1348-
str_command,
1349-
bp::std_out > stdout_stream,
1350-
bp::std_err > stderr_stream,
1351-
bp::std_in < stdin_stream
1352-
);
1353-
if (!str_std_in.empty()) {
1354-
stdin_stream << str_std_in << std::endl;
1355-
}
1356-
stdin_stream.pipe().close();
1357-
1358-
std::string result;
1359-
std::string error;
1360-
std::getline(stdout_stream, result);
1361-
std::getline(stderr_stream, error);
1362-
1363-
c.wait();
1364-
const int n_error = c.exit_code();
1365-
if (n_error) throw std::runtime_error(strprintf("RunCommandParseJSON error: process(%s) returned %d: %s\n", str_command, n_error, error));
1366-
if (!result_json.read(result)) throw std::runtime_error("Unable to parse JSON: " + result);
1367-
1368-
return result_json;
1369-
#else
1370-
throw std::runtime_error("Compiled without external signing support (required for external signing).");
1371-
#endif // ENABLE_EXTERNAL_SIGNER
1372-
}
13731322

13741323
void SetupEnvironment()
13751324
{

src/util/system.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,6 @@ std::string ShellEscape(const std::string& arg);
107107
#if HAVE_SYSTEM
108108
void runCommand(const std::string& strCommand);
109109
#endif
110-
/**
111-
* Execute a command which returns JSON, and parse the result.
112-
*
113-
* @param str_command The command to execute, including any arguments
114-
* @param str_std_in string to pass to stdin
115-
* @return parsed JSON
116-
*/
117-
UniValue RunCommandParseJSON(const std::string& str_command, const std::string& str_std_in="");
118110

119111
/**
120112
* Most paths passed as configuration arguments are treated as relative to

0 commit comments

Comments
 (0)