Skip to content

Commit

Permalink
Kernel: Store process names as KString
Browse files Browse the repository at this point in the history
  • Loading branch information
awesomekling committed Sep 7, 2021
1 parent db2e67f commit 55b0b06
Show file tree
Hide file tree
Showing 15 changed files with 46 additions and 32 deletions.
6 changes: 5 additions & 1 deletion Kernel/Bus/USB/UHCI/UHCIController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
5 changes: 4 additions & 1 deletion Kernel/FileSystem/Plan9FileSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down
5 changes: 4 additions & 1 deletion Kernel/Net/NetworkTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
13 changes: 7 additions & 6 deletions Kernel/Process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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))
Expand Down
12 changes: 6 additions & 6 deletions Kernel/Process.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,13 @@ class Process final
};

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&);

Expand Down Expand Up @@ -207,7 +207,7 @@ class Process final
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(); }
Expand Down Expand Up @@ -521,8 +521,8 @@ class Process final
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();

Expand Down Expand Up @@ -586,7 +586,7 @@ class Process final

mutable IntrusiveListNode<Process> m_list_node;

String m_name;
NonnullOwnPtr<KString> m_name;

OwnPtr<Memory::AddressSpace> m_space;

Expand Down
2 changes: 1 addition & 1 deletion Kernel/Scheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions Kernel/Syscalls/execve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
3 changes: 2 additions & 1 deletion Kernel/Syscalls/fork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
7 changes: 3 additions & 4 deletions Kernel/Syscalls/process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion Kernel/Syscalls/thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion Kernel/Tasks/FinalizerTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion Kernel/Tasks/SyncTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
7 changes: 5 additions & 2 deletions Kernel/WorkQueue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions Kernel/WorkQueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -42,6 +40,8 @@ class WorkQueue {
}

private:
explicit WorkQueue(StringView);

struct WorkItem {
IntrusiveListNode<WorkItem> m_node;
Function<void()> function;
Expand Down
2 changes: 1 addition & 1 deletion Kernel/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 55b0b06

Please sign in to comment.