blob: 582e2466a1e215eda441ef9959a4ff3af277459e [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>
8#include <base/stringprintf.h>
9
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) {
Elly Jones73161782012-08-14 17:37:05 -040029 char *envvar = getenv("DEBUGD_HELPERS");
Xiaowen Xinf675f4c2013-06-14 02:27:11 -070030 string path = StringPrintf("%s/icmp", envvar ? envvar
31 : "/usr/libexec/debugd/helpers");
Elly Jones73161782012-08-14 17:37:05 -040032 if (path.length() > PATH_MAX)
33 return "<path too long>";
34 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
55}; // namespace debugd