Ticket #1380: win_env_fix.diff

File win_env_fix.diff, 2.6 KB (added by aylee, 9 years ago)

Final patch

  • SGE_Starter.cpp

    old new  
    4545#include <Wtsapi32.h>
    4646#include <Sddl.h>
    4747#include <process.h>
     48#include <Userenv.h>
    4849
    4950// Global Variables:
    5051static char  *g_pCmdLine       = NULL;
     
    686687      SecureZeroMemory(szPass, sizeof(szPass));
    687688      ConvertStringToWideChar(szCmdLine, szwCmdLine, nLen);
    688689     
     690          LPVOID pszEnv = NULL;
     691
     692      WriteToLogFile("Try to get user env...");
     693         
     694          HANDLE hToken;
     695
     696      WriteToLogFile("Calling LogonUserW...");
     697          bResult = LogonUserW(
     698          szwUser,
     699          szwDomain,
     700          szwPass,
     701          LOGON32_LOGON_INTERACTIVE,
     702          LOGON32_PROVIDER_DEFAULT,
     703                  &hToken);
     704      if (!bResult) {
     705              WriteToLogFile("LogonUserW failed for %s", szwUser);
     706          };
     707         
     708       
     709        // as SGE_Starter.cpp is started as SYSTEM user it gets
     710        // "HOME=/" set. This can confuse e.g. the Python function
     711        // os.path.expanduser("~/...")
     712        //
     713        // I'm too lazy to figure out the correct value and removing
     714        // it is better than to have a wrong value...
     715        WriteToLogFile("Removing HOME environment variable");
     716        SetEnvironmentVariable("HOME", NULL);
     717       
     718    // PATH is also weird and filled with stuff like:
     719        // "C:\Windows\SUA\windows\system32" and "C"
     720    // even worse the rather important "C:\Windows\System32" is missing..
     721        // Let Windows just set the fresh default path
     722        WriteToLogFile("Removing PATH environment variable");
     723        SetEnvironmentVariable("PATH", NULL);
     724       
     725    WriteToLogFile("CreateEnvironmentBlock...");
     726    // TRUE: inherit environment from this process
     727    if (!CreateEnvironmentBlock(&pszEnv, hToken, TRUE)) {
     728            WriteToLogFile("CreateEnvironmentBlock failed for %s", szwUser);
     729            pszEnv = NULL;
     730        } else {
     731          WriteToLogFile("Got user env!");
     732        };
     733         
    689734      WriteToLogFile("Creating Process with command line \"%s\"", szCmdLine);
    690735      bResult = CreateProcessWithLogonW(
    691736         szwUser,
     
    694739         LOGON_WITH_PROFILE,
    695740         NULL,
    696741         szwCmdLine,
    697          NORMAL_PRIORITY_CLASS,
    698          NULL,
     742         NORMAL_PRIORITY_CLASS | CREATE_UNICODE_ENVIRONMENT,
     743         pszEnv,
    699744         NULL,
    700745         (LPSTARTUPINFOW)&si,
    701746         &pi);
    702747
     748      CloseHandle(hToken);
     749          if (!DestroyEnvironmentBlock(pszEnv)) {
     750              WriteToLogFile("Could not destroy environment block!");
     751          };
     752
    703753      // Free all allocated memory immediately (esp. the password buffer), but
    704754      // don't let the SecureZeroMemory() function overwrite our LastError.
    705755      dwError = GetLastError();