blob: 92a349d85914ba570e48a4011c535ae200c0694a [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 base::StringPrintf;
14using std::map;
15using std::string;
16
Elly Jones73161782012-08-14 17:37:05 -040017namespace debugd {
18
Xiaowen Xinf675f4c2013-06-14 02:27:11 -070019string ICMPTool::TestICMP(const string& host, DBus::Error* error) {
20 map<string, string> options;
21 return TestICMPWithOptions(host, options, error);
22}
23
24string ICMPTool::TestICMPWithOptions(const string& host,
25 const map<string, string>& options,
26 DBus::Error* error) {
Ben Chan297c3c22013-07-17 17:34:12 -070027 string path;
28 if (!SandboxedProcess::GetHelperPath("icmp", &path))
Elly Jones73161782012-08-14 17:37:05 -040029 return "<path too long>";
Ben Chan297c3c22013-07-17 17:34:12 -070030
Elly Jones73161782012-08-14 17:37:05 -040031 ProcessWithOutput p;
32 if (!p.Init())
33 return "<can't create process>";
34 p.AddArg(path);
Xiaowen Xinf675f4c2013-06-14 02:27:11 -070035
Ben Chane2fa3572017-02-08 22:46:18 -080036 for (const auto& option : options) {
Xiaowen Xinf675f4c2013-06-14 02:27:11 -070037 // No need to quote here because chromeos:ProcessImpl (base class of
38 // ProcessWithOutput) passes arguments as is to helpers/icmp, which will
39 // check arguments before executing in the shell.
Ben Chane2fa3572017-02-08 22:46:18 -080040 p.AddArg(
41 StringPrintf("--%s=%s", option.first.c_str(), option.second.c_str()));
Xiaowen Xinf675f4c2013-06-14 02:27:11 -070042 }
43
Elly Jones73161782012-08-14 17:37:05 -040044 p.AddArg(host);
45 p.Run();
Xiaowen Xinf675f4c2013-06-14 02:27:11 -070046 string out;
Elly Jones73161782012-08-14 17:37:05 -040047 p.GetOutput(&out);
48 return out;
49}
50
Ben Chana0011d82014-05-13 00:19:29 -070051} // namespace debugd