Skip to content

Commit

Permalink
[Action runner] Support running generic targets as non-elevated (#3863)
Browse files Browse the repository at this point in the history
* Initial work, not tested

* Forgot the most important part

* Use target argument instead of hardcoded string

* Removed old way of running Launcher

* Completed rebase, updated some code
  • Loading branch information
ivan100sic committed Jun 8, 2020
1 parent 21929b1 commit eb4b429
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 11 deletions.
57 changes: 49 additions & 8 deletions src/action_runner/action_runner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,22 +186,63 @@ int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
}
std::wstring_view action{ args[1] };

if (action == L"-start_PowerLauncher")
if (action == L"-run-non-elevated")
{
HANDLE hMapFile = OpenFileMappingW(FILE_MAP_WRITE, FALSE, POWER_LAUNCHER_PID_SHARED_FILE);
if (hMapFile)
int nextArg = 2;

std::wstring_view target;
std::wstring_view pidFile;

while (nextArg < nArgs)
{
if (std::wstring_view(args[nextArg]) == L"-target" && nextArg + 1 < nArgs)
{
target = args[nextArg + 1];
nextArg += 2;
}
else if (std::wstring_view(args[nextArg]) == L"-pidFile" && nextArg + 1 < nArgs)
{
pidFile = args[nextArg + 1];
nextArg += 2;
}
else
{
nextArg++;
}
}

HANDLE hMapFile = NULL;
PDWORD pidBuffer = NULL;

if (!pidFile.empty())
{
hMapFile = OpenFileMappingW(FILE_MAP_WRITE, FALSE, pidFile.data());
if (hMapFile)
{
pidBuffer = reinterpret_cast<PDWORD>(MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, sizeof(DWORD)));
if (pidBuffer)
{
*pidBuffer = 0;
}
}
}

run_same_elevation(target.data(), L"", pidBuffer);

// cleanup
if (!pidFile.empty())
{
PDWORD pidBuffer = reinterpret_cast<PDWORD>(MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, sizeof(DWORD)));
if (pidBuffer)
{
*pidBuffer = 0;
run_same_elevation(L"modules\\launcher\\PowerLauncher.exe", L"", pidBuffer);
FlushViewOfFile(pidBuffer, sizeof(DWORD));
UnmapViewOfFile(pidBuffer);
}

FlushFileBuffers(hMapFile);
CloseHandle(hMapFile);
if (hMapFile)
{
FlushFileBuffers(hMapFile);
CloseHandle(hMapFile);
}
}
}
else if (action == L"-install_dotnet")
Expand Down
12 changes: 9 additions & 3 deletions src/modules/launcher/Microsoft.Launcher/dllmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,14 @@ class Microsoft_Launcher : public PowertoyModuleIface {
else
{
std::wstring action_runner_path = get_module_folderpath();
action_runner_path += L"\\action_runner.exe";

std::wstring params;
params += L"-run-non-elevated ";
params += L"-target modules\\launcher\\PowerLauncher.exe ";
params += L"-pidFile ";
params += POWER_LAUNCHER_PID_SHARED_FILE;

action_runner_path += L"\\action_runner.exe";
// Set up the shared file from which to retrieve the PID of PowerLauncher
HANDLE hMapFile = CreateFileMappingW(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, sizeof(DWORD), POWER_LAUNCHER_PID_SHARED_FILE);
if (hMapFile)
Expand All @@ -157,9 +163,9 @@ class Microsoft_Launcher : public PowertoyModuleIface {
*pidBuffer = 0;
m_hProcess = NULL;

if (run_non_elevated(action_runner_path, L"-start_PowerLauncher", nullptr))
if (run_non_elevated(action_runner_path, params, pidBuffer))
{
const int maxRetries = 20;
const int maxRetries = 80;
for (int retry = 0; retry < maxRetries; ++retry)
{
Sleep(50);
Expand Down

0 comments on commit eb4b429

Please sign in to comment.