Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ jobs:
archlinux:base-devel bash -lc '
set -euo pipefail
pacman -Syu --noconfirm git jq python libarchive
python tests/oma-service-removal.py
python tests/upstream-watch.py
./bin/sync-upstream self-test
./bin/sync-rebuilds --self-test
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,4 @@ pkgbuilds/yay/yay/

# Python helpers and offline tests
__pycache__/
.release-verification/
18 changes: 13 additions & 5 deletions pkgbuilds/omaspeak-bin/PKGBUILD
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

pkgname=omaspeak-bin
_pkgname=${pkgname%-bin}
pkgver=0.0.1
_upstream_ver=0.0.1
pkgrel=1
pkgver=0.0.3
_upstream_ver=0.0.3
pkgrel=4
pkgdesc='Local-first text-to-speech application and daemon (pre-built binary)'
arch=('x86_64' 'aarch64')
url='https://github.com/jacob-vincent-mink/omaspeak'
Expand All @@ -27,13 +27,21 @@ conflicts=("${_pkgname}")
install="${pkgname}.install"
options=('!strip' '!debug')

source=('package-remove' 'remove-user-services.hook')
sha256sums=('f1b527448529b45fee2f96b4c0a19b11087377c0ac9f43842bab0d04fcfd3b9f'
'9f1a0c2f5031fcd5905de77643a8727b792e07c582c09c2ba179f0714f118b20')

source_x86_64=("${_pkgname}-${_upstream_ver}-linux-x86_64.tar.xz::${url}/releases/download/v${_upstream_ver}/${_pkgname}-${_upstream_ver}-linux-x86_64.tar.xz")
source_aarch64=("${_pkgname}-${_upstream_ver}-linux-aarch64.tar.xz::${url}/releases/download/v${_upstream_ver}/${_pkgname}-${_upstream_ver}-linux-aarch64.tar.xz")

sha256sums_x86_64=('9e318960fb15fdf955efbb8dda9bc8eb2b9d0932a9acd9e31b85bf3492ca78ea')
sha256sums_aarch64=('8a0d7728d0b6d3f447ab7a616389fc8c54a0617f14922b33593ca60b425deb8d')
sha256sums_x86_64=('c72428bf6989582b5aa802f39e9390e4cacf26f7fbfacf76645118286c7d1ab2')
sha256sums_aarch64=('88fc4ea8c275b9d5b9d41602d32dbca77ee30de0fc7dcbc06ad0e819fd41545a')

