interplane.Get Omega
Read in your language

spark-connector

aien-dev/spark-connector

Brought by drakestapleton

branch main Branches Tags History

spark-connector / runner / 50cmd.om

754 lines · 14708 bytes Raw Blame History
1note runner/50cmd.om -- the commands: join, start, stop, status, set rate N, leave, once, selftest.
2note The background runner is this program: start forks, the child calls setsid and becomes the
3note supervisor (r_loop). For each cycle the supervisor forks a worker that does one job (r_job) and
4note exits; a worker that dies takes nothing else with it. There is no systemd unit and no cron:
5note after the computer restarts, `interplane-runner start` runs it again.
6
7text rtset "settings"
8text rtpid "runner.pid"
9text rtlog "log"
10text rtlog1 "log.1"
11text rtsetnew "settings.new"
12text rtpidnew "runner.pid.new"
13text rtbinr "bin/interplane-runner"
14text rtbino "bin/omc"
15text rtbinrp "bin/interplane-runner.part"
16text rtbinop "bin/omc.part"
17text rtbusy "The background runner is working on this computer; stop it first (interplane-runner stop).\n"
18text rtbin "bin"
19text rtdefh "https://interplane.io"
20text rthubk "hub "
21text rtratek "rate "
22text rtregn "register"
23text rtpubn "pub"
24text rtraten "rate"
25text rtleave "leave"
26text rtokreg "ipcc1 ok registered\n"
27text rtokleft "ipcc1 ok left\n"
28text rtdevnul "/dev/null"
29text rtself "/proc/self/exe"
30text rtproc "/proc/"
31text rtexe "/exe"
32text rtselfn "selftest"
33text rtselfs "text m \"interplane cell ok\\n\"\nfn main to int\nk = write 1 m 0 len m\nret 0\nend\n"
34text rtselfo "interplane cell ok\n"
35text rtexit0 "0"
36
37text rtusage "usage: interplane-runner join [HUB] | start | stop | status | set rate N | leave | once | selftest\n"
38text rtnohome "interplane-runner: HOME is not set to an absolute path\n"
39
40text rtj1 "\nInterplane runner: what joining does to this computer\n\nThis computer will check small public programs that people share on Interplane, and tell\nInterplane what it got, so anyone can see whether the result is the same on other machines.\n\n- It asks "
41text rtj2 " for work. Nothing from outside can connect to this computer.\n- Each check builds one small public Omega program and runs its example in a sealed box:\n no network, none of your files, no environment variables, no GPU, no other programs.\n- Each check takes at most 2 seconds of one CPU core to build and 2 to run, 128 MiB of address space (the most memory it can reserve),\n 1 MiB of output, and 3 seconds of clock time for each of the two steps. These are fixed.\n- Everything it keeps is in "
42text rtj3 " (under 10 MiB).\n- It makes a key here. The secret part never leaves this computer; Interplane only sees\n the public part, which is how your results are told apart from everyone else's.\n\nYour setting: at most "
43text rtj4 " checks an hour, so at most "
44text rtj5 " CPU-seconds an hour. Change it at any time\n(0 pauses): "
45text rtj6 "/bin/interplane-runner set rate N\n\nStop: "
46text rtj7 "/bin/interplane-runner stop\nLeave and remove everything: "
47text rtj8 "/bin/interplane-runner leave\nIt does not start by itself after this computer restarts; to go on, run\n "
48text rtj9 "/bin/interplane-runner start\n\nType yes and press Enter to join. Anything else cancels and removes what was downloaded.\n> "
49text rtjcan "Cancelled. Nothing was registered, and "
50text rtjcan2 " was removed.\n"
51text rtjok "\nJoined. This computer's key fingerprint (its public name on Interplane):\n "
52text rtjok2 "\nIts checks will be listed at "
53text rtjok3 "/community/\nThe runner is working in the background now.\n"
54text rtjalr "This computer has already joined.\n"
55text rtjcell "This computer cannot run the sealed box (it needs Linux 6.2 or newer with Landlock, on 64-bit ARM).\nNothing was registered, and the downloaded files were removed.\n"
56text rtjomc "The downloaded compiler is not the one Interplane pins (omc sha256 3ad9f10f...). Nothing was registered.\n"
57text rtjreg "Interplane did not accept the registration: "
58text rtjreg2 "Nothing is left on this computer.\n"
59text rtjkey "Could not make the key. Nothing was registered.\n"
60text rtjhub "The hub address is not valid.\n"
61text rtst1 "Interplane runner\nfolder: "
62text rtst2 "\nkey: "
63text rtst3 "\nhub: "
64text rtst4 "\nsetting: at most "
65text rtst5 " checks an hour (0 = paused)\nrunning: "
66text rtyes "yes, process "
67text rtno "no\n"
68text rtnotj "Interplane runner: this computer has not joined.\n"
69text rtstop "Stopped. Start again with: "
70text rtstop2 "/bin/interplane-runner start\n"
71text rtstrt "Running in the background, process "
72text rtstrtf "Could not start the background runner.\n"
73text rtsetok "Setting saved: at most "
74text rtsetok2 " checks an hour.\n"
75text rtsetbad "The rate must be a whole number of checks an hour (0 pauses).\n"
76text rtleft "Left Interplane. Removed "
77text rtleft2 "\nInterplane could not be told (the result list keeps the key, which can do nothing more without\nthis computer's secret part, now deleted).\n"
78text rtselok "cell self-test: pass\n"
79text rtselno "cell self-test: FAIL\n"
80
81note ---- settings: "hub <url>\nrate <n>\n"
82fn r_setsave to bool
83d = new uchar 1024 0
84u = new uchar 512 0
85n = get rgv rhubl
86for i ulong n bound 512
87put u i get rhub i
88end
89k = r_cp d 0 rthubk
90k = r_cp d k part u 0 n
91k = r_cp d k rtnl
92k = r_cp d k rtratek
93k = r_dec d k get rgv rrate
94k = r_cp d k rtnl
95x = r_path rtset
96ret r_replace part d 0 k
97end
98
99fn r_setload to bool
100d = new uchar 1024 0
101x = r_path rtset
102r = r_rd d
103if lt r 0
104ret false
105end
106n = ulong r
107e = ulong 0
108while lt e n bound 1024
109if eq get d e 10
110break
111end
112e = add e 1
113end
114if or ge e n le e len rthubk
115ret false
116end
117if not r_eq part d 0 len rthubk rthubk
118ret false
119end
120if not r_hubset part d len rthubk sub e len rthubk
121ret false
122end
123s = add e 1
124f = s
125while lt f n bound 1024
126if eq get d f 10
127break
128end
129f = add f 1
130end
131if or ge f n le sub f s len rtratek
132ret false
133end
134if not r_eq part d s len rtratek rtratek
135ret false
136end
137v = r_undec part d add s len rtratek sub sub f s len rtratek
138if eq v 18446744073709551615
139ret false
140end
141put rgv rrate v
142ret true
143end
144
145note ---- processes
146fn r_say_dir to void
147w = write 1 rhm 0 get rgv rhml
148end
149
150note r_exe: readlink of path p into o; its length or 0
151fn r_exe p view uchar * o edit uchar * to ulong
152r = readlnk p 0 len p o 0 len o
153if or le r 0 ge r long len o
154ret 0
155end
156ret ulong r
157end
158
159note r_isours: pid is a running interplane-runner from this same executable
160fn r_isours pid ulong to bool
161p = new uchar 64 0
162a = new uchar 4096 0
163b = new uchar 4096 0
164k = r_cp p 0 rtproc
165k = r_dec p k pid
166k = r_cp p k rtexe
167n = r_exe part p 0 k a
168m = r_exe rtself b
169if or eq n 0 eq m 0
170ret false
171end
172ret r_eq part a 0 n part b 0 m
173end
174
175note r_pidget: the pid in runner.pid, 0 when there is none or it is not a running runner
176fn r_pidget to ulong
177b = new uchar 32 0
178n = r_path rtpid
179r = r_rd b
180if le r 0
181ret 0
182end
183v = r_undec part b 0 ulong r
184if or eq v 0 gt v 4194304
185ret 0
186end
187if not r_isours v
188ret 0
189end
190ret v
191end
192
193fn r_stop to void
194pid = r_pidget
195if ne pid 0
196x = killpid int neg long pid 15
197for i ulong 100
198if ne killpid int pid 0 0
199break
200end
201x = sleepns 50000000
202end
203if eq killpid int pid 0 0
204x = killpid int neg long pid 9
205x = sleepns 200000000
206end
207end
208n = r_path rtpid
209x = unlink rph 0 n
210end
211
212note r_bgio: descriptors 0..2 of the background runner: /dev/null, then the log twice
213fn r_bgio to void
214fd = fopen rtdevnul 0 len rtdevnul 524290 0
215if ge fd 0
216x = dupfd int fd 0 0
217x = fclose int fd
218end
219n = r_path rtlog
220fd = fopen rph 0 n rfoapp 384
221if ge fd 0
222x = dupfd int fd 1 0
223x = dupfd int fd 2 0
224x = fclose int fd
225end
226end
227
228note r_logrot: when the log passes 1 MiB, it becomes log.1 (replacing the older one) and a new log
229note starts; the volunteer was promised a bounded folder
230fn r_logrot to void
231sb = new uchar 128 0
232r = fstat 1 sb 0 128
233if lt r 0
234ret
235end
236sz = ulong 0
237for i ulong 8
238sz = or sz shl ulong get sb add 48 i mul i 8
239end
240if le sz 1048576
241ret
242end
243a = new uchar 4100 0
244b = new uchar 4100 0
245p = new uchar 8300 0
246x = r_path rtlog
247na = r_phcp a
248x = r_path rtlog1
249nb = r_phcp b
250put p 0 2
251put p 1 0
252k = r_pk p 2 part a 0 na
253k = r_pk p k part b 0 nb
254r = renames p 0 k 0
255r_bgio
256end
257
258note r_wpid: write this process's id to runner.pid
259fn r_wpid to bool
260d = new uchar 24 0
261k = r_dec d 0 ulong getpid
262x = r_path rtpid
263ret r_replace part d 0 k
264end
265
266note r_loop: the supervisor. One worker per cycle; the wait after it follows the volunteer's rate:
267note after a counted result 3600/rate seconds, after "no work" at least 600 s, after an error a
268note backoff that doubles from 60 s to 3600 s, after a result the cell could not run (code 3) an
269note hour, with a line in the log. Rate 0 pauses (checked again every 60 s). The work folder is
270note removed when the supervisor starts and after each worker has exited (never while a worker
271note runs), so a job a stop or crash interrupted leaves nothing behind for long.
272fn r_loop to int
273st = new uchar 4 0
274back = ulong 60
275r_rmnm rtwork
276while true bound 9223372036854775807
277r_logrot
278if not r_setload
279ret 0
280end
281rate = get rgv rrate
282if eq rate 0
283x = sleepns 60000000000
284continue
285end
286pid = fork
287if eq pid 0
288c = r_job
289exit int c
290end
291c = ulong 2
292if gt pid 0
293w = waitpid int pid st 0 4 0
294s = r_u32 st 0
295if eq and s 127 0
296c = and shr s 8 255
297end
298end
299r_rmnm rtwork
300if eq c 3
301r_logt
302r_say rtlcellp
303end
304iv = div 3600 rate
305wt = iv
306if eq c 0
307back = 60
308end
309if eq c 1
310back = 60
311if lt wt 600
312wt = 600
313end
314end
315if gt c 1
316if lt wt back
317wt = back
318end
319back = mul back 2
320if gt back 3600
321back = 3600
322end
323end
324if eq c 3
325wt = 3600
326end
327x = sleepns long mul wt 1000000000
328end
329ret 0
330end
331
332note r_start: the background runner (needs the key and settings)
333fn r_start to bool
334old = r_pidget
335if ne old 0
336r_say rtstrt
337r_num old
338r_say rtnl
339ret true
340end
341pid = fork
342if lt pid 0
343ret false
344end
345if eq pid 0
346x = setsid
347r_bgio
348if not r_wpid
349exit 1
350end
351c = r_loop
352exit c
353end
354for i ulong 40
355x = sleepns 50000000
356if eq r_pidget ulong pid
357r_say rtstrt
358r_num ulong pid
359r_say rtnl
360ret true
361end
362end
363ret false
364end
365
366note ---- hub messages
367fn r_register to long
368r_mbeg rtregn
369p = new uchar 130 0
370for i ulong 130
371put p i get rpubh i
372end
373r_mline rtpubn p
374r_mtime
375r_mnum rtraten get rgv rrate
376if not r_msign
377ret -1
378end
379ret r_post
380end
381
382fn r_sayleave to long
383r_mbeg rtleave
384r_mkey
385r_mtime
386if not r_msign
387ret -1
388end
389ret r_post
390end
391
392note ---- the cell self-test: compile and run a two-line program in the cell
393fn r_selftest to bool
394h = new uchar 32 0
395x = new uchar 64 0
396if not r_jdir rtselfn
397ret false
398end
399n = r_jpath rtsrc
400ok = r_wr rtselfs rfonew 384
401n = r_jpath rtsin
402e = new uchar 1 0
403ok = and ok r_wr part e 0 0 rfonew 384
404if ok
405ok = r_check
406end
407if ok
408r_sha rtselfo h
409k = r_hex h x 0
410ok = and r_jfeq rfout x r_jfeq rfexit rtexit0
411ok = and ok eq get rgv rexl 1
412end
413r_jdel
414ret ok
415end
416
417note ---- commands
418fn r_yes to bool
419b = new uchar 64 0
420n = ulong 0
421while lt n 63 bound 64
422r = read 0 b n 1
423if le r 0
424break
425end
426if eq get b n 10
427break
428end
429n = add n 1
430end
431if ne n 3
432ret false
433end
434ret and and eq get b 0 121 eq get b 1 101 eq get b 2 115
435end
436
437note r_rmall: remove what the runner and the join line put in the state folder, by name, then the
438note folder itself (only when that leaves it empty: anything else in it stays)
439fn r_rmall to void
440r_rmnm rtkey
441r_rmnm rtset
442r_rmnm rtpid
443r_rmnm rtlog
444r_rmnm rtlog1
445r_rmbin
446r_rmnm rtwork
447r_rmnm rtsetnew
448r_rmnm rtpidnew
449n = r_path rtxnone
450x = rmdir rph 0 n
451end
452
453note r_rmbin: the two files join put in bin/ (and the partial downloads of the join line), then bin/
454fn r_rmbin to void
455r_rmnm rtbinr
456r_rmnm rtbino
457r_rmnm rtbinrp
458r_rmnm rtbinop
459n = r_path rtbin
460x = rmdir rph 0 n
461end
462
463fn r_rmnm nm view uchar * to void
464p = new uchar 4100 0
465n = r_path nm
466k = r_phcp p
467x = r_rmtree part p 0 k
468end
469
470fn c_join to int
471u = new uchar 512 0
472if r_kload
473r_say rtjalr
474ret c_status
475end
476if ge argcnt 3
477n = argread 2 u 0 400
478if le n 0
479r_say rtjhub
480ret 2
481end
482if not r_hubset part u 0 ulong n
483r_say rtjhub
484ret 2
485end
486else
487x = r_hubset rtdefh
488end
489put rgv rrate 30
490if not r_omcok
491r_say rtjomc
492r_rmall
493ret 3
494end
495if not r_selftest
496r_say rtjcell
497r_rmall
498ret 3
499end
500r_say rtj1
501w = write 1 rhub 0 get rgv rhubl
502r_say rtj2
503r_say_dir
504r_say rtj3
505r_num get rgv rrate
506r_say rtj4
507r_num mul get rgv rrate 4
508r_say rtj5
509r_say_dir
510r_say rtj6
511r_say_dir
512r_say rtj7
513r_say_dir
514r_say rtj8
515r_say_dir
516r_say rtj9
517if not r_yes
518r_say rtjcan
519r_say_dir
520r_say rtjcan2
521r_rmall
522ret 1
523end
524if not r_kgen
525r_say rtjkey
526r_rmall
527ret 4
528end
529s = r_register
530if or ne s 200 not r_rspis rtokreg
531r_say rtjreg
532if lt s 0
533r_say rtxneg
534r_num ulong neg s
535else
536r_rspline
537end
538r_say rtnl
539r_say rtjreg2
540r_rmall
541ret 4
542end
543if not r_setsave
544r_rmall
545ret 4
546end
547r_say rtjok
548w = write 1 rkid 0 64
549r_say rtjok2
550w = write 1 rhub 0 get rgv rhubl
551r_say rtjok3
552if not r_start
553r_say rtstrtf
554ret 5
555end
556ret 0
557end
558
559fn c_status to int
560if not r_kload
561r_say rtnotj
562ret 1
563end
564if not r_setload
565r_say rtnotj
566ret 1
567end
568r_say rtst1
569r_say_dir
570r_say rtst2
571w = write 1 rkid 0 64
572r_say rtst3
573w = write 1 rhub 0 get rgv rhubl
574r_say rtst4
575r_num get rgv rrate
576r_say rtst5
577pid = r_pidget
578if eq pid 0
579r_say rtno
580else
581r_say rtyes
582r_num pid
583r_say rtnl
584end
585ret 0
586end
587
588fn c_ready to bool
589if not r_kload
590r_say rtnotj
591ret false
592end
593if not r_setload
594r_say rtnotj
595ret false
596end
597ret true
598end
599
600fn c_start to int
601if not c_ready
602ret 1
603end
604if not r_start
605r_say rtstrtf
606ret 5
607end
608ret 0
609end
610
611fn c_stop to int
612r_stop
613r_say rtstop
614r_say_dir
615r_say rtstop2
616ret 0
617end
618
619fn c_set to int
620b = new uchar 32 0
621if ne argcnt 4
622r_say rtusage
623ret 2
624end
625n = argread 2 b 0 32
626if not r_eq part b 0 ulong n rtraten
627r_say rtusage
628ret 2
629end
630n = argread 3 b 0 32
631if le n 0
632r_say rtsetbad
633ret 2
634end
635v = r_undec part b 0 ulong n
636if eq v 18446744073709551615
637r_say rtsetbad
638ret 2
639end
640if not c_ready
641ret 1
642end
643put rgv rrate v
644if not r_setsave
645ret 4
646end
647s = r_register
648r_say rtsetok
649r_num v
650r_say rtsetok2
651ret 0
652end
653
654fn c_leave to int
655r_stop
656told = false
657if r_kload
658if r_setload
659s = r_sayleave
660told = and eq s 200 r_rspis rtokleft
661end
662end
663r_rmall
664r_say rtleft
665r_say_dir
666r_say rtnl
667if not told
668r_say rtleft2
669end
670ret 0
671end
672
673fn c_once to int
674if not c_ready
675ret 1
676end
677if ne r_pidget 0
678r_say rtbusy
679ret 1
680end
681ret int r_job
682end
683
684fn c_self to int
685if r_home
686if ne r_pidget 0
687r_say rtbusy
688ret 1
689end
690end
691x = r_hubset rtdefh
692if not r_omcok
693r_say rtjomc
694ret 3
695end
696if r_selftest
697r_say rtselok
698ret 0
699end
700r_say rtselno
701ret 3
702end
703
704text rcjoin "join"
705text rcstart "start"
706text rcstop "stop"
707text rcstat "status"
708text rcset "set"
709text rcleave "leave"
710text rconce "once"
711
712fn main to int
713a = new uchar 16 0
714if lt argcnt 2
715r_say rtusage
716ret 2
717end
718n = argread 1 a 0 16
719if le n 0
720r_say rtusage
721ret 2
722end
723if not r_home
724r_say rtnohome
725ret 2
726end
727m = ulong n
728if r_eq part a 0 m rcjoin
729ret c_join
730end
731if r_eq part a 0 m rcstart
732ret c_start
733end
734if r_eq part a 0 m rcstop
735ret c_stop
736end
737if r_eq part a 0 m rcstat
738ret c_status
739end
740if r_eq part a 0 m rcset
741ret c_set
742end
743if r_eq part a 0 m rcleave
744ret c_leave
745end
746if r_eq part a 0 m rconce
747ret c_once
748end
749if r_eq part a 0 m rtselfn
750ret c_self
751end
752r_say rtusage
753ret 2
754end