diff --git a/Kernel/ProcessGroup.cpp b/Kernel/ProcessGroup.cpp index c2830778be7..609906dc124 100644 --- a/Kernel/ProcessGroup.cpp +++ b/Kernel/ProcessGroup.cpp @@ -24,7 +24,7 @@ ProcessGroup::~ProcessGroup() }); } -RefPtr ProcessGroup::create(ProcessGroupID pgid) +RefPtr ProcessGroup::try_create(ProcessGroupID pgid) { auto process_group = adopt_ref_if_nonnull(new (nothrow) ProcessGroup(pgid)); if (!process_group) @@ -35,7 +35,7 @@ RefPtr ProcessGroup::create(ProcessGroupID pgid) return process_group; } -RefPtr ProcessGroup::find_or_create(ProcessGroupID pgid) +RefPtr ProcessGroup::try_find_or_create(ProcessGroupID pgid) { return process_groups().with([&](auto& groups) -> RefPtr { for (auto& group : groups) { diff --git a/Kernel/ProcessGroup.h b/Kernel/ProcessGroup.h index 86261c4a5ca..bc34aa0a9ba 100644 --- a/Kernel/ProcessGroup.h +++ b/Kernel/ProcessGroup.h @@ -24,8 +24,8 @@ class ProcessGroup public: ~ProcessGroup(); - static RefPtr create(ProcessGroupID); - static RefPtr find_or_create(ProcessGroupID); + static RefPtr try_create(ProcessGroupID); + static RefPtr try_find_or_create(ProcessGroupID); static RefPtr from_pgid(ProcessGroupID); const ProcessGroupID& pgid() const { return m_pgid; } diff --git a/Kernel/Syscalls/setpgid.cpp b/Kernel/Syscalls/setpgid.cpp index 7800c5b3a9c..5ad18f0521c 100644 --- a/Kernel/Syscalls/setpgid.cpp +++ b/Kernel/Syscalls/setpgid.cpp @@ -37,7 +37,12 @@ KResultOr Process::sys$setsid() if (found_process_with_same_pgid_as_my_pid) return EPERM; // Create a new Session and a new ProcessGroup. - m_pg = ProcessGroup::create(ProcessGroupID(pid().value())); + + auto new_pg = ProcessGroup::try_create(ProcessGroupID(pid().value())); + if (!new_pg) + return ENOMEM; + + m_pg = move(new_pg); m_tty = nullptr; ProtectedDataMutationScope scope { *this }; m_protected_values.sid = pid().value(); @@ -117,10 +122,10 @@ KResultOr Process::sys$setpgid(pid_t specified_pid, pid_t specified_pgi return EPERM; } // FIXME: There are more EPERM conditions to check for here.. - process->m_pg = ProcessGroup::find_or_create(new_pgid); - if (!process->m_pg) { + auto process_group = ProcessGroup::try_find_or_create(new_pgid); + if (!process_group) return ENOMEM; - } + process->m_pg = move(process_group); return 0; }