/* * Copyright (c) 2022, Liav A. * * SPDX-License-Identifier: BSD-2-Clause */ #include #include #include #include namespace Kernel { static Singleton s_the; static Atomic s_jail_id; UNMAP_AFTER_INIT JailManagement::JailManagement() = default; JailIndex JailManagement::generate_jail_id() { return s_jail_id.fetch_add(1); } JailManagement& JailManagement::the() { return *s_the; } LockRefPtr JailManagement::find_jail_by_index(JailIndex index) { return m_jails.with([&](auto& list) -> LockRefPtr { for (auto& jail : list) { if (jail.index() == index) return jail; } return {}; }); } ErrorOr JailManagement::for_each_in_same_jail(Function(Jail&)> callback) { return Process::current().jail().with([&](auto const& my_jail) -> ErrorOr { // Note: If we are in a jail, don't reveal anything about the outside world, // not even the fact that we are in which jail... if (my_jail) return {}; return m_jails.with([&](auto& list) -> ErrorOr { for (auto& jail : list) { TRY(callback(jail)); } return {}; }); }); } LockRefPtr JailManagement::find_first_jail_by_name(StringView name) { return m_jails.with([&](auto& list) -> LockRefPtr { for (auto& jail : list) { if (jail.name() == name) return jail; } return {}; }); } ErrorOr> JailManagement::create_jail(NonnullOwnPtr name) { return m_jails.with([&](auto& list) -> ErrorOr> { auto jail = TRY(Jail::create({}, move(name), generate_jail_id())); list.append(jail); return jail; }); } }