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