Files
roytam1 04083ef9b4 import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1235261 - Part 1: Rename nsAutoTArray to AutoTArray. r=froydnj (0662c2ac56)
- Bug 1235261 - Part 2: Switch some uses of AutoFallibleTArray to AutoTArray. r=froydnj (ab52085f2a)
- Bug 1235261 - Part 3: Switch remaining uses of AutoFallibleTArray to AutoTArray. r=froydnj (3763b16ddd)
- Bug 1235261 - Part 4: Remove AutoFallibleTArray. r=froydnj (5480b0d786)
- Bug 1235261 - Part 5: Merge nsAutoArrayBase into AutoTArray. r=froydnj (6c64e73e3b)
- Bug 1235261 - Part 7: Remove AutoInfallibleTArray. r=froydnj (acf266464e)
- Bug 1222624: Make xpath document() function use nsIPrincipals and nsIURIs rather than strings. r=peterv (5ee694d132)
- Bug 1235261 - Part 6: Rename AutoInfallibleTArray to AutoTArray. r=froydnj (d282f7df6c)
- Bug 1241394 - Hit testing with 3d transforms should use fuzzy when comparing depths. r=thinker (6c3f50670f)
- Bug 1241394 - Follow up to fix windows build bustage. (02ab2600af)
- Bug 1241394 - Check clip for the children of the establisher. r=mattwoodrow (46f151ea55)
- bug 1241453 - allow caching proxies in xpcAccessibleDocuments r=davidb (f5d41ad2ee)
- Bug 1247364 - use AllChildrenIterator::Seek by a11y tree walker, r=davidb (0ec230908e)
- Bug 1248840 - rename TreeWalker::NextChild, r=yzen (c89ecc5a29)
- Bug 1249927 - devirtualize CanHavaAnonymousChildren, r=davdib (89e8088e63)
- Bug 1206598 - Use universal reference to reduce the redundant copy. r=nfroyd (bae4ad6dd1)
- Bug 1247364 - add AllChildrenIterator::Seek, r=bz (215abebf12)
- bug 1241453 - allow storing proxies in xpcAccessibleGeneric::mIntl r=davidb (dd5e6c896b)
- bug 1241453 - allow constructing xpcAccessibles with proxies r=davidb (d0258122be)
- bug 1241453 - fixup xpcAccessible Intl() methods to not assume mIntl is always an Accessible r=davidb (168f71fdf5)
- bug 1241453 - allow xpcAccessibleDocument::mCache to use proxies as keys r=davidb (85b7eec81c)
- bug 1241453 - assert accessibles are only added to non remote xpcAccessibleDocuments r=davidb (7731b21d17)
- bug 1243077 - add ToXPC{,Document} overloads for proxied accessibles r=davidb (7bc085f1b5)
- bug 1243077 - add AccessibleOrProxy xpcAccessible::IntlGeneric() r=davidb (006a635992)
- Bug 1245464 - initialize with 0 mSupportedIfaces in xpcAccessibleGeneric in order to avoid corrupted result after bit-wise operation. r=surkov (ae41bafcef)
- bug 1241453 - allow caching xpc documents for remote documents r=davidb (a357630690)
- bug 1241453 - factor dispatching nsIAccessibleEvents out of HandleAccEvent() r=davidb (091073d981)
- Bug 1249183 - Suppress GC harder, r=terrence (2185ccb4dd)
- Bug 1248420 - Handle JSObject::getGroup OOM in js::ArraySetLength. r=jandem (04b67c8d31)
- Bug 1242270 - Add SPS pseudo frames for the Array.prototype methods; r=shu (f5e5871439)
- Bug 1247701 - Bail from ArrayShiftDenseKernel if the array is used by for-in iteration. r=jandem (41eff38954)
- Bug 1247701 followup - Change ArrayShiftDenseKernel to receive handle. r=bz (b29ce0c555)
2023-11-09 17:37:18 +08:00

