mirror_corosync-qdevice/init/corosync-qdevice.in
Jan Friesse 1965225e3e init: Fix init scripts to work with containers
Previously init scripts were not using pid file so pidof was used. This
is usually not a problem, but when containers are used it may result to
killing improper instance when issued on host.

Solution is to always use pidfile.

Also try to use LSB complaint status codes.

Signed-off-by: Jan Friesse <jfriesse@redhat.com>
Reviewed-by: Christine Caulfield <ccaulfie@redhat.com>
2018-11-15 17:34:35 +01:00

204 lines
3.9 KiB
Plaintext
Executable File

#!@BASHPATH@
# Authors:
# Jan Friesse <jfriesse@redhat.com>
#
# License: Revised BSD
# chkconfig: - 20 80
# description: Corosync Qdevice daemon
# processname: corosync-qdevice
#
### BEGIN INIT INFO
# Provides: corosync-qdevice
# Required-Start: corosync
# Required-Stop: corosync
# Default-Start:
# Default-Stop:
# Short-Description: Starts and stops Corosync Qdevice daemon.
# Description: Starts and stops Corosync Qdevice daemon.
### END INIT INFO
desc="Corosync Qdevice daemon"
prog="corosync-qdevice"
prog_pid_file="@LOCALSTATEDIR@/run/corosync-qdevice/$prog.pid"
# set secure PATH
PATH="/sbin:/bin:/usr/sbin:/usr/bin:@SBINDIR@"
success()
{
echo -ne "[ OK ]\r"
}
failure()
{
echo -ne "[FAILED]\r"
}
# pid_var_run pid_file
# Echo pid from given pid_file.
# Returns LSB exit code for the 'status' action.
pid_var_run()
{
local pid_file="$1"
local pid
if [ -f "$pid_file" ]; then
[ ! -r "$pid_file" ] && return 4
pid=$(cat "$pid_file")
[ -z "$pid" ] && return 1
[ -n "${pid//[0-9]/}" ] && return 1
if kill -n 0 "$pid" 2>/dev/null;then
echo "$pid"
return 0
else
return 1
fi
fi
return 3
}
# status [-p pid_file] {program}
status()
{
local pid_file
if [ "$1" = "-p" ]; then
pid_file=$2
shift 2
fi
pid=$(pid_var_run "$pid_file" 2>/dev/null)
res=$?
if [ $res -ne 0 -a -z "$pid_file" ]; then
pid=$(__pids_pidof "$1")
[ -n "$pid" ]
res=$?
fi
if [ $res -ne 0 ]; then
echo "$1 is stopped"
else
echo "$1 (pid $pid) is running..."
fi
return $res
}
[ -f @INITCONFIGDIR@/$prog ] && . @INITCONFIGDIR@/$prog
case '@INITCONFIGDIR@' in
*/sysconfig) # rpm based distros
[ -f @INITDDIR@/functions ] && . @INITDDIR@/functions
[ -z "$LOCK_FILE" ] && LOCK_FILE="@LOCALSTATEDIR@/lock/subsys/$prog";;
*/default) # deb based distros
[ -z "$LOCK_FILE" ] && LOCK_FILE="@LOCALSTATEDIR@/lock/$prog";;
esac
# The version of __pids_pidof in /etc/init.d/functions calls pidof with -x
# This means it matches scripts, including this one.
# Redefine it here so that status (from the same file) works.
# Otherwise simultaneous calls to stop() will loop forever
__pids_pidof() {
pidof -c -o $$ -o $PPID -o %PPID "$1" || \
pidof -c -o $$ -o $PPID -o %PPID "${1##*/}"
}
cluster_disabled_at_boot()
{
if grep -q nocluster /proc/cmdline && \
[ "$(tty)" = "/dev/console" ]; then
echo -e "not configured to run at boot"
failure
return 1
fi
return 0
}
start()
{
echo -n "Starting $desc ($prog): "
! cluster_disabled_at_boot && return
# most recent distributions use tmpfs for @LOCALSTATEDIR@/run
# to avoid to clean it up on every boot.
# they also assume that init scripts will create
# required subdirectories for proper operations
if [ ! -d "@LOCALSTATEDIR@/run/corosync-qdevice" ];then
mkdir -p "@LOCALSTATEDIR@/run/corosync-qdevice"
chmod 0770 "@LOCALSTATEDIR@/run/corosync-qdevice"
fi
if status -p "$prog_pid_file" "$prog" > /dev/null 2>&1; then
success
else
$prog $COROSYNC_QDEVICE_OPTIONS > /dev/null 2>&1
if [ "$?" != 0 ]; then
failure
rtrn=1
else
touch $LOCK_FILE
success
fi
fi
echo
}
stop()
{
! status -p "$prog_pid_file" "$prog" > /dev/null 2>&1 && return
echo -n "Signaling $desc ($prog) to terminate: "
kill -TERM "$(pid_var_run $prog_pid_file)" > /dev/null 2>&1
success
echo
echo -n "Waiting for $prog services to unload:"
while status -p "$prog_pid_file" "$prog" > /dev/null 2>&1; do
sleep 1
echo -n "."
done
rm -f $LOCK_FILE
success
echo
}
restart()
{
stop
start
}
rtrn=0
case "$1" in
start)
start
;;
restart|reload|force-reload)
restart
;;
condrestart|try-restart)
if status -p "$prog_pid_file" "$prog" > /dev/null 2>&1; then
restart
fi
;;
status)
status -p "$prog_pid_file" "$prog"
rtrn=$?
;;
stop)
stop
;;
*)
echo "usage: $0 {start|stop|restart|reload|force-reload|condrestart|try-restart|status}"
rtrn=2
;;
esac
exit $rtrn