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> |
Mike Frysinger | f9da3d3 | 2017-09-19 23:41:27 -0400 | [diff] [blame^] | 10 | #include <base/stl_util.h> |
Elly Jones | 0c016cc | 2011-12-19 16:19:22 -0500 | [diff] [blame] | 11 | |
Eric Caruso | cc7106c | 2017-04-27 14:22:42 -0700 | [diff] [blame] | 12 | #include "debugd/src/error_utils.h" |
| 13 | |
Elly Jones | 0c016cc | 2011-12-19 16:19:22 -0500 | [diff] [blame] | 14 | namespace debugd { |
| 15 | |
Ben Chan | a0011d8 | 2014-05-13 00:19:29 -0700 | [diff] [blame] | 16 | namespace { |
| 17 | |
| 18 | const char kErrorNoSuchProcess[] = "org.chromium.debugd.error.NoSuchProcess"; |
| 19 | |
| 20 | } // namespace |
Elly Jones | 0c016cc | 2011-12-19 16:19:22 -0500 | [diff] [blame] | 21 | |
Jorge Lucangeli Obes | 623f8ca | 2014-09-18 10:50:06 -0700 | [diff] [blame] | 22 | ProcessWithId* SubprocessTool::CreateProcess(bool sandboxed, |
| 23 | bool access_root_mount_ns) { |
Ben Chan | 2314289 | 2017-02-15 12:34:13 -0800 | [diff] [blame] | 24 | auto process = base::MakeUnique<ProcessWithId>(); |
Jorge Lucangeli Obes | 623f8ca | 2014-09-18 10:50:06 -0700 | [diff] [blame] | 25 | if (!sandboxed) |
Ben Chan | 2314289 | 2017-02-15 12:34:13 -0800 | [diff] [blame] | 26 | process->DisableSandbox(); |
| 27 | |
Jorge Lucangeli Obes | 623f8ca | 2014-09-18 10:50:06 -0700 | [diff] [blame] | 28 | if (access_root_mount_ns) |
Ben Chan | 2314289 | 2017-02-15 12:34:13 -0800 | [diff] [blame] | 29 | process->AllowAccessRootMountNamespace(); |
| 30 | |
Mike Frysinger | f9da3d3 | 2017-09-19 23:41:27 -0400 | [diff] [blame^] | 31 | if (!process->Init()) |
Ben Chan | 64d19b2 | 2017-02-06 14:03:47 -0800 | [diff] [blame] | 32 | return nullptr; |
Ben Chan | 2314289 | 2017-02-15 12:34:13 -0800 | [diff] [blame] | 33 | |
| 34 | ProcessWithId* process_ptr = process.get(); |
Mike Frysinger | f9da3d3 | 2017-09-19 23:41:27 -0400 | [diff] [blame^] | 35 | if (RecordProcess(std::move(process))) |
| 36 | return process_ptr; |
| 37 | |
| 38 | return nullptr; |
| 39 | } |
| 40 | |
| 41 | bool SubprocessTool::RecordProcess(std::unique_ptr<ProcessWithId> process) { |
| 42 | if (ContainsKey(processes_, process->id())) |
| 43 | return false; |
| 44 | |
Ben Chan | 2314289 | 2017-02-15 12:34:13 -0800 | [diff] [blame] | 45 | processes_[process->id()] = std::move(process); |
Mike Frysinger | f9da3d3 | 2017-09-19 23:41:27 -0400 | [diff] [blame^] | 46 | return true; |
Elly Jones | 0c016cc | 2011-12-19 16:19:22 -0500 | [diff] [blame] | 47 | } |
| 48 | |
Eric Caruso | cc7106c | 2017-04-27 14:22:42 -0700 | [diff] [blame] | 49 | bool SubprocessTool::Stop(const std::string& handle, brillo::ErrorPtr* error) { |
Elly Jones | 0c016cc | 2011-12-19 16:19:22 -0500 | [diff] [blame] | 50 | if (processes_.count(handle) != 1) { |
Eric Caruso | cc7106c | 2017-04-27 14:22:42 -0700 | [diff] [blame] | 51 | DEBUGD_ADD_ERROR(error, kErrorNoSuchProcess, handle.c_str()); |
Eric Caruso | 8fe49c7 | 2017-04-25 10:43:59 -0700 | [diff] [blame] | 52 | return false; |
Elly Jones | 0c016cc | 2011-12-19 16:19:22 -0500 | [diff] [blame] | 53 | } |
Ben Chan | 2314289 | 2017-02-15 12:34:13 -0800 | [diff] [blame] | 54 | ProcessWithId* process_ptr = processes_[handle].get(); |
| 55 | process_ptr->KillProcessGroup(); |
Elly Jones | 0c016cc | 2011-12-19 16:19:22 -0500 | [diff] [blame] | 56 | processes_.erase(handle); |
Eric Caruso | 8fe49c7 | 2017-04-25 10:43:59 -0700 | [diff] [blame] | 57 | return true; |
Elly Jones | 0c016cc | 2011-12-19 16:19:22 -0500 | [diff] [blame] | 58 | } |
| 59 | |
Ben Chan | a0011d8 | 2014-05-13 00:19:29 -0700 | [diff] [blame] | 60 | } // namespace debugd |