Elly Jones | a44d22d | 2012-01-05 18:05:56 -0500 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Elly Jones | e7cb5b3 | 2011-12-01 14:18:32 -0500 | [diff] [blame] | 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 Jones | e7cb5b3 | 2011-12-01 14:18:32 -0500 | [diff] [blame] | 10 | #include "process_with_id.h" |
| 11 | |
| 12 | namespace debugd { |
| 13 | |
Elly Fong-Jones | d9a16cd | 2012-11-12 16:09:49 -0500 | [diff] [blame] | 14 | const char* kSetuidHack = "/usr/libexec/debugd/helpers/minijail-setuid-hack.sh"; |
Elly Jones | e7cb5b3 | 2011-12-01 14:18:32 -0500 | [diff] [blame] | 15 | const char* kPing = "/bin/ping"; |
| 16 | |
Elly Jones | 0c016cc | 2011-12-19 16:19:22 -0500 | [diff] [blame] | 17 | PingTool::PingTool() : SubprocessTool() { } |
Elly Jones | e7cb5b3 | 2011-12-01 14:18:32 -0500 | [diff] [blame] | 18 | PingTool::~PingTool() { } |
| 19 | |
| 20 | std::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-Jones | d9a16cd | 2012-11-12 16:09:49 -0500 | [diff] [blame] | 25 | ProcessWithId* p = CreateProcess(true); |
Elly Jones | 0c016cc | 2011-12-19 16:19:22 -0500 | [diff] [blame] | 26 | if (!p) |
Elly Jones | e7cb5b3 | 2011-12-01 14:18:32 -0500 | [diff] [blame] | 27 | return ""; |
Elly Fong-Jones | d9a16cd | 2012-11-12 16:09:49 -0500 | [diff] [blame] | 28 | p->AddArg(kSetuidHack); |
Elly Jones | e7cb5b3 | 2011-12-01 14:18:32 -0500 | [diff] [blame] | 29 | p->AddArg(kPing); |
Paul Stewart | c0479d8 | 2013-02-04 10:02:53 -0800 | [diff] [blame] | 30 | if (options.count("broadcast") == 1) { |
| 31 | p->AddArg("-b"); |
| 32 | } |
Elly Jones | e7cb5b3 | 2011-12-01 14:18:32 -0500 | [diff] [blame] | 33 | 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 Jones | e7cb5b3 | 2011-12-01 14:18:32 -0500 | [diff] [blame] | 38 | } |
| 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 Jones | e7cb5b3 | 2011-12-01 14:18:32 -0500 | [diff] [blame] | 54 | LOG(INFO) << "ping: running process id: " << p->id(); |
| 55 | p->Start(); |
| 56 | return p->id(); |
| 57 | } |
| 58 | |
Elly Jones | e7cb5b3 | 2011-12-01 14:18:32 -0500 | [diff] [blame] | 59 | }; // namespace debugd |