Elly Jones | 7316178 | 2012-08-14 17:37:05 -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 | #include "icmp_tool.h" |
| 6 | |
| 7 | #include <stdlib.h> |
Ben Chan | 9953a59 | 2014-02-05 23:32:00 -0800 | [diff] [blame] | 8 | #include <base/strings/stringprintf.h> |
Elly Jones | 7316178 | 2012-08-14 17:37:05 -0400 | [diff] [blame] | 9 | |
| 10 | #include "process_with_output.h" |
| 11 | |
Xiaowen Xin | f675f4c | 2013-06-14 02:27:11 -0700 | [diff] [blame] | 12 | using base::StringPrintf; |
| 13 | using std::map; |
| 14 | using std::string; |
| 15 | |
Elly Jones | 7316178 | 2012-08-14 17:37:05 -0400 | [diff] [blame] | 16 | namespace debugd { |
| 17 | |
| 18 | ICMPTool::ICMPTool() { } |
| 19 | ICMPTool::~ICMPTool() { } |
| 20 | |
Xiaowen Xin | f675f4c | 2013-06-14 02:27:11 -0700 | [diff] [blame] | 21 | string ICMPTool::TestICMP(const string& host, DBus::Error* error) { |
| 22 | map<string, string> options; |
| 23 | return TestICMPWithOptions(host, options, error); |
| 24 | } |
| 25 | |
| 26 | string ICMPTool::TestICMPWithOptions(const string& host, |
| 27 | const map<string, string>& options, |
| 28 | DBus::Error* error) { |
Ben Chan | 297c3c2 | 2013-07-17 17:34:12 -0700 | [diff] [blame] | 29 | string path; |
| 30 | if (!SandboxedProcess::GetHelperPath("icmp", &path)) |
Elly Jones | 7316178 | 2012-08-14 17:37:05 -0400 | [diff] [blame] | 31 | return "<path too long>"; |
Ben Chan | 297c3c2 | 2013-07-17 17:34:12 -0700 | [diff] [blame] | 32 | |
Elly Jones | 7316178 | 2012-08-14 17:37:05 -0400 | [diff] [blame] | 33 | ProcessWithOutput p; |
| 34 | if (!p.Init()) |
| 35 | return "<can't create process>"; |
| 36 | p.AddArg(path); |
Xiaowen Xin | f675f4c | 2013-06-14 02:27:11 -0700 | [diff] [blame] | 37 | |
| 38 | for (map<string, string>::const_iterator it = options.begin(); |
| 39 | it != options.end(); |
| 40 | it++) { |
| 41 | // No need to quote here because chromeos:ProcessImpl (base class of |
| 42 | // ProcessWithOutput) passes arguments as is to helpers/icmp, which will |
| 43 | // check arguments before executing in the shell. |
| 44 | p.AddArg(StringPrintf("--%s=%s", it->first.c_str(), it->second.c_str())); |
| 45 | } |
| 46 | |
Elly Jones | 7316178 | 2012-08-14 17:37:05 -0400 | [diff] [blame] | 47 | p.AddArg(host); |
| 48 | p.Run(); |
Xiaowen Xin | f675f4c | 2013-06-14 02:27:11 -0700 | [diff] [blame] | 49 | string out; |
Elly Jones | 7316178 | 2012-08-14 17:37:05 -0400 | [diff] [blame] | 50 | p.GetOutput(&out); |
| 51 | return out; |
| 52 | } |
| 53 | |
Ben Chan | a0011d8 | 2014-05-13 00:19:29 -0700 | [diff] [blame^] | 54 | } // namespace debugd |