summaryrefslogtreecommitdiff
path: root/Kernel/Syscalls/hostname.cpp
blob: 1d841890877e1d99db27af0b6c00b7abcdbfba72 (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
/*
 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <Kernel/Process.h>

namespace Kernel {

ErrorOr<FlatPtr> Process::sys$gethostname(Userspace<char*> buffer, size_t size)
{
    VERIFY_NO_PROCESS_BIG_LOCK(this)
    TRY(require_promise(Pledge::stdio));
    if (size > NumericLimits<ssize_t>::max())
        return EINVAL;
    return hostname().with_shared([&](const auto& name) -> ErrorOr<FlatPtr> {
        if (size < (name.length() + 1))
            return ENAMETOOLONG;
        TRY(copy_to_user(buffer, name.characters(), name.length() + 1));
        return 0;
    });
}

ErrorOr<FlatPtr> Process::sys$sethostname(Userspace<const char*> buffer, size_t length)
{
    VERIFY_NO_PROCESS_BIG_LOCK(this)
    TRY(require_no_promises());

    if (!is_superuser())
        return EPERM;
    if (length > 64)
        return ENAMETOOLONG;
    auto new_name = TRY(try_copy_kstring_from_user(buffer, length));
    return hostname().with_exclusive([&](auto& name) -> ErrorOr<FlatPtr> {
        // FIXME: Use KString instead of String here.
        name = new_name->view();
        return 0;
    });
}

}