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
|
|
|
{
|
2022-08-17 21:03:04 +01:00
|
|
|
VERIFY_NO_PROCESS_BIG_LOCK(this);
|
2021-12-29 01:11:45 -08:00
|
|
|
TRY(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;
|
2022-04-01 20:58:27 +03:00
|
|
|
return hostname().with_shared([&](auto const& name) -> ErrorOr<FlatPtr> {
|
2022-01-13 00:27:33 +02:00
|
|
|
if (size < (name->length() + 1))
|
2021-07-18 15:00:48 +02:00
|
|
|
return ENAMETOOLONG;
|
2022-01-13 00:27:33 +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
|
|
|
}
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
ErrorOr<FlatPtr> Process::sys$sethostname(Userspace<char const*> buffer, size_t length)
|
2020-07-30 23:38:15 +02:00
|
|
|
{
|
2022-08-17 21:03:04 +01:00
|
|
|
VERIFY_NO_PROCESS_BIG_LOCK(this);
|
2021-12-29 01:11:45 -08:00
|
|
|
TRY(require_no_promises());
|
2021-12-29 00:16:27 -08:00
|
|
|
|
2022-08-20 18:21:01 -04:00
|
|
|
auto credentials = this->credentials();
|
|
|
|
if (!credentials->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));
|
2022-01-11 22:16:18 +02:00
|
|
|
hostname().with_exclusive([&](auto& name) {
|
2022-01-13 00:27:33 +02:00
|
|
|
name = move(new_name);
|
2021-07-18 15:00:48 +02:00
|
|
|
});
|
2022-01-11 22:16:18 +02:00
|
|
|
return 0;
|
2020-07-30 23:38:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|