nsWindowsRestart.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. // This file is not build directly. Instead, it is included in multiple
  5. // shared objects.
  6. #ifdef nsWindowsRestart_cpp
  7. #error "nsWindowsRestart.cpp is not a header file, and must only be included once."
  8. #else
  9. #define nsWindowsRestart_cpp
  10. #endif
  11. #include "nsUTF8Utils.h"
  12. #include <shellapi.h>
  13. // Needed for CreateEnvironmentBlock
  14. #include <userenv.h>
  15. #pragma comment(lib, "userenv.lib")
  16. /**
  17. * Get the length that the string will take and takes into account the
  18. * additional length if the string needs to be quoted and if characters need to
  19. * be escaped.
  20. */
  21. static int ArgStrLen(const wchar_t *s)
  22. {
  23. int backslashes = 0;
  24. int i = wcslen(s);
  25. BOOL hasDoubleQuote = wcschr(s, L'"') != nullptr;
  26. // Only add doublequotes if the string contains a space or a tab
  27. BOOL addDoubleQuotes = wcspbrk(s, L" \t") != nullptr;
  28. if (addDoubleQuotes) {
  29. i += 2; // initial and final duoblequote
  30. }
  31. if (hasDoubleQuote) {
  32. while (*s) {
  33. if (*s == '\\') {
  34. ++backslashes;
  35. } else {
  36. if (*s == '"') {
  37. // Escape the doublequote and all backslashes preceding the doublequote
  38. i += backslashes + 1;
  39. }
  40. backslashes = 0;
  41. }
  42. ++s;
  43. }
  44. }
  45. return i;
  46. }
  47. /**
  48. * Copy string "s" to string "d", quoting the argument as appropriate and
  49. * escaping doublequotes along with any backslashes that immediately precede
  50. * doublequotes.
  51. * The CRT parses this to retrieve the original argc/argv that we meant,
  52. * see STDARGV.C in the MSVC CRT sources.
  53. *
  54. * @return the end of the string
  55. */
  56. static wchar_t* ArgToString(wchar_t *d, const wchar_t *s)
  57. {
  58. int backslashes = 0;
  59. BOOL hasDoubleQuote = wcschr(s, L'"') != nullptr;
  60. // Only add doublequotes if the string contains a space or a tab
  61. BOOL addDoubleQuotes = wcspbrk(s, L" \t") != nullptr;
  62. if (addDoubleQuotes) {
  63. *d = '"'; // initial doublequote
  64. ++d;
  65. }
  66. if (hasDoubleQuote) {
  67. int i;
  68. while (*s) {
  69. if (*s == '\\') {
  70. ++backslashes;
  71. } else {
  72. if (*s == '"') {
  73. // Escape the doublequote and all backslashes preceding the doublequote
  74. for (i = 0; i <= backslashes; ++i) {
  75. *d = '\\';
  76. ++d;
  77. }
  78. }
  79. backslashes = 0;
  80. }
  81. *d = *s;
  82. ++d; ++s;
  83. }
  84. } else {
  85. wcscpy(d, s);
  86. d += wcslen(s);
  87. }
  88. if (addDoubleQuotes) {
  89. *d = '"'; // final doublequote
  90. ++d;
  91. }
  92. return d;
  93. }
  94. /**
  95. * Creates a command line from a list of arguments. The returned
  96. * string is allocated with "malloc" and should be "free"d.
  97. *
  98. * argv is UTF8
  99. */
  100. wchar_t*
  101. MakeCommandLine(int argc, wchar_t **argv)
  102. {
  103. int i;
  104. int len = 0;
  105. // The + 1 of the last argument handles the allocation for null termination
  106. for (i = 0; i < argc; ++i)
  107. len += ArgStrLen(argv[i]) + 1;
  108. // Protect against callers that pass 0 arguments
  109. if (len == 0)
  110. len = 1;
  111. wchar_t *s = (wchar_t*) malloc(len * sizeof(wchar_t));
  112. if (!s)
  113. return nullptr;
  114. wchar_t *c = s;
  115. for (i = 0; i < argc; ++i) {
  116. c = ArgToString(c, argv[i]);
  117. if (i + 1 != argc) {
  118. *c = ' ';
  119. ++c;
  120. }
  121. }
  122. *c = '\0';
  123. return s;
  124. }
  125. /**
  126. * Convert UTF8 to UTF16 without using the normal XPCOM goop, which we
  127. * can't link to updater.exe.
  128. */
  129. static char16_t*
  130. AllocConvertUTF8toUTF16(const char *arg)
  131. {
  132. // UTF16 can't be longer in units than UTF8
  133. int len = strlen(arg);
  134. char16_t *s = new char16_t[(len + 1) * sizeof(char16_t)];
  135. if (!s)
  136. return nullptr;
  137. ConvertUTF8toUTF16 convert(s);
  138. convert.write(arg, len);
  139. convert.write_terminator();
  140. return s;
  141. }
  142. static void
  143. FreeAllocStrings(int argc, wchar_t **argv)
  144. {
  145. while (argc) {
  146. --argc;
  147. delete [] argv[argc];
  148. }
  149. delete [] argv;
  150. }
  151. /**
  152. * Launch a child process with the specified arguments.
  153. * @note argv[0] is ignored
  154. * @note The form of this function that takes char **argv expects UTF-8
  155. */
  156. BOOL
  157. WinLaunchChild(const wchar_t *exePath,
  158. int argc, wchar_t **argv,
  159. HANDLE userToken = nullptr,
  160. HANDLE *hProcess = nullptr);
  161. BOOL
  162. WinLaunchChild(const wchar_t *exePath,
  163. int argc, char **argv,
  164. HANDLE userToken,
  165. HANDLE *hProcess)
  166. {
  167. wchar_t** argvConverted = new wchar_t*[argc];
  168. if (!argvConverted)
  169. return FALSE;
  170. for (int i = 0; i < argc; ++i) {
  171. argvConverted[i] = reinterpret_cast<wchar_t*>(AllocConvertUTF8toUTF16(argv[i]));
  172. if (!argvConverted[i]) {
  173. FreeAllocStrings(i, argvConverted);
  174. return FALSE;
  175. }
  176. }
  177. BOOL ok = WinLaunchChild(exePath, argc, argvConverted, userToken, hProcess);
  178. FreeAllocStrings(argc, argvConverted);
  179. return ok;
  180. }
  181. BOOL
  182. WinLaunchChild(const wchar_t *exePath,
  183. int argc,
  184. wchar_t **argv,
  185. HANDLE userToken,
  186. HANDLE *hProcess)
  187. {
  188. wchar_t *cl;
  189. BOOL ok;
  190. cl = MakeCommandLine(argc, argv);
  191. if (!cl) {
  192. return FALSE;
  193. }
  194. STARTUPINFOW si = {0};
  195. si.cb = sizeof(STARTUPINFOW);
  196. si.lpDesktop = L"winsta0\\Default";
  197. PROCESS_INFORMATION pi = {0};
  198. if (userToken == nullptr) {
  199. ok = CreateProcessW(exePath,
  200. cl,
  201. nullptr, // no special security attributes
  202. nullptr, // no special thread attributes
  203. FALSE, // don't inherit filehandles
  204. 0, // creation flags
  205. nullptr, // inherit my environment
  206. nullptr, // use my current directory
  207. &si,
  208. &pi);
  209. } else {
  210. // Create an environment block for the process we're about to start using
  211. // the user's token.
  212. LPVOID environmentBlock = nullptr;
  213. if (!CreateEnvironmentBlock(&environmentBlock, userToken, TRUE)) {
  214. environmentBlock = nullptr;
  215. }
  216. ok = CreateProcessAsUserW(userToken,
  217. exePath,
  218. cl,
  219. nullptr, // no special security attributes
  220. nullptr, // no special thread attributes
  221. FALSE, // don't inherit filehandles
  222. 0, // creation flags
  223. environmentBlock,
  224. nullptr, // use my current directory
  225. &si,
  226. &pi);
  227. if (environmentBlock) {
  228. DestroyEnvironmentBlock(environmentBlock);
  229. }
  230. }
  231. if (ok) {
  232. if (hProcess) {
  233. *hProcess = pi.hProcess; // the caller now owns the HANDLE
  234. } else {
  235. CloseHandle(pi.hProcess);
  236. }
  237. CloseHandle(pi.hThread);
  238. } else {
  239. LPVOID lpMsgBuf = nullptr;
  240. FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
  241. FORMAT_MESSAGE_FROM_SYSTEM |
  242. FORMAT_MESSAGE_IGNORE_INSERTS,
  243. nullptr,
  244. GetLastError(),
  245. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  246. (LPTSTR) &lpMsgBuf,
  247. 0,
  248. nullptr);
  249. wprintf(L"Error restarting: %s\n", lpMsgBuf ? static_cast<const wchar_t*>(lpMsgBuf) : L"(null)");
  250. if (lpMsgBuf)
  251. LocalFree(lpMsgBuf);
  252. }
  253. free(cl);
  254. return ok;
  255. }