| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/process_util.h" | 5 #include "base/process_util.h" |
| 6 | 6 |
| 7 #include <fcntl.h> | 7 #include <fcntl.h> |
| 8 #include <io.h> | 8 #include <io.h> |
| 9 #include <windows.h> | 9 #include <windows.h> |
| 10 #include <userenv.h> | 10 #include <userenv.h> |
| 11 #include <psapi.h> | 11 #include <psapi.h> |
| 12 | 12 |
| 13 #include <ios> | 13 #include <ios> |
| 14 | 14 |
| 15 #include "base/command_line.h" | 15 #include "base/command_line.h" |
| 16 #include "base/debug_util.h" | 16 #include "base/debug/stack_trace.h" |
| 17 #include "base/logging.h" | 17 #include "base/logging.h" |
| 18 #include "base/metrics/histogram.h" | 18 #include "base/metrics/histogram.h" |
| 19 #include "base/scoped_ptr.h" | 19 #include "base/scoped_ptr.h" |
| 20 #include "base/win/scoped_handle.h" | 20 #include "base/win/scoped_handle.h" |
| 21 #include "base/win/windows_version.h" | 21 #include "base/win/windows_version.h" |
| 22 | 22 |
| 23 // userenv.dll is required for CreateEnvironmentBlock(). | 23 // userenv.dll is required for CreateEnvironmentBlock(). |
| 24 #pragma comment(lib, "userenv.lib") | 24 #pragma comment(lib, "userenv.lib") |
| 25 | 25 |
| 26 namespace base { | 26 namespace base { |
| 27 | 27 |
| 28 namespace { | 28 namespace { |
| 29 | 29 |
| 30 // System pagesize. This value remains constant on x86/64 architectures. | 30 // System pagesize. This value remains constant on x86/64 architectures. |
| 31 const int PAGESIZE_KB = 4; | 31 const int PAGESIZE_KB = 4; |
| 32 | 32 |
| 33 // HeapSetInformation function pointer. | 33 // HeapSetInformation function pointer. |
| 34 typedef BOOL (WINAPI* HeapSetFn)(HANDLE, HEAP_INFORMATION_CLASS, PVOID, SIZE_T); | 34 typedef BOOL (WINAPI* HeapSetFn)(HANDLE, HEAP_INFORMATION_CLASS, PVOID, SIZE_T); |
| 35 | 35 |
| 36 // Previous unhandled filter. Will be called if not NULL when we intercept an | 36 // Previous unhandled filter. Will be called if not NULL when we intercept an |
| 37 // exception. Only used in unit tests. | 37 // exception. Only used in unit tests. |
| 38 LPTOP_LEVEL_EXCEPTION_FILTER g_previous_filter = NULL; | 38 LPTOP_LEVEL_EXCEPTION_FILTER g_previous_filter = NULL; |
| 39 | 39 |
| 40 // Prints the exception call stack. | 40 // Prints the exception call stack. |
| 41 // This is the unit tests exception filter. | 41 // This is the unit tests exception filter. |
| 42 long WINAPI StackDumpExceptionFilter(EXCEPTION_POINTERS* info) { | 42 long WINAPI StackDumpExceptionFilter(EXCEPTION_POINTERS* info) { |
| 43 StackTrace(info).PrintBacktrace(); | 43 debug::StackTrace(info).PrintBacktrace(); |
| 44 if (g_previous_filter) | 44 if (g_previous_filter) |
| 45 return g_previous_filter(info); | 45 return g_previous_filter(info); |
| 46 return EXCEPTION_CONTINUE_SEARCH; | 46 return EXCEPTION_CONTINUE_SEARCH; |
| 47 } | 47 } |
| 48 | 48 |
| 49 // Connects back to a console if available. | 49 // Connects back to a console if available. |
| 50 void AttachToConsole() { | 50 void AttachToConsole() { |
| 51 if (!AttachConsole(ATTACH_PARENT_PROCESS)) { | 51 if (!AttachConsole(ATTACH_PARENT_PROCESS)) { |
| 52 unsigned int result = GetLastError(); | 52 unsigned int result = GetLastError(); |
| 53 // Was probably already attached. | 53 // Was probably already attached. |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 148 | 148 |
| 149 // We're screwed. | 149 // We're screwed. |
| 150 NOTREACHED(); | 150 NOTREACHED(); |
| 151 return 0; | 151 return 0; |
| 152 } | 152 } |
| 153 | 153 |
| 154 bool GetProcessIntegrityLevel(ProcessHandle process, IntegrityLevel *level) { | 154 bool GetProcessIntegrityLevel(ProcessHandle process, IntegrityLevel *level) { |
| 155 if (!level) | 155 if (!level) |
| 156 return false; | 156 return false; |
| 157 | 157 |
| 158 if (base::win::GetVersion() < base::win::VERSION_VISTA) | 158 if (win::GetVersion() < base::win::VERSION_VISTA) |
| 159 return false; | 159 return false; |
| 160 | 160 |
| 161 HANDLE process_token; | 161 HANDLE process_token; |
| 162 if (!OpenProcessToken(process, TOKEN_QUERY | TOKEN_QUERY_SOURCE, | 162 if (!OpenProcessToken(process, TOKEN_QUERY | TOKEN_QUERY_SOURCE, |
| 163 &process_token)) | 163 &process_token)) |
| 164 return false; | 164 return false; |
| 165 | 165 |
| 166 base::win::ScopedHandle scoped_process_token(process_token); | 166 win::ScopedHandle scoped_process_token(process_token); |
| 167 | 167 |
| 168 DWORD token_info_length = 0; | 168 DWORD token_info_length = 0; |
| 169 if (GetTokenInformation(process_token, TokenIntegrityLevel, NULL, 0, | 169 if (GetTokenInformation(process_token, TokenIntegrityLevel, NULL, 0, |
| 170 &token_info_length) || | 170 &token_info_length) || |
| 171 GetLastError() != ERROR_INSUFFICIENT_BUFFER) | 171 GetLastError() != ERROR_INSUFFICIENT_BUFFER) |
| 172 return false; | 172 return false; |
| 173 | 173 |
| 174 scoped_array<char> token_label_bytes(new char[token_info_length]); | 174 scoped_array<char> token_label_bytes(new char[token_info_length]); |
| 175 if (!token_label_bytes.get()) | 175 if (!token_label_bytes.get()) |
| 176 return false; | 176 return false; |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 319 sa_attr.bInheritHandle = TRUE; | 319 sa_attr.bInheritHandle = TRUE; |
| 320 sa_attr.lpSecurityDescriptor = NULL; | 320 sa_attr.lpSecurityDescriptor = NULL; |
| 321 | 321 |
| 322 // Create the pipe for the child process's STDOUT. | 322 // Create the pipe for the child process's STDOUT. |
| 323 if (!CreatePipe(&out_read, &out_write, &sa_attr, 0)) { | 323 if (!CreatePipe(&out_read, &out_write, &sa_attr, 0)) { |
| 324 NOTREACHED() << "Failed to create pipe"; | 324 NOTREACHED() << "Failed to create pipe"; |
| 325 return false; | 325 return false; |
| 326 } | 326 } |
| 327 | 327 |
| 328 // Ensure we don't leak the handles. | 328 // Ensure we don't leak the handles. |
| 329 base::win::ScopedHandle scoped_out_read(out_read); | 329 win::ScopedHandle scoped_out_read(out_read); |
| 330 base::win::ScopedHandle scoped_out_write(out_write); | 330 win::ScopedHandle scoped_out_write(out_write); |
| 331 | 331 |
| 332 // Ensure the read handle to the pipe for STDOUT is not inherited. | 332 // Ensure the read handle to the pipe for STDOUT is not inherited. |
| 333 if (!SetHandleInformation(out_read, HANDLE_FLAG_INHERIT, 0)) { | 333 if (!SetHandleInformation(out_read, HANDLE_FLAG_INHERIT, 0)) { |
| 334 NOTREACHED() << "Failed to disabled pipe inheritance"; | 334 NOTREACHED() << "Failed to disabled pipe inheritance"; |
| 335 return false; | 335 return false; |
| 336 } | 336 } |
| 337 | 337 |
| 338 // Now create the child process | 338 // Now create the child process |
| 339 PROCESS_INFORMATION proc_info = { 0 }; | 339 PROCESS_INFORMATION proc_info = { 0 }; |
| 340 STARTUPINFO start_info = { 0 }; | 340 STARTUPINFO start_info = { 0 }; |
| (...skipping 546 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 887 | 887 |
| 888 PERFORMANCE_INFORMATION info; | 888 PERFORMANCE_INFORMATION info; |
| 889 if (!InternalGetPerformanceInfo(&info, sizeof(info))) { | 889 if (!InternalGetPerformanceInfo(&info, sizeof(info))) { |
| 890 LOG(ERROR) << "Failed to fetch internal performance info."; | 890 LOG(ERROR) << "Failed to fetch internal performance info."; |
| 891 return 0; | 891 return 0; |
| 892 } | 892 } |
| 893 return (info.CommitTotal * system_info.dwPageSize) / 1024; | 893 return (info.CommitTotal * system_info.dwPageSize) / 1024; |
| 894 } | 894 } |
| 895 | 895 |
| 896 } // namespace base | 896 } // namespace base |
| OLD | NEW |