launch_daemon: Added volume_mounted event.

* Triggered whenever a volume is mounted (surprise!).
* There is no way to specify which volume you are interested in for now
  (if someone knows a good use case, I'd be willing to add that,
  though :-)).
This commit is contained in:
Axel Dörfler 2015-10-17 14:10:31 +02:00
parent 7cd19b7e5c
commit 0f3fcbe437
4 changed files with 232 additions and 0 deletions

View File

@ -18,6 +18,7 @@
#include "BaseJob.h"
#include "LaunchDaemon.h"
#include "Utility.h"
#include "VolumeWatcher.h"
class EventContainer : public Event {
@ -115,6 +116,21 @@ private:
};
class VolumeMountedEvent : public Event, public VolumeListener {
public:
VolumeMountedEvent(Event* parent,
const BMessage& args);
virtual status_t Register(EventRegistrator& registrator);
virtual void Unregister(EventRegistrator& registrator);
virtual BString ToString() const;
virtual void VolumeMounted(dev_t device);
virtual void VolumeUnmounted(dev_t device);
};
static Event*
create_event(Event* parent, const char* name, const BMessenger* target,
const BMessage& args)
@ -130,6 +146,8 @@ create_event(Event* parent, const char* name, const BMessenger* target,
return new DemandEvent(parent);
if (strcmp(name, "file_created") == 0)
return new FileCreatedEvent(parent, args);
if (strcmp(name, "volume_mounted") == 0)
return new VolumeMountedEvent(parent, args);
return new ExternalEvent(parent, name, args);
}
@ -506,6 +524,51 @@ FileCreatedEvent::ToString() const
// #pragma mark -
VolumeMountedEvent::VolumeMountedEvent(Event* parent, const BMessage& args)
:
Event(parent)
{
}
status_t
VolumeMountedEvent::Register(EventRegistrator& registrator)
{
VolumeWatcher::Register(this);
return B_OK;
}
void
VolumeMountedEvent::Unregister(EventRegistrator& registrator)
{
VolumeWatcher::Unregister(this);
}
BString
VolumeMountedEvent::ToString() const
{
return "volume_mounted";
}
void
VolumeMountedEvent::VolumeMounted(dev_t device)
{
Trigger();
}
void
VolumeMountedEvent::VolumeUnmounted(dev_t device)
{
}
// #pragma mark -
/*static*/ Event*
Events::FromMessage(const BMessenger& target, const BMessage& message)
{

View File

@ -16,6 +16,7 @@ Server launch_daemon
SettingsParser.cpp
Target.cpp
Utility.cpp
VolumeWatcher.cpp
Worker.cpp
# init jobs

View File

@ -0,0 +1,126 @@
/*
* Copyright 2015, Axel Dörfler, axeld@pinc-software.de.
* Distributed under the terms of the MIT License.
*/
//! The backbone of the VolumeMountedEvent.
#include "VolumeWatcher.h"
#include <Application.h>
#include <Autolock.h>
#include <NodeMonitor.h>
static BLocker sLocker("volume watcher");
static VolumeWatcher* sWatcher;
VolumeListener::~VolumeListener()
{
}
// #pragma mark -
VolumeWatcher::VolumeWatcher()
:
BHandler("volume watcher")
{
if (be_app->Lock()) {
be_app->AddHandler(this);
watch_node(NULL, B_WATCH_MOUNT, this);
be_app->Unlock();
}
}
VolumeWatcher::~VolumeWatcher()
{
if (be_app->Lock()) {
stop_watching(this);
be_app->RemoveHandler(this);
be_app->Unlock();
}
}
void
VolumeWatcher::AddListener(VolumeListener* listener)
{
BAutolock lock(sLocker);
fListeners.AddItem(listener);
}
void
VolumeWatcher::RemoveListener(VolumeListener* listener)
{
BAutolock lock(sLocker);
fListeners.RemoveItem(listener);
}
int32
VolumeWatcher::CountListeners() const
{
BAutolock lock(sLocker);
return fListeners.CountItems();
}
void
VolumeWatcher::MessageReceived(BMessage* message)
{
switch (message->what) {
case B_NODE_MONITOR:
{
int32 opcode = message->GetInt32("opcode", -1);
if (opcode == B_DEVICE_MOUNTED) {
dev_t device;
if (message->FindInt32("new device", &device) == B_OK) {
BAutolock lock(sLocker);
for (int32 i = 0; i < fListeners.CountItems(); i++) {
fListeners.ItemAt(i)->VolumeMounted(device);
}
}
} else if (opcode == B_DEVICE_UNMOUNTED) {
dev_t device;
if (message->FindInt32("device", &device) == B_OK) {
BAutolock lock(sLocker);
for (int32 i = 0; i < fListeners.CountItems(); i++) {
fListeners.ItemAt(i)->VolumeUnmounted(device);
}
}
}
break;
}
}
}
/*static*/ void
VolumeWatcher::Register(VolumeListener* listener)
{
BAutolock lock(sLocker);
if (sWatcher == NULL)
sWatcher = new VolumeWatcher();
sWatcher->AddListener(listener);
}
/*static*/ void
VolumeWatcher::Unregister(VolumeListener* listener)
{
BAutolock lock(sLocker);
sWatcher->RemoveListener(listener);
if (sWatcher->CountListeners() == 0)
delete sWatcher;
}

View File

@ -0,0 +1,42 @@
/*
* Copyright 2015, Axel Dörfler, axeld@pinc-software.de.
* Distributed under the terms of the MIT License.
*/
#ifndef VOLUME_WATCHER_H
#define VOLUME_WATCHER_H
#include <Handler.h>
#include <ObjectList.h>
class VolumeListener {
public:
virtual ~VolumeListener();
virtual void VolumeMounted(dev_t device) = 0;
virtual void VolumeUnmounted(dev_t device) = 0;
};
class VolumeWatcher : public BHandler {
public:
VolumeWatcher();
virtual ~VolumeWatcher();
void AddListener(VolumeListener* listener);
void RemoveListener(VolumeListener* listener);
int32 CountListeners() const;
virtual void MessageReceived(BMessage* message);
static void Register(VolumeListener* listener);
static void Unregister(VolumeListener* listener);
protected:
BObjectList<VolumeListener>
fListeners;
};
#endif // VOLUME_WATCHER_H