forked from yuk7/wsldl
-
Notifications
You must be signed in to change notification settings - Fork 1
/
wsld.h
186 lines (160 loc) · 6.15 KB
/
wsld.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
* Copyright (c) 2017-2020 yuk7
* Author: yuk7 <[email protected]>
*
* Released under the MIT license
* http://opensource.org/licenses/mit-license.php
*/
#ifndef WSLD_H_
#define WSLD_H_
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
#include <windows.h>
#ifdef __cplusplus
extern "C" {
#endif
// WSL APIs declarations. Old mingw-w64 may not have wslapi.h
HRESULT WINAPI WslIsDistributionRegistered (PCWSTR);
HRESULT WINAPI WslRegisterDistribution (PCWSTR,PCWSTR);
HRESULT WINAPI WslUnregisterDistribution (PCWSTR);
HRESULT WINAPI WslConfigureDistribution (PCWSTR,ULONG,INT);
HRESULT WINAPI WslGetDistributionConfiguration (PCWSTR,ULONG*,ULONG*,INT*,PSTR*,ULONG*);
HRESULT WINAPI WslLaunchInteractive (PCWSTR,PCWSTR,BOOL,DWORD*);
HRESULT WINAPI WslLaunch (PCWSTR,PCWSTR,BOOL,HANDLE,HANDLE,HANDLE,HANDLE*);
#define LXSS_BASE_RKEY L"Software\\Microsoft\\Windows\\CurrentVersion\\Lxss"
#define WSLDL_TERM_KEY L"wsldl-term"
#define MAX_DISTRO_NAME_SIZE 50
#define MAX_BASEPATH_SIZE 128
#define UUID_SIZE 38
struct WslInstallation {
wchar_t uuid[UUID_SIZE+1];
wchar_t basePath[MAX_BASEPATH_SIZE];
wchar_t distroName[MAX_DISTRO_NAME_SIZE];
long termInfo;
} WslInstallation;
struct WslInstallation WslGetInstallationInfo(wchar_t *DistributionName) {
struct WslInstallation wslInstallation = {.uuid = {0}, .basePath = {0}, .distroName = {0}, .termInfo = 0};
HKEY hKey;
LONG rres;
if(RegOpenKeyExW(HKEY_CURRENT_USER,LXSS_BASE_RKEY, 0, KEY_READ, &hKey) == ERROR_SUCCESS)
{
int i;
for(i=0;;i++)
{
wchar_t subKeyF[200];
wcscpy_s(subKeyF,(sizeof(subKeyF)/sizeof(subKeyF[0])),LXSS_BASE_RKEY);
wchar_t subKey[200];
DWORD subKeySz = 100;
FILETIME ftLastWriteTime;
rres = RegEnumKeyExW(hKey, i, subKey, &subKeySz, NULL, NULL, NULL, &ftLastWriteTime);
if (rres == ERROR_NO_MORE_ITEMS)
break;
else if(rres != ERROR_SUCCESS)
{
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);
wchar_t regDistName[MAX_DISTRO_NAME_SIZE*2];
DWORD dwSize = MAX_DISTRO_NAME_SIZE;
rres = RegQueryValueExW(hKeyS, L"DistributionName", NULL, &dwType, (LPBYTE)®DistName,&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) && (_wcsicmp(regDistName,DistributionName)==0))
{
// SUCCESS: Distribution found
wcscpy_s(wslInstallation.uuid, UUID_SIZE*2, subKey);
DWORD dnSize = MAX_DISTRO_NAME_SIZE*2;
RegQueryValueExW(hKeyS, L"DistributionName", NULL, &dwType, (LPBYTE)&wslInstallation.distroName, &dnSize);
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);
}
DWORD tiSize = (int)sizeof(wslInstallation.termInfo);
RegQueryValueExW(hKeyS, WSLDL_TERM_KEY, NULL, NULL, (LPBYTE)&wslInstallation.termInfo, &tiSize);
RegCloseKey(hKey);
RegCloseKey(hKeyS);
return wslInstallation;
}
RegCloseKey(hKeyS);
}
}
RegCloseKey(hKey);
return wslInstallation;
}
int WslSetInstallationPathInfo(wchar_t *uuid,wchar_t *basePath)
{
LONG rres;
HKEY hKey;
wchar_t RKey[200];
wcscpy_s(RKey,(sizeof(RKey)/sizeof(RKey[0])),LXSS_BASE_RKEY);
wcscat_s(RKey,(sizeof(RKey)/sizeof(RKey[0])),L"\\");
wcscat_s(RKey,(sizeof(RKey)/sizeof(RKey[0])),uuid);
RegOpenKeyExW(HKEY_CURRENT_USER,RKey, 0, KEY_SET_VALUE, &hKey);
rres = RegSetValueExW(hKey,L"BasePath",0,REG_SZ,(const BYTE*)basePath,MAX_BASEPATH_SIZE);
if (rres != ERROR_SUCCESS)
{
fwprintf(stderr,L"ERROR:[%i] Write registory key failed\n", rres);
return 1;
}
return 0;
}
int WsldlSetTerminalInfo(wchar_t *uuid ,long id)
{
LONG rres;
HKEY hKey;
wchar_t RKey[200];
wcscpy_s(RKey,(sizeof(RKey)/sizeof(RKey[0])),LXSS_BASE_RKEY);
wcscat_s(RKey,(sizeof(RKey)/sizeof(RKey[0])),L"\\");
wcscat_s(RKey,(sizeof(RKey)/sizeof(RKey[0])),uuid);
RegOpenKeyExW(HKEY_CURRENT_USER,RKey, 0, KEY_SET_VALUE, &hKey);
rres = RegSetValueExW(hKey, WSLDL_TERM_KEY, 0, REG_DWORD, (const BYTE*)&id, (int)sizeof(id));
return rres;
}
unsigned long WslExec(wchar_t *DistroName, wchar_t *command, char *result, long unsigned int *len)
{
HANDLE hProcess;
HANDLE hOutTmp,hOut;
HANDLE hInTmp,hIn;
SECURITY_ATTRIBUTES sa;
sa.nLength = sizeof(sa);
sa.bInheritHandle = TRUE;
sa.lpSecurityDescriptor = NULL;
CreatePipe(&hOut, &hOutTmp, &sa, 0);
CreatePipe(&hIn, &hInTmp, &sa, 0);
if(WslLaunch(DistroName, command, 1, hInTmp, hOutTmp, hOutTmp, &hProcess))
{
return 100000;
}
CloseHandle(hInTmp);
CloseHandle(hOutTmp);
WaitForSingleObject(hProcess, INFINITE);
unsigned long exitcode;
GetExitCodeProcess(hProcess, &exitcode);
if(!ReadFile(hOut, result, *len, len, NULL))
{
return 200000;
}
if(result[(strrchr(result, '\0') - result) - 1] == '\n')
{
result[(strrchr(result, '\0') - result) - 1] = '\0';
}
CloseHandle(hIn);
CloseHandle(hOut);
CloseHandle(hProcess);
return exitcode;
}
#ifdef __cplusplus
}
#endif
#endif