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

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: Apply Check directly to the function. 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 CHECK(::OpenProcessToken(::GetCurrentProcess(), TOKEN_QUERY, &token));
38 38
39 base::win::ScopedHandle scoped_process_token(token); 39 base::win::ScopedHandle scoped_process_token(token);
40 40
41 // (Following call will fail with ERROR_INSUFFICIENT_BUFFER and give us the 41 // (Following call will fail with ERROR_INSUFFICIENT_BUFFER and give us the
42 // required size.) 42 // required size.)
43 scoped_ptr<char[]> token_user_bytes; 43 scoped_ptr<char[]> token_user_bytes;
44 DWORD token_user_size; 44 DWORD token_user_size;
45 DWORD token_user_size2; 45 DWORD token_user_size2;
46 BOOL result = ::GetTokenInformation(token, TokenUser, NULL, 0, 46 BOOL result = ::GetTokenInformation(token, TokenUser, NULL, 0,
47 &token_user_size); 47 &token_user_size);
48 err = ::GetLastError(); 48 err = ::GetLastError();
49 CHECK(!result && err == ERROR_INSUFFICIENT_BUFFER); 49 CHECK(!result && err == ERROR_INSUFFICIENT_BUFFER);
50 50
51 token_user_bytes.reset(new char[token_user_size]); 51 token_user_bytes.reset(new char[token_user_size]);
52 if (!token_user_bytes.get()) 52 CHECK(token_user_bytes.get());
53 return E_OUTOFMEMORY;
54 53
55 if (!::GetTokenInformation(token, TokenUser, token_user_bytes.get(), 54 CHECK(::GetTokenInformation(token, TokenUser, token_user_bytes.get(),
56 token_user_size, &token_user_size2)) { 55 token_user_size, &token_user_size2))
Roger Tawa OOO till Jul 10th 2014/03/01 14:54:58 Remove extra space, add missing ; at end.
yiyaoliu 2014/03/05 21:32:44 Done.
57 return E_FAIL;
58 }
59 56
60 WCHAR user_name[UNLEN + 1]; // max username length 57 WCHAR user_name[UNLEN + 1]; // max username length
61 WCHAR domain_name[UNLEN + 1]; 58 WCHAR domain_name[UNLEN + 1];
62 DWORD user_name_size = UNLEN + 1; 59 DWORD user_name_size = UNLEN + 1;
63 DWORD domain_name_size = UNLEN + 1; 60 DWORD domain_name_size = UNLEN + 1;
64 SID_NAME_USE sid_type; 61 SID_NAME_USE sid_type;
65 TOKEN_USER* token_user = 62 TOKEN_USER* token_user =
66 reinterpret_cast<TOKEN_USER*>(token_user_bytes.get()); 63 reinterpret_cast<TOKEN_USER*>(token_user_bytes.get());
67 if (!token_user) 64 if (!token_user)
68 return E_FAIL; 65 CHECK(0);
Roger Tawa OOO till Jul 10th 2014/03/01 14:54:58 You can replace lines 64-65 with: CHECK(token_
yiyaoliu 2014/03/05 21:32:44 Done.
66
69 PSID user_sid = token_user->User.Sid; 67 PSID user_sid = token_user->User.Sid;
70 if (!::LookupAccountSidW(NULL, user_sid, user_name, &user_name_size, 68 CHECK(::LookupAccountSidW(NULL, user_sid, user_name, &user_name_size,
71 domain_name, &domain_name_size, &sid_type)) { 69 domain_name, &domain_name_size, &sid_type))
72 return E_FAIL;
73 }
74 70
75 if (name != NULL) { 71 if (name != NULL) {
76 *name = user_name; 72 *name = user_name;
77 } 73 }
78 if (domain != NULL) { 74 if (domain != NULL) {
79 *domain = domain_name; 75 *domain = domain_name;
80 } 76 }
81 if (sid != NULL) { 77 if (sid != NULL) {
82 LPWSTR string_sid; 78 LPWSTR string_sid;
83 ConvertSidToStringSidW(user_sid, &string_sid); 79 ConvertSidToStringSidW(user_sid, &string_sid);
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
186 } 182 }
187 183
188 evaluated = true; 184 evaluated = true;
189 if (!has_rights) 185 if (!has_rights)
190 ASSERT_STRING("ProcessInfo::HasAdminRights: Does not have admin rights."); 186 ASSERT_STRING("ProcessInfo::HasAdminRights: Does not have admin rights.");
191 187
192 return has_rights; 188 return has_rights;
193 } 189 }
194 190
195 }; // namespace 191 }; // 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