Skip to content

Commit

Permalink
Refactor ConsoleProcessList (#14421)
Browse files Browse the repository at this point in the history
This commit is just a slight refactor of `ConsoleProcessList` which I've noticed
was in a poor shape. It replaces iterators with for-range loops, etc.

Additionally this fixes a bug in `SetConsoleWindowOwner`, where it used to
default to the newest client process instead of the oldest.

Finally, it changes the process container type from a doubly linked list
over to a simple array/vector, because using linked lists for heap allocated
elements felt quite a bit silly. To preserve the previous behavior of
`GetProcessList`, it simply iterates through the vector backwards.

## Validation Steps Performed
* All unit/feature tests pass ✅
* Launching a TUI application inside pwsh inside cmd
  and exiting kills all 3 applications ✅

(cherry picked from commit 391abaf)
Service-Card-Id: 87207825
Service-Version: 1.16
  • Loading branch information
lhecker authored and DHowett committed Dec 12, 2022
1 parent a7b0fdf commit 2f1d2ea
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 206 deletions.
1 change: 0 additions & 1 deletion src/host/ft_fuzzer/fuzzmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ struct NullDeviceComm : public IDeviceComm
RETURN_IF_FAILED(gci.ProcessHandleList.AllocProcessData(GetCurrentProcessId(),
GetCurrentThreadId(),
0,
nullptr,
&pProcessHandle));
pProcessHandle->fRootProcess = true;

Expand Down
40 changes: 15 additions & 25 deletions src/host/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,27 +274,21 @@ void ProcessCtrlEvents()
const auto LimitingProcessId = gci.LimitingProcessId;
gci.LimitingProcessId = 0;

ConsoleProcessTerminationRecord* rgProcessHandleList;
size_t cProcessHandleList;

auto hr = gci.ProcessHandleList
.GetTerminationRecordsByGroupId(LimitingProcessId,
WI_IsFlagSet(gci.CtrlFlags,
CONSOLE_CTRL_CLOSE_FLAG),
&rgProcessHandleList,
&cProcessHandleList);

if (FAILED(hr) || cProcessHandleList == 0)
std::vector<ConsoleProcessTerminationRecord> termRecords;
const auto hr = gci.ProcessHandleList
.GetTerminationRecordsByGroupId(LimitingProcessId,
WI_IsFlagSet(gci.CtrlFlags,
CONSOLE_CTRL_CLOSE_FLAG),
termRecords);

if (FAILED(hr) || termRecords.empty())
{
gci.UnlockConsole();
return;
}

// Copy ctrl flags.
auto CtrlFlags = gci.CtrlFlags;
FAIL_FAST_IF(!(!((CtrlFlags & (CONSOLE_CTRL_CLOSE_FLAG | CONSOLE_CTRL_BREAK_FLAG | CONSOLE_CTRL_C_FLAG)) && (CtrlFlags & (CONSOLE_CTRL_LOGOFF_FLAG | CONSOLE_CTRL_SHUTDOWN_FLAG)))));

gci.CtrlFlags = 0;
const auto CtrlFlags = std::exchange(gci.CtrlFlags, 0);

gci.UnlockConsole();

Expand Down Expand Up @@ -329,10 +323,13 @@ void ProcessCtrlEvents()
case CONSOLE_CTRL_SHUTDOWN_FLAG:
EventType = CTRL_SHUTDOWN_EVENT;
break;

default:
return;
}

