Add testonly=true to components/pairing:unit_tests
[chromium-blink-merge.git] / base / process / process_handle_win.cc
blob3bc3a125e0d46132586c7a4b3865706f970e680b
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.
5 #include "base/process/process_handle.h"
7 #include <windows.h>
9 #include "base/memory/scoped_ptr.h"
10 #include "base/win/scoped_handle.h"
11 #include "base/win/windows_version.h"
13 namespace base {
15 ProcessId GetCurrentProcId() {
16 return ::GetCurrentProcessId();
19 ProcessHandle GetCurrentProcessHandle() {
20 return ::GetCurrentProcess();
23 bool OpenProcessHandle(ProcessId pid, ProcessHandle* handle) {
24 // We try to limit privileges granted to the handle. If you need this
25 // for test code, consider using OpenPrivilegedProcessHandle instead of
26 // adding more privileges here.
27 ProcessHandle result = OpenProcess(PROCESS_TERMINATE |
28 PROCESS_QUERY_INFORMATION |
29 SYNCHRONIZE,
30 FALSE, pid);
32 if (result == NULL)
33 return false;
35 *handle = result;
36 return true;
39 bool OpenPrivilegedProcessHandle(ProcessId pid, ProcessHandle* handle) {
40 ProcessHandle result = OpenProcess(PROCESS_DUP_HANDLE |
41 PROCESS_TERMINATE |
42 PROCESS_QUERY_INFORMATION |
43 PROCESS_VM_READ |
44 SYNCHRONIZE,
45 FALSE, pid);
47 if (result == NULL)
48 return false;
50 *handle = result;
51 return true;
54 bool OpenProcessHandleWithAccess(ProcessId pid,
55 uint32 access_flags,
56 ProcessHandle* handle) {
57 ProcessHandle result = OpenProcess(access_flags, FALSE, pid);
59 if (result == NULL)
60 return false;
62 *handle = result;
63 return true;
66 void CloseProcessHandle(ProcessHandle process) {
67 CloseHandle(process);
70 ProcessId GetProcId(ProcessHandle process) {
71 // This returns 0 if we have insufficient rights to query the process handle.
72 return GetProcessId(process);
75 bool GetProcessIntegrityLevel(ProcessHandle process, IntegrityLevel *level) {
76 if (!level)
77 return false;
79 if (win::GetVersion() < base::win::VERSION_VISTA)
80 return false;
82 HANDLE process_token;
83 if (!OpenProcessToken(process, TOKEN_QUERY | TOKEN_QUERY_SOURCE,
84 &process_token))
85 return false;
87 win::ScopedHandle scoped_process_token(process_token);
89 DWORD token_info_length = 0;
90 if (GetTokenInformation(process_token, TokenIntegrityLevel, NULL, 0,
91 &token_info_length) ||
92 GetLastError() != ERROR_INSUFFICIENT_BUFFER)
93 return false;
95 scoped_ptr<char[]> token_label_bytes(new char[token_info_length]);
96 if (!token_label_bytes.get())
97 return false;
99 TOKEN_MANDATORY_LABEL* token_label =
100 reinterpret_cast<TOKEN_MANDATORY_LABEL*>(token_label_bytes.get());
101 if (!token_label)
102 return false;
104 if (!GetTokenInformation(process_token, TokenIntegrityLevel, token_label,
105 token_info_length, &token_info_length))
106 return false;
108 DWORD integrity_level = *GetSidSubAuthority(token_label->Label.Sid,
109 (DWORD)(UCHAR)(*GetSidSubAuthorityCount(token_label->Label.Sid)-1));
111 if (integrity_level < SECURITY_MANDATORY_MEDIUM_RID) {
112 *level = LOW_INTEGRITY;
113 } else if (integrity_level >= SECURITY_MANDATORY_MEDIUM_RID &&
114 integrity_level < SECURITY_MANDATORY_HIGH_RID) {
115 *level = MEDIUM_INTEGRITY;
116 } else if (integrity_level >= SECURITY_MANDATORY_HIGH_RID) {
117 *level = HIGH_INTEGRITY;
118 } else {
119 NOTREACHED();
120 return false;
123 return true;
126 } // namespace base