relauncher_win.cc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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/process/process_handle.h"
  9. #include "base/strings/strcat_win.h"
  10. #include "base/strings/string_number_conversions_win.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. namespace relauncher::internal {
  15. namespace {
  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. base::ProcessId ppid = base::GetParentProcessId(handle);
  38. if (ppid == 0u) {
  39. LOG(ERROR) << "Could not get parent process handle";
  40. return nullptr;
  41. }
  42. return ::OpenProcess(PROCESS_ALL_ACCESS, TRUE, ppid);
  43. }
  44. StringType AddQuoteForArg(const StringType& arg) {
  45. // We follow the quoting rules of CommandLineToArgvW.
  46. // http://msdn.microsoft.com/en-us/library/17w5ykft.aspx
  47. std::wstring quotable_chars(L" \\\"");
  48. if (arg.find_first_of(quotable_chars) == std::wstring::npos) {
  49. // No quoting necessary.
  50. return arg;
  51. }
  52. std::wstring out;
  53. out.push_back(L'"');
  54. for (size_t i = 0; i < arg.size(); ++i) {
  55. if (arg[i] == '\\') {
  56. // Find the extent of this run of backslashes.
  57. size_t start = i, end = start + 1;
  58. for (; end < arg.size() && arg[end] == '\\'; ++end) {
  59. }
  60. size_t backslash_count = end - start;
  61. // Backslashes are escapes only if the run is followed by a double quote.
  62. // Since we also will end the string with a double quote, we escape for
  63. // either a double quote or the end of the string.
  64. if (end == arg.size() || arg[end] == '"') {
  65. // To quote, we need to output 2x as many backslashes.
  66. backslash_count *= 2;
  67. }
  68. for (size_t j = 0; j < backslash_count; ++j)
  69. out.push_back('\\');
  70. // Advance i to one before the end to balance i++ in loop.
  71. i = end - 1;
  72. } else if (arg[i] == '"') {
  73. out.push_back('\\');
  74. out.push_back('"');
  75. } else {
  76. out.push_back(arg[i]);
  77. }
  78. }
  79. out.push_back('"');
  80. return out;
  81. }
  82. } // namespace
  83. StringType GetWaitEventName(base::ProcessId pid) {
  84. return base::StrCat({L"ElectronRelauncherWaitEvent-",
  85. base::NumberToWString(static_cast<int>(pid))});
  86. }
  87. StringType ArgvToCommandLineString(const StringVector& argv) {
  88. StringType command_line;
  89. for (const StringType& arg : argv) {
  90. if (!command_line.empty())
  91. command_line += L' ';
  92. command_line += AddQuoteForArg(arg);
  93. }
  94. return command_line;
  95. }
  96. void RelauncherSynchronizeWithParent() {
  97. base::Process process = base::Process::Current();
  98. base::win::ScopedHandle parent_process(
  99. GetParentProcessHandle(process.Handle()));
  100. // Notify the parent process that it can quit now.
  101. StringType name = internal::GetWaitEventName(process.Pid());
  102. base::win::ScopedHandle wait_event(
  103. CreateEvent(nullptr, TRUE, FALSE, name.c_str()));
  104. ::SetEvent(wait_event.Get());
  105. // Wait for parent process to quit.
  106. WaitForSingleObject(parent_process.Get(), INFINITE);
  107. }
  108. int LaunchProgram(const StringVector& relauncher_args,
  109. const StringVector& argv) {
  110. base::LaunchOptions options;
  111. base::Process process =
  112. base::LaunchProcess(ArgvToCommandLineString(argv), options);
  113. return process.IsValid() ? 0 : 1;
  114. }
  115. } // namespace relauncher::internal