blob: 79b62d1bdcba71ef41d0951b0dd277f07977530c [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
Eric Caruso8fe49c72017-04-25 10:43:59 -070023bool 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-Jonesd9a16cd2012-11-12 16:09:49 -050028 ProcessWithId* p = CreateProcess(true);
Elly Jones0c016cc2011-12-19 16:19:22 -050029 if (!p)
Eric Caruso8fe49c72017-04-25 10:43:59 -070030 return false;
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -050031 p->AddArg(kSetuidHack);
Mike Frysinger9bab10a2015-05-18 03:35:32 -040032
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 Stewartc0479d82013-02-04 10:02:53 -080039 if (options.count("broadcast") == 1) {
40 p->AddArg("-b");
41 }
Elly Jonese7cb5b32011-12-01 14:18:32 -050042 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 Jonese7cb5b32011-12-01 14:18:32 -050047 }
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 Jonese7cb5b32011-12-01 14:18:32 -050063 LOG(INFO) << "ping: running process id: " << p->id();
64 p->Start();
Eric Caruso8fe49c72017-04-25 10:43:59 -070065 *out_id = p->id();
66 return true;
Elly Jonese7cb5b32011-12-01 14:18:32 -050067}
68
Ben Chana0011d82014-05-13 00:19:29 -070069} // namespace debugd