1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-24 22:17:42 +00:00

Kernel: Store process names as KString

This commit is contained in:
Andreas Kling 2021-09-07 12:53:28 +02:00
parent db2e67fd53
commit 55b0b06897
15 changed files with 46 additions and 32 deletions

View file

@ -472,7 +472,11 @@ void UHCIController::spawn_port_proc()
{ {
RefPtr<Thread> usb_hotplug_thread; RefPtr<Thread> usb_hotplug_thread;
Process::create_kernel_process(usb_hotplug_thread, "UHCIHotplug", [&] { auto process_name = KString::try_create("UHCI hotplug");
if (process_name.is_error())
TODO();
Process::create_kernel_process(usb_hotplug_thread, process_name.release_value(), [&] {
for (;;) { for (;;) {
if (m_root_hub) if (m_root_hub)
m_root_hub->check_for_port_updates(); m_root_hub->check_for_port_updates();

View file

@ -655,7 +655,10 @@ void Plan9FS::ensure_thread()
{ {
SpinlockLocker lock(m_thread_lock); SpinlockLocker lock(m_thread_lock);
if (!m_thread_running.exchange(true, AK::MemoryOrder::memory_order_acq_rel)) { if (!m_thread_running.exchange(true, AK::MemoryOrder::memory_order_acq_rel)) {
Process::create_kernel_process(m_thread, "Plan9FS", [&]() { auto process_name = KString::try_create("Plan9FS");
if (process_name.is_error())
TODO();
Process::create_kernel_process(m_thread, process_name.release_value(), [&]() {
thread_main(); thread_main();
m_thread_running.store(false, AK::MemoryOrder::memory_order_release); m_thread_running.store(false, AK::MemoryOrder::memory_order_release);
}); });

View file

@ -43,7 +43,10 @@ static HashTable<RefPtr<TCPSocket>>* delayed_ack_sockets;
void NetworkTask::spawn() void NetworkTask::spawn()
{ {
RefPtr<Thread> thread; RefPtr<Thread> thread;
Process::create_kernel_process(thread, "NetworkTask", NetworkTask_main, nullptr); auto name = KString::try_create("NetworkTask");
if (name.is_error())
TODO();
Process::create_kernel_process(thread, name.release_value(), NetworkTask_main, nullptr);
network_task = thread; network_task = thread;
} }

View file

@ -145,12 +145,13 @@ void Process::register_new(Process& process)
KResultOr<NonnullRefPtr<Process>> Process::try_create_user_process(RefPtr<Thread>& first_thread, String const& path, UserID uid, GroupID gid, Vector<String> arguments, Vector<String> environment, TTY* tty) KResultOr<NonnullRefPtr<Process>> Process::try_create_user_process(RefPtr<Thread>& first_thread, String const& path, UserID uid, GroupID gid, Vector<String> arguments, Vector<String> environment, TTY* tty)
{ {
auto parts = path.split('/'); auto parts = path.split_view('/');
if (arguments.is_empty()) { if (arguments.is_empty()) {
arguments.append(parts.last()); arguments.append(parts.last());
} }
auto process = TRY(Process::try_create(first_thread, parts.take_last(), uid, gid, ProcessID(0), false, VirtualFileSystem::the().root_custody(), nullptr, tty)); auto name = TRY(KString::try_create(parts.last()));
auto process = TRY(Process::try_create(first_thread, move(name), uid, gid, ProcessID(0), false, VirtualFileSystem::the().root_custody(), nullptr, tty));
if (!process->m_fds.try_resize(process->m_fds.max_open())) { if (!process->m_fds.try_resize(process->m_fds.max_open())) {
first_thread = nullptr; first_thread = nullptr;
@ -180,7 +181,7 @@ KResultOr<NonnullRefPtr<Process>> Process::try_create_user_process(RefPtr<Thread
return process; return process;
} }
RefPtr<Process> Process::create_kernel_process(RefPtr<Thread>& first_thread, String&& name, void (*entry)(void*), void* entry_data, u32 affinity, RegisterProcess do_register) RefPtr<Process> Process::create_kernel_process(RefPtr<Thread>& first_thread, NonnullOwnPtr<KString> name, void (*entry)(void*), void* entry_data, u32 affinity, RegisterProcess do_register)
{ {
auto process_or_error = Process::try_create(first_thread, move(name), UserID(0), GroupID(0), ProcessID(0), true); auto process_or_error = Process::try_create(first_thread, move(name), UserID(0), GroupID(0), ProcessID(0), true);
if (process_or_error.is_error()) if (process_or_error.is_error())
@ -217,15 +218,15 @@ void Process::unprotect_data()
}); });
} }
KResultOr<NonnullRefPtr<Process>> Process::try_create(RefPtr<Thread>& first_thread, String const& name, UserID uid, GroupID gid, ProcessID ppid, bool is_kernel_process, RefPtr<Custody> cwd, RefPtr<Custody> executable, TTY* tty, Process* fork_parent) KResultOr<NonnullRefPtr<Process>> Process::try_create(RefPtr<Thread>& first_thread, NonnullOwnPtr<KString> name, UserID uid, GroupID gid, ProcessID ppid, bool is_kernel_process, RefPtr<Custody> cwd, RefPtr<Custody> executable, TTY* tty, Process* fork_parent)
{ {
auto space = TRY(Memory::AddressSpace::try_create(fork_parent ? &fork_parent->address_space() : nullptr)); auto space = TRY(Memory::AddressSpace::try_create(fork_parent ? &fork_parent->address_space() : nullptr));
auto process = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) Process(name, uid, gid, ppid, is_kernel_process, move(cwd), move(executable), tty))); auto process = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) Process(move(name), uid, gid, ppid, is_kernel_process, move(cwd), move(executable), tty)));
TRY(process->attach_resources(move(space), first_thread, fork_parent)); TRY(process->attach_resources(move(space), first_thread, fork_parent));
return process; return process;
} }
Process::Process(const String& name, UserID uid, GroupID gid, ProcessID ppid, bool is_kernel_process, RefPtr<Custody> cwd, RefPtr<Custody> executable, TTY* tty) Process::Process(NonnullOwnPtr<KString> name, UserID uid, GroupID gid, ProcessID ppid, bool is_kernel_process, RefPtr<Custody> cwd, RefPtr<Custody> executable, TTY* tty)
: m_name(move(name)) : m_name(move(name))
, m_is_kernel_process(is_kernel_process) , m_is_kernel_process(is_kernel_process)
, m_executable(move(executable)) , m_executable(move(executable))

View file

@ -172,13 +172,13 @@ public:
}; };
template<typename EntryFunction> template<typename EntryFunction>
static RefPtr<Process> create_kernel_process(RefPtr<Thread>& first_thread, String&& name, EntryFunction entry, u32 affinity = THREAD_AFFINITY_DEFAULT, RegisterProcess do_register = RegisterProcess::Yes) static RefPtr<Process> create_kernel_process(RefPtr<Thread>& first_thread, NonnullOwnPtr<KString> name, EntryFunction entry, u32 affinity = THREAD_AFFINITY_DEFAULT, RegisterProcess do_register = RegisterProcess::Yes)
{ {
auto* entry_func = new EntryFunction(move(entry)); auto* entry_func = new EntryFunction(move(entry));
return create_kernel_process(first_thread, move(name), &Process::kernel_process_trampoline<EntryFunction>, entry_func, affinity, do_register); return create_kernel_process(first_thread, move(name), &Process::kernel_process_trampoline<EntryFunction>, entry_func, affinity, do_register);
} }
static RefPtr<Process> create_kernel_process(RefPtr<Thread>& first_thread, String&& name, void (*entry)(void*), void* entry_data = nullptr, u32 affinity = THREAD_AFFINITY_DEFAULT, RegisterProcess do_register = RegisterProcess::Yes); static RefPtr<Process> create_kernel_process(RefPtr<Thread>& first_thread, NonnullOwnPtr<KString> name, void (*entry)(void*), void* entry_data = nullptr, u32 affinity = THREAD_AFFINITY_DEFAULT, RegisterProcess do_register = RegisterProcess::Yes);
static KResultOr<NonnullRefPtr<Process>> try_create_user_process(RefPtr<Thread>& first_thread, String const& path, UserID, GroupID, Vector<String> arguments, Vector<String> environment, TTY*); static KResultOr<NonnullRefPtr<Process>> try_create_user_process(RefPtr<Thread>& first_thread, String const& path, UserID, GroupID, Vector<String> arguments, Vector<String> environment, TTY*);
static void register_new(Process&); static void register_new(Process&);
@ -207,7 +207,7 @@ public:
static RefPtr<Process> from_pid(ProcessID); static RefPtr<Process> from_pid(ProcessID);
static SessionID get_sid_from_pgid(ProcessGroupID pgid); static SessionID get_sid_from_pgid(ProcessGroupID pgid);
const String& name() const { return m_name; } StringView name() const { return m_name->view(); }
ProcessID pid() const { return m_protected_values.pid; } ProcessID pid() const { return m_protected_values.pid; }
SessionID sid() const { return m_protected_values.sid; } SessionID sid() const { return m_protected_values.sid; }
bool is_session_leader() const { return sid().value() == pid().value(); } bool is_session_leader() const { return sid().value() == pid().value(); }
@ -521,8 +521,8 @@ private:
bool add_thread(Thread&); bool add_thread(Thread&);
bool remove_thread(Thread&); bool remove_thread(Thread&);
Process(const String& name, UserID, GroupID, ProcessID ppid, bool is_kernel_process, RefPtr<Custody> cwd, RefPtr<Custody> executable, TTY* tty); Process(NonnullOwnPtr<KString> name, UserID, GroupID, ProcessID ppid, bool is_kernel_process, RefPtr<Custody> cwd, RefPtr<Custody> executable, TTY* tty);
static KResultOr<NonnullRefPtr<Process>> try_create(RefPtr<Thread>& first_thread, String const& name, UserID, GroupID, ProcessID ppid, bool is_kernel_process, RefPtr<Custody> cwd = nullptr, RefPtr<Custody> executable = nullptr, TTY* = nullptr, Process* fork_parent = nullptr); static KResultOr<NonnullRefPtr<Process>> try_create(RefPtr<Thread>& first_thread, NonnullOwnPtr<KString> name, UserID, GroupID, ProcessID ppid, bool is_kernel_process, RefPtr<Custody> cwd = nullptr, RefPtr<Custody> executable = nullptr, TTY* = nullptr, Process* fork_parent = nullptr);
KResult attach_resources(NonnullOwnPtr<Memory::AddressSpace>&&, RefPtr<Thread>& first_thread, Process* fork_parent); KResult attach_resources(NonnullOwnPtr<Memory::AddressSpace>&&, RefPtr<Thread>& first_thread, Process* fork_parent);
static ProcessID allocate_pid(); static ProcessID allocate_pid();
@ -586,7 +586,7 @@ private:
mutable IntrusiveListNode<Process> m_list_node; mutable IntrusiveListNode<Process> m_list_node;
String m_name; NonnullOwnPtr<KString> m_name;
OwnPtr<Memory::AddressSpace> m_space; OwnPtr<Memory::AddressSpace> m_space;

View file

@ -413,7 +413,7 @@ UNMAP_AFTER_INIT void Scheduler::initialize()
g_finalizer_wait_queue = new WaitQueue; g_finalizer_wait_queue = new WaitQueue;
g_finalizer_has_work.store(false, AK::MemoryOrder::memory_order_release); g_finalizer_has_work.store(false, AK::MemoryOrder::memory_order_release);
s_colonel_process = Process::create_kernel_process(idle_thread, "colonel", idle_loop, nullptr, 1, Process::RegisterProcess::No).leak_ref(); s_colonel_process = Process::create_kernel_process(idle_thread, KString::must_create("colonel"), idle_loop, nullptr, 1, Process::RegisterProcess::No).leak_ref();
VERIFY(s_colonel_process); VERIFY(s_colonel_process);
VERIFY(idle_thread); VERIFY(idle_thread);
idle_thread->set_priority(THREAD_PRIORITY_MIN); idle_thread->set_priority(THREAD_PRIORITY_MIN);

View file

@ -446,12 +446,12 @@ KResult Process::do_exec(NonnullRefPtr<FileDescription> main_program_description
if (!validate_stack_size(arguments, environment)) if (!validate_stack_size(arguments, environment))
return E2BIG; return E2BIG;
auto parts = path.split('/'); auto parts = path.split_view('/');
if (parts.is_empty()) if (parts.is_empty())
return ENOENT; return ENOENT;
auto new_process_name = parts.take_last(); auto new_process_name = TRY(KString::try_create(parts.last()));
auto new_main_thread_name = TRY(KString::try_create(new_process_name)); auto new_main_thread_name = TRY(new_process_name->try_clone());
auto main_program_metadata = main_program_description->metadata(); auto main_program_metadata = main_program_description->metadata();

View file

@ -18,7 +18,8 @@ KResultOr<FlatPtr> Process::sys$fork(RegisterState& regs)
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this); VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this);
REQUIRE_PROMISE(proc); REQUIRE_PROMISE(proc);
RefPtr<Thread> child_first_thread; RefPtr<Thread> child_first_thread;
auto child = TRY(Process::try_create(child_first_thread, m_name, uid(), gid(), pid(), m_is_kernel_process, m_cwd, m_executable, m_tty, this)); auto child_name = TRY(m_name->try_clone());
auto child = TRY(Process::try_create(child_first_thread, move(child_name), uid(), gid(), pid(), m_is_kernel_process, m_cwd, m_executable, m_tty, this));
child->m_veil_state = m_veil_state; child->m_veil_state = m_veil_state;
child->m_unveiled_paths = m_unveiled_paths.deep_copy(); child->m_unveiled_paths = m_unveiled_paths.deep_copy();

View file

@ -27,10 +27,10 @@ KResultOr<FlatPtr> Process::sys$get_process_name(Userspace<char*> buffer, size_t
{ {
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this) VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
REQUIRE_PROMISE(stdio); REQUIRE_PROMISE(stdio);
if (m_name.length() + 1 > buffer_size) if (m_name->length() + 1 > buffer_size)
return ENAMETOOLONG; return ENAMETOOLONG;
return copy_to_user(buffer, m_name.characters(), m_name.length() + 1); return copy_to_user(buffer, m_name->characters(), m_name->length() + 1);
} }
KResultOr<FlatPtr> Process::sys$set_process_name(Userspace<const char*> user_name, size_t user_name_length) KResultOr<FlatPtr> Process::sys$set_process_name(Userspace<const char*> user_name, size_t user_name_length)
@ -43,8 +43,7 @@ KResultOr<FlatPtr> Process::sys$set_process_name(Userspace<const char*> user_nam
// Empty and whitespace-only names only exist to confuse users. // Empty and whitespace-only names only exist to confuse users.
if (name->view().is_whitespace()) if (name->view().is_whitespace())
return EINVAL; return EINVAL;
// FIXME: There's a String copy here. Process::m_name should be a KString. m_name = move(name);
m_name = name->view();
return 0; return 0;
} }

View file

@ -49,7 +49,7 @@ KResultOr<FlatPtr> Process::sys$create_thread(void* (*entry)(void*), Userspace<c
// We know this thread is not the main_thread, // We know this thread is not the main_thread,
// So give it a unique name until the user calls $set_thread_name on it // So give it a unique name until the user calls $set_thread_name on it
// length + 4 to give space for our extra junk at the end // length + 4 to give space for our extra junk at the end
StringBuilder builder(m_name.length() + 4); StringBuilder builder(m_name->length() + 4);
thread->set_name(move(new_thread_name)); thread->set_name(move(new_thread_name));
if (!is_thread_joinable) if (!is_thread_joinable)

View file

@ -24,7 +24,7 @@ static void finalizer_task(void*)
UNMAP_AFTER_INIT void FinalizerTask::spawn() UNMAP_AFTER_INIT void FinalizerTask::spawn()
{ {
RefPtr<Thread> finalizer_thread; RefPtr<Thread> finalizer_thread;
auto finalizer_process = Process::create_kernel_process(finalizer_thread, "FinalizerTask", finalizer_task, nullptr); auto finalizer_process = Process::create_kernel_process(finalizer_thread, KString::must_create("FinalizerTask"), finalizer_task, nullptr);
VERIFY(finalizer_process); VERIFY(finalizer_process);
g_finalizer = finalizer_thread; g_finalizer = finalizer_thread;
} }

View file

@ -15,7 +15,7 @@ namespace Kernel {
UNMAP_AFTER_INIT void SyncTask::spawn() UNMAP_AFTER_INIT void SyncTask::spawn()
{ {
RefPtr<Thread> syncd_thread; RefPtr<Thread> syncd_thread;
Process::create_kernel_process(syncd_thread, "SyncTask", [] { Process::create_kernel_process(syncd_thread, KString::must_create("SyncTask"), [] {
dbgln("SyncTask is running"); dbgln("SyncTask is running");
for (;;) { for (;;) {
VirtualFileSystem::sync(); VirtualFileSystem::sync();

View file

@ -19,10 +19,13 @@ UNMAP_AFTER_INIT void WorkQueue::initialize()
g_io_work = new WorkQueue("IO WorkQueue"); g_io_work = new WorkQueue("IO WorkQueue");
} }
UNMAP_AFTER_INIT WorkQueue::WorkQueue(const char* name) UNMAP_AFTER_INIT WorkQueue::WorkQueue(StringView name)
{ {
RefPtr<Thread> thread; RefPtr<Thread> thread;
Process::create_kernel_process(thread, name, [this] { auto name_kstring = KString::try_create(name);
if (name_kstring.is_error())
TODO();
Process::create_kernel_process(thread, name_kstring.release_value(), [this] {
for (;;) { for (;;) {
WorkItem* item; WorkItem* item;
bool have_more; bool have_more;

View file

@ -20,8 +20,6 @@ class WorkQueue {
public: public:
static void initialize(); static void initialize();
WorkQueue(const char*);
void queue(void (*function)(void*), void* data = nullptr, void (*free_data)(void*) = nullptr) void queue(void (*function)(void*), void* data = nullptr, void (*free_data)(void*) = nullptr)
{ {
auto* item = new WorkItem; // TODO: use a pool auto* item = new WorkItem; // TODO: use a pool
@ -42,6 +40,8 @@ public:
} }
private: private:
explicit WorkQueue(StringView);
struct WorkItem { struct WorkItem {
IntrusiveListNode<WorkItem> m_node; IntrusiveListNode<WorkItem> m_node;
Function<void()> function; Function<void()> function;

View file

@ -215,7 +215,7 @@ extern "C" [[noreturn]] UNMAP_AFTER_INIT void init(BootInfo const& boot_info)
{ {
RefPtr<Thread> init_stage2_thread; RefPtr<Thread> init_stage2_thread;
Process::create_kernel_process(init_stage2_thread, "init_stage2", init_stage2, nullptr, THREAD_AFFINITY_DEFAULT, Process::RegisterProcess::No); Process::create_kernel_process(init_stage2_thread, KString::must_create("init_stage2"), init_stage2, nullptr, THREAD_AFFINITY_DEFAULT, Process::RegisterProcess::No);
// We need to make sure we drop the reference for init_stage2_thread // We need to make sure we drop the reference for init_stage2_thread
// before calling into Scheduler::start, otherwise we will have a // before calling into Scheduler::start, otherwise we will have a
// dangling Thread that never gets cleaned up // dangling Thread that never gets cleaned up