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); |
| 30 | if (options.count("count") == 1) { |
| 31 | // If we try to convert a non-int value to an int here, dbus-c++ will toss |
| 32 | // a C++ exception, which the dbus-c++ main loop will convert into a dbus |
| 33 | // exception and return it to our caller. |
| 34 | p->AddIntOption("-c", options.find("count")->second); |
Elly Jones | e7cb5b3 | 2011-12-01 14:18:32 -0500 | [diff] [blame] | 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 Jones | e7cb5b3 | 2011-12-01 14:18:32 -0500 | [diff] [blame] | 51 | LOG(INFO) << "ping: running process id: " << p->id(); |
| 52 | p->Start(); |
| 53 | return p->id(); |
| 54 | } |
| 55 | |
Elly Jones | e7cb5b3 | 2011-12-01 14:18:32 -0500 | [diff] [blame] | 56 | }; // namespace debugd |