blob: d4d7ea7b2f896013b867f8d66264d06cba51b1c5 [file] [log] [blame]
[email protected]992a60652013-07-15 18:29:351// 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
rickyz5b937a32015-10-21 23:52:208#include <stdint.h>
9#include <sys/types.h>
10
[email protected]992a60652013-07-15 18:29:3511#include "base/base_export.h"
[email protected]992a60652013-07-15 18:29:3512#include "base/files/file_path.h"
13#include "build/build_config.h"
14
[email protected]992a60652013-07-15 18:29:3515#if defined(OS_WIN)
16#include <windows.h>
17#endif
18
scottmg297cc932017-05-24 03:45:5819#if defined(OS_FUCHSIA)
Scott Grahamfe0e9f462017-09-18 21:25:0420#include <zircon/types.h>
scottmg297cc932017-05-24 03:45:5821#endif
22
[email protected]992a60652013-07-15 18:29:3523namespace 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)
29typedef HANDLE ProcessHandle;
30typedef DWORD ProcessId;
31typedef HANDLE UserTokenHandle;
32const ProcessHandle kNullProcessHandle = NULL;
33const ProcessId kNullProcessId = 0;
scottmg297cc932017-05-24 03:45:5834#elif defined(OS_FUCHSIA)
Scott Grahamfe0e9f462017-09-18 21:25:0435typedef zx_handle_t ProcessHandle;
36typedef zx_koid_t ProcessId;
37const ProcessHandle kNullProcessHandle = ZX_HANDLE_INVALID;
38const ProcessId kNullProcessId = ZX_KOID_INVALID;
[email protected]992a60652013-07-15 18:29:3539#elif defined(OS_POSIX)
40// On POSIX, our ProcessHandle will just be the PID.
41typedef pid_t ProcessHandle;
42typedef pid_t ProcessId;
43const ProcessHandle kNullProcessHandle = 0;
44const ProcessId kNullProcessId = 0;
45#endif // defined(OS_WIN)
46
Bruce Dawson3859fd8f2017-08-08 15:57:1747// 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);
Kevin Marshallb73a98f2017-08-10 05:39:0450#if defined(OS_WIN) || defined(OS_FUCHSIA)
Bruce Dawson3859fd8f2017-08-08 15:57:1751#define CrPRIdPid "ld"
52#else
53#define CrPRIdPid "d"
54#endif
55
[email protected]992a60652013-07-15 18:29:3556// Returns the id of the current process.
rickyz5b937a32015-10-21 23:52:2057// Note that on some platforms, this is not guaranteed to be unique across
58// processes (use GetUniqueIdForProcess if uniqueness is required).
[email protected]992a60652013-07-15 18:29:3559BASE_EXPORT ProcessId GetCurrentProcId();
60
rickyz5b937a32015-10-21 23:52:2061// 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.
65BASE_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.
74BASE_EXPORT void InitUniqueIdForProcessInPidNamespace(
75 ProcessId pid_outside_of_namespace);
76#endif
77
[email protected]992a60652013-07-15 18:29:3578// Returns the ProcessHandle of the current process.
79BASE_EXPORT ProcessHandle GetCurrentProcessHandle();
80
rickyz5b937a32015-10-21 23:52:2081// 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.
rvargas6c690f12015-02-13 18:07:5984// DEPRECATED. New code should be using Process::Pid() instead.
rickyz5b937a32015-10-21 23:52:2085// Note that on some platforms, this is not guaranteed to be unique across
86// processes.
[email protected]992a60652013-07-15 18:29:3587BASE_EXPORT ProcessId GetProcId(ProcessHandle process);
88
Scott Grahame160c7f2017-05-25 23:07:1489#if !defined(OS_FUCHSIA)
90// Returns the ID for the parent of the given process. Not available on Fuchsia.
jam1288a4e2015-12-09 02:11:5391BASE_EXPORT ProcessId GetParentProcessId(ProcessHandle process);
Scott Grahame160c7f2017-05-25 23:07:1492#endif // !defined(OS_FUCHSIA)
jam1288a4e2015-12-09 02:11:5393
[email protected]c17ecbe2014-05-01 10:50:0594#if defined(OS_POSIX)
[email protected]992a60652013-07-15 18:29:3595// Returns the path to the executable of the given process.
96BASE_EXPORT FilePath GetProcessExecutablePath(ProcessHandle process);
[email protected]992a60652013-07-15 18:29:3597#endif
98
99} // namespace base
100
101#endif // BASE_PROCESS_PROCESS_HANDLE_H_