blob: a4175400b6b74235b7fae68f3e37f209cdc27fba [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
Alex Vakulenko262be3f2014-07-30 15:25:50 -07005#include "debugd/src/ping_tool.h"
Elly Jonese7cb5b32011-12-01 14:18:32 -05006
7#include <map>
8#include <string>
9
Alex Vakulenko262be3f2014-07-30 15:25:50 -070010#include "debugd/src/process_with_id.h"
Elly Jonese7cb5b32011-12-01 14:18:32 -050011
12namespace debugd {
13
Ben Chanaf125862017-02-08 23:11:18 -080014namespace {
15
16const char kSetuidHack[] =
17 "/usr/libexec/debugd/helpers/minijail-setuid-hack.sh";
18const char kPing[] = "/bin/ping";
19const char kPing6[] = "/bin/ping6";
20
21} // namespace
Elly Jonese7cb5b32011-12-01 14:18:32 -050022
Elly Jonese7cb5b32011-12-01 14:18:32 -050023std::string PingTool::Start(const DBus::FileDescriptor& outfd,
24 const std::string& destination,
Ben Chana0011d82014-05-13 00:19:29 -070025 const std::map<std::string, DBus::Variant>& options,
26 DBus::Error* error) {
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -050027 ProcessWithId* p = CreateProcess(true);
Elly Jones0c016cc2011-12-19 16:19:22 -050028 if (!p)
Elly Jonese7cb5b32011-12-01 14:18:32 -050029 return "";
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -050030 p->AddArg(kSetuidHack);
Mike Frysinger9bab10a2015-05-18 03:35:32 -040031
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 Stewartc0479d82013-02-04 10:02:53 -080038 if (options.count("broadcast") == 1) {
39 p->AddArg("-b");
40 }
Elly Jonese7cb5b32011-12-01 14:18:32 -050041 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 Jonese7cb5b32011-12-01 14:18:32 -050046 }
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 Jonese7cb5b32011-12-01 14:18:32 -050062 LOG(INFO) << "ping: running process id: " << p->id();
63 p->Start();
64 return p->id();
65}
66
Ben Chana0011d82014-05-13 00:19:29 -070067} // namespace debugd