| 1 | // Started by the packaged BusyBox init through /etc/inittab on a diskless guest. |
| 2 | module main |
| 3 | |
| 4 | import os |
| 5 | |
| 6 | #include <fcntl.h> |
| 7 | #include <unistd.h> |
| 8 | #include <sys/reboot.h> |
| 9 | |
| 10 | fn C.open(&char, int, ...) int |
| 11 | fn C.dup2(int, int) int |
| 12 | fn C.close(int) int |
| 13 | fn C.reboot(int) int |
| 14 | |
| 15 | fn run(argv []string) !string { |
| 16 | result := os.exec(argv) |
| 17 | if result.exit_code != 0 { |
| 18 | return error('${argv} exited ${result.exit_code}: ${result.output}') |
| 19 | } |
| 20 | return result.output.trim_space() |
| 21 | } |
| 22 | |
| 23 | fn check(argv []string, expected string) ! { |
| 24 | actual := run(argv)! |
| 25 | if actual != expected { |
| 26 | return error('${argv}: expected ${expected}, got ${actual}') |
| 27 | } |
| 28 | println('${argv} -> ${actual}') |
| 29 | } |
| 30 | |
| 31 | fn verify() ! { |
| 32 | for path in ['/sbin/init', '/bin/sh', '/bin/cat'] { |
| 33 | if !os.is_link(path) || os.real_path(path) != '/bin/busybox' { |
| 34 | return error('${path} must resolve to /bin/busybox') |
| 35 | } |
| 36 | } |
| 37 | check(['/bin/busybox', 'uname', '-m'], 'x86_64')! |
| 38 | check(['/bin/sh', '-c', r'echo $((6 * 7))'], '42')! |
| 39 | check(['/bin/busybox', 'ash', '-c', r'echo $((3 * 7))'], '21')! |
| 40 | check(['/bin/cat', '/etc/inittab'], '::sysinit:/busybox-probe')! |
| 41 | println(run(['/bin/busybox', 'sha256sum', '/bin/busybox'])!) |
| 42 | } |
| 43 | |
| 44 | fn main() { |
| 45 | if os.getppid() != 1 || run(['/bin/busybox', 'uname', '-s']) or { '' } != 'Vinix' { |
| 46 | eprintln('requires a Vinix guest with the probe started directly by init') |
| 47 | exit(2) |
| 48 | } |
| 49 | fd := C.open(c'/dev/com1', C.O_WRONLY) |
| 50 | if fd < 0 || C.dup2(fd, 1) < 0 || C.dup2(fd, 2) < 0 { |
| 51 | exit(2) |
| 52 | } |
| 53 | if fd > 2 { |
| 54 | C.close(fd) |
| 55 | } |
| 56 | verify() or { |
| 57 | eprintln('VPAC BUSYBOX GUEST FAIL ${err}') |
| 58 | C.reboot(C.RB_POWER_OFF) |
| 59 | exit(1) |
| 60 | } |
| 61 | println('VPAC BUSYBOX GUEST PASS') |
| 62 | C.reboot(C.RB_POWER_OFF) |
| 63 | } |
| 64 | |