Kernel: Migrate hostname locking to ProtectedValue
This commit is contained in:
parent
9517100672
commit
626b99ce1c
Notes:
sideshowbarker
2024-07-18 07:20:07 +09:00
Author: https://github.com/boricj Commit: https://github.com/SerenityOS/serenity/commit/626b99ce1c4 Pull-request: https://github.com/SerenityOS/serenity/pull/8851 Reviewed-by: https://github.com/awesomekling ✅ Reviewed-by: https://github.com/bgianfo
4 changed files with 32 additions and 28 deletions
|
@ -4,6 +4,7 @@
|
|||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/Singleton.h>
|
||||
#include <AK/StdLibExtras.h>
|
||||
#include <AK/StringBuilder.h>
|
||||
#include <AK/Time.h>
|
||||
|
@ -45,11 +46,16 @@ static void create_signal_trampoline();
|
|||
RecursiveSpinLock g_processes_lock;
|
||||
static Atomic<pid_t> 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<String, OwnPtr<Module>>* g_modules;
|
||||
READONLY_AFTER_INIT Memory::Region* g_signal_trampoline_region;
|
||||
|
||||
static AK::Singleton<ProtectedValue<String>> s_hostname;
|
||||
|
||||
ProtectedValue<String>& 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();
|
||||
}
|
||||
|
|
|
@ -22,6 +22,7 @@
|
|||
#include <Kernel/Forward.h>
|
||||
#include <Kernel/FutexQueue.h>
|
||||
#include <Kernel/Locking/Mutex.h>
|
||||
#include <Kernel/Locking/ProtectedValue.h>
|
||||
#include <Kernel/Memory/AddressSpace.h>
|
||||
#include <Kernel/PerformanceEventBuffer.h>
|
||||
#include <Kernel/ProcessGroup.h>
|
||||
|
@ -33,6 +34,7 @@
|
|||
|
||||
namespace Kernel {
|
||||
|
||||
ProtectedValue<String>& hostname();
|
||||
Time kgettimeofday();
|
||||
|
||||
#define ENUMERATE_PLEDGE_PROMISES \
|
||||
|
|
|
@ -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;
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -11,15 +11,8 @@ namespace Kernel {
|
|||
KResultOr<FlatPtr> Process::sys$uname(Userspace<utsname*> 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<FlatPtr> Process::sys$uname(Userspace<utsname*> 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;
|
||||
|
|
Loading…
Add table
Reference in a new issue