blob: 9bbf62d9e6bae8b3d5a1a25c142d31198d69adda [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
Alex Vakulenko262be3f2014-07-30 15:25:50 -070011#include "debugd/src/process_with_output.h"
Elly Jones73161782012-08-14 17:37:05 -040012
Xiaowen Xinf675f4c2013-06-14 02:27:11 -070013using std::map;
14using std::string;
15
Elly Jones73161782012-08-14 17:37:05 -040016namespace debugd {
17
Eric Carusoc93a15c2017-04-24 16:15:12 -070018string ICMPTool::TestICMP(const string& host) {
Xiaowen Xinf675f4c2013-06-14 02:27:11 -070019 map<string, string> options;
Eric Carusoc93a15c2017-04-24 16:15:12 -070020 return TestICMPWithOptions(host, options);
Xiaowen Xinf675f4c2013-06-14 02:27:11 -070021}
22
23string ICMPTool::TestICMPWithOptions(const string& host,
Eric Carusoc93a15c2017-04-24 16:15:12 -070024 const map<string, string>& options) {
Ben Chan297c3c22013-07-17 17:34:12 -070025 string path;
26 if (!SandboxedProcess::GetHelperPath("icmp", &path))
Elly Jones73161782012-08-14 17:37:05 -040027 return "<path too long>";
Ben Chan297c3c22013-07-17 17:34:12 -070028
Elly Jones73161782012-08-14 17:37:05 -040029 ProcessWithOutput p;
30 if (!p.Init())
31 return "<can't create process>";
32 p.AddArg(path);
Xiaowen Xinf675f4c2013-06-14 02:27:11 -070033
Ben Chane2fa3572017-02-08 22:46:18 -080034 for (const auto& option : options) {
Xiaowen Xinf675f4c2013-06-14 02:27:11 -070035 // No need to quote here because chromeos:ProcessImpl (base class of
36 // ProcessWithOutput) passes arguments as is to helpers/icmp, which will
37 // check arguments before executing in the shell.
Eric Caruso96d03d32017-04-25 18:01:17 -070038 p.AddArg(base::StringPrintf("--%s=%s",
39 option.first.c_str(), option.second.c_str()));
Xiaowen Xinf675f4c2013-06-14 02:27:11 -070040 }
41
Elly Jones73161782012-08-14 17:37:05 -040042 p.AddArg(host);
43 p.Run();
Xiaowen Xinf675f4c2013-06-14 02:27:11 -070044 string out;
Elly Jones73161782012-08-14 17:37:05 -040045 p.GetOutput(&out);
46 return out;
47}
48
Ben Chana0011d82014-05-13 00:19:29 -070049} // namespace debugd