blob: c2fdbc5be548742cee262edb18597f661077cd40 [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
13namespace debugd {
14
15ExampleTool::ExampleTool() { }
16
17ExampleTool::~ExampleTool() { }
18
19// Tool methods have the same signature as the generated DBus adaptors. Most
20// pertinently, this means they take their DBus::Error argument as a non-const
21// reference (hence the NOLINT). Tool methods are generally written in
22// can't-fail style, since their output is usually going to be displayed to the
23// user; instead of returning a DBus exception, we tend to return a string
24// indicating what went wrong.
25std::string ExampleTool::GetExample(DBus::Error& error) { // NOLINT
26 // This environment var controls the root for debugd helpers, which lets
27 // people develop helpers even when verified root is on.
28 char *envvar = getenv("DEBUGD_HELPERS");
29 std::string path = StringPrintf("%s/example", envvar ? envvar
30 : "/usr/libexec/debugd/helpers");
31 if (path.length() > PATH_MAX)
32 return "<path too long>";
33 // This whole method is synchronous, so we create a subprocess, let it run to
34 // completion, then gather up its output to return it.
35 ProcessWithOutput process;
36 if (!process.Init())
37 return "<process init failed>";
38 // If you're going to add switches to a command, have a look at the Process
39 // interface; there's support for adding options specifically.
40 process.AddArg(path);
41 process.AddArg("hello");
42 // Run the process to completion. If the process might take a while, you may
43 // have to make this asynchronous using .Start().
44 if (process.Run() != 0)
45 return "<process exited with nonzero status>";
46 std::string output;
47 process.GetOutput(&output);
48 return output;
49}
50
51}; // namespace debugd