BIND 10 trac2875, updated. ed5835fe2d9ad196d5287674f9e9448e4dad26e6 [2875] Helper class to launch subprocess

BIND 10 source code commits bind10-changes at lists.isc.org
Thu Jul 18 09:02:55 UTC 2013


The branch, trac2875 has been updated
       via  ed5835fe2d9ad196d5287674f9e9448e4dad26e6 (commit)
      from  5739858315f803a8be995976533c9eed3dc87797 (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
commit ed5835fe2d9ad196d5287674f9e9448e4dad26e6
Author: Michal 'vorner' Vaner <michal.vaner at nic.cz>
Date:   Thu Jul 18 11:02:38 2013 +0200

    [2875] Helper class to launch subprocess

-----------------------------------------------------------------------

Summary of changes:
 src/bin/resolver/bench/Makefile.am                 |    1 +
 src/bin/resolver/bench/subprocess.cc               |   67 ++++++++++++++++++++
 .../bench/{naive_resolver.h => subprocess.h}       |   39 +++++++-----
 3 files changed, 92 insertions(+), 15 deletions(-)
 create mode 100644 src/bin/resolver/bench/subprocess.cc
 copy src/bin/resolver/bench/{naive_resolver.h => subprocess.h} (51%)

-----------------------------------------------------------------------
diff --git a/src/bin/resolver/bench/Makefile.am b/src/bin/resolver/bench/Makefile.am
index ece73a3..109d6a6 100644
--- a/src/bin/resolver/bench/Makefile.am
+++ b/src/bin/resolver/bench/Makefile.am
@@ -18,6 +18,7 @@ resolver_bench_SOURCES = main.cc
 resolver_bench_SOURCES += fake_resolution.h fake_resolution.cc
 resolver_bench_SOURCES += dummy_work.h dummy_work.cc
 resolver_bench_SOURCES += naive_resolver.h naive_resolver.cc
+resolver_bench_SOURCES += subprocess.h subprocess.cc
 
 resolver_bench_LDADD = $(top_builddir)/src/lib/exceptions/libb10-exceptions.la
 resolver_bench_LDADD += $(top_builddir)/src/lib/asiolink/libb10-asiolink.la
diff --git a/src/bin/resolver/bench/subprocess.cc b/src/bin/resolver/bench/subprocess.cc
new file mode 100644
index 0000000..d3e8c95
--- /dev/null
+++ b/src/bin/resolver/bench/subprocess.cc
@@ -0,0 +1,67 @@
+// Copyright (C) 2013  Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+#include <resolver/bench/subprocess.h>
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/wait.h>
+#include <cstdlib>
+#include <iostream>
+
+using namespace std;
+
+namespace isc {
+namespace resolver {
+namespace bench {
+
+Subprocess::Subprocess(const boost::function<void(int)>& main) :
+    channel_(-1),
+    pid(-1)
+{
+    int sockets[2];
+    int result = socketpair(AF_UNIX, SOCK_STREAM, 0, sockets);
+    assert(result == 0);
+    pid = fork();
+    assert(pid != -1);
+    if (pid) {
+        // In the parent. Close the child side of socket, return to caller.
+        close(sockets[1]);
+        channel_ = sockets[0];
+        return;
+    } else {
+        // In the child. We close the parent side of socket and run the
+        // main function. Then we terminate the process.
+        close(sockets[0]);
+        try {
+            main(sockets[1]);
+            close(sockets[1]);
+            exit(0);
+        } catch (const exception& e) {
+            cerr << "Exception: " << e.what() << endl;
+        } catch (...) {
+            cerr << "Unknown exception" << endl;
+        }
+        abort();
+    }
+}
+
+Subprocess::~Subprocess() {
+    int result = waitpid(pid, NULL, 0);
+    assert(result != -1);
+}
+
+}
+}
+}
diff --git a/src/bin/resolver/bench/subprocess.h b/src/bin/resolver/bench/subprocess.h
new file mode 100644
index 0000000..b3821f1
--- /dev/null
+++ b/src/bin/resolver/bench/subprocess.h
@@ -0,0 +1,53 @@
+// Copyright (C) 2013  Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+#ifndef RESOLVER_BENCH_SUBPROCESS_H
+#define RESOLVER_BENCH_SUBPROCESS_H
+
+#include <boost/function.hpp>
+#include <boost/noncopyable.hpp>
+
+#include <unistd.h>
+
+namespace isc {
+namespace resolver {
+namespace bench {
+
+/// \brief Helper to run a subprocess.
+class Subprocess : boost::noncopyable {
+public:
+    /// \brief Run a function in subprocess.
+    ///
+    /// This will fork and run the provided function in the sub
+    /// process. A communication socket is created and one end
+    /// is passed to the function. The other is provided by
+    /// the channel() method to the parent.
+    Subprocess(const boost::function<void(int)>& main);
+    /// \brief Wait for the subprocess to terminate.
+    ~Subprocess();
+    /// A file descriptor for socket to communicate with the
+    /// function.
+    int channel() const {
+        return (channel_);
+    }
+private:
+    int channel_;
+    pid_t pid;
+};
+
+}
+}
+}
+
+#endif



More information about the bind10-changes mailing list