readlink.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/StringView.h>
  7. #include <Kernel/FileSystem/VirtualFileSystem.h>
  8. #include <Kernel/Process.h>
  9. namespace Kernel {
  10. KResultOr<FlatPtr> Process::sys$readlink(Userspace<const Syscall::SC_readlink_params*> user_params)
  11. {
  12. VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
  13. REQUIRE_PROMISE(rpath);
  14. Syscall::SC_readlink_params params;
  15. if (!copy_from_user(&params, user_params))
  16. return EFAULT;
  17. auto path = get_syscall_path_argument(params.path);
  18. if (path.is_error())
  19. return path.error();
  20. auto result = VirtualFileSystem::the().open(path.value()->view(), O_RDONLY | O_NOFOLLOW_NOERROR, 0, current_directory());
  21. if (result.is_error())
  22. return result.error();
  23. auto description = result.value();
  24. if (!description->metadata().is_symlink())
  25. return EINVAL;
  26. auto contents = description->read_entire_file();
  27. if (contents.is_error())
  28. return contents.error();
  29. auto& link_target = *contents.value();
  30. auto size_to_copy = min(link_target.size(), params.buffer.size);
  31. if (!copy_to_user(params.buffer.data, link_target.data(), size_to_copy))
  32. return EFAULT;
  33. // Note: we return the whole size here, not the copied size.
  34. return link_target.size();
  35. }
  36. }