Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#include <AK/StringView.h>
8#include <Kernel/FileSystem/VirtualFileSystem.h>
9#include <Kernel/Process.h>
10
11namespace Kernel {
12
13ErrorOr<FlatPtr> Process::sys$readlink(Userspace<Syscall::SC_readlink_params const*> user_params)
14{
15 VERIFY_NO_PROCESS_BIG_LOCK(this);
16 TRY(require_promise(Pledge::rpath));
17 auto params = TRY(copy_typed_from_user(user_params));
18
19 auto path = TRY(get_syscall_path_argument(params.path));
20 auto description = TRY(VirtualFileSystem::the().open(credentials(), path->view(), O_RDONLY | O_NOFOLLOW_NOERROR, 0, TRY(custody_for_dirfd(params.dirfd))));
21
22 if (!description->metadata().is_symlink())
23 return EINVAL;
24
25 auto link_target = TRY(description->read_entire_file());
26 auto size_to_copy = min(link_target->size(), params.buffer.size);
27 TRY(copy_to_user(params.buffer.data, link_target->data(), size_to_copy));
28 // Note: we return the whole size here, not the copied size.
29 return link_target->size();
30}
31
32}