-
Notifications
You must be signed in to change notification settings - Fork 1.7k
CLI: Initialize cidfile option #40135
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: feature/wsl-for-apps
Are you sure you want to change the base?
Changes from 4 commits
207f05d
4c2ffc5
c981875
575d3cd
86295a8
801a7ac
ce4d909
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,6 +20,8 @@ Module Name: | |
| #include <wslutil.h> | ||
| #include <WSLCProcessLauncher.h> | ||
| #include <CommandLine.h> | ||
| #include <filesystem> | ||
| #include <fstream> | ||
AmelBawa-msft marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| #include <unordered_map> | ||
| #include <wslc.h> | ||
|
|
||
|
|
@@ -37,6 +39,35 @@ static void SetContainerArguments(WSLCProcessOptions& options, std::vector<const | |
| options.CommandLine = {.Values = argsStorage.data(), .Count = static_cast<ULONG>(argsStorage.size())}; | ||
| } | ||
|
|
||
| static void WriteContainerIdToFile(const std::optional<std::wstring>& cidFilePath, const std::string& containerId) | ||
| { | ||
| if (!cidFilePath.has_value()) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| const auto path = std::filesystem::path(cidFilePath.value()); | ||
| HANDLE file = ::CreateFileW(path.c_str(), GENERIC_WRITE, 0, nullptr, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, nullptr); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I recommend using: That way we don't manually have to close the handle |
||
| if (file == INVALID_HANDLE_VALUE) | ||
| { | ||
| const auto error = ::GetLastError(); | ||
| const auto errorMessage = wsl::shared::string::MultiByteToWide(std::system_category().message(error)); | ||
| THROW_HR_WITH_USER_ERROR(HRESULT_FROM_WIN32(error), Localization::MessageWslcFailedToOpenFile(*cidFilePath, errorMessage)); | ||
| } | ||
|
|
||
| DWORD bytesWritten{}; | ||
| const bool writeSuccess = ::WriteFile(file, containerId.data(), static_cast<DWORD>(containerId.size()), &bytesWritten, nullptr) != FALSE; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: We can simplify this to: I think it would be OK not to have specialized errors for write errors here, since they're very unlikely since we successfully opened the handle at this point |
||
| const HRESULT closeResult = ::CloseHandle(file) ? S_OK : HRESULT_FROM_WIN32(GetLastError()); | ||
| if (!writeSuccess || bytesWritten != containerId.size()) | ||
AmelBawa-msft marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| const auto error = writeSuccess ? ERROR_WRITE_FAULT : ::GetLastError(); | ||
| const auto errorMessage = wsl::shared::string::MultiByteToWide(std::system_category().message(error)); | ||
| THROW_HR_WITH_USER_ERROR(HRESULT_FROM_WIN32(error), Localization::MessageWslcFailedToOpenFile(*cidFilePath, errorMessage)); | ||
| } | ||
AmelBawa-msft marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| THROW_IF_FAILED(closeResult); | ||
AmelBawa-msft marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| static wsl::windows::common::RunningWSLCContainer CreateInternal(Session& session, const std::string& image, const ContainerOptions& options) | ||
| { | ||
| auto processFlags = WSLCProcessFlagsNone; | ||
|
|
@@ -294,6 +325,10 @@ int ContainerService::Run(Session& session, const std::string& image, ContainerO | |
| runningContainer.SetDeleteOnClose(false); | ||
| auto& container = runningContainer.Get(); | ||
|
|
||
| WSLCContainerId containerId{}; | ||
| THROW_IF_FAILED(container.GetId(containerId)); | ||
| WriteContainerIdToFile(runOptions.CidFile, containerId); | ||
|
|
||
AmelBawa-msft marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // Start the created container | ||
| WSLCContainerStartFlags startFlags{}; | ||
| WI_SetFlagIf(startFlags, WSLCContainerStartFlagsAttach, !runOptions.Detach); | ||
|
|
@@ -306,8 +341,6 @@ int ContainerService::Run(Session& session, const std::string& image, ContainerO | |
| return consoleService.AttachToCurrentConsole(runningContainer.GetInitProcess()); | ||
| } | ||
|
|
||
| WSLCContainerId containerId{}; | ||
| THROW_IF_FAILED(container.GetId(containerId)); | ||
| PrintMessage(L"%hs", stdout, containerId); | ||
| return 0; | ||
| } | ||
|
|
@@ -319,6 +352,7 @@ CreateContainerResult ContainerService::Create(Session& session, const std::stri | |
| auto& container = runningContainer.Get(); | ||
| WSLCContainerId id{}; | ||
| THROW_IF_FAILED(container.GetId(id)); | ||
| WriteContainerIdToFile(runOptions.CidFile, id); | ||
| return {.Id = id}; | ||
AmelBawa-msft marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't recommend manually checking if it the file exists since that creates a potential race condition with the CreateFile() call later.