spark-supervisor
Process supervisor and health monitor for sovereign daemons
Brought by drakestapleton
spark-supervisor / om / watch.om
| 1 | note aien-watch DIR SUFFIX PERIOD TIMEOUT PROG [ARGS...]: path trigger for aien-supervise (replaces |
| 2 | note systemd .path units). Runs inside the service's sandbox as the service user. Every PERIOD |
| 3 | note seconds it lists DIR; when DIR holds a non-hidden entry ending in SUFFIX ("*" = any name) it |
| 4 | note runs PROG once with this process's environment (through /usr/bin/env -i, since exec in Omega |
| 5 | note passes none) and waits for it; TIMEOUT seconds (0 = none) later a run still going gets |
| 6 | note SIGTERM, 5 s after that SIGKILL (TimeoutStartSec). It runs again when the set of matching |
| 7 | note names changes, after a failure (backoff PERIOD*2^k up to 300 s), and at least hourly while |
| 8 | note matches remain. An unreadable DIR ends it with status 1, so the supervisor restarts it. |
| 9 | text tenv "/usr/bin/env" |
| 10 | text tdi "-i" |
| 11 | text tstar "*" |
| 12 | text wpre "aien-watch: " |
| 13 | text wusage "usage: aien-watch DIR SUFFIX PERIOD TIMEOUT PROG [ARGS...]" |
| 14 | text wbadp "bad PERIOD or TIMEOUT" |
| 15 | text wlist "cannot list " |
| 16 | text wrun "ran for " |
| 17 | text wmat " matching entries, status " |
| 18 | text wtoo "arguments and environment do not fit one exec packet" |
| 19 | text wtmo "run timed out, sending SIGTERM\n" |
| 20 | global sfx uchar 256 |
| 21 | global wv ulong 16 |
| 22 | global st4 uchar 4 |
| 23 | const WSFX ulong 0 |
| 24 | const WSIG ulong 1 |
| 25 | const WRC ulong 2 |
| 26 | const WLAST ulong 3 |
| 27 | const WFAIL ulong 4 |
| 28 | const WPER ulong 5 |
| 29 | const WTMO ulong 6 |
| 30 | |
| 31 | fn wmsg t view uchar * to void |
| 32 | ostr wpre |
| 33 | ostr t |
| 34 | end |
| 35 | |
| 36 | fn match k ulong to bool |
| 37 | if eq dnch k 0 46 |
| 38 | ret false |
| 39 | end |
| 40 | n = get wv WSFX |
| 41 | if and eq n 1 eq get sfx 0 42 |
| 42 | ret true |
| 43 | end |
| 44 | m = get dnl k |
| 45 | if lt m n |
| 46 | ret false |
| 47 | end |
| 48 | o = sub add get dno k m n |
| 49 | for i ulong n |
| 50 | if ne get dnb add o i get sfx i |
| 51 | ret false |
| 52 | end |
| 53 | end |
| 54 | ret true |
| 55 | end |
| 56 | |
| 57 | note Order-independent signature of the matching names: count in the high bits plus the sum |
| 58 | note of each name's FNV-1a hash. |
| 59 | fn nhash k ulong to ulong |
| 60 | h = ulong 14695981039346656037 |
| 61 | for i ulong get dnl k |
| 62 | h = xor h ulong dnch k i |
| 63 | h = mul h 1099511628211 |
| 64 | end |
| 65 | ret h |
| 66 | end |
| 67 | |
| 68 | fn signat to ulong |
| 69 | s = ulong 0 |
| 70 | c = ulong 0 |
| 71 | for k ulong get gu GDC |
| 72 | if match k |
| 73 | s = add s nhash k |
| 74 | c = add c 1 |
| 75 | end |
| 76 | end |
| 77 | put gu GSV c |
| 78 | ret add s shl c 48 |
| 79 | end |
| 80 | |
| 81 | fn dirpth to void |
| 82 | pset |
| 83 | n = arg 1 |
| 84 | pav n |
| 85 | end |
| 86 | |
| 87 | note Packet: /usr/bin/env -i <each environment entry> PROG ARGS... (false when it does not fit). |
| 88 | fn buildpk to bool |
| 89 | pkreset |
| 90 | pkstr tenv |
| 91 | pkstr tdi |
| 92 | c = ulong envcnt |
| 93 | for j ulong c |
| 94 | r = envread j av 0 len av |
| 95 | if lt r 0 |
| 96 | ret false |
| 97 | end |
| 98 | if not pkroom ulong r |
| 99 | ret false |
| 100 | end |
| 101 | pkav ulong r |
| 102 | end |
| 103 | a = ulong argcnt |
| 104 | for i ulong sub a 5 |
| 105 | n = arg add i 5 |
| 106 | if not pkroom n |
| 107 | ret false |
| 108 | end |
| 109 | pkav n |
| 110 | end |
| 111 | ret true |
| 112 | end |
| 113 | |
| 114 | fn decode to long |
| 115 | v = or ulong get st4 0 shl ulong get st4 1 8 |
| 116 | if ne and v 127 0 |
| 117 | ret long add 128 and v 127 |
| 118 | end |
| 119 | ret long shr v 8 |
| 120 | end |
| 121 | |
| 122 | fn runone to long |
| 123 | pid = fork |
| 124 | if lt pid 0 |
| 125 | ret -1 |
| 126 | end |
| 127 | if eq pid 0 |
| 128 | r = execprog pk 0 get gu GPK |
| 129 | exit 127 |
| 130 | end |
| 131 | t0 = monons |
| 132 | tmo = mul long get wv WTMO 1000000000 |
| 133 | sent = ulong 0 |
| 134 | for outer ulong 1000000 |
| 135 | for inner ulong 1000000 |
| 136 | r = waitpid int pid st4 0 4 1 |
| 137 | if eq r pid |
| 138 | ret decode |
| 139 | end |
| 140 | if lt r 0 |
| 141 | ret -1 |
| 142 | end |
| 143 | el = sub monons t0 |
| 144 | if and gt tmo 0 and eq sent 0 ge el tmo |
| 145 | wmsg wtmo |
| 146 | oflush 2 |
| 147 | k = killpid int pid 15 |
| 148 | sent = 1 |
| 149 | end |
| 150 | if and eq sent 1 ge el add tmo 5000000000 |
| 151 | k = killpid int pid 9 |
| 152 | sent = 2 |
| 153 | end |
| 154 | s = sleepns 100000000 |
| 155 | end |
| 156 | end |
| 157 | ret -1 |
| 158 | end |
| 159 | |
| 160 | fn report c ulong rc long to void |
| 161 | wmsg wrun |
| 162 | onum long c |
| 163 | ostr wmat |
| 164 | onum rc |
| 165 | ostr tnl |
| 166 | oflush 2 |
| 167 | end |
| 168 | |
| 169 | fn pause1 to void |
| 170 | p = get wv WPER |
| 171 | f = get wv WFAIL |
| 172 | if gt f 0 |
| 173 | s = shl p f |
| 174 | if gt s 300 |
| 175 | s = 300 |
| 176 | end |
| 177 | p = s |
| 178 | end |
| 179 | r = sleepns mul long p 1000000000 |
| 180 | end |
| 181 | |
| 182 | fn numarg i ulong lo long hi long to long |
| 183 | n = arg i |
| 184 | if eq n 0 |
| 185 | ret -1 |
| 186 | end |
| 187 | p = long 0 |
| 188 | for j ulong n |
| 189 | c = get av j |
| 190 | if or or lt c 48 gt c 57 gt p 100000000 |
| 191 | ret -1 |
| 192 | end |
| 193 | p = add mul p 10 long sub c 48 |
| 194 | end |
| 195 | if or lt p lo gt p hi |
| 196 | ret -1 |
| 197 | end |
| 198 | ret p |
| 199 | end |
| 200 | |
| 201 | fn main to int |
| 202 | if lt argcnt 6 |
| 203 | wmsg wusage |
| 204 | ostr tnl |
| 205 | oflush 2 |
| 206 | ret 2 |
| 207 | end |
| 208 | n = arg 2 |
| 209 | if or eq n 0 gt n 256 |
| 210 | wmsg wusage |
| 211 | ostr tnl |
| 212 | oflush 2 |
| 213 | ret 2 |
| 214 | end |
| 215 | for i ulong n |
| 216 | put sfx i get av i |
| 217 | end |
| 218 | put wv WSFX n |
| 219 | p = numarg 3 1 86400 |
| 220 | t = numarg 4 0 86400 |
| 221 | if or lt p 0 lt t 0 |
| 222 | wmsg wbadp |
| 223 | ostr tnl |
| 224 | oflush 2 |
| 225 | ret 2 |
| 226 | end |
| 227 | put wv WPER ulong p |
| 228 | put wv WTMO ulong t |
| 229 | if not buildpk |
| 230 | wmsg wtoo |
| 231 | ostr tnl |
| 232 | oflush 2 |
| 233 | ret 2 |
| 234 | end |
| 235 | put wv WSIG 0 |
| 236 | put wv WRC 0 |
| 237 | put wv WLAST 0 |
| 238 | put wv WFAIL 0 |
| 239 | for outer ulong 1000000 |
| 240 | for inner ulong 1000000 |
| 241 | r = wstep |
| 242 | if ne r 0 |
| 243 | ret int r |
| 244 | end |
| 245 | end |
| 246 | end |
| 247 | ret 0 |
| 248 | end |
| 249 | |
| 250 | fn wstep to long |
| 251 | dirpth |
| 252 | r = lsdir |
| 253 | if lt r 0 |
| 254 | wmsg wlist |
| 255 | dirpth |
| 256 | opth |
| 257 | ostr tnl |
| 258 | oflush 2 |
| 259 | ret 1 |
| 260 | end |
| 261 | s = signat |
| 262 | c = get gu GSV |
| 263 | now = ulong div monons 1000000000 |
| 264 | due = or ne s get wv WSIG ne get wv WRC 0 |
| 265 | if ge sub now get wv WLAST 3600 |
| 266 | due = true |
| 267 | end |
| 268 | if and gt c 0 due |
| 269 | rc = runone |
| 270 | report c rc |
| 271 | put wv WSIG s |
| 272 | put wv WRC ulong rc |
| 273 | put wv WLAST now |
| 274 | if eq rc 0 |
| 275 | put wv WFAIL 0 |
| 276 | else |
| 277 | if lt get wv WFAIL 9 |
| 278 | put wv WFAIL add get wv WFAIL 1 |
| 279 | end |
| 280 | end |
| 281 | end |
| 282 | pause1 |
| 283 | ret 0 |
| 284 | end |