Browse Source

[1960] Added support for wrapped program execution.

Marcin Siodelski 12 years ago
parent
commit
f07fdbfa1a
2 changed files with 51 additions and 0 deletions
  1. 38 0
      tests/tools/perfdhcp/test_control.cc
  2. 13 0
      tests/tools/perfdhcp/test_control.h

+ 38 - 0
tests/tools/perfdhcp/test_control.cc

@@ -19,6 +19,7 @@
 #include <stdint.h>
 #include <unistd.h>
 #include <signal.h>
+#include <sys/wait.h>
 
 #include <boost/date_time/posix_time/posix_time.hpp>
 
@@ -467,6 +468,14 @@ TestControl::getTemplateBuffer(const size_t idx) const {
 }
 
 void
+TestControl::handleChild(int) {
+    int status = 0;
+    while (wait3(&status, WNOHANG, NULL) > 0) {
+        // continue
+    }
+}
+
+void
 TestControl::handleInterrupt(int) {
     interrupted_ = true;
 }
@@ -972,6 +981,12 @@ TestControl::run() {
             }
         }
     }
+
+    // Fork and run command specified with -w<wrapped-command>
+    if (!options.getWrapped().empty()) {
+        runWrapped();
+    }
+
     // Initialize Statistics Manager. Release previous if any.
     initializeStatsMgr();
     for (;;) {
@@ -1028,6 +1043,12 @@ TestControl::run() {
         }
     }
     printStats();
+
+    if (!options.getWrapped().empty()) {
+        const bool do_stop = true;
+        runWrapped(do_stop);
+    }
+
     // Print server id.
     if (testDiags('s') && (first_packet_serverid_.size() > 0)) {
         std::cout << "Server id: " << vector2Hex(first_packet_serverid_) << std::endl;
@@ -1039,6 +1060,23 @@ TestControl::run() {
 }
 
 void
+TestControl::runWrapped(bool do_stop /*= false */) const {
+    CommandOptions& options = CommandOptions::instance();
+    if (!options.getWrapped().empty()) {
+        pid_t pid = 0;
+        signal(SIGCHLD, handleChild);
+        pid = fork();
+        if (pid < 0) {
+            isc_throw(Unexpected, "unable to fork");
+        } else if (pid == 0) {
+            execlp(options.getWrapped().c_str(), 
+                   do_stop ? "stop" : "start",
+                   NULL);
+        }
+    }
+}
+
+void
 TestControl::sendDiscover4(const TestControlSocket& socket,
                            const bool preload /*= false*/) {
     last_sent_ = microsec_clock::universal_time();

+ 13 - 0
tests/tools/perfdhcp/test_control.h

@@ -746,6 +746,14 @@ private:
     /// \return number of sent packets.
     uint64_t getSentPacketsNum(const ExchangeType xchg_type) const;
 
+    /// \brief Handle child signal.
+    ///
+    /// Function handles child signal by waiting for
+    /// the process to complete.
+    ///
+    /// \param sig signal (ignored)
+    static void handleChild(int sig);
+
     /// \brief Handle interrupt signal.
     ///
     /// Function sets flag indicating that program has been
@@ -766,6 +774,11 @@ private:
     /// to template_buffers_ vector.
     void readPacketTemplate(const std::string& file_name);
 
+    /// \brief Run wrapped command.
+    ///
+    /// \param do_stop execute wrapped command with "stop" argument.
+    void runWrapped(bool do_stop = false) const;
+
     /// \brief Convert vector in hexadecimal string.
     ///
     /// \todo Consider moving this function to src/lib/util.