auto Status = STATUS_SUCCESS;
for (size_t i = 0; i < cProcessHandleList; i++)
for (const auto& r : termRecords)
{
/*
* Status will be non-successful if a process attached to this console
Expand All @@ -346,20 +343,13 @@ void ProcessCtrlEvents()
if (NT_SUCCESS(Status))
{
Status = ServiceLocator::LocateConsoleControl()
->EndTask(rgProcessHandleList[i].dwProcessID,
->EndTask(r.dwProcessID,
EventType,
CtrlFlags);
if (rgProcessHandleList[i].hProcess == nullptr)
if (!r.hProcess)
{
Status = STATUS_SUCCESS;
}
}

if (rgProcessHandleList[i].hProcess != nullptr)
{
CloseHandle(rgProcessHandleList[i].hProcess);
}
}

delete[] rgProcessHandleList;
}
6 changes: 3 additions & 3 deletions src/interactivity/win32/windowio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ VOID SetConsoleWindowOwner(const HWND hwnd, _Inout_opt_ ConsoleProcessHandle* pP
else
{
// Find a process to own the console window. If there are none then let's use conhost's.
pProcessData = gci.ProcessHandleList.FindProcessInList(ConsoleProcessList::ROOT_PROCESS_ID);
pProcessData = gci.ProcessHandleList.GetRootProcess();
if (!pProcessData)
{
// No root process ID? Pick the oldest existing process.
pProcessData = gci.ProcessHandleList.GetFirstProcess();
pProcessData = gci.ProcessHandleList.GetOldestProcess();
}

if (pProcessData != nullptr)
Expand Down Expand Up @@ -986,7 +986,7 @@ LRESULT CALLBACK DialogHookProc(int nCode, WPARAM /*wParam*/, LPARAM lParam)
NTSTATUS InitWindowsSubsystem(_Out_ HHOOK* phhook)
{
auto& gci = ServiceLocator::LocateGlobals().getConsoleInformation();
auto ProcessData = gci.ProcessHandleList.FindProcessInList(ConsoleProcessList::ROOT_PROCESS_ID);
auto ProcessData = gci.ProcessHandleList.GetRootProcess();
FAIL_FAST_IF(!(ProcessData != nullptr && ProcessData->fRootProcess));

// Create and activate the main window
Expand Down
1 change: 0 additions & 1 deletion src/server/ApiDispatchersInternal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ using Microsoft::Console::Interactivity::ServiceLocator;
RETURN_IF_FAILED(gci.ProcessHandleList.AllocProcessData(a->ProcessGroupId,
0,
a->ProcessGroupId,
ProcessHandle,
nullptr));
}
}
Expand Down
1 change: 0 additions & 1 deletion src/server/IoDispatchers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,6 @@ PCONSOLE_API_MSG IoDispatchers::ConsoleHandleConnectionRequest(_In_ PCONSOLE_API
Status = NTSTATUS_FROM_HRESULT(gci.ProcessHandleList.AllocProcessData(dwProcessId,
dwThreadId,
Cac.ProcessGroupId,
nullptr,
&ProcessData));

if (!NT_SUCCESS(Status))
Expand Down
18 changes: 9 additions & 9 deletions src/server/ProcessHandle.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ Revision History:
class ConsoleProcessHandle
{
public:
ConsoleProcessHandle(const DWORD dwProcessId,
const DWORD dwThreadId,
const ULONG ulProcessGroupId);
~ConsoleProcessHandle() = default;
ConsoleProcessHandle(const ConsoleProcessHandle&) = delete;
ConsoleProcessHandle(ConsoleProcessHandle&&) = delete;
ConsoleProcessHandle& operator=(const ConsoleProcessHandle&) & = delete;
ConsoleProcessHandle& operator=(ConsoleProcessHandle&&) & = delete;

const std::unique_ptr<ConsoleWaitQueue> pWaitBlockQueue;
std::unique_ptr<ConsoleHandleData> pInputHandle;
std::unique_ptr<ConsoleHandleData> pOutputHandle;
Expand All @@ -47,15 +56,6 @@ class ConsoleProcessHandle
const ULONG64 GetProcessCreationTime() const;

private:
ConsoleProcessHandle(const DWORD dwProcessId,
const DWORD dwThreadId,
const ULONG ulProcessGroupId);
~ConsoleProcessHandle() = default;
ConsoleProcessHandle(const ConsoleProcessHandle&) = delete;
ConsoleProcessHandle(ConsoleProcessHandle&&) = delete;
ConsoleProcessHandle& operator=(const ConsoleProcessHandle&) & = delete;
ConsoleProcessHandle& operator=(ConsoleProcessHandle&&) & = delete;

ULONG _ulTerminateCount;
ULONG const _ulProcessGroupId;
wil::unique_handle const _hProcess;
Expand Down
Loading

1 comment on commit 2f1d2ea

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@check-spelling-bot Report

🔴 Please review

See the 📜action log for details.

Unrecognized words (2)

BGRA
finsh

Previously acknowledged words that are now absent aabbcc abbcc bgra DECARM DECCARA DECERA DECFRA DECRARA DECSACE DECSERA DECXCPR GETKEYSTATE hicon IWIC MAPVIRTUALKEY nto otepad Qaabbcc Qxxxxxxxxxxxxxxx Tdd VKKEYSCAN wic wincodec xwwyzz xxyyzz ZYXWVU ZYXWVUTd :arrow_right:
To accept ✔️ these unrecognized words as correct and remove the previously acknowledged and now absent words, run the following commands

... in a clone of the git@github.com:microsoft/terminal.git repository
on the release-1.16 branch (ℹ️ how do I use this?):

curl -s -S -L 'https://raw.githubusercontent.com/check-spelling/check-spelling/v0.0.21/apply.pl' |
perl - 'https://github.com/microsoft/terminal/actions/runs/3680815486/attempts/1'
✏️ Contributor please read this

By default the command suggestion will generate a file named based on your commit. That's generally ok as long as you add the file to your commit. Someone can reorganize it later.

⚠️ The command is written for posix shells. If it doesn't work for you, you can manually add (one word per line) / remove items to expect.txt and the excludes.txt files.

If the listed items are:

  • ... misspelled, then please correct them instead of using the command.
  • ... names, please add them to .github/actions/spelling/allow/names.txt.
  • ... APIs, you can add them to a file in .github/actions/spelling/allow/.
  • ... just things you're using, please add them to an appropriate file in .github/actions/spelling/expect/.
  • ... tokens you only need in one place and shouldn't generally be used, you can add an item in an appropriate file in .github/actions/spelling/patterns/.

See the README.md in each directory for more information.

🔬 You can test your commits without appending to a PR by creating a new branch with that extra change and pushing it to your fork. The check-spelling action will run in response to your push -- it doesn't require an open pull request. By using such a branch, you can limit the number of typos your peers see you make. 😉

If the flagged items are 🤯 false positives

If items relate to a ...

  • binary file (or some other file you wouldn't want to check at all).

    Please add a file path to the excludes.txt file matching the containing file.

    File paths are Perl 5 Regular Expressions - you can test yours before committing to verify it will match your files.

    ^ refers to the file's path from the root of the repository, so ^README\.md$ would exclude README.md (on whichever branch you're using).

  • well-formed pattern.

    If you can write a pattern that would match it,
    try adding it to the patterns.txt file.

    Patterns are Perl 5 Regular Expressions - you can test yours before committing to verify it will match your lines.

    Note that patterns can't match multiline strings.

Please sign in to comment.