relauncher_win.cc 3.8 KB

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