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 "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 =
  25. NtQueryInformationProcess(handle, ProcessBasicInformation, &pbi,
  26. 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. }
  50. size_t backslash_count = end - start;
  51. // Backslashes are escapes only if the run is followed by a double quote.
  52. // Since we also will end the string with a double quote, we escape for
  53. // either a double quote or the end of the string.
  54. if (end == arg.size() || arg[end] == '"') {
  55. // To quote, we need to output 2x as many backslashes.
  56. backslash_count *= 2;
  57. }
  58. for (size_t j = 0; j < backslash_count; ++j)
  59. out.push_back('\\');
  60. // Advance i to one before the end to balance i++ in loop.
  61. i = end - 1;
  62. } else if (arg[i] == '"') {
  63. out.push_back('\\');
  64. out.push_back('"');
  65. } else {
  66. out.push_back(arg[i]);
  67. }
  68. }
  69. out.push_back('"');
  70. return out;
  71. }
  72. } // namespace
  73. StringType GetWaitEventName(base::ProcessId pid) {
  74. return base::StringPrintf(L"%ls-%d", kWaitEventName, static_cast<int>(pid));
  75. }
  76. StringType ArgvToCommandLineString(const StringVector& argv) {
  77. StringType command_line;
  78. for (const StringType& arg : argv) {
  79. if (!command_line.empty())
  80. command_line += L' ';
  81. command_line += AddQuoteForArg(arg);
  82. }
  83. return command_line;
  84. }
  85. void RelauncherSynchronizeWithParent() {
  86. base::Process process = base::Process::Current();
  87. base::win::ScopedHandle parent_process(
  88. GetParentProcessHandle(process.Handle()));
  89. // Notify the parent process that it can quit now.
  90. StringType name = internal::GetWaitEventName(process.Pid());
  91. base::win::ScopedHandle wait_event(
  92. ::CreateEventW(NULL, TRUE, FALSE, name.c_str()));
  93. ::SetEvent(wait_event.Get());
  94. // Wait for parent process to quit.
  95. WaitForSingleObject(parent_process.Get(), INFINITE);
  96. }
  97. int LaunchProgram(const StringVector& relauncher_args,
  98. const StringVector& argv) {
  99. base::LaunchOptions options;
  100. base::Process process =
  101. base::LaunchProcess(ArgvToCommandLineString(argv), options);
  102. return process.IsValid() ? 0 : 1;
  103. }
  104. } // namespace internal
  105. } // namespace relauncher