Repository: SerenityOS/serenity
File: Kernel/Tasks/Process.cpp (C++)

Code before:
}

void Process::protect_data()
{
    m_protected_data_refs.unref([&]() {
        MM.set_page_writable_direct(VirtualAddress { &this->m_protected_values_do_not_access_directly }, false);
    });
}

void Process::unprotect_data()
{
    m_protected_data_refs.ref([&]() {
        MM.set_page_writable_direct(VirtualAddress { &this->m_protected_values_do_not_access_directly }, true);
    });
}

ErrorOr<Process::ProcessAndFirstThread> Process::create_from_fork(Process& parent)
{
    VERIFY(!parent.is_kernel_process());
    auto const& credentials = parent.credentials();

    Process::Name name {};
    parent.name().with([&name](auto& process_name) {
        name.store_characters(process_name.representable_view());
    });

    return TRY(Process::create_impl(name.representable_view(),
        credentials->uid(), credentials->gid(), parent.pid(),
        parent.is_kernel_process(), parent.vfs_root_context(),
        parent.hostname_context(), parent.current_directory(),
        parent.executable(), parent.tty(), &parent));
}

ErrorOr<Process::ProcessAndFirstThread> Process::create_spawned(UserID uid, GroupID gid, ProcessID ppid, NonnullRefPtr<VFSRootContext> vfs_root_context, NonnullRefPtr<HostnameContext> hostname_context, NonnullRefPtr<Custody> current_directory, RefPtr<TTY> tty)
{
    return TRY(Process::create_impl("Spawned Process"sv,
        uid, gid, ppid,
        false, vfs_root_context,
        hostname_context, current_directory,
        nullptr, tty, nullptr));
}

ErrorOr<Process::ProcessAndFirstThread> Process::create_impl(StringView name, UserID uid, GroupID gid, ProcessID ppid, bool is_kernel_process, NonnullRefPtr<VFSRootContext> vfs_root_context, NonnullRefPtr<HostnameContext> hostname_context, RefPtr<Custody> current_directory, RefPtr<Custody> executable, RefPtr<TTY> tty, Process* fork_parent)
{
    auto unveil_tree = UnveilNode { TRY(KString::try_create("/"sv)), UnveilMetadata(TRY(KString::try_create("/"sv))) };
    auto exec_unveil_tree = UnveilNode { TRY(KString::try_create("/"sv)), UnveilMetadata(TRY(KString::try_create("/"sv))) };
    auto credentials = TRY(Credentials::create(uid, gid, uid, gid, uid, gid, {}, fork_parent ? fork_parent->sid() : 0, fork_parent ? fork_parent->pgid() : 0));

    auto process = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) Process(name, move(credentials), ppid, is_kernel_process, move(vfs_root_context), move(hostname_context), move(current_directory), move(executable), tty, move(unveil_tree), move(exec_unveil_tree), kgettimeofday())));

    OwnPtr<Memory::AddressSpace> new_address_space;

Review comment (suggestion):
This `parent.is_kerenl_process()` should be superfluous as you `VERIFY(!parent.is_kernel_process());` a couple lines above.

Code after:
{
    m_protected_data_refs.unref([&]() {
        MM.set_page_writable_direct(VirtualAddress { &this->m_protected_values_do_not_access_directly }, false);
    });
}

void Process::unprotect_data()
{
    m_protected_data_refs.ref([&]() {
        MM.set_page_writable_direct(VirtualAddress { &this->m_protected_values_do_not_access_directly }, true);
    });
}

ErrorOr<Process::ProcessAndFirstThread> Process::create_from_fork(Process& parent)
{
    VERIFY(!parent.is_kernel_process());
    auto const& credentials = parent.credentials();

    Process::Name name {};
    parent.name().with([&name](auto& process_name) {
        name.store_characters(process_name.representable_view());
    });

    return Process::create_impl(name.representable_view(),
        credentials->uid(), credentials->gid(), parent.pid(),
        parent.is_kernel_process(), parent.vfs_root_context(),
        parent.hostname_context(), parent.current_directory(),
        parent.executable(), parent.tty(), &parent);
}

ErrorOr<Process::ProcessAndFirstThread> Process::create_spawned(UserID uid, GroupID gid, ProcessID ppid, NonnullRefPtr<VFSRootContext> vfs_root_context, NonnullRefPtr<HostnameContext> hostname_context, NonnullRefPtr<Custody> current_directory, RefPtr<TTY> tty)
{
    // This name shouldn't be visible as we will `exec` on this process before
    // commiting it.
    return Process::create_impl("PLACEHOLDER NAME"sv,
        uid, gid, ppid,
        false, vfs_root_context,
        hostname_context, current_directory,
        nullptr, tty, nullptr);
}

ErrorOr<Process::ProcessAndFirstThread> Process::create_impl(StringView name, UserID uid, GroupID gid, ProcessID ppid, bool is_kernel_process, NonnullRefPtr<VFSRootContext> vfs_root_context, NonnullRefPtr<HostnameContext> hostname_context, RefPtr<Custody> current_directory, RefPtr<Custody> executable, RefPtr<TTY> tty, Process* fork_parent)
{
    auto unveil_tree = UnveilNode { TRY(KString::try_create("/"sv)), UnveilMetadata(TRY(KString::try_create("/"sv))) };
    auto exec_unveil_tree = UnveilNode { TRY(KString::try_create("/"sv)), UnveilMetadata(TRY(KString::try_create("/"sv))) };
    auto credentials = TRY(Credentials::create(uid, gid, uid, gid, uid, gid, {}, fork_parent ? fork_parent->sid() : 0, fork_parent ? fork_parent->pgid() : 0));

    auto process = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) Process(name, move(credentials), ppid, is_kernel_process, move(vfs_root_context), move(hostname_context), move(current_directory), move(executable), tty, move(unveil_tree), move(exec_unveil_tree), kgettimeofday())));

    OwnPtr<Memory::AddressSpace> new_address_space;
    if (fork_parent) {