StringVariable.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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/Variables/StringVariable.h>
  7. #include <Kernel/Sections.h>
  8. #include <Kernel/Tasks/Process.h>
  9. namespace Kernel {
  10. ErrorOr<void> SysFSSystemStringVariable::try_generate(KBufferBuilder& builder)
  11. {
  12. auto string_value = TRY(value());
  13. return builder.appendff("{}\n", string_value->view());
  14. }
  15. ErrorOr<size_t> SysFSSystemStringVariable::write_bytes(off_t, size_t count, UserOrKernelBuffer const& buffer, OpenFileDescription*)
  16. {
  17. MutexLocker locker(m_refresh_lock);
  18. // Note: We do all of this code before taking the spinlock because then we disable
  19. // interrupts so page faults will not work.
  20. char* value = nullptr;
  21. auto new_value = TRY(KString::try_create_uninitialized(count, value));
  22. TRY(buffer.read(value, count));
  23. auto new_value_without_possible_newlines = TRY(KString::try_create(new_value->view().trim("\n"sv)));
  24. // NOTE: If we are in a jail, don't let the current process to change the variable.
  25. if (Process::current().is_currently_in_jail())
  26. return Error::from_errno(EPERM);
  27. set_value(move(new_value_without_possible_newlines));
  28. return count;
  29. }
  30. ErrorOr<void> SysFSSystemStringVariable::truncate(u64 size)
  31. {
  32. if (size != 0)
  33. return EPERM;
  34. return {};
  35. }
  36. }