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