mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 17:47:44 +00:00
Kernel: Migrate hostname locking to ProtectedValue
This commit is contained in:
parent
9517100672
commit
626b99ce1c
4 changed files with 32 additions and 28 deletions
|
@ -4,6 +4,7 @@
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <AK/Singleton.h>
|
||||||
#include <AK/StdLibExtras.h>
|
#include <AK/StdLibExtras.h>
|
||||||
#include <AK/StringBuilder.h>
|
#include <AK/StringBuilder.h>
|
||||||
#include <AK/Time.h>
|
#include <AK/Time.h>
|
||||||
|
@ -45,11 +46,16 @@ static void create_signal_trampoline();
|
||||||
RecursiveSpinLock g_processes_lock;
|
RecursiveSpinLock g_processes_lock;
|
||||||
static Atomic<pid_t> next_pid;
|
static Atomic<pid_t> next_pid;
|
||||||
READONLY_AFTER_INIT Process::List* g_processes;
|
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 HashMap<String, OwnPtr<Module>>* g_modules;
|
||||||
READONLY_AFTER_INIT Memory::Region* g_signal_trampoline_region;
|
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()
|
ProcessID Process::allocate_pid()
|
||||||
{
|
{
|
||||||
// Overflow is UB, and negative PIDs wreck havoc.
|
// 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);
|
next_pid.store(0, AK::MemoryOrder::memory_order_release);
|
||||||
g_processes = new Process::List();
|
g_processes = new Process::List();
|
||||||
g_process_groups = new ProcessGroup::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();
|
create_signal_trampoline();
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,6 +22,7 @@
|
||||||
#include <Kernel/Forward.h>
|
#include <Kernel/Forward.h>
|
||||||
#include <Kernel/FutexQueue.h>
|
#include <Kernel/FutexQueue.h>
|
||||||
#include <Kernel/Locking/Mutex.h>
|
#include <Kernel/Locking/Mutex.h>
|
||||||
|
#include <Kernel/Locking/ProtectedValue.h>
|
||||||
#include <Kernel/Memory/AddressSpace.h>
|
#include <Kernel/Memory/AddressSpace.h>
|
||||||
#include <Kernel/PerformanceEventBuffer.h>
|
#include <Kernel/PerformanceEventBuffer.h>
|
||||||
#include <Kernel/ProcessGroup.h>
|
#include <Kernel/ProcessGroup.h>
|
||||||
|
@ -33,6 +34,7 @@
|
||||||
|
|
||||||
namespace Kernel {
|
namespace Kernel {
|
||||||
|
|
||||||
|
ProtectedValue<String>& hostname();
|
||||||
Time kgettimeofday();
|
Time kgettimeofday();
|
||||||
|
|
||||||
#define ENUMERATE_PLEDGE_PROMISES \
|
#define ENUMERATE_PLEDGE_PROMISES \
|
||||||
|
|
|
@ -8,37 +8,36 @@
|
||||||
|
|
||||||
namespace Kernel {
|
namespace Kernel {
|
||||||
|
|
||||||
extern String* g_hostname;
|
|
||||||
extern Mutex* g_hostname_lock;
|
|
||||||
|
|
||||||
KResultOr<FlatPtr> Process::sys$gethostname(Userspace<char*> buffer, size_t size)
|
KResultOr<FlatPtr> Process::sys$gethostname(Userspace<char*> buffer, size_t size)
|
||||||
{
|
{
|
||||||
VERIFY_NO_PROCESS_BIG_LOCK(this)
|
VERIFY_NO_PROCESS_BIG_LOCK(this)
|
||||||
REQUIRE_PROMISE(stdio);
|
REQUIRE_PROMISE(stdio);
|
||||||
if (size > NumericLimits<ssize_t>::max())
|
if (size > NumericLimits<ssize_t>::max())
|
||||||
return EINVAL;
|
return EINVAL;
|
||||||
MutexLocker locker(*g_hostname_lock, Mutex::Mode::Shared);
|
return hostname().with_shared([&](const auto& name) -> KResultOr<FlatPtr> {
|
||||||
if (size < (g_hostname->length() + 1))
|
if (size < (name.length() + 1))
|
||||||
return ENAMETOOLONG;
|
return ENAMETOOLONG;
|
||||||
if (!copy_to_user(buffer, g_hostname->characters(), g_hostname->length() + 1))
|
if (!copy_to_user(buffer, name.characters(), name.length() + 1))
|
||||||
return EFAULT;
|
return EFAULT;
|
||||||
return 0;
|
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)
|
VERIFY_NO_PROCESS_BIG_LOCK(this)
|
||||||
REQUIRE_NO_PROMISES;
|
REQUIRE_NO_PROMISES;
|
||||||
if (!is_superuser())
|
if (!is_superuser())
|
||||||
return EPERM;
|
return EPERM;
|
||||||
MutexLocker locker(*g_hostname_lock, Mutex::Mode::Exclusive);
|
|
||||||
if (length > 64)
|
if (length > 64)
|
||||||
return ENAMETOOLONG;
|
return ENAMETOOLONG;
|
||||||
auto copied_hostname = copy_string_from_user(hostname, length);
|
return hostname().with_exclusive([&](auto& name) -> KResultOr<FlatPtr> {
|
||||||
if (copied_hostname.is_null())
|
auto copied_hostname = copy_string_from_user(buffer, length);
|
||||||
return EFAULT;
|
if (copied_hostname.is_null())
|
||||||
*g_hostname = move(copied_hostname);
|
return EFAULT;
|
||||||
return 0;
|
name = move(copied_hostname);
|
||||||
|
return 0;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,15 +11,8 @@ namespace Kernel {
|
||||||
KResultOr<FlatPtr> Process::sys$uname(Userspace<utsname*> user_buf)
|
KResultOr<FlatPtr> Process::sys$uname(Userspace<utsname*> user_buf)
|
||||||
{
|
{
|
||||||
VERIFY_NO_PROCESS_BIG_LOCK(this)
|
VERIFY_NO_PROCESS_BIG_LOCK(this)
|
||||||
extern String* g_hostname;
|
|
||||||
extern Mutex* g_hostname_lock;
|
|
||||||
|
|
||||||
REQUIRE_PROMISE(stdio);
|
REQUIRE_PROMISE(stdio);
|
||||||
|
|
||||||
MutexLocker locker(*g_hostname_lock, Mutex::Mode::Shared);
|
|
||||||
if (g_hostname->length() + 1 > sizeof(utsname::nodename))
|
|
||||||
return ENAMETOOLONG;
|
|
||||||
|
|
||||||
utsname buf {};
|
utsname buf {};
|
||||||
memcpy(buf.sysname, "SerenityOS", 11);
|
memcpy(buf.sysname, "SerenityOS", 11);
|
||||||
memcpy(buf.release, "1.0-dev", 8);
|
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);
|
memcpy(buf.machine, "x86_64", 7);
|
||||||
#endif
|
#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))
|
if (!copy_to_user(user_buf, &buf))
|
||||||
return EFAULT;
|
return EFAULT;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue