blob: 1e92a8d9b766605b38278fa67dae6878b65d7ada [file] [log] [blame]
// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "debugd/src/ping_tool.h"
#include <map>
#include <string>
#include "debugd/src/error_utils.h"
#include "debugd/src/process_with_id.h"
#include "debugd/src/variant_utils.h"
namespace debugd {
namespace {
const char kSetuidHack[] =
"/usr/libexec/debugd/helpers/minijail-setuid-hack.sh";
const char kPing[] = "/bin/ping";
const char kPing6[] = "/bin/ping6";
const char kPingToolErrorString[] = "org.chromium.debugd.error.Ping";
} // namespace
bool PingTool::Start(const dbus::FileDescriptor& outfd,
const std::string& destination,
const brillo::VariantDictionary& options,
std::string* out_id,
brillo::ErrorPtr* error) {
ProcessWithId* p = CreateProcess();
if (!p) {
DEBUGD_ADD_ERROR(
error, kPingToolErrorString, "Could not create ping process");
return false;
}
p->AddArg(kSetuidHack);
if (brillo::GetVariantValueOrDefault<bool>(options, "v6"))
p->AddArg(kPing6);
else
p->AddArg(kPing);
if (options.count("broadcast") == 1)
p->AddArg("-b");
if (!AddIntOption(p, options, "count", "-c", error))
return false;
if (!AddIntOption(p, options, "interval", "-i", error))
return false;
if (options.count("numeric") == 1)
p->AddArg("-n");
if (!AddIntOption(p, options, "packetsize", "-s", error))
return false;
if (!AddIntOption(p, options, "waittime", "-W", error))
return false;
p->AddArg(destination);
p->BindFd(outfd.value(), STDOUT_FILENO);
p->BindFd(outfd.value(), STDERR_FILENO);
LOG(INFO) << "ping: running process id: " << p->id();
p->Start();
*out_id = p->id();
return true;
}
} // namespace debugd