blob: 0a7614f31812e1cac3e2243a73eb9e87ca4fad0e [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
28 // This environment var controls the root for debugd helpers, which lets
29 // people develop helpers even when verified root is on.
30 char *envvar = getenv("DEBUGD_HELPERS");
31 std::string path = StringPrintf("%s/example", envvar ? envvar
32 : "/usr/libexec/debugd/helpers");
33 if (path.length() > PATH_MAX)
34 return "<path too long>";
35 // This whole method is synchronous, so we create a subprocess, let it run to
36 // completion, then gather up its output to return it.
37 ProcessWithOutput process;
38 if (!process.Init())
39 return "<process init failed>";
40 // If you're going to add switches to a command, have a look at the Process
41 // interface; there's support for adding options specifically.
42 process.AddArg(path);
43 process.AddArg("hello");
44 // Run the process to completion. If the process might take a while, you may
45 // have to make this asynchronous using .Start().
46 if (process.Run() != 0)
47 return "<process exited with nonzero status>";
48 std::string output;
49 process.GetOutput(&output);
50 return output;
51}
52
53}; // namespace debugd