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 | |
Elly Jones | 0c016cc | 2011-12-19 16:19:22 -0500 | [diff] [blame] | 7 | #include <signal.h> |
| 8 | |
Ben Chan | a0011d8 | 2014-05-13 00:19:29 -0700 | [diff] [blame^] | 9 | #include <dbus-c++/dbus.h> |
| 10 | |
Elly Jones | 0c016cc | 2011-12-19 16:19:22 -0500 | [diff] [blame] | 11 | #include "process_with_id.h" |
| 12 | |
| 13 | namespace debugd { |
| 14 | |
Ben Chan | a0011d8 | 2014-05-13 00:19:29 -0700 | [diff] [blame^] | 15 | namespace { |
| 16 | |
| 17 | const char kErrorNoSuchProcess[] = "org.chromium.debugd.error.NoSuchProcess"; |
| 18 | |
| 19 | } // namespace |
Elly Jones | 0c016cc | 2011-12-19 16:19:22 -0500 | [diff] [blame] | 20 | |
| 21 | SubprocessTool::SubprocessTool() { } |
| 22 | SubprocessTool::~SubprocessTool() { } |
| 23 | |
Elly Fong-Jones | d9a16cd | 2012-11-12 16:09:49 -0500 | [diff] [blame] | 24 | ProcessWithId* SubprocessTool::CreateProcess(bool sandbox) { |
Elly Jones | 0c016cc | 2011-12-19 16:19:22 -0500 | [diff] [blame] | 25 | ProcessWithId* p = new ProcessWithId(); |
Elly Fong-Jones | d9a16cd | 2012-11-12 16:09:49 -0500 | [diff] [blame] | 26 | if (!sandbox) |
| 27 | p->DisableSandbox(); |
Elly Jones | 0c016cc | 2011-12-19 16:19:22 -0500 | [diff] [blame] | 28 | if (!p->Init() || processes_.count(p->id()) == 1) |
| 29 | return NULL; |
| 30 | processes_[p->id()] = p; |
| 31 | return p; |
| 32 | } |
| 33 | |
Ben Chan | a0011d8 | 2014-05-13 00:19:29 -0700 | [diff] [blame^] | 34 | void SubprocessTool::Stop(const std::string& handle, DBus::Error* error) { |
Elly Jones | 0c016cc | 2011-12-19 16:19:22 -0500 | [diff] [blame] | 35 | if (processes_.count(handle) != 1) { |
Ben Chan | a0011d8 | 2014-05-13 00:19:29 -0700 | [diff] [blame^] | 36 | error->set(kErrorNoSuchProcess, handle.c_str()); |
Elly Jones | 0c016cc | 2011-12-19 16:19:22 -0500 | [diff] [blame] | 37 | 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 Chan | a0011d8 | 2014-05-13 00:19:29 -0700 | [diff] [blame^] | 46 | } // namespace debugd |