Elly Jones | 0c016cc | 2011-12-19 16:19:22 -0500 | [diff] [blame^] | 1 | // Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
| 2 | // 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 | |
| 12 | namespace debugd { |
| 13 | |
| 14 | const char* kErrorNoSuchProcess = |
| 15 | "org.chromium.debugd.error.NoSuchProcess"; |
| 16 | |
| 17 | SubprocessTool::SubprocessTool() { } |
| 18 | SubprocessTool::~SubprocessTool() { } |
| 19 | |
| 20 | ProcessWithId* SubprocessTool::CreateProcess() { |
| 21 | ProcessWithId* p = new ProcessWithId(); |
| 22 | if (!p->Init() || processes_.count(p->id()) == 1) |
| 23 | return NULL; |
| 24 | processes_[p->id()] = p; |
| 25 | return p; |
| 26 | } |
| 27 | |
| 28 | void SubprocessTool::Stop(const std::string& handle, DBus::Error& error) { |
| 29 | if (processes_.count(handle) != 1) { |
| 30 | error.set(kErrorNoSuchProcess, handle.c_str()); |
| 31 | return; |
| 32 | } |
| 33 | ProcessWithId* p = processes_[handle]; |
| 34 | p->Kill(SIGKILL, 0); // no timeout |
| 35 | p->Wait(); |
| 36 | processes_.erase(handle); |
| 37 | delete p; |
| 38 | } |
| 39 | |
| 40 | }; // namespace debugd |