summaryrefslogtreecommitdiff
path: root/Kernel/Syscalls/utimensat.cpp
blob: 643eeaba642aed29e2606d16a1b4a5b389f0b78f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
 * Copyright (c) 2022, Ariel Don <ariel@arieldon.com>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <AK/Assertions.h>
#include <AK/StringView.h>
#include <Kernel/FileSystem/VirtualFileSystem.h>
#include <Kernel/KLexicalPath.h>
#include <Kernel/Tasks/Process.h>

namespace Kernel {

ErrorOr<FlatPtr> Process::sys$futimens(Userspace<Syscall::SC_futimens_params const*> user_params)
{
    VERIFY_NO_PROCESS_BIG_LOCK(this);
    TRY(require_promise(Pledge::fattr));

    auto params = TRY(copy_typed_from_user(user_params));
    auto now = kgettimeofday().to_timespec();

    timespec times[2];
    if (params.times) {
        TRY(copy_from_user(times, params.times, sizeof(times)));
        if (times[0].tv_nsec == UTIME_NOW)
            times[0] = now;
        if (times[1].tv_nsec == UTIME_NOW)
            times[1] = now;
    } else {
        // According to POSIX, both access and modification times are set to
        // the current time given a nullptr.
        times[0] = now;
        times[1] = now;
    }

    auto description = TRY(open_file_description(params.fd));
    if (!description->inode())
        return EBADF;
    if (!description->custody())
        return EBADF;

    auto& atime = times[0];
    auto& mtime = times[1];
    TRY(VirtualFileSystem::the().do_utimens(credentials(), *description->custody(), atime, mtime));
    return 0;
}

ErrorOr<FlatPtr> Process::sys$utimensat(Userspace<Syscall::SC_utimensat_params const*> user_params)
{
    VERIFY_NO_PROCESS_BIG_LOCK(this);
    TRY(require_promise(Pledge::fattr));

    auto params = TRY(copy_typed_from_user(user_params));
    auto now = kgettimeofday().to_timespec();
    int follow_symlink = params.flag & AT_SYMLINK_NOFOLLOW ? O_NOFOLLOW_NOERROR : 0;

    timespec times[2];
    if (params.times) {
        TRY(copy_from_user(times, params.times, sizeof(times)));
        if (times[0].tv_nsec == UTIME_NOW)
            times[0] = now;
        if (times[1].tv_nsec == UTIME_NOW)
            times[1] = now;
    } else {
        // According to POSIX, both access and modification times are set to
        // the current time given a nullptr.
        times[0] = now;
        times[1] = now;
    }

    auto path = TRY(get_syscall_path_argument(params.path));
    auto base = TRY(custody_for_dirfd(params.dirfd));
    auto& atime = times[0];
    auto& mtime = times[1];
    TRY(VirtualFileSystem::the().utimensat(credentials(), path->view(), *base, atime, mtime, follow_symlink));
    return 0;
}

}