package() {
install -Dm755 "${srcdir}/package-remove" "${pkgdir}/usr/lib/${_pkgname}/package-remove"
install -Dm644 "${srcdir}/remove-user-services.hook" \
"${pkgdir}/usr/share/libalpm/hooks/30-${_pkgname}-remove-user-services.hook"

local release_root="${srcdir}/${_pkgname}-${_upstream_ver}-linux-${CARCH}"

install -Dm755 "${release_root}/${_pkgname}" "${pkgdir}/usr/bin/${_pkgname}"
Expand Down
150 changes: 150 additions & 0 deletions pkgbuilds/omaspeak-bin/package-remove
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
#!/bin/bash
# Invoked only by the package's Remove/PreTransaction ALPM hook.
set -eu

# systemd's configuration parser throws the whitespace around an assignment away
# (parse_line() runs both halves through strstrip()), so a drop-in written as
# ExecStart = /home/alice/build/omawake daemon
# picks the executable just as surely as the unspaced form does.
readonly execstart_assignment='^[[:space:]]*ExecStart[[:space:]]*='

owned_unit() {
local commands
[[ -f $1 && ! -L $1 ]] || return 1
# Only a unit in the very shape the application generates is ever deleted;
# anything else, however it is spaced, stays somebody's own file.
commands=$(grep '^ExecStart=' "$1") || return 1
[[ $commands != *$'\n'* ]] || return 1
grep -Eq "^ExecStart=\"?/usr/bin/$2\"?([[:space:]]|$)" <<< "$commands"
}

# Decode systemd's shell_maybe_quote() output, including cescape_char() escapes.
# Never evaluate manager-controlled values as shell syntax.
unquote_manager_value() {
local text=$1 decoded= character octal
if [[ $text != '$'* ]]; then
unquoted_value=$text
return 0
fi
[[ $text == \$\'*\' ]] || return 1
text=${text:2:${#text}-3}
while [[ -n $text ]]; do
if [[ ${text:0:1} != \\ ]]; then
decoded+=${text:0:1}
text=${text:1}
continue
fi
case ${text:1:1} in
\\ | "'") decoded+=${text:1:1} ;;
a | b | f | n | r | t | v)
printf -v character '%b' "\\${text:1:1}"
decoded+=$character
;;
[0-3])
octal=${text:1:3}
[[ $octal =~ ^[0-3][0-7]{2}$ && $octal != 000 ]] || return 1
printf -v character '%b' "\\0$octal"
decoded+=$character
text=${text:4}
continue
;;
*) return 1 ;;
esac
text=${text:2}
done
unquoted_value=$decoded
}

remove_for_user() {
local app=$1 user_home=$2 runtime=$3 config="$2/.config" unit="$1.service"
local online=false effective path target manager_environment load_state
if [[ -S $runtime/bus || -S $runtime/systemd/private ]]; then
online=true
# Do not evaluate shell syntax from a user manager's environment.
manager_environment=$(systemctl --user show-environment) || return 1
while IFS= read -r line; do
case $line in
XDG_CONFIG_HOME=*)
if ! unquote_manager_value "${line#XDG_CONFIG_HOME=}"; then
echo ":: Cannot tell which directory $user_home's manager reads $unit from; removal aborted." >&2
return 1
fi
# An XDG_CONFIG_HOME that is not an absolute path is no setting at all:
# the manager itself falls back to the home's .config directory then.
if [[ $unquoted_value == /* ]]; then config=$unquoted_value; fi
;;
esac
done <<< "$manager_environment"
effective=$(systemctl --user show "$unit" --property=ExecStart --value) || return 1
if [[ -n $effective && $effective != *"path=/usr/bin/$app ;"* ]]; then
echo ":: Preserving $unit for $user_home: it uses another executable."
return 0
fi
fi
path="$config/systemd/user/$unit"
if ! $online && grep -qs "$execstart_assignment" "$path.d/"*.conf; then
echo ":: Preserving offline service with an executable override: $path."
return 0
fi
if [[ -e $path || -L $path ]]; then
if ! owned_unit "$path" "$app"; then
echo ":: Preserving custom or masked unit $path."
return 0
fi
fi
if $online; then
# Stop first, and fail the package transaction if stopping fails.
load_state=$(systemctl --user show "$unit" --property=LoadState --value) || return 1
if [[ $load_state != not-found ]]; then
systemctl --user stop "$unit" || return 1
# A unit that had failed stays failed once it is stopped, which is the one
# state systemd will reset. For every other state it answers that the unit
# is not loaded and exits non-zero, so the reset is asked for only where it
# applies: a healthy unit leaving the transaction never aborts a removal.
if [[ $(systemctl --user show "$unit" --property=ActiveState --value) == failed ]]; then
systemctl --user reset-failed "$unit" || return 1
fi
# Disabling also removes enablement links outside the normal target.
systemctl --user disable "$unit" || return 1
fi
fi
# Offline users have no bus. Remove only exact enablement links for this unit.
# All filesystem operations run as the owning user, never as pacman's root.
for target in "$config/systemd/user/"*.wants/"$unit" "$config/systemd/user/"*.requires/"$unit"; do
[[ -L $target ]] || continue
case $(realpath -m -- "$target") in
"$path"|"/usr/lib/systemd/user/$unit") rm -- "$target" ;;
esac
done
if owned_unit "$path" "$app"; then rm -- "$path"; fi
if $online; then systemctl --user daemon-reload || return 1; fi
}

if [[ ${1-} == --user ]]; then
shift
case ${1-} in omawake|omaspeak) ;; *) exit 2 ;; esac
[[ $# == 3 ]] || exit 2
remove_for_user "$@"
exit
fi

[[ $# == 1 ]] || exit 2
case $1 in omawake|omaspeak) ;; *) exit 2 ;; esac
app=$1
result=0
# Include logged-out users as well as active/lingering user managers.
accounts=$(getent passwd) || exit 1
while IFS=: read -r account _ user_id _ _ user_home _; do
[[ $user_home == /* ]] || continue
runtime="/run/user/$user_id"
if [[ ! -d $user_home/.config/systemd/user && ! -S $runtime/bus && ! -S $runtime/systemd/private ]]; then
continue
fi
if ! runuser -u "$account" -- env -u XDG_CONFIG_HOME \
XDG_RUNTIME_DIR="$runtime" DBUS_SESSION_BUS_ADDRESS="unix:path=$runtime/bus" \
"$0" --user "$app" "$user_home" "$runtime"; then
echo ":: Could not clean up $app for $account; removal aborted. Stop/remove the user service and retry." >&2
result=1
fi
done <<< "$accounts"
exit "$result"
10 changes: 10 additions & 0 deletions pkgbuilds/omaspeak-bin/remove-user-services.hook
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[Trigger]
Operation = Remove
Type = Package
Target = omaspeak-bin

[Action]
Description = Stop and remove omaspeak user services before package removal
When = PreTransaction
Exec = /usr/lib/omaspeak/package-remove omaspeak
AbortOnFail
18 changes: 13 additions & 5 deletions pkgbuilds/omawake-bin/PKGBUILD
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

pkgname=omawake-bin
_pkgname=${pkgname%-bin}
pkgver=0.0.2
_upstream_ver=0.0.2
pkgrel=1
pkgver=0.0.3
_upstream_ver=0.0.3
pkgrel=4
pkgdesc='Configurable local wake-word daemon (pre-built binary)'
arch=('x86_64' 'aarch64')
url='https://github.com/jacob-vincent-mink/omawake'
Expand All @@ -27,13 +27,21 @@ conflicts=("${_pkgname}")
install="${pkgname}.install"
options=('!strip' '!debug')

source=('package-remove' 'remove-user-services.hook')
sha256sums=('f1b527448529b45fee2f96b4c0a19b11087377c0ac9f43842bab0d04fcfd3b9f'
'a0bb2e9de807bdb2cc8d0076eac3c21555c910eb8baa06acfba18fea716b9014')

source_x86_64=("${_pkgname}-${_upstream_ver}-linux-x86_64.tar.xz::${url}/releases/download/v${_upstream_ver}/${_pkgname}-${_upstream_ver}-linux-x86_64.tar.xz")
source_aarch64=("${_pkgname}-${_upstream_ver}-linux-aarch64.tar.xz::${url}/releases/download/v${_upstream_ver}/${_pkgname}-${_upstream_ver}-linux-aarch64.tar.xz")

sha256sums_x86_64=('94f0677eb497babd351cb154e9adf057e4b796efe0241d809a7f7cf84742a515')
sha256sums_aarch64=('acb359b21bbe4909b13c18a0de63f8106c6b254943074b57d8fd70af41421659')
sha256sums_x86_64=('fa374341f60760b04c9a97d76f7ad2679463f5b2b673ee6d9c1ceee97d3f0669')
sha256sums_aarch64=('384eb11872c873a33332acf51f567dc4d4e327e57563b61056e4d0aa7fe470eb')

package() {
install -Dm755 "${srcdir}/package-remove" "${pkgdir}/usr/lib/${_pkgname}/package-remove"
install -Dm644 "${srcdir}/remove-user-services.hook" \
"${pkgdir}/usr/share/libalpm/hooks/30-${_pkgname}-remove-user-services.hook"

local release_root="${srcdir}/${_pkgname}-${_upstream_ver}-linux-${CARCH}"

install -Dm755 "${release_root}/${_pkgname}" "${pkgdir}/usr/bin/${_pkgname}"
Expand Down
150 changes: 150 additions & 0 deletions pkgbuilds/omawake-bin/package-remove
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
#!/bin/bash
# Invoked only by the package's Remove/PreTransaction ALPM hook.
set -eu

# systemd's configuration parser throws the whitespace around an assignment away
# (parse_line() runs both halves through strstrip()), so a drop-in written as
# ExecStart = /home/alice/build/omawake daemon
# picks the executable just as surely as the unspaced form does.
readonly execstart_assignment='^[[:space:]]*ExecStart[[:space:]]*='

owned_unit() {
local commands
[[ -f $1 && ! -L $1 ]] || return 1
# Only a unit in the very shape the application generates is ever deleted;
# anything else, however it is spaced, stays somebody's own file.
commands=$(grep '^ExecStart=' "$1") || return 1
[[ $commands != *$'\n'* ]] || return 1
grep -Eq "^ExecStart=\"?/usr/bin/$2\"?([[:space:]]|$)" <<< "$commands"
}

# Decode systemd's shell_maybe_quote() output, including cescape_char() escapes.
# Never evaluate manager-controlled values as shell syntax.
unquote_manager_value() {
local text=$1 decoded= character octal
if [[ $text != '$'* ]]; then
unquoted_value=$text
return 0
fi
[[ $text == \$\'*\' ]] || return 1
text=${text:2:${#text}-3}
while [[ -n $text ]]; do
if [[ ${text:0:1} != \\ ]]; then
decoded+=${text:0:1}
text=${text:1}
continue
fi
case ${text:1:1} in
\\ | "'") decoded+=${text:1:1} ;;
a | b | f | n | r | t | v)
printf -v character '%b' "\\${text:1:1}"
decoded+=$character
;;
[0-3])
octal=${text:1:3}
[[ $octal =~ ^[0-3][0-7]{2}$ && $octal != 000 ]] || return 1
printf -v character '%b' "\\0$octal"
decoded+=$character
text=${text:4}
continue
;;
*) return 1 ;;
esac
text=${text:2}
done
unquoted_value=$decoded
}

remove_for_user() {
local app=$1 user_home=$2 runtime=$3 config="$2/.config" unit="$1.service"
local online=false effective path target manager_environment load_state
if [[ -S $runtime/bus || -S $runtime/systemd/private ]]; then
online=true
# Do not evaluate shell syntax from a user manager's environment.
manager_environment=$(systemctl --user show-environment) || return 1
while IFS= read -r line; do
case $line in
XDG_CONFIG_HOME=*)
if ! unquote_manager_value "${line#XDG_CONFIG_HOME=}"; then
echo ":: Cannot tell which directory $user_home's manager reads $unit from; removal aborted." >&2
return 1
fi
# An XDG_CONFIG_HOME that is not an absolute path is no setting at all:
# the manager itself falls back to the home's .config directory then.
if [[ $unquoted_value == /* ]]; then config=$unquoted_value; fi
;;
esac
done <<< "$manager_environment"
effective=$(systemctl --user show "$unit" --property=ExecStart --value) || return 1
if [[ -n $effective && $effective != *"path=/usr/bin/$app ;"* ]]; then
echo ":: Preserving $unit for $user_home: it uses another executable."
return 0
fi
fi
path="$config/systemd/user/$unit"
if ! $online && grep -qs "$execstart_assignment" "$path.d/"*.conf; then
echo ":: Preserving offline service with an executable override: $path."
return 0
fi
if [[ -e $path || -L $path ]]; then
if ! owned_unit "$path" "$app"; then
echo ":: Preserving custom or masked unit $path."
return 0
fi
fi
if $online; then
# Stop first, and fail the package transaction if stopping fails.
load_state=$(systemctl --user show "$unit" --property=LoadState --value) || return 1
if [[ $load_state != not-found ]]; then
systemctl --user stop "$unit" || return 1
# A unit that had failed stays failed once it is stopped, which is the one
# state systemd will reset. For every other state it answers that the unit
# is not loaded and exits non-zero, so the reset is asked for only where it
# applies: a healthy unit leaving the transaction never aborts a removal.
if [[ $(systemctl --user show "$unit" --property=ActiveState --value) == failed ]]; then
systemctl --user reset-failed "$unit" || return 1
fi
# Disabling also removes enablement links outside the normal target.
systemctl --user disable "$unit" || return 1
fi
fi
# Offline users have no bus. Remove only exact enablement links for this unit.
# All filesystem operations run as the owning user, never as pacman's root.
for target in "$config/systemd/user/"*.wants/"$unit" "$config/systemd/user/"*.requires/"$unit"; do
[[ -L $target ]] || continue
case $(realpath -m -- "$target") in
"$path"|"/usr/lib/systemd/user/$unit") rm -- "$target" ;;
esac
done
if owned_unit "$path" "$app"; then rm -- "$path"; fi
if $online; then systemctl --user daemon-reload || return 1; fi
}

if [[ ${1-} == --user ]]; then
shift
case ${1-} in omawake|omaspeak) ;; *) exit 2 ;; esac
[[ $# == 3 ]] || exit 2
remove_for_user "$@"
exit
fi

[[ $# == 1 ]] || exit 2
case $1 in omawake|omaspeak) ;; *) exit 2 ;; esac
app=$1
result=0
# Include logged-out users as well as active/lingering user managers.
accounts=$(getent passwd) || exit 1
while IFS=: read -r account _ user_id _ _ user_home _; do
[[ $user_home == /* ]] || continue
runtime="/run/user/$user_id"
if [[ ! -d $user_home/.config/systemd/user && ! -S $runtime/bus && ! -S $runtime/systemd/private ]]; then
continue
fi
if ! runuser -u "$account" -- env -u XDG_CONFIG_HOME \
XDG_RUNTIME_DIR="$runtime" DBUS_SESSION_BUS_ADDRESS="unix:path=$runtime/bus" \
"$0" --user "$app" "$user_home" "$runtime"; then
echo ":: Could not clean up $app for $account; removal aborted. Stop/remove the user service and retry." >&2
result=1
fi
done <<< "$accounts"
exit "$result"
Loading
Loading