2013-06-14 11:01:02 -07: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/. */
|
|
|
|
|
2013-09-12 07:47:37 -07:00
|
|
|
#include <algorithm>
|
|
|
|
#include <vector>
|
|
|
|
|
2013-06-14 11:01:02 -07:00
|
|
|
#include "IOInterposer.h"
|
2013-09-12 07:47:37 -07:00
|
|
|
|
|
|
|
#include "mozilla/Mutex.h"
|
|
|
|
#include "mozilla/StaticPtr.h"
|
2013-06-14 11:01:02 -07:00
|
|
|
|
|
|
|
using namespace mozilla;
|
|
|
|
|
2013-09-12 07:47:37 -07:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
/** Lists of Observers */
|
|
|
|
struct ObserverLists {
|
|
|
|
ObserverLists()
|
|
|
|
{
|
|
|
|
mObserverListsLock = PR_NewLock();
|
|
|
|
// We don't do MOZ_COUNT_CTOR(ObserverLists) as we will need to leak the
|
|
|
|
// IO interposer when doing late-write checks, which uses IO interposing
|
|
|
|
// to check for writes while static destructors are invoked.
|
|
|
|
}
|
|
|
|
|
|
|
|
// mObserverListsLock guards access to lists of observers
|
2013-11-11 11:16:31 -08:00
|
|
|
// Note, we can use mozilla::Mutex here as the ObserverLists may be leaked,
|
|
|
|
// as we want to monitor IO during shutdown. Furthermore, as we may have to
|
2013-09-12 07:47:37 -07:00
|
|
|
// unregister observers during shutdown an OffTheBooksMutex is not an option
|
2013-11-11 11:16:31 -08:00
|
|
|
// either, as it base calls into sDeadlockDetector which may be nullptr
|
|
|
|
// during shutdown.
|
2013-09-12 07:47:37 -07:00
|
|
|
PRLock* mObserverListsLock;
|
|
|
|
|
|
|
|
~ObserverLists()
|
|
|
|
{
|
|
|
|
PR_DestroyLock(mObserverListsLock);
|
|
|
|
mObserverListsLock = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lists of observers for read, write and fsync events respectively
|
|
|
|
// These are implemented as vectors since they are allowed to survive gecko,
|
|
|
|
// without reporting leaks. This is necessary for the IOInterposer to be used
|
|
|
|
// for late-write checks.
|
2013-10-01 12:53:19 -07:00
|
|
|
std::vector<IOInterposeObserver*> mCreateObservers;
|
2013-09-12 07:47:37 -07:00
|
|
|
std::vector<IOInterposeObserver*> mReadObservers;
|
|
|
|
std::vector<IOInterposeObserver*> mWriteObservers;
|
|
|
|
std::vector<IOInterposeObserver*> mFSyncObservers;
|
2013-10-01 12:53:19 -07:00
|
|
|
std::vector<IOInterposeObserver*> mStatObservers;
|
|
|
|
std::vector<IOInterposeObserver*> mCloseObservers;
|
2013-09-12 07:47:37 -07:00
|
|
|
};
|
2013-06-14 11:01:02 -07:00
|
|
|
|
2013-09-12 07:47:37 -07:00
|
|
|
/**
|
|
|
|
* A quick and dirty RAII class to automatically lock a PRLock
|
|
|
|
*/
|
|
|
|
class AutoPRLock
|
2013-06-14 11:01:02 -07:00
|
|
|
{
|
2013-09-12 07:47:37 -07:00
|
|
|
PRLock* mLock;
|
|
|
|
public:
|
|
|
|
AutoPRLock(PRLock* aLock)
|
|
|
|
: mLock(aLock)
|
|
|
|
{
|
|
|
|
PR_Lock(aLock);
|
2013-06-14 11:01:02 -07:00
|
|
|
}
|
2013-09-12 07:47:37 -07:00
|
|
|
~AutoPRLock()
|
|
|
|
{
|
|
|
|
PR_Unlock(mLock);
|
|
|
|
}
|
|
|
|
};
|
2013-06-14 11:01:02 -07:00
|
|
|
|
2013-09-12 07:47:37 -07:00
|
|
|
// List of observers registered
|
|
|
|
static StaticAutoPtr<ObserverLists> sObserverLists;
|
2013-06-14 11:01:02 -07:00
|
|
|
|
2013-09-12 07:47:37 -07:00
|
|
|
/** Find if a vector contains a specific element */
|
|
|
|
template<class T>
|
|
|
|
bool VectorContains(const std::vector<T>& vector, const T& element)
|
2013-06-14 11:01:02 -07:00
|
|
|
{
|
2013-09-12 07:47:37 -07:00
|
|
|
return std::find(vector.begin(), vector.end(), element) != vector.end();
|
2013-06-14 11:01:02 -07:00
|
|
|
}
|
|
|
|
|
2013-09-12 07:47:37 -07:00
|
|
|
/** Remove element from a vector */
|
|
|
|
template<class T>
|
|
|
|
void VectorRemove(std::vector<T>& vector, const T& element)
|
2013-06-14 11:01:02 -07:00
|
|
|
{
|
2013-09-12 07:47:37 -07:00
|
|
|
typename std::vector<T>::iterator newEnd = std::remove(vector.begin(),
|
|
|
|
vector.end(), element);
|
|
|
|
vector.erase(newEnd, vector.end());
|
2013-06-14 11:01:02 -07:00
|
|
|
}
|
|
|
|
|
2013-09-12 07:47:37 -07:00
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
// Flags tracking which operations are being observed
|
|
|
|
IOInterposeObserver::Operation IOInterposer::sObservedOperations =
|
|
|
|
IOInterposeObserver::OpNone;
|
|
|
|
|
|
|
|
/* static */ void IOInterposer::Init()
|
2013-06-14 11:01:02 -07:00
|
|
|
{
|
2013-09-12 07:47:37 -07:00
|
|
|
// Don't initialize twice...
|
|
|
|
if (sObserverLists) {
|
|
|
|
return;
|
2013-06-14 11:01:02 -07:00
|
|
|
}
|
2013-09-12 07:47:37 -07:00
|
|
|
sObserverLists = new ObserverLists();
|
|
|
|
sObservedOperations = IOInterposeObserver::OpNone;
|
2013-06-14 11:01:02 -07:00
|
|
|
}
|
|
|
|
|
2013-09-12 07:47:37 -07:00
|
|
|
/* static */ void IOInterposer::Clear()
|
2013-06-14 11:01:02 -07:00
|
|
|
{
|
2013-09-12 07:47:37 -07:00
|
|
|
// Clear() shouldn't be called if Init() wasn't called,
|
|
|
|
MOZ_ASSERT(sObserverLists);
|
|
|
|
if (sObserverLists) {
|
|
|
|
// We require everybody unregister before clearing. If somebody didn't then
|
|
|
|
// this is probably a case where one consumer clears the IO interposer and
|
|
|
|
// another consumer still wants events.
|
|
|
|
MOZ_ASSERT(sObserverLists->mReadObservers.empty());
|
|
|
|
MOZ_ASSERT(sObserverLists->mWriteObservers.empty());
|
|
|
|
MOZ_ASSERT(sObserverLists->mFSyncObservers.empty());
|
|
|
|
|
|
|
|
sObserverLists = nullptr;
|
|
|
|
sObservedOperations = IOInterposeObserver::OpNone;
|
2013-06-14 11:01:02 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-12 07:47:37 -07:00
|
|
|
/* static */ void IOInterposer::Report(
|
|
|
|
IOInterposeObserver::Observation& aObservation)
|
2013-06-14 11:01:02 -07:00
|
|
|
{
|
2013-09-12 07:47:37 -07:00
|
|
|
// IOInterposer::Init most be called before this method
|
|
|
|
MOZ_ASSERT(sObserverLists);
|
|
|
|
if (!sObserverLists) {
|
|
|
|
return;
|
2013-06-14 11:01:02 -07:00
|
|
|
}
|
2013-09-12 07:47:37 -07:00
|
|
|
|
|
|
|
//TODO: We only need read access here, so we should investigate the
|
|
|
|
// performance overhead involved in using some kind of shared lock.
|
|
|
|
// Work towards this end is tracked in bug #913653
|
|
|
|
AutoPRLock listLock(sObserverLists->mObserverListsLock);
|
|
|
|
|
|
|
|
// Don't try to report if there's nobody listening
|
|
|
|
if (!IOInterposer::IsObservedOperation(aObservation.ObservedOperation())) {
|
|
|
|
return;
|
2013-06-14 11:01:02 -07:00
|
|
|
}
|
|
|
|
|
2013-09-12 07:47:37 -07:00
|
|
|
// Decide which list of observers to inform
|
|
|
|
std::vector<IOInterposeObserver*>* observers = nullptr;
|
|
|
|
switch (aObservation.ObservedOperation()) {
|
2013-10-01 12:53:19 -07:00
|
|
|
case IOInterposeObserver::OpCreateOrOpen:
|
|
|
|
{
|
|
|
|
observers = &sObserverLists->mCreateObservers;
|
|
|
|
}
|
|
|
|
break;
|
2013-06-14 11:01:02 -07:00
|
|
|
case IOInterposeObserver::OpRead:
|
|
|
|
{
|
2013-09-12 07:47:37 -07:00
|
|
|
observers = &sObserverLists->mReadObservers;
|
2013-06-14 11:01:02 -07:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case IOInterposeObserver::OpWrite:
|
|
|
|
{
|
2013-09-12 07:47:37 -07:00
|
|
|
observers = &sObserverLists->mWriteObservers;
|
2013-06-14 11:01:02 -07:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case IOInterposeObserver::OpFSync:
|
|
|
|
{
|
2013-09-12 07:47:37 -07:00
|
|
|
observers = &sObserverLists->mFSyncObservers;
|
2013-06-14 11:01:02 -07:00
|
|
|
}
|
|
|
|
break;
|
2013-10-01 12:53:19 -07:00
|
|
|
case IOInterposeObserver::OpStat:
|
|
|
|
{
|
|
|
|
observers = &sObserverLists->mStatObservers;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case IOInterposeObserver::OpClose:
|
|
|
|
{
|
|
|
|
observers = &sObserverLists->mCloseObservers;
|
|
|
|
}
|
|
|
|
break;
|
2013-06-14 11:01:02 -07:00
|
|
|
default:
|
2013-09-12 07:47:37 -07:00
|
|
|
{
|
|
|
|
// Invalid IO operation, see documentation comment for Report()
|
|
|
|
MOZ_ASSERT(false);
|
|
|
|
// Just ignore is in non-debug builds.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(observers);
|
|
|
|
|
|
|
|
// Inform observers
|
|
|
|
uint32_t nObservers = observers->size();
|
|
|
|
for (uint32_t i = 0; i < nObservers; ++i) {
|
|
|
|
(*observers)[i]->Observe(aObservation);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ void IOInterposer::Register(IOInterposeObserver::Operation aOp,
|
|
|
|
IOInterposeObserver* aObserver)
|
|
|
|
{
|
|
|
|
// IOInterposer::Init most be called before this method
|
|
|
|
MOZ_ASSERT(sObserverLists);
|
2013-11-11 11:16:31 -08:00
|
|
|
// We should never register nullptr as observer
|
2013-09-12 07:47:37 -07:00
|
|
|
MOZ_ASSERT(aObserver);
|
|
|
|
if (!sObserverLists || !aObserver) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
AutoPRLock listLock(sObserverLists->mObserverListsLock);
|
|
|
|
|
|
|
|
// You can register to observe multiple types of observations
|
|
|
|
// but you'll never be registered twice for the same observations.
|
2013-10-01 12:53:19 -07:00
|
|
|
if (aOp & IOInterposeObserver::OpCreateOrOpen &&
|
|
|
|
!VectorContains(sObserverLists->mCreateObservers, aObserver)) {
|
|
|
|
sObserverLists->mCreateObservers.push_back(aObserver);
|
|
|
|
}
|
2013-09-12 07:47:37 -07:00
|
|
|
if (aOp & IOInterposeObserver::OpRead &&
|
|
|
|
!VectorContains(sObserverLists->mReadObservers, aObserver)) {
|
|
|
|
sObserverLists->mReadObservers.push_back(aObserver);
|
2013-06-14 11:01:02 -07:00
|
|
|
}
|
2013-09-12 07:47:37 -07:00
|
|
|
if (aOp & IOInterposeObserver::OpWrite &&
|
|
|
|
!VectorContains(sObserverLists->mWriteObservers, aObserver)) {
|
|
|
|
sObserverLists->mWriteObservers.push_back(aObserver);
|
|
|
|
}
|
|
|
|
if (aOp & IOInterposeObserver::OpFSync &&
|
|
|
|
!VectorContains(sObserverLists->mFSyncObservers, aObserver)) {
|
|
|
|
sObserverLists->mFSyncObservers.push_back(aObserver);
|
|
|
|
}
|
2013-10-01 12:53:19 -07:00
|
|
|
if (aOp & IOInterposeObserver::OpStat &&
|
|
|
|
!VectorContains(sObserverLists->mStatObservers, aObserver)) {
|
|
|
|
sObserverLists->mStatObservers.push_back(aObserver);
|
|
|
|
}
|
|
|
|
if (aOp & IOInterposeObserver::OpClose &&
|
|
|
|
!VectorContains(sObserverLists->mCloseObservers, aObserver)) {
|
|
|
|
sObserverLists->mCloseObservers.push_back(aObserver);
|
|
|
|
}
|
2013-09-12 07:47:37 -07:00
|
|
|
|
|
|
|
// Update field of observed operation with the operations that the new
|
|
|
|
// observer is observing.
|
|
|
|
sObservedOperations = (IOInterposeObserver::Operation)
|
|
|
|
(sObservedOperations | aOp);
|
2013-06-14 11:01:02 -07:00
|
|
|
}
|
|
|
|
|
2013-09-12 07:47:37 -07:00
|
|
|
/* static */ void IOInterposer::Unregister(IOInterposeObserver::Operation aOp,
|
|
|
|
IOInterposeObserver* aObserver)
|
|
|
|
{
|
|
|
|
// IOInterposer::Init most be called before this method.
|
|
|
|
MOZ_ASSERT(sObserverLists);
|
|
|
|
if (!sObserverLists) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
AutoPRLock listLock(sObserverLists->mObserverListsLock);
|
|
|
|
|
|
|
|
if (aOp & IOInterposeObserver::OpRead) {
|
|
|
|
VectorRemove(sObserverLists->mReadObservers, aObserver);
|
|
|
|
if (sObserverLists->mReadObservers.empty()) {
|
|
|
|
sObservedOperations = (IOInterposeObserver::Operation)
|
|
|
|
(sObservedOperations & ~IOInterposeObserver::OpRead);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (aOp & IOInterposeObserver::OpWrite) {
|
|
|
|
VectorRemove(sObserverLists->mWriteObservers, aObserver);
|
|
|
|
if (sObserverLists->mWriteObservers.empty()) {
|
|
|
|
sObservedOperations = (IOInterposeObserver::Operation)
|
|
|
|
(sObservedOperations & ~IOInterposeObserver::OpWrite);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (aOp & IOInterposeObserver::OpFSync) {
|
|
|
|
VectorRemove(sObserverLists->mFSyncObservers, aObserver);
|
|
|
|
if (sObserverLists->mFSyncObservers.empty()) {
|
|
|
|
sObservedOperations = (IOInterposeObserver::Operation)
|
|
|
|
(sObservedOperations & ~IOInterposeObserver::OpFSync);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|