Elly Jones | a44d22d | 2012-01-05 18:05:56 -0500 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Elly Jones | 0c016cc | 2011-12-19 16:19:22 -0500 | [diff] [blame] | 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 | |
Elly Fong-Jones | d9a16cd | 2012-11-12 16:09:49 -0500 | [diff] [blame] | 20 | ProcessWithId* SubprocessTool::CreateProcess(bool sandbox) { |
Elly Jones | 0c016cc | 2011-12-19 16:19:22 -0500 | [diff] [blame] | 21 | ProcessWithId* p = new ProcessWithId(); |
Elly Fong-Jones | d9a16cd | 2012-11-12 16:09:49 -0500 | [diff] [blame] | 22 | if (!sandbox) |
| 23 | p->DisableSandbox(); |
Elly Jones | 0c016cc | 2011-12-19 16:19:22 -0500 | [diff] [blame] | 24 | if (!p->Init() || processes_.count(p->id()) == 1) |
| 25 | return NULL; |
| 26 | processes_[p->id()] = p; |
| 27 | return p; |
| 28 | } |
| 29 | |
| 30 | void 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 |