// scriptlet staging and ldconfig ordering follow trans.c/remove.c; shell arguments stay literal. // SPDX-License-Identifier: GPL-2.0-or-later module engine import crypto.rand import os fn read_scriptlet_descriptor(fd i32) ![]u8 { mut result := []u8{} mut buffer := []u8{len: 32768} for { count := C.read(fd, buffer.data, uint(buffer.len)) if count < 0 && C.errno == C.EINTR { continue } if count < 0 { return os.last_error() } if count == 0 { break } result << buffer[..count] } return result } struct ScriptletContents { data []u8 mode u32 native bool } fn (mut handle Handle) package_scriptlet(pkg &Package) !ScriptletContents { if pkg.origin != .local_db { return error('scriptlet is unavailable for a sync package') } directory := handle.local.open_local_entry(pkg)! defer { handle.close_process_descriptor(directory) } flags := C.O_RDONLY | C.O_NOFOLLOW | C.O_NONBLOCK | C.O_CLOEXEC mut fd := C.openat(directory, c'native_hook', flags) native := fd >= 0 if !native && C.errno == C.ENOENT { fd = C.openat(directory, c'install', flags) } if fd < 0 { return os.last_error() } defer { handle.close_process_descriptor(fd) } mut size := i64(0) mut mode := u32(0) mut links := u64(0) if C.v_alpm_download_stat(fd, &size, &mode, &links) != 0 { return os.last_error() } if mode & 0o170000 != 0o100000 { return error('scriptlet is not a regular file') } return ScriptletContents{ data: read_scriptlet_descriptor(fd)!, mode: if native { u32(0o700) } else { mode & 0o7777 }, native: native } } fn scriptlet_contains(text string, function string) bool { for line in text.split_into_lines() { if line.all_before('#').contains(function) { return true } } return false } fn (mut handle Handle) scriptlet_tmp_directory(mut root RootFilesystem) !i32 { mut path := root.resolve('tmp', true)! defer { path.close() } mask := C.umask(0) status := C.mkdirat(path.fd, unsafe { &char(path.name.str) }, 0o1777) problem := os.last_error() C.umask(mask) if status != 0 && problem.code() != int(C.EEXIST) { return problem } fd := C.openat(path.fd, unsafe { &char(path.name.str) }, root_directory_flags()) if fd < 0 { return os.last_error() } return fd } fn (handle &Handle) scriptlet_directory_failure(problem IError) IError { handle.log(.error, localized_message('could not create temp directory\n')) return problem } fn (handle &Handle) scriptlet_copy_failure(pkg &Package, path string, problem IError) IError { if pkg.origin == .local_db { handle.log(.error, localized_message('could not copy tempfile to %s (%s)\n', path, problem.msg())) } return problem } fn (mut handle Handle) copy_scriptlet_contents(pkg &Package, directory i32, staged string, contents ScriptletContents) ! { fd := C.openat(directory, c'.INSTALL', C.O_WRONLY | C.O_CREAT | C.O_EXCL | C.O_NOFOLLOW | C.O_CLOEXEC, 0) if fd < 0 { return handle.scriptlet_copy_failure(pkg, staged, os.last_error()) } mut writer := fd defer { handle.close_process_descriptor(writer) } // util.c copies the source mode before writing, independently of the caller's umask. if C.fchmod(writer, contents.mode) != 0 { return handle.scriptlet_copy_failure(pkg, staged, os.last_error()) } mut position := 0 for position < contents.data.len { written := C.write(writer, unsafe { &u8(contents.data.data) + position }, uint(contents.data.len - position)) if written < 0 && C.errno == C.EINTR { continue } if written <= 0 { return handle.scriptlet_copy_failure(pkg, staged, os.last_error()) } position += int(written) } closed := C.close(writer) writer = -1 if closed != 0 { return handle.scriptlet_copy_failure(pkg, staged, os.last_error()) } } fn (mut handle Handle) run_package_scriptlet(mut root RootFilesystem, pkg &Package, function string, version string, old_version ?string) ! { if function !in ['pre_install', 'post_install', 'pre_upgrade', 'post_upgrade', 'pre_remove', 'post_remove'] { return error('invalid scriptlet phase') } mut contents := ScriptletContents{} if pkg.origin != .file { contents = handle.package_scriptlet(pkg)! if !contents.native && !scriptlet_contains(contents.data.bytestr(), function) { return } } tmp := handle.scriptlet_tmp_directory(mut root) or { return handle.scriptlet_directory_failure(err) } defer { handle.close_process_descriptor(tmp) } mut name := '' for _ in 0 .. 16 { random := rand.bytes(12) or { return handle.scriptlet_directory_failure(err) } name = 'alpm_' + random.hex() if C.mkdirat(tmp, unsafe { &char(name.str) }, 0o700) == 0 { break } if C.errno != C.EEXIST { return handle.scriptlet_directory_failure(os.last_error()) } name = '' } if name == '' { return handle.scriptlet_directory_failure(error('cannot reserve scriptlet directory')) } defer { if C.unlinkat(tmp, unsafe { &char(name.str) }, C.AT_REMOVEDIR) != 0 { handle.diagnostics << 'could not remove scriptlet directory: ${os.last_error().msg()}' handle.log(.warning, localized_message('could not remove tmpdir %s\n', '${handle.root}tmp/${name}')) } } directory := C.openat(tmp, unsafe { &char(name.str) }, root_directory_flags()) if directory < 0 { return os.last_error() } defer { handle.close_process_descriptor(directory) } staged := '${handle.root}tmp/${name}/.INSTALL' defer { if C.unlinkat(directory, c'.INSTALL', 0) != 0 { handle.diagnostics << 'could not remove staged scriptlet: ${os.last_error().msg()}' handle.log(.warning, localized_message('could not remove %s\n', '${handle.root}tmp/${name}/.INSTALL')) } } mut native := contents.native if pkg.origin == .file { native = handle.stage_archive_scriptlet(pkg, directory)! if !native { staged_contents := handle.staged_scriptlet_contents(directory)! if !scriptlet_contains(staged_contents.bytestr(), function) { return } } } else { handle.copy_scriptlet_contents(pkg, directory, staged, contents)! } if native { // the archive supplies an executable; execv inside the transaction root // passes the phase, package and versions as distinct arguments. command := '/tmp/${name}/.INSTALL' mut argv := [command, '--nspkg-hook', function.replace('_', '-'), pkg.name, version] if previous := old_version { argv << previous } handle.run_chroot_command(mut root, command, argv, [])! return } mut args := ['/bin/sh', '-c', r'. "$1"; "$2" "$3"', 'alpm-scriptlet', '/tmp/${name}/.INSTALL', function, version] if previous := old_version { args[2] = r'. "$1"; "$2" "$3" "$4"' args << previous } handle.run_chroot_command(mut root, '/bin/sh', args, [])! } fn (mut handle Handle) run_ldconfig(mut root RootFilesystem) ! { if !root.stat('etc/ld.so.conf', true)!.present { return } mut command := root.resolve('sbin/ldconfig', true) or { if err.code() in [int(C.ENOENT), int(C.ENOTDIR)] { return } return err } defer { command.close() } if C.faccessat(command.fd, unsafe { &char(command.name.str) }, C.X_OK, C.AT_EACCESS) != 0 { return } handle.run_chroot_command(mut root, '/sbin/ldconfig', ['ldconfig'], [])! }