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 | |
Alex Vakulenko | 262be3f | 2014-07-30 15:25:50 -0700 | [diff] [blame] | 5 | #include "debugd/src/subprocess_tool.h" |
Elly Jones | 0c016cc | 2011-12-19 16:19:22 -0500 | [diff] [blame] | 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 | |
Alex Vakulenko | 262be3f | 2014-07-30 15:25:50 -0700 | [diff] [blame] | 11 | #include "debugd/src/process_with_id.h" |
Elly Jones | 0c016cc | 2011-12-19 16:19:22 -0500 | [diff] [blame] | 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 | |
Elly Fong-Jones | d9a16cd | 2012-11-12 16:09:49 -0500 | [diff] [blame] | 21 | ProcessWithId* SubprocessTool::CreateProcess(bool sandbox) { |
Elly Jones | 0c016cc | 2011-12-19 16:19:22 -0500 | [diff] [blame] | 22 | ProcessWithId* p = new ProcessWithId(); |
Elly Fong-Jones | d9a16cd | 2012-11-12 16:09:49 -0500 | [diff] [blame] | 23 | if (!sandbox) |
| 24 | p->DisableSandbox(); |
Elly Jones | 0c016cc | 2011-12-19 16:19:22 -0500 | [diff] [blame] | 25 | if (!p->Init() || processes_.count(p->id()) == 1) |
| 26 | return NULL; |
| 27 | processes_[p->id()] = p; |
| 28 | return p; |
| 29 | } |
| 30 | |
Ben Chan | a0011d8 | 2014-05-13 00:19:29 -0700 | [diff] [blame] | 31 | void SubprocessTool::Stop(const std::string& handle, DBus::Error* error) { |
Elly Jones | 0c016cc | 2011-12-19 16:19:22 -0500 | [diff] [blame] | 32 | if (processes_.count(handle) != 1) { |
Ben Chan | a0011d8 | 2014-05-13 00:19:29 -0700 | [diff] [blame] | 33 | error->set(kErrorNoSuchProcess, handle.c_str()); |
Elly Jones | 0c016cc | 2011-12-19 16:19:22 -0500 | [diff] [blame] | 34 | return; |
| 35 | } |
| 36 | ProcessWithId* p = processes_[handle]; |
| 37 | p->Kill(SIGKILL, 0); // no timeout |
| 38 | p->Wait(); |
| 39 | processes_.erase(handle); |
| 40 | delete p; |
| 41 | } |
| 42 | |
Ben Chan | a0011d8 | 2014-05-13 00:19:29 -0700 | [diff] [blame] | 43 | } // namespace debugd |