diff --git a/scripts/libmakepkg/util/lock.sh.in b/scripts/libmakepkg/util/lock.sh.in new file mode 100644 index 00000000..b7a9437d --- /dev/null +++ b/scripts/libmakepkg/util/lock.sh.in @@ -0,0 +1,31 @@ +#!/bin/bash +# +# lock.sh - functions to handle pacman locking +# +# Copyright (c) 2024 Pacman Development Team +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# + +is_pacman_locked() { + local lockfile="$(pacman-conf DBPath)/db.lck" + if [[ -f $lockfile ]]; then + local pid=$(<"$lockfile") + # Check if the pid is valid and the process is still running + if [[ -n $pid && $pid =~ ^[0-9]+$ && -d /proc/$pid ]]; then + return 0 + fi + fi + return 1 +} \ No newline at end of file diff --git a/scripts/libmakepkg/util/meson.build b/scripts/libmakepkg/util/meson.build index a160fd06..38c02ff0 100644 --- a/scripts/libmakepkg/util/meson.build +++ b/scripts/libmakepkg/util/meson.build @@ -5,6 +5,7 @@ sources = [ 'config.sh.in', 'dirsize.sh.in', 'error.sh.in', + 'lock.sh.in', 'message.sh.in', 'option.sh.in', 'parseopts.sh.in', diff --git a/scripts/makepkg.sh.in b/scripts/makepkg.sh.in index 75df3650..a44d59c2 100644 --- a/scripts/makepkg.sh.in +++ b/scripts/makepkg.sh.in @@ -247,14 +247,11 @@ run_pacman() { else cmd=(su root -c "$cmdescape") fi - local lockfile="$(pacman-conf DBPath)/db.lck" - while [[ -f $lockfile ]]; do - local timer=0 + local timer=0 + while is_pacman_locked && (( timer < 10 )); do + (( ++timer )) msg "$(gettext "Pacman is currently in use, please wait...")" - while [[ -f $lockfile ]] && (( timer < 10 )); do - (( ++timer )) - sleep 3 - done + sleep 3 done fi "${cmd[@]}" diff --git a/scripts/pacman-db-upgrade.sh.in b/scripts/pacman-db-upgrade.sh.in index 6942cede..363060c7 100644 --- a/scripts/pacman-db-upgrade.sh.in +++ b/scripts/pacman-db-upgrade.sh.in @@ -30,6 +30,7 @@ declare -r myver='@PACKAGE_VERSION@' MAKEPKG_LIBRARY=${MAKEPKG_LIBRARY:-'@libmakepkgdir@'} # Import libmakepkg +source "$MAKEPKG_LIBRARY"/util/lock.sh source "$MAKEPKG_LIBRARY"/util/message.sh source "$MAKEPKG_LIBRARY"/util/parseopts.sh @@ -132,15 +133,18 @@ fi # strip any trailing slash from our dbroot dbroot="${dbroot%/}" + +# make sure pacman isn't running +if is_pacman_locked; then + die "$(gettext "Pacman lock file was found. Cannot run while pacman is running.")" +fi + # form the path to our lockfile location lockfile="${dbroot}/db.lck" -# make sure pacman isn't running -if [[ -f $lockfile ]]; then - die "$(gettext "Pacman lock file was found. Cannot run while pacman is running.")" -fi # do not let pacman run while we do this -touch "$lockfile" +# write our pid to the lockfile +echo $$ > "$lockfile" if [[ -f "${dbroot}"/local/ALPM_DB_VERSION ]]; then db_version=$(cat "${dbroot}"/local/ALPM_DB_VERSION)