blob: d191d152efb6cbf5c60b1d51e06562693d5ed644 [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
Elly Jonese7cb5b32011-12-01 14:18:32 -05007#include <string>
8
Eric Carusocc7106c2017-04-27 14:22:42 -07009#include "debugd/src/error_utils.h"
Alex Vakulenko262be3f2014-07-30 15:25:50 -070010#include "debugd/src/process_with_id.h"
Eric Carusocc7106c2017-04-27 14:22:42 -070011#include "debugd/src/variant_utils.h"
Elly Jonese7cb5b32011-12-01 14:18:32 -050012
13namespace debugd {
14
Ben Chanaf125862017-02-08 23:11:18 -080015namespace {
16
17const char kSetuidHack[] =
18 "/usr/libexec/debugd/helpers/minijail-setuid-hack.sh";
19const char kPing[] = "/bin/ping";
20const char kPing6[] = "/bin/ping6";
21
Eric Carusocc7106c2017-04-27 14:22:42 -070022const char kPingToolErrorString[] = "org.chromium.debugd.error.Ping";
23
Ben Chanaf125862017-02-08 23:11:18 -080024} // namespace
Elly Jonese7cb5b32011-12-01 14:18:32 -050025
Eric Caruso0b241882018-04-04 13:43:46 -070026bool PingTool::Start(const base::ScopedFD& outfd,
Eric Caruso8fe49c72017-04-25 10:43:59 -070027 const std::string& destination,
Eric Carusocc7106c2017-04-27 14:22:42 -070028 const brillo::VariantDictionary& options,
Eric Caruso8fe49c72017-04-25 10:43:59 -070029 std::string* out_id,
Eric Carusocc7106c2017-04-27 14:22:42 -070030 brillo::ErrorPtr* error) {
Tom Hughesd6c2d392020-08-24 18:12:11 -070031 ProcessWithId* p = CreateProcess(
32 true /* sandboxed */, false /* access_root_mount_ns */,
33 {"-pvrl", "--profile=minimalistic-mountns", "--uts", "-k",
34 "tmpfs,/run,tmpfs,MS_NODEV|MS_NOEXEC|MS_NOSUID,mode=755,size=10M",
Ben Chanb1351362019-02-20 11:14:20 -080035 // A /run/shill bind mount is needed to access /etc/resolv.conf, which
36 // is a symlink to /run/shill/resolv.conf.
Mike Frysinger56379d72019-02-19 16:03:03 -050037 "-b", "/run/shill"});
Eric Carusocc7106c2017-04-27 14:22:42 -070038 if (!p) {
Tom Hughesd6c2d392020-08-24 18:12:11 -070039 DEBUGD_ADD_ERROR(error, kPingToolErrorString,
40 "Could not create ping process");
Eric Caruso8fe49c72017-04-25 10:43:59 -070041 return false;
Eric Carusocc7106c2017-04-27 14:22:42 -070042 }
Mike Frysinger9bab10a2015-05-18 03:35:32 -040043
Eric Carusocc7106c2017-04-27 14:22:42 -070044 p->AddArg(kSetuidHack);
45 if (brillo::GetVariantValueOrDefault<bool>(options, "v6"))
Mike Frysinger9bab10a2015-05-18 03:35:32 -040046 p->AddArg(kPing6);
47 else
48 p->AddArg(kPing);
49
Eric Carusocc7106c2017-04-27 14:22:42 -070050 if (options.count("broadcast") == 1)
Paul Stewartc0479d82013-02-04 10:02:53 -080051 p->AddArg("-b");
Eric Carusocc7106c2017-04-27 14:22:42 -070052 if (!AddIntOption(p, options, "count", "-c", error))
53 return false;
54 if (!AddIntOption(p, options, "interval", "-i", error))
55 return false;
56 if (options.count("numeric") == 1)
Elly Jonese7cb5b32011-12-01 14:18:32 -050057 p->AddArg("-n");
Eric Carusocc7106c2017-04-27 14:22:42 -070058 if (!AddIntOption(p, options, "packetsize", "-s", error))
59 return false;
60 if (!AddIntOption(p, options, "waittime", "-W", error))
61 return false;
62
Alex Khouderchah258d3ca2019-07-31 17:40:54 -070063 auto interface = options.find("interface");
64 if (interface != options.end()) {
65 p->AddStringOption("-I", interface->second.Get<std::string>());
66 }
67
Elly Jonese7cb5b32011-12-01 14:18:32 -050068 p->AddArg(destination);
Eric Caruso0b241882018-04-04 13:43:46 -070069 p->BindFd(outfd.get(), STDOUT_FILENO);
70 p->BindFd(outfd.get(), STDERR_FILENO);
Elly Jonese7cb5b32011-12-01 14:18:32 -050071 LOG(INFO) << "ping: running process id: " << p->id();
72 p->Start();
Eric Caruso8fe49c72017-04-25 10:43:59 -070073 *out_id = p->id();
74 return true;
Elly Jonese7cb5b32011-12-01 14:18:32 -050075}
76
Ben Chana0011d82014-05-13 00:19:29 -070077} // namespace debugd