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