2020-07-30 21:38:15 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-07-30 21:38:15 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <Kernel/Process.h>
|
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
2021-11-07 23:51:39 +00:00
|
|
|
ErrorOr<FlatPtr> Process::sys$gethostname(Userspace<char*> buffer, size_t size)
|
2020-07-30 21:38:15 +00:00
|
|
|
{
|
2021-07-18 18:27:13 +00:00
|
|
|
VERIFY_NO_PROCESS_BIG_LOCK(this)
|
2021-12-29 09:11:45 +00:00
|
|
|
TRY(require_promise(Pledge::stdio));
|
2021-06-16 14:44:15 +00:00
|
|
|
if (size > NumericLimits<ssize_t>::max())
|
2021-03-01 12:49:16 +00:00
|
|
|
return EINVAL;
|
2021-11-07 23:51:39 +00:00
|
|
|
return hostname().with_shared([&](const auto& name) -> ErrorOr<FlatPtr> {
|
2022-01-12 22:27:33 +00:00
|
|
|
if (size < (name->length() + 1))
|
2021-07-18 13:00:48 +00:00
|
|
|
return ENAMETOOLONG;
|
2022-01-12 22:27:33 +00:00
|
|
|
TRY(copy_to_user(buffer, name->characters(), name->length() + 1));
|
2021-07-18 13:00:48 +00:00
|
|
|
return 0;
|
|
|
|
});
|
2020-07-30 21:38:15 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 23:51:39 +00:00
|
|
|
ErrorOr<FlatPtr> Process::sys$sethostname(Userspace<const char*> buffer, size_t length)
|
2020-07-30 21:38:15 +00:00
|
|
|
{
|
2021-07-18 18:27:13 +00:00
|
|
|
VERIFY_NO_PROCESS_BIG_LOCK(this)
|
2021-12-29 09:11:45 +00:00
|
|
|
TRY(require_no_promises());
|
2021-12-29 08:16:27 +00:00
|
|
|
|
2020-07-30 21:38:15 +00:00
|
|
|
if (!is_superuser())
|
2021-03-01 12:49:16 +00:00
|
|
|
return EPERM;
|
2020-07-30 21:38:15 +00:00
|
|
|
if (length > 64)
|
2021-03-01 12:49:16 +00:00
|
|
|
return ENAMETOOLONG;
|
2021-09-05 16:22:18 +00:00
|
|
|
auto new_name = TRY(try_copy_kstring_from_user(buffer, length));
|
2022-01-11 20:16:18 +00:00
|
|
|
hostname().with_exclusive([&](auto& name) {
|
2022-01-12 22:27:33 +00:00
|
|
|
name = move(new_name);
|
2021-07-18 13:00:48 +00:00
|
|
|
});
|
2022-01-11 20:16:18 +00:00
|
|
|
return 0;
|
2020-07-30 21:38:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|