Skip to content

Commit

Permalink
added method to obtain installation dir to check for installation dur…
Browse files Browse the repository at this point in the history
…ing start
  • Loading branch information
abelsromero committed Oct 14, 2018
1 parent d237d3a commit 301e293
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 35 deletions.
38 changes: 29 additions & 9 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#define ARRAY_LENGTH(a) (sizeof(a)/sizeof(a[0]))

unsigned long QueryUser(wchar_t *TargetName,wchar_t *username);
bool dirExists(const char* dirName);
int InstallDist(wchar_t *TargetName,wchar_t *tgzname);
HRESULT RemoveDist(wchar_t *TargetName);
void show_usage();
Expand All @@ -31,7 +32,7 @@ int main()
int wargc;
wargv = CommandLineToArgvW(GetCommandLineW(),&wargc);

if(wargc >1 && wcscmp(wargv[1],L"version")==0)
if(wargc >1 && wcscmp(wargv[1],L"version")==0)
{
show_version();
return 0;
Expand All @@ -46,9 +47,6 @@ int main()

WslApiInit();




if(!WslIsDistributionRegistered(TargetName))
{
wchar_t tgzname[MAX_PATH] = L"rootfs.tar.gz";
Expand Down Expand Up @@ -83,7 +81,16 @@ int main()

if(wargc == 1)
{
hr = WslLaunchInteractive(TargetName,L"", false, &exitCode);
WslInstallation wslInstallation = WslGetInstallationInfo(TargetName);
char buffer[MAX_BASEPATH_SIZE];
wcstombs(buffer, wslInstallation.basePath, MAX_BASEPATH_SIZE);
if (!dirExists(buffer))
{
fwprintf(stderr,L"Installation directory not found: %s.\nMake sure it exists or reinstall.\n",wslInstallation.basePath);
hr = E_ABORT;
}
else
hr = WslLaunchInteractive(TargetName,L"", false, &exitCode);
}
else if(wcscmp(wargv[1],L"run") == 0)
{
Expand Down Expand Up @@ -182,16 +189,16 @@ int main()
wprintf(L"on");
else
wprintf(L"off");
hr = S_OK;
hr = S_OK;
}
else if(wcscmp(wargv[2],L"--lxuid") == 0)
{
wchar_t LxUID[50] = L"";
if(WslGetLxUID(TargetName,LxUID) == NULL)
WslInstallation wsl = WslGetInstallationInfo(TargetName);
if(wsl.uuid == NULL)
{
hr = E_FAIL;
}
wprintf(L"%s",LxUID);
wprintf(L"%.*s",UUID_SIZE,wsl.uuid);
hr = S_OK;
}
else
Expand Down Expand Up @@ -223,6 +230,13 @@ int main()
{
return exitCode;
}
// already controlled error cases
else if(hr==E_ABORT)
{
wprintf(L"Press any key to continue...");
getchar();
return hr;
}
else if(hr==E_INVALIDARG)
{
fwprintf(stderr,L"ERROR:Invalid Argument.\n\n");
Expand All @@ -239,6 +253,12 @@ int main()
}
}

bool dirExists(const char* dirName)
{
DWORD ftyp = GetFileAttributesA(dirName);
return (ftyp != INVALID_FILE_ATTRIBUTES) && (ftyp & FILE_ATTRIBUTE_DIRECTORY);
}

unsigned long QueryUser(wchar_t *TargetName,wchar_t *username)
{
HANDLE hProcess;
Expand Down
64 changes: 38 additions & 26 deletions wsld.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ WSLGETDISTRIBUTIONCONFIGURATION WslGetDistributionConfiguration;
WSLLAUNCHINTERACTIVE WslLaunchInteractive;
WSLLAUNCH WslLaunch;

#define MAX_DISTRO_NAME_SIZE 50
#define MAX_BASEPATH_SIZE 128
#define UUID_SIZE 38

struct WslInstallation {
wchar_t uuid[UUID_SIZE];
wchar_t basePath[MAX_BASEPATH_SIZE];
};

void WslApiFree()
{
FreeLibrary(WslHmod);
Expand Down Expand Up @@ -73,8 +82,9 @@ int WslApiInit()
return 0;
}

wchar_t *WslGetLxUID(wchar_t *DistributionName,wchar_t *LxUID)
{
WslInstallation WslGetInstallationInfo(wchar_t *DistributionName) {
WslInstallation wslInstallation;

wchar_t RKey[]=L"Software\\Microsoft\\Windows\\CurrentVersion\\Lxss";
HKEY hKey;
LONG rres;
Expand All @@ -83,52 +93,54 @@ wchar_t *WslGetLxUID(wchar_t *DistributionName,wchar_t *LxUID)
int i;
for(i=0;;i++)
{
wchar_t subKey[200];
wchar_t subKeyF[200];
wcscpy_s(subKeyF,(sizeof(subKeyF)/sizeof(subKeyF[0])),RKey);
wchar_t regDistName[100];

wchar_t subKey[200];
DWORD subKeySz = 100;
DWORD dwType;
DWORD dwSize = 50;
FILETIME ftLastWriteTime;

rres = RegEnumKeyExW(hKey, i, subKey, &subKeySz, NULL, NULL, NULL, &ftLastWriteTime);
if (rres == ERROR_NO_MORE_ITEMS)
break;
else if(rres != ERROR_SUCCESS)
{
//ERROR
LxUID = NULL;
return LxUID;
return wslInstallation;
}

DWORD dwType;
HKEY hKeyS;
wcscat_s(subKeyF,(sizeof(subKeyF)/sizeof(subKeyF[0])),L"\\");
wcscat_s(subKeyF,(sizeof(subKeyF)/sizeof(subKeyF[0])),subKey);
RegOpenKeyExW(HKEY_CURRENT_USER,subKeyF, 0, KEY_READ, &hKeyS);
RegQueryValueExW(hKeyS, L"DistributionName", NULL, &dwType, (LPBYTE)&regDistName,&dwSize);
if((subKeySz == 38)&&(wcscmp(regDistName,DistributionName)==0))

wchar_t regDistName[MAX_DISTRO_NAME_SIZE*2];
DWORD dwSize = MAX_DISTRO_NAME_SIZE;
rres = RegQueryValueExW(hKeyS, L"DistributionName", NULL, &dwType, (LPBYTE)&regDistName,&dwSize);
if (rres != ERROR_SUCCESS)
{
// TODO: this helps for diagnostic, but we should implement a better error handling in the future
fwprintf(stderr,L"ERROR:[%i] Could not read registry key\n", rres);
}
if((subKeySz == UUID_SIZE) && (wcscmp(regDistName,DistributionName)==0))
{
//SUCCESS:Distribution found!
//return LxUID
// SUCCESS: Distribution found
wcscpy_s(wslInstallation.uuid, UUID_SIZE*2, subKey);
DWORD pathSize = MAX_BASEPATH_SIZE*2;
rres = RegQueryValueExW(hKeyS, L"BasePath", NULL, &dwType, (LPBYTE)&wslInstallation.basePath, &pathSize);
if (rres != ERROR_SUCCESS)
{
fwprintf(stderr,L"ERROR:[%i] Could not read registry key\n", rres);
}
RegCloseKey(hKey);
RegCloseKey(hKeyS);
wcscpy_s(LxUID,40,subKey);
return LxUID;
return wslInstallation;
}
RegCloseKey(hKeyS);
}
}
else
{
//ERROR
LxUID = NULL;
return LxUID;
}
}
RegCloseKey(hKey);
//ERROR:Distribution Not Found
LxUID = NULL;
return LxUID;

return wslInstallation;
}

#ifdef __cplusplus
Expand Down

0 comments on commit 301e293

Please sign in to comment.