From: Marcin Siodelski Date: Thu, 5 Feb 2015 15:15:26 +0000 (+0100) Subject: [3669] Added status clearing to ProcessSpawn. X-Git-Tag: trac3712_base~13^2~6 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=aa7dc61e02a11dda099598d26d4efe7d40d09d1b;p=thirdparty%2Fkea.git [3669] Added status clearing to ProcessSpawn. --- diff --git a/src/lib/util/process_spawn.cc b/src/lib/util/process_spawn.cc index 0986413db6..b09a898113 100644 --- a/src/lib/util/process_spawn.cc +++ b/src/lib/util/process_spawn.cc @@ -81,6 +81,15 @@ public: /// @return Exit code of the process. int getExitStatus(const pid_t pid) const; + /// @brief Removes the status of the process with a specified PID. + /// + /// This method removes the status of the process with a specified PID. + /// If the process is still running, the status is not removed and the + /// exception is thrown. + /// + /// @param pid A process pid. + void clearStatus(const pid_t pid); + private: /// @brief Copies the argument specified as a C++ string to the new @@ -211,8 +220,8 @@ int ProcessSpawnImpl::getExitStatus(const pid_t pid) const { std::map::const_iterator status = process_status_.find(pid); if (status == process_status_.end()) { - isc_throw(BadValue, "the process with the pid '" << pid - << "' hasn't been spawned and it status cannnot be" + isc_throw(InvalidOperation, "the process with the pid '" << pid + << "' hasn't been spawned and it status cannot be" " returned"); } return (WEXITSTATUS(status->second)); @@ -244,6 +253,14 @@ ProcessSpawnImpl::waitForProcess(int signum) { return (false); } +void +ProcessSpawnImpl::clearStatus(const pid_t pid) { + if (isRunning(pid)) { + isc_throw(InvalidOperation, "unable to remove the status for the" + "process (pid: " << pid << ") which is still running"); + } + process_status_.erase(pid); +} ProcessSpawn::ProcessSpawn(const std::string& executable, const ProcessArgs& args) @@ -279,5 +296,10 @@ ProcessSpawn::getExitStatus(const pid_t pid) const { return (impl_->getExitStatus(pid)); } +void +ProcessSpawn::clearStatus(const pid_t pid) { + return (impl_->clearStatus(pid)); +} + } } diff --git a/src/lib/util/process_spawn.h b/src/lib/util/process_spawn.h index 977fe39456..84390c99dd 100644 --- a/src/lib/util/process_spawn.h +++ b/src/lib/util/process_spawn.h @@ -106,6 +106,15 @@ public: /// @return Exit code of the process. int getExitStatus(const pid_t pid) const; + /// @brief Removes the status of the process with a specified PID. + /// + /// This method removes the status of the process with a specified PID. + /// If the process is still running, the status is not removed and the + /// exception is thrown. + /// + /// @param pid A process pid. + void clearStatus(const pid_t pid); + private: /// @brief A pointer to the implementation of this class. diff --git a/src/lib/util/tests/process_spawn_unittest.cc b/src/lib/util/tests/process_spawn_unittest.cc index 0def8dfe3b..a23646fcfc 100644 --- a/src/lib/util/tests/process_spawn_unittest.cc +++ b/src/lib/util/tests/process_spawn_unittest.cc @@ -71,6 +71,8 @@ TEST(ProcessSpawn, spawnWithArgs) { // This test verifies that the single ProcessSpawn object can be used // to start two processes and that their status codes can be gathered. +// It also checks that it is possible to clear the status of the +// process. TEST(ProcessSpawn, spawnTwoProcesses) { std::vector args; args.push_back("-p"); @@ -84,6 +86,16 @@ TEST(ProcessSpawn, spawnTwoProcesses) { ASSERT_TRUE(waitForProcess(process, pid2, 2)); EXPECT_NE(process.getExitStatus(pid1), process.getExitStatus(pid2)); + + // Clear the status of the first process. An atttempt to get the status + // for the cleared process should result in exception. But, there should + // be no exception for the second process. + process.clearStatus(pid1); + EXPECT_THROW(process.getExitStatus(pid1), InvalidOperation); + EXPECT_NO_THROW(process.getExitStatus(pid2)); + + process.clearStatus(pid2); + EXPECT_THROW(process.getExitStatus(pid2), InvalidOperation); } // This test verifies that the external application can be ran without