The Pedigree Project 0.1
PosixProcess.h
1/*
2 * Copyright (c) 2008-2014, Pedigree Developers
3 *
4 * Please see the CONTRIB file in the root of the source tree for a full
5 * list of contributors.
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20#ifndef POSIX_PROCESS_H
21#define POSIX_PROCESS_H
22
23#include "pedigree/kernel/Log.h"
24#include "pedigree/kernel/compiler.h"
25#include "pedigree/kernel/machine/TimerHandler.h"
26#include "pedigree/kernel/process/Process.h"
27#include "pedigree/kernel/processor/types.h"
28
29#include "PosixSubsystem.h"
30#include "credential-state.h"
31#include "process-accounting.h"
32
33class PosixProcess;
34class Timer;
35
37 friend class PosixProcess;
38 friend class ProcessGroupManager;
39
40 public:
41 ProcessGroup() = default;
42 virtual ~ProcessGroup();
43
44 int processGroupId = 0;
45 size_t sessionId = 0;
46 PosixProcess* Leader = nullptr;
48 SharedPointer<UserspacePid> sessionIdentity;
49
50 private:
51 ProcessGroup(const ProcessGroup&) = delete;
52 ProcessGroup& operator=(const ProcessGroup&) = delete;
53 PosixProcess* firstMember = nullptr;
54 size_t memberCount = 0;
55 ProcessGroup* registryNext = nullptr;
56 bool registered = false;
57};
58
60 public:
69
72 IntervalTimer(PosixProcess* pProcess, Mode mode = Hardware);
73 virtual ~IntervalTimer();
74
77 void setInterval(Time::Timestamp interval, Time::Timestamp* prevInterval = nullptr);
78
80 void setTimerValue(Time::Timestamp value, Time::Timestamp* prevValue = nullptr);
81
83 void setIntervalAndValue(Time::Timestamp interval, Time::Timestamp value,
84 Time::Timestamp* prevInterval = nullptr,
85 Time::Timestamp* prevValue = nullptr);
86
88 void disarm();
89
90 void getIntervalAndValue(Time::Timestamp& interval, Time::Timestamp& value);
91
93 void consumeCpuTime(Time::Timestamp absoluteTotal);
94
95 Time::Timestamp getInterval() const;
96 Time::Timestamp getValue() const;
97
98 private:
99 virtual void timer(uint64_t delta);
100
101 Time::Timestamp absoluteTotal() const;
102 bool advanceTimeLocked(Time::Timestamp absoluteTotal);
103 void armHardwareLocked();
104 void setArmedLocked(bool armed);
105 void signal();
106
107 PosixProcess* m_Process;
108 Mode m_Mode;
109 Time::Timestamp m_Value;
110 Time::Timestamp m_Interval;
111 Time::Timestamp m_LastTotal;
112 Spinlock m_Lock;
113 bool m_Armed;
114 Timer* m_pTimer;
115};
116
117class EXPORTED_PUBLIC PosixProcess : public Process {
118 friend class ProcessGroup;
119 friend class IntervalTimer;
120
121 public:
127 Leader = 0,
128
131
133 NoGroup
134 };
135
136 PosixProcess();
137
139 PosixProcess(Process* pParent, bool bCopyOnWrite = true,
140 FilesystemContextMode filesystemContext = FilesystemContextMode::Inherit,
141 bool emptyAddressSpace = false);
142 virtual ~PosixProcess();
143
148 void publish();
149
150 void setProcessGroup(ProcessGroup* newGroup);
151 void inheritProcessGroup(PosixProcess* parent);
152 ProcessGroup* getProcessGroup() const;
153 bool getProcessGroupId(size_t& groupId, const UserspacePidNamespace* space = nullptr) const;
154 void leaveProcessGroup();
155
156 void setGroupMembership(Membership type);
157 Membership getGroupMembership() const;
158
159 size_t getSessionId(const UserspacePidNamespace* space = nullptr) const;
160 bool sharesSession(const PosixProcess& other) const;
161 bool jobControlReady() const;
162 void markExecCommitted();
163 bool hasExecCommitted() const;
164 Time::Timestamp getStartTimeTicks() const {
165 return m_AccountingLifetime.started;
166 }
167 void snapshotAccountingMemory();
168 int createSession();
169 int changeProcessGroup(PosixProcess& caller, int groupId);
170
171 void setMask(uint32_t mask);
172 uint32_t getMask() const;
173
174 IntervalTimer& getRealIntervalTimer();
175 IntervalTimer& getVirtualIntervalTimer();
176 IntervalTimer& getProfileIntervalTimer();
177
178 using CredentialSnapshot = PosixCredentials::Snapshot;
179 using CredentialChange = PosixCredentials::Change;
180 using CredentialStatus = PosixCredentials::Status;
181 CredentialSnapshot snapshotCredentials() const;
182 FilesystemCredentials realFilesystemCredentials() const;
183 bool snapshotFilesystemCredentials(const Thread*, FilesystemCredentials&) const override;
184 bool installUserIdentity(User*, Group*, const uint32_t*, size_t) override;
185 CredentialStatus changeCredentials(Thread&, CredentialChange, uint32_t, uint32_t, uint32_t);
186 CredentialStatus replaceGroups(Thread&, const uint32_t*, size_t);
187 uint32_t changeFilesystemId(Thread&, bool group, uint32_t requested);
188 void setDumpable(bool);
189 void commitExecCredentials(Thread&, bool allExecutableFilesReadable);
190
191 int64_t getUserId() const final {
192 return static_cast<int64_t>(__atomic_load_n(&m_RealUserId, __ATOMIC_ACQUIRE));
193 }
194 virtual int64_t getGroupId() const;
195 virtual int64_t getEffectiveUserId() const;
196 virtual int64_t getEffectiveGroupId() const;
197 virtual void getSupplementalGroupIds(Vector<int64_t>& vec) const;
198
199 void setUserId(int64_t id) override;
200 void setGroupId(int64_t id) override;
201 void setEffectiveUserId(int64_t id) override;
202 void setEffectiveGroupId(int64_t id) override;
203 void setSupplementalGroupIds(const Vector<int64_t>& vec);
204
205 int64_t getSavedUserId() const;
206 int64_t getSavedGroupId() const;
207 void setSavedUserId(int64_t id);
208 void setSavedGroupId(int64_t id);
209
210 private:
211 void setTrustedIdentity(uint32_t CredentialSnapshot::* field, int64_t id);
212
213 // Register with other systems e.g. procfs
214 void registerProcess();
215 void unregisterProcess();
216
217 virtual void reportTimesUpdated(Time::Timestamp userTotal, Time::Timestamp total);
218 virtual void processTerminated();
219
221 PosixProcess& operator=(const PosixProcess&);
222
223 void initializeJobControl(Process* parent);
224 void publishCredentialReadCache();
225 ProcessAccountingLifetime m_AccountingLifetime;
226 size_t m_SessionId;
227 ProcessGroup* m_pProcessGroup;
228 PosixProcess* m_GroupPrevious;
229 PosixProcess* m_GroupNext;
230 bool m_ExecCommitted;
231 Membership m_GroupMembership;
232 uint32_t m_Mask;
233
234 IntervalTimer m_RealIntervalTimer;
235 IntervalTimer m_VirtualIntervalTimer;
236 IntervalTimer m_ProfileIntervalTimer;
237
238 CredentialSnapshot m_Credentials;
239 // Read-only identity syscalls need one scalar and should not copy the full
240 // credential snapshot or contend on its mutation lock.
241 volatile uint32_t m_RealUserId;
242 bool m_bRegistered;
243};
244
245#endif
Definition Group.h:32
void setInterval(Time::Timestamp interval, Time::Timestamp *prevInterval=nullptr)
@ Virtual
CPU time in user mode only.
@ Hardware
Hardware-backed timer (wall time).
@ Profile
CPU time in user and system.
void setTimerValue(Time::Timestamp value, Time::Timestamp *prevValue=nullptr)
Set the current value of the timer.
void consumeCpuTime(Time::Timestamp absoluteTotal)
virtual void timer(uint64_t delta)
void setIntervalAndValue(Time::Timestamp interval, Time::Timestamp value, Time::Timestamp *prevInterval=nullptr, Time::Timestamp *prevValue=nullptr)
Set both interval and value atomically.
int64_t getUserId() const final
virtual void reportTimesUpdated(Time::Timestamp userTotal, Time::Timestamp total)
Definition Process.h:984
virtual void processTerminated()
Definition Process.h:1007
void publish()
Definition Process.cc:944
Definition User.h:32
A vector / dynamic array.
Definition Vector.h:33