blob: 900dee620211c7f30c387c966c9ac858d7678831 [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:061// Copyright 2013 The Chromium Authors
[email protected]992a60652013-07-15 18:29:352// 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
rickyz5b937a32015-10-21 23:52:208#include <stdint.h>
9#include <sys/types.h>
10
kylechard917b982023-11-30 22:52:4411#include <compare>
Lei Zhang17575082021-05-10 20:19:1812#include <iosfwd>
13
[email protected]992a60652013-07-15 18:29:3514#include "base/base_export.h"
[email protected]992a60652013-07-15 18:29:3515#include "build/build_config.h"
16
Xiaohan Wang37e81612022-01-15 18:27:0017#if BUILDFLAG(IS_WIN)
Bruce Dawsonbfdc3fd2018-01-03 20:32:3618#include "base/win/windows_types.h"
[email protected]992a60652013-07-15 18:29:3519#endif
20
Xiaohan Wang37e81612022-01-15 18:27:0021#if BUILDFLAG(IS_FUCHSIA)
Scott Grahamfe0e9f462017-09-18 21:25:0422#include <zircon/types.h>
scottmg297cc932017-05-24 03:45:5823#endif
24
[email protected]992a60652013-07-15 18:29:3525namespace base {
26
Lei Zhang17575082021-05-10 20:19:1827class FilePath;
28
[email protected]992a60652013-07-15 18:29:3529// ProcessHandle is a platform specific type which represents the underlying OS
30// handle to a process.
31// ProcessId is a number which identifies the process in the OS.
Xiaohan Wang37e81612022-01-15 18:27:0032#if BUILDFLAG(IS_WIN)
[email protected]992a60652013-07-15 18:29:3533typedef HANDLE ProcessHandle;
34typedef DWORD ProcessId;
35typedef HANDLE UserTokenHandle;
36const ProcessHandle kNullProcessHandle = NULL;
37const ProcessId kNullProcessId = 0;
Xiaohan Wang37e81612022-01-15 18:27:0038#elif BUILDFLAG(IS_FUCHSIA)
Scott Grahamfe0e9f462017-09-18 21:25:0439typedef zx_handle_t ProcessHandle;
40typedef zx_koid_t ProcessId;
41const ProcessHandle kNullProcessHandle = ZX_HANDLE_INVALID;
42const ProcessId kNullProcessId = ZX_KOID_INVALID;
Xiaohan Wang37e81612022-01-15 18:27:0043#elif BUILDFLAG(IS_POSIX)
[email protected]992a60652013-07-15 18:29:3544// On POSIX, our ProcessHandle will just be the PID.
45typedef pid_t ProcessHandle;
46typedef pid_t ProcessId;
47const ProcessHandle kNullProcessHandle = 0;
48const ProcessId kNullProcessId = 0;
Xiaohan Wang37e81612022-01-15 18:27:0049#endif // BUILDFLAG(IS_WIN)
[email protected]992a60652013-07-15 18:29:3550
Bruce Dawson3859fd8f2017-08-08 15:57:1751// To print ProcessIds portably use CrPRIdPid (based on PRIuS and friends from
52// C99 and format_macros.h) like this:
53// base::StringPrintf("PID is %" CrPRIdPid ".\n", pid);
Xiaohan Wang37e81612022-01-15 18:27:0054#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_FUCHSIA)
Bruce Dawson3859fd8f2017-08-08 15:57:1755#define CrPRIdPid "ld"
56#else
57#define CrPRIdPid "d"
58#endif
59
Chris Findeisen2a373a2d2019-08-15 19:44:3060class UniqueProcId {
61 public:
62 explicit UniqueProcId(ProcessId value) : value_(value) {}
63 UniqueProcId(const UniqueProcId& other) = default;
64 UniqueProcId& operator=(const UniqueProcId& other) = default;
65
66 // Returns the process PID. WARNING: On some platforms, the pid may not be
67 // valid within the current process sandbox.
68 ProcessId GetUnsafeValue() const { return value_; }
69
kylechard917b982023-11-30 22:52:4470 friend bool operator==(const UniqueProcId&, const UniqueProcId&) = default;
71 friend auto operator<=>(const UniqueProcId&, const UniqueProcId&) = default;
Chris Findeisen2a373a2d2019-08-15 19:44:3072
73 private:
74 ProcessId value_;
75};
76
77std::ostream& operator<<(std::ostream& os, const UniqueProcId& obj);
78
[email protected]992a60652013-07-15 18:29:3579// Returns the id of the current process.
rickyz5b937a32015-10-21 23:52:2080// Note that on some platforms, this is not guaranteed to be unique across
81// processes (use GetUniqueIdForProcess if uniqueness is required).
[email protected]992a60652013-07-15 18:29:3582BASE_EXPORT ProcessId GetCurrentProcId();
83
rickyz5b937a32015-10-21 23:52:2084// Returns a unique ID for the current process. The ID will be unique across all
85// currently running processes within the chrome session, but IDs of terminated
Chris Findeisen2a373a2d2019-08-15 19:44:3086// processes may be reused.
87BASE_EXPORT UniqueProcId GetUniqueIdForProcess();
rickyz5b937a32015-10-21 23:52:2088
Xiaohan Wang37e81612022-01-15 18:27:0089#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
rickyz5b937a32015-10-21 23:52:2090// When a process is started in a different PID namespace from the browser
91// process, this function must be called with the process's PID in the browser's
92// PID namespace in order to initialize its unique ID. Not thread safe.
93// WARNING: To avoid inconsistent results from GetUniqueIdForProcess, this
94// should only be called very early after process startup - ideally as soon
95// after process creation as possible.
96BASE_EXPORT void InitUniqueIdForProcessInPidNamespace(
97 ProcessId pid_outside_of_namespace);
98#endif
99
[email protected]992a60652013-07-15 18:29:35100// Returns the ProcessHandle of the current process.
101BASE_EXPORT ProcessHandle GetCurrentProcessHandle();
102
rickyz5b937a32015-10-21 23:52:20103// Returns the process ID for the specified process. This is functionally the
104// same as Windows' GetProcessId(), but works on versions of Windows before Win
105// XP SP1 as well.
rvargas6c690f12015-02-13 18:07:59106// DEPRECATED. New code should be using Process::Pid() instead.
rickyz5b937a32015-10-21 23:52:20107// Note that on some platforms, this is not guaranteed to be unique across
108// processes.
[email protected]992a60652013-07-15 18:29:35109BASE_EXPORT ProcessId GetProcId(ProcessHandle process);
110
Xiaohan Wang37e81612022-01-15 18:27:00111#if !BUILDFLAG(IS_FUCHSIA)
Scott Grahame160c7f2017-05-25 23:07:14112// Returns the ID for the parent of the given process. Not available on Fuchsia.
Zijie He4dd88ae422017-09-20 01:30:18113// Returning a negative value indicates an error, such as if the |process| does
114// not exist. Returns 0 when |process| has no parent process.
jam1288a4e2015-12-09 02:11:53115BASE_EXPORT ProcessId GetParentProcessId(ProcessHandle process);
Xiaohan Wang37e81612022-01-15 18:27:00116#endif // !BUILDFLAG(IS_FUCHSIA)
jam1288a4e2015-12-09 02:11:53117
Xiaohan Wang37e81612022-01-15 18:27:00118#if BUILDFLAG(IS_POSIX)
[email protected]992a60652013-07-15 18:29:35119// Returns the path to the executable of the given process.
120BASE_EXPORT FilePath GetProcessExecutablePath(ProcessHandle process);
[email protected]992a60652013-07-15 18:29:35121#endif
122
123} // namespace base
124
125#endif // BASE_PROCESS_PROCESS_HANDLE_H_