The Pedigree Project 0.1
uts-namespace.h
1/* Copyright (c) 2026, Pedigree Developers. */
2#ifndef POSIX_UTS_NAMESPACE_H
3#define POSIX_UTS_NAMESPACE_H
4
5#include "pedigree/kernel/compiler.h"
6#include "pedigree/kernel/process/Mutex.h"
7#include "pedigree/kernel/processor/types.h"
8#include "pedigree/kernel/utilities/Pointers.h"
9#include "pedigree/kernel/utilities/SharedPointer.h"
10
11#include "user-namespace.h"
12
13class Process;
14class Thread;
18
19enum class UtsStatus { Success, NoMemory, NoSpace, Missing, Denied, Invalid };
20
21class EXPORTED_PUBLIC PosixUtsNamespace {
22 public:
23 struct Snapshot {
24 char node[65] = {};
25 char domain[65] = {};
26 };
27 PosixUtsNamespace(uint64_t identity, const Snapshot& names, bool charged,
28 const UserNamespaceRef& owner = {});
29 const UserNamespaceRef& owner() const {
30 return m_Owner;
31 }
33 uint64_t identity() const {
34 return m_Identity;
35 }
36 Snapshot snapshot() const;
37 void setName(bool domain, const char* bytes, size_t length);
38
39 private:
40 PosixUtsNamespace(const PosixUtsNamespace&) = delete;
41 PosixUtsNamespace& operator=(const PosixUtsNamespace&) = delete;
42 const UserNamespaceRef m_Owner;
43 const uint64_t m_Identity;
44 const bool m_Charged;
45 mutable Mutex m_Lock;
46 Snapshot m_Names;
47};
49
50class EXPORTED_PUBLIC PosixUtsTarget {
51 public:
54 PosixUtsTarget& operator=(const PosixUtsTarget&);
56 explicit operator bool() const;
57
58 private:
59 friend class PosixNamespaceContext;
60 friend UtsStatus posix_uts_acquire_target(const PosixUtsTarget&, UtsRef&);
63 size_t m_ExpectedTaskId = 0;
64};
65
66class EXPORTED_PUBLIC PreparedUtsThread {
67 public:
70
71 private:
72 friend class PosixNamespaceContext;
73 friend UtsStatus posix_uts_prepare_thread(const UtsRef&, bool, UniquePointer<PreparedUtsThread>&,
74 const UserNamespaceRef&);
75 PreparedUtsThread(const PreparedUtsThread&) = delete;
76 PreparedUtsThread& operator=(const PreparedUtsThread&) = delete;
78 PreparedUtsThread* m_Next = nullptr;
79};
80
81class EXPORTED_PUBLIC PosixNamespaceContext {
82 public:
85 bool valid() const;
86 void attach(Process& process);
87 bool acquireThread(const Thread& thread, UtsRef& result) const;
88 void publishThread(UniquePointer<PreparedUtsThread>& prepared, Thread& thread, bool leader);
89 void promoteExec(const Thread& thread);
90 void retireThread(const Thread& thread);
91 void close();
92 UtsStatus replaceThread(const Thread& thread, const UtsRef& replacement);
93
94 bool leaderTarget(PosixUtsTarget& result) const;
95 bool taskTarget(size_t taskId, PosixUtsTarget& result) const;
96 bool threadTarget(const Thread& thread, PosixUtsTarget& result) const;
97 bool nextTaskTarget(size_t afterTaskId, size_t& taskId, PosixUtsTarget& result) const;
98
99 private:
101 PosixNamespaceContext& operator=(const PosixNamespaceContext&) = delete;
102 friend UtsStatus posix_uts_acquire_target(const PosixUtsTarget&, UtsRef&);
104};
105
106EXPORTED_PUBLIC UtsStatus posix_uts_initial(UtsRef& result);
107EXPORTED_PUBLIC UtsStatus posix_uts_copy(const UtsRef& source, UtsRef& result,
108 const UserNamespaceRef& owner = {});
109EXPORTED_PUBLIC UtsStatus posix_uts_prepare_thread(const UtsRef& source, bool copy,
111 const UserNamespaceRef& owner = {});
112EXPORTED_PUBLIC UtsStatus posix_uts_acquire_target(const PosixUtsTarget& target, UtsRef& result);
113EXPORTED_PUBLIC int posix_uts_error(UtsStatus status);
114
115#endif
Definition Mutex.h:56