blob: c501008f74199a0e6179b63482207cf77d07c432 [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
5#include "ping_tool.h"
6
7#include <map>
8#include <string>
9
Elly Jonese7cb5b32011-12-01 14:18:32 -050010#include "process_with_id.h"
11
12namespace debugd {
13
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -050014const char* kSetuidHack = "/usr/libexec/debugd/helpers/minijail-setuid-hack.sh";
Elly Jonese7cb5b32011-12-01 14:18:32 -050015const char* kPing = "/bin/ping";
16
Elly Jones0c016cc2011-12-19 16:19:22 -050017PingTool::PingTool() : SubprocessTool() { }
Elly Jonese7cb5b32011-12-01 14:18:32 -050018PingTool::~PingTool() { }
19
20std::string PingTool::Start(const DBus::FileDescriptor& outfd,
21 const std::string& destination,
22 const std::map<std::string, DBus::Variant>&
23 options,
24 DBus::Error& error) {
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -050025 ProcessWithId* p = CreateProcess(true);
Elly Jones0c016cc2011-12-19 16:19:22 -050026 if (!p)
Elly Jonese7cb5b32011-12-01 14:18:32 -050027 return "";
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -050028 p->AddArg(kSetuidHack);
Elly Jonese7cb5b32011-12-01 14:18:32 -050029 p->AddArg(kPing);
30 if (options.count("count") == 1) {
31 // If we try to convert a non-int value to an int here, dbus-c++ will toss
32 // a C++ exception, which the dbus-c++ main loop will convert into a dbus
33 // exception and return it to our caller.
34 p->AddIntOption("-c", options.find("count")->second);
Elly Jonese7cb5b32011-12-01 14:18:32 -050035 }
36 if (options.count("interval") == 1) {
37 p->AddIntOption("-i", options.find("interval")->second);
38 }
39 if (options.count("numeric") == 1) {
40 p->AddArg("-n");
41 }
42 if (options.count("packetsize") == 1) {
43 p->AddIntOption("-s", options.find("packetsize")->second);
44 }
45 if (options.count("waittime") == 1) {
46 p->AddIntOption("-W", options.find("waittime")->second);
47 }
48 p->AddArg(destination);
49 p->BindFd(outfd.get(), STDOUT_FILENO);
50 p->BindFd(outfd.get(), STDERR_FILENO);
Elly Jonese7cb5b32011-12-01 14:18:32 -050051 LOG(INFO) << "ping: running process id: " << p->id();
52 p->Start();
53 return p->id();
54}
55
Elly Jonese7cb5b32011-12-01 14:18:32 -050056}; // namespace debugd