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 | |
Ben Chan | 2314289 | 2017-02-15 12:34:13 -0800 | [diff] [blame] | 7 | #include <utility> |
Elly Jones | 0c016cc | 2011-12-19 16:19:22 -0500 | [diff] [blame] | 8 | |
Ben Chan | 2314289 | 2017-02-15 12:34:13 -0800 | [diff] [blame] | 9 | #include <base/memory/ptr_util.h> |
Elly Jones | 0c016cc | 2011-12-19 16:19:22 -0500 | [diff] [blame] | 10 | |
| 11 | namespace debugd { |
| 12 | |
Ben Chan | a0011d8 | 2014-05-13 00:19:29 -0700 | [diff] [blame] | 13 | namespace { |
| 14 | |
| 15 | const char kErrorNoSuchProcess[] = "org.chromium.debugd.error.NoSuchProcess"; |
| 16 | |
| 17 | } // namespace |
Elly Jones | 0c016cc | 2011-12-19 16:19:22 -0500 | [diff] [blame] | 18 | |
Jorge Lucangeli Obes | 623f8ca | 2014-09-18 10:50:06 -0700 | [diff] [blame] | 19 | ProcessWithId* SubprocessTool::CreateProcess(bool sandboxed) { |
| 20 | return CreateProcess(sandboxed, false); |
| 21 | } |
| 22 | |
| 23 | ProcessWithId* SubprocessTool::CreateProcess(bool sandboxed, |
| 24 | bool access_root_mount_ns) { |
Ben Chan | 2314289 | 2017-02-15 12:34:13 -0800 | [diff] [blame] | 25 | auto process = base::MakeUnique<ProcessWithId>(); |
Jorge Lucangeli Obes | 623f8ca | 2014-09-18 10:50:06 -0700 | [diff] [blame] | 26 | if (!sandboxed) |
Ben Chan | 2314289 | 2017-02-15 12:34:13 -0800 | [diff] [blame] | 27 | process->DisableSandbox(); |
| 28 | |
Jorge Lucangeli Obes | 623f8ca | 2014-09-18 10:50:06 -0700 | [diff] [blame] | 29 | if (access_root_mount_ns) |
Ben Chan | 2314289 | 2017-02-15 12:34:13 -0800 | [diff] [blame] | 30 | process->AllowAccessRootMountNamespace(); |
| 31 | |
| 32 | if (!process->Init() || processes_.count(process->id()) == 1) |
Ben Chan | 64d19b2 | 2017-02-06 14:03:47 -0800 | [diff] [blame] | 33 | return nullptr; |
Ben Chan | 2314289 | 2017-02-15 12:34:13 -0800 | [diff] [blame] | 34 | |
| 35 | ProcessWithId* process_ptr = process.get(); |
| 36 | processes_[process->id()] = std::move(process); |
| 37 | return process_ptr; |
Elly Jones | 0c016cc | 2011-12-19 16:19:22 -0500 | [diff] [blame] | 38 | } |
| 39 | |
Ben Chan | a0011d8 | 2014-05-13 00:19:29 -0700 | [diff] [blame] | 40 | void SubprocessTool::Stop(const std::string& handle, DBus::Error* error) { |
Elly Jones | 0c016cc | 2011-12-19 16:19:22 -0500 | [diff] [blame] | 41 | if (processes_.count(handle) != 1) { |
Ben Chan | a0011d8 | 2014-05-13 00:19:29 -0700 | [diff] [blame] | 42 | error->set(kErrorNoSuchProcess, handle.c_str()); |
Elly Jones | 0c016cc | 2011-12-19 16:19:22 -0500 | [diff] [blame] | 43 | return; |
| 44 | } |
Ben Chan | 2314289 | 2017-02-15 12:34:13 -0800 | [diff] [blame] | 45 | ProcessWithId* process_ptr = processes_[handle].get(); |
| 46 | process_ptr->KillProcessGroup(); |
Elly Jones | 0c016cc | 2011-12-19 16:19:22 -0500 | [diff] [blame] | 47 | processes_.erase(handle); |
Elly Jones | 0c016cc | 2011-12-19 16:19:22 -0500 | [diff] [blame] | 48 | } |
| 49 | |
Ben Chan | a0011d8 | 2014-05-13 00:19:29 -0700 | [diff] [blame] | 50 | } // namespace debugd |