interplane-forge
INTERPLANE Forge
Brought by drakestapleton
interplane-forge / salvage / collector.om
| 1 | note collector.om: the salvage collector as its own program. Compose om2 + pinned om/tls/sha2.om + |
| 2 | note salvage/00core.om + salvage/10verify.om + this unit. It runs outside the server's namespace, |
| 3 | note because verification needs the pinned compiler, bwrap and prlimit and the server has none of them. |
| 4 | note Environment, all required: |
| 5 | note INTERPLANE_SALVAGE_DIR the store the server uses (absolute, ending in /) |
| 6 | note INTERPLANE_SALVAGE_WORK a private scratch folder (absolute, ending in /), never inside the |
| 7 | note store: the sandboxed server can write the store, and scratch files |
| 8 | note are compiled and executed |
| 9 | note OMEGA_COMPILER absolute path of the pinned omc; slverify refuses any other digest |
| 10 | note It polls the one job file about three times a second, verifies the queued record in the |
| 11 | note sandbox, writes the receipt with the store's key and removes the job. It never creates the |
| 12 | note store or its key: until the server has made them it waits. With any argument it makes one pass |
| 13 | note and exits: 0 nothing queued, 1 a receipt was written, 2 the job ended without one, 3 no key yet. |
| 14 | note The store is input from a less trusted process: names are fixed, the job must be a record id, |
| 15 | note the record must hash to that id, every read is bounded and refuses a symlink as the file. |
| 16 | text tcenv "INTERPLANE_SALVAGE_DIR=" |
| 17 | text tcwenv "INTERPLANE_SALVAGE_WORK=" |
| 18 | text tcbad "salvage-collector: INTERPLANE_SALVAGE_DIR and INTERPLANE_SALVAGE_WORK must be absolute paths ending in /, the second outside the first\n" |
| 19 | note tcenvroot: the value of the variable whose name with = is t, as the store root or the scratch root |
| 20 | fn tcenvroot t view uchar * work bool to bool |
| 21 | b = new uchar 1024 0 |
| 22 | for i ulong ulong envcnt bound 4096 |
| 23 | n = envread i b 0 1024 |
| 24 | if ge n long len t |
| 25 | same = true |
| 26 | for j ulong len t |
| 27 | if ne get b j get t j |
| 28 | same = false |
| 29 | end |
| 30 | end |
| 31 | if same |
| 32 | if work |
| 33 | ret slwroot view part b len t sub ulong n len t |
| 34 | end |
| 35 | ret slroot view part b len t sub ulong n len t |
| 36 | end |
| 37 | end |
| 38 | end |
| 39 | ret false |
| 40 | end |
| 41 | note tcapart: the scratch root is not the store and not inside it |
| 42 | fn tcapart to bool |
| 43 | n = get glrootn 0 |
| 44 | if lt get glwrootn 0 n |
| 45 | ret true |
| 46 | end |
| 47 | for i ulong n bound 512 |
| 48 | if ne get glwroot i get glroot i |
| 49 | ret true |
| 50 | end |
| 51 | end |
| 52 | ret false |
| 53 | end |
| 54 | note tckey: the receipt key the server created, read and never written here |
| 55 | fn tckey to bool |
| 56 | b = new uchar 32 0 |
| 57 | p = new uchar 1024 0 |
| 58 | slpath tlempty tlkey |
| 59 | n = slpget p |
| 60 | r = slread view part p 0 n b |
| 61 | if ne r 32 |
| 62 | ret false |
| 63 | end |
| 64 | for i ulong 32 |
| 65 | put glkey i get b i |
| 66 | end |
| 67 | ret true |
| 68 | end |
| 69 | fn tcboot to bool |
| 70 | if not tcenvroot tcenv false |
| 71 | ret false |
| 72 | end |
| 73 | if not tcenvroot tcwenv true |
| 74 | ret false |
| 75 | end |
| 76 | ret tcapart |
| 77 | end |
| 78 | fn main to int |
| 79 | if not tcboot |
| 80 | r = write 2 tcbad 0 len tcbad |
| 81 | ret 1 |
| 82 | end |
| 83 | once = gt argcnt 1 |
| 84 | while true bound 9223372036854775807 |
| 85 | if tckey |
| 86 | c = slcollect |
| 87 | if once |
| 88 | ret int c |
| 89 | end |
| 90 | else |
| 91 | if once |
| 92 | ret 3 |
| 93 | end |
| 94 | end |
| 95 | z = sleepns 300000000 |
| 96 | end |
| 97 | ret 0 |
| 98 | end |