blob: b48f17508e44ee3cc950584346cb6cf3a7019dea [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
Alex Vakulenko262be3f2014-07-30 15:25:50 -07009#include "debugd/src/example_tool.h"
Elly Jonese0ec6012012-07-17 12:39:51 -040010
Alex Vakulenko262be3f2014-07-30 15:25:50 -070011#include "debugd/src/process_with_output.h"
Elly Jonese0ec6012012-07-17 12:39:51 -040012
Elly Jonese0ec6012012-07-17 12:39:51 -040013namespace debugd {
14
Eric Carusoc93a15c2017-04-24 16:15:12 -070015// Tool methods have a similar signature as the generated DBus adaptors.
16// Tool methods are generally written in can't-fail style, since
Ben Chana0011d82014-05-13 00:19:29 -070017// their output is usually going to be displayed to the user; instead of
18// returning a DBus exception, we tend to return a string indicating what went
19// wrong.
Eric Carusoc93a15c2017-04-24 16:15:12 -070020std::string ExampleTool::GetExample() {
Ben Chan297c3c22013-07-17 17:34:12 -070021 std::string path;
22 if (!SandboxedProcess::GetHelperPath("example", &path))
Elly Jonese0ec6012012-07-17 12:39:51 -040023 return "<path too long>";
24 // This whole method is synchronous, so we create a subprocess, let it run to
25 // completion, then gather up its output to return it.
26 ProcessWithOutput process;
27 if (!process.Init())
28 return "<process init failed>";
29 // If you're going to add switches to a command, have a look at the Process
30 // interface; there's support for adding options specifically.
31 process.AddArg(path);
32 process.AddArg("hello");
33 // Run the process to completion. If the process might take a while, you may
34 // have to make this asynchronous using .Start().
35 if (process.Run() != 0)
36 return "<process exited with nonzero status>";
37 std::string output;
38 process.GetOutput(&output);
39 return output;
40}
41
Ben Chana0011d82014-05-13 00:19:29 -070042} // namespace debugd