| 1 | // scriptlet staging and ldconfig ordering follow trans.c/remove.c; shell arguments stay literal. |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | module engine |
| 4 | |
| 5 | import crypto.rand |
| 6 | import os |
| 7 | |
| 8 | fn read_scriptlet_descriptor(fd i32) ![]u8 { |
| 9 | mut result := []u8{} |
| 10 | mut buffer := []u8{len: 32768} |
| 11 | for { |
| 12 | count := C.read(fd, buffer.data, uint(buffer.len)) |
| 13 | if count < 0 && C.errno == C.EINTR { continue } |
| 14 | if count < 0 { return os.last_error() } |
| 15 | if count == 0 { break } |
| 16 | result << buffer[..count] |
| 17 | } |
| 18 | return result |
| 19 | } |
| 20 | |
| 21 | struct ScriptletContents { |
| 22 | data []u8 |
| 23 | mode u32 |
| 24 | native bool |
| 25 | } |
| 26 | |
| 27 | fn (mut handle Handle) package_scriptlet(pkg &Package) !ScriptletContents { |
| 28 | if pkg.origin != .local_db { return error('scriptlet is unavailable for a sync package') } |
| 29 | directory := handle.local.open_local_entry(pkg)! |
| 30 | defer { handle.close_process_descriptor(directory) } |
| 31 | flags := C.O_RDONLY | C.O_NOFOLLOW | C.O_NONBLOCK | C.O_CLOEXEC |
| 32 | mut fd := C.openat(directory, c'native_hook', flags) |
| 33 | native := fd >= 0 |
| 34 | if !native && C.errno == C.ENOENT { fd = C.openat(directory, c'install', flags) } |
| 35 | if fd < 0 { return os.last_error() } |
| 36 | defer { handle.close_process_descriptor(fd) } |
| 37 | mut size := i64(0) |
| 38 | mut mode := u32(0) |
| 39 | mut links := u64(0) |
| 40 | if C.v_alpm_download_stat(fd, &size, &mode, &links) != 0 { return os.last_error() } |
| 41 | if mode & 0o170000 != 0o100000 { return error('scriptlet is not a regular file') } |
| 42 | return ScriptletContents{ data: read_scriptlet_descriptor(fd)!, mode: if native { |
| 43 | u32(0o700) |
| 44 | } else { |
| 45 | mode & 0o7777 |
| 46 | }, native: native } |
| 47 | } |
| 48 | |
| 49 | fn scriptlet_contains(text string, function string) bool { |
| 50 | for line in text.split_into_lines() { |
| 51 | if line.all_before('#').contains(function) { return true } |
| 52 | } |
| 53 | return false |
| 54 | } |
| 55 | |
| 56 | fn (mut handle Handle) scriptlet_tmp_directory(mut root RootFilesystem) !i32 { |
| 57 | mut path := root.resolve('tmp', true)! |
| 58 | defer { path.close() } |
| 59 | mask := C.umask(0) |
| 60 | status := C.mkdirat(path.fd, unsafe { &char(path.name.str) }, 0o1777) |
| 61 | problem := os.last_error() |
| 62 | C.umask(mask) |
| 63 | if status != 0 && problem.code() != int(C.EEXIST) { return problem } |
| 64 | fd := C.openat(path.fd, unsafe { &char(path.name.str) }, root_directory_flags()) |
| 65 | if fd < 0 { return os.last_error() } |
| 66 | return fd |
| 67 | } |
| 68 | |
| 69 | fn (handle &Handle) scriptlet_directory_failure(problem IError) IError { |
| 70 | handle.log(.error, localized_message('could not create temp directory\n')) |
| 71 | return problem |
| 72 | } |
| 73 | |
| 74 | fn (handle &Handle) scriptlet_copy_failure(pkg &Package, path string, problem IError) IError { |
| 75 | if pkg.origin == .local_db { |
| 76 | handle.log(.error, localized_message('could not copy tempfile to %s (%s)\n', path, problem.msg())) |
| 77 | } |
| 78 | return problem |
| 79 | } |
| 80 | |
| 81 | fn (mut handle Handle) copy_scriptlet_contents(pkg &Package, directory i32, staged string, contents ScriptletContents) ! { |
| 82 | fd := C.openat(directory, c'.INSTALL', C.O_WRONLY | C.O_CREAT | C.O_EXCL | C.O_NOFOLLOW | C.O_CLOEXEC, 0) |
| 83 | if fd < 0 { return handle.scriptlet_copy_failure(pkg, staged, os.last_error()) } |
| 84 | mut writer := fd |
| 85 | defer { handle.close_process_descriptor(writer) } |
| 86 | // util.c copies the source mode before writing, independently of the caller's umask. |
| 87 | if C.fchmod(writer, contents.mode) != 0 { |
| 88 | return handle.scriptlet_copy_failure(pkg, staged, os.last_error()) |
| 89 | } |
| 90 | mut position := 0 |
| 91 | for position < contents.data.len { |
| 92 | written := C.write(writer, unsafe { &u8(contents.data.data) + position }, uint(contents.data.len - position)) |
| 93 | if written < 0 && C.errno == C.EINTR { continue } |
| 94 | if written <= 0 { return handle.scriptlet_copy_failure(pkg, staged, os.last_error()) } |
| 95 | position += int(written) |
| 96 | } |
| 97 | closed := C.close(writer) |
| 98 | writer = -1 |
| 99 | if closed != 0 { return handle.scriptlet_copy_failure(pkg, staged, os.last_error()) } |
| 100 | } |
| 101 | |
| 102 | fn (mut handle Handle) run_package_scriptlet(mut root RootFilesystem, pkg &Package, function string, version string, old_version ?string) ! { |
| 103 | if function !in ['pre_install', 'post_install', 'pre_upgrade', 'post_upgrade', 'pre_remove', |
| 104 | 'post_remove'] { |
| 105 | return error('invalid scriptlet phase') |
| 106 | } |
| 107 | mut contents := ScriptletContents{} |
| 108 | if pkg.origin != .file { |
| 109 | contents = handle.package_scriptlet(pkg)! |
| 110 | if !contents.native && !scriptlet_contains(contents.data.bytestr(), function) { return } |
| 111 | } |
| 112 | tmp := handle.scriptlet_tmp_directory(mut root) or { return handle.scriptlet_directory_failure(err) } |
| 113 | defer { handle.close_process_descriptor(tmp) } |
| 114 | mut name := '' |
| 115 | for _ in 0 .. 16 { |
| 116 | random := rand.bytes(12) or { return handle.scriptlet_directory_failure(err) } |
| 117 | name = 'alpm_' + random.hex() |
| 118 | if C.mkdirat(tmp, unsafe { &char(name.str) }, 0o700) == 0 { break } |
| 119 | if C.errno != C.EEXIST { return handle.scriptlet_directory_failure(os.last_error()) } |
| 120 | name = '' |
| 121 | } |
| 122 | if name == '' { |
| 123 | return handle.scriptlet_directory_failure(error('cannot reserve scriptlet directory')) |
| 124 | } |
| 125 | defer { |
| 126 | if C.unlinkat(tmp, unsafe { &char(name.str) }, C.AT_REMOVEDIR) != 0 { |
| 127 | handle.diagnostics << 'could not remove scriptlet directory: ${os.last_error().msg()}' |
| 128 | handle.log(.warning, localized_message('could not remove tmpdir %s\n', '${handle.root}tmp/${name}')) |
| 129 | } |
| 130 | } |
| 131 | directory := C.openat(tmp, unsafe { &char(name.str) }, root_directory_flags()) |
| 132 | if directory < 0 { return os.last_error() } |
| 133 | defer { handle.close_process_descriptor(directory) } |
| 134 | staged := '${handle.root}tmp/${name}/.INSTALL' |
| 135 | defer { |
| 136 | if C.unlinkat(directory, c'.INSTALL', 0) != 0 { |
| 137 | handle.diagnostics << 'could not remove staged scriptlet: ${os.last_error().msg()}' |
| 138 | handle.log(.warning, localized_message('could not remove %s\n', '${handle.root}tmp/${name}/.INSTALL')) |
| 139 | } |
| 140 | } |
| 141 | mut native := contents.native |
| 142 | if pkg.origin == .file { |
| 143 | native = handle.stage_archive_scriptlet(pkg, directory)! |
| 144 | if !native { |
| 145 | staged_contents := handle.staged_scriptlet_contents(directory)! |
| 146 | if !scriptlet_contains(staged_contents.bytestr(), function) { return } |
| 147 | } |
| 148 | } else { |
| 149 | handle.copy_scriptlet_contents(pkg, directory, staged, contents)! |
| 150 | } |
| 151 | if native { |
| 152 | // the archive supplies an executable; execv inside the transaction root |
| 153 | // passes the phase, package and versions as distinct arguments. |
| 154 | command := '/tmp/${name}/.INSTALL' |
| 155 | mut argv := [command, '--nspkg-hook', function.replace('_', '-'), pkg.name, version] |
| 156 | if previous := old_version { argv << previous } |
| 157 | handle.run_chroot_command(mut root, command, argv, [])! |
| 158 | return |
| 159 | } |
| 160 | mut args := ['/bin/sh', '-c', r'. "$1"; "$2" "$3"', 'alpm-scriptlet', '/tmp/${name}/.INSTALL', |
| 161 | function, version] |
| 162 | if previous := old_version { |
| 163 | args[2] = r'. "$1"; "$2" "$3" "$4"' |
| 164 | args << previous |
| 165 | } |
| 166 | handle.run_chroot_command(mut root, '/bin/sh', args, [])! |
| 167 | } |
| 168 | |
| 169 | fn (mut handle Handle) run_ldconfig(mut root RootFilesystem) ! { |
| 170 | if !root.stat('etc/ld.so.conf', true)!.present { return } |
| 171 | mut command := root.resolve('sbin/ldconfig', true) or { |
| 172 | if err.code() in [int(C.ENOENT), int(C.ENOTDIR)] { return } |
| 173 | return err |
| 174 | } |
| 175 | defer { command.close() } |
| 176 | if C.faccessat(command.fd, unsafe { &char(command.name.str) }, C.X_OK, C.AT_EACCESS) != 0 { |
| 177 | return |
| 178 | } |
| 179 | handle.run_chroot_command(mut root, '/sbin/ldconfig', ['ldconfig'], [])! |
| 180 | } |
| 181 | |