Skip to content

Commit

Permalink
Enhance check for other simultaneously running 'rear'
Browse files Browse the repository at this point in the history
In usr/sbin/rear enhance check for other simultaneously running 'rear'
to let it find running 'rear' instances independent of how it was called by the user
in particular also when 'rear' is run from a GitHub checkout/clone
cf. #2826 (comment)
  • Loading branch information
jsmeix committed Jul 1, 2022
1 parent 479f158 commit e04125b
Showing 1 changed file with 63 additions and 5 deletions.
68 changes: 63 additions & 5 deletions usr/sbin/rear
Expand Up @@ -384,16 +384,74 @@ readonly RUNTIME_LOGFILE
# When this currently running instance cannot run simultaneously with another instance
# test that this currently running instance does not run simultaneously with another instance:
if ! test "$can_run_simultaneously" ; then
# In this case pidof is needed to test what running instances there are:
# In this case pidof is needed to test what running instances are there:
if ! type pidof 1>/dev/null ; then
echo "ERROR: Required program 'pidof' missing, please check your PATH" >&2
echo "ERROR: Required program 'pidof' missing" >&2
exit 1
fi
# For unknown reasons '-o %PPID' does not work for pidof at least in SLES11
# so that a manual test is done to find out if another pid != $$ is running:
for pid in $( pidof -x "$SCRIPT_FILE" ) ; do
# so that a manual test is done to find out if another pid != $$ is running.
# This test is only some best effort attempt to find what running instances are there
# because what pidof finds depends how 'rear' is called and what pidof version is used.
# For example pidof in SLES10 SP4 seems to only consider the basename:
# # ps auxw | grep ssh
# ... 3132 ... /usr/sbin/sshd -o PidFile=/var/run/sshd.init.pid
# ... 3622 ... sshd: root@pts/1
# # pidof -x sshd
# 3622 3132
# # pidof -x /usr/sbin/sshd
# 3622 3132
# # pidof -x in/sshd
# 3622 3132
# In contrast pidof in openSUSE Leap 15.3 checks exactly what was specified when it contains a path:
# # ps auxw | grep ssh
# ... 2991 ... /usr/bin/ssh-agent /usr/bin/gpg-agent --sh --daemon --keep-display /etc/X11/xinit/xinitrc
# ... 18219 ... /usr/bin/ssh-agent -D -a /run/user/1000/keyring/.ssh
# # pidof -x "ssh-agent"
# 18219 2991
# # pidof -x "/usr/bin/ssh-agent"
# 18219 2991
# # pidof -x "usr/bin/ssh-agent"
# [no output]
# We do not want to use only the basename 'rear' to avoid that pidof accidentally also shows
# other 'rear' programs which are not Relax-and-Recover but some different software.
# Examples how 'rear' looks in the 'ps' output:
# When Relax-and-Recover is normally installed (e.g. as RPM package) so the 'rear' program is /usr/sbin/rear:
# # type -a rear
# rear is /usr/sbin/rear
# # rear mkrescue
# # ps auxw | grep rear
# ... /bin/bash /usr/sbin/rear mkrescue
# # cd /
# # ./usr/sbin/rear mkrescue
# # ps auxw | grep rear
# ... /bin/bash ./usr/sbin/rear mkrescue
# # cd /usr/sbin
# # ./rear mkrescue
# # ps auxw | grep rear
# ... /bin/bash ./rear mkrescue
# When a Relax-and-Recover GitHub code checkout/clone is used so the 'rear' program is /path/to/checkout/usr/sbin/rear:
# # /path/to/checkout/usr/sbin/rear mkrescue
# # ps auxw | grep rear
# ... /bin/bash /path/to/checkout/usr/sbin/rear mkrescue
# # cd /path/to/checkout
# # usr/sbin/rear mkrescue
# # ps auxw | grep rear
# ... /bin/bash usr/sbin/rear mkrescue
# # ./usr/sbin/rear mkrescue
# # ps auxw | grep rear
# ... /bin/bash ./usr/sbin/rear mkrescue
# # cd /path/to/checkout/usr/sbin
# # ./rear mkrescue
# # ps auxw | grep rear
# ... /bin/bash ./rear mkrescue
# So we check $SCRIPT_FILE which is /usr/sbin/rear or /path/to/checkout/usr/sbin/rear
# and /usr/sbin/rear usr/sbin/rear ./usr/sbin/rear ./sbin/rear ./rear ($PROGRAM is 'rear').
# We need to explicitly check /usr/sbin/rear because $SCRIPT_FILE could be /path/to/checkout/usr/sbin/rear
# and then another simultaneously running instance could be /usr/sbin/rear (e.g. from an RPM package):
for pid in $( pidof -x "$SCRIPT_FILE" "/usr/sbin/$PROGRAM" "usr/sbin/$PROGRAM" "./usr/sbin/$PROGRAM" "./sbin/$PROGRAM" "./$PROGRAM" ) ; do
if test "$pid" != $$ ; then
echo "ERROR: $PROGRAM is already running, not starting again" >&2
echo "ERROR: $PROGRAM is already running with PID $pid, not starting again" >&2
exit 1
fi
done
Expand Down

0 comments on commit e04125b

Please sign in to comment.