#!/bin/sh # Sl0ptiminer installer for Linux on 64-bit x86 or 64-bit ARM, and for Macs with Apple silicon. # # curl -fsSL https://sl0ptiminer.com/install.sh | sh # # What it does, in order: # 1. checks that this machine can run the package (Linux on 64-bit x86 or 64-bit ARM, or a Mac with Apple silicon), # and picks its package # (an x86 processor without AVX2, FMA and BMI2 mines on graphics cards only); # 2. downloads the package, its checksum list and the list's signature; # 3. checks the package against the checksum written in this file and against the list, # and checks the list's signature with minisign or, without it, with OpenSSL 3 or Python 3; # 4. unpacks the package into ~/sloptiminer (another place: sh -s -- --dir PATH); # 5. prints the command that starts mining. # Run it again to update: the new version is unpacked next to the one in use, which is kept; versions older than # that are removed (keep them all: sh -s -- --keep-old). To go back to the version before: sh -s -- --back # When the installed package has its own updater (update.sh), this file hands the update over to it. # It needs no root rights, changes nothing outside that folder and starts or stops nothing. # The steps by hand: https://sl0ptiminer.com/guides/linux/ and https://sl0ptiminer.com/guides/mac/ set -eu main() { version='1.0.2' file='' sha256='' file_gnu='sloptiminer-1.0.2-linux-x86_64-gnu.tar.gz' # the x86 package for glibc 2.31 or newer; it also mines on graphics cards sha256_gnu='c0d0283655d0711e4a974eba37723eec81745fcdcc231bc6aefe948152621030' file_arm='' # the static package for 64-bit ARM: any Linux, processor only; for systems without glibc 2.31 sha256_arm='' file_arm_gnu='sloptiminer-1.0.2-linux-aarch64-gnu.tar.gz' # the ARM package for glibc 2.31 or newer; it also mines on graphics cards sha256_arm_gnu='8e75d7f2a4ad359b67af87dcc279f5e4f7ddc44f7f209cb6d1c0fdd0e0d9a18e' file_mac='sloptiminer-1.0.2-macos-aarch64.tar.gz' # Macs with Apple silicon; processor only sha256_mac='6249c31d60d3dba4ebc33d07a1a3d27139b8603eea4515c3a3881f401002023b' key='RWQ/WbRNa0qAYKdPxfZD1yo1E0fQ7UDCqHt7BEga1v66ufUi3b8JWVOs' site="${SLOPTIMINER_SITE:-https://sl0ptiminer.com}" # another origin serving the same /dl/ layout (a mirror, a test) dir="$HOME/sloptiminer" back= keep_old= no_opencl= no_list= tmp= incoming= locked= # whatever way this ends: the scratch folder, a half-unpacked package and this run's lock go trap 'rm -rf ${tmp:+"$tmp"} ${incoming:+"$incoming"} ${locked:+"$dir/.lock"}' EXIT trap 'exit 130' INT TERM while [ $# -gt 0 ]; do case "$1" in --dir) [ $# -ge 2 ] || stop "--dir needs a folder"; dir=$2; shift 2 ;; --back) back=1; shift ;; --keep-old) keep_old=1; shift ;; -h | --help) sed -n '2,19p' "$0" 2>/dev/null || true; exit 0 ;; *) stop "unknown option: $1" ;; esac done case "$dir" in '') stop "--dir needs a folder" ;; /*) ;; *) dir="$PWD/$dir" ;; esac # What is in the install folder gets run. So the folder must be this user's own, and neither it nor any folder # above it may be writable by anyone but this user and root. A folder that is not there yet is made closed. near=$dir while [ ! -e "$near" ] && [ ! -L "$near" ]; do near=$(dirname "$near"); done if [ "$near" = "$dir" ]; then [ -d "$dir" ] && [ -O "$dir" ] || stop "$dir is not a folder of your own. Nothing was changed. Pick another folder with --dir."; fi # The 0.8.2 installer made this folder with a plain mkdir -p, so under umask 002 others in the group may write to it. # When that is the only fault (every folder above it passed) and it is this user's own folder, not a link and not a # shared sticky one, it is closed and the user is told, as the packaged updater does. Version folders need nothing: # tar keeps the package's 755. if ! safe_path "$near"; then if [ "$near" = "$dir" ] && [ -n "$open" ] && [ "$unsafe" = "$open" ] && plain_dir "$dir" && [ -O "$dir" ] && [ -z "$(find "$dir" -prune -perm -1000)" ] && chmod go-w "$dir" 2>/dev/null && safe_path "$dir"; then say "$dir was writable by other users; it is now writable by you only (chmod go-w $dir)." else stop "$unsafe is not yours or root's alone: others may write to it, so what is installed under it could be changed. Nothing was changed. Pick another folder with --dir, or close that one: chmod go-w $unsafe" fi fi [ ! -d "$dir" ] || lock # A machine that already has Sl0ptiminer updates with the updater that came inside its installed, verified package: # that script and the keys it checks with are already on this machine, so a changed copy of this file cannot stand in # for them. This file only starts it, and only from a version folder the installer makes: a plain folder in the # install folder, with the updater as a plain file in it. cur=$(readlink "$dir/current" 2>/dev/null || true) case "$cur" in '' | . | .. | *[!A-Za-z0-9._-]*) cur= ;; sloptiminer-[0-9]*-linux-* | sloptiminer-[0-9]*-macos-*) ;; *) cur= ;; esac if [ -n "$cur" ] && plain_dir "$dir/$cur" && plain_file "$dir/$cur/update.sh" && [ -x "$dir/$cur/update.sh" ]; then set -- --dir "$dir" [ -z "$back" ] || set -- "$@" --back [ -z "$keep_old" ] || set -- "$@" --keep-old say "Sl0ptiminer is installed here. Its own updater takes over: $dir/current/update.sh" exec "$dir/$cur/update.sh" "$@" fi # --back: use the version that was in use before the last change. Nothing is downloaded or removed. if [ -n "$back" ]; then now=$(readlink "$dir/current" 2>/dev/null || true) prev=$(cat "$dir/.previous" 2>/dev/null || true) case "$prev" in '' | . | .. | *[!A-Za-z0-9._-]*) prev= ;; sloptiminer-[0-9]*-linux-* | sloptiminer-[0-9]*-macos-*) ;; *) prev= ;; esac [ -n "$prev" ] && plain_dir "$dir/$prev" && plain_file "$dir/$prev/sloptiminer" && [ -x "$dir/$prev/sloptiminer" ] || stop "There is no earlier version in $dir to go back to." ln -sfn "$prev" "$dir/current" remember "$now" say "Now using $prev. $now stays in $dir." say "Restart the miner to use it. Its first start measures the CPU again; that takes up to two minutes." say "To return to the newest version, run the install command again." exit 0 fi # 1. can this machine run it? [ -n "$version" ] || stop "No public build yet. Releases appear at https://sl0ptiminer.com/download/" os=$(uname -s) case "$os" in Linux | Darwin) ;; *) stop "This installer is for Linux and macOS. Other systems: https://sl0ptiminer.com/guides/" ;; esac # glibc 2.31 or newer gets the package that also mines on graphics cards; any other system the static one. # On ARM that package reaches a card through an OpenCL library; a system without one is told so further down. # The glibc package is picked only on a clear answer: the probe succeeds, says "glibc N.N" with 2.31 or more, and # the programs of this system are 64-bit (a 64-bit kernel can run a 32-bit system), and the system's library list # names the 64-bit loader that package starts with. Anything else, or no clear answer: the static one. libc=$(getconf GNU_LIBC_VERSION 2>/dev/null) || libc= bits=$(getconf LONG_BIT 2>/dev/null) || bits= v=${libc#glibc } case "$libc" in "glibc "[0-9]*.[0-9]*) ;; *) v= ;; esac case "$v" in *[!0-9.]* | *..* | *.) v= ;; esac glibc231= if [ -n "$v" ] && [ "$bits" = 64 ]; then major=${v%%.*}; minor=${v#*.}; minor=${minor%%.*} if [ "$major" -gt 2 ] || { [ "$major" -eq 2 ] && [ "$minor" -ge 31 ]; }; then glibc231=1; fi fi case "$os-$(uname -m)" in # A Mac: Apple silicon only. A Terminal that runs under Rosetta says x86_64; the machine itself answers sysctl. Darwin-*) [ "$(uname -m)" = arm64 ] || [ "$(sysctl -n hw.optional.arm64 2>/dev/null)" = 1 ] || stop "Sl0ptiminer for macOS needs a Mac with Apple silicon (M1 or newer). Other systems: https://sl0ptiminer.com/guides/" arm=1; file=$file_mac; sha256=$sha256_mac; want=macos-aarch64 ;; *-x86_64 | *-amd64) arm=; want=linux-x86_64-v3 if [ -n "$file_gnu" ] && gnu_ok ld-linux-x86-64.so.2 x86-64; then file=$file_gnu; sha256=$sha256_gnu; want=linux-x86_64-gnu; fi ;; *-aarch64 | *-arm64) arm=1; file=$file_arm; sha256=$sha256_arm; want=linux-aarch64 if [ -n "$file_arm_gnu" ] && gnu_ok ld-linux-aarch64.so.1 aarch64; then file=$file_arm_gnu; sha256=$sha256_arm_gnu; want=linux-aarch64-gnu listed=0; lib_listed libOpenCL.so.1 aarch64 || listed=$? [ "$listed" != 1 ] || no_opencl=1 fi ;; *) stop "Sl0ptiminer needs a 64-bit x86 or 64-bit ARM processor; this machine is $(uname -m). Other systems: https://sl0ptiminer.com/guides/" ;; esac [ -n "$file" ] || stop "Version $version has no package for this processor. Packages: https://sl0ptiminer.com/download/" # The package this machine gets has one name, and the folder inside it the same name: nothing else is taken. top="sloptiminer-$version-$want" case "$top" in *[!A-Za-z0-9._-]*) stop "This installer is damaged. Nothing was installed. Get it again: https://sl0ptiminer.com/install.sh" ;; esac [ "$file" = "$top.tar.gz" ] || stop "This installer is damaged. Nothing was installed. Get it again: https://sl0ptiminer.com/install.sh" [ ! -d /hive ] || stop "This is HiveOS. Install Sl0ptiminer from a flight sheet: https://sl0ptiminer.com/guides/hiveos/" # Mining on the processor needs AVX2, FMA and BMI2. Without them the glibc package still mines on graphics cards # (the miner turns processor mining off by itself); the static package mines on the processor only, so it is refused. cpu_note= if [ -z "$arm" ] && [ -r /proc/cpuinfo ]; then lacks= for flag in avx2 fma bmi2; do grep -qw "$flag" /proc/cpuinfo || lacks="$lacks $flag" done if [ -n "$lacks" ]; then [ "$want" = linux-x86_64-gnu ] || stop "This processor lacks:$lacks. Mining on the processor needs AVX2, FMA and BMI2, and this system can run only the package that mines on the processor (the one for graphics cards needs glibc 2.31 or newer)." cpu_note="~ This processor lacks:$lacks. CPU mining stays off on it; the graphics cards mine." fi fi have tar || stop "tar is missing. Install it and run this again." if have sha256sum; then sum='sha256sum'; elif have shasum; then sum='shasum -a 256'; else stop "sha256sum is missing. Install coreutils and run this again."; fi if have curl; then get='curl -fsSL -o'; elif have wget; then get='wget -q -O'; else stop "curl or wget is needed. Install one and run this again."; fi # 2. download into a scratch folder that is removed on every way out tmp=$(mktemp -d) base="$site/dl/$version" say "Sl0ptiminer $version" say "Licence: $site/licence/ ยท installing or running Sl0ptiminer means you accept it" [ -z "$cpu_note" ] || say "$cpu_note" $get "$tmp/$file" "$base/$file" || stop "Could not download $base/$file. Check the connection and run this again." $get "$tmp/SHA256SUMS" "$base/SHA256SUMS" || stop "Could not download the checksum list. Run this again." $get "$tmp/SHA256SUMS.minisig" "$base/SHA256SUMS.minisig" || stop "Could not download the signature. Run this again." say "+ downloaded $file" if [ -n "$no_list" ]; then say "~ the system's library list (ldconfig) could not be read, so this is the package that runs on any Linux and mines on the processor only." fi if [ -n "$no_opencl" ]; then say "~ the system's library list (ldconfig) names no OpenCL library (libOpenCL.so.1). Without one the miner mines on the processor only." say " With an NVIDIA card: install its driver. The miner then also mines on the card from its next start; no new install is needed." fi # 3. the package must match the checksum in this file and the signed list got=$($sum "$tmp/$file" | cut -d ' ' -f 1) [ "$got" = "$sha256" ] || stop "The downloaded package does not match its checksum. Nothing was installed. Run this again; if it repeats, write to security@sl0ptiminer.com." grep -qxF "$sha256 $file" "$tmp/SHA256SUMS" || stop "The checksum list does not name this package. Nothing was installed. Write to security@sl0ptiminer.com." say "+ checksum matches" # The signature says the list is ours. minisign checks it; without minisign, OpenSSL 3 can, and without that # Python 3. A machine with none of the three cannot check it, and nothing is installed there (owner 2026-11-21). wrong="The signature of the checksum list is wrong. Nothing was installed. Write to security@sl0ptiminer.com." if have minisign; then minisign -Vm "$tmp/SHA256SUMS" -P "$key" >/dev/null 2>&1 || stop "$wrong" say "+ signature checked (minisign)" elif openssl version 2>/dev/null | grep -q '^OpenSSL [3-9]'; then signed_by_key "$tmp/SHA256SUMS" "$tmp/SHA256SUMS.minisig" "$key" "$tmp" || stop "$wrong" say "+ signature checked (OpenSSL)" # (On a Mac without the developer tools, /usr/bin/python3 only offers to install them: it is not run there.) elif { [ "$os" != Darwin ] || [ "$(command -v python3)" != /usr/bin/python3 ] || xcode-select -p >/dev/null 2>&1; } && python3 -c 'import hashlib; hashlib.blake2b' >/dev/null 2>&1; then signed_by_key_py "$tmp/SHA256SUMS" "$tmp/SHA256SUMS.minisig" "$key" || stop "$wrong" say "+ signature checked (Python)" else if [ "$os" = Darwin ]; then how='brew install minisign (Homebrew: https://brew.sh)' elif have apt-get; then how='sudo apt install minisign' elif have dnf; then how='sudo dnf install minisign' elif have pacman; then how='sudo pacman -S minisign' elif have zypper; then how='sudo zypper install minisign' elif have apk; then how='sudo apk add minisign' else how='see https://sl0ptiminer.com/wiki/verify/'; fi stop "The signature cannot be checked here: this machine has no minisign, no OpenSSL 3 and no Python 3. Nothing was installed. Install minisign and run this again: $how" fi # 4. unpack next to earlier versions # Before anything is unpacked: every entry must lie inside the one folder the package is named for, be there once, # and be a plain file or a folder. No link, no other kind of entry, no path that leads out. (The package as a whole # was checked against its signed checksum above; which files it holds is not pinned here, so that a later package # may hold one more.) layout="The package has an unexpected layout. Nothing was installed." tar -tzf "$tmp/$file" > "$tmp/entries" || stop "$layout" while IFS= read -r entry; do entry=${entry#./} case "$entry" in '' | "$top" | "$top"/*) ;; *) stop "$layout" ;; esac case "/$entry/" in */../*) stop "$layout" ;; esac done < "$tmp/entries" [ -z "$(sed -e 's|^\./||' -e 's|/$||' "$tmp/entries" | sort | uniq -d)" ] || stop "$layout" tar -tvzf "$tmp/$file" > "$tmp/kinds" || stop "$layout" if cut -c 1 "$tmp/kinds" | grep -q '[^d-]'; then stop "$layout"; fi [ -d "$dir" ] || (umask 022 && mkdir -p "$dir") [ -n "$locked" ] || lock before=$(readlink "$dir/current" 2>/dev/null || true) # Unpacked inside $dir, so the last step is one rename on one file system: an interrupted run never leaves a # half-copied version folder. What a cut-off run did leave, its .incoming folder, goes now: this run holds the lock. rm -rf "$dir"/.incoming.* incoming="$dir/.incoming.$$" mkdir "$incoming" tar -xzf "$tmp/$file" -C "$incoming" plain_file "$incoming/$top/sloptiminer" && [ -x "$incoming/$top/sloptiminer" ] || stop "$layout" # The program must start on this machine before it is taken into use: a wrong C library or processor shows here. "$incoming/$top/sloptiminer" --version >/dev/null 2>&1 || if [ "$os" = Darwin ]; then stop "This package does not start on this Mac. Nothing was installed. See https://sl0ptiminer.com/guides/mac/"; else stop "This package does not start on this machine. Nothing was installed. The Linux packages need glibc 2.31 or newer: https://sl0ptiminer.com/guides/linux/"; fi if [ -e "$dir/$top" ] || [ -L "$dir/$top" ]; then # A folder of this name is used only when it is this very package: the same files, and nothing else. plain_dir "$dir/$top" && same_folder "$incoming/$top" "$dir/$top" || stop "$dir/$top is there already and is not this package. Nothing was changed. Move it away and run this again." rm -rf "$incoming" say "+ $version is already in $dir/$top" else mv "$incoming/$top" "$dir/$top" rmdir "$incoming" say "+ unpacked to $dir/$top" fi ln -sfn "$top" "$dir/current" if [ -n "$before" ] && [ "$before" != "$top" ]; then remember "$before"; fi # Old versions: the one in use now and the one before it stay; older ones go. Only what the installer made is # touched: a plain folder named like this package with a version in the name and the program inside. Never a link. if [ -z "$keep_old" ]; then prev=$(cat "$dir/.previous" 2>/dev/null || true) for old in "$dir"/sloptiminer-*-"$want"; do name=${old##*/} mid=${name#sloptiminer-}; mid=${mid%"-$want"} case "$mid" in '' | *[!0-9A-Za-z.]*) continue ;; esac plain_dir "$old" && plain_file "$old/sloptiminer" || continue [ "$name" != "$top" ] && [ "$name" != "$prev" ] || continue rm -rf "$old" say "- removed the older version $name" done fi # 5. what to do next say "" if [ -n "$before" ] && [ "$before" != "$top" ]; then say "Updated from $before to $top. The old version stays in $dir/$before." say "Restart the miner to use the new version. Its first start measures the CPU again; that takes up to two minutes." say "What changed: https://sl0ptiminer.com/releases/" say "To go back to the old version: curl -fsSL https://sl0ptiminer.com/install.sh | sh -s -- --back" elif [ "$before" = "$top" ]; then say "This is the current version. Nothing changed." else say "Start mining with the algorithm, your pool and your address:" say "" say " cd $dir/current" say " ./sloptiminer mine -a ALGORITHM -o HOST:PORT -u YOUR_ADDRESS.rig1" say "" say "ALGORITHM is the algorithm you mine, for example hoohash: https://sl0ptiminer.com/algorithms/" say "The first start tests the miner and measures the CPU once; it takes up to two minutes." say "Quick start: https://sl0ptiminer.com/wiki/quick-start/" say "To update later, run the install command again." fi } # signed_by_key LIST SIGNATURE KEY SCRATCH: a minisign signature checked with OpenSSL 3. A minisign key is "Ed", an # 8-byte key number and a 32-byte Ed25519 key; a signature is "ED" (over the BLAKE2b-512 hash of the file, the usual # form) or "Ed" (over the file), the key number and 64 bytes; a second signature covers the first and the trusted comment. signed_by_key() { t="$4/sig" mkdir -p "$t" || return 1 printf '%s\n' "$3" | openssl base64 -d -A > "$t/key" 2>/dev/null || return 1 sed -n 2p "$2" | openssl base64 -d -A > "$t/sig" 2>/dev/null || return 1 sed -n 4p "$2" | openssl base64 -d -A > "$t/global" 2>/dev/null || return 1 [ "$(wc -c < "$t/key")" -eq 42 ] || return 1 [ "$(wc -c < "$t/sig")" -eq 74 ] || return 1 [ "$(wc -c < "$t/global")" -eq 64 ] || return 1 head -c 10 "$t/key" | tail -c 8 > "$t/kid" head -c 10 "$t/sig" | tail -c 8 > "$t/sid" cmp -s "$t/kid" "$t/sid" || return 1 { printf '%s\n' '-----BEGIN PUBLIC KEY-----' printf 'MCowBQYDK2VwAyEA%s\n' "$(tail -c 32 "$t/key" | openssl base64 -A)" printf '%s\n' '-----END PUBLIC KEY-----' } > "$t/pem" tail -c 64 "$t/sig" > "$t/s" case "$(head -c 2 "$t/sig")" in ED) openssl dgst -blake2b512 -binary "$1" > "$t/msg" || return 1 ;; Ed) cp "$1" "$t/msg" || return 1 ;; *) return 1 ;; esac openssl pkeyutl -verify -pubin -inkey "$t/pem" -rawin -in "$t/msg" -sigfile "$t/s" >/dev/null 2>&1 || return 1 { cat "$t/s"; sed -n 3p "$2" | sed 's/^trusted comment: //' | tr -d '\n'; } > "$t/gmsg" openssl pkeyutl -verify -pubin -inkey "$t/pem" -rawin -in "$t/gmsg" -sigfile "$t/global" >/dev/null 2>&1 || return 1 } # signed_by_key_py LIST SIGNATURE KEY: the same check with Python 3 alone (3.6 or newer), for a machine that has # neither minisign nor OpenSSL 3. Ed25519 as RFC 8032 section 6 writes it: checking only, with public values only. signed_by_key_py() { python3 - "$1" "$2" "$3" <<'PY' import base64, hashlib, sys p = 2**255 - 19 L = 2**252 + 27742317777372353535851937790883648493 d = -121665 * pow(121666, p - 2, p) % p I = pow(2, (p - 1) // 4, p) def point(b): # 32 bytes -> a point (x, y, z, t), or an error when they are not one y = int.from_bytes(b, 'little') sign, y = y >> 255, y & ((1 << 255) - 1) if y >= p: raise ValueError xx = (y * y - 1) * pow(d * y * y + 1, p - 2, p) % p x = pow(xx, (p + 3) // 8, p) if (x * x - xx) % p: x = x * I % p if (x * x - xx) % p or (x == 0 and sign): raise ValueError if x & 1 != sign: x = p - x return (x, y, 1, x * y % p) def add(P, Q): A, B = (P[1] - P[0]) * (Q[1] - Q[0]) % p, (P[1] + P[0]) * (Q[1] + Q[0]) % p C, D = 2 * P[3] * Q[3] * d % p, 2 * P[2] * Q[2] % p E, F, G, H = B - A, D - C, D + C, B + A return (E * F % p, G * H % p, F * G % p, E * H % p) def mul(s, P): Q = (0, 1, 1, 0) while s: if s & 1: Q = add(Q, P) P, s = add(P, P), s >> 1 return Q def good(key, message, sig): if len(key) != 32 or len(sig) != 64: return False try: A, R = point(key), point(sig[:32]) except ValueError: return False s = int.from_bytes(sig[32:], 'little') if s >= L: return False h = int.from_bytes(hashlib.sha512(sig[:32] + key + message).digest(), 'little') % L X, Y = mul(s, point((4 * pow(5, p - 2, p) % p).to_bytes(32, 'little'))), add(R, mul(h, A)) return (X[0] * Y[2] - Y[0] * X[2]) % p == 0 and (X[1] * Y[2] - Y[1] * X[2]) % p == 0 try: listed = open(sys.argv[1], 'rb').read() lines = open(sys.argv[2], 'rb').read().split(b'\n') key, sig, both = base64.b64decode(sys.argv[3], validate=True), base64.b64decode(lines[1], validate=True), base64.b64decode(lines[3], validate=True) comment = lines[2] if len(key) != 42 or len(sig) != 74 or len(both) != 64 or key[2:10] != sig[2:10] or not comment.startswith(b'trusted comment: '): sys.exit(1) message = hashlib.blake2b(listed).digest() if sig[:2] == b'ED' else listed if sig[:2] == b'Ed' else None ok = message is not None and good(key[10:], message, sig[10:]) and good(key[10:], sig[10:] + comment[len(b'trusted comment: '):], both) except Exception: ok = False sys.exit(0 if ok else 1) PY } # lib_listed NAME KIND: 0 when the system's library list names that library for that kind of processor, 1 when it # names none, 2 when the list cannot be read. The NVIDIA driver setup brings the OpenCL library. # ponytail: the list only (ldconfig -p), no search of folders; a library outside the list is not seen. lib_listed() { if have ldconfig; then l=ldconfig; elif [ -x /sbin/ldconfig ]; then l=/sbin/ldconfig; elif [ -x /usr/sbin/ldconfig ]; then l=/usr/sbin/ldconfig; else return 2; fi libs=$("$l" -p 2>/dev/null) || return 2 [ -n "$libs" ] || return 2 if printf '%s\n' "$libs" | grep -F "$1" | grep -qi "$2"; then return 0; fi return 1 } # gnu_ok LOADER KIND: this system can start the glibc package: glibc 2.31 or newer, and the library list names the # loader that package starts with. A list that cannot be read is no clear answer: no, and the user is told. gnu_ok() { [ -n "$glibc231" ] || return 1 listed=0; lib_listed "$1" "$2" || listed=$? [ "$listed" != 2 ] || no_list=1 [ "$listed" = 0 ] } # plain_dir PATH: a folder of this user's own that is not a link. plain_file PATH: the same for a file. While the install # folder was open to others (0.8.2, umask 002), someone else could have put a folder or file of their own in it; such a # folder is never run, switched to or removed. plain_dir() { [ -d "$1" ] && [ ! -L "$1" ] && [ -O "$1" ]; } plain_file() { [ -f "$1" ] && [ ! -L "$1" ] && [ -O "$1" ]; } # same_files A B: every file of folder A is in folder B as a plain file with the same bytes same_files() { (cd "$1" && find . -type f) | while IFS= read -r f; do [ -f "$2/$f" ] && [ ! -L "$2/$f" ] && cmp -s "$1/$f" "$2/$f" || exit 1 done } # same_folder A B: the two folders hold the same plain files, and B holds nothing but files and folders same_folder() { same_files "$1" "$2" && same_files "$2" "$1" && [ -z "$(find "$2" ! -type f ! -type d 2>/dev/null)" ]; } # remember NAME: write down the version that was in use before, through a new file of its own name, so that nothing # already lying in the folder is written through remember() { note=$(mktemp "$dir/.previous.XXXXXX") && printf '%s\n' "$1" > "$note" && mv -f "$note" "$dir/.previous"; } # safe_path DIR: DIR and every folder above it belong to this user or to root, and nobody else can write to them. # Above DIR a sticky folder such as /tmp counts as closed: only an entry's owner can replace it. When it fails, # $unsafe is the folder that did; $open is DIR when DIR is writable by others, and DIR is reported only after every # folder above it passed. ponytail: owner and permission bits only; access lists are not read. safe_path() { unsafe=$(cd "$1" 2>/dev/null && pwd -P) || { unsafe=$1; return 1; } me=$(id -u); leaf=1; open= while :; do line=$(ls -ldn "$unsafe" 2>/dev/null) || return 1 set -f; set -- $line; set +f [ $# -ge 3 ] || return 1 case "$3" in "$me" | 0) ;; *) return 1 ;; esac case "$1" in d*) ;; *) return 1 ;; esac case "$1" in d????w* | d???????w*) if [ -n "$leaf" ]; then open=$unsafe; else case "$1" in d????????[tT]*) ;; *) return 1 ;; esac; fi ;; esac leaf= if [ "$unsafe" = / ]; then [ -z "$open" ] || { unsafe=$open; return 1; }; return 0; fi unsafe=$(dirname "$unsafe") done } # lock: one install or update at a time in this folder, from the first look at it to the last clean-up. The lock is # a folder that holds the number of the process; the install command hands it on to the updater it starts, which is # the same process. A lock whose process is gone is taken over. # ponytail: two runs that find a dead run's lock in the same instant can both take it. lock() { busy="Another install or update is running in $dir. Wait for it to end and run this again." if ! mkdir "$dir/.lock" 2>/dev/null; then holder=$(cat "$dir/.lock/pid" 2>/dev/null || true) if [ -z "$holder" ]; then sleep 1; holder=$(cat "$dir/.lock/pid" 2>/dev/null || true); fi case "$holder" in *[!0-9]*) holder= ;; esac if [ "$holder" != "$$" ]; then if [ -n "$holder" ] && kill -0 "$holder" 2>/dev/null; then stop "$busy"; fi rm -rf "$dir/.lock" mkdir "$dir/.lock" 2>/dev/null || stop "$busy" fi fi printf '%s\n' "$$" > "$dir/.lock/pid" locked=1 } say() { printf '%s\n' "$*"; } stop() { printf '!! %s\n' "$*" >&2; exit 1; } have() { command -v "$1" >/dev/null 2>&1; } # The whole file is read before anything runs, so a cut-off download cannot run half of it. main "$@"