blob: 6d372e4ac6ac799a7d9ce202a4e2dbc2573debb5 [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
Hardik Goyalb09d6b02019-08-13 16:15:50 -070011#include "debugd/src/helper_utils.h"
Alex Vakulenko262be3f2014-07-30 15:25:50 -070012#include "debugd/src/process_with_output.h"
Elly Jonese0ec6012012-07-17 12:39:51 -040013
Elly Jonese0ec6012012-07-17 12:39:51 -040014namespace debugd {
15
Eric Carusoc93a15c2017-04-24 16:15:12 -070016// Tool methods have a similar signature as the generated DBus adaptors.
17// Tool methods are generally written in can't-fail style, since
Ben Chana0011d82014-05-13 00:19:29 -070018// 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.
Eric Carusoc93a15c2017-04-24 16:15:12 -070021std::string ExampleTool::GetExample() {
Ben Chan297c3c22013-07-17 17:34:12 -070022 std::string path;
Hardik Goyalb09d6b02019-08-13 16:15:50 -070023 if (!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