readlink.cpp 1.0 KB

1234567891011121314151617181920212223242526272829303132
  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. ErrorOr<FlatPtr> Process::sys$readlink(Userspace<Syscall::SC_readlink_params const*> user_params)
  11. {
  12. VERIFY_NO_PROCESS_BIG_LOCK(this);
  13. TRY(require_promise(Pledge::rpath));
  14. auto params = TRY(copy_typed_from_user(user_params));
  15. auto path = TRY(get_syscall_path_argument(params.path));
  16. auto description = TRY(VirtualFileSystem::the().open(credentials(), path->view(), O_RDONLY | O_NOFOLLOW_NOERROR, 0, current_directory()));
  17. if (!description->metadata().is_symlink())
  18. return EINVAL;
  19. auto link_target = TRY(description->read_entire_file());
  20. auto size_to_copy = min(link_target->size(), params.buffer.size);
  21. TRY(copy_to_user(params.buffer.data, link_target->data(), size_to_copy));
  22. // Note: we return the whole size here, not the copied size.
  23. return link_target->size();
  24. }
  25. }