blob: c1f323ab9a04d996433d748150f2984ba419de7f [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
Ben Chana0011d82014-05-13 00:19:29 -070015// Tool methods have a similar signature as the generated DBus adaptors, except
16// for the DBus::Error argument, which is passed in as a pointer instead of a
17// reference. Tool methods are generally written in can't-fail style, since
18// their output is usually going to be displayed to the user; instead of
19// returning a DBus exception, we tend to return a string indicating what went
20// wrong.
21std::string ExampleTool::GetExample(DBus::Error* error) {
Ben Chan297c3c22013-07-17 17:34:12 -070022 std::string path;
23 if (!SandboxedProcess::GetHelperPath("example", &path))
Elly Jonese0ec6012012-07-17 12:39:51 -040024 return "<path too long>";
25 // This whole method is synchronous, so we create a subprocess, let it run to
26 // completion, then gather up its output to return it.
27 ProcessWithOutput process;
28 if (!process.Init())
29 return "<process init failed>";
30 // If you're going to add switches to a command, have a look at the Process
31 // interface; there's support for adding options specifically.
32 process.AddArg(path);
33 process.AddArg("hello");
34 // Run the process to completion. If the process might take a while, you may
35 // have to make this asynchronous using .Start().
36 if (process.Run() != 0)
37 return "<process exited with nonzero status>";
38 std::string output;
39 process.GetOutput(&output);
40 return output;
41}
42
Ben Chana0011d82014-05-13 00:19:29 -070043} // namespace debugd