xref: /aosp_15_r20/external/libchrome/base/process/process_handle.cc (revision 635a864187cb8b6c713ff48b7e790a6b21769273)
1*635a8641SAndroid Build Coastguard Worker // Copyright 2015 The Chromium Authors. All rights reserved.
2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file.
4*635a8641SAndroid Build Coastguard Worker 
5*635a8641SAndroid Build Coastguard Worker #include <stdint.h>
6*635a8641SAndroid Build Coastguard Worker 
7*635a8641SAndroid Build Coastguard Worker #include "base/logging.h"
8*635a8641SAndroid Build Coastguard Worker #include "base/process/process_handle.h"
9*635a8641SAndroid Build Coastguard Worker #include "build/build_config.h"
10*635a8641SAndroid Build Coastguard Worker 
11*635a8641SAndroid Build Coastguard Worker namespace base {
12*635a8641SAndroid Build Coastguard Worker 
13*635a8641SAndroid Build Coastguard Worker namespace {
14*635a8641SAndroid Build Coastguard Worker bool g_have_unique_id = false;
15*635a8641SAndroid Build Coastguard Worker uint32_t g_unique_id;
16*635a8641SAndroid Build Coastguard Worker 
17*635a8641SAndroid Build Coastguard Worker // The process which set |g_unique_id|.
18*635a8641SAndroid Build Coastguard Worker ProcessId g_procid;
19*635a8641SAndroid Build Coastguard Worker 
20*635a8641SAndroid Build Coastguard Worker // Mangle IDs so that they are not accidentally used as PIDs, e.g. as an
21*635a8641SAndroid Build Coastguard Worker // argument to kill or waitpid.
MangleProcessId(ProcessId process_id)22*635a8641SAndroid Build Coastguard Worker uint32_t MangleProcessId(ProcessId process_id) {
23*635a8641SAndroid Build Coastguard Worker   // Add a large power of 10 so that the pid is still the pid is still readable
24*635a8641SAndroid Build Coastguard Worker   // inside the mangled id.
25*635a8641SAndroid Build Coastguard Worker   return static_cast<uint32_t>(process_id) + 1000000000U;
26*635a8641SAndroid Build Coastguard Worker }
27*635a8641SAndroid Build Coastguard Worker 
28*635a8641SAndroid Build Coastguard Worker }  // namespace
29*635a8641SAndroid Build Coastguard Worker 
GetUniqueIdForProcess()30*635a8641SAndroid Build Coastguard Worker uint32_t GetUniqueIdForProcess() {
31*635a8641SAndroid Build Coastguard Worker   if (!g_have_unique_id) {
32*635a8641SAndroid Build Coastguard Worker     return MangleProcessId(GetCurrentProcId());
33*635a8641SAndroid Build Coastguard Worker   }
34*635a8641SAndroid Build Coastguard Worker 
35*635a8641SAndroid Build Coastguard Worker   // Make sure we are the same process that set |g_procid|. This check may have
36*635a8641SAndroid Build Coastguard Worker   // false negatives (if a process ID was reused) but should have no false
37*635a8641SAndroid Build Coastguard Worker   // positives.
38*635a8641SAndroid Build Coastguard Worker   DCHECK_EQ(GetCurrentProcId(), g_procid);
39*635a8641SAndroid Build Coastguard Worker   return g_unique_id;
40*635a8641SAndroid Build Coastguard Worker }
41*635a8641SAndroid Build Coastguard Worker 
42*635a8641SAndroid Build Coastguard Worker #if defined(OS_LINUX) || defined(OS_AIX)
43*635a8641SAndroid Build Coastguard Worker 
InitUniqueIdForProcessInPidNamespace(ProcessId pid_outside_of_namespace)44*635a8641SAndroid Build Coastguard Worker void InitUniqueIdForProcessInPidNamespace(ProcessId pid_outside_of_namespace) {
45*635a8641SAndroid Build Coastguard Worker   g_unique_id = MangleProcessId(pid_outside_of_namespace);
46*635a8641SAndroid Build Coastguard Worker   g_procid = GetCurrentProcId();
47*635a8641SAndroid Build Coastguard Worker   g_have_unique_id = true;
48*635a8641SAndroid Build Coastguard Worker }
49*635a8641SAndroid Build Coastguard Worker 
50*635a8641SAndroid Build Coastguard Worker #endif
51*635a8641SAndroid Build Coastguard Worker 
52*635a8641SAndroid Build Coastguard Worker }  // namespace base
53