blob: bb3de73ab9c4765510c80ef5f117cbe0229fddaa [file] [log] [blame]
Elly Jonese7cb5b32011-12-01 14:18:32 -05001// Copyright (c) 2011 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 "ping_tool.h"
6
7#include <map>
8#include <string>
9
Elly Jonese7cb5b32011-12-01 14:18:32 -050010#include "process_with_id.h"
11
12namespace debugd {
13
Elly Jonese7cb5b32011-12-01 14:18:32 -050014const char* kPing = "/bin/ping";
15
Elly Jones0c016cc2011-12-19 16:19:22 -050016PingTool::PingTool() : SubprocessTool() { }
Elly Jonese7cb5b32011-12-01 14:18:32 -050017PingTool::~PingTool() { }
18
19std::string PingTool::Start(const DBus::FileDescriptor& outfd,
20 const std::string& destination,
21 const std::map<std::string, DBus::Variant>&
22 options,
23 DBus::Error& error) {
Elly Jones0c016cc2011-12-19 16:19:22 -050024 ProcessWithId* p = CreateProcess();
25 if (!p)
Elly Jonese7cb5b32011-12-01 14:18:32 -050026 return "";
27 p->AddArg(kPing);
28 if (options.count("count") == 1) {
29 // If we try to convert a non-int value to an int here, dbus-c++ will toss
30 // a C++ exception, which the dbus-c++ main loop will convert into a dbus
31 // exception and return it to our caller.
32 p->AddIntOption("-c", options.find("count")->second);
33 } else {
34 p->AddIntOption("-c", 4);
35 }
36 if (options.count("interval") == 1) {
37 p->AddIntOption("-i", options.find("interval")->second);
38 }
39 if (options.count("numeric") == 1) {
40 p->AddArg("-n");
41 }
42 if (options.count("packetsize") == 1) {
43 p->AddIntOption("-s", options.find("packetsize")->second);
44 }
45 if (options.count("waittime") == 1) {
46 p->AddIntOption("-W", options.find("waittime")->second);
47 }
48 p->AddArg(destination);
49 p->BindFd(outfd.get(), STDOUT_FILENO);
50 p->BindFd(outfd.get(), STDERR_FILENO);
Elly Jonese7cb5b32011-12-01 14:18:32 -050051 LOG(INFO) << "ping: running process id: " << p->id();
52 p->Start();
53 return p->id();
54}
55
Elly Jonese7cb5b32011-12-01 14:18:32 -050056}; // namespace debugd