Skip to content
This repository was archived by the owner on Mar 23, 2020. It is now read-only.

Commit ff5a7d1

Browse files
committed
main: use uid_from_username.
1 parent 56c2249 commit ff5a7d1

File tree

1 file changed

+6
-35
lines changed

1 file changed

+6
-35
lines changed

main.c

Lines changed: 6 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -63,42 +63,13 @@ HRESULT new_user(void)
6363
hr = _WslLaunchInteractive(DISTOR_NAME, command, FALSE, &exit_code);
6464
} while (FAILED(hr) || exit_code != 0);
6565

66-
HANDLE read_pipe, write_pipe;
67-
SECURITY_ATTRIBUTES sec_attr;
68-
sec_attr.nLength = sizeof(SECURITY_ATTRIBUTES);
69-
sec_attr.lpSecurityDescriptor = NULL;
70-
sec_attr.bInheritHandle = TRUE;
66+
wchar_t username_w[33];
67+
for (int i = 0; i < ARRAYSIZE(username_w) && username[i]; i++)
68+
username_w[i] = username[i];
7169

72-
if (CreatePipe(&read_pipe, &write_pipe, &sec_attr, 0))
73-
{
74-
swprintf(command, ARRAYSIZE(command), L"id -u %hs", username);
75-
76-
HANDLE handle_stdin = GetStdHandle(STD_INPUT_HANDLE);
77-
HANDLE handle_stderr = GetStdHandle(STD_ERROR_HANDLE);
78-
HANDLE process_handle;
79-
hr = _WslLaunch(DISTOR_NAME, command, FALSE, handle_stdin, write_pipe, handle_stderr, &process_handle);
80-
if (SUCCEEDED(hr))
81-
{
82-
WaitForSingleObject(process_handle, INFINITE);
83-
GetExitCodeProcess(process_handle, &exit_code);
84-
CloseHandle(process_handle);
85-
86-
if (exit_code == 0)
87-
{
88-
char uid_string[16];
89-
DWORD byte_read;
90-
if (ReadFile(read_pipe, uid_string, 15, &byte_read, NULL))
91-
{
92-
CloseHandle(read_pipe);
93-
CloseHandle(write_pipe);
94-
95-
uid_string[byte_read] = '\0';
96-
ULONG uid = strtoul(uid_string, NULL, 10);
97-
hr = _WslConfigureDistribution(DISTOR_NAME, uid, WSL_DISTRIBUTION_FLAGS_DEFAULT);
98-
}
99-
}
100-
}
101-
}
70+
ULONG uid = uid_from_username(DISTOR_NAME, username_w);
71+
if (uid != ULONG_MAX)
72+
hr = _WslConfigureDistribution(DISTOR_NAME, uid, WSL_DISTRIBUTION_FLAGS_DEFAULT);
10273
}
10374
return hr;
10475
}

0 commit comments

Comments
 (0)