interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / core / hive-view / src / run.om

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