blob: bdad5f2025c0e8baf264e14d0c8936d916b29fe4 [file] [log] [blame]
Elly Jonesa44d22d2012-01-05 18:05:56 -05001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Elly Jonese7cb5b32011-12-01 14:18:32 -05002// 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 Fong-Jonesd9a16cd2012-11-12 16:09:49 -050014const char* kSetuidHack = "/usr/libexec/debugd/helpers/minijail-setuid-hack.sh";
Elly Jonese7cb5b32011-12-01 14:18:32 -050015const char* kPing = "/bin/ping";
16
Elly Jones0c016cc2011-12-19 16:19:22 -050017PingTool::PingTool() : SubprocessTool() { }
Elly Jonese7cb5b32011-12-01 14:18:32 -050018PingTool::~PingTool() { }
19
20std::string PingTool::Start(const DBus::FileDescriptor& outfd,
21 const std::string& destination,
22 const std::map<std::string, DBus::Variant>&
23 options,
24 DBus::Error& error) {
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -050025 ProcessWithId* p = CreateProcess(true);
Elly Jones0c016cc2011-12-19 16:19:22 -050026 if (!p)
Elly Jonese7cb5b32011-12-01 14:18:32 -050027 return "";
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -050028 p->AddArg(kSetuidHack);
Elly Jonese7cb5b32011-12-01 14:18:32 -050029 p->AddArg(kPing);
Paul Stewartc0479d82013-02-04 10:02:53 -080030 if (options.count("broadcast") == 1) {
31 p->AddArg("-b");
32 }
Elly Jonese7cb5b32011-12-01 14:18:32 -050033 if (options.count("count") == 1) {
34 // If we try to convert a non-int value to an int here, dbus-c++ will toss
35 // a C++ exception, which the dbus-c++ main loop will convert into a dbus
36 // exception and return it to our caller.
37 p->AddIntOption("-c", options.find("count")->second);
Elly Jonese7cb5b32011-12-01 14:18:32 -050038 }
39 if (options.count("interval") == 1) {
40 p->AddIntOption("-i", options.find("interval")->second);
41 }
42 if (options.count("numeric") == 1) {
43 p->AddArg("-n");
44 }
45 if (options.count("packetsize") == 1) {
46 p->AddIntOption("-s", options.find("packetsize")->second);
47 }
48 if (options.count("waittime") == 1) {
49 p->AddIntOption("-W", options.find("waittime")->second);
50 }
51 p->AddArg(destination);
52 p->BindFd(outfd.get(), STDOUT_FILENO);
53 p->BindFd(outfd.get(), STDERR_FILENO);
Elly Jonese7cb5b32011-12-01 14:18:32 -050054 LOG(INFO) << "ping: running process id: " << p->id();
55 p->Start();
56 return p->id();
57}
58
Elly Jonese7cb5b32011-12-01 14:18:32 -050059}; // namespace debugd