blob: 488fd1251dbff615c753449f3a08a7af175232eb [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
5#include "icmp_tool.h"
6
7#include <stdlib.h>
Ben Chan9953a592014-02-05 23:32:00 -08008#include <base/strings/stringprintf.h>
Elly Jones73161782012-08-14 17:37:05 -04009
10#include "process_with_output.h"
11
Xiaowen Xinf675f4c2013-06-14 02:27:11 -070012using base::StringPrintf;
13using std::map;
14using std::string;
15
Elly Jones73161782012-08-14 17:37:05 -040016namespace debugd {
17
18ICMPTool::ICMPTool() { }
19ICMPTool::~ICMPTool() { }
20
Xiaowen Xinf675f4c2013-06-14 02:27:11 -070021string ICMPTool::TestICMP(const string& host, DBus::Error* error) {
22 map<string, string> options;
23 return TestICMPWithOptions(host, options, error);
24}
25
26string ICMPTool::TestICMPWithOptions(const string& host,
27 const map<string, string>& options,
28 DBus::Error* error) {
Ben Chan297c3c22013-07-17 17:34:12 -070029 string path;
30 if (!SandboxedProcess::GetHelperPath("icmp", &path))
Elly Jones73161782012-08-14 17:37:05 -040031 return "<path too long>";
Ben Chan297c3c22013-07-17 17:34:12 -070032
Elly Jones73161782012-08-14 17:37:05 -040033 ProcessWithOutput p;
34 if (!p.Init())
35 return "<can't create process>";
36 p.AddArg(path);
Xiaowen Xinf675f4c2013-06-14 02:27:11 -070037
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 Jones73161782012-08-14 17:37:05 -040047 p.AddArg(host);
48 p.Run();
Xiaowen Xinf675f4c2013-06-14 02:27:11 -070049 string out;
Elly Jones73161782012-08-14 17:37:05 -040050 p.GetOutput(&out);
51 return out;
52}
53
Ben Chana0011d82014-05-13 00:19:29 -070054} // namespace debugd