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/RefPtr.h>
8#include <Kernel/FileSystem/Custody.h>
9#include <Kernel/FileSystem/VirtualFileSystem.h>
10#include <Kernel/Process.h>
11
12namespace Kernel {
13
14ErrorOr<FlatPtr> Process::sys$fstat(int fd, Userspace<stat*> user_statbuf)
15{
16 VERIFY_NO_PROCESS_BIG_LOCK(this);
17 TRY(require_promise(Pledge::stdio));
18 auto description = TRY(open_file_description(fd));
19 auto buffer = TRY(description->stat());
20 TRY(copy_to_user(user_statbuf, &buffer));
21 return 0;
22}
23
24ErrorOr<FlatPtr> Process::sys$stat(Userspace<Syscall::SC_stat_params const*> user_params)
25{
26 VERIFY_NO_PROCESS_BIG_LOCK(this);
27 TRY(require_promise(Pledge::rpath));
28 auto params = TRY(copy_typed_from_user(user_params));
29
30 auto path = TRY(get_syscall_path_argument(params.path));
31
32 RefPtr<Custody> base;
33 if (params.dirfd == AT_FDCWD) {
34 base = current_directory();
35 } else {
36 auto base_description = TRY(open_file_description(params.dirfd));
37 if (!base_description->is_directory())
38 return ENOTDIR;
39 if (!base_description->custody())
40 return EINVAL;
41 base = base_description->custody();
42 }
43 auto metadata = TRY(VirtualFileSystem::the().lookup_metadata(credentials(), path->view(), *base, params.follow_symlinks ? 0 : O_NOFOLLOW_NOERROR));
44 auto statbuf = TRY(metadata.stat());
45 TRY(copy_to_user(params.statbuf, &statbuf));
46 return 0;
47}
48
49}