ソースを参照

Kernel: Store process names as KString

Andreas Kling 3 年 前
コミット
55b0b06897

+ 5 - 1
Kernel/Bus/USB/UHCI/UHCIController.cpp

@@ -472,7 +472,11 @@ void UHCIController::spawn_port_proc()
 {
     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 (;;) {
             if (m_root_hub)
                 m_root_hub->check_for_port_updates();

+ 4 - 1
Kernel/FileSystem/Plan9FileSystem.cpp

@@ -655,7 +655,10 @@ void Plan9FS::ensure_thread()
 {
     SpinlockLocker lock(m_thread_lock);
     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();
             m_thread_running.store(false, AK::MemoryOrder::memory_order_release);
         });

+ 4 - 1
Kernel/Net/NetworkTask.cpp

@@ -43,7 +43,10 @@ static HashTable<RefPtr<TCPSocket>>* delayed_ack_sockets;
 void NetworkTask::spawn()
 {
     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;
 }
 

+ 7 - 6
Kernel/Process.cpp

@@ -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)
 {
-    auto parts = path.split('/');
+    auto parts = path.split_view('/');
     if (arguments.is_empty()) {
         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())) {
         first_thread = nullptr;
@@ -180,7 +181,7 @@ KResultOr<NonnullRefPtr<Process>> Process::try_create_user_process(RefPtr<Thread
     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);
     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 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));
     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_is_kernel_process(is_kernel_process)
     , m_executable(move(executable))

+ 6 - 6
Kernel/Process.h

@@ -172,13 +172,13 @@ public:
     };
 
     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));
         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 void register_new(Process&);
 
@@ -207,7 +207,7 @@ public:
     static RefPtr<Process> from_pid(ProcessID);
     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; }
     SessionID sid() const { return m_protected_values.sid; }
     bool is_session_leader() const { return sid().value() == pid().value(); }
@@ -521,8 +521,8 @@ private:
     bool add_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);
-    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);
+    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, 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);
     static ProcessID allocate_pid();
 
@@ -586,7 +586,7 @@ private:
 
     mutable IntrusiveListNode<Process> m_list_node;
 
-    String m_name;
+    NonnullOwnPtr<KString> m_name;
 
     OwnPtr<Memory::AddressSpace> m_space;
 

+ 1 - 1
Kernel/Scheduler.cpp

@@ -413,7 +413,7 @@ UNMAP_AFTER_INIT void Scheduler::initialize()
     g_finalizer_wait_queue = new WaitQueue;
 
     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(idle_thread);
     idle_thread->set_priority(THREAD_PRIORITY_MIN);

+ 3 - 3
Kernel/Syscalls/execve.cpp

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

+ 2 - 1
Kernel/Syscalls/fork.cpp

@@ -18,7 +18,8 @@ KResultOr<FlatPtr> Process::sys$fork(RegisterState& regs)
     VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this);
     REQUIRE_PROMISE(proc);
     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_unveiled_paths = m_unveiled_paths.deep_copy();
 

+ 3 - 4
Kernel/Syscalls/process.cpp

@@ -27,10 +27,10 @@ KResultOr<FlatPtr> Process::sys$get_process_name(Userspace<char*> buffer, size_t
 {
     VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
     REQUIRE_PROMISE(stdio);
-    if (m_name.length() + 1 > buffer_size)
+    if (m_name->length() + 1 > buffer_size)
         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)
@@ -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.
     if (name->view().is_whitespace())
         return EINVAL;
-    // FIXME: There's a String copy here. Process::m_name should be a KString.
-    m_name = name->view();
+    m_name = move(name);
     return 0;
 }
 

+ 1 - 1
Kernel/Syscalls/thread.cpp

@@ -49,7 +49,7 @@ KResultOr<FlatPtr> Process::sys$create_thread(void* (*entry)(void*), Userspace<c
     // We know this thread is not the main_thread,
     // 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
-    StringBuilder builder(m_name.length() + 4);
+    StringBuilder builder(m_name->length() + 4);
     thread->set_name(move(new_thread_name));
 
     if (!is_thread_joinable)

+ 1 - 1
Kernel/Tasks/FinalizerTask.cpp

@@ -24,7 +24,7 @@ static void finalizer_task(void*)
 UNMAP_AFTER_INIT void FinalizerTask::spawn()
 {
     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);
     g_finalizer = finalizer_thread;
 }

+ 1 - 1
Kernel/Tasks/SyncTask.cpp

@@ -15,7 +15,7 @@ namespace Kernel {
 UNMAP_AFTER_INIT void SyncTask::spawn()
 {
     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");
         for (;;) {
             VirtualFileSystem::sync();

+ 5 - 2
Kernel/WorkQueue.cpp

@@ -19,10 +19,13 @@ UNMAP_AFTER_INIT void WorkQueue::initialize()
     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;
-    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 (;;) {
             WorkItem* item;
             bool have_more;

+ 2 - 2
Kernel/WorkQueue.h

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

+ 1 - 1
Kernel/init.cpp

@@ -215,7 +215,7 @@ extern "C" [[noreturn]] UNMAP_AFTER_INIT void init(BootInfo const& boot_info)
 
     {
         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
         // before calling into Scheduler::start, otherwise we will have a
         // dangling Thread that never gets cleaned up