| 1 | module engine |
| 2 | |
| 3 | import os |
| 4 | |
| 5 | struct CommandTrace { |
| 6 | mut: |
| 7 | lines []string |
| 8 | } |
| 9 | |
| 10 | fn command_output(context voidptr, event CommitEvent) { |
| 11 | mut trace := unsafe { &CommandTrace(context) } |
| 12 | if event.kind == .scriptlet_info { trace.lines << event.line } |
| 13 | } |
| 14 | |
| 15 | fn command_fixture(name string) !&Handle { |
| 16 | db := os.join_path(os.vtmp_dir(), 'alpm-command-${name}-${os.getpid()}') |
| 17 | os.mkdir_all(db)! |
| 18 | return initialize('/', db) |
| 19 | } |
| 20 | |
| 21 | fn cleanup_command_fixture(mut handle Handle) { |
| 22 | path := handle.dbpath |
| 23 | handle.release() or { eprintln(err) } |
| 24 | os.rmdir_all(path) or { eprintln(err) } |
| 25 | } |
| 26 | |
| 27 | fn test_command_combines_output_normalizes_partial_lines_and_reports_status() ! { |
| 28 | mut handle := command_fixture('output')! |
| 29 | defer { cleanup_command_fixture(mut handle) } |
| 30 | mut root := handle.open_root_filesystem()! |
| 31 | defer { root.close() } |
| 32 | mut trace := &CommandTrace{} |
| 33 | handle.set_commit_callback(command_output, trace)! |
| 34 | handle.run_chroot_command(mut root, '/bin/sh', ['sh', '-c', |
| 35 | "printf 'out\\n'; printf 'err\\n' >&2; printf tail"], [])! |
| 36 | assert trace.lines == ['out\n', 'err\n', 'tail\n'] |
| 37 | trace.lines.clear() |
| 38 | handle.run_chroot_command(mut root, '/bin/sh', ['sh', '-c', "printf '%s' " + 'x'.repeat(5000)], [])! |
| 39 | assert trace.lines.map(it.len) == [2047, 2047, 909] |
| 40 | assert trace.lines.join('').replace('\n', '') == 'x'.repeat(5000) |
| 41 | for script in ['exit 7', 'kill -TERM $$'] { |
| 42 | if _ := handle.run_chroot_command(mut root, '/bin/sh', ['sh', '-c', script], []) { |
| 43 | assert false |
| 44 | } |
| 45 | assert !handle.process_active |
| 46 | } |
| 47 | if _ := handle.run_chroot_command(mut root, '/missing-command', ['/missing-command'], []) { |
| 48 | assert false |
| 49 | } |
| 50 | assert trace.lines.last().contains('execv failed') |
| 51 | } |
| 52 | |
| 53 | fn test_command_pumps_both_directions_and_handles_closed_stdin_without_sigpipe() ! { |
| 54 | mut handle := command_fixture('duplex')! |
| 55 | defer { cleanup_command_fixture(mut handle) } |
| 56 | mut root := handle.open_root_filesystem()! |
| 57 | defer { root.close() } |
| 58 | mut trace := &CommandTrace{} |
| 59 | handle.set_commit_callback(command_output, trace)! |
| 60 | input := 'after\n'.repeat(50000).bytes() |
| 61 | handle.run_chroot_command(mut root, '/bin/sh', ['sh', '-c', |
| 62 | 'awk \'BEGIN { for(i=0;i<50000;i++) print "before" }\'; cat'], input)! |
| 63 | assert trace.lines.len == 100000 |
| 64 | assert trace.lines[..50000].all(it == 'before\n') |
| 65 | assert trace.lines[50000..].all(it == 'after\n') |
| 66 | trace.lines.clear() |
| 67 | handle.run_chroot_command(mut root, '/bin/sh', ['sh', '-c', 'exec 0<&-; printf done'], input)! |
| 68 | assert trace.lines == ['done\n'] |
| 69 | } |
| 70 | |
| 71 | fn test_command_child_environment_signals_and_descriptors_leave_parent_unchanged() ! { |
| 72 | mut handle := command_fixture('environment')! |
| 73 | defer { cleanup_command_fixture(mut handle) } |
| 74 | mut root := handle.open_root_filesystem()! |
| 75 | defer { root.close() } |
| 76 | mut trace := &CommandTrace{} |
| 77 | handle.set_commit_callback(command_output, trace)! |
| 78 | old_environment := os.environ() |
| 79 | assert os.setenv('BASH_ENV', '/not-an-alpm-startup-file', true) == 0 |
| 80 | defer { |
| 81 | if value := old_environment['BASH_ENV'] { |
| 82 | assert os.setenv('BASH_ENV', value, true) == 0 |
| 83 | } else { |
| 84 | assert os.unsetenv('BASH_ENV') == 0 |
| 85 | } |
| 86 | } |
| 87 | old_mask := C.umask(0o077) |
| 88 | defer { C.umask(old_mask) } |
| 89 | mut old_pipe := C.v_alpm_saved_signal{} |
| 90 | assert C.v_alpm_signal_ignore(C.SIGPIPE, &old_pipe) == 0 |
| 91 | defer { assert C.v_alpm_signal_restore(C.SIGPIPE, &old_pipe) == 0 } |
| 92 | inherited := C.open(c'/dev/null', C.O_RDONLY) |
| 93 | assert inherited >= 0 |
| 94 | defer { assert C.close(inherited) == 0 } |
| 95 | handle.run_chroot_command(mut root, '/bin/sh', ['sh', '-c', |
| 96 | r'printf "%s\n" "${BASH_ENV-unset}"; umask; pwd; test ! -e /proc/self/fd/' + inherited.str()], [])! |
| 97 | assert trace.lines == ['unset\n', '0022\n', '/\n'] |
| 98 | assert os.getenv('BASH_ENV') == '/not-an-alpm-startup-file' |
| 99 | assert C.umask(0o077) == 0o077 |
| 100 | if _ := handle.run_chroot_command(mut root, '/bin/sh', ['sh', '-c', 'kill -PIPE $$'], []) { |
| 101 | assert false |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | fn test_command_accepts_closed_standard_descriptors_and_deleted_parent_cwd() ! { |
| 106 | mut handle := command_fixture('descriptors')! |
| 107 | defer { cleanup_command_fixture(mut handle) } |
| 108 | mut root := handle.open_root_filesystem()! |
| 109 | defer { root.close() } |
| 110 | child := C.fork() |
| 111 | assert child >= 0 |
| 112 | if child == 0 { |
| 113 | for fd in [i32(0), 1, 2] { if C.close(fd) != 0 { C._exit(2) } } |
| 114 | mut child_root := handle.open_root_filesystem() or { |
| 115 | C._exit(3) |
| 116 | return |
| 117 | } |
| 118 | if child_root.fd != 0 { C._exit(4) } |
| 119 | handle.run_chroot_command(mut child_root, '/bin/sh', ['sh', '-c', 'printf output'], []) or { C._exit(5) } |
| 120 | C._exit(0) |
| 121 | } |
| 122 | mut status := i32(0) |
| 123 | for { |
| 124 | if C.v_alpm_wait_child(child, &status, 0) >= 0 { break } |
| 125 | if C.errno != C.EINTR { return os.last_error() } |
| 126 | } |
| 127 | assert C.v_alpm_exit_status(status) == 0 |
| 128 | previous := os.getwd() |
| 129 | defer { os.chdir(previous) or { eprintln(err) } } |
| 130 | removed := os.join_path(handle.dbpath, 'removed-cwd') |
| 131 | os.mkdir(removed)! |
| 132 | os.chdir(removed)! |
| 133 | os.rmdir(removed)! |
| 134 | handle.run_chroot_command(mut root, '/bin/sh', ['sh', '-c', 'test "$PWD" = /'], [])! |
| 135 | } |
| 136 | |