Home | History | Annotate | Download | only in process
      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 <windows.h>
     17 #endif
     18 
     19 namespace base {
     20 
     21 // ProcessHandle is a platform specific type which represents the underlying OS
     22 // handle to a process.
     23 // ProcessId is a number which identifies the process in the OS.
     24 #if defined(OS_WIN)
     25 typedef HANDLE ProcessHandle;
     26 typedef DWORD ProcessId;
     27 typedef HANDLE UserTokenHandle;
     28 const ProcessHandle kNullProcessHandle = NULL;
     29 const ProcessId kNullProcessId = 0;
     30 #elif defined(OS_POSIX)
     31 // On POSIX, our ProcessHandle will just be the PID.
     32 typedef pid_t ProcessHandle;
     33 typedef pid_t ProcessId;
     34 const ProcessHandle kNullProcessHandle = 0;
     35 const ProcessId kNullProcessId = 0;
     36 #endif  // defined(OS_WIN)
     37 
     38 // Returns the id of the current process.
     39 // Note that on some platforms, this is not guaranteed to be unique across
     40 // processes (use GetUniqueIdForProcess if uniqueness is required).
     41 BASE_EXPORT ProcessId GetCurrentProcId();
     42 
     43 // Returns a unique ID for the current process. The ID will be unique across all
     44 // currently running processes within the chrome session, but IDs of terminated
     45 // processes may be reused. This returns an opaque value that is different from
     46 // a process's PID.
     47 BASE_EXPORT uint32_t GetUniqueIdForProcess();
     48 
     49 #if defined(OS_LINUX)
     50 // When a process is started in a different PID namespace from the browser
     51 // process, this function must be called with the process's PID in the browser's
     52 // PID namespace in order to initialize its unique ID. Not thread safe.
     53 // WARNING: To avoid inconsistent results from GetUniqueIdForProcess, this
     54 // should only be called very early after process startup - ideally as soon
     55 // after process creation as possible.
     56 BASE_EXPORT void InitUniqueIdForProcessInPidNamespace(
     57     ProcessId pid_outside_of_namespace);
     58 #endif
     59 
     60 // Returns the ProcessHandle of the current process.
     61 BASE_EXPORT ProcessHandle GetCurrentProcessHandle();
     62 
     63 // Returns the process ID for the specified process. This is functionally the
     64 // same as Windows' GetProcessId(), but works on versions of Windows before Win
     65 // XP SP1 as well.
     66 // DEPRECATED. New code should be using Process::Pid() instead.
     67 // Note that on some platforms, this is not guaranteed to be unique across
     68 // processes.
     69 BASE_EXPORT ProcessId GetProcId(ProcessHandle process);
     70 
     71 // Returns the ID for the parent of the given process.
     72 BASE_EXPORT ProcessId GetParentProcessId(ProcessHandle process);
     73 
     74 #if defined(OS_POSIX)
     75 // Returns the path to the executable of the given process.
     76 BASE_EXPORT FilePath GetProcessExecutablePath(ProcessHandle process);
     77 #endif
     78 
     79 }  // namespace base
     80 
     81 #endif  // BASE_PROCESS_PROCESS_HANDLE_H_
     82