hive-view
aien-dev/hive-view
Brought by drakestapleton
| 1 | note hive-view src/run.om: run one program with an exact argument list (gpk), no shell, no PATH |
| 2 | note search, empty environment (execprog), stdin and stderr on /dev/null, stdout into gob, |
| 3 | note a monotonic deadline, SIGKILL on overrun, and reaping. File reads into gob, atomic writes. |
| 4 | fn vwchild wfd int to void |
| 5 | nul = fopen tdevnul 0 len tdevnul 2 0 |
| 6 | if lt nul 0 |
| 7 | exit 126 |
| 8 | end |
| 9 | x = dupfd int nul 0 0 |
| 10 | x = dupfd wfd 1 0 |
| 11 | x = dupfd int nul 2 0 |
| 12 | x = execprog gpk 0 get gpkl 0 |
| 13 | exit 127 |
| 14 | end |
| 15 | note vwdrain: read the pipe into gob until end of file; false on deadline or read error |
| 16 | fn vwdrain fd int dl long to bool |
| 17 | for it ulong 1000000 |
| 18 | left = sub dl long monons |
| 19 | if le left 0 |
| 20 | ret false |
| 21 | end |
| 22 | w = waitio fd 1 left |
| 23 | if lt w 0 |
| 24 | if ne w -4 |
| 25 | ret false |
| 26 | end |
| 27 | continue |
| 28 | end |
| 29 | if eq w 0 |
| 30 | continue |
| 31 | end |
| 32 | n = get gol 0 |
| 33 | if ge n len gob |
| 34 | put gotr 0 1 |
| 35 | r = read fd gjunk 0 4096 |
| 36 | if eq r 0 |
| 37 | ret true |
| 38 | end |
| 39 | if lt r 0 |
| 40 | if and ne r -4 ne r -11 |
| 41 | ret false |
| 42 | end |
| 43 | end |
| 44 | continue |
| 45 | end |
| 46 | r = read fd gob n sub len gob n |
| 47 | if eq r 0 |
| 48 | ret true |
| 49 | end |
| 50 | if lt r 0 |
| 51 | if and ne r -4 ne r -11 |
| 52 | ret false |
| 53 | end |
| 54 | continue |
| 55 | end |
| 56 | put gol 0 add n ulong r |
| 57 | end |
| 58 | ret false |
| 59 | end |
| 60 | note vwreap: wait for pid until dl, then SIGKILL and wait; returns the exit code, or -3 when signalled |
| 61 | fn vwreap pid int dl long to long |
| 62 | for it ulong 100000 |
| 63 | w = waitpid pid gwst 0 4 1 |
| 64 | if eq w long pid |
| 65 | s = or ulong get gwst 0 shl ulong get gwst 1 8 |
| 66 | if eq and s 127 0 |
| 67 | ret long and shr s 8 255 |
| 68 | end |
| 69 | ret -3 |
| 70 | end |
| 71 | if lt w 0 |
| 72 | if ne w -4 |
| 73 | ret -4 |
| 74 | end |
| 75 | continue |
| 76 | end |
| 77 | if gt long monons dl |
| 78 | x = killpid pid 9 |
| 79 | w = waitpid pid gwst 0 4 0 |
| 80 | ret -1 |
| 81 | end |
| 82 | x = sleepns 2000000 |
| 83 | end |
| 84 | ret -4 |
| 85 | end |
| 86 | note vwrun: run gpk with timeout tmo ns. Output in gob[0, gol). Returns the exit status 0..255, |
| 87 | note -1 timeout (killed), -2 could not start, -3 killed by a signal, -4 wait failure. |
| 88 | fn vwrun tmo long to long |
| 89 | put gol 0 0 |
| 90 | put goa 0 0 |
| 91 | put gotr 0 0 |
| 92 | r = pipefds gpfd 0 8 524288 |
| 93 | if lt r 0 |
| 94 | ret -2 |
| 95 | end |
| 96 | rfd = vwpfd 0 |
| 97 | wfd = vwpfd 1 |
| 98 | dl = add long monons tmo |
| 99 | pid = fork |
| 100 | if lt pid 0 |
| 101 | x = fclose rfd |
| 102 | x = fclose wfd |
| 103 | ret -2 |
| 104 | end |
| 105 | if eq pid 0 |
| 106 | vwchild wfd |
| 107 | exit 127 |
| 108 | end |
| 109 | x = fclose wfd |
| 110 | ok = vwdrain rfd dl |
| 111 | x = fclose rfd |
| 112 | if not ok |
| 113 | x = killpid int pid 9 |
| 114 | end |
| 115 | st = vwreap int pid dl |
| 116 | if not ok |
| 117 | ret -1 |
| 118 | end |
| 119 | ret st |
| 120 | end |
| 121 | note ---- file reads into gob. Path in gpth[0, gpl). Results: bytes in gob[goa, gol), or -errno. |
| 122 | fn vwfopen to long |
| 123 | ret fopen gpth 0 get gpl 0 524288 0 |
| 124 | end |
| 125 | note vwrdall: up to cap bytes from the start (also for /proc files, whose stat size is 0) |
| 126 | fn vwrdall cap ulong to long |
| 127 | put gol 0 0 |
| 128 | put goa 0 0 |
| 129 | put gotr 0 0 |
| 130 | fd = vwfopen |
| 131 | if lt fd 0 |
| 132 | ret fd |
| 133 | end |
| 134 | c = cap |
| 135 | if gt c len gob |
| 136 | c = len gob |
| 137 | end |
| 138 | for it ulong 100000 |
| 139 | n = get gol 0 |
| 140 | if ge n c |
| 141 | put gotr 0 1 |
| 142 | break |
| 143 | end |
| 144 | r = read int fd gob n sub c n |
| 145 | if le r 0 |
| 146 | break |
| 147 | end |
| 148 | put gol 0 add n ulong r |
| 149 | end |
| 150 | x = fclose int fd |
| 151 | ret long get gol 0 |
| 152 | end |
| 153 | note vwrdtail: the last cap bytes; when that starts mid-file, goa skips the partial first line |
| 154 | fn vwrdtail cap ulong to long |
| 155 | put gol 0 0 |
| 156 | put goa 0 0 |
| 157 | put gotr 0 0 |
| 158 | fd = vwfopen |
| 159 | if lt fd 0 |
| 160 | ret fd |
| 161 | end |
| 162 | r = fstat int fd gstb 0 128 |
| 163 | if lt r 0 |
| 164 | x = fclose int fd |
| 165 | ret r |
| 166 | end |
| 167 | size = vwst64 48 |
| 168 | c = cap |
| 169 | if gt c len gob |
| 170 | c = len gob |
| 171 | end |
| 172 | pos = ulong 0 |
| 173 | if gt size c |
| 174 | pos = sub size c |
| 175 | put gotr 0 1 |
| 176 | end |
| 177 | for it ulong 100000 |
| 178 | n = get gol 0 |
| 179 | if ge n c |
| 180 | break |
| 181 | end |
| 182 | k = pread int fd gob n sub c n long add pos n |
| 183 | if le k 0 |
| 184 | break |
| 185 | end |
| 186 | put gol 0 add n ulong k |
| 187 | end |
| 188 | x = fclose int fd |
| 189 | if gt pos 0 |
| 190 | vwskipl |
| 191 | end |
| 192 | ret long sub get gol 0 get goa 0 |
| 193 | end |
| 194 | fn vwskipl to void |
| 195 | n = get gol 0 |
| 196 | for i ulong n |
| 197 | if eq get gob i 10 |
| 198 | put goa 0 add i 1 |
| 199 | ret |
| 200 | end |
| 201 | end |
| 202 | put goa 0 n |
| 203 | end |
| 204 | note ---- atomic write of ghd[0, ghdl) + gsb[0, gsl) to <run>/s<slot>: write s<slot>.t, then rename |
| 205 | fn vwwgob fd int a ulong n ulong to bool |
| 206 | p = ulong 0 |
| 207 | for it ulong 100000 |
| 208 | if ge p n |
| 209 | ret true |
| 210 | end |
| 211 | w = write fd gsb add a p sub n p |
| 212 | if le w 0 |
| 213 | ret false |
| 214 | end |
| 215 | p = add p ulong w |
| 216 | end |
| 217 | ret false |
| 218 | end |
| 219 | fn vwwhd fd int to bool |
| 220 | n = get ghdl 0 |
| 221 | p = ulong 0 |
| 222 | for it ulong 1000 |
| 223 | if ge p n |
| 224 | ret true |
| 225 | end |
| 226 | w = write fd ghd p sub n p |
| 227 | if le w 0 |
| 228 | ret false |
| 229 | end |
| 230 | p = add p ulong w |
| 231 | end |
| 232 | ret false |
| 233 | end |
| 234 | fn vwpslot slot ulong to void |
| 235 | vwpb |
| 236 | vwpr |
| 237 | vwpc 47 |
| 238 | vwpc 115 |
| 239 | vwpd slot |
| 240 | end |
| 241 | fn vwwsec slot ulong to bool |
| 242 | vwpslot slot |
| 243 | ret vwwpath |
| 244 | end |
| 245 | note vwwpath: final path in gpth[0, gpl); writes gpth + ".t" then renames it over the final path |
| 246 | fn vwwpath to bool |
| 247 | n = get gpl 0 |
| 248 | vwpc 46 |
| 249 | vwpc 116 |
| 250 | fd = fopen gpth 0 get gpl 0 524865 384 |
| 251 | if lt fd 0 |
| 252 | ret false |
| 253 | end |
| 254 | ok = vwwhd int fd |
| 255 | if ok |
| 256 | ok = vwwgob int fd 0 get gsl 0 |
| 257 | end |
| 258 | x = fclose int fd |
| 259 | if not ok |
| 260 | ret false |
| 261 | end |
| 262 | vwkb |
| 263 | vwkopen |
| 264 | for i ulong add n 2 |
| 265 | vwkc get gpth i |
| 266 | end |
| 267 | vwkopen |
| 268 | for i ulong n |
| 269 | vwkc get gpth i |
| 270 | end |
| 271 | r = renames gpk 0 get gpkl 0 0 |
| 272 | ret eq r 0 |
| 273 | end |