Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2015 The Chromium Authors |
rickyz | 5b937a3 | 2015-10-21 23:52:20 | [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 | |
Chris Findeisen | 2a373a2d | 2019-08-15 19:44:30 | [diff] [blame] | 5 | #include "base/process/process_handle.h" |
| 6 | |
rickyz | 5b937a3 | 2015-10-21 23:52:20 | [diff] [blame] | 7 | #include <stdint.h> |
| 8 | |
Hans Wennborg | afeb390 | 2020-06-17 14:42:29 | [diff] [blame] | 9 | #include <ostream> |
| 10 | |
Hans Wennborg | c3cffa6 | 2020-04-27 10:09:12 | [diff] [blame] | 11 | #include "base/check.h" |
rickyz | 5b937a3 | 2015-10-21 23:52:20 | [diff] [blame] | 12 | #include "build/build_config.h" |
| 13 | |
| 14 | namespace base { |
| 15 | |
| 16 | namespace { |
Chris Findeisen | 2a373a2d | 2019-08-15 19:44:30 | [diff] [blame] | 17 | ProcessId g_pid_outside_of_namespace = kNullProcessId; |
rickyz | 5b937a3 | 2015-10-21 23:52:20 | [diff] [blame] | 18 | } // namespace |
| 19 | |
Chris Findeisen | 2a373a2d | 2019-08-15 19:44:30 | [diff] [blame] | 20 | std::ostream& operator<<(std::ostream& os, const UniqueProcId& obj) { |
| 21 | os << obj.GetUnsafeValue(); |
| 22 | return os; |
| 23 | } |
rickyz | 5b937a3 | 2015-10-21 23:52:20 | [diff] [blame] | 24 | |
Chris Findeisen | 2a373a2d | 2019-08-15 19:44:30 | [diff] [blame] | 25 | UniqueProcId GetUniqueIdForProcess() { |
| 26 | // Used for logging. Must not use LogMessage or any of the macros that call |
| 27 | // into it. |
| 28 | return (g_pid_outside_of_namespace != kNullProcessId) |
| 29 | ? UniqueProcId(g_pid_outside_of_namespace) |
| 30 | : UniqueProcId(GetCurrentProcId()); |
rickyz | 5b937a3 | 2015-10-21 23:52:20 | [diff] [blame] | 31 | } |
| 32 | |
Xiaohan Wang | 37e8161 | 2022-01-15 18:27:00 | [diff] [blame] | 33 | #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_AIX) |
rickyz | 5b937a3 | 2015-10-21 23:52:20 | [diff] [blame] | 34 | |
| 35 | void InitUniqueIdForProcessInPidNamespace(ProcessId pid_outside_of_namespace) { |
Chris Findeisen | 2a373a2d | 2019-08-15 19:44:30 | [diff] [blame] | 36 | DCHECK(pid_outside_of_namespace != kNullProcessId); |
| 37 | g_pid_outside_of_namespace = pid_outside_of_namespace; |
rickyz | 5b937a3 | 2015-10-21 23:52:20 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | #endif |
| 41 | |
| 42 | } // namespace base |