From 3f74d8d568bd7772e2267d088ded2ab0f614d775 Mon Sep 17 00:00:00 2001 From: Corinna Vinschen Date: Thu, 31 Mar 2011 15:33:53 +0000 Subject: [PATCH] * uinfo.cc (cygheap_user::init): Don't call GetUserName. Fetch username from Windows environment instead. Explain why. (cygheap_user::env_domain): Use MAX_DOMAIN_NAME_LEN rather than DNLEN to specify the size of the domain name buffer. --- winsup/cygwin/ChangeLog | 7 +++++++ winsup/cygwin/uinfo.cc | 26 +++++++++++++++++++------- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/winsup/cygwin/ChangeLog b/winsup/cygwin/ChangeLog index 244069f90..07305fb87 100644 --- a/winsup/cygwin/ChangeLog +++ b/winsup/cygwin/ChangeLog @@ -1,3 +1,10 @@ +2011-03-31 Corinna Vinschen + + * uinfo.cc (cygheap_user::init): Don't call GetUserName. Fetch username + from Windows environment instead. Explain why. + (cygheap_user::env_domain): Use MAX_DOMAIN_NAME_LEN rather than DNLEN + to specify the size of the domain name buffer. + 2011-03-30 Corinna Vinschen * hires.h: Fix copyright. diff --git a/winsup/cygwin/uinfo.cc b/winsup/cygwin/uinfo.cc index 66cab121e..25c868ce4 100644 --- a/winsup/cygwin/uinfo.cc +++ b/winsup/cygwin/uinfo.cc @@ -40,12 +40,24 @@ cygheap_user::init () WCHAR user_name[UNLEN + 1]; DWORD user_name_len = UNLEN + 1; - if (!GetUserNameW (user_name, &user_name_len)) - wcpcpy (user_name, L"unknown"); - - char mb_user_name[user_name_len = sys_wcstombs (NULL, 0, user_name)]; - sys_wcstombs (mb_user_name, user_name_len, user_name); - set_name (mb_user_name); + /* This code is only run if a Cygwin process gets started by a native + Win32 process. We try to get the username from the environment, + first USERNAME (Win32), then USER (POSIX). If that fails (which is + very unlikely), it only has an impact if we don't have an entry in + /etc/passwd for this user either. In that case the username sticks + to "unknown". Since this is called early in initialization, and + since we don't want pull in a dependency to any other DLL except + ntdll and kernel32 at this early stage, don't call GetUserName, + GetUserNameEx, NetWkstaUserGetInfo, etc. */ + if (GetEnvironmentVariableW (L"USERNAME", user_name, user_name_len) + || GetEnvironmentVariableW (L"USER", user_name, user_name_len)) + { + char mb_user_name[user_name_len = sys_wcstombs (NULL, 0, user_name)]; + sys_wcstombs (mb_user_name, user_name_len, user_name); + set_name (mb_user_name); + } + else + set_name ("unknown"); DWORD siz; PSECURITY_DESCRIPTOR psd; @@ -384,7 +396,7 @@ cygheap_user::env_domain (const char *name, size_t namelen) DWORD ulen = UNLEN + 1; WCHAR username[ulen]; - DWORD dlen = DNLEN + 1; + DWORD dlen = MAX_DOMAIN_NAME_LEN + 1; WCHAR userdomain[dlen]; SID_NAME_USE use;