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 | |
Jorge Lucangeli Obes | 623f8ca | 2014-09-18 10:50:06 -0700 | [diff] [blame] | 21 | ProcessWithId* SubprocessTool::CreateProcess(bool sandboxed) { |
| 22 | return CreateProcess(sandboxed, false); |
| 23 | } |
| 24 | |
| 25 | ProcessWithId* SubprocessTool::CreateProcess(bool sandboxed, |
| 26 | bool access_root_mount_ns) { |
Elly Jones | 0c016cc | 2011-12-19 16:19:22 -0500 | [diff] [blame] | 27 | ProcessWithId* p = new ProcessWithId(); |
Jorge Lucangeli Obes | 623f8ca | 2014-09-18 10:50:06 -0700 | [diff] [blame] | 28 | if (!sandboxed) |
Elly Fong-Jones | d9a16cd | 2012-11-12 16:09:49 -0500 | [diff] [blame] | 29 | p->DisableSandbox(); |
Jorge Lucangeli Obes | 623f8ca | 2014-09-18 10:50:06 -0700 | [diff] [blame] | 30 | if (access_root_mount_ns) |
| 31 | p->AllowAccessRootMountNamespace(); |
Elly Jones | 0c016cc | 2011-12-19 16:19:22 -0500 | [diff] [blame] | 32 | if (!p->Init() || processes_.count(p->id()) == 1) |
| 33 | return NULL; |
| 34 | processes_[p->id()] = p; |
| 35 | return p; |
| 36 | } |
| 37 | |
Ben Chan | a0011d8 | 2014-05-13 00:19:29 -0700 | [diff] [blame] | 38 | void SubprocessTool::Stop(const std::string& handle, DBus::Error* error) { |
Elly Jones | 0c016cc | 2011-12-19 16:19:22 -0500 | [diff] [blame] | 39 | if (processes_.count(handle) != 1) { |
Ben Chan | a0011d8 | 2014-05-13 00:19:29 -0700 | [diff] [blame] | 40 | error->set(kErrorNoSuchProcess, handle.c_str()); |
Elly Jones | 0c016cc | 2011-12-19 16:19:22 -0500 | [diff] [blame] | 41 | return; |
| 42 | } |
| 43 | ProcessWithId* p = processes_[handle]; |
| 44 | p->Kill(SIGKILL, 0); // no timeout |
| 45 | p->Wait(); |
| 46 | processes_.erase(handle); |
| 47 | delete p; |
| 48 | } |
| 49 | |
Ben Chan | a0011d8 | 2014-05-13 00:19:29 -0700 | [diff] [blame] | 50 | } // namespace debugd |