diff --git a/Kernel/Process.cpp b/Kernel/Process.cpp index f9dd0d80c5..d27be21616 100644 --- a/Kernel/Process.cpp +++ b/Kernel/Process.cpp @@ -4,6 +4,7 @@ * SPDX-License-Identifier: BSD-2-Clause */ +#include #include #include #include @@ -45,11 +46,16 @@ static void create_signal_trampoline(); RecursiveSpinLock g_processes_lock; static Atomic next_pid; READONLY_AFTER_INIT Process::List* g_processes; -READONLY_AFTER_INIT String* g_hostname; -READONLY_AFTER_INIT Mutex* g_hostname_lock; READONLY_AFTER_INIT HashMap>* g_modules; READONLY_AFTER_INIT Memory::Region* g_signal_trampoline_region; +static AK::Singleton> s_hostname; + +ProtectedValue& hostname() +{ + return *s_hostname; +} + ProcessID Process::allocate_pid() { // Overflow is UB, and negative PIDs wreck havoc. @@ -67,8 +73,10 @@ UNMAP_AFTER_INIT void Process::initialize() next_pid.store(0, AK::MemoryOrder::memory_order_release); g_processes = new Process::List(); g_process_groups = new ProcessGroup::List(); - g_hostname = new String("courage"); - g_hostname_lock = new Mutex; + + hostname().with_exclusive([&](auto& name) { + name = "courage"; + }); create_signal_trampoline(); } diff --git a/Kernel/Process.h b/Kernel/Process.h index cc3f75f5f1..d9e8220cf6 100644 --- a/Kernel/Process.h +++ b/Kernel/Process.h @@ -22,6 +22,7 @@ #include #include #include +#include #include #include #include @@ -33,6 +34,7 @@ namespace Kernel { +ProtectedValue& hostname(); Time kgettimeofday(); #define ENUMERATE_PLEDGE_PROMISES \ 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 Process::sys$gethostname(Userspace buffer, size_t size) { VERIFY_NO_PROCESS_BIG_LOCK(this) REQUIRE_PROMISE(stdio); if (size > NumericLimits::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 { + if (size < (name.length() + 1)) + return ENAMETOOLONG; + if (!copy_to_user(buffer, name.characters(), name.length() + 1)) + return EFAULT; + return 0; + }); } -KResultOr Process::sys$sethostname(Userspace hostname, size_t length) +KResultOr Process::sys$sethostname(Userspace 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 { + auto copied_hostname = copy_string_from_user(buffer, length); + if (copied_hostname.is_null()) + return EFAULT; + name = move(copied_hostname); + return 0; + }); } } diff --git a/Kernel/Syscalls/uname.cpp b/Kernel/Syscalls/uname.cpp index 6519d96080..a8450d5654 100644 --- a/Kernel/Syscalls/uname.cpp +++ b/Kernel/Syscalls/uname.cpp @@ -11,15 +11,8 @@ namespace Kernel { KResultOr Process::sys$uname(Userspace user_buf) { VERIFY_NO_PROCESS_BIG_LOCK(this) - extern String* g_hostname; - extern Mutex* g_hostname_lock; - REQUIRE_PROMISE(stdio); - MutexLocker locker(*g_hostname_lock, Mutex::Mode::Shared); - if (g_hostname->length() + 1 > sizeof(utsname::nodename)) - return ENAMETOOLONG; - utsname buf {}; memcpy(buf.sysname, "SerenityOS", 11); memcpy(buf.release, "1.0-dev", 8); @@ -30,7 +23,9 @@ KResultOr Process::sys$uname(Userspace user_buf) memcpy(buf.machine, "x86_64", 7); #endif - memcpy(buf.nodename, g_hostname->characters(), g_hostname->length() + 1); + hostname().with_shared([&](const auto& name) { + memcpy(buf.nodename, name.characters(), name.length() + 1); + }); if (!copy_to_user(user_buf, &buf)) return EFAULT;