serenity/Kernel/Syscalls/waitid.cpp
Liav A 5e062414c1 Kernel: Add support for jails
Our implementation for Jails resembles much of how FreeBSD jails are
working - it's essentially only a matter of using a RefPtr in the
Process class to a Jail object. Then, when we iterate over all processes
in various cases, we could ensure if either the current process is in
jail and therefore should be restricted what is visible in terms of
PID isolation, and also to be able to expose metadata about Jails in
/sys/kernel/jails node (which does not reveal anything to a process
which is in jail).

A lifetime model for the Jail object is currently plain simple - there's
simpy no way to manually delete a Jail object once it was created. Such
feature should be carefully designed to allow safe destruction of a Jail
without the possibility of releasing a process which is in Jail from the
actual jail. Each process which is attached into a Jail cannot leave it
until the end of a Process (i.e. when finalizing a Process). All jails
are kept being referenced in the JailManagement. When a last attached
process is finalized, the Jail is automatically destroyed.
2022-11-05 18:00:58 -06:00

63 lines
1.9 KiB
C++

/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/Variant.h>
#include <Kernel/Debug.h>
#include <Kernel/Process.h>
namespace Kernel {
ErrorOr<siginfo_t> Process::do_waitid(Variant<Empty, NonnullLockRefPtr<Process>, NonnullLockRefPtr<ProcessGroup>> waitee, int options)
{
ErrorOr<siginfo_t> result = siginfo_t {};
if (Thread::current()->block<Thread::WaitBlocker>({}, options, move(waitee), result).was_interrupted())
return EINTR;
VERIFY(!result.is_error() || (options & WNOHANG));
return result;
}
ErrorOr<FlatPtr> Process::sys$waitid(Userspace<Syscall::SC_waitid_params const*> user_params)
{
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this);
TRY(require_promise(Pledge::proc));
auto params = TRY(copy_typed_from_user(user_params));
Variant<Empty, NonnullLockRefPtr<Process>, NonnullLockRefPtr<ProcessGroup>> waitee;
switch (params.idtype) {
case P_ALL:
break;
case P_PID: {
auto waitee_process = Process::from_pid_in_same_jail(params.id);
if (!waitee_process)
return ECHILD;
bool waitee_is_child = waitee_process->ppid() == Process::current().pid();
bool waitee_is_our_tracee = waitee_process->has_tracee_thread(Process::current().pid());
if (!waitee_is_child && !waitee_is_our_tracee)
return ECHILD;
waitee = waitee_process.release_nonnull();
break;
}
case P_PGID: {
auto waitee_group = ProcessGroup::from_pgid(params.id);
if (!waitee_group) {
return ECHILD;
}
waitee = waitee_group.release_nonnull();
break;
}
default:
return EINVAL;
}
dbgln_if(PROCESS_DEBUG, "sys$waitid({}, {}, {}, {})", params.idtype, params.id, params.infop, params.options);
auto siginfo = TRY(do_waitid(move(waitee), params.options));
TRY(copy_to_user(params.infop, &siginfo));
return 0;
}
}