blob: 36bad0cacff7e5f4106ca8d8f4ed6367bd86acfb [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
Eric Carusocc7106c2017-04-27 14:22:42 -070010#include "debugd/src/error_utils.h"
Alex Vakulenko262be3f2014-07-30 15:25:50 -070011#include "debugd/src/process_with_id.h"
Eric Carusocc7106c2017-04-27 14:22:42 -070012#include "debugd/src/variant_utils.h"
Elly Jonese7cb5b32011-12-01 14:18:32 -050013
14namespace debugd {
15
Ben Chanaf125862017-02-08 23:11:18 -080016namespace {
17
18const char kSetuidHack[] =
19 "/usr/libexec/debugd/helpers/minijail-setuid-hack.sh";
20const char kPing[] = "/bin/ping";
21const char kPing6[] = "/bin/ping6";
22
Eric Carusocc7106c2017-04-27 14:22:42 -070023const char kPingToolErrorString[] = "org.chromium.debugd.error.Ping";
24
Ben Chanaf125862017-02-08 23:11:18 -080025} // namespace
Elly Jonese7cb5b32011-12-01 14:18:32 -050026
Eric Carusocc7106c2017-04-27 14:22:42 -070027bool PingTool::Start(const dbus::FileDescriptor& outfd,
Eric Caruso8fe49c72017-04-25 10:43:59 -070028 const std::string& destination,
Eric Carusocc7106c2017-04-27 14:22:42 -070029 const brillo::VariantDictionary& options,
Eric Caruso8fe49c72017-04-25 10:43:59 -070030 std::string* out_id,
Eric Carusocc7106c2017-04-27 14:22:42 -070031 brillo::ErrorPtr* error) {
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -050032 ProcessWithId* p = CreateProcess(true);
Eric Carusocc7106c2017-04-27 14:22:42 -070033 if (!p) {
34 DEBUGD_ADD_ERROR(
35 error, kPingToolErrorString, "Could not create ping process");
Eric Caruso8fe49c72017-04-25 10:43:59 -070036 return false;
Eric Carusocc7106c2017-04-27 14:22:42 -070037 }
Mike Frysinger9bab10a2015-05-18 03:35:32 -040038
Eric Carusocc7106c2017-04-27 14:22:42 -070039 p->AddArg(kSetuidHack);
40 if (brillo::GetVariantValueOrDefault<bool>(options, "v6"))
Mike Frysinger9bab10a2015-05-18 03:35:32 -040041 p->AddArg(kPing6);
42 else
43 p->AddArg(kPing);
44
Eric Carusocc7106c2017-04-27 14:22:42 -070045 if (options.count("broadcast") == 1)
Paul Stewartc0479d82013-02-04 10:02:53 -080046 p->AddArg("-b");
Eric Carusocc7106c2017-04-27 14:22:42 -070047 if (!AddIntOption(p, options, "count", "-c", error))
48 return false;
49 if (!AddIntOption(p, options, "interval", "-i", error))
50 return false;
51 if (options.count("numeric") == 1)
Elly Jonese7cb5b32011-12-01 14:18:32 -050052 p->AddArg("-n");
Eric Carusocc7106c2017-04-27 14:22:42 -070053 if (!AddIntOption(p, options, "packetsize", "-s", error))
54 return false;
55 if (!AddIntOption(p, options, "waittime", "-W", error))
56 return false;
57
Elly Jonese7cb5b32011-12-01 14:18:32 -050058 p->AddArg(destination);
Eric Carusocc7106c2017-04-27 14:22:42 -070059 p->BindFd(outfd.value(), STDOUT_FILENO);
60 p->BindFd(outfd.value(), STDERR_FILENO);
Elly Jonese7cb5b32011-12-01 14:18:32 -050061 LOG(INFO) << "ping: running process id: " << p->id();
62 p->Start();
Eric Caruso8fe49c72017-04-25 10:43:59 -070063 *out_id = p->id();
64 return true;
Elly Jonese7cb5b32011-12-01 14:18:32 -050065}
66
Ben Chana0011d82014-05-13 00:19:29 -070067} // namespace debugd