Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 * Copyright (c) 2022, Daniel Bertalan <dani@danielbertalan.dev>
4 *
5 * SPDX-License-Identifier: BSD-2-Clause
6 */
7
8#include <AK/StringView.h>
9#include <Kernel/FileSystem/VirtualFileSystem.h>
10#include <Kernel/Process.h>
11
12namespace Kernel {
13
14ErrorOr<FlatPtr> Process::sys$chmod(Userspace<Syscall::SC_chmod_params const*> user_params)
15{
16 VERIFY_NO_PROCESS_BIG_LOCK(this);
17 TRY(require_promise(Pledge::fattr));
18 auto params = TRY(copy_typed_from_user(user_params));
19 auto path = TRY(get_syscall_path_argument(params.path));
20
21 RefPtr<Custody> base;
22 if (params.dirfd == AT_FDCWD) {
23 base = current_directory();
24 } else {
25 auto base_description = TRY(open_file_description(params.dirfd));
26 if (!base_description->custody())
27 return EINVAL;
28 base = base_description->custody();
29 }
30
31 TRY(VirtualFileSystem::the().chmod(credentials(), path->view(), params.mode, *base, params.follow_symlinks ? 0 : O_NOFOLLOW_NOERROR));
32 return 0;
33}
34
35ErrorOr<FlatPtr> Process::sys$fchmod(int fd, mode_t mode)
36{
37 VERIFY_NO_PROCESS_BIG_LOCK(this);
38 TRY(require_promise(Pledge::fattr));
39 auto description = TRY(open_file_description(fd));
40 TRY(description->chmod(credentials(), mode));
41 return 0;
42}
43
44}