vfs: check userland buffers before reading them.
[haiku.git] / src / servers / registrar / PackageWatchingManager.cpp
blob6716cb5d98c014b8495e79b98364e0430eba9b4f
1 /*
2 * Copyright 2014, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Distributed under the terms of the MIT License.
4 */
7 #include "PackageWatchingManager.h"
9 #include <new>
11 #include <package/PackageRoster.h>
13 #include <RegistrarDefs.h>
15 #include "Debug.h"
16 #include "EventMaskWatcher.h"
19 using namespace BPackageKit;
20 using namespace BPrivate;
23 PackageWatchingManager::PackageWatchingManager()
28 PackageWatchingManager::~PackageWatchingManager()
33 void
34 PackageWatchingManager::HandleStartStopWatching(BMessage* request)
36 status_t error = request->what == B_REG_PACKAGE_START_WATCHING
37 ? _AddWatcher(request) : _RemoveWatcher(request);
39 if (error == B_OK) {
40 BMessage reply(B_REG_SUCCESS);
41 request->SendReply(&reply);
42 } else {
43 BMessage reply(B_REG_ERROR);
44 reply.AddInt32("error", error);
45 request->SendReply(&reply);
50 void
51 PackageWatchingManager::NotifyWatchers(BMessage* message)
53 int32 event;
54 if (message->FindInt32("event", &event) != B_OK) {
55 WARNING("No event field in notification message\n");
56 return;
59 uint32 eventMask;
60 switch (event) {
61 case B_INSTALLATION_LOCATION_PACKAGES_CHANGED:
62 eventMask = B_WATCH_PACKAGE_INSTALLATION_LOCATIONS;
63 break;
64 default:
65 WARNING("Invalid event: %" B_PRId32 "\n", event);
66 return;
69 EventMaskWatcherFilter filter(eventMask);
70 fWatchingService.NotifyWatchers(message, &filter);
74 status_t
75 PackageWatchingManager::_AddWatcher(const BMessage* request)
77 BMessenger target;
78 uint32 eventMask;
79 status_t error;
80 if ((error = request->FindMessenger("target", &target)) != B_OK
81 || (error = request->FindUInt32("events", &eventMask)) != B_OK) {
82 return error;
85 Watcher* watcher = new(std::nothrow) EventMaskWatcher(target, eventMask);
86 if (watcher == NULL || !fWatchingService.AddWatcher(watcher)) {
87 delete watcher;
88 return B_NO_MEMORY;
91 return B_OK;
95 status_t
96 PackageWatchingManager::_RemoveWatcher(const BMessage* request)
98 BMessenger target;
99 status_t error;
100 if ((error = request->FindMessenger("target", &target)) != B_OK)
101 return error;
103 if (!fWatchingService.RemoveWatcher(target))
104 return B_BAD_VALUE;
106 return B_OK;