blob: 3d2066c960cd615417d2d3681c0b064adda82150 [file] [log] [blame]
Elly Jones73161782012-08-14 17:37:05 -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
Alex Vakulenko262be3f2014-07-30 15:25:50 -07005#include "debugd/src/icmp_tool.h"
Elly Jones73161782012-08-14 17:37:05 -04006
7#include <stdlib.h>
Ben Chan5facf4a2014-07-23 16:36:54 -07008
Ben Chan9953a592014-02-05 23:32:00 -08009#include <base/strings/stringprintf.h>
Elly Jones73161782012-08-14 17:37:05 -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 Jones73161782012-08-14 17:37:05 -040013
Xiaowen Xinf675f4c2013-06-14 02:27:11 -070014using std::map;
15using std::string;
16
Elly Jones73161782012-08-14 17:37:05 -040017namespace debugd {
18
Eric Carusoc93a15c2017-04-24 16:15:12 -070019string ICMPTool::TestICMP(const string& host) {
Xiaowen Xinf675f4c2013-06-14 02:27:11 -070020 map<string, string> options;
Eric Carusoc93a15c2017-04-24 16:15:12 -070021 return TestICMPWithOptions(host, options);
Xiaowen Xinf675f4c2013-06-14 02:27:11 -070022}
23
24string ICMPTool::TestICMPWithOptions(const string& host,
Eric Carusoc93a15c2017-04-24 16:15:12 -070025 const map<string, string>& options) {
Ben Chan297c3c22013-07-17 17:34:12 -070026 string path;
Hardik Goyalb09d6b02019-08-13 16:15:50 -070027 if (!GetHelperPath("icmp", &path))
Elly Jones73161782012-08-14 17:37:05 -040028 return "<path too long>";
Ben Chan297c3c22013-07-17 17:34:12 -070029
Elly Jones73161782012-08-14 17:37:05 -040030 ProcessWithOutput p;
31 if (!p.Init())
32 return "<can't create process>";
33 p.AddArg(path);
Xiaowen Xinf675f4c2013-06-14 02:27:11 -070034
Ben Chane2fa3572017-02-08 22:46:18 -080035 for (const auto& option : options) {
Xiaowen Xinf675f4c2013-06-14 02:27:11 -070036 // 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.
Eric Caruso96d03d32017-04-25 18:01:17 -070039 p.AddArg(base::StringPrintf("--%s=%s",
40 option.first.c_str(), option.second.c_str()));
Xiaowen Xinf675f4c2013-06-14 02:27:11 -070041 }
42
Elly Jones73161782012-08-14 17:37:05 -040043 p.AddArg(host);
44 p.Run();
Xiaowen Xinf675f4c2013-06-14 02:27:11 -070045 string out;
Elly Jones73161782012-08-14 17:37:05 -040046 p.GetOutput(&out);
47 return out;
48}
49
Ben Chana0011d82014-05-13 00:19:29 -070050} // namespace debugd