blob: 89173675e5b17fbecf26cb630c22521e26e17165 [file] [log] [blame]
Elly Jonese0ec6012012-07-17 12:39:51 -04001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4//
5// This is an example of a tool. A tool is the implementation of one or more of
6// debugd's dbus methods. The main DebugDaemon class creates a single instance
7// of each tool and calls it to answer methods.
8
9#include "example_tool.h"
10
11#include "process_with_output.h"
12
Ben Chan55903dd2014-04-24 00:29:04 -070013using base::StringPrintf;
14
Elly Jonese0ec6012012-07-17 12:39:51 -040015namespace debugd {
16
17ExampleTool::ExampleTool() { }
18
19ExampleTool::~ExampleTool() { }
20
21// Tool methods have the same signature as the generated DBus adaptors. Most
22// pertinently, this means they take their DBus::Error argument as a non-const
23// reference (hence the NOLINT). Tool methods are generally written in
24// can't-fail style, since their output is usually going to be displayed to the
25// user; instead of returning a DBus exception, we tend to return a string
26// indicating what went wrong.
27std::string ExampleTool::GetExample(DBus::Error& error) { // NOLINT
Ben Chan297c3c22013-07-17 17:34:12 -070028 std::string path;
29 if (!SandboxedProcess::GetHelperPath("example", &path))
Elly Jonese0ec6012012-07-17 12:39:51 -040030 return "<path too long>";
31 // This whole method is synchronous, so we create a subprocess, let it run to
32 // completion, then gather up its output to return it.
33 ProcessWithOutput process;
34 if (!process.Init())
35 return "<process init failed>";
36 // If you're going to add switches to a command, have a look at the Process
37 // interface; there's support for adding options specifically.
38 process.AddArg(path);
39 process.AddArg("hello");
40 // Run the process to completion. If the process might take a while, you may
41 // have to make this asynchronous using .Start().
42 if (process.Run() != 0)
43 return "<process exited with nonzero status>";
44 std::string output;
45 process.GetOutput(&output);
46 return output;
47}
48
49}; // namespace debugd