2020-07-30 21:38:15 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-07-30 21:38:15 +00:00
|
|
|
*/
|
|
|
|
|
2021-06-21 15:34:09 +00:00
|
|
|
#include <Kernel/Arch/x86/InterruptDisabler.h>
|
2020-07-30 21:38:15 +00:00
|
|
|
#include <Kernel/Process.h>
|
|
|
|
#include <Kernel/TTY/TTY.h>
|
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
2021-11-07 23:51:39 +00:00
|
|
|
ErrorOr<FlatPtr> Process::sys$getsid(pid_t pid)
|
2020-07-30 21:38:15 +00:00
|
|
|
{
|
2021-07-18 18:20:12 +00:00
|
|
|
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
|
2021-12-29 09:11:45 +00:00
|
|
|
TRY(require_promise(Pledge::proc));
|
2020-07-30 21:38:15 +00:00
|
|
|
if (pid == 0)
|
2021-03-10 18:59:46 +00:00
|
|
|
return sid().value();
|
2020-08-02 02:04:56 +00:00
|
|
|
auto process = Process::from_pid(pid);
|
2020-07-30 21:38:15 +00:00
|
|
|
if (!process)
|
2021-03-01 12:49:16 +00:00
|
|
|
return ESRCH;
|
2021-03-10 18:59:46 +00:00
|
|
|
if (sid() != process->sid())
|
2021-03-01 12:49:16 +00:00
|
|
|
return EPERM;
|
2021-03-10 18:59:46 +00:00
|
|
|
return process->sid().value();
|
2020-07-30 21:38:15 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 23:51:39 +00:00
|
|
|
ErrorOr<FlatPtr> Process::sys$setsid()
|
2020-07-30 21:38:15 +00:00
|
|
|
{
|
2021-07-18 18:20:12 +00:00
|
|
|
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
|
2021-12-29 09:11:45 +00:00
|
|
|
TRY(require_promise(Pledge::proc));
|
2020-07-30 21:38:15 +00:00
|
|
|
InterruptDisabler disabler;
|
|
|
|
bool found_process_with_same_pgid_as_my_pid = false;
|
2020-08-08 15:32:34 +00:00
|
|
|
Process::for_each_in_pgrp(pid().value(), [&](auto&) {
|
2020-07-30 21:38:15 +00: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 12:49:16 +00:00
|
|
|
return EPERM;
|
2020-08-08 20:04:20 +00:00
|
|
|
// Create a new Session and a new ProcessGroup.
|
2021-09-04 20:58:59 +00:00
|
|
|
|
2021-09-05 19:48:04 +00:00
|
|
|
m_pg = TRY(ProcessGroup::try_create(ProcessGroupID(pid().value())));
|
2020-07-30 21:38:15 +00:00
|
|
|
m_tty = nullptr;
|
2021-03-11 12:13:05 +00:00
|
|
|
ProtectedDataMutationScope scope { *this };
|
2021-08-07 19:30:06 +00:00
|
|
|
m_protected_values.sid = pid().value();
|
2021-03-10 18:59:46 +00:00
|
|
|
return sid().value();
|
2020-07-30 21:38:15 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 23:51:39 +00:00
|
|
|
ErrorOr<FlatPtr> Process::sys$getpgid(pid_t pid)
|
2020-07-30 21:38:15 +00:00
|
|
|
{
|
2021-07-18 18:20:12 +00:00
|
|
|
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
|
2021-12-29 09:11:45 +00:00
|
|
|
TRY(require_promise(Pledge::proc));
|
2020-07-30 21:38:15 +00:00
|
|
|
if (pid == 0)
|
2020-08-15 19:13:19 +00:00
|
|
|
return pgid().value();
|
2020-08-02 02:04:56 +00:00
|
|
|
auto process = Process::from_pid(pid);
|
2020-07-30 21:38:15 +00:00
|
|
|
if (!process)
|
2021-03-01 12:49:16 +00:00
|
|
|
return ESRCH;
|
2020-08-15 19:13:19 +00:00
|
|
|
return process->pgid().value();
|
2020-07-30 21:38:15 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 23:51:39 +00:00
|
|
|
ErrorOr<FlatPtr> Process::sys$getpgrp()
|
2020-07-30 21:38:15 +00:00
|
|
|
{
|
2021-07-18 18:20:12 +00:00
|
|
|
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
|
2021-12-29 09:11:45 +00:00
|
|
|
TRY(require_promise(Pledge::stdio));
|
2020-08-15 19:13:19 +00:00
|
|
|
return pgid().value();
|
2020-07-30 21:38:15 +00:00
|
|
|
}
|
|
|
|
|
2020-08-08 20:04:20 +00:00
|
|
|
SessionID Process::get_sid_from_pgid(ProcessGroupID pgid)
|
2020-07-30 21:38:15 +00:00
|
|
|
{
|
2020-08-08 20:04:20 +00: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-30 21:38:15 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 23:51:39 +00:00
|
|
|
ErrorOr<FlatPtr> Process::sys$setpgid(pid_t specified_pid, pid_t specified_pgid)
|
2020-07-30 21:38:15 +00:00
|
|
|
{
|
2021-07-18 18:20:12 +00:00
|
|
|
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
|
2021-12-29 09:11:45 +00:00
|
|
|
TRY(require_promise(Pledge::proc));
|
2021-03-10 18:59:46 +00:00
|
|
|
ProcessID pid = specified_pid ? ProcessID(specified_pid) : this->pid();
|
2020-07-30 21:38:15 +00: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 12:49:16 +00:00
|
|
|
return EINVAL;
|
2020-07-30 21:38:15 +00:00
|
|
|
}
|
2020-08-02 02:04:56 +00:00
|
|
|
auto process = Process::from_pid(pid);
|
2020-07-30 21:38:15 +00:00
|
|
|
if (!process)
|
2021-03-01 12:49:16 +00:00
|
|
|
return ESRCH;
|
2021-03-10 18:59:46 +00:00
|
|
|
if (process != this && process->ppid() != this->pid()) {
|
2020-07-30 21:38:15 +00: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 12:49:16 +00:00
|
|
|
return ESRCH;
|
2020-07-30 21:38:15 +00:00
|
|
|
}
|
2020-08-08 20:04:20 +00:00
|
|
|
if (process->is_session_leader()) {
|
2020-07-30 21:38:15 +00:00
|
|
|
// The process indicated by the pid argument is a session leader.
|
2021-03-01 12:49:16 +00:00
|
|
|
return EPERM;
|
2020-07-30 21:38:15 +00:00
|
|
|
}
|
2021-03-10 18:59:46 +00:00
|
|
|
if (process->ppid() == this->pid() && process->sid() != sid()) {
|
2020-07-30 21:38:15 +00: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 12:49:16 +00:00
|
|
|
return EPERM;
|
2020-07-30 21:38:15 +00:00
|
|
|
}
|
|
|
|
|
2021-03-10 18:59:46 +00:00
|
|
|
ProcessGroupID new_pgid = specified_pgid ? ProcessGroupID(specified_pgid) : process->pid().value();
|
2020-08-08 20:04:20 +00:00
|
|
|
SessionID current_sid = sid();
|
|
|
|
SessionID new_sid = get_sid_from_pgid(new_pgid);
|
2020-08-11 11:23:02 +00:00
|
|
|
if (new_sid != -1 && current_sid != new_sid) {
|
2020-07-30 21:38:15 +00:00
|
|
|
// Can't move a process between sessions.
|
2021-03-01 12:49:16 +00:00
|
|
|
return EPERM;
|
2020-07-30 21:38:15 +00:00
|
|
|
}
|
2021-03-10 18:59:46 +00:00
|
|
|
if (new_sid == -1 && new_pgid != process->pid().value()) {
|
2020-08-11 11:23:02 +00: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 12:49:16 +00:00
|
|
|
return EPERM;
|
2020-08-11 11:23:02 +00:00
|
|
|
}
|
2020-07-30 21:38:15 +00:00
|
|
|
// FIXME: There are more EPERM conditions to check for here..
|
2021-09-05 19:48:04 +00:00
|
|
|
process->m_pg = TRY(ProcessGroup::try_find_or_create(new_pgid));
|
2020-07-30 21:38:15 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|