blob: 19d16e49361787834a212fbc5003f35b69cef1d7 [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,
Ben Chana0011d82014-05-13 00:19:29 -070022 const std::map<std::string, DBus::Variant>& options,
23 DBus::Error* error) {
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -050024 ProcessWithId* p = CreateProcess(true);
Elly Jones0c016cc2011-12-19 16:19:22 -050025 if (!p)
Elly Jonese7cb5b32011-12-01 14:18:32 -050026 return "";
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -050027 p->AddArg(kSetuidHack);
Elly Jonese7cb5b32011-12-01 14:18:32 -050028 p->AddArg(kPing);
Paul Stewartc0479d82013-02-04 10:02:53 -080029 if (options.count("broadcast") == 1) {
30 p->AddArg("-b");
31 }
Elly Jonese7cb5b32011-12-01 14:18:32 -050032 if (options.count("count") == 1) {
33 // If we try to convert a non-int value to an int here, dbus-c++ will toss
34 // a C++ exception, which the dbus-c++ main loop will convert into a dbus
35 // exception and return it to our caller.
36 p->AddIntOption("-c", options.find("count")->second);
Elly Jonese7cb5b32011-12-01 14:18:32 -050037 }
38 if (options.count("interval") == 1) {
39 p->AddIntOption("-i", options.find("interval")->second);
40 }
41 if (options.count("numeric") == 1) {
42 p->AddArg("-n");
43 }
44 if (options.count("packetsize") == 1) {
45 p->AddIntOption("-s", options.find("packetsize")->second);
46 }
47 if (options.count("waittime") == 1) {
48 p->AddIntOption("-W", options.find("waittime")->second);
49 }
50 p->AddArg(destination);
51 p->BindFd(outfd.get(), STDOUT_FILENO);
52 p->BindFd(outfd.get(), STDERR_FILENO);
Elly Jonese7cb5b32011-12-01 14:18:32 -050053 LOG(INFO) << "ping: running process id: " << p->id();
54 p->Start();
55 return p->id();
56}
57
Ben Chana0011d82014-05-13 00:19:29 -070058} // namespace debugd