relauncher_win.cc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // Copyright (c) 2016 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "shell/browser/relauncher.h"
  5. #include <windows.h>
  6. #include "base/logging.h"
  7. #include "base/process/launch.h"
  8. #include "base/strings/strcat_win.h"
  9. #include "base/strings/string_number_conversions_win.h"
  10. #include "base/strings/utf_string_conversions.h"
  11. #include "base/win/scoped_handle.h"
  12. #include "sandbox/win/src/nt_internals.h"
  13. #include "sandbox/win/src/win_utils.h"
  14. #include "ui/base/win/shell.h"
  15. namespace relauncher::internal {
  16. namespace {
  17. const CharType* kWaitEventName = L"ElectronRelauncherWaitEvent";
  18. struct PROCESS_BASIC_INFORMATION {
  19. union {
  20. NTSTATUS ExitStatus;
  21. PVOID padding_for_x64_0;
  22. };
  23. PPEB PebBaseAddress;
  24. KAFFINITY AffinityMask;
  25. union {
  26. KPRIORITY BasePriority;
  27. PVOID padding_for_x64_1;
  28. };
  29. union {
  30. DWORD UniqueProcessId;
  31. PVOID padding_for_x64_2;
  32. };
  33. union {
  34. DWORD InheritedFromUniqueProcessId;
  35. PVOID padding_for_x64_3;
  36. };
  37. };
  38. HANDLE GetParentProcessHandle(base::ProcessHandle handle) {
  39. NtQueryInformationProcessFunction NtQueryInformationProcess = nullptr;
  40. ResolveNTFunctionPtr("NtQueryInformationProcess", &NtQueryInformationProcess);
  41. if (!NtQueryInformationProcess) {
  42. LOG(ERROR) << "Unable to get NtQueryInformationProcess";
  43. return nullptr;
  44. }
  45. PROCESS_BASIC_INFORMATION pbi;
  46. LONG status =
  47. NtQueryInformationProcess(handle, ProcessBasicInformation, &pbi,
  48. sizeof(PROCESS_BASIC_INFORMATION), nullptr);
  49. if (!NT_SUCCESS(status)) {
  50. LOG(ERROR) << "NtQueryInformationProcess failed";
  51. return nullptr;
  52. }
  53. return ::OpenProcess(PROCESS_ALL_ACCESS, TRUE,
  54. pbi.InheritedFromUniqueProcessId);
  55. }
  56. StringType AddQuoteForArg(const StringType& arg) {
  57. // We follow the quoting rules of CommandLineToArgvW.
  58. // http://msdn.microsoft.com/en-us/library/17w5ykft.aspx
  59. std::wstring quotable_chars(L" \\\"");
  60. if (arg.find_first_of(quotable_chars) == std::wstring::npos) {
  61. // No quoting necessary.
  62. return arg;
  63. }
  64. std::wstring out;
  65. out.push_back(L'"');
  66. for (size_t i = 0; i < arg.size(); ++i) {
  67. if (arg[i] == '\\') {
  68. // Find the extent of this run of backslashes.
  69. size_t start = i, end = start + 1;
  70. for (; end < arg.size() && arg[end] == '\\'; ++end) {
  71. }
  72. size_t backslash_count = end - start;
  73. // Backslashes are escapes only if the run is followed by a double quote.
  74. // Since we also will end the string with a double quote, we escape for
  75. // either a double quote or the end of the string.
  76. if (end == arg.size() || arg[end] == '"') {
  77. // To quote, we need to output 2x as many backslashes.
  78. backslash_count *= 2;
  79. }
  80. for (size_t j = 0; j < backslash_count; ++j)
  81. out.push_back('\\');
  82. // Advance i to one before the end to balance i++ in loop.
  83. i = end - 1;
  84. } else if (arg[i] == '"') {
  85. out.push_back('\\');
  86. out.push_back('"');
  87. } else {
  88. out.push_back(arg[i]);
  89. }
  90. }
  91. out.push_back('"');
  92. return out;
  93. }
  94. } // namespace
  95. StringType GetWaitEventName(base::ProcessId pid) {
  96. return base::StrCat(
  97. {kWaitEventName, L"-", base::NumberToWString(static_cast<int>(pid))});
  98. }
  99. StringType ArgvToCommandLineString(const StringVector& argv) {
  100. StringType command_line;
  101. for (const StringType& arg : argv) {
  102. if (!command_line.empty())
  103. command_line += L' ';
  104. command_line += AddQuoteForArg(arg);
  105. }
  106. return command_line;
  107. }
  108. void RelauncherSynchronizeWithParent() {
  109. base::Process process = base::Process::Current();
  110. base::win::ScopedHandle parent_process(
  111. GetParentProcessHandle(process.Handle()));
  112. // Notify the parent process that it can quit now.
  113. StringType name = internal::GetWaitEventName(process.Pid());
  114. base::win::ScopedHandle wait_event(
  115. CreateEvent(nullptr, TRUE, FALSE, name.c_str()));
  116. ::SetEvent(wait_event.Get());
  117. // Wait for parent process to quit.
  118. WaitForSingleObject(parent_process.Get(), INFINITE);
  119. }
  120. int LaunchProgram(const StringVector& relauncher_args,
  121. const StringVector& argv) {
  122. base::LaunchOptions options;
  123. base::Process process =
  124. base::LaunchProcess(ArgvToCommandLineString(argv), options);
  125. return process.IsValid() ? 0 : 1;
  126. }
  127. } // namespace relauncher::internal