Browse Source

[3669] Added status clearing to ProcessSpawn.

Marcin Siodelski 10 years ago
parent
commit
aa7dc61e02

+ 24 - 2
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<pid_t, int>::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));
+}
+
 }
 }

+ 9 - 0
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.

+ 12 - 0
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<std::string> 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