spark-supervisor
Process supervisor and health monitor for sovereign daemons
Brought by drakestapleton
spark-supervisor / om / services.om
| 1 | note Additional user-service contract. Dependency names are resolved after the initial scan. |
| 2 | |
| 3 | fn sxDefaults i ulong to void |
| 4 | put sxDepCount i 0 |
| 5 | put sxPort i 0 |
| 6 | put sxHealthLen i 0 |
| 7 | put cgMem i 0 |
| 8 | put cgTasks i 0 |
| 9 | put cgCpu i 0 |
| 10 | put sxNice i 0 |
| 11 | end |
| 12 | |
| 13 | fn sxKey to bool |
| 14 | ret or or or or or wis 0 sxAfter wis 0 sxHealth wis 0 sxMemory wis 0 sxTasks wis 0 sxCpu wis 0 knice |
| 15 | end |
| 16 | |
| 17 | fn sxApply i ulong to bool |
| 18 | if wis 0 sxAfter |
| 19 | if lt nw 2 |
| 20 | ret false |
| 21 | end |
| 22 | for k ulong sub nw 1 |
| 23 | if not sxAddDependency i add k 1 |
| 24 | ret false |
| 25 | end |
| 26 | end |
| 27 | ret true |
| 28 | end |
| 29 | if wis 0 sxHealth |
| 30 | if ne nw 3 |
| 31 | ret false |
| 32 | end |
| 33 | port = wnum 1 |
| 34 | if or lt port 1 gt port 65535 |
| 35 | ret false |
| 36 | end |
| 37 | n = wlen 2 |
| 38 | if or eq n 0 ge n 4096 |
| 39 | ret false |
| 40 | end |
| 41 | if ne wch 2 0 47 |
| 42 | ret false |
| 43 | end |
| 44 | put sxPort i ulong port |
| 45 | put sxHealthLen i n |
| 46 | for j ulong n |
| 47 | c = wch 2 j |
| 48 | if or lt c 33 gt c 126 |
| 49 | ret false |
| 50 | end |
| 51 | put sxHealthPath add mul i 4096 j c |
| 52 | end |
| 53 | ret true |
| 54 | end |
| 55 | if ne nw 2 |
| 56 | ret false |
| 57 | end |
| 58 | v = wnum 1 |
| 59 | if wis 0 knice |
| 60 | if or lt v 0 gt v 19 |
| 61 | ret false |
| 62 | end |
| 63 | put sxNice i v |
| 64 | ret true |
| 65 | end |
| 66 | if le v 0 |
| 67 | ret false |
| 68 | end |
| 69 | if wis 0 sxMemory |
| 70 | put cgMem i ulong v |
| 71 | ret true |
| 72 | end |
| 73 | if wis 0 sxTasks |
| 74 | put cgTasks i ulong v |
| 75 | ret true |
| 76 | end |
| 77 | if wis 0 sxCpu |
| 78 | if gt v 10000 |
| 79 | ret false |
| 80 | end |
| 81 | put cgCpu i ulong v |
| 82 | ret true |
| 83 | end |
| 84 | ret false |
| 85 | end |
| 86 | |
| 87 | fn sxNamesEqual slot ulong other ulong to bool |
| 88 | for j ulong get snl other |
| 89 | leftbyte = get snm add mul other 32 j |
| 90 | rightbyte = get sxDepNames add mul slot 32 j |
| 91 | if ne leftbyte rightbyte |
| 92 | ret false |
| 93 | end |
| 94 | end |
| 95 | ret true |
| 96 | end |
| 97 | |
| 98 | fn sxDepIndex i ulong k ulong to long |
| 99 | slot = add mul i 48 k |
| 100 | for other ulong get sv VN |
| 101 | if eq get snl other get sxDepLens slot |
| 102 | if sxNamesEqual slot other |
| 103 | ret long other |
| 104 | end |
| 105 | end |
| 106 | end |
| 107 | ret -1 |
| 108 | end |
| 109 | |
| 110 | fn sxCycle i ulong target ulong depth ulong to bool |
| 111 | if ge depth get sv VN |
| 112 | ret true |
| 113 | end |
| 114 | for k ulong get sxDepCount i |
| 115 | other = sxDepIndex i k |
| 116 | if lt other 0 |
| 117 | ret true |
| 118 | end |
| 119 | if eq ulong other target |
| 120 | ret true |
| 121 | end |
| 122 | if sxCycle ulong other target add depth 1 |
| 123 | ret true |
| 124 | end |
| 125 | end |
| 126 | ret false |
| 127 | end |
| 128 | |
| 129 | fn sxReady i ulong to bool |
| 130 | if sxCycle i i 0 |
| 131 | put sxReasonCode i 2 |
| 132 | setst i 7 |
| 133 | ret false |
| 134 | end |
| 135 | for k ulong get sxDepCount i |
| 136 | other = sxDepIndex i k |
| 137 | if lt other 0 |
| 138 | ret false |
| 139 | end |
| 140 | if or ne get sst ulong other 1 eq get sxHealthy ulong other 0 |
| 141 | ret false |
| 142 | end |
| 143 | end |
| 144 | ret true |
| 145 | end |
| 146 | |
| 147 | fn sxHealthSocket i ulong to long |
| 148 | address = new uchar 16 0 |
| 149 | put address 0 2 |
| 150 | port = get sxPort i |
| 151 | put address 2 uchar shr port 8 |
| 152 | put address 3 uchar and port 255 |
| 153 | put address 4 127 |
| 154 | put address 7 1 |
| 155 | fd = socknew 2 526337 0 |
| 156 | if lt fd 0 |
| 157 | ret fd |
| 158 | end |
| 159 | r = connect int fd address 0 16 |
| 160 | if and lt r 0 ne r -115 |
| 161 | c = fclose int fd |
| 162 | ret r |
| 163 | end |
| 164 | r = waitio int fd 4 100000000 |
| 165 | if le r 0 |
| 166 | c = fclose int fd |
| 167 | ret -110 |
| 168 | end |
| 169 | ret fd |
| 170 | end |
| 171 | |
| 172 | fn sxHealthRequest i ulong req edit uchar 8192 to ulong |
| 173 | put gu GOB 0 |
| 174 | ostr sxHttpPrefix |
| 175 | for j ulong get sxHealthLen i |
| 176 | ob1 get sxHealthPath add mul i 4096 j |
| 177 | end |
| 178 | for j ulong len sxHttpTail |
| 179 | if eq get sxHttpTail j 10 |
| 180 | ob1 13 |
| 181 | end |
| 182 | ob1 get sxHttpTail j |
| 183 | end |
| 184 | n = get gu GOB |
| 185 | for j ulong n |
| 186 | put req j get ob j |
| 187 | end |
| 188 | put gu GOB 0 |
| 189 | ret n |
| 190 | end |
| 191 | |
| 192 | fn sxHttp i ulong to bool |
| 193 | response = new uchar 256 0 |
| 194 | req = new uchar 8192 0 |
| 195 | fd = sxHealthSocket i |
| 196 | if lt fd 0 |
| 197 | ret false |
| 198 | end |
| 199 | n = sxHealthRequest i req |
| 200 | position = ulong 0 |
| 201 | while lt position n bound 8192 |
| 202 | r = send int fd req position sub n position 0 |
| 203 | if le r 0 |
| 204 | c = fclose int fd |
| 205 | ret false |
| 206 | end |
| 207 | position = add position ulong r |
| 208 | end |
| 209 | position = 0 |
| 210 | deadline = add monons 200000000 |
| 211 | while lt position 12 bound 256 |
| 212 | remaining = sub deadline monons |
| 213 | if le remaining 0 |
| 214 | c = fclose int fd |
| 215 | ret false |
| 216 | end |
| 217 | r = waitio int fd 1 remaining |
| 218 | if le r 0 |
| 219 | c = fclose int fd |
| 220 | ret false |
| 221 | end |
| 222 | r = recv int fd response position sub len response position 0 |
| 223 | if le r 0 |
| 224 | c = fclose int fd |
| 225 | ret false |
| 226 | end |
| 227 | position = add position ulong r |
| 228 | end |
| 229 | c = fclose int fd |
| 230 | if ne get response 0 72 |
| 231 | ret false |
| 232 | end |
| 233 | if ne get response 1 84 |
| 234 | ret false |
| 235 | end |
| 236 | if ne get response 2 84 |
| 237 | ret false |
| 238 | end |
| 239 | if ne get response 3 80 |
| 240 | ret false |
| 241 | end |
| 242 | ret and eq get response 8 32 eq get response 9 50 |
| 243 | end |
| 244 | |
| 245 | fn sxSetHealth i ulong value ulong to void |
| 246 | if ne get sxHealthy i value |
| 247 | put sxHealthy i value |
| 248 | put sv VBEAT 1 |
| 249 | end |
| 250 | end |
| 251 | |
| 252 | fn sxUpdateHealth i ulong to void |
| 253 | if or ne get sst i 1 le get spid i 0 |
| 254 | sxSetHealth i 0 |
| 255 | ret |
| 256 | end |
| 257 | if ne killpid int get spid i 0 0 |
| 258 | sxSetHealth i 0 |
| 259 | ret |
| 260 | end |
| 261 | if gt get sxPort i 0 |
| 262 | if not sxHttp i |
| 263 | sxSetHealth i 0 |
| 264 | ret |
| 265 | end |
| 266 | end |
| 267 | sxSetHealth i 1 |
| 268 | end |
| 269 | |
| 270 | fn sxHasLiveDependent target ulong to bool |
| 271 | for i ulong get sv VN |
| 272 | if gt get spid i 0 |
| 273 | for k ulong get sxDepCount i |
| 274 | if eq sxDepIndex i k long target |
| 275 | ret true |
| 276 | end |
| 277 | end |
| 278 | end |
| 279 | end |
| 280 | ret false |
| 281 | end |
| 282 | |
| 283 | fn sxShutdownStep to void |
| 284 | if eq get sv VEXIT 0 |
| 285 | ret |
| 286 | end |
| 287 | for k ulong get sv VN |
| 288 | i = sub sub get sv VN k 1 |
| 289 | put sdn i 1 |
| 290 | if not sxHasLiveDependent i |
| 291 | stopsvc i |
| 292 | end |
| 293 | end |
| 294 | end |
| 295 | |
| 296 | fn sxStatus i ulong to void |
| 297 | ostr sxHealthField |
| 298 | onum long get sxHealthy i |
| 299 | ostr sxTrigger |
| 300 | if eq get sxReasonCode i 1 |
| 301 | ostr sxReason |
| 302 | ostr sxLimit |
| 303 | onum get cgWhy i |
| 304 | end |
| 305 | if eq get sxReasonCode i 2 |
| 306 | ostr sxReason |
| 307 | ostr sxDependency |
| 308 | end |
| 309 | if gt get cgOom i 0 |
| 310 | ostr tsp |
| 311 | ostr sxOomStatus |
| 312 | end |
| 313 | end |
| 314 | |
| 315 | fn sxExec i ulong to long |
| 316 | environment = new uchar 4096 0 |
| 317 | n = ulong 2 |
| 318 | count = ulong 0 |
| 319 | p = ulong 0 |
| 320 | while lt p get gu GSF bound 999999 |
| 321 | p = lnext p |
| 322 | if and ge nw 2 wis 0 kenv |
| 323 | for k ulong sub nw 1 |
| 324 | wordidx = add k 1 |
| 325 | wlenv = wlen wordidx |
| 326 | if gt add add n wlenv 2 len environment |
| 327 | ret -7 |
| 328 | end |
| 329 | put environment n uchar and wlenv 255 |
| 330 | put environment add n 1 uchar shr wlenv 8 |
| 331 | n = add n 2 |
| 332 | for j ulong wlenv |
| 333 | put environment n wch wordidx j |
| 334 | n = add n 1 |
| 335 | end |
| 336 | count = add count 1 |
| 337 | end |
| 338 | end |
| 339 | end |
| 340 | put environment 0 uchar and count 255 |
| 341 | put environment 1 uchar shr count 8 |
| 342 | if eq count 0 |
| 343 | ret execprog pk 0 get gu GPK |
| 344 | end |
| 345 | ret execenv pk 0 get gu GPK environment 0 n |
| 346 | end |
| 347 | |
| 348 | fn sxUserPacket i ulong to bool |
| 349 | pkreset |
| 350 | p = ulong 0 |
| 351 | while lt p get gu GSF bound 999999 |
| 352 | p = lnext p |
| 353 | if and gt nw 0 wis 0 kwatch |
| 354 | note Legacy watcher is a separate seeded Omega executable, not a shell command. |
| 355 | pkstr twatch |
| 356 | for k ulong sub nw 1 |
| 357 | if not pkroom wlen add k 1 |
| 358 | ret false |
| 359 | end |
| 360 | pkword add k 1 |
| 361 | end |
| 362 | end |
| 363 | end |
| 364 | p = 0 |
| 365 | while lt p get gu GSF bound 999999 |
| 366 | p = lnext p |
| 367 | if and gt nw 0 wis 0 kexec |
| 368 | for k ulong sub nw 1 |
| 369 | if not pkroom wlen add k 1 |
| 370 | ret false |
| 371 | end |
| 372 | pkword add k 1 |
| 373 | end |
| 374 | ret true |
| 375 | end |
| 376 | end |
| 377 | ret false |
| 378 | end |
| 379 | fn sxCleanGroup i ulong to void |
| 380 | pid = get spid i |
| 381 | if gt pid 0 |
| 382 | r = killpid neg int pid 9 |
| 383 | end |
| 384 | end |
| 385 | |
| 386 | fn sxAddDependency i ulong widx ulong to bool |
| 387 | n = wlen widx |
| 388 | if not okname get ws widx get we widx |
| 389 | ret false |
| 390 | end |
| 391 | cnt = get sxDepCount i |
| 392 | if ge cnt 48 |
| 393 | ret false |
| 394 | end |
| 395 | slot = add mul i 48 cnt |
| 396 | put sxDepLens slot n |
| 397 | for j ulong n |
| 398 | b = wch widx j |
| 399 | put sxDepNames add mul slot 32 j b |
| 400 | end |
| 401 | put sxDepCount i add cnt 1 |
| 402 | ret true |
| 403 | end |
| 404 |