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
|
|
|
*/
|
|
|
|
|
2022-10-05 17:27:36 +00:00
|
|
|
#include <Kernel/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
|
|
|
{
|
2022-08-17 20:03:04 +00:00
|
|
|
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this);
|
2022-10-03 11:42:15 +00:00
|
|
|
TRY(require_promise(Pledge::stdio));
|
2020-07-30 21:38:15 +00:00
|
|
|
if (pid == 0)
|
2021-03-10 18:59:46 +00:00
|
|
|
return sid().value();
|
2022-11-02 20:26:02 +00:00
|
|
|
auto process = Process::from_pid_in_same_jail(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
|
|
|
{
|
2022-08-17 20:03:04 +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;
|
2022-11-02 20:26:02 +00:00
|
|
|
TRY(Process::for_each_in_pgrp_in_same_jail(pid().value(), [&](auto&) -> ErrorOr<void> {
|
2020-07-30 21:38:15 +00:00
|
|
|
found_process_with_same_pgid_as_my_pid = true;
|
2022-11-02 20:26:02 +00:00
|
|
|
return {};
|
|
|
|
}));
|
2020-07-30 21:38:15 +00:00
|
|
|
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;
|
2022-08-21 10:18:26 +00:00
|
|
|
return with_mutable_protected_data([&](auto& protected_data) -> ErrorOr<FlatPtr> {
|
|
|
|
protected_data.sid = pid().value();
|
|
|
|
return protected_data.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
|
|
|
{
|
2022-08-17 20:03:04 +00:00
|
|
|
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this);
|
2022-10-03 11:42:15 +00:00
|
|
|
TRY(require_promise(Pledge::stdio));
|
2020-07-30 21:38:15 +00:00
|
|
|
if (pid == 0)
|
2020-08-15 19:13:19 +00:00
|
|
|
return pgid().value();
|
2022-11-02 20:26:02 +00:00
|
|
|
auto process = Process::from_pid_in_same_jail(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
|
|
|
{
|
2022-08-17 20:03:04 +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 };
|
2022-11-02 20:26:02 +00:00
|
|
|
MUST(Process::current().for_each_in_pgrp_in_same_jail(pgid, [&](auto& process) -> ErrorOr<void> {
|
2020-08-08 20:04:20 +00:00
|
|
|
sid = process.sid();
|
2022-11-02 20:26:02 +00:00
|
|
|
return {};
|
|
|
|
}));
|
2020-08-08 20:04:20 +00:00
|
|
|
|
|
|
|
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
|
|
|
{
|
2022-08-17 20:03:04 +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
|
|
|
}
|
2022-11-02 20:26:02 +00:00
|
|
|
auto process = Process::from_pid_in_same_jail(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;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|