GlobalInformation.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * Copyright (c) 2022, Liav A. <liavalb@hotmail.co.il>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <Kernel/FileSystem/SysFS/Subsystems/Kernel/GlobalInformation.h>
  7. #include <Kernel/Process.h>
  8. namespace Kernel {
  9. ErrorOr<size_t> SysFSGlobalInformation::read_bytes(off_t offset, size_t count, UserOrKernelBuffer& buffer, OpenFileDescription* description) const
  10. {
  11. dbgln_if(SYSFS_DEBUG, "SysFSGlobalInformation @ {}: read_bytes offset: {} count: {}", name(), offset, count);
  12. VERIFY(offset >= 0);
  13. VERIFY(buffer.user_or_kernel_ptr());
  14. if (!description)
  15. return Error::from_errno(EIO);
  16. MutexLocker locker(m_refresh_lock);
  17. if (!description->data()) {
  18. dbgln("SysFSGlobalInformation: Do not have cached data!");
  19. return Error::from_errno(EIO);
  20. }
  21. auto& typed_cached_data = static_cast<SysFSInodeData&>(*description->data());
  22. auto& data_buffer = typed_cached_data.buffer;
  23. if (!data_buffer || (size_t)offset >= data_buffer->size())
  24. return 0;
  25. ssize_t nread = min(static_cast<off_t>(data_buffer->size() - offset), static_cast<off_t>(count));
  26. TRY(buffer.write(data_buffer->data() + offset, nread));
  27. return nread;
  28. }
  29. SysFSGlobalInformation::SysFSGlobalInformation(SysFSDirectory const& parent_directory)
  30. : SysFSComponent(parent_directory)
  31. {
  32. }
  33. ErrorOr<void> SysFSGlobalInformation::refresh_data(OpenFileDescription& description) const
  34. {
  35. MutexLocker lock(m_refresh_lock);
  36. auto& cached_data = description.data();
  37. if (!cached_data) {
  38. cached_data = adopt_own_if_nonnull(new (nothrow) SysFSInodeData);
  39. if (!cached_data)
  40. return ENOMEM;
  41. }
  42. auto builder = TRY(KBufferBuilder::try_create());
  43. TRY(Process::current().jail().with([&](auto& my_jail) -> ErrorOr<void> {
  44. if (my_jail && !is_readable_by_jailed_processes())
  45. return Error::from_errno(EPERM);
  46. TRY(const_cast<SysFSGlobalInformation&>(*this).try_generate(builder));
  47. return {};
  48. }));
  49. auto& typed_cached_data = static_cast<SysFSInodeData&>(*cached_data);
  50. typed_cached_data.buffer = builder.build();
  51. if (!typed_cached_data.buffer)
  52. return ENOMEM;
  53. return {};
  54. }
  55. }