2020-07-31 00:38:15 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-07-31 00:38:15 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <Kernel/Process.h>
|
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
|
|
|
extern String* g_hostname;
|
2021-07-17 22:09:51 +03:00
|
|
|
extern Mutex* g_hostname_lock;
|
2020-07-31 00:38:15 +03:00
|
|
|
|
2021-06-28 21:59:35 +03:00
|
|
|
KResultOr<FlatPtr> Process::sys$gethostname(Userspace<char*> buffer, size_t size)
|
2020-07-31 00:38:15 +03:00
|
|
|
{
|
|
|
|
REQUIRE_PROMISE(stdio);
|
2021-06-16 17:44:15 +03:00
|
|
|
if (size > NumericLimits<ssize_t>::max())
|
2021-03-01 15:49:16 +03:00
|
|
|
return EINVAL;
|
2021-07-17 22:09:51 +03:00
|
|
|
Locker locker(*g_hostname_lock, Mutex::Mode::Shared);
|
2021-06-17 12:15:55 +03:00
|
|
|
if (size < (g_hostname->length() + 1))
|
2021-03-01 15:49:16 +03:00
|
|
|
return ENAMETOOLONG;
|
2020-09-12 06:11:07 +03:00
|
|
|
if (!copy_to_user(buffer, g_hostname->characters(), g_hostname->length() + 1))
|
2021-03-01 15:49:16 +03:00
|
|
|
return EFAULT;
|
2020-07-31 00:38:15 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-06-28 21:59:35 +03:00
|
|
|
KResultOr<FlatPtr> Process::sys$sethostname(Userspace<const char*> hostname, size_t length)
|
2020-07-31 00:38:15 +03:00
|
|
|
{
|
|
|
|
REQUIRE_NO_PROMISES;
|
|
|
|
if (!is_superuser())
|
2021-03-01 15:49:16 +03:00
|
|
|
return EPERM;
|
2021-07-17 22:09:51 +03:00
|
|
|
Locker locker(*g_hostname_lock, Mutex::Mode::Exclusive);
|
2020-07-31 00:38:15 +03:00
|
|
|
if (length > 64)
|
2021-03-01 15:49:16 +03:00
|
|
|
return ENAMETOOLONG;
|
2020-09-12 06:11:07 +03:00
|
|
|
auto copied_hostname = copy_string_from_user(hostname, length);
|
|
|
|
if (copied_hostname.is_null())
|
2021-03-01 15:49:16 +03:00
|
|
|
return EFAULT;
|
2020-09-12 06:11:07 +03:00
|
|
|
*g_hostname = move(copied_hostname);
|
2020-07-31 00:38:15 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|