OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include <stdint.h> | |
6 | |
7 #include "base/logging.h" | |
8 #include "base/process/process_handle.h" | |
9 #include "build/build_config.h" | |
10 | |
11 namespace base { | |
12 | |
13 namespace { | |
14 // This is never a valid mangled process ID. | |
danakj
2015/09/23 21:56:32
This is getting kinda magical and hard to really b
rickyz (no longer on Chrome)
2015/09/24 01:22:03
Good idea, done
| |
15 const uint32_t kInvalidUniqueId = 0xffffffffU; | |
16 | |
17 uint32_t g_unique_id = kInvalidUniqueId; | |
18 | |
19 // The process which set |g_unique_id|. | |
20 ProcessId g_procid; | |
21 | |
22 // Mangle IDs so that they are not accidentally used as PIDs, e.g. as an | |
23 // argument to kill or waitpid. | |
24 uint32_t MangleProcessId(ProcessId process_id) { | |
25 // Add a large power of 10 so that the pid is still the pid is still readable | |
danakj
2015/09/23 21:56:31
"the pid is still the pid is still readable"
"the
rickyz (no longer on Chrome)
2015/09/24 01:22:03
Done
| |
26 // for debugging | |
27 return static_cast<uint32_t>(process_id) + 1000000000U; | |
28 } | |
29 | |
30 } // namespace | |
31 | |
32 uint32_t GetUniqueIdForProcess() { | |
33 if (g_unique_id == kInvalidUniqueId) { | |
34 return MangleProcessId(GetCurrentProcId()); | |
35 } | |
36 | |
37 // Make sure we are the same process that set |g_procid|. This check may have | |
38 // false negatives (if a process ID was reused) but should have no false | |
39 // positives. | |
40 DCHECK_EQ(GetCurrentProcId(), g_procid); | |
41 return g_unique_id; | |
42 } | |
43 | |
44 #if defined(OS_LINUX) | |
45 | |
46 void InitUniqueIdForProcessInPidNamespace(ProcessId pid_outside_of_namespace) { | |
47 g_unique_id = MangleProcessId(pid_outside_of_namespace); | |
48 g_procid = GetCurrentProcId(); | |
49 } | |
50 | |
51 #endif | |
52 | |
53 } // namespace base | |
OLD | NEW |