Serenity Operating System
1/*
2 * Copyright (c) 2022-2023, Liav A. <liavalb@hotmail.co.il>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#pragma once
8
9#include <AK/DistinctNumeric.h>
10#include <AK/Error.h>
11#include <AK/IntrusiveList.h>
12#include <AK/IntrusiveListRelaxedConst.h>
13#include <AK/OwnPtr.h>
14#include <AK/RefPtr.h>
15#include <AK/Try.h>
16#include <AK/Types.h>
17#include <Kernel/KString.h>
18#include <Kernel/Library/LockRefPtr.h>
19#include <Kernel/Locking/SpinlockProtected.h>
20
21namespace Kernel {
22
23class ProcessList;
24
25AK_TYPEDEF_DISTINCT_ORDERED_ID(u64, JailIndex);
26
27class Jail : public RefCounted<Jail> {
28
29public:
30 NonnullRefPtr<ProcessList> process_list();
31
32 static LockRefPtr<Jail> find_by_index(JailIndex);
33 static ErrorOr<NonnullLockRefPtr<Jail>> create(NonnullOwnPtr<KString> name);
34 static ErrorOr<void> for_each_when_process_is_not_jailed(Function<ErrorOr<void>(Jail const&)> callback);
35
36 StringView name() const { return m_name->view(); }
37 JailIndex index() const { return m_index; }
38
39 void detach(Badge<Process>);
40 SpinlockProtected<size_t, LockRank::None>& attach_count() { return m_attach_count; }
41
42private:
43 Jail(NonnullOwnPtr<KString>, JailIndex, NonnullRefPtr<ProcessList>);
44
45 NonnullOwnPtr<KString> m_name;
46 JailIndex const m_index;
47
48 IntrusiveListNode<Jail, NonnullLockRefPtr<Jail>> m_list_node;
49
50public:
51 using List = IntrusiveListRelaxedConst<&Jail::m_list_node>;
52
53private:
54 NonnullRefPtr<ProcessList> m_process_list;
55
56 SpinlockProtected<size_t, LockRank::None> m_attach_count { 0 };
57};
58
59}