interplane.Get Omega
Read in your language

spark-supervisor

Process supervisor and health monitor for sovereign daemons

Brought by drakestapleton

branch main Branches Tags History

spark-supervisor / om / limits.om

264 lines · 3644 bytes Raw Blame History
1note Direct cgroup v2 user delegation. No systemd command is executed.
2note The configured root must be a private child of a writable delegated domain.
3
4fn cgrootpath to void
5pset
6for j ulong ulong get cgData 0
7pb get cgRoot j
8end
9end
10
11fn cgleafpath to void
12pset
13for j ulong ulong get cgData 1
14pb get cgLeaf j
15end
16end
17
18fn cgleafsave to void
19put cgData 1 long plen
20for j ulong plen
21put cgLeaf j get pth j
22end
23end
24
25note Write all bytes, then verify an exact numeric readback (kernel adds newline).
26fn cgWriteValue value ulong to long
27fd = popen 655361 0
28if lt fd 0
29ret fd
30end
31put gu GOB 0
32onum long value
33n = get gu GOB
34position = ulong 0
35while lt position n bound 4096
36w = write int fd ob position sub n position
37if le w 0
38c = fclose int fd
39ret -5
40end
41position = add position ulong w
42end
43c = fclose int fd
44fd = popen ORDO 0
45if lt fd 0
46ret fd
47end
48r = read int fd ebuf 0 len ebuf
49c = fclose int fd
50if lt r long n
51ret -5
52end
53for j ulong n
54if ne get ob j get ebuf j
55ret -5
56end
57end
58if eq r long add n 1
59if ne get ebuf n 10
60ret -5
61end
62else
63if ne r long n
64ret -5
65end
66end
67put gu GOB 0
68ret 0
69end
70
71fn cgSet key view uchar * value ulong to long
72cgleafpath
73pstr key
74ret cgWriteValue value
75end
76
77fn cgMove pid ulong to long
78ret cgSet cgProcs pid
79end
80
81note cgroup.procs readback may contain other PIDs; enter is checked by /proc/self/cgroup too.
82fn cgEnter to long
83cgleafpath
84pstr cgProcs
85fd = popen 655361 0
86if lt fd 0
87ret fd
88end
89put gu GOB 0
90onum getpid
91w = write int fd ob 0 get gu GOB
92n = get gu GOB
93put gu GOB 0
94c = fclose int fd
95if ne w long n
96ret -5
97end
98ret 0
99end
100
101fn cgInitialize to void
102put cgData 2 -2
103n = envget cgEnv
104if le n 0
105ret
106end
107if ge n 4000
108put cgData 2 -36
109ret
110end
111if ne get av 0 47
112put cgData 2 -22
113ret
114end
115for j ulong ulong n
116put cgRoot j get av j
117end
118put cgData 0 n
119cgrootpath
120r = makedir pth 0 plen 448
121if and lt r 0 ne r -17
122put cgData 2 r
123ret
124end
125cgrootpath
126pstr cgManager
127r = makedir pth 0 plen 448
128if and lt r 0 ne r -17
129put cgData 2 r
130ret
131end
132cgleafsave
133r = cgEnter
134if lt r 0
135put cgData 2 r
136ret
137end
138cgrootpath
139pstr cgControl
140fd = popen 655361 0
141if lt fd 0
142put cgData 2 fd
143ret
144end
145w = write int fd cgControllers 0 len cgControllers
146c = fclose int fd
147if ne w long len cgControllers
148put cgData 2 -5
149ret
150end
151put cgData 2 0
152end
153
154fn cgPrepare memory ulong tasks ulong weight ulong to long
155if lt get cgData 2 0
156ret get cgData 2
157end
158cgleafpath
159r = makedir pth 0 plen 448
160if lt r 0
161ret r
162end
163if gt memory 0
164r = cgSet cgMemory memory
165if lt r 0
166ret r
167end
168r = cgSet cgSwap 0
169if lt r 0
170ret r
171end
172r = cgSet cgOomGroup 1
173if lt r 0
174ret r
175end
176end
177if gt tasks 0
178r = cgSet cgPids tasks
179if lt r 0
180ret r
181end
182end
183if gt weight 0
184r = cgSet cgWeight weight
185if lt r 0
186ret r
187end
188end
189ret 0
190end
191
192fn cgServicePath i ulong to void
193cgrootpath
194pstr cgSvc
195pname i
196cgleafsave
197end
198
199fn cgServicePrepare i ulong to long
200if not cgLimited i
201ret 0
202end
203cgServicePath i
204cgRemove
205cgServicePath i
206ret cgPrepare get cgMem i get cgTasks i get cgCpu i
207end
208
209fn cgLimited i ulong to bool
210ret or or gt get cgMem i 0 gt get cgTasks i 0 gt get cgCpu i 0
211end
212
213fn cgServiceEnter i ulong to long
214if not cgLimited i
215ret 0
216end
217cgServicePath i
218ret cgEnter
219end
220
221fn cgKillLeaf to void
222cgleafpath
223pstr cgKill
224fd = popen 655361 0
225if ge fd 0
226w = write int fd cgOne 0 len cgOne
227c = fclose int fd
228end
229end
230
231fn cgRemove to void
232cgleafpath
233r = rmdir pth 0 plen
234end
235
236fn cgOomCount to ulong
237cgleafpath
238pstr cgEvents
239r = rdfile
240if lt r 0
241ret 0
242end
243p = ulong 0
244while lt p get gu GSF bound 999999
245p = lnext p
246if wis 0 cgOomKey
247v = wnum 1
248if ge v 0
249ret ulong v
250end
251end
252end
253ret 0
254end
255
256fn cgServiceExit i ulong to void
257if not cgLimited i
258ret
259end
260cgServicePath i
261put cgOom i cgOomCount
262cgKillLeaf
263cgRemove
264end