Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(183)

Side by Side Diff: rlz/win/lib/process_info.cc

Issue 177843009: Make GetUserName() return more specific error code, so that we could know how the function failed. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add CHECK for debugging. Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 // Information about the current process. 5 // Information about the current process.
6 6
7 #include "rlz/win/lib/process_info.h" 7 #include "rlz/win/lib/process_info.h"
8 8
9 #include <windows.h> 9 #include <windows.h>
10 #include <Sddl.h> // For ConvertSidToStringSid. 10 #include <Sddl.h> // For ConvertSidToStringSid.
(...skipping 15 matching lines...) Expand all
26 26
27 // Get the current username & domain the hard way. (GetUserNameEx would be 27 // Get the current username & domain the hard way. (GetUserNameEx would be
28 // nice, but unfortunately requires connectivity to a domain controller. 28 // nice, but unfortunately requires connectivity to a domain controller.
29 // Useless.) 29 // Useless.)
30 30
31 // (Following call doesn't work if running as a Service - because a Service 31 // (Following call doesn't work if running as a Service - because a Service
32 // runs under special accounts like LOCAL_SYSTEM, not as the logged in user. 32 // runs under special accounts like LOCAL_SYSTEM, not as the logged in user.
33 // In which case, search for and use the process handle of a running 33 // In which case, search for and use the process handle of a running
34 // Explorer.exe.) 34 // Explorer.exe.)
35 HANDLE token; 35 HANDLE token;
36 if (!::OpenProcessToken(::GetCurrentProcess(), TOKEN_QUERY, &token)) 36
37 return E_FAIL; 37 HANDLE process_handle = ::GetCurrentProcess()
38 if (!process_handle)
39 CHECK(0)
jar (doing other things) 2014/02/26 00:27:08 Please try to compile code before sending for a re
40 return ::GetLastError()
jar (doing other things) 2014/02/26 00:27:08 Note that IF you used a semicolon (on line 39), si
41
42 if (!::OpenProcessToken(process_handle, TOKEN_QUERY, &token))
43 CHECK(0)
44 return ::GetLastError()
38 45
39 base::win::ScopedHandle scoped_process_token(token); 46 base::win::ScopedHandle scoped_process_token(token);
40 47
41 // (Following call will fail with ERROR_INSUFFICIENT_BUFFER and give us the 48 // (Following call will fail with ERROR_INSUFFICIENT_BUFFER and give us the
42 // required size.) 49 // required size.)
43 scoped_ptr<char[]> token_user_bytes; 50 scoped_ptr<char[]> token_user_bytes;
44 DWORD token_user_size; 51 DWORD token_user_size;
45 DWORD token_user_size2; 52 DWORD token_user_size2;
46 BOOL result = ::GetTokenInformation(token, TokenUser, NULL, 0, 53 BOOL result = ::GetTokenInformation(token, TokenUser, NULL, 0,
47 &token_user_size); 54 &token_user_size);
48 err = ::GetLastError(); 55 err = ::GetLastError();
49 CHECK(!result && err == ERROR_INSUFFICIENT_BUFFER); 56 CHECK(!result && err == ERROR_INSUFFICIENT_BUFFER);
50 57
51 token_user_bytes.reset(new char[token_user_size]); 58 token_user_bytes.reset(new char[token_user_size]);
52 if (!token_user_bytes.get()) 59 if (!token_user_bytes.get())
60 CHECK(0)
53 return E_OUTOFMEMORY; 61 return E_OUTOFMEMORY;
54 62
55 if (!::GetTokenInformation(token, TokenUser, token_user_bytes.get(), 63 if (!::GetTokenInformation(token, TokenUser, token_user_bytes.get(),
56 token_user_size, &token_user_size2)) { 64 token_user_size, &token_user_size2)) {
57 return E_FAIL; 65 CHECK(0)
66 return ::GetLastError();
58 } 67 }
59 68
60 WCHAR user_name[UNLEN + 1]; // max username length 69 WCHAR user_name[UNLEN + 1]; // max username length
61 WCHAR domain_name[UNLEN + 1]; 70 WCHAR domain_name[UNLEN + 1];
62 DWORD user_name_size = UNLEN + 1; 71 DWORD user_name_size = UNLEN + 1;
63 DWORD domain_name_size = UNLEN + 1; 72 DWORD domain_name_size = UNLEN + 1;
64 SID_NAME_USE sid_type; 73 SID_NAME_USE sid_type;
65 TOKEN_USER* token_user = 74 TOKEN_USER* token_user =
66 reinterpret_cast<TOKEN_USER*>(token_user_bytes.get()); 75 reinterpret_cast<TOKEN_USER*>(token_user_bytes.get());
67 if (!token_user) 76 if (!token_user)
68 return E_FAIL; 77 CHECK(0)
78 return ::GetLastError();
69 PSID user_sid = token_user->User.Sid; 79 PSID user_sid = token_user->User.Sid;
70 if (!::LookupAccountSidW(NULL, user_sid, user_name, &user_name_size, 80 if (!::LookupAccountSidW(NULL, user_sid, user_name, &user_name_size,
71 domain_name, &domain_name_size, &sid_type)) { 81 domain_name, &domain_name_size, &sid_type)) {
72 return E_FAIL; 82 CHECK(0)
83 return ::GetLastError();
73 } 84 }
74 85
75 if (name != NULL) { 86 if (name != NULL) {
76 *name = user_name; 87 *name = user_name;
77 } 88 }
78 if (domain != NULL) { 89 if (domain != NULL) {
79 *domain = domain_name; 90 *domain = domain_name;
80 } 91 }
81 if (sid != NULL) { 92 if (sid != NULL) {
82 LPWSTR string_sid; 93 LPWSTR string_sid;
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
186 } 197 }
187 198
188 evaluated = true; 199 evaluated = true;
189 if (!has_rights) 200 if (!has_rights)
190 ASSERT_STRING("ProcessInfo::HasAdminRights: Does not have admin rights."); 201 ASSERT_STRING("ProcessInfo::HasAdminRights: Does not have admin rights.");
191 202
192 return has_rights; 203 return has_rights;
193 } 204 }
194 205
195 }; // namespace 206 }; // namespace
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698