2020-07-31 00:38:15 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-07-31 00:38:15 +03:00
|
|
|
*/
|
|
|
|
|
2021-06-21 18:34:09 +03:00
|
|
|
#include <Kernel/Arch/x86/InterruptDisabler.h>
|
2020-07-31 00:38:15 +03:00
|
|
|
#include <Kernel/Process.h>
|
|
|
|
#include <Kernel/TTY/TTY.h>
|
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
2021-06-28 21:59:35 +03:00
|
|
|
KResultOr<FlatPtr> Process::sys$getsid(pid_t pid)
|
2020-07-31 00:38:15 +03:00
|
|
|
{
|
2021-07-18 21:20:12 +03:00
|
|
|
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
|
2020-07-31 00:38:15 +03:00
|
|
|
REQUIRE_PROMISE(proc);
|
|
|
|
if (pid == 0)
|
2021-03-10 21:59:46 +03:00
|
|
|
return sid().value();
|
2020-08-02 05:04:56 +03:00
|
|
|
auto process = Process::from_pid(pid);
|
2020-07-31 00:38:15 +03:00
|
|
|
if (!process)
|
2021-03-01 15:49:16 +03:00
|
|
|
return ESRCH;
|
2021-03-10 21:59:46 +03:00
|
|
|
if (sid() != process->sid())
|
2021-03-01 15:49:16 +03:00
|
|
|
return EPERM;
|
2021-03-10 21:59:46 +03:00
|
|
|
return process->sid().value();
|
2020-07-31 00:38:15 +03:00
|
|
|
}
|
|
|
|
|
2021-06-28 21:59:35 +03:00
|
|
|
KResultOr<FlatPtr> Process::sys$setsid()
|
2020-07-31 00:38:15 +03:00
|
|
|
{
|
2021-07-18 21:20:12 +03:00
|
|
|
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
|
2020-07-31 00:38:15 +03:00
|
|
|
REQUIRE_PROMISE(proc);
|
|
|
|
InterruptDisabler disabler;
|
|
|
|
bool found_process_with_same_pgid_as_my_pid = false;
|
2020-08-08 18:32:34 +03:00
|
|
|
Process::for_each_in_pgrp(pid().value(), [&](auto&) {
|
2020-07-31 00:38:15 +03:00
|
|
|
found_process_with_same_pgid_as_my_pid = true;
|
|
|
|
return IterationDecision::Break;
|
|
|
|
});
|
|
|
|
if (found_process_with_same_pgid_as_my_pid)
|
2021-03-01 15:49:16 +03:00
|
|
|
return EPERM;
|
2020-08-08 23:04:20 +03:00
|
|
|
// Create a new Session and a new ProcessGroup.
|
2021-03-10 21:59:46 +03:00
|
|
|
m_pg = ProcessGroup::create(ProcessGroupID(pid().value()));
|
2020-07-31 00:38:15 +03:00
|
|
|
m_tty = nullptr;
|
2021-03-11 15:13:05 +03:00
|
|
|
ProtectedDataMutationScope scope { *this };
|
|
|
|
m_sid = pid().value();
|
2021-03-10 21:59:46 +03:00
|
|
|
return sid().value();
|
2020-07-31 00:38:15 +03:00
|
|
|
}
|
|
|
|
|
2021-06-28 21:59:35 +03:00
|
|
|
KResultOr<FlatPtr> Process::sys$getpgid(pid_t pid)
|
2020-07-31 00:38:15 +03:00
|
|
|
{
|
2021-07-18 21:20:12 +03:00
|
|
|
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
|
2020-07-31 00:38:15 +03:00
|
|
|
REQUIRE_PROMISE(proc);
|
|
|
|
if (pid == 0)
|
2020-08-15 22:13:19 +03:00
|
|
|
return pgid().value();
|
2020-08-02 05:04:56 +03:00
|
|
|
auto process = Process::from_pid(pid);
|
2020-07-31 00:38:15 +03:00
|
|
|
if (!process)
|
2021-03-01 15:49:16 +03:00
|
|
|
return ESRCH;
|
2020-08-15 22:13:19 +03:00
|
|
|
return process->pgid().value();
|
2020-07-31 00:38:15 +03:00
|
|
|
}
|
|
|
|
|
2021-06-28 21:59:35 +03:00
|
|
|
KResultOr<FlatPtr> Process::sys$getpgrp()
|
2020-07-31 00:38:15 +03:00
|
|
|
{
|
2021-07-18 21:20:12 +03:00
|
|
|
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
|
2020-07-31 00:38:15 +03:00
|
|
|
REQUIRE_PROMISE(stdio);
|
2020-08-15 22:13:19 +03:00
|
|
|
return pgid().value();
|
2020-07-31 00:38:15 +03:00
|
|
|
}
|
|
|
|
|
2020-08-08 23:04:20 +03:00
|
|
|
SessionID Process::get_sid_from_pgid(ProcessGroupID pgid)
|
2020-07-31 00:38:15 +03:00
|
|
|
{
|
2020-08-08 23:04:20 +03:00
|
|
|
// FIXME: This xor sys$setsid() uses the wrong locking mechanism.
|
|
|
|
|
|
|
|
SessionID sid { -1 };
|
|
|
|
Process::for_each_in_pgrp(pgid, [&](auto& process) {
|
|
|
|
sid = process.sid();
|
|
|
|
return IterationDecision::Break;
|
|
|
|
});
|
|
|
|
|
|
|
|
return sid;
|
2020-07-31 00:38:15 +03:00
|
|
|
}
|
|
|
|
|
2021-06-28 21:59:35 +03:00
|
|
|
KResultOr<FlatPtr> Process::sys$setpgid(pid_t specified_pid, pid_t specified_pgid)
|
2020-07-31 00:38:15 +03:00
|
|
|
{
|
2021-07-18 21:20:12 +03:00
|
|
|
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
|
2020-07-31 00:38:15 +03:00
|
|
|
REQUIRE_PROMISE(proc);
|
2021-03-10 21:59:46 +03:00
|
|
|
ProcessID pid = specified_pid ? ProcessID(specified_pid) : this->pid();
|
2020-07-31 00:38:15 +03:00
|
|
|
if (specified_pgid < 0) {
|
|
|
|
// The value of the pgid argument is less than 0, or is not a value supported by the implementation.
|
2021-03-01 15:49:16 +03:00
|
|
|
return EINVAL;
|
2020-07-31 00:38:15 +03:00
|
|
|
}
|
2020-08-02 05:04:56 +03:00
|
|
|
auto process = Process::from_pid(pid);
|
2020-07-31 00:38:15 +03:00
|
|
|
if (!process)
|
2021-03-01 15:49:16 +03:00
|
|
|
return ESRCH;
|
2021-03-10 21:59:46 +03:00
|
|
|
if (process != this && process->ppid() != this->pid()) {
|
2020-07-31 00:38:15 +03:00
|
|
|
// The value of the pid argument does not match the process ID
|
|
|
|
// of the calling process or of a child process of the calling process.
|
2021-03-01 15:49:16 +03:00
|
|
|
return ESRCH;
|
2020-07-31 00:38:15 +03:00
|
|
|
}
|
2020-08-08 23:04:20 +03:00
|
|
|
if (process->is_session_leader()) {
|
2020-07-31 00:38:15 +03:00
|
|
|
// The process indicated by the pid argument is a session leader.
|
2021-03-01 15:49:16 +03:00
|
|
|
return EPERM;
|
2020-07-31 00:38:15 +03:00
|
|
|
}
|
2021-03-10 21:59:46 +03:00
|
|
|
if (process->ppid() == this->pid() && process->sid() != sid()) {
|
2020-07-31 00:38:15 +03:00
|
|
|
// The value of the pid argument matches the process ID of a child
|
|
|
|
// process of the calling process and the child process is not in
|
|
|
|
// the same session as the calling process.
|
2021-03-01 15:49:16 +03:00
|
|
|
return EPERM;
|
2020-07-31 00:38:15 +03:00
|
|
|
}
|
|
|
|
|
2021-03-10 21:59:46 +03:00
|
|
|
ProcessGroupID new_pgid = specified_pgid ? ProcessGroupID(specified_pgid) : process->pid().value();
|
2020-08-08 23:04:20 +03:00
|
|
|
SessionID current_sid = sid();
|
|
|
|
SessionID new_sid = get_sid_from_pgid(new_pgid);
|
2020-08-11 14:23:02 +03:00
|
|
|
if (new_sid != -1 && current_sid != new_sid) {
|
2020-07-31 00:38:15 +03:00
|
|
|
// Can't move a process between sessions.
|
2021-03-01 15:49:16 +03:00
|
|
|
return EPERM;
|
2020-07-31 00:38:15 +03:00
|
|
|
}
|
2021-03-10 21:59:46 +03:00
|
|
|
if (new_sid == -1 && new_pgid != process->pid().value()) {
|
2020-08-11 14:23:02 +03:00
|
|
|
// The value of the pgid argument is valid, but is not
|
|
|
|
// the calling pid, and is not an existing process group.
|
2021-03-01 15:49:16 +03:00
|
|
|
return EPERM;
|
2020-08-11 14:23:02 +03:00
|
|
|
}
|
2020-07-31 00:38:15 +03:00
|
|
|
// FIXME: There are more EPERM conditions to check for here..
|
2020-08-15 22:13:19 +03:00
|
|
|
process->m_pg = ProcessGroup::find_or_create(new_pgid);
|
2021-05-18 12:07:25 +03:00
|
|
|
if (!process->m_pg) {
|
|
|
|
return ENOMEM;
|
|
|
|
}
|
2020-07-31 00:38:15 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|