• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2013 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 #ifndef BASE_PROCESS_PROCESS_HANDLE_H_
6 #define BASE_PROCESS_PROCESS_HANDLE_H_
7 
8 #include <stdint.h>
9 #include <sys/types.h>
10 
11 #include "base/base_export.h"
12 #include "base/files/file_path.h"
13 #include "build/build_config.h"
14 
15 #if defined(OS_WIN)
16 #include "base/win/windows_types.h"
17 #endif
18 
19 #if defined(OS_FUCHSIA)
20 #include <zircon/types.h>
21 #endif
22 
23 namespace base {
24 
25 // ProcessHandle is a platform specific type which represents the underlying OS
26 // handle to a process.
27 // ProcessId is a number which identifies the process in the OS.
28 #if defined(OS_WIN)
29 typedef HANDLE ProcessHandle;
30 typedef DWORD ProcessId;
31 typedef HANDLE UserTokenHandle;
32 const ProcessHandle kNullProcessHandle = NULL;
33 const ProcessId kNullProcessId = 0;
34 #elif defined(OS_FUCHSIA)
35 typedef zx_handle_t ProcessHandle;
36 typedef zx_koid_t ProcessId;
37 const ProcessHandle kNullProcessHandle = ZX_HANDLE_INVALID;
38 const ProcessId kNullProcessId = ZX_KOID_INVALID;
39 #elif defined(OS_POSIX)
40 // On POSIX, our ProcessHandle will just be the PID.
41 typedef pid_t ProcessHandle;
42 typedef pid_t ProcessId;
43 const ProcessHandle kNullProcessHandle = 0;
44 const ProcessId kNullProcessId = 0;
45 #endif  // defined(OS_WIN)
46 
47 // To print ProcessIds portably use CrPRIdPid (based on PRIuS and friends from
48 // C99 and format_macros.h) like this:
49 // base::StringPrintf("PID is %" CrPRIdPid ".\n", pid);
50 #if defined(OS_WIN) || defined(OS_FUCHSIA)
51 #define CrPRIdPid "ld"
52 #else
53 #define CrPRIdPid "d"
54 #endif
55 
56 // Returns the id of the current process.
57 // Note that on some platforms, this is not guaranteed to be unique across
58 // processes (use GetUniqueIdForProcess if uniqueness is required).
59 BASE_EXPORT ProcessId GetCurrentProcId();
60 
61 // Returns a unique ID for the current process. The ID will be unique across all
62 // currently running processes within the chrome session, but IDs of terminated
63 // processes may be reused. This returns an opaque value that is different from
64 // a process's PID.
65 BASE_EXPORT uint32_t GetUniqueIdForProcess();
66 
67 #if defined(OS_LINUX)
68 // When a process is started in a different PID namespace from the browser
69 // process, this function must be called with the process's PID in the browser's
70 // PID namespace in order to initialize its unique ID. Not thread safe.
71 // WARNING: To avoid inconsistent results from GetUniqueIdForProcess, this
72 // should only be called very early after process startup - ideally as soon
73 // after process creation as possible.
74 BASE_EXPORT void InitUniqueIdForProcessInPidNamespace(
75     ProcessId pid_outside_of_namespace);
76 #endif
77 
78 // Returns the ProcessHandle of the current process.
79 BASE_EXPORT ProcessHandle GetCurrentProcessHandle();
80 
81 // Returns the process ID for the specified process. This is functionally the
82 // same as Windows' GetProcessId(), but works on versions of Windows before Win
83 // XP SP1 as well.
84 // DEPRECATED. New code should be using Process::Pid() instead.
85 // Note that on some platforms, this is not guaranteed to be unique across
86 // processes.
87 BASE_EXPORT ProcessId GetProcId(ProcessHandle process);
88 
89 #if !defined(OS_FUCHSIA)
90 // Returns the ID for the parent of the given process. Not available on Fuchsia.
91 // Returning a negative value indicates an error, such as if the |process| does
92 // not exist. Returns 0 when |process| has no parent process.
93 BASE_EXPORT ProcessId GetParentProcessId(ProcessHandle process);
94 #endif  // !defined(OS_FUCHSIA)
95 
96 #if defined(OS_POSIX)
97 // Returns the path to the executable of the given process.
98 BASE_EXPORT FilePath GetProcessExecutablePath(ProcessHandle process);
99 #endif
100 
101 }  // namespace base
102 
103 #endif  // BASE_PROCESS_PROCESS_HANDLE_H_
104