blob: a40945524e2bf499b36bafbf686d4756b718c61e [file] [log] [blame]
Elly Jonesa44d22d2012-01-05 18:05:56 -05001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Elly Jones0c016cc2011-12-19 16:19:22 -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 "subprocess_tool.h"
6
Elly Jones0c016cc2011-12-19 16:19:22 -05007#include <signal.h>
8
Ben Chana0011d82014-05-13 00:19:29 -07009#include <dbus-c++/dbus.h>
10
Elly Jones0c016cc2011-12-19 16:19:22 -050011#include "process_with_id.h"
12
13namespace debugd {
14
Ben Chana0011d82014-05-13 00:19:29 -070015namespace {
16
17const char kErrorNoSuchProcess[] = "org.chromium.debugd.error.NoSuchProcess";
18
19} // namespace
Elly Jones0c016cc2011-12-19 16:19:22 -050020
21SubprocessTool::SubprocessTool() { }
22SubprocessTool::~SubprocessTool() { }
23
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -050024ProcessWithId* SubprocessTool::CreateProcess(bool sandbox) {
Elly Jones0c016cc2011-12-19 16:19:22 -050025 ProcessWithId* p = new ProcessWithId();
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -050026 if (!sandbox)
27 p->DisableSandbox();
Elly Jones0c016cc2011-12-19 16:19:22 -050028 if (!p->Init() || processes_.count(p->id()) == 1)
29 return NULL;
30 processes_[p->id()] = p;
31 return p;
32}
33
Ben Chana0011d82014-05-13 00:19:29 -070034void SubprocessTool::Stop(const std::string& handle, DBus::Error* error) {
Elly Jones0c016cc2011-12-19 16:19:22 -050035 if (processes_.count(handle) != 1) {
Ben Chana0011d82014-05-13 00:19:29 -070036 error->set(kErrorNoSuchProcess, handle.c_str());
Elly Jones0c016cc2011-12-19 16:19:22 -050037 return;
38 }
39 ProcessWithId* p = processes_[handle];
40 p->Kill(SIGKILL, 0); // no timeout
41 p->Wait();
42 processes_.erase(handle);
43 delete p;
44}
45
Ben Chana0011d82014-05-13 00:19:29 -070046} // namespace debugd