// duplex child io and output events follow pacman util.c without changing the parent's cwd. // SPDX-License-Identifier: GPL-2.0-or-later module engine import os #include "@DIR/native_process.h" fn C.v_alpm_process_pair(&i32) i32 fn C.v_alpm_process_nonblock(i32) i32 fn C.v_alpm_process_poll(i32, i32, &i32, &i32) i32 fn C.v_alpm_process_send(i32, voidptr, u32) i32 fn C.v_alpm_process_exec(&char, &&char) i32 fn C.fchdir(i32) i32 fn C.chroot(&char) i32 fn C.strsignal(i32) &char fn (mut handle Handle) close_process_descriptor(fd i32) i32 { if fd >= 0 && C.close(fd) != 0 { handle.diagnostics << 'could not close process descriptor: ${os.last_error().msg()}' } return -1 } fn process_close_inherited() ! { if C.v_alpm_close_range(3, ~u32(0)) == 0 { return } if C.errno != C.ENOSYS { return os.last_error() } limit := C.sysconf(C._SC_OPEN_MAX) if limit < 0 { return os.last_error() } for fd := i32(3); fd < limit; fd++ { if C.close(fd) != 0 && C.errno != C.EBADF { return os.last_error() } } } fn process_child_failure(message string) { process_child_output_failure(message + '\n') } fn process_child_output_failure(line string) { mut offset := 0 for offset < line.len { count := C.write(2, unsafe { line.str + offset }, uint(line.len - offset)) if count < 0 && C.errno == C.EINTR { continue } if count <= 0 { break } offset += int(count) } C._exit(1) } fn process_child(root_fd i32, change_root bool, command string, argv []&char, input i32, output i32) { for pair in [[input, i32(0)], [output, i32(1)], [output, i32(2)]] { for { if C.dup2(pair[0], pair[1]) >= 0 { break } if C.errno != C.EINTR { C._exit(1) } } } if C.fchdir(root_fd) != 0 { process_child_failure('could not enter package root: ${os.last_error().msg()}') } if change_root && C.chroot(c'.') != 0 { process_child_output_failure(localized_message('could not change the root directory (%s)\n', os.last_error().msg())) } if C.chdir(c'/') != 0 { process_child_output_failure(localized_message('could not change directory to %s (%s)\n', '/', os.last_error().msg())) } if os.setenv('SHLVL', '1', false) != 0 || os.unsetenv('BASH_ENV') != 0 { process_child_failure('could not prepare command environment') } C.umask(0o022) for number in [C.SIGABRT, C.SIGALRM, C.SIGBUS, C.SIGCHLD, C.SIGCONT, C.SIGFPE, C.SIGHUP, C.SIGILL, C.SIGINT, C.SIGPIPE, C.SIGQUIT, C.SIGSEGV, C.SIGTERM, C.SIGTSTP, C.SIGTTIN, C.SIGTTOU, C.SIGUSR1, C.SIGUSR2, C.SIGPROF, C.SIGSYS, C.SIGTRAP, C.SIGURG, C.SIGVTALRM, C.SIGXCPU, C.SIGXFSZ] { if C.v_alpm_signal_default(number) != 0 { process_child_failure('could not reset command signals') } } $if linux { if C.v_alpm_signal_default(C.SIGPOLL) != 0 { process_child_failure('could not reset command SIGPOLL') } } process_close_inherited() or { process_child_failure(err.msg()) } // an empty Exec= yields an argv containing only NULL, as wordsplit does in C. command_pointer := if argv.len == 1 { &char(unsafe { nil }) } else { unsafe { &char(command.str) } } C.v_alpm_process_exec(command_pointer, unsafe { &&char(argv.data) }) process_child_output_failure(localized_message('call to execv failed (%s)\n', os.last_error().msg())) } fn (mut handle Handle) process_output(mut buffered []u8, data []u8) { for byte in data { buffered << byte if byte == `\n` || buffered.len == 2046 { line := buffered.bytestr() + if byte == `\n` { '' } else { '\n' } handle.record_action_as('ALPM-SCRIPTLET', line) handle.commit_event(CommitEvent{ kind: .scriptlet_info, line: line }) buffered.clear() } } } fn (mut handle Handle) run_chroot_command(mut root RootFilesystem, command string, args []string, input []u8) ! { if handle.process_active { return error('a package command is already executing') } if command.contains('\x00') || (args.len == 0 && command != '') || args.any(it.contains('\x00')) { return error('invalid package command') } handle.process_active = true defer { handle.process_active = false } // stdio redirection must not overwrite a root originally opened as fd 0, 1 or 2. root_fd := C.v_alpm_duplicate_pipe(root.fd) if root_fd < 0 { return os.last_error() } defer { handle.close_process_descriptor(root_fd) } label := if args.len == 0 { '(null)' } else { command } handle.log(.debug, 'executing "${label}" under chroot "${handle.root}"\n') C.fflush(unsafe { nil }) mut output_pair := [i32(-1), -1] if C.v_alpm_process_pair(unsafe { &i32(output_pair.data) }) != 0 { problem := os.last_error() handle.log(.error, localized_message('could not create pipe (%s)\n', problem.msg())) return problem } defer { for fd in output_pair { handle.close_process_descriptor(fd) } } mut input_pair := [i32(-1), -1] if C.v_alpm_process_pair(unsafe { &i32(input_pair.data) }) != 0 { problem := os.last_error() handle.log(.error, localized_message('could not create pipe (%s)\n', problem.msg())) return problem } defer { for fd in input_pair { handle.close_process_descriptor(fd) } } // argv strings remain rooted in the parent until waitpid has reaped the child. mut argv := []&char{cap: args.len + 1} for arg in args { argv << unsafe { &char(arg.str) } } argv << &char(unsafe { nil }) child := fork_process() if child < 0 { problem := os.last_error() handle.log(.error, localized_message('could not fork a new process (%s)\n', problem.msg())) return problem } if child == 0 { process_child(root_fd, handle.root != '/', command, argv, input_pair[0], output_pair[1]) C._exit(1) } input_pair[0] = handle.close_process_descriptor(input_pair[0]) output_pair[1] = handle.close_process_descriptor(output_pair[1]) mut failure := '' if C.v_alpm_process_nonblock(output_pair[0]) != 0 || C.v_alpm_process_nonblock(input_pair[1]) != 0 { failure = os.last_error().msg() } mut position := 0 mut buffered := []u8{cap: 2046} mut block := []u8{len: 2046} if input.len == 0 { input_pair[1] = handle.close_process_descriptor(input_pair[1]) } for failure == '' && (output_pair[0] >= 0 || input_pair[1] >= 0) { mut out_events := i32(0) mut in_events := i32(0) ready := C.v_alpm_process_poll(output_pair[0], input_pair[1], &out_events, &in_events) if ready < 0 && C.errno == C.EINTR { continue } if ready < 0 { failure = os.last_error().msg() break } if out_events != 0 { count := C.read(output_pair[0], block.data, uint(block.len - buffered.len)) if count > 0 { handle.process_output(mut buffered, block[..count]) } else if count == 0 { output_pair[0] = handle.close_process_descriptor(output_pair[0]) } else if C.errno !in [C.EINTR, C.EAGAIN, C.EWOULDBLOCK] { failure = os.last_error().msg() } } if in_events & C.POLLOUT != 0 { count := C.v_alpm_process_send(input_pair[1], unsafe { &u8(input.data) + position }, u32(int_min(4096, input.len - position))) if count > 0 { position += count } if count < 0 && C.errno !in [C.EINTR, C.EAGAIN, C.EWOULDBLOCK] { if C.errno != C.EPIPE { failure = os.last_error().msg() } input_pair[1] = handle.close_process_descriptor(input_pair[1]) } if position == input.len { input_pair[1] = handle.close_process_descriptor(input_pair[1]) } } else if in_events != 0 { input_pair[1] = handle.close_process_descriptor(input_pair[1]) } } if buffered.len > 0 { line := buffered.bytestr() + '\n' handle.record_action_as('ALPM-SCRIPTLET', line) handle.commit_event(CommitEvent{ kind: .scriptlet_info, line: line }) } if failure != '' { C.kill(child, C.SIGKILL) } mut status := i32(0) for { if C.v_alpm_wait_child(child, &status, 0) >= 0 { break } if C.errno != C.EINTR { failure_error := os.last_error() handle.log(.error, localized_message('call to waitpid failed (%s)\n', failure_error.msg())) return failure_error } } if failure != '' { return error('package command io: ${failure}') } signal := C.v_alpm_term_signal(status) if signal != 0 { description := C.strsignal(signal) message := if description == unsafe { nil } { translated_message('Unknown signal') } else { unsafe { cstring_to_vstring(description) } } handle.log(.error, localized_message('command terminated by signal %d: %s\n', signal, message)) return error('package command terminated by signal ${signal}') } handle.log(.debug, 'call to waitpid succeeded\n') code := C.v_alpm_exit_status(status) if code != 0 { handle.log(.error, localized_message('command failed to execute correctly\n')) return error('package command exited ${code}') } }