relauncher_win.cc 3.8 KB

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