diff options
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/asiolink/process_spawn.cc | 60 | ||||
-rw-r--r-- | src/lib/asiolink/process_spawn.h | 39 | ||||
-rw-r--r-- | src/lib/asiolink/tests/process_spawn_unittest.cc | 45 | ||||
-rw-r--r-- | src/lib/dhcpsrv/memfile_lease_mgr.cc | 3 | ||||
-rw-r--r-- | src/lib/dhcpsrv/tests/memfile_lease_mgr_unittest.cc | 1 | ||||
-rw-r--r-- | src/lib/mysql/mysql_connection.cc | 2 | ||||
-rw-r--r-- | src/lib/pgsql/pgsql_connection.cc | 2 |
7 files changed, 68 insertions, 84 deletions
diff --git a/src/lib/asiolink/process_spawn.cc b/src/lib/asiolink/process_spawn.cc index 9f55b51d28..32e0ec7644 100644 --- a/src/lib/asiolink/process_spawn.cc +++ b/src/lib/asiolink/process_spawn.cc @@ -77,20 +77,18 @@ public: /// @brief Constructor. /// - /// @param io_service The IOService which handles signal handlers. + /// @param sync enables synchronous mode (spawning thread waits on + /// child to complete if true) /// @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. - /// @param sync enables synchronous mode (spawning thread waits on - /// child to complete if true) - ProcessSpawnImpl(IOServicePtr io_service, + /// environment before adding 'vars' on top. + ProcessSpawnImpl(const bool sync, const std::string& executable, const ProcessArgs& args, const ProcessEnvVars& vars, - const bool inherit_env, - const bool sync); + const bool inherit_env); /// @brief Destructor. ~ProcessSpawnImpl(); @@ -211,11 +209,16 @@ private: /// for any child process /// @param sync whether this function is called immediately after spawning /// (synchronous) or not (asynchronous, default). - static void waitForProcess(int signum, pid_t const wpid = -1, bool const sync = false); + static void waitForProcess(int signum, pid_t const wpid = -1, + bool const sync = false); /// @brief A map holding the status codes of executed processes. static ProcessCollection process_collection_; + /// @brief Whether the process is waited immediately after spawning + /// (synchronous) or not (asynchronous, default). + bool sync_; + /// @brief Path to an executable. std::string executable_; @@ -236,13 +239,6 @@ private: /// @brief Mutex to protect internal state. static std::mutex mutex_; - - /// @brief The IOService which handles IO operations. - IOServicePtr io_service_; - - /// @brief Whether the process is waited immediately after spawning - /// (synchronous) or not (asynchronous, default). - bool sync_; }; ProcessCollection ProcessSpawnImpl::process_collection_; @@ -252,14 +248,13 @@ void ProcessSpawnImpl::IOSignalSetInitializer::initIOSignalSet(IOServicePtr io_s static IOSignalSetInitializer init(io_service); } -ProcessSpawnImpl::ProcessSpawnImpl(IOServicePtr io_service, +ProcessSpawnImpl::ProcessSpawnImpl(const bool sync, const std::string& executable, const ProcessArgs& args, const ProcessEnvVars& vars, - const bool inherit_env, - const bool sync) - : executable_(executable), args_(new char*[args.size() + 2]), - store_(false), io_service_(io_service), sync_(sync) { + const bool inherit_env) + : sync_(sync), executable_(executable), args_(new char*[args.size() + 2]), + store_(false) { // Size of vars except the trailing null size_t vars_size; @@ -335,7 +330,7 @@ pid_t ProcessSpawnImpl::spawn(bool dismiss) { lock_guard<std::mutex> lk(mutex_); if (!sync_) { - ProcessSpawnImpl::IOSignalSetInitializer::initIOSignalSet(io_service_); + ProcessSpawnImpl::IOSignalSetInitializer::initIOSignalSet(ProcessSpawn::getIOService()); } // Create the child pid_t pid = fork(); @@ -479,29 +474,14 @@ ProcessSpawnImpl::clearState(const pid_t pid) { } } -ProcessSpawn::ProcessSpawn(IOServicePtr io_service, - const std::string& executable, - const ProcessArgs& args, - const ProcessEnvVars& vars, - const bool inherit_env /* = false */) - : impl_(new ProcessSpawnImpl(io_service, - executable, - args, - vars, - inherit_env, - /* sync = */ false)) { -} +IOServicePtr ProcessSpawn::io_service_; -ProcessSpawn::ProcessSpawn(const std::string& executable, +ProcessSpawn::ProcessSpawn(const bool sync, + const std::string& executable, const ProcessArgs& args, const ProcessEnvVars& vars, const bool inherit_env /* = false */) - : impl_(new ProcessSpawnImpl(IOServicePtr(new IOService()), - executable, - args, - vars, - inherit_env, - /* sync = */ true)) { + : impl_(new ProcessSpawnImpl(sync, executable, args, vars, inherit_env)) { } std::string diff --git a/src/lib/asiolink/process_spawn.h b/src/lib/asiolink/process_spawn.h index 5cdb70721f..f6bcb62ed9 100644 --- a/src/lib/asiolink/process_spawn.h +++ b/src/lib/asiolink/process_spawn.h @@ -63,33 +63,19 @@ public: /// @brief Constructor. /// - /// @param io_service The IOService which handles signal handlers. + /// @param sync enables synchronous mode (spawning thread waits on + /// child to complete if true) /// @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(isc::asiolink::IOServicePtr io_service, + /// environment before adding 'vars' on top. + ProcessSpawn(bool sync, const std::string& executable, const ProcessArgs& args = ProcessArgs(), const ProcessEnvVars& vars = ProcessEnvVars(), const bool inherit_env = false); - /// @brief Constructor for synchronous spawn and wait. - /// - /// Abstracts away the IO service detail since the caller is not - /// required to interact with it in sync 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(const std::string& executable, - const ProcessArgs& args = ProcessArgs(), - const ProcessEnvVars& vars = ProcessEnvVars(), - const bool inherit_env = false); - /// @brief Destructor. ~ProcessSpawn() = default; @@ -155,8 +141,25 @@ public: /// @param pid A process pid. void clearState(const pid_t pid); + /// @brief Get the I/O service. + /// + /// @return the I/O service. + static isc::asiolink::IOServicePtr getIOService() { + return (io_service_); + } + + /// @brief Set the I/O service. + /// + /// @param io_service the I/O service. + static void setIOService(isc::asiolink::IOServicePtr io_service) { + io_service_ = io_service; + } + private: + /// @brief The IOService object, used for all ASIO operations. + static isc::asiolink::IOServicePtr io_service_; + /// @brief A smart pointer to the implementation of this class. ProcessSpawnImplPtr impl_; }; diff --git a/src/lib/asiolink/tests/process_spawn_unittest.cc b/src/lib/asiolink/tests/process_spawn_unittest.cc index 63355fb8de..e7b66888f4 100644 --- a/src/lib/asiolink/tests/process_spawn_unittest.cc +++ b/src/lib/asiolink/tests/process_spawn_unittest.cc @@ -56,6 +56,7 @@ public: ProcessSpawnTest() : io_service_(getIOService()), test_timer_(io_service_), test_time_ms_(0), io_signal_set_(), processed_signals_() { + ProcessSpawn::setIOService(getIOService()); io_signal_set_.reset(new IOSignalSet(io_service_, std::bind(&ProcessSpawnTest::processSignal, @@ -114,7 +115,7 @@ TEST_F(ProcessSpawnTest, spawnWithArgs) { args.push_back("-e"); args.push_back("64"); - ProcessSpawn process(io_service_, TEST_SCRIPT_SH, args); + ProcessSpawn process(false, TEST_SCRIPT_SH, args); pid_t pid = 0; ASSERT_NO_THROW(pid = process.spawn()); @@ -148,7 +149,7 @@ TEST_F(ProcessSpawnTest, spawnWithArgsAndEnvVars) { args.push_back("TEST_VARIABLE_VALUE"); vars.push_back("TEST_VARIABLE_NAME=TEST_VARIABLE_VALUE"); - ProcessSpawn process(io_service_, TEST_SCRIPT_SH, args, vars); + ProcessSpawn process(false, TEST_SCRIPT_SH, args, vars); pid_t pid = 0; ASSERT_NO_THROW(pid = process.spawn()); @@ -180,7 +181,7 @@ TEST_F(ProcessSpawnTest, spawnTwoProcesses) { vector<string> args; args.push_back("-p"); - ProcessSpawn process(io_service_, TEST_SCRIPT_SH, args); + ProcessSpawn process(false, TEST_SCRIPT_SH, args); pid_t pid1 = 0; ASSERT_NO_THROW(pid1 = process.spawn()); @@ -236,7 +237,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(io_service_, TEST_SCRIPT_SH); + ProcessSpawn process(false, TEST_SCRIPT_SH); pid_t pid = 0; ASSERT_NO_THROW(pid = process.spawn()); @@ -285,14 +286,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(io_service_, "foo"), + ASSERT_THROW_MSG(ProcessSpawn process(false, "foo"), ProcessSpawnError, expected); std::string name = INVALID_TEST_SCRIPT_SH; expected = "File not executable: "; expected += name; - ASSERT_THROW_MSG(ProcessSpawn process(io_service_, name), + ASSERT_THROW_MSG(ProcessSpawn process(false, name), ProcessSpawnError, expected); } @@ -306,7 +307,7 @@ TEST_F(ProcessSpawnTest, getCommandLine) { args.push_back("-y"); args.push_back("foo"); args.push_back("bar"); - ProcessSpawn process(io_service_, TEST_SCRIPT_SH, args); + ProcessSpawn process(false, TEST_SCRIPT_SH, args); std::string expected = TEST_SCRIPT_SH; expected += " -x -y foo bar"; EXPECT_EQ(expected, process.getCommandLine()); @@ -314,7 +315,7 @@ TEST_F(ProcessSpawnTest, getCommandLine) { { // Case 2: no arguments. - ProcessSpawn process(io_service_, TEST_SCRIPT_SH); + ProcessSpawn process(false, TEST_SCRIPT_SH); EXPECT_EQ(TEST_SCRIPT_SH, process.getCommandLine()); } } @@ -328,7 +329,7 @@ TEST_F(ProcessSpawnTest, isRunning) { args.push_back("-s"); args.push_back("10"); - ProcessSpawn process(io_service_, TEST_SCRIPT_SH, args); + ProcessSpawn process(false, TEST_SCRIPT_SH, args); pid_t pid = 0; ASSERT_NO_THROW(pid = process.spawn()); @@ -362,7 +363,7 @@ TEST_F(ProcessSpawnTest, inheritEnv) { ProcessEnvVars vars{"VAR=value"}; - ProcessSpawn process(io_service_, TEST_SCRIPT_SH, args, vars, + ProcessSpawn process(false, TEST_SCRIPT_SH, args, vars, /* inherit_env = */ true); pid_t pid = 0; ASSERT_NO_THROW(pid = process.spawn()); @@ -396,7 +397,7 @@ TEST_F(ProcessSpawnTest, inheritEnvWithParentVar) { ProcessEnvVars vars{"VAR=value"}; - ProcessSpawn process(io_service_, TEST_SCRIPT_SH, args, vars, + ProcessSpawn process(false, TEST_SCRIPT_SH, args, vars, /* inherit_env = */ true); pid_t pid = 0; ASSERT_NO_THROW(pid = process.spawn()); @@ -428,7 +429,7 @@ TEST_F(ProcessSpawnTest, spawnWithArgsSync) { args.push_back("-e"); args.push_back("64"); - ProcessSpawn process(TEST_SCRIPT_SH, args); + ProcessSpawn process(true, TEST_SCRIPT_SH, args); pid_t pid = 0; ASSERT_NO_THROW(pid = process.spawn()); @@ -446,7 +447,7 @@ TEST_F(ProcessSpawnTest, spawnWithArgsAndEnvVarsSync) { args.push_back("TEST_VARIABLE_VALUE"); vars.push_back("TEST_VARIABLE_NAME=TEST_VARIABLE_VALUE"); - ProcessSpawn process(TEST_SCRIPT_SH, args, vars); + ProcessSpawn process(true, TEST_SCRIPT_SH, args, vars); pid_t pid = 0; ASSERT_NO_THROW(pid = process.spawn()); @@ -462,7 +463,7 @@ TEST_F(ProcessSpawnTest, spawnTwoProcessesSync) { vector<string> args; args.push_back("-p"); - ProcessSpawn process(TEST_SCRIPT_SH, args); + ProcessSpawn process(true, TEST_SCRIPT_SH, args); pid_t pid1 = 0; ASSERT_NO_THROW(pid1 = process.spawn()); @@ -485,7 +486,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(TEST_SCRIPT_SH); + ProcessSpawn process(true, TEST_SCRIPT_SH); pid_t pid = 0; ASSERT_NO_THROW(pid = process.spawn()); @@ -501,14 +502,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("foo"), + ASSERT_THROW_MSG(ProcessSpawn process(true, "foo"), ProcessSpawnError, expected); std::string name = INVALID_TEST_SCRIPT_SH; expected = "File not executable: "; expected += name; - ASSERT_THROW_MSG(ProcessSpawn process(name), + ASSERT_THROW_MSG(ProcessSpawn process(true, name), ProcessSpawnError, expected); } @@ -522,7 +523,7 @@ TEST_F(ProcessSpawnTest, getCommandLineSync) { args.push_back("-y"); args.push_back("foo"); args.push_back("bar"); - ProcessSpawn process(TEST_SCRIPT_SH, args); + ProcessSpawn process(true, TEST_SCRIPT_SH, args); std::string expected = TEST_SCRIPT_SH; expected += " -x -y foo bar"; EXPECT_EQ(expected, process.getCommandLine()); @@ -530,7 +531,7 @@ TEST_F(ProcessSpawnTest, getCommandLineSync) { { // Case 2: no arguments. - ProcessSpawn process(TEST_SCRIPT_SH); + ProcessSpawn process(true, TEST_SCRIPT_SH); EXPECT_EQ(TEST_SCRIPT_SH, process.getCommandLine()); } } @@ -538,7 +539,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(TEST_SCRIPT_SH); + ProcessSpawn process(true, TEST_SCRIPT_SH); pid_t pid = 0; ASSERT_NO_THROW(pid = process.spawn()); @@ -553,7 +554,7 @@ TEST_F(ProcessSpawnTest, inheritEnvSync) { ProcessEnvVars vars{"VAR=value"}; - ProcessSpawn process(TEST_SCRIPT_SH, args, vars, + ProcessSpawn process(true, TEST_SCRIPT_SH, args, vars, /* inherit_env = */ true); pid_t pid = 0; ASSERT_NO_THROW(pid = process.spawn()); @@ -571,7 +572,7 @@ TEST_F(ProcessSpawnTest, inheritEnvWithParentVarSync) { ProcessEnvVars vars{"VAR=value"}; - ProcessSpawn process(TEST_SCRIPT_SH, args, vars, + ProcessSpawn process(true, 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 c89b02ee28..aa534665b2 100644 --- a/src/lib/dhcpsrv/memfile_lease_mgr.cc +++ b/src/lib/dhcpsrv/memfile_lease_mgr.cc @@ -7,7 +7,6 @@ #include <config.h> #include <asiolink/addr_utilities.h> -#include <database/database_connection.h> #include <dhcpsrv/cfg_consistency.h> #include <dhcpsrv/cfgmgr.h> #include <dhcpsrv/dhcpsrv_exceptions.h> @@ -210,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(DatabaseConnection::getIOService(), executable, args)); + process_.reset(new ProcessSpawn(false, 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/dhcpsrv/tests/memfile_lease_mgr_unittest.cc b/src/lib/dhcpsrv/tests/memfile_lease_mgr_unittest.cc index 7e2c656017..ecec43ddaa 100644 --- a/src/lib/dhcpsrv/tests/memfile_lease_mgr_unittest.cc +++ b/src/lib/dhcpsrv/tests/memfile_lease_mgr_unittest.cc @@ -118,6 +118,7 @@ public: timer_mgr_->setIOService(io_service_); DatabaseConnection::setIOService(io_service_); + ProcessSpawn::setIOService(io_service_); std::ostringstream s; s << KEA_LFC_BUILD_DIR << "/kea-lfc"; diff --git a/src/lib/mysql/mysql_connection.cc b/src/lib/mysql/mysql_connection.cc index f25029cdc1..052e8539a2 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(KEA_ADMIN_, kea_admin_parameters, vars, + ProcessSpawn kea_admin(true, 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 68b7a4cba4..b7a45628c7 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(KEA_ADMIN_, kea_admin_parameters, vars, + ProcessSpawn kea_admin(true, 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()); |