2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2021-05-12 22:17:51 +03:00
|
|
|
* Copyright (c) 2021, sin-ack <sin-ack@protonmail.com>
|
2020-01-18 11:38:21 +03:00
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2019-07-22 21:01:11 +03:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/Badge.h>
|
2021-05-12 22:17:51 +03:00
|
|
|
#include <AK/Checked.h>
|
2019-07-22 21:01:11 +03:00
|
|
|
#include <AK/CircularQueue.h>
|
2021-05-12 22:17:51 +03:00
|
|
|
#include <AK/HashMap.h>
|
|
|
|
#include <AK/NonnullOwnPtr.h>
|
2020-11-07 10:38:48 +03:00
|
|
|
#include <Kernel/API/InodeWatcherEvent.h>
|
2019-07-22 21:01:11 +03:00
|
|
|
#include <Kernel/FileSystem/File.h>
|
2021-07-11 12:49:16 +03:00
|
|
|
#include <Kernel/Forward.h>
|
2023-04-21 14:48:11 +03:00
|
|
|
#include <Kernel/Locking/MutexProtected.h>
|
|
|
|
#include <Kernel/Locking/SpinlockProtected.h>
|
2019-07-22 21:01:11 +03:00
|
|
|
|
2020-02-16 03:27:42 +03:00
|
|
|
namespace Kernel {
|
|
|
|
|
2021-05-12 22:17:51 +03:00
|
|
|
// A specific description of a watch.
|
|
|
|
struct WatchDescription {
|
|
|
|
int wd;
|
|
|
|
Inode& inode;
|
|
|
|
unsigned event_mask;
|
|
|
|
|
2021-11-08 02:51:39 +03:00
|
|
|
static ErrorOr<NonnullOwnPtr<WatchDescription>> create(int wd, Inode& inode, unsigned event_mask)
|
2021-05-12 22:17:51 +03:00
|
|
|
{
|
2021-09-05 22:40:01 +03:00
|
|
|
return adopt_nonnull_own_or_enomem(new (nothrow) WatchDescription(wd, inode, event_mask));
|
2021-05-12 22:17:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
WatchDescription(int wd, Inode& inode, unsigned event_mask)
|
|
|
|
: wd(wd)
|
|
|
|
, inode(inode)
|
|
|
|
, event_mask(event_mask)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-07-22 21:01:11 +03:00
|
|
|
class InodeWatcher final : public File {
|
|
|
|
public:
|
2023-03-10 09:53:02 +03:00
|
|
|
static ErrorOr<NonnullRefPtr<InodeWatcher>> try_create();
|
2019-07-22 21:01:11 +03:00
|
|
|
virtual ~InodeWatcher() override;
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
virtual bool can_read(OpenFileDescription const&, u64) const override;
|
2021-11-08 02:51:39 +03:00
|
|
|
virtual ErrorOr<size_t> read(OpenFileDescription&, u64, UserOrKernelBuffer&, size_t) override;
|
2021-05-12 22:17:51 +03:00
|
|
|
// Can't write to an inode watcher.
|
2022-04-01 20:58:27 +03:00
|
|
|
virtual bool can_write(OpenFileDescription const&, u64) const override { return true; }
|
|
|
|
virtual ErrorOr<size_t> write(OpenFileDescription&, u64, UserOrKernelBuffer const&, size_t) override { return EIO; }
|
2021-11-08 02:51:39 +03:00
|
|
|
virtual ErrorOr<void> close() override;
|
2021-05-12 22:17:51 +03:00
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
virtual ErrorOr<NonnullOwnPtr<KString>> pseudo_path(OpenFileDescription const&) const override;
|
2023-07-08 05:48:11 +03:00
|
|
|
virtual StringView class_name() const override { return "InodeWatcher"sv; }
|
2021-05-12 22:17:51 +03:00
|
|
|
virtual bool is_inode_watcher() const override { return true; }
|
|
|
|
|
2022-01-11 23:04:53 +03:00
|
|
|
void notify_inode_event(Badge<Inode>, InodeIdentifier, InodeWatcherEvent::Type, StringView name = {});
|
2019-07-22 21:01:11 +03:00
|
|
|
|
2021-11-08 02:51:39 +03:00
|
|
|
ErrorOr<int> register_inode(Inode&, unsigned event_mask);
|
|
|
|
ErrorOr<void> unregister_by_wd(int);
|
2021-05-12 22:17:51 +03:00
|
|
|
void unregister_by_inode(Badge<Inode>, InodeIdentifier);
|
2019-07-22 21:01:11 +03:00
|
|
|
|
|
|
|
private:
|
2021-05-12 22:17:51 +03:00
|
|
|
explicit InodeWatcher() { }
|
|
|
|
|
|
|
|
struct Event {
|
|
|
|
int wd { 0 };
|
|
|
|
InodeWatcherEvent::Type type { InodeWatcherEvent::Type::Invalid };
|
2022-01-21 13:56:00 +03:00
|
|
|
OwnPtr<KString> path;
|
2021-05-12 22:17:51 +03:00
|
|
|
};
|
2023-04-21 14:48:11 +03:00
|
|
|
SpinlockProtected<CircularQueue<Event, 32>, LockRank::None> m_queue;
|
2021-05-12 22:17:51 +03:00
|
|
|
Checked<int> m_wd_counter { 1 };
|
2019-07-22 21:01:11 +03:00
|
|
|
|
2021-05-12 22:17:51 +03:00
|
|
|
// NOTE: These two hashmaps provide two different ways of reaching the same
|
|
|
|
// watch description, so they will overlap.
|
2023-04-21 14:48:11 +03:00
|
|
|
struct WatchMaps {
|
|
|
|
HashMap<int, NonnullOwnPtr<WatchDescription>> wd_to_watches;
|
|
|
|
HashMap<InodeIdentifier, WatchDescription*> inode_to_watches;
|
|
|
|
};
|
|
|
|
|
|
|
|
mutable SpinlockProtected<WatchMaps, LockRank::None> m_watch_maps;
|
2019-07-22 21:01:11 +03:00
|
|
|
};
|
2020-02-16 03:27:42 +03:00
|
|
|
|
|
|
|
}
|