InodeWatcher.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, sin-ack <sin-ack@protonmail.com>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <Kernel/FileSystem/Inode.h>
  8. #include <Kernel/FileSystem/InodeWatcher.h>
  9. #include <Kernel/Tasks/Process.h>
  10. namespace Kernel {
  11. ErrorOr<NonnullRefPtr<InodeWatcher>> InodeWatcher::try_create()
  12. {
  13. return adopt_nonnull_ref_or_enomem(new (nothrow) InodeWatcher);
  14. }
  15. InodeWatcher::~InodeWatcher()
  16. {
  17. (void)close();
  18. }
  19. bool InodeWatcher::can_read(OpenFileDescription const&, u64) const
  20. {
  21. return m_queue.with([](auto& queue) { return !queue.is_empty(); });
  22. }
  23. ErrorOr<size_t> InodeWatcher::read(OpenFileDescription&, u64, UserOrKernelBuffer& buffer, size_t buffer_size)
  24. {
  25. auto event = TRY(m_queue.with([](auto& queue) -> ErrorOr<Event> {
  26. if (queue.is_empty()) {
  27. // can_read will catch the blocking case.
  28. return EAGAIN;
  29. }
  30. return queue.dequeue();
  31. }));
  32. size_t bytes_to_write = sizeof(InodeWatcherEvent);
  33. if (event.path)
  34. bytes_to_write += event.path->length() + 1;
  35. if (buffer_size < bytes_to_write)
  36. return EINVAL;
  37. auto result = buffer.write_buffered<MAXIMUM_EVENT_SIZE>(bytes_to_write, [&](Bytes bytes) {
  38. size_t offset = 0;
  39. memcpy(bytes.offset(offset), &event.wd, sizeof(InodeWatcherEvent::watch_descriptor));
  40. offset += sizeof(InodeWatcherEvent::watch_descriptor);
  41. memcpy(bytes.offset(offset), &event.type, sizeof(InodeWatcherEvent::type));
  42. offset += sizeof(InodeWatcherEvent::type);
  43. if (event.path) {
  44. size_t name_length = event.path->length() + 1;
  45. memcpy(bytes.offset(offset), &name_length, sizeof(InodeWatcherEvent::name_length));
  46. offset += sizeof(InodeWatcherEvent::name_length);
  47. memcpy(bytes.offset(offset), event.path->characters(), name_length);
  48. } else {
  49. memset(bytes.offset(offset), 0, sizeof(InodeWatcherEvent::name_length));
  50. }
  51. return bytes.size();
  52. });
  53. evaluate_block_conditions();
  54. return result;
  55. }
  56. ErrorOr<void> InodeWatcher::close()
  57. {
  58. m_watch_maps.with([this](auto& watch_maps) {
  59. for (auto& entry : watch_maps.wd_to_watches) {
  60. auto& inode = const_cast<Inode&>(entry.value->inode);
  61. inode.unregister_watcher({}, *this);
  62. }
  63. watch_maps.inode_to_watches.clear();
  64. watch_maps.wd_to_watches.clear();
  65. });
  66. return {};
  67. }
  68. ErrorOr<NonnullOwnPtr<KString>> InodeWatcher::pseudo_path(OpenFileDescription const&) const
  69. {
  70. return m_watch_maps.with([](auto& watch_maps) -> ErrorOr<NonnullOwnPtr<KString>> {
  71. return KString::formatted("InodeWatcher:({})", watch_maps.wd_to_watches.size());
  72. });
  73. }
  74. void InodeWatcher::notify_inode_event(Badge<Inode>, InodeIdentifier inode_id, InodeWatcherEvent::Type event_type, StringView name)
  75. {
  76. m_watch_maps.with([this, inode_id, event_type, name](auto& watch_maps) {
  77. auto it = watch_maps.inode_to_watches.find(inode_id);
  78. if (it == watch_maps.inode_to_watches.end())
  79. return;
  80. auto& watcher = *it->value;
  81. if (!(watcher.event_mask & static_cast<unsigned>(event_type)))
  82. return;
  83. m_queue.with([watcher, event_type, name](auto& queue) {
  84. OwnPtr<KString> path;
  85. if (!name.is_null())
  86. path = KString::try_create(name).release_value_but_fixme_should_propagate_errors();
  87. queue.enqueue({ watcher.wd, event_type, move(path) });
  88. });
  89. });
  90. evaluate_block_conditions();
  91. }
  92. ErrorOr<int> InodeWatcher::register_inode(Inode& inode, unsigned event_mask)
  93. {
  94. return m_watch_maps.with([this, &inode, event_mask](auto& watch_maps) -> ErrorOr<int> {
  95. if (watch_maps.inode_to_watches.find(inode.identifier()) != watch_maps.inode_to_watches.end())
  96. return EEXIST;
  97. int wd = -1;
  98. do {
  99. wd = m_wd_counter.value();
  100. m_wd_counter++;
  101. if (m_wd_counter.has_overflow())
  102. m_wd_counter = 1;
  103. } while (watch_maps.wd_to_watches.find(wd) != watch_maps.wd_to_watches.end());
  104. auto description = TRY(WatchDescription::create(wd, inode, event_mask));
  105. TRY(watch_maps.inode_to_watches.try_set(inode.identifier(), description.ptr()));
  106. auto set_result = watch_maps.wd_to_watches.try_set(wd, move(description));
  107. if (set_result.is_error()) {
  108. watch_maps.inode_to_watches.remove(inode.identifier());
  109. return set_result.release_error();
  110. }
  111. auto register_result = inode.register_watcher({}, *this);
  112. if (register_result.is_error()) {
  113. watch_maps.inode_to_watches.remove(inode.identifier());
  114. watch_maps.wd_to_watches.remove(wd);
  115. return register_result.release_error();
  116. }
  117. return wd;
  118. });
  119. }
  120. ErrorOr<void> InodeWatcher::unregister_by_wd(int wd)
  121. {
  122. TRY(m_watch_maps.with([this, wd](auto& watch_maps) -> ErrorOr<void> {
  123. auto it = watch_maps.wd_to_watches.find(wd);
  124. if (it == watch_maps.wd_to_watches.end())
  125. return ENOENT;
  126. auto& inode = it->value->inode;
  127. inode.unregister_watcher({}, *this);
  128. watch_maps.inode_to_watches.remove(inode.identifier());
  129. watch_maps.wd_to_watches.remove(it);
  130. return {};
  131. }));
  132. return {};
  133. }
  134. void InodeWatcher::unregister_by_inode(Badge<Inode>, InodeIdentifier identifier)
  135. {
  136. m_watch_maps.with([identifier](auto& watch_maps) {
  137. auto it = watch_maps.inode_to_watches.find(identifier);
  138. if (it == watch_maps.inode_to_watches.end())
  139. return;
  140. // NOTE: no need to call unregister_watcher here, the Inode calls us.
  141. watch_maps.inode_to_watches.remove(identifier);
  142. watch_maps.wd_to_watches.remove(it->value->wd);
  143. });
  144. }
  145. }