Elly Jones | e0ec601 | 2012-07-17 12:39:51 -0400 | [diff] [blame] | 1 | // 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 Vakulenko | 262be3f | 2014-07-30 15:25:50 -0700 | [diff] [blame] | 9 | #include "debugd/src/example_tool.h" |
Elly Jones | e0ec601 | 2012-07-17 12:39:51 -0400 | [diff] [blame] | 10 | |
Hardik Goyal | b09d6b0 | 2019-08-13 16:15:50 -0700 | [diff] [blame] | 11 | #include "debugd/src/helper_utils.h" |
Alex Vakulenko | 262be3f | 2014-07-30 15:25:50 -0700 | [diff] [blame] | 12 | #include "debugd/src/process_with_output.h" |
Elly Jones | e0ec601 | 2012-07-17 12:39:51 -0400 | [diff] [blame] | 13 | |
Elly Jones | e0ec601 | 2012-07-17 12:39:51 -0400 | [diff] [blame] | 14 | namespace debugd { |
| 15 | |
Eric Caruso | c93a15c | 2017-04-24 16:15:12 -0700 | [diff] [blame] | 16 | // Tool methods have a similar signature as the generated DBus adaptors. |
| 17 | // Tool methods are generally written in can't-fail style, since |
Ben Chan | a0011d8 | 2014-05-13 00:19:29 -0700 | [diff] [blame] | 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. |
Eric Caruso | c93a15c | 2017-04-24 16:15:12 -0700 | [diff] [blame] | 21 | std::string ExampleTool::GetExample() { |
Ben Chan | 297c3c2 | 2013-07-17 17:34:12 -0700 | [diff] [blame] | 22 | std::string path; |
Hardik Goyal | b09d6b0 | 2019-08-13 16:15:50 -0700 | [diff] [blame] | 23 | if (!GetHelperPath("example", &path)) |
Elly Jones | e0ec601 | 2012-07-17 12:39:51 -0400 | [diff] [blame] | 24 | 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 Chan | a0011d8 | 2014-05-13 00:19:29 -0700 | [diff] [blame] | 43 | } // namespace debugd |