interplane.Get Omega
Read in your language

hive-view

aien-dev/hive-view

Brought by drakestapleton

branch main Branches Tags History

hive-view / tests / gate.om

1239 lines · 23748 bytes Raw Blame History
1note hive-view tests/gate.om: the acceptance gate, in pure Omega. Built like the server, with
2note src/util.om and src/run.om (no shell, exact argument lists) and this file as the entry:
3note gate run PORT drive a running server on 127.0.0.1:PORT through every check below;
4note prints one PASS/FAIL line per check, exit 0 only when all pass
5note gate cpu PID SECS CPU ticks (USER_HZ) used by server PID with its children and by the
6note tmux server over SECS seconds
7note The run mode creates its own tmux session hv-test whose only program is
8note `/usr/bin/tee -a /tmp/hv-gate-sink` on a raw (-icanon -echo) terminal, so every byte the
9note dashboard types is recorded exactly; it is deleted at the end (which is itself a check).
10global gaport ulong 1
11global gax uchar 64
12global gaxl ulong 1
13global gatg uchar 1024
14global gatgl ulong 1
15global gabd uchar 40000
16global gabdl ulong 1
17global garb uchar 262144
18global garbl ulong 1
19global galoc uchar 1024
20global galocl ulong 1
21global gack ulong 1
22global gakey uchar 64
23global gatx uchar 8192
24global gatxl ulong 1
25global gapass ulong 1
26global gafail ulong 1
27global gatk ulong 4
28text tget "GET"
29text tpost "POST"
30text tenvh "HOME="
31text ttokf "/.config/hive-view/token"
32text tlogf "/.local/state/hive-view/sends.log"
33text tsink "/tmp/hv-gate-sink"
34text ttmux "/usr/bin/tmux"
35text tstty "/usr/bin/stty"
36text ttee "/usr/bin/tee"
37text tnew "new-session"
38text tdd "-d"
39text tds "-s"
40text tdx "-x"
41text tdy "-y"
42text t120 "120"
43text t30 "30"
44text tdash "-a"
45text tkill "kill-session"
46text tdt "-t"
47text tdisp "display-message"
48text tdp "-p"
49text tptty "#{pane_tty}"
50text tpid "#{pid}"
51text tsessn "hv-test"
52text tsesst "=hv-test:"
53text tsesk "=hv-test"
54text tdf "-F"
55text ticanon "-icanon"
56text techo "-echo"
57text thost "Host"
58text tcook "Cookie"
59text torig "Origin"
60text tctype "Content-Type"
61text tform "application/x-www-form-urlencoded"
62text tckpre "hv="
63text thttp "http://"
64text tevil "http://evil.example"
65text tevilh "evil.example"
66text tlocn "location"
67text tsetck "set-cookie"
68text tphealth "/health"
69text troot "/"
70text tqt "/?t="
71text tpsend "/send"
72text tpses "/s/hv-test"
73text tpane "/p/pane?n=hv-test"
74text tpenq "/p/entries?f=questions"
75text tpend "/p/entries?f=decisions"
76text tendh "</body></html>"
77text tkpat "name=\"k\" value=\""
78text tline "line"
79text tenter "enter"
80text tescape "escape"
81text tps "s"
82text tpq "q"
83text tpass "PASS "
84text tfail "FAIL "
85text tsum "gate: "
86text tsum2 " passed, "
87text tsum3 " failed\n"
88text tsha "sha256="
89text tbyt "bytes="
90text tmark "gate-secret-marker-5c1e"
91text tescs "&lt;script&gt;alert(1)&lt;/script&gt;"
92text tscr "<script>"
93text tcpu1 "hive-view ticks "
94text tcpu2 " tmux-server ticks "
95text tcpu3 " over "
96text tcpu4 " s (USER_HZ 100)\n"
97text tstat "/stat"
98text tproc "/proc/"
99text tchild "/task/"
100text tchild2 "/children"
101note ---- the cases typed literally: each must arrive byte for byte, followed by Enter (LF)
102text c01 "semi;"
103text c02 ";"
104text c03 ";;"
105text c04 "-n"
106text c05 "--"
107text c06 "$(id) `id` 'q' \"dq\" & | > <"
108text c07 "é ü 漢字 🐝"
109text c08 "<script>alert(1)</script>"
110text c09 " leading and trailing spaces "
111text c10 "%41+%20&a=escape"
112text c11 "send-keys -t =omp-queen: Enter"
113text n01 "send ;semi"
114note ---- output
115fn gaout to void
116n = get gsl 0
117p = ulong 0
118for it ulong 1000
119if ge p n
120break
121end
122w = write 1 gsb p sub n p
123if le w 0
124break
125end
126p = add p ulong w
127end
128put gsl 0 0
129end
130fn gachk ok bool name view uchar * to void
131if ok
132vwsbt tpass
133put gapass 0 add get gapass 0 1
134else
135vwsbt tfail
136put gafail 0 add get gafail 0 1
137end
138vwsbt name
139vwsbc 10
140gaout
141end
142note ---- environment
143fn gahome to bool
144eb = new uchar 600 0
145for i ulong ulong envcnt
146n = envread i eb 0 600
147if gt n 5
148if heq view part eb 0 5 tenvh
149k = hmin sub ulong n 5 511
150for j ulong k
151put ghome j get eb add 5 j
152end
153put ghomel 0 k
154ret gt k 0
155end
156end
157end
158ret false
159end
160fn gatok to bool
161vwpb
162vwph
163vwpt ttokf
164r = vwrdall 128
165if lt r 64
166ret false
167end
168for i ulong 64
169put gtok i get gob i
170end
171ret true
172end
173fn gadec a view uchar * to ulong
174v = ulong 0
175for i ulong len a
176c = get a i
177if not vwdig c
178ret v
179end
180v = add mul v 10 ulong sub c 48
181end
182ret v
183end
184note ---- HTTP client: one connection per request. fl: 1 cookie, 2 good Origin, 4 foreign Origin,
185note 8 foreign Host, 16 bad cookie. The response body lands in garb, Location in galoc, and gack
186note is 1 when Set-Cookie carried hv=<token>.
187fn gaxbuf to void
188put gaxl 0 0
189s = new uchar 24 0
190p = ulong 0
191put s 0 49
192put s 1 50
193put s 2 55
194put s 3 46
195put s 4 48
196put s 5 46
197put s 6 48
198put s 7 46
199put s 8 49
200put s 9 58
201p = 10
202for i ulong 10
203put gax i get s i
204end
205d = new uchar 20 0
206k = ulong 0
207x = get gaport 0
208for it ulong 20
209put d k uchar add rem x 10 48
210k = add k 1
211x = div x 10
212if eq x 0
213break
214end
215end
216for i ulong k
217put gax add p i get d sub sub k 1 i
218end
219put gaxl 0 add p k
220end
221fn gahdrs cs edit ulong 320 io edit uchar 131072 fl ulong to long
222hb = new uchar 64 0
223ck = new uchar 67 0
224o = new uchar 96 0
225n = get gaxl 0
226for i ulong n
227put hb i get gax i
228end
229r = hhname cs io thost
230if ne and fl 8 0
231r = hfe r hhval cs io tevilh
232else
233r = hfe r hhval cs io view part hb 0 n
234end
235if ne and fl 17 0
236for i ulong 3
237put ck i get tckpre i
238end
239for i ulong 64
240put ck add 3 i get gtok i
241end
242if ne and fl 16 0
243put ck 3 48
244if eq get gtok 0 48
245put ck 3 49
246end
247end
248r = hfe r hhname cs io tcook
249r = hfe r hhval cs io ck
250end
251if ne and fl 2 0
252for i ulong 96
253put o i 0
254end
255for i ulong 7
256put o i get thttp i
257end
258n = get gaxl 0
259for i ulong n
260put o add 7 i get gax i
261end
262r = hfe r hhname cs io torig
263r = hfe r hhval cs io view part o 0 add 7 n
264end
265if ne and fl 4 0
266r = hfe r hhname cs io torig
267r = hfe r hhval cs io tevil
268end
269ret r
270end
271fn gawreq cs edit ulong 320 io edit uchar 131072 post bool fl ulong to long
272tg = new uchar 1024 0
273bd = new uchar 40000 0
274r = long 0
275if post
276r = hreqst cs io tpost
277else
278r = hreqst cs io tget
279end
280tn = get gatgl 0
281for i ulong tn
282put tg i get gatg i
283end
284r = hfe r hreqtg cs io view part tg 0 tn
285r = hfe r gahdrs cs io fl
286bl = ulong 0
287if post
288r = hfe r hhname cs io tctype
289r = hfe r hhval cs io tform
290bl = get gabdl 0
291end
292r = hfe r hhend cs io 0 bl
293if post
294for i ulong bl
295put bd i get gabd i
296end
297r = hfe r hput cs io view part bd 0 bl
298end
299ret hfe r hwfin cs io
300end
301fn garbody cs edit ulong 320 io edit uchar 131072 to void
302rb = new uchar 262144 0
303put cs HBCAP 262144
304n = hbodyall cs io rb
305m = ulong 0
306if gt n 0
307m = ulong n
308end
309for i ulong m
310put garb i get rb i
311end
312put garbl 0 m
313end
314fn gareq post bool fl ulong to long
315a = new uchar 16 0
316cs = new ulong 320 0
317io = new uchar 131072 0
318put garbl 0 0
319put galocl 0 0
320put gack 0 0
321r = haddr4 a uint 2130706433 get gaport 0
322fd = hdial a 3000000000
323if lt fd 0
324ret fd
325end
326r = hinit cs io int fd 0
327hsettmo cs 10000000000 10000000000
328hsetbdl cs 10000000000
329r = hfe r gawreq cs io post fl
330if lt r 0
331x = hclose cs
332ret r
333end
334s = hrsprd cs io
335if lt s 0
336x = hclose cs
337ret s
338end
339garesp cs io
340garbody cs io
341x = hclose cs
342ret s
343end
344fn garesp cs edit ulong 320 io edit uchar 131072 to void
345i = hhfind cs io tlocn
346if ge i 0
347vo = hhvoff cs ulong i
348vl = hmin hhvlen cs ulong i 1024
349for k ulong vl
350put galoc k get io add vo k
351end
352put galocl 0 vl
353end
354j = hhfind cs io tsetck
355if ge j 0
356vo = hhvoff cs ulong j
357vl = hhvlen cs ulong j
358if ge vl 67
359ok = heq view part io vo 3 tckpre
360for k ulong 64
361if ne get io add add vo 3 k get gtok k
362ok = false
363end
364end
365if ok
366put gack 0 1
367end
368end
369end
370end
371fn gatgt t view uchar * to void
372for i ulong len t
373put gatg i get t i
374end
375put gatgl 0 len t
376end
377fn gaget t view uchar * fl ulong to long
378gatgt t
379ret gareq false fl
380end
381fn hmax2 a long b long to long
382if gt a b
383ret a
384end
385ret b
386end
387fn garbat a ulong t view uchar * to bool
388if gt add a len t get garbl 0
389ret false
390end
391for i ulong len t
392if ne get garb add a i get t i
393ret false
394end
395end
396ret true
397end
398fn gaobat a ulong t view uchar * to bool
399if gt add a len t get gol 0
400ret false
401end
402for i ulong len t
403if ne get gob add a i get t i
404ret false
405end
406end
407ret true
408end
409note gahas: garb contains t
410fn gahas t view uchar * to bool
411n = get garbl 0
412m = len t
413if gt m n
414ret false
415end
416for i ulong add sub n m 1
417if garbat i t
418ret true
419end
420end
421ret false
422end
423note ---- form bodies
424fn gabc c uchar to void
425n = get gabdl 0
426if lt n len gabd
427put gabd n c
428put gabdl 0 add n 1
429end
430end
431fn gabt t view uchar * to void
432for i ulong len t
433gabc get t i
434end
435end
436note gabe: percent-encode every byte except ASCII letters and digits
437fn gabe a view uchar * to void
438for i ulong len a
439c = get a i
440if or vwdig c or and ge c 65 le c 90 and ge c 97 le c 122
441gabc c
442else
443gabc 37
444gabc get thexd ulong shr ulong c 4
445gabc get thexd and ulong c 15
446end
447end
448end
449fn gafld k uchar v view uchar * to void
450if gt get gabdl 0 0
451gabc 38
452end
453gabc k
454gabc 61
455gabe v
456end
457note gaform: s, k, r, a and t (the text is gatx[0, gatxl)); badk 1 sends a wrong key
458fn gaform sn view uchar * act view uchar * badk ulong to void
459z = new uchar 64 48
460kk = new uchar 64 0
461tx = new uchar 8192 0
462put gabdl 0 0
463gafld 115 sn
464if eq badk 1
465gafld 107 z
466else
467for i ulong 64
468put kk i get gakey i
469end
470gafld 107 kk
471end
472gafld 114 tps
473gafld 97 act
474n = get gatxl 0
475for i ulong n
476put tx i get gatx i
477end
478gafld 116 view part tx 0 n
479end
480fn gapost fl ulong to long
481gatgt tpsend
482ret gareq true fl
483end
484note galq: the number after "<c>=" in the Location query, or 999999
485fn galq c uchar to ulong
486n = get galocl 0
487for i ulong n
488if and eq get galoc i c lt add i 1 n
489if eq get galoc add i 1 61
490p = add i 2
491if or eq i 0 or eq get galoc sub i 1 63 eq get galoc sub i 1 38
492v = ulong 0
493ok = false
494for k ulong 20
495if ge add p k n
496break
497end
498d = get galoc add p k
499if not vwdig d
500break
501end
502v = add mul v 10 ulong sub d 48
503ok = true
504end
505if ok
506ret v
507end
508end
509end
510end
511end
512ret 999999
513end
514note ---- the sink file written by tee inside hv-test
515fn gasinkp to void
516vwpb
517vwpt tsink
518end
519fn gasize to ulong
520gasinkp
521fd = vwfopen
522if lt fd 0
523ret 0
524end
525r = fstat int fd gstb 0 128
526x = fclose int fd
527if lt r 0
528ret 0
529end
530ret vwst64 48
531end
532fn gawait want ulong to bool
533for it ulong 60
534if ge gasize want
535ret true
536end
537x = sleepns 50000000
538end
539ret ge gasize want
540end
541note gasink: the sink from offset off holds exactly gatx[0, gatxl) followed by the byte tail
542fn gasink off ulong tail long to bool
543gasinkp
544r = vwrdall 262144
545if lt r 0
546ret false
547end
548n = get gatxl 0
549want = n
550if ge tail 0
551want = add n 1
552end
553if ne get gol 0 add off want
554ret false
555end
556for i ulong n
557if ne get gob add off i get gatx i
558ret false
559end
560end
561if ge tail 0
562ret eq get gob add off n uchar tail
563end
564ret true
565end
566fn gasett t view uchar * to void
567for i ulong len t
568put gatx i get t i
569end
570put gatxl 0 len t
571end
572note gatype: type gatx with action act; true when the redirect reports m=0, the byte count, and the
573note sink received exactly the text plus tail (LF for a line, ESC for Escape, LF for Enter)
574fn gatype act view uchar * tail long to bool
575s0 = gasize
576gaform tsessn act 0
577st = gapass2
578if ne st 302
579ret false
580end
581nb = ulong 0
582if heq act tline
583nb = get gatxl 0
584end
585if ne galq 109 0
586ret false
587end
588if ne galq 98 nb
589ret false
590end
591want = add s0 add nb 1
592if not gawait want
593ret false
594end
595if heq act tline
596ret gasink s0 tail
597end
598gasinkp
599r = vwrdall 262144
600if ne get gol 0 want
601ret false
602end
603ret eq get gob s0 uchar tail
604end
605fn gapass2 to long
606ret gapost 3
607end
608note garef: the send is refused with code m and nothing reaches the sink
609fn garef act view uchar * m ulong to bool
610s0 = gasize
611gaform tsessn act 0
612st = gapost 3
613if ne st 302
614ret false
615end
616if ne galq 109 m
617ret false
618end
619x = sleepns 300000000
620ret eq gasize s0
621end
622note ---- tmux and the sink session
623fn gatmux to void
624vwkb
625vwka ttmux
626end
627fn gamkses to bool
628gatmux
629vwka tkill
630vwka tdt
631vwka tsesk
632r = vwrun 5000000000
633gasinkp
634u = unlink gpth 0 get gpl 0
635gatmux
636vwka tnew
637vwka tdd
638vwka tds
639vwka tsessn
640vwka tdx
641vwka t120
642vwka tdy
643vwka t30
644vwka ttee
645vwka tdash
646vwka tsink
647r = vwrun 5000000000
648if ne r 0
649ret false
650end
651gatmux
652vwka tdisp
653vwka tdp
654vwka tdt
655vwka tsesst
656vwka tptty
657r = vwrun 5000000000
658if ne r 0
659ret false
660end
661n = get gol 0
662if and gt n 0 eq get gob sub n 1 10
663n = sub n 1
664end
665if or eq n 0 gt n 200
666ret false
667end
668vwkb
669vwka tstty
670vwka tdf
671vwkopen
672for i ulong n
673vwkc get gob i
674end
675vwka ticanon
676vwka techo
677r = vwrun 5000000000
678x = sleepns 300000000
679ret eq r 0
680end
681fn gakilss to bool
682gatmux
683vwka tkill
684vwka tdt
685vwka tsesk
686r = vwrun 5000000000
687ret eq r 0
688end
689note gakey: the form key from the hv-test page
690fn gagetk to bool
691st = gaget tpses 1
692if ne st 200
693ret false
694end
695n = get garbl 0
696m = len tkpat
697if lt n add m 64
698ret false
699end
700for i ulong sub sub n m 63
701if garbat i tkpat
702for k ulong 64
703put gakey k get garb add add i m k
704end
705ret true
706end
707end
708ret false
709end
710note ---- send log: the newest line names the bytes and the sha256 of the text; the file never
711note contains the marker text
712fn galogok to bool
713dg = new uchar 32 0
714tx = new uchar 8192 0
715tn = get gatxl 0
716for i ulong tn
717put tx i get gatx i
718end
719k = sha2_digest 256 view part tx 0 tn dg
720hx = new uchar 71 0
721for i ulong 7
722put hx i get tsha i
723end
724for i ulong 32
725put hx add 7 mul i 2 get thexd ulong shr ulong get dg i 4
726put hx add 8 mul i 2 get thexd and ulong get dg i 15
727end
728vwpb
729vwph
730vwpt tlogf
731r = vwrdtail 4096
732if lt r 0
733ret false
734end
735e = get gol 0
736if eq e 0
737ret false
738end
739if eq get gob sub e 1 10
740e = sub e 1
741end
742b = e
743for it ulong 4096
744if eq b 0
745break
746end
747if eq get gob sub b 1 10
748break
749end
750b = sub b 1
751end
752ok = false
753for i ulong sub e b
754if gt add add b i 71 e
755break
756end
757if gaobat add b i hx
758ok = true
759end
760end
761ret ok
762end
763fn galogno to bool
764vwpb
765vwph
766vwpt tlogf
767r = vwrdall 262144
768n = get gol 0
769m = len tmark
770if lt n m
771ret true
772end
773for i ulong add sub n m 1
774if gaobat i tmark
775ret false
776end
777end
778ret true
779end
780text tbadact "type"
781text thc01 "health answers 200 without auth"
782text thc02 "GET / without the cookie is refused (403)"
783text thc03 "a panel without the cookie is refused (403)"
784text thc04 "a wrong cookie is refused (403)"
785text thc05 "a wrong one-time link is refused (403)"
786text thc06 "the one-time link answers 302 and sets the token cookie"
787text thc07 "a foreign Host header is refused (400)"
788text thc08 "GET /send is refused (405)"
789text thc09 "test session hv-test created (tee into the sink, raw terminal)"
790text thc10 "the form key is on the session page"
791text thc11 "POST /send without the cookie is refused (403)"
792text thc12 "POST /send without an Origin header is refused (403)"
793text thc13 "POST /send from a foreign Origin is refused (403)"
794text thc14 "POST /send with a wrong form key is refused (403)"
795text thc15 "nothing reached the session from the refused posts"
796text thc16 "literal backslash-semicolon inside the text"
797text thc17 "literal backslash-semicolon alone"
798text thc18 "text ending in backslash-semicolon"
799text thc19 "4096-byte text (the limit) with semicolons at the chunk edges"
800text thc20 "Enter button sends one Enter"
801text thc21 "Escape button sends one Escape"
802text thc22 "marker text typed"
803text thc23 "send log names the byte count and sha256 of the text"
804text thc24 "send log never contains the text"
805text thc25 "4097-byte text refused (m=7), nothing typed"
806text thc26 "control byte 0x01 refused (m=4), nothing typed"
807text thc27 "newline inside the text refused (m=4), nothing typed"
808text thc28 "DEL refused (m=4), nothing typed"
809text thc29 "invalid UTF-8 byte 0xff refused (m=4), nothing typed"
810text thc30 "overlong UTF-8 refused (m=4), nothing typed"
811text thc31 "carriage return refused (m=4), nothing typed"
812text thc32 "tab refused (m=4), nothing typed"
813text thc33 "escape byte inside the text refused (m=4), nothing typed"
814text thc34 "unknown action refused (m=3), nothing typed"
815text thc35 "malformed and forged session names refused (m=2), nothing typed"
816text thc36 "duplicate or unknown form fields refused (m=1), nothing typed"
817text thc37 "a body over 16 KiB is refused (413), nothing typed"
818text thc38 "<script> on a session screen is shown escaped, never as markup"
819text thc39 "test session hv-test deleted"
820text thc40 "typing into the deleted session is refused (m=5)"
821text thc41 "hv-test no longer exists"
822text thc44 "4096-byte text of 3-byte UTF-8 characters, one split by the 2048-byte chunk edge"
823text thc45 "Enter with text in the box presses only Enter (the text is not typed)"
824text thc46 "QUESTIONS panel answers 200 with the whole page (no \"## \" entries or no file)"
825text thc47 "DECISIONS panel answers 200 with the whole page (no \"## \" entries or no file)"
826text thc42 "with the send log unwritable the send is refused (m=8), nothing typed"
827fn gareq2 t view uchar * to long
828gatgt t
829ret gareq false 0
830end
831note gafbad: forged names (shell metacharacters, a tmux target, path, empty, too long) get m=2
832text nb1 "hv-test;ls"
833text nb2 "=omp-queen:"
834text nb3 "../hv-test"
835text nb4 "hv test"
836text nb5 "omp-queen:0.1"
837fn gafbn n view uchar * to bool
838s0 = gasize
839gasett c01
840gaform n tline 0
841st = gapost 3
842if or ne st 302 ne galq 109 2
843ret false
844end
845ret eq gasize s0
846end
847fn gafbad to bool
848e = new uchar 1 0
849l = new uchar 64 97
850ok = gafbn nb1
851ok = and ok gafbn nb2
852ok = and ok gafbn nb3
853ok = and ok gafbn nb4
854ok = and ok gafbn nb5
855ok = and ok gafbn view part e 0 0
856ok = and ok gafbn l
857ret ok
858end
859note gafdup: a repeated field, an unknown field, a field without '=', a bad percent escape
860text db1 "&s=hv-test"
861text db2 "&x=1"
862text db3 "&t"
863text db4 "&t=%zz"
864fn gafdb t view uchar * to bool
865s0 = gasize
866gasett c01
867gaform tsessn tline 0
868gabt t
869st = gapost 3
870if or ne st 302 ne galq 109 1
871ret false
872end
873ret eq gasize s0
874end
875fn gafdup to bool
876ok = gafdb db1
877ok = and ok gafdb db2
878ok = and ok gafdb db3
879ok = and ok gafdb db4
880ret ok
881end
882fn gafbig to bool
883s0 = gasize
884gasett c01
885gaform tsessn tline 0
886for i ulong 16400
887gabc 120
888end
889st = gapost 3
890x = sleepns 300000000
891ret and eq st 413 eq gasize s0
892end
893note gaents: an entries panel answers 200 with the whole page. With a HOME that has no QUESTIONS or
894note DECISIONS file (a scratch server, CI) this drives the fallback that shows the last 4000 bytes.
895fn gaents t view uchar * to bool
896st = gaget t 1
897if ne st 200
898ret false
899end
900ret gahas tendh
901end
902note gapane: type gatx (a <script> line) again so it is on the visible screen, wait for the poller
903note to capture it, then read the pane panel: the markup must arrive escaped
904fn gapane to bool
905if not gatype tline 10
906ret false
907end
908for it ulong 20
909x = sleepns 500000000
910st = gaget tpane 1
911if eq st 200
912if gahas tescs
913ret not gahas tscr
914end
915end
916end
917ret false
918end
919note ---- the run mode
920fn garun to int
921gaxbuf
922if not gatok
923ret 3
924end
925gachk eq gaget tphealth 0 200 thc01
926gachk eq gaget troot 0 403 thc02
927gachk eq gaget tpane 0 403 thc03
928gachk eq gaget troot 16 403 thc04
929z = new uchar 68 48
930for i ulong 4
931put z i get tqt i
932end
933gachk eq gareq2 z 403 thc05
934for i ulong 64
935put z add 4 i get gtok i
936end
937st = gareq2 z
938gachk and eq st 302 eq get gack 0 1 thc06
939gachk eq gaget troot 9 400 thc07
940gachk eq gaget tpsend 1 405 thc08
941gachk gamkses thc09
942gachk gagetk thc10
943gasett c01
944gaform tsessn tline 0
945gachk eq gapost 0 403 thc11
946gachk eq gapost 1 403 thc12
947gachk eq gapost 5 403 thc13
948gaform tsessn tline 1
949gachk eq gapost 3 403 thc14
950gachk eq gasize 0 thc15
951gasett c01
952gachk gatype tline 10 c01
953gasett c02
954gachk gatype tline 10 c02
955gasett c03
956gachk gatype tline 10 c03
957gasett c04
958gachk gatype tline 10 c04
959gasett c05
960gachk gatype tline 10 c05
961gasett c06
962gachk gatype tline 10 c06
963gasett c07
964gachk gatype tline 10 c07
965gasett c08
966gachk gatype tline 10 c08
967gasett c09
968gachk gatype tline 10 c09
969gasett c10
970gachk gatype tline 10 c10
971gasett c11
972gachk gatype tline 10 c11
973gasett n01
974put gatx 4 92
975gachk gatype tline 10 thc16
976put gatx 0 92
977put gatx 1 59
978put gatxl 0 2
979gachk gatype tline 10 thc17
980put gatx 0 98
981put gatx 1 115
982put gatx 2 92
983put gatx 3 59
984put gatxl 0 4
985gachk gatype tline 10 thc18
986for i ulong 4096
987put gatx i uchar add 97 rem i 26
988end
989put gatx 2047 59
990put gatx 4095 59
991put gatxl 0 4096
992gachk gatype tline 10 thc19
993put gatx 0 97
994for i ulong 1365
995put gatx add 1 mul i 3 226
996put gatx add 2 mul i 3 130
997put gatx add 3 mul i 3 172
998end
999put gatxl 0 4096
1000gachk gatype tline 10 thc44
1001put gatxl 0 0
1002gachk gatype tenter 10 thc20
1003gachk gatype tescape 27 thc21
1004gasett tmark
1005gachk gatype tline 10 thc22
1006gachk galogok thc23
1007gachk galogno thc24
1008for i ulong 4097
1009put gatx i 120
1010end
1011put gatxl 0 4097
1012gachk garef tline 7 thc25
1013gasett c01
1014put gatx 2 1
1015gachk garef tline 4 thc26
1016put gatx 2 10
1017gachk garef tline 4 thc27
1018put gatx 2 127
1019gachk garef tline 4 thc28
1020put gatx 2 255
1021gachk garef tline 4 thc29
1022put gatx 1 192
1023put gatx 2 175
1024gachk garef tline 4 thc30
1025put gatx 2 13
1026gachk garef tline 4 thc31
1027put gatx 2 9
1028gachk garef tline 4 thc32
1029put gatx 2 27
1030gachk garef tline 4 thc33
1031gasett c01
1032gachk garef tbadact 3 thc34
1033gachk gatype tenter 10 thc45
1034gachk gafbad thc35
1035gachk gafdup thc36
1036gachk gafbig thc37
1037gasett c08
1038gachk gapane thc38
1039gachk gaents tpenq thc46
1040gachk gaents tpend thc47
1041gachk gakilss thc39
1042gasett c01
1043gachk garef tline 5 thc40
1044gachk not gakilss thc41
1045gaend
1046ret garc
1047end
1048note gaend / garc: delete the sink file, print the summary; exit 0 only when nothing failed
1049fn gaend to void
1050gasinkp
1051u = unlink gpth 0 get gpl 0
1052vwsbt tsum
1053vwsbd get gapass 0
1054vwsbt tsum2
1055vwsbd get gafail 0
1056vwsbt tsum3
1057gaout
1058end
1059fn garc to int
1060if eq get gafail 0 0
1061ret 0
1062end
1063ret 1
1064end
1065note ganolog: against a server whose HOME has no writable ~/.local/state/hive-view, a send is
1066note refused with code 8 before anything is typed (the gate runs with that same HOME)
1067fn ganolog to int
1068gaxbuf
1069if not gatok
1070ret 3
1071end
1072z = new uchar 68 48
1073for i ulong 4
1074put z i get tqt i
1075end
1076for i ulong 64
1077put z add 4 i get gtok i
1078end
1079st = gareq2 z
1080gachk and eq st 302 eq get gack 0 1 thc06
1081gachk gamkses thc09
1082gachk gagetk thc10
1083gasett c01
1084gachk garef tline 8 thc42
1085gachk gakilss thc39
1086gaend
1087ret garc
1088end
1089note ---- the cpu mode: utime + stime + cutime + cstime (fields 14..17 of /proc/PID/stat)
1090fn gastat pid ulong to ulong
1091vwpb
1092vwpt tproc
1093vwpd pid
1094vwpt tstat
1095r = vwrdall 4096
1096if lt r 0
1097ret 0
1098end
1099n = get gol 0
1100p = ulong 0
1101for i ulong n
1102if eq get gob i 41
1103p = i
1104end
1105end
1106f = ulong 2
1107v = ulong 0
1108s = ulong 0
1109for i ulong sub n p
1110c = get gob add p i
1111if eq c 32
1112if and ge f 14 le f 17
1113s = add s v
1114end
1115f = add f 1
1116v = 0
1117else
1118if vwdig c
1119v = add mul v 10 ulong sub c 48
1120end
1121end
1122end
1123ret s
1124end
1125note gatree: ticks of pid plus its direct children (the poller and the connection handlers)
1126fn gatree pid ulong to ulong
1127t = gastat pid
1128vwpb
1129vwpt tproc
1130vwpd pid
1131vwpt tchild
1132vwpd pid
1133vwpt tchild2
1134r = vwrdall 4096
1135if lt r 0
1136ret t
1137end
1138n = get gol 0
1139kids = new ulong 256 0
1140k = ulong 0
1141v = ulong 0
1142for i ulong add n 1
1143c = uchar 32
1144if lt i n
1145c = get gob i
1146end
1147if vwdig c
1148v = add mul v 10 ulong sub c 48
1149else
1150if and gt v 0 lt k 256
1151put kids k v
1152k = add k 1
1153end
1154v = 0
1155end
1156end
1157for i ulong k
1158t = add t gastat get kids i
1159end
1160ret t
1161end
1162fn gatmxp to ulong
1163gatmux
1164vwka tdisp
1165vwka tdp
1166vwka tpid
1167r = vwrun 5000000000
1168if ne r 0
1169ret 0
1170end
1171v = ulong 0
1172for i ulong get gol 0
1173c = get gob i
1174if not vwdig c
1175break
1176end
1177v = add mul v 10 ulong sub c 48
1178end
1179ret v
1180end
1181fn gacpu pid ulong secs ulong to int
1182tp = gatmxp
1183a0 = gatree pid
1184b0 = gastat tp
1185x = sleepns long mul secs 1000000000
1186a1 = gatree pid
1187b1 = gastat tp
1188da = ulong 0
1189if gt a1 a0
1190da = sub a1 a0
1191end
1192db = ulong 0
1193if gt b1 b0
1194db = sub b1 b0
1195end
1196vwsbt tcpu1
1197vwsbd da
1198vwsbt tcpu2
1199vwsbd db
1200vwsbt tcpu3
1201vwsbd secs
1202vwsbt tcpu4
1203gaout
1204ret 0
1205end
1206text trun "run"
1207text tcpu "cpu"
1208text tnolog "nolog"
1209fn main to int
1210if not gahome
1211ret 6
1212end
1213ab = new uchar 512 0
1214n = argread 1 ab 0 512
1215if lt argcnt 3
1216ret 2
1217end
1218if heq view part ab 0 ulong hmin ulong n 512 trun
1219m = argread 2 ab 0 512
1220put gaport 0 gadec view part ab 0 ulong m
1221ret garun
1222end
1223if heq view part ab 0 ulong hmin ulong n 512 tcpu
1224if lt argcnt 4
1225ret 2
1226end
1227m = argread 2 ab 0 512
1228pid = gadec view part ab 0 ulong m
1229m = argread 3 ab 0 512
1230secs = gadec view part ab 0 ulong m
1231ret gacpu pid secs
1232end
1233if heq view part ab 0 ulong hmin ulong n 512 tnolog
1234m = argread 2 ab 0 512
1235put gaport 0 gadec view part ab 0 ulong m
1236ret ganolog
1237end
1238ret 2
1239end