blob: 1adeca029ee195db9ca4963ad995d6347089ab04 [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
7#include <dbus-c++/dbus.h>
8#include <signal.h>
9
10#include "process_with_id.h"
11
12namespace debugd {
13
14const char* kErrorNoSuchProcess =
15 "org.chromium.debugd.error.NoSuchProcess";
16
17SubprocessTool::SubprocessTool() { }
18SubprocessTool::~SubprocessTool() { }
19
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -050020ProcessWithId* SubprocessTool::CreateProcess(bool sandbox) {
Elly Jones0c016cc2011-12-19 16:19:22 -050021 ProcessWithId* p = new ProcessWithId();
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -050022 if (!sandbox)
23 p->DisableSandbox();
Elly Jones0c016cc2011-12-19 16:19:22 -050024 if (!p->Init() || processes_.count(p->id()) == 1)
25 return NULL;
26 processes_[p->id()] = p;
27 return p;
28}
29
30void SubprocessTool::Stop(const std::string& handle, DBus::Error& error) {
31 if (processes_.count(handle) != 1) {
32 error.set(kErrorNoSuchProcess, handle.c_str());
33 return;
34 }
35 ProcessWithId* p = processes_[handle];
36 p->Kill(SIGKILL, 0); // no timeout
37 p->Wait();
38 processes_.erase(handle);
39 delete p;
40}
41
42}; // namespace debugd