summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRazvan Becheriu <razvan@isc.org>2024-04-22 20:42:28 +0200
committerRazvan Becheriu <razvan@isc.org>2024-04-22 22:00:31 +0200
commitf27254c45d79c7c907780e17da6d20e5f699c1c1 (patch)
tree3e1cbb0c7167ec03b94b1a85a0ba991e9ff2d943
parent[#3315] use stopAndPoll (diff)
downloadkea-f27254c45d79c7c907780e17da6d20e5f699c1c1.tar.xz
kea-f27254c45d79c7c907780e17da6d20e5f699c1c1.zip
[#3315] use enum SpawnMode instead of bool
-rw-r--r--src/hooks/dhcp/mysql_cb/mysql_cb_callouts.cc4
-rw-r--r--src/hooks/dhcp/pgsql_cb/pgsql_cb_callouts.cc4
-rw-r--r--src/hooks/dhcp/run_script/run_script.cc4
-rw-r--r--src/lib/asiolink/process_spawn.cc19
-rw-r--r--src/lib/asiolink/process_spawn.h11
-rw-r--r--src/lib/asiolink/tests/process_spawn_unittest.cc44
-rw-r--r--src/lib/dhcpsrv/memfile_lease_mgr.cc2
-rw-r--r--src/lib/mysql/mysql_connection.cc2
-rw-r--r--src/lib/pgsql/pgsql_connection.cc2
9 files changed, 48 insertions, 44 deletions
diff --git a/src/hooks/dhcp/mysql_cb/mysql_cb_callouts.cc b/src/hooks/dhcp/mysql_cb/mysql_cb_callouts.cc
index 50db009e38..5a16c9e86c 100644
--- a/src/hooks/dhcp/mysql_cb/mysql_cb_callouts.cc
+++ b/src/hooks/dhcp/mysql_cb/mysql_cb_callouts.cc
@@ -68,7 +68,7 @@ int load(LibraryHandle& /* handle */) {
///
/// @param handle callout handle passed to the callout.
/// @return 0 on success, 1 otherwise.
-int dhcp4_srv_configured(CalloutHandle& handle) {
+int dhcp4_srv_configured(CalloutHandle& /* handle */) {
isc::dhcp::MySqlConfigBackendImpl::setIOService(IOServicePtr(new IOService()));
IOServiceMgr::instance().registerIOService(isc::dhcp::MySqlConfigBackendImpl::getIOService());
return (0);
@@ -80,7 +80,7 @@ int dhcp4_srv_configured(CalloutHandle& handle) {
///
/// @param handle callout handle passed to the callout.
/// @return 0 on success, 1 otherwise.
-int dhcp6_srv_configured(CalloutHandle& handle) {
+int dhcp6_srv_configured(CalloutHandle& /* handle */) {
isc::dhcp::MySqlConfigBackendImpl::setIOService(IOServicePtr(new IOService()));
IOServiceMgr::instance().registerIOService(isc::dhcp::MySqlConfigBackendImpl::getIOService());
return (0);
diff --git a/src/hooks/dhcp/pgsql_cb/pgsql_cb_callouts.cc b/src/hooks/dhcp/pgsql_cb/pgsql_cb_callouts.cc
index 78dbdb5d56..ef8842232e 100644
--- a/src/hooks/dhcp/pgsql_cb/pgsql_cb_callouts.cc
+++ b/src/hooks/dhcp/pgsql_cb/pgsql_cb_callouts.cc
@@ -68,7 +68,7 @@ int load(LibraryHandle& /* handle */) {
///
/// @param handle callout handle passed to the callout.
/// @return 0 on success, 1 otherwise.
-int dhcp4_srv_configured(CalloutHandle& handle) {
+int dhcp4_srv_configured(CalloutHandle& /* handle */) {
isc::dhcp::PgSqlConfigBackendImpl::setIOService(IOServicePtr(new IOService()));
IOServiceMgr::instance().registerIOService(isc::dhcp::PgSqlConfigBackendImpl::getIOService());
return (0);
@@ -80,7 +80,7 @@ int dhcp4_srv_configured(CalloutHandle& handle) {
///
/// @param handle callout handle passed to the callout.
/// @return 0 on success, 1 otherwise.
-int dhcp6_srv_configured(CalloutHandle& handle) {
+int dhcp6_srv_configured(CalloutHandle& /* handle */) {
isc::dhcp::PgSqlConfigBackendImpl::setIOService(IOServicePtr(new IOService()));
IOServiceMgr::instance().registerIOService(isc::dhcp::PgSqlConfigBackendImpl::getIOService());
return (0);
diff --git a/src/hooks/dhcp/run_script/run_script.cc b/src/hooks/dhcp/run_script/run_script.cc
index 01ab56ad2a..964c8448ee 100644
--- a/src/hooks/dhcp/run_script/run_script.cc
+++ b/src/hooks/dhcp/run_script/run_script.cc
@@ -31,7 +31,7 @@ RunScriptImpl::configure(LibraryHandle& handle) {
isc_throw(InvalidParameter, "The 'name' parameter must be a string");
}
try {
- ProcessSpawn process(false, name->stringValue());
+ ProcessSpawn process(ProcessSpawn::ASYNC, name->stringValue());
} catch (const isc::Exception& ex) {
isc_throw(InvalidParameter, "Invalid 'name' parameter: " << ex.what());
}
@@ -47,7 +47,7 @@ RunScriptImpl::configure(LibraryHandle& handle) {
void
RunScriptImpl::runScript(const ProcessArgs& args, const ProcessEnvVars& vars) {
- ProcessSpawn process(false, name_, args, vars);
+ ProcessSpawn process(ProcessSpawn::ASYNC, name_, args, vars);
process.spawn(true);
}
diff --git a/src/lib/asiolink/process_spawn.cc b/src/lib/asiolink/process_spawn.cc
index b325b110a9..98964eb7f4 100644
--- a/src/lib/asiolink/process_spawn.cc
+++ b/src/lib/asiolink/process_spawn.cc
@@ -77,14 +77,13 @@ public:
/// @brief Constructor.
///
- /// @param sync enables synchronous mode (spawning thread waits on
- /// child to complete if true)
+ /// @param mode specifies synchronous or asynchronous mode.
/// @param executable A full path to the program to be executed.
/// @param args Arguments for the program to be executed.
/// @param vars Environment variables for the program to be executed.
/// @param inherit_env whether the spawned process will inherit the
/// environment before adding 'vars' on top.
- ProcessSpawnImpl(const bool sync,
+ ProcessSpawnImpl(const ProcessSpawn::SpawnMode mode,
const std::string& executable,
const ProcessArgs& args,
const ProcessEnvVars& vars,
@@ -217,7 +216,7 @@ private:
/// @brief Whether the process is waited immediately after spawning
/// (synchronous) or not (asynchronous).
- bool sync_;
+ ProcessSpawn::SpawnMode mode_;
/// @brief Path to an executable.
std::string executable_;
@@ -248,12 +247,12 @@ void ProcessSpawnImpl::IOSignalSetInitializer::initIOSignalSet(IOServicePtr io_s
static IOSignalSetInitializer init(io_service);
}
-ProcessSpawnImpl::ProcessSpawnImpl(const bool sync,
+ProcessSpawnImpl::ProcessSpawnImpl(const ProcessSpawn::SpawnMode mode,
const std::string& executable,
const ProcessArgs& args,
const ProcessEnvVars& vars,
const bool inherit_env)
- : sync_(sync), executable_(executable), args_(new char*[args.size() + 2]),
+ : mode_(mode), executable_(executable), args_(new char*[args.size() + 2]),
store_(false) {
// Size of vars except the trailing null
@@ -329,7 +328,7 @@ ProcessSpawnImpl::getCommandLine() const {
pid_t
ProcessSpawnImpl::spawn(bool dismiss) {
lock_guard<std::mutex> lk(mutex_);
- if (!sync_) {
+ if (mode_ == ProcessSpawn::ASYNC) {
ProcessSpawnImpl::IOSignalSetInitializer::initIOSignalSet(ProcessSpawn::getIOService());
}
// Create the child
@@ -355,7 +354,7 @@ ProcessSpawnImpl::spawn(bool dismiss) {
process_collection_[this].insert(std::pair<pid_t, ProcessStatePtr>(pid, ProcessStatePtr(new ProcessState())));
}
- if (sync_) {
+ if (mode_ == ProcessSpawn::SYNC) {
waitForProcess(SIGCHLD, pid, /* sync = */ true);
}
@@ -476,12 +475,12 @@ ProcessSpawnImpl::clearState(const pid_t pid) {
IOServicePtr ProcessSpawn::io_service_;
-ProcessSpawn::ProcessSpawn(const bool sync,
+ProcessSpawn::ProcessSpawn(const SpawnMode mode,
const std::string& executable,
const ProcessArgs& args,
const ProcessEnvVars& vars,
const bool inherit_env /* = false */)
- : impl_(new ProcessSpawnImpl(sync, executable, args, vars, inherit_env)) {
+ : impl_(new ProcessSpawnImpl(mode, executable, args, vars, inherit_env)) {
}
std::string
diff --git a/src/lib/asiolink/process_spawn.h b/src/lib/asiolink/process_spawn.h
index f6bcb62ed9..d8944fcf26 100644
--- a/src/lib/asiolink/process_spawn.h
+++ b/src/lib/asiolink/process_spawn.h
@@ -61,16 +61,21 @@ typedef std::vector<std::string> ProcessEnvVars;
class ProcessSpawn : boost::noncopyable {
public:
+ /// @brief The spawn type.
+ enum SpawnMode {
+ ASYNC, // thread continues without waiting for the child to finish.
+ SYNC // thread waits for the child to finish.
+ };
+
/// @brief Constructor.
///
- /// @param sync enables synchronous mode (spawning thread waits on
- /// child to complete if true)
+ /// @param mode specifies synchronous or asynchronous mode.
/// @param executable A full path to the program to be executed.
/// @param args Arguments for the program to be executed.
/// @param vars Environment variables for the program to be executed.
/// @param inherit_env whether the spawned process will inherit the
/// environment before adding 'vars' on top.
- ProcessSpawn(bool sync,
+ ProcessSpawn(const SpawnMode mode,
const std::string& executable,
const ProcessArgs& args = ProcessArgs(),
const ProcessEnvVars& vars = ProcessEnvVars(),
diff --git a/src/lib/asiolink/tests/process_spawn_unittest.cc b/src/lib/asiolink/tests/process_spawn_unittest.cc
index 86ffc221f6..71fdebb695 100644
--- a/src/lib/asiolink/tests/process_spawn_unittest.cc
+++ b/src/lib/asiolink/tests/process_spawn_unittest.cc
@@ -110,7 +110,7 @@ TEST_F(ProcessSpawnTest, spawnWithArgs) {
args.push_back("-e");
args.push_back("64");
- ProcessSpawn process(false, TEST_SCRIPT_SH, args);
+ ProcessSpawn process(ProcessSpawn::ASYNC, TEST_SCRIPT_SH, args);
pid_t pid = 0;
ASSERT_NO_THROW(pid = process.spawn());
@@ -144,7 +144,7 @@ TEST_F(ProcessSpawnTest, spawnWithArgsAndEnvVars) {
args.push_back("TEST_VARIABLE_VALUE");
vars.push_back("TEST_VARIABLE_NAME=TEST_VARIABLE_VALUE");
- ProcessSpawn process(false, TEST_SCRIPT_SH, args, vars);
+ ProcessSpawn process(ProcessSpawn::ASYNC, TEST_SCRIPT_SH, args, vars);
pid_t pid = 0;
ASSERT_NO_THROW(pid = process.spawn());
@@ -176,7 +176,7 @@ TEST_F(ProcessSpawnTest, spawnTwoProcesses) {
vector<string> args;
args.push_back("-p");
- ProcessSpawn process(false, TEST_SCRIPT_SH, args);
+ ProcessSpawn process(ProcessSpawn::ASYNC, TEST_SCRIPT_SH, args);
pid_t pid1 = 0;
ASSERT_NO_THROW(pid1 = process.spawn());
@@ -232,7 +232,7 @@ TEST_F(ProcessSpawnTest, spawnTwoProcesses) {
// This test verifies that the external application can be ran without
// arguments and that the exit code is gathered.
TEST_F(ProcessSpawnTest, spawnNoArgs) {
- ProcessSpawn process(false, TEST_SCRIPT_SH);
+ ProcessSpawn process(ProcessSpawn::ASYNC, TEST_SCRIPT_SH);
pid_t pid = 0;
ASSERT_NO_THROW(pid = process.spawn());
@@ -281,14 +281,14 @@ TEST_F(ProcessSpawnTest, spawnNoArgs) {
// application can't be executed.
TEST_F(ProcessSpawnTest, invalidExecutable) {
std::string expected = "File not found: foo";
- ASSERT_THROW_MSG(ProcessSpawn process(false, "foo"),
+ ASSERT_THROW_MSG(ProcessSpawn process(ProcessSpawn::ASYNC, "foo"),
ProcessSpawnError, expected);
std::string name = INVALID_TEST_SCRIPT_SH;
expected = "File not executable: ";
expected += name;
- ASSERT_THROW_MSG(ProcessSpawn process(false, name),
+ ASSERT_THROW_MSG(ProcessSpawn process(ProcessSpawn::ASYNC, name),
ProcessSpawnError, expected);
}
@@ -302,7 +302,7 @@ TEST_F(ProcessSpawnTest, getCommandLine) {
args.push_back("-y");
args.push_back("foo");
args.push_back("bar");
- ProcessSpawn process(false, TEST_SCRIPT_SH, args);
+ ProcessSpawn process(ProcessSpawn::ASYNC, TEST_SCRIPT_SH, args);
std::string expected = TEST_SCRIPT_SH;
expected += " -x -y foo bar";
EXPECT_EQ(expected, process.getCommandLine());
@@ -310,7 +310,7 @@ TEST_F(ProcessSpawnTest, getCommandLine) {
{
// Case 2: no arguments.
- ProcessSpawn process(false, TEST_SCRIPT_SH);
+ ProcessSpawn process(ProcessSpawn::ASYNC, TEST_SCRIPT_SH);
EXPECT_EQ(TEST_SCRIPT_SH, process.getCommandLine());
}
}
@@ -324,7 +324,7 @@ TEST_F(ProcessSpawnTest, isRunning) {
args.push_back("-s");
args.push_back("10");
- ProcessSpawn process(false, TEST_SCRIPT_SH, args);
+ ProcessSpawn process(ProcessSpawn::ASYNC, TEST_SCRIPT_SH, args);
pid_t pid = 0;
ASSERT_NO_THROW(pid = process.spawn());
@@ -358,7 +358,7 @@ TEST_F(ProcessSpawnTest, inheritEnv) {
ProcessEnvVars vars{"VAR=value"};
- ProcessSpawn process(false, TEST_SCRIPT_SH, args, vars,
+ ProcessSpawn process(ProcessSpawn::ASYNC, TEST_SCRIPT_SH, args, vars,
/* inherit_env = */ true);
pid_t pid = 0;
ASSERT_NO_THROW(pid = process.spawn());
@@ -392,7 +392,7 @@ TEST_F(ProcessSpawnTest, inheritEnvWithParentVar) {
ProcessEnvVars vars{"VAR=value"};
- ProcessSpawn process(false, TEST_SCRIPT_SH, args, vars,
+ ProcessSpawn process(ProcessSpawn::ASYNC, TEST_SCRIPT_SH, args, vars,
/* inherit_env = */ true);
pid_t pid = 0;
ASSERT_NO_THROW(pid = process.spawn());
@@ -424,7 +424,7 @@ TEST_F(ProcessSpawnTest, spawnWithArgsSync) {
args.push_back("-e");
args.push_back("64");
- ProcessSpawn process(true, TEST_SCRIPT_SH, args);
+ ProcessSpawn process(ProcessSpawn::SYNC, TEST_SCRIPT_SH, args);
pid_t pid = 0;
ASSERT_NO_THROW(pid = process.spawn());
@@ -442,7 +442,7 @@ TEST_F(ProcessSpawnTest, spawnWithArgsAndEnvVarsSync) {
args.push_back("TEST_VARIABLE_VALUE");
vars.push_back("TEST_VARIABLE_NAME=TEST_VARIABLE_VALUE");
- ProcessSpawn process(true, TEST_SCRIPT_SH, args, vars);
+ ProcessSpawn process(ProcessSpawn::SYNC, TEST_SCRIPT_SH, args, vars);
pid_t pid = 0;
ASSERT_NO_THROW(pid = process.spawn());
@@ -458,7 +458,7 @@ TEST_F(ProcessSpawnTest, spawnTwoProcessesSync) {
vector<string> args;
args.push_back("-p");
- ProcessSpawn process(true, TEST_SCRIPT_SH, args);
+ ProcessSpawn process(ProcessSpawn::SYNC, TEST_SCRIPT_SH, args);
pid_t pid1 = 0;
ASSERT_NO_THROW(pid1 = process.spawn());
@@ -481,7 +481,7 @@ TEST_F(ProcessSpawnTest, spawnTwoProcessesSync) {
// This test verifies that the external application can be ran synchronously
// without arguments and that the exit code is gathered.
TEST_F(ProcessSpawnTest, spawnNoArgsSync) {
- ProcessSpawn process(true, TEST_SCRIPT_SH);
+ ProcessSpawn process(ProcessSpawn::SYNC, TEST_SCRIPT_SH);
pid_t pid = 0;
ASSERT_NO_THROW(pid = process.spawn());
@@ -497,14 +497,14 @@ TEST_F(ProcessSpawnTest, spawnNoArgsSync) {
// application can't be executed synchronously.
TEST_F(ProcessSpawnTest, invalidExecutableSync) {
std::string expected = "File not found: foo";
- ASSERT_THROW_MSG(ProcessSpawn process(true, "foo"),
+ ASSERT_THROW_MSG(ProcessSpawn process(ProcessSpawn::SYNC, "foo"),
ProcessSpawnError, expected);
std::string name = INVALID_TEST_SCRIPT_SH;
expected = "File not executable: ";
expected += name;
- ASSERT_THROW_MSG(ProcessSpawn process(true, name),
+ ASSERT_THROW_MSG(ProcessSpawn process(ProcessSpawn::SYNC, name),
ProcessSpawnError, expected);
}
@@ -518,7 +518,7 @@ TEST_F(ProcessSpawnTest, getCommandLineSync) {
args.push_back("-y");
args.push_back("foo");
args.push_back("bar");
- ProcessSpawn process(true, TEST_SCRIPT_SH, args);
+ ProcessSpawn process(ProcessSpawn::SYNC, TEST_SCRIPT_SH, args);
std::string expected = TEST_SCRIPT_SH;
expected += " -x -y foo bar";
EXPECT_EQ(expected, process.getCommandLine());
@@ -526,7 +526,7 @@ TEST_F(ProcessSpawnTest, getCommandLineSync) {
{
// Case 2: no arguments.
- ProcessSpawn process(true, TEST_SCRIPT_SH);
+ ProcessSpawn process(ProcessSpawn::SYNC, TEST_SCRIPT_SH);
EXPECT_EQ(TEST_SCRIPT_SH, process.getCommandLine());
}
}
@@ -534,7 +534,7 @@ TEST_F(ProcessSpawnTest, getCommandLineSync) {
// This test verifies that the synchronous process reports as not running after
// it was spawned.
TEST_F(ProcessSpawnTest, isRunningSync) {
- ProcessSpawn process(true, TEST_SCRIPT_SH);
+ ProcessSpawn process(ProcessSpawn::SYNC, TEST_SCRIPT_SH);
pid_t pid = 0;
ASSERT_NO_THROW(pid = process.spawn());
@@ -549,7 +549,7 @@ TEST_F(ProcessSpawnTest, inheritEnvSync) {
ProcessEnvVars vars{"VAR=value"};
- ProcessSpawn process(true, TEST_SCRIPT_SH, args, vars,
+ ProcessSpawn process(ProcessSpawn::SYNC, TEST_SCRIPT_SH, args, vars,
/* inherit_env = */ true);
pid_t pid = 0;
ASSERT_NO_THROW(pid = process.spawn());
@@ -567,7 +567,7 @@ TEST_F(ProcessSpawnTest, inheritEnvWithParentVarSync) {
ProcessEnvVars vars{"VAR=value"};
- ProcessSpawn process(true, TEST_SCRIPT_SH, args, vars,
+ ProcessSpawn process(ProcessSpawn::SYNC, TEST_SCRIPT_SH, args, vars,
/* inherit_env = */ true);
pid_t pid = 0;
ASSERT_NO_THROW(pid = process.spawn());
diff --git a/src/lib/dhcpsrv/memfile_lease_mgr.cc b/src/lib/dhcpsrv/memfile_lease_mgr.cc
index aa534665b2..37d670645b 100644
--- a/src/lib/dhcpsrv/memfile_lease_mgr.cc
+++ b/src/lib/dhcpsrv/memfile_lease_mgr.cc
@@ -209,7 +209,7 @@ LFCSetup::setup(const uint32_t lfc_interval,
args.push_back("ignored-path");
// Create the process (do not start it yet).
- process_.reset(new ProcessSpawn(false, executable, args));
+ process_.reset(new ProcessSpawn(ProcessSpawn::ASYNC, executable, args));
// If we've been told to run it once now, invoke the callback directly.
if (run_once_now) {
diff --git a/src/lib/mysql/mysql_connection.cc b/src/lib/mysql/mysql_connection.cc
index 052e8539a2..4ce3d15aa2 100644
--- a/src/lib/mysql/mysql_connection.cc
+++ b/src/lib/mysql/mysql_connection.cc
@@ -434,7 +434,7 @@ MySqlConnection::initializeSchema(const ParameterMap& parameters) {
kea_admin_parameters.insert(kea_admin_parameters.begin(), "db-init");
// Run.
- ProcessSpawn kea_admin(true, KEA_ADMIN_, kea_admin_parameters, vars,
+ ProcessSpawn kea_admin(ProcessSpawn::SYNC, KEA_ADMIN_, kea_admin_parameters, vars,
/* inherit_env = */ true);
DB_LOG_INFO(MYSQL_INITIALIZE_SCHEMA).arg(kea_admin.getCommandLine());
pid_t const pid(kea_admin.spawn());
diff --git a/src/lib/pgsql/pgsql_connection.cc b/src/lib/pgsql/pgsql_connection.cc
index b7a45628c7..e63a699a1a 100644
--- a/src/lib/pgsql/pgsql_connection.cc
+++ b/src/lib/pgsql/pgsql_connection.cc
@@ -237,7 +237,7 @@ PgSqlConnection::initializeSchema(const ParameterMap& parameters) {
kea_admin_parameters.insert(kea_admin_parameters.begin(), "db-init");
// Run.
- ProcessSpawn kea_admin(true, KEA_ADMIN_, kea_admin_parameters, vars,
+ ProcessSpawn kea_admin(ProcessSpawn::SYNC, KEA_ADMIN_, kea_admin_parameters, vars,
/* inherit_env = */ true);
DB_LOG_INFO(PGSQL_INITIALIZE_SCHEMA).arg(kea_admin.getCommandLine());
pid_t const pid(kea_admin.spawn());