kudu-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From t...@apache.org
Subject [4/8] incubator-kudu git commit: Factor out function to get output from forked subprocess
Date Wed, 06 Jan 2016 23:46:16 GMT
Factor out function to get output from forked subprocess

Similar to backticks in Perl or the shell.

Change-Id: Idba22393d86fc93e340a459ffe2806f8caf74596
Reviewed-on: http://gerrit.cloudera.org:8080/1714
Tested-by: Internal Jenkins
Reviewed-by: Todd Lipcon <todd@cloudera.com>


Project: http://git-wip-us.apache.org/repos/asf/incubator-kudu/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-kudu/commit/183e8161
Tree: http://git-wip-us.apache.org/repos/asf/incubator-kudu/tree/183e8161
Diff: http://git-wip-us.apache.org/repos/asf/incubator-kudu/diff/183e8161

Branch: refs/heads/master
Commit: 183e81612317c521ab4e7a51080e5c15c192b036
Parents: dca76ca
Author: Mike Percy <mpercy@cloudera.com>
Authored: Tue Jan 5 17:15:59 2016 -0800
Committer: Todd Lipcon <todd@cloudera.com>
Committed: Wed Jan 6 20:19:10 2016 +0000

----------------------------------------------------------------------
 src/kudu/util/net/net_util.cc | 46 +++++---------------------------------
 src/kudu/util/subprocess.cc   | 37 ++++++++++++++++++++++++++++++
 src/kudu/util/subprocess.h    |  5 +++++
 3 files changed, 48 insertions(+), 40 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-kudu/blob/183e8161/src/kudu/util/net/net_util.cc
----------------------------------------------------------------------
diff --git a/src/kudu/util/net/net_util.cc b/src/kudu/util/net/net_util.cc
index 5f7b75c..56074d2 100644
--- a/src/kudu/util/net/net_util.cc
+++ b/src/kudu/util/net/net_util.cc
@@ -265,47 +265,13 @@ void TryRunLsof(const Sockaddr& addr, vector<string>* log)
{
                            << "Trying to use lsof to find any processes listening "
                            << "on the same port:";
   LOG_STRING(INFO, log) << "$ " << cmd;
-  Subprocess p("/bin/bash",
-               boost::assign::list_of<string>("bash")("-c")(cmd));
-  p.ShareParentStdout(false);
-  Status s = p.Start();
-  if (!s.ok()) {
-    LOG_STRING(WARNING, log) << "Unable to fork bash: " << s.ToString();
-    return;
+  vector<string> argv = boost::assign::list_of<string>("/bin/bash")("-c")(cmd);
+  string results;
+  Status s = Subprocess::Call(argv, &results);
+  if (PREDICT_FALSE(!s.ok())) {
+    LOG_STRING(WARNING, log) << s.ToString();
   }
-
-  close(p.ReleaseChildStdinFd());
-
-  faststring results;
-  char buf[1024];
-  while (true) {
-    ssize_t n = read(p.from_child_stdout_fd(), buf, arraysize(buf));
-    if (n == 0) {
-      // EOF
-      break;
-    }
-    if (n < 0) {
-      if (errno == EINTR) continue;
-      LOG_STRING(WARNING, log) << "IO error reading from bash: " <<
-        ErrnoToString(errno);
-      close(p.ReleaseChildStdoutFd());
-      break;
-    }
-
-    results.append(buf, n);
-  }
-
-  int rc;
-  s = p.Wait(&rc);
-  if (!s.ok()) {
-    LOG_STRING(WARNING, log) << "Unable to wait for lsof: " << s.ToString();
-    return;
-  }
-  if (rc != 0) {
-    LOG_STRING(WARNING, log) << "lsof failed";
-  }
-
-  LOG_STRING(WARNING, log) << results.ToString();
+  LOG_STRING(WARNING, log) << results;
 }
 
 } // namespace kudu

http://git-wip-us.apache.org/repos/asf/incubator-kudu/blob/183e8161/src/kudu/util/subprocess.cc
----------------------------------------------------------------------
diff --git a/src/kudu/util/subprocess.cc b/src/kudu/util/subprocess.cc
index a9e3d8e..0e62dfc 100644
--- a/src/kudu/util/subprocess.cc
+++ b/src/kudu/util/subprocess.cc
@@ -387,6 +387,43 @@ Status Subprocess::Call(const vector<string>& argv) {
   }
 }
 
+Status Subprocess::Call(const vector<string>& argv, string* stdout_out) {
+  Subprocess p(argv[0], argv);
+  p.ShareParentStdout(false);
+  RETURN_NOT_OK_PREPEND(p.Start(), "Unable to fork " + argv[0]);
+  int err = close(p.ReleaseChildStdinFd());
+  if (PREDICT_FALSE(err != 0)) {
+    return Status::IOError("Unable to close child process stdin", ErrnoToString(errno), errno);
+  }
+
+  stdout_out->clear();
+  char buf[1024];
+  while (true) {
+    ssize_t n = read(p.from_child_stdout_fd(), buf, arraysize(buf));
+    if (n == 0) {
+      // EOF
+      break;
+    }
+    if (n < 0) {
+      if (errno == EINTR) continue;
+      return Status::IOError("IO error reading from " + argv[0], ErrnoToString(errno), errno);
+    }
+
+    stdout_out->append(buf, n);
+  }
+
+  int retcode;
+  RETURN_NOT_OK_PREPEND(p.Wait(&retcode), "Unable to wait() for " + argv[0]);
+
+  if (PREDICT_FALSE(retcode != 0)) {
+    return Status::RuntimeError(Substitute(
+        "Subprocess '$0' terminated with non-zero exit status $1",
+        argv[0],
+        retcode));
+  }
+  return Status::OK();
+}
+
 int Subprocess::CheckAndOffer(int stdfd) const {
   CHECK_EQ(state_, kRunning);
   CHECK_EQ(fd_state_[stdfd], PIPED);

http://git-wip-us.apache.org/repos/asf/incubator-kudu/blob/183e8161/src/kudu/util/subprocess.h
----------------------------------------------------------------------
diff --git a/src/kudu/util/subprocess.h b/src/kudu/util/subprocess.h
index cd614fd..7f259ec 100644
--- a/src/kudu/util/subprocess.h
+++ b/src/kudu/util/subprocess.h
@@ -97,6 +97,11 @@ class Subprocess {
   // executable as argv[0] and the arguments to the program in argv[1..n].
   static Status Call(const std::vector<std::string>& argv);
 
+  // Same as above, but collects the output from the child process stdout into
+  // 'stdout_out'.
+  static Status Call(const std::vector<std::string>& argv,
+                     std::string* stdout_out);
+
   // Return the pipe fd to the child's standard stream.
   // Stream should not be disabled or shared.
   int to_child_stdin_fd()    const { return CheckAndOffer(STDIN_FILENO); }


Mime
View raw message