279 lines
8.3 KiB
C++

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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/. */
#include "mozilla/ProfileUnlockerWin.h"
#include "nsCOMPtr.h"
#include "nsIFile.h"
#include "nsTArray.h"
#include "nsXPCOM.h"
namespace mozilla {
/**
* RAII class to obtain and manage a handle to a Restart Manager session.
* It opens a new handle upon construction and releases it upon destruction.
*/
class MOZ_STACK_CLASS ScopedRestartManagerSession
{
public:
explicit ScopedRestartManagerSession(ProfileUnlockerWin& aUnlocker)
: mError(ERROR_INVALID_HANDLE)
, mHandle((DWORD)-1) // 0 is a valid restart manager handle
, mUnlocker(aUnlocker)
{
mError = mUnlocker.StartSession(mHandle);
}
~ScopedRestartManagerSession()
{
if (mError == ERROR_SUCCESS) {
mUnlocker.EndSession(mHandle);
}
}
/**
* @return true if the handle is a valid Restart Ranager handle.
*/
inline bool
ok()
{
return mError == ERROR_SUCCESS;
}
/**
* @return the Restart Manager handle to pass to other Restart Manager APIs.
*/
inline DWORD
handle()
{
return mHandle;
}
private:
DWORD mError;
DWORD mHandle;
ProfileUnlockerWin& mUnlocker;
};
ProfileUnlockerWin::ProfileUnlockerWin(const nsAString& aFileName)
: mRmStartSession(nullptr)
, mRmRegisterResources(nullptr)
, mRmGetList(nullptr)
, mRmEndSession(nullptr)
, mQueryFullProcessImageName(nullptr)
, mFileName(aFileName)
{
}
ProfileUnlockerWin::~ProfileUnlockerWin()
{
}
NS_IMPL_ISUPPORTS(ProfileUnlockerWin, nsIProfileUnlocker)
nsresult
ProfileUnlockerWin::Init()
{
MOZ_ASSERT(!mRestartMgrModule);
if (mFileName.IsEmpty()) {
return NS_ERROR_ILLEGAL_VALUE;
}
nsModuleHandle module(::LoadLibraryW(L"Rstrtmgr.dll"));
if (!module) {
return NS_ERROR_NOT_AVAILABLE;
}
mRmStartSession =
reinterpret_cast<RMSTARTSESSION>(::GetProcAddress(module, "RmStartSession"));
if (!mRmStartSession) {
return NS_ERROR_UNEXPECTED;
}
mRmRegisterResources =
reinterpret_cast<RMREGISTERRESOURCES>(::GetProcAddress(module,
"RmRegisterResources"));
if (!mRmRegisterResources) {
return NS_ERROR_UNEXPECTED;
}
mRmGetList = reinterpret_cast<RMGETLIST>(::GetProcAddress(module,
"RmGetList"));
if (!mRmGetList) {
return NS_ERROR_UNEXPECTED;
}
mRmEndSession = reinterpret_cast<RMENDSESSION>(::GetProcAddress(module,
"RmEndSession"));
if (!mRmEndSession) {
return NS_ERROR_UNEXPECTED;
}
mQueryFullProcessImageName =
reinterpret_cast<QUERYFULLPROCESSIMAGENAME>(::GetProcAddress(
::GetModuleHandleW(L"kernel32.dll"),
"QueryFullProcessImageNameW"));
if (!mQueryFullProcessImageName) {
return NS_ERROR_NOT_AVAILABLE;
}
mRestartMgrModule.steal(module);
return NS_OK;
}
DWORD
ProfileUnlockerWin::StartSession(DWORD& aHandle)
{
WCHAR sessionKey[CCH_RM_SESSION_KEY + 1] = {0};
return mRmStartSession(&aHandle, 0, sessionKey);
}
void
ProfileUnlockerWin::EndSession(DWORD aHandle)
{
mRmEndSession(aHandle);
}
NS_IMETHODIMP
ProfileUnlockerWin::Unlock(uint32_t aSeverity)
{
if (!mRestartMgrModule) {
return NS_ERROR_NOT_INITIALIZED;
}
if (aSeverity != FORCE_QUIT) {
return NS_ERROR_NOT_IMPLEMENTED;
}
ScopedRestartManagerSession session(*this);
if (!session.ok()) {
return NS_ERROR_FAILURE;
}
LPCWSTR resources[] = { mFileName.get() };
DWORD error = mRmRegisterResources(session.handle(), 1, resources, 0, nullptr,
0, nullptr);
if (error != ERROR_SUCCESS) {
return NS_ERROR_FAILURE;
}
// Using a AutoTArray here because we expect the required size to be 1.
AutoTArray<RM_PROCESS_INFO, 1> info;
UINT numEntries;
UINT numEntriesNeeded = 1;
error = ERROR_MORE_DATA;
DWORD reason = RmRebootReasonNone;
while (error == ERROR_MORE_DATA) {
info.SetLength(numEntriesNeeded);
numEntries = numEntriesNeeded;
error = mRmGetList(session.handle(), &numEntriesNeeded, &numEntries,
&info[0], &reason);
}
if (error != ERROR_SUCCESS) {
return NS_ERROR_FAILURE;
}
if (numEntries == 0) {
// Nobody else is locking the file; the other process must have terminated
return NS_OK;
}
nsresult rv = NS_ERROR_FAILURE;
for (UINT i = 0; i < numEntries; ++i) {
rv = TryToTerminate(info[i].Process);
if (NS_SUCCEEDED(rv)) {
return NS_OK;
}
}
// If nothing could be unlocked then we return the error code of the last
// failure that was returned.
return rv;
}
nsresult
ProfileUnlockerWin::TryToTerminate(RM_UNIQUE_PROCESS& aProcess)
{
// Subtle: If the target process terminated before this call to OpenProcess,
// this call will still succeed. This is because the restart manager session
// internally retains a handle to the target process. The rules for Windows
// PIDs state that the PID of a terminated process remains valid as long as
// at least one handle to that process remains open, so when we reach this
// point the PID is still valid and the process will open successfully.
DWORD accessRights = PROCESS_QUERY_LIMITED_INFORMATION | PROCESS_TERMINATE;
nsAutoHandle otherProcess(::OpenProcess(accessRights, FALSE,
aProcess.dwProcessId));
if (!otherProcess) {
return NS_ERROR_FAILURE;
}
FILETIME creationTime, exitTime, kernelTime, userTime;
if (!::GetProcessTimes(otherProcess, &creationTime, &exitTime, &kernelTime,
&userTime)) {
return NS_ERROR_FAILURE;
}
if (::CompareFileTime(&aProcess.ProcessStartTime, &creationTime)) {
return NS_ERROR_NOT_AVAILABLE;
}
WCHAR imageName[MAX_PATH];
DWORD imageNameLen = MAX_PATH;
if (!mQueryFullProcessImageName(otherProcess, 0, imageName, &imageNameLen)) {
// The error codes for this function are not very descriptive. There are
// actually two failure cases here: Either the call failed because the
// process is no longer running, or it failed for some other reason. We
// need to know which case that is.
DWORD otherProcessExitCode;
if (!::GetExitCodeProcess(otherProcess, &otherProcessExitCode) ||
otherProcessExitCode == STILL_ACTIVE) {
// The other process is still running.
return NS_ERROR_FAILURE;
}
// The other process must have terminated. We should return NS_OK so that
// this process may proceed with startup.
return NS_OK;
}
nsCOMPtr<nsIFile> otherProcessImageName;
if (NS_FAILED(NS_NewLocalFile(nsDependentString(imageName, imageNameLen),
false, getter_AddRefs(otherProcessImageName)))) {
return NS_ERROR_FAILURE;
}
nsAutoString otherProcessLeafName;
if (NS_FAILED(otherProcessImageName->GetLeafName(otherProcessLeafName))) {
return NS_ERROR_FAILURE;
}
imageNameLen = MAX_PATH;
if (!mQueryFullProcessImageName(::GetCurrentProcess(), 0, imageName,
&imageNameLen)) {
return NS_ERROR_FAILURE;
}
nsCOMPtr<nsIFile> thisProcessImageName;
if (NS_FAILED(NS_NewLocalFile(nsDependentString(imageName, imageNameLen),
false, getter_AddRefs(thisProcessImageName)))) {
return NS_ERROR_FAILURE;
}
nsAutoString thisProcessLeafName;
if (NS_FAILED(thisProcessImageName->GetLeafName(thisProcessLeafName))) {
return NS_ERROR_FAILURE;
}
// Make sure the image leaf names match
if (_wcsicmp(otherProcessLeafName.get(), thisProcessLeafName.get())) {
return NS_ERROR_NOT_AVAILABLE;
}
// We know that another process holds the lock and that it shares the same
// image name as our process. Let's kill it.
// Subtle: TerminateProcess returning ERROR_ACCESS_DENIED is actually an
// indicator that the target process managed to shut down on its own. In that
// case we should return NS_OK since we may proceed with startup.
if (!::TerminateProcess(otherProcess, 1) &&
::GetLastError() != ERROR_ACCESS_DENIED) {
return NS_ERROR_FAILURE;
}
return NS_OK;
}
} // namespace mozilla