[debugd] add GetRoutes() API.

This API exposes the system routing table.

BUG=chromium-os:23839
TEST=Adhoc,platform_DebugDaemonGetRoutes
dbus-send --system --fixed --print-reply --dest=org.chromium.debugd \
/org/chromium/debugd org.chromium.debugd.GetRoutes \
dict:string:variant:numeric,boolean:true:v6,boolean:true

Change-Id: I2018beff1ec44ba1f145f02a9534923590c3cc04
Signed-off-by: Elly Jones <ellyjones@chromium.org>
diff --git a/debugd/src/process_with_output.cc b/debugd/src/process_with_output.cc
new file mode 100644
index 0000000..bdf1124
--- /dev/null
+++ b/debugd/src/process_with_output.cc
@@ -0,0 +1,49 @@
+// Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "process_with_output.h"
+
+#include <base/basictypes.h>
+#include <base/file_path.h>
+#include <base/file_util.h>
+#include <base/string_split.h>
+
+namespace debugd {
+
+ProcessWithOutput::ProcessWithOutput() : outfile_(NULL) { }
+ProcessWithOutput::~ProcessWithOutput() {
+  if (outfile_)
+    fclose(outfile_);
+  if (!outfile_path_.empty())
+    file_util::Delete(outfile_path_, false);  // not recursive
+}
+
+bool ProcessWithOutput::Init() {
+  outfile_ = file_util::CreateAndOpenTemporaryFile(&outfile_path_);
+  if (!outfile_)
+    return false;
+  // We can't just RedirectOutput to the file we just created, since
+  // RedirectOutput uses O_CREAT | O_EXCL to open the target file (i.e., it'll
+  // fail if the file already exists). We can't CreateTemporaryFile() and then
+  // use that filename, since we'd have to remove it before using
+  // RedirectOutput, which exposes us to a /tmp race. Instead, bind outfile_'s
+  // fd to the subprocess's stdout and stderr.
+  BindFd(fileno(outfile_), STDOUT_FILENO);
+  BindFd(fileno(outfile_), STDERR_FILENO);
+  return true;
+}
+
+bool ProcessWithOutput::GetOutputLines(std::vector<std::string>* output) {
+  std::string contents;
+  if (!file_util::ReadFileToString(outfile_path_, &contents))
+    return false;
+  base::SplitString(contents, '\n', output);
+  return true;
+}
+
+bool ProcessWithOutput::GetOutput(std::string* output) {
+  return file_util::ReadFileToString(outfile_path_, output);
+}
+
+};  // namespace debugd