vfs: check userland buffers before reading them.
[haiku.git] / src / servers / launch / VolumeWatcher.cpp
blobaf7eb8fc52dcb761b65cbfc840e2422f523dc07d
1 /*
2 * Copyright 2015, Axel Dörfler, axeld@pinc-software.de.
3 * Distributed under the terms of the MIT License.
4 */
7 //! The backbone of the VolumeMountedEvent.
10 #include "VolumeWatcher.h"
12 #include <Application.h>
13 #include <Autolock.h>
14 #include <NodeMonitor.h>
17 static BLocker sLocker("volume watcher");
18 static VolumeWatcher* sWatcher;
21 VolumeListener::~VolumeListener()
26 // #pragma mark -
29 VolumeWatcher::VolumeWatcher()
31 BHandler("volume watcher")
33 if (be_app->Lock()) {
34 be_app->AddHandler(this);
36 watch_node(NULL, B_WATCH_MOUNT, this);
37 be_app->Unlock();
42 VolumeWatcher::~VolumeWatcher()
44 if (be_app->Lock()) {
45 stop_watching(this);
47 be_app->RemoveHandler(this);
48 be_app->Unlock();
53 void
54 VolumeWatcher::AddListener(VolumeListener* listener)
56 BAutolock lock(sLocker);
57 fListeners.AddItem(listener);
61 void
62 VolumeWatcher::RemoveListener(VolumeListener* listener)
64 BAutolock lock(sLocker);
65 fListeners.RemoveItem(listener);
69 int32
70 VolumeWatcher::CountListeners() const
72 BAutolock lock(sLocker);
73 return fListeners.CountItems();
77 void
78 VolumeWatcher::MessageReceived(BMessage* message)
80 switch (message->what) {
81 case B_NODE_MONITOR:
83 int32 opcode = message->GetInt32("opcode", -1);
84 if (opcode == B_DEVICE_MOUNTED) {
85 dev_t device;
86 if (message->FindInt32("new device", &device) == B_OK) {
87 BAutolock lock(sLocker);
88 for (int32 i = 0; i < fListeners.CountItems(); i++) {
89 fListeners.ItemAt(i)->VolumeMounted(device);
92 } else if (opcode == B_DEVICE_UNMOUNTED) {
93 dev_t device;
94 if (message->FindInt32("device", &device) == B_OK) {
95 BAutolock lock(sLocker);
96 for (int32 i = 0; i < fListeners.CountItems(); i++) {
97 fListeners.ItemAt(i)->VolumeUnmounted(device);
101 break;
107 /*static*/ void
108 VolumeWatcher::Register(VolumeListener* listener)
110 BAutolock lock(sLocker);
111 if (sWatcher == NULL)
112 sWatcher = new VolumeWatcher();
114 sWatcher->AddListener(listener);
118 /*static*/ void
119 VolumeWatcher::Unregister(VolumeListener* listener)
121 BAutolock lock(sLocker);
122 sWatcher->RemoveListener(listener);
124 if (sWatcher->CountListeners() == 0)
125 delete sWatcher;