2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2005-08-31 21:50:32 +04:00
|
|
|
|
|
|
|
// This file is not build directly. Instead, it is included in multiple
|
|
|
|
// shared objects.
|
|
|
|
|
|
|
|
#ifdef nsWindowsRestart_cpp
|
|
|
|
# error \
|
|
|
|
"nsWindowsRestart.cpp is not a header file, and must only be included once."
|
|
|
|
#else
|
|
|
|
# define nsWindowsRestart_cpp
|
|
|
|
#endif
|
|
|
|
|
2018-04-20 02:22:41 +03:00
|
|
|
#include "mozilla/CmdLineAndEnvUtils.h"
|
2007-12-31 18:15:43 +03:00
|
|
|
#include "nsUTF8Utils.h"
|
|
|
|
|
2007-01-30 08:14:50 +03:00
|
|
|
#include <shellapi.h>
|
2012-01-05 08:19:14 +04:00
|
|
|
|
2012-01-05 08:19:15 +04:00
|
|
|
// Needed for CreateEnvironmentBlock
|
|
|
|
#include <userenv.h>
|
2018-08-29 06:50:39 +03:00
|
|
|
#ifndef __MINGW32__
|
2012-01-05 08:19:14 +04:00
|
|
|
# pragma comment(lib, "userenv.lib")
|
2018-08-29 06:50:39 +03:00
|
|
|
#endif
|
2007-01-30 08:14:50 +03:00
|
|
|
|
2007-12-31 18:15:43 +03:00
|
|
|
/**
|
|
|
|
* Convert UTF8 to UTF16 without using the normal XPCOM goop, which we
|
|
|
|
* can't link to updater.exe.
|
|
|
|
*/
|
2019-05-01 11:47:10 +03:00
|
|
|
static char16_t* AllocConvertUTF8toUTF16(const char* arg) {
|
2007-12-31 18:15:43 +03:00
|
|
|
// UTF16 can't be longer in units than UTF8
|
2018-07-06 10:44:43 +03:00
|
|
|
size_t len = strlen(arg);
|
2019-05-01 11:47:10 +03:00
|
|
|
char16_t* s = new char16_t[(len + 1) * sizeof(char16_t)];
|
2013-10-11 00:36:42 +04:00
|
|
|
if (!s) return nullptr;
|
2007-12-31 18:15:43 +03:00
|
|
|
|
2018-07-06 10:44:43 +03:00
|
|
|
size_t dstLen = ::MultiByteToWideChar(CP_UTF8, 0, arg, len,
|
2019-05-01 11:47:10 +03:00
|
|
|
reinterpret_cast<wchar_t*>(s), len);
|
2018-07-06 10:44:43 +03:00
|
|
|
s[dstLen] = 0;
|
|
|
|
|
2007-12-31 18:15:43 +03:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
static void FreeAllocStrings(int argc, wchar_t** argv) {
|
2007-12-31 18:15:43 +03:00
|
|
|
while (argc) {
|
|
|
|
--argc;
|
|
|
|
delete[] argv[argc];
|
|
|
|
}
|
|
|
|
|
|
|
|
delete[] argv;
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
static wchar_t** AllocConvertUTF8toUTF16Strings(int argc, char** argv) {
|
|
|
|
wchar_t** argvConverted = new wchar_t*[argc];
|
2018-05-16 01:37:45 +03:00
|
|
|
if (!argvConverted) return nullptr;
|
|
|
|
|
|
|
|
for (int i = 0; i < argc; ++i) {
|
|
|
|
argvConverted[i] =
|
2019-05-01 11:47:10 +03:00
|
|
|
reinterpret_cast<wchar_t*>(AllocConvertUTF8toUTF16(argv[i]));
|
2018-05-16 01:37:45 +03:00
|
|
|
if (!argvConverted[i]) {
|
|
|
|
FreeAllocStrings(i, argvConverted);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return argvConverted;
|
|
|
|
}
|
2012-05-22 18:50:04 +04:00
|
|
|
|
2005-08-31 21:50:32 +04:00
|
|
|
/**
|
|
|
|
* Launch a child process with the specified arguments.
|
|
|
|
* @note argv[0] is ignored
|
2007-12-31 18:15:43 +03:00
|
|
|
* @note The form of this function that takes char **argv expects UTF-8
|
2005-08-31 21:50:32 +04:00
|
|
|
*/
|
2007-12-31 18:15:43 +03:00
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
BOOL WinLaunchChild(const wchar_t* exePath, int argc, wchar_t** argv,
|
|
|
|
HANDLE userToken = nullptr, HANDLE* hProcess = nullptr);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
BOOL WinLaunchChild(const wchar_t* exePath, int argc, char** argv,
|
|
|
|
HANDLE userToken, HANDLE* hProcess) {
|
|
|
|
wchar_t** argvConverted = AllocConvertUTF8toUTF16Strings(argc, argv);
|
2007-12-31 18:15:43 +03:00
|
|
|
if (!argvConverted) return FALSE;
|
|
|
|
|
2012-05-22 18:50:04 +04:00
|
|
|
BOOL ok = WinLaunchChild(exePath, argc, argvConverted, userToken, hProcess);
|
2007-12-31 18:15:43 +03:00
|
|
|
FreeAllocStrings(argc, argvConverted);
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
BOOL WinLaunchChild(const wchar_t* exePath, int argc, wchar_t** argv,
|
|
|
|
HANDLE userToken, HANDLE* hProcess) {
|
2007-01-30 08:14:50 +03:00
|
|
|
BOOL ok;
|
2009-02-06 02:50:44 +03:00
|
|
|
|
2018-04-20 02:22:41 +03:00
|
|
|
mozilla::UniquePtr<wchar_t[]> cl(mozilla::MakeCommandLine(argc, argv));
|
2012-01-05 08:19:14 +04:00
|
|
|
if (!cl) {
|
2005-08-31 21:50:32 +04:00
|
|
|
return FALSE;
|
2012-01-05 08:19:14 +04:00
|
|
|
}
|
2005-08-31 21:50:32 +04:00
|
|
|
|
2012-01-05 08:19:14 +04:00
|
|
|
STARTUPINFOW si = {0};
|
|
|
|
si.cb = sizeof(STARTUPINFOW);
|
2018-09-18 16:04:28 +03:00
|
|
|
si.lpDesktop = const_cast<LPWSTR>(L"winsta0\\Default");
|
2009-01-02 03:18:33 +03:00
|
|
|
PROCESS_INFORMATION pi = {0};
|
|
|
|
|
2013-10-11 00:36:42 +04:00
|
|
|
if (userToken == nullptr) {
|
2012-01-05 08:19:14 +04:00
|
|
|
ok = CreateProcessW(exePath, cl.get(),
|
2013-10-11 00:36:42 +04:00
|
|
|
nullptr, // no special security attributes
|
|
|
|
nullptr, // no special thread attributes
|
2012-01-05 08:19:14 +04:00
|
|
|
FALSE, // don't inherit filehandles
|
2012-01-20 01:30:05 +04:00
|
|
|
0, // creation flags
|
2013-10-11 00:36:42 +04:00
|
|
|
nullptr, // inherit my environment
|
|
|
|
nullptr, // use my current directory
|
2012-01-05 08:19:14 +04:00
|
|
|
&si, &pi);
|
|
|
|
} else {
|
|
|
|
// Create an environment block for the process we're about to start using
|
|
|
|
// the user's token.
|
2013-10-11 00:36:42 +04:00
|
|
|
LPVOID environmentBlock = nullptr;
|
2012-01-05 08:19:14 +04:00
|
|
|
if (!CreateEnvironmentBlock(&environmentBlock, userToken, TRUE)) {
|
2013-10-11 00:36:42 +04:00
|
|
|
environmentBlock = nullptr;
|
2012-01-05 08:19:14 +04:00
|
|
|
}
|
|
|
|
|
2017-07-06 15:00:35 +03:00
|
|
|
ok = CreateProcessAsUserW(userToken, exePath, cl.get(),
|
2013-10-11 00:36:42 +04:00
|
|
|
nullptr, // no special security attributes
|
|
|
|
nullptr, // no special thread attributes
|
|
|
|
FALSE, // don't inherit filehandles
|
|
|
|
0, // creation flags
|
2012-01-05 08:19:14 +04:00
|
|
|
environmentBlock,
|
2013-10-11 00:36:42 +04:00
|
|
|
nullptr, // use my current directory
|
2012-01-05 08:19:14 +04:00
|
|
|
&si, &pi);
|
|
|
|
|
|
|
|
if (environmentBlock) {
|
|
|
|
DestroyEnvironmentBlock(environmentBlock);
|
|
|
|
}
|
|
|
|
}
|
2009-01-02 03:18:33 +03:00
|
|
|
|
|
|
|
if (ok) {
|
2012-05-22 18:50:04 +04:00
|
|
|
if (hProcess) {
|
|
|
|
*hProcess = pi.hProcess; // the caller now owns the HANDLE
|
|
|
|
} else {
|
|
|
|
CloseHandle(pi.hProcess);
|
|
|
|
}
|
2009-01-02 03:18:33 +03:00
|
|
|
CloseHandle(pi.hThread);
|
2009-02-06 02:50:44 +03:00
|
|
|
} else {
|
2013-10-11 00:36:42 +04:00
|
|
|
LPVOID lpMsgBuf = nullptr;
|
2009-02-06 02:50:44 +03:00
|
|
|
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
|
2012-01-05 08:19:14 +04:00
|
|
|
FORMAT_MESSAGE_IGNORE_INSERTS,
|
|
|
|
nullptr, GetLastError(),
|
|
|
|
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&lpMsgBuf,
|
2013-10-11 00:36:42 +04:00
|
|
|
0, nullptr);
|
2016-03-24 04:40:01 +03:00
|
|
|
wprintf(L"Error restarting: %s\n",
|
2019-05-01 11:47:10 +03:00
|
|
|
lpMsgBuf ? static_cast<const wchar_t*>(lpMsgBuf) : L"(null)");
|
2009-05-14 18:17:51 +04:00
|
|
|
if (lpMsgBuf) LocalFree(lpMsgBuf);
|
2005-08-31 21:50:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return ok;
|
|
|
|
}
|