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 | |
Alex Vakulenko | 262be3f | 2014-07-30 15:25:50 -0700 | [diff] [blame] | 5 | #include "debugd/src/ping_tool.h" |
Elly Jones | e7cb5b3 | 2011-12-01 14:18:32 -0500 | [diff] [blame] | 6 | |
| 7 | #include <map> |
| 8 | #include <string> |
| 9 | |
Alex Vakulenko | 262be3f | 2014-07-30 15:25:50 -0700 | [diff] [blame] | 10 | #include "debugd/src/process_with_id.h" |
Elly Jones | e7cb5b3 | 2011-12-01 14:18:32 -0500 | [diff] [blame] | 11 | |
| 12 | namespace debugd { |
| 13 | |
Ben Chan | af12586 | 2017-02-08 23:11:18 -0800 | [diff] [blame^] | 14 | namespace { |
| 15 | |
| 16 | const char kSetuidHack[] = |
| 17 | "/usr/libexec/debugd/helpers/minijail-setuid-hack.sh"; |
| 18 | const char kPing[] = "/bin/ping"; |
| 19 | const char kPing6[] = "/bin/ping6"; |
| 20 | |
| 21 | } // namespace |
Elly Jones | e7cb5b3 | 2011-12-01 14:18:32 -0500 | [diff] [blame] | 22 | |
Elly Jones | e7cb5b3 | 2011-12-01 14:18:32 -0500 | [diff] [blame] | 23 | std::string PingTool::Start(const DBus::FileDescriptor& outfd, |
| 24 | const std::string& destination, |
Ben Chan | a0011d8 | 2014-05-13 00:19:29 -0700 | [diff] [blame] | 25 | const std::map<std::string, DBus::Variant>& options, |
| 26 | DBus::Error* error) { |
Elly Fong-Jones | d9a16cd | 2012-11-12 16:09:49 -0500 | [diff] [blame] | 27 | ProcessWithId* p = CreateProcess(true); |
Elly Jones | 0c016cc | 2011-12-19 16:19:22 -0500 | [diff] [blame] | 28 | if (!p) |
Elly Jones | e7cb5b3 | 2011-12-01 14:18:32 -0500 | [diff] [blame] | 29 | return ""; |
Elly Fong-Jones | d9a16cd | 2012-11-12 16:09:49 -0500 | [diff] [blame] | 30 | p->AddArg(kSetuidHack); |
Mike Frysinger | 9bab10a | 2015-05-18 03:35:32 -0400 | [diff] [blame] | 31 | |
| 32 | auto option_iter = options.find("v6"); |
| 33 | if (option_iter != options.end() && option_iter->second.reader().get_bool()) |
| 34 | p->AddArg(kPing6); |
| 35 | else |
| 36 | p->AddArg(kPing); |
| 37 | |
Paul Stewart | c0479d8 | 2013-02-04 10:02:53 -0800 | [diff] [blame] | 38 | if (options.count("broadcast") == 1) { |
| 39 | p->AddArg("-b"); |
| 40 | } |
Elly Jones | e7cb5b3 | 2011-12-01 14:18:32 -0500 | [diff] [blame] | 41 | if (options.count("count") == 1) { |
| 42 | // If we try to convert a non-int value to an int here, dbus-c++ will toss |
| 43 | // a C++ exception, which the dbus-c++ main loop will convert into a dbus |
| 44 | // exception and return it to our caller. |
| 45 | p->AddIntOption("-c", options.find("count")->second); |
Elly Jones | e7cb5b3 | 2011-12-01 14:18:32 -0500 | [diff] [blame] | 46 | } |
| 47 | if (options.count("interval") == 1) { |
| 48 | p->AddIntOption("-i", options.find("interval")->second); |
| 49 | } |
| 50 | if (options.count("numeric") == 1) { |
| 51 | p->AddArg("-n"); |
| 52 | } |
| 53 | if (options.count("packetsize") == 1) { |
| 54 | p->AddIntOption("-s", options.find("packetsize")->second); |
| 55 | } |
| 56 | if (options.count("waittime") == 1) { |
| 57 | p->AddIntOption("-W", options.find("waittime")->second); |
| 58 | } |
| 59 | p->AddArg(destination); |
| 60 | p->BindFd(outfd.get(), STDOUT_FILENO); |
| 61 | p->BindFd(outfd.get(), STDERR_FILENO); |
Elly Jones | e7cb5b3 | 2011-12-01 14:18:32 -0500 | [diff] [blame] | 62 | LOG(INFO) << "ping: running process id: " << p->id(); |
| 63 | p->Start(); |
| 64 | return p->id(); |
| 65 | } |
| 66 | |
Ben Chan | a0011d8 | 2014-05-13 00:19:29 -0700 | [diff] [blame] | 67 | } // namespace debugd |