relauncher_win.cc 4.2 KB

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