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