blob: 1db73bab8b2cfc69288647a6e2ee89ef2c433742 [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
19ICMPTool::ICMPTool() { }
20ICMPTool::~ICMPTool() { }
21
Xiaowen Xinf675f4c2013-06-14 02:27:11 -070022string ICMPTool::TestICMP(const string& host, DBus::Error* error) {
23 map<string, string> options;
24 return TestICMPWithOptions(host, options, error);
25}
26
27string ICMPTool::TestICMPWithOptions(const string& host,
28 const map<string, string>& options,
29 DBus::Error* error) {
Ben Chan297c3c22013-07-17 17:34:12 -070030 string path;
31 if (!SandboxedProcess::GetHelperPath("icmp", &path))
Elly Jones73161782012-08-14 17:37:05 -040032 return "<path too long>";
Ben Chan297c3c22013-07-17 17:34:12 -070033
Elly Jones73161782012-08-14 17:37:05 -040034 ProcessWithOutput p;
35 if (!p.Init())
36 return "<can't create process>";
37 p.AddArg(path);
Xiaowen Xinf675f4c2013-06-14 02:27:11 -070038
39 for (map<string, string>::const_iterator it = options.begin();
40 it != options.end();
41 it++) {
42 // No need to quote here because chromeos:ProcessImpl (base class of
43 // ProcessWithOutput) passes arguments as is to helpers/icmp, which will
44 // check arguments before executing in the shell.
45 p.AddArg(StringPrintf("--%s=%s", it->first.c_str(), it->second.c_str()));
46 }
47
Elly Jones73161782012-08-14 17:37:05 -040048 p.AddArg(host);
49 p.Run();
Xiaowen Xinf675f4c2013-06-14 02:27:11 -070050 string out;
Elly Jones73161782012-08-14 17:37:05 -040051 p.GetOutput(&out);
52 return out;
53}
54
Ben Chana0011d82014-05-13 00:19:29 -070055} // namespace debugd