summaryrefslogtreecommitdiff
path: root/Kernel/Syscalls/hostname.cpp
diff options
context:
space:
mode:
authorJean-Baptiste Boric <jblbeurope@gmail.com>2021-07-18 15:00:48 +0200
committerAndreas Kling <kling@serenityos.org>2021-08-07 11:48:00 +0200
commit626b99ce1c44353f1f8dbc74a488fa003812ad20 (patch)
tree4558dd2f17e4ae1583314dac5f6f8bd3a5956a31 /Kernel/Syscalls/hostname.cpp
parent9517100672bc01867e40b64753a429b9e7bff054 (diff)
downloadserenity-626b99ce1c44353f1f8dbc74a488fa003812ad20.zip
Kernel: Migrate hostname locking to ProtectedValue
Diffstat (limited to 'Kernel/Syscalls/hostname.cpp')
-rw-r--r--Kernel/Syscalls/hostname.cpp31
1 files changed, 15 insertions, 16 deletions
diff --git a/Kernel/Syscalls/hostname.cpp b/Kernel/Syscalls/hostname.cpp
index 2d9376ddbc..19ca6a473c 100644
--- a/Kernel/Syscalls/hostname.cpp
+++ b/Kernel/Syscalls/hostname.cpp
@@ -8,37 +8,36 @@
namespace Kernel {
-extern String* g_hostname;
-extern Mutex* g_hostname_lock;
-
KResultOr<FlatPtr> Process::sys$gethostname(Userspace<char*> buffer, size_t size)
{
VERIFY_NO_PROCESS_BIG_LOCK(this)
REQUIRE_PROMISE(stdio);
if (size > NumericLimits<ssize_t>::max())
return EINVAL;
- MutexLocker locker(*g_hostname_lock, Mutex::Mode::Shared);
- if (size < (g_hostname->length() + 1))
- return ENAMETOOLONG;
- if (!copy_to_user(buffer, g_hostname->characters(), g_hostname->length() + 1))
- return EFAULT;
- return 0;
+ return hostname().with_shared([&](const auto& name) -> KResultOr<FlatPtr> {
+ if (size < (name.length() + 1))
+ return ENAMETOOLONG;
+ if (!copy_to_user(buffer, name.characters(), name.length() + 1))
+ return EFAULT;
+ return 0;
+ });
}
-KResultOr<FlatPtr> Process::sys$sethostname(Userspace<const char*> hostname, size_t length)
+KResultOr<FlatPtr> Process::sys$sethostname(Userspace<const char*> buffer, size_t length)
{
VERIFY_NO_PROCESS_BIG_LOCK(this)
REQUIRE_NO_PROMISES;
if (!is_superuser())
return EPERM;
- MutexLocker locker(*g_hostname_lock, Mutex::Mode::Exclusive);
if (length > 64)
return ENAMETOOLONG;
- auto copied_hostname = copy_string_from_user(hostname, length);
- if (copied_hostname.is_null())
- return EFAULT;
- *g_hostname = move(copied_hostname);
- return 0;
+ return hostname().with_exclusive([&](auto& name) -> KResultOr<FlatPtr> {
+ auto copied_hostname = copy_string_from_user(buffer, length);
+ if (copied_hostname.is_null())
+ return EFAULT;
+ name = move(copied_hostname);
+ return 0;
+ });
}
}