Skip to content

Commit

Permalink
Only code shell functions once.
Browse files Browse the repository at this point in the history
All bareos-ctl-* scripts code the same set of functions which is kind
of stupid. This patch creates a common set of functions which are
included into the script so we only need to code them once and reuse
them. Also added support for pgrep support which is on Platforms like
Solaris and xBSD next to pidof support.
  • Loading branch information
Marco van Wieringen committed Feb 17, 2015
1 parent 184919b commit 50877a3
Show file tree
Hide file tree
Showing 6 changed files with 198 additions and 465 deletions.
1 change: 1 addition & 0 deletions autoconf/configure.in
Expand Up @@ -117,6 +117,7 @@ AC_PATH_PROG(PKGCONFIG, pkg-config, [])
AC_PATH_PROG(QMAKE, qmake, [])
AC_PATH_PROG(GMAKE, gmake, [])
AC_PATH_PROG(PIDOF, pidof, [])
AC_PATH_PROG(PGREP, pgrep, [])
AC_PATH_PROG(GCORE, gcore, [])
AC_PATH_PROG(GDB, gdb, [])
AC_PATH_PROG(DBX, dbx, [])
Expand Down
1 change: 1 addition & 0 deletions scripts/Makefile.in
Expand Up @@ -37,6 +37,7 @@ install: installdirs
$(INSTALL_SCRIPT) bareos $(DESTDIR)$(scriptdir)/bareos
$(INSTALL_SCRIPT) bareos_config $(DESTDIR)$(scriptdir)/bareos_config
$(INSTALL_SCRIPT) bareos $(DESTDIR)$(sbindir)/bareos
$(INSTALL_SCRIPT) bareos-ctl-funcs $(DESTDIR)$(scriptdir)/bareos-ctl-funcs
$(INSTALL_SCRIPT) bareos-ctl-dir $(DESTDIR)$(scriptdir)/bareos-ctl-dir
$(INSTALL_SCRIPT) bareos-ctl-fd $(DESTDIR)$(scriptdir)/bareos-ctl-fd
$(INSTALL_SCRIPT) bareos-ctl-sd $(DESTDIR)$(scriptdir)/bareos-ctl-sd
Expand Down
162 changes: 7 additions & 155 deletions scripts/bareos-ctl-dir.in
Expand Up @@ -32,164 +32,16 @@ DIR_PORT=@dir_port@
DIR_USER=@dir_user@
DIR_GROUP=@dir_group@
Bareos="@BAREOS@"
PIDOF=@PIDOF@

# A function to stop a program.
killproc() {
RC=0
# Test syntax.
if [ $# = 0 ]; then
echo "Usage: killproc {program} {port} [signal]"
return 1
fi

notset=0
# check for third arg to be kill level
if [ "$3" != "" ] ; then
killlevel=$3
else
notset=1
killlevel="-9"
fi

# Get base program name
base=`basename $1`

# Find pid.
pid=`pidofproc $base $2`

# Kill it.
if [ "$pid" != "" ] ; then
if [ "$notset" = "1" ] ; then
if ${PS} -p "$pid">/dev/null 2>&1; then
# TERM first, then KILL if not dead
kill -TERM $pid 2>/dev/null
sleep 1
if ${PS} -p "$pid" >/dev/null 2>&1 ; then
sleep 1
if ${PS} -p "$pid" >/dev/null 2>&1 ; then
sleep 3
if ${PS} -p "$pid" >/dev/null 2>&1 ; then
kill -KILL $pid 2>/dev/null
fi
fi
fi
fi
${PS} -p "$pid" >/dev/null 2>&1
RC=$?
[ $RC -eq 0 ] && failure "$base shutdown" || success "$base shutdown"
# RC=$((! $RC))
# use specified level only
else
if ${PS} -p "$pid" >/dev/null 2>&1; then
kill $killlevel $pid 2>/dev/null
RC=$?
[ $RC -eq 0 ] && success "$base $killlevel" || failure "$base $killlevel"
fi
fi
else
failure "$base shutdown"
fi
# Remove pid file if any.
if [ "$notset" = "1" ]; then
rm -f ${PIDDIR}/$base.$2.pid
fi
return $RC
}

# A function to find the pid of a program.
pidofproc() {
pid=""
# Test syntax.
if [ $# = 0 ] ; then
echo "Usage: pidofproc {program}"
return 1
fi

# Get base program name
base=`basename $1`

# First try PID file
if [ -f ${PIDDIR}/$base.$2.pid ] ; then
pid=`head -n 1 ${PIDDIR}/$base.$2.pid`
if [ "$pid" != "" ] ; then
echo $pid
return 0
fi
fi

# Next try "pidof"
if [ -x ${PIDOF} ] ; then
pid=`${PIDOF} $1`
fi
if [ "$pid" != "" ] ; then
echo $pid
return 0
fi

# Finally try to extract it from ps
pid=`${PSCMD} | grep $1 | ${AWK} '{ print $1 }' | tr '\n' ' '`
echo $pid
return 0
}

status() {
pid=""
# Test syntax.
if [ $# = 0 ] ; then
echo "Usage: status {program} {port}"
return 1
fi

# Get base program name
base=`basename $1`

# First try "pidof"
if [ -x ${PIDOF} ] ; then
pid=`${PIDOF} $1`
fi
if [ "$pid" != "" ] ; then
echo "$base (pid $pid) is running..."
return 0
else
pid=`${PSCMD} | ${AWK} 'BEGIN { prog=ARGV[1]; ARGC=1 }
{ if ((prog == $2) || (("(" prog ")") == $2) ||
(("[" prog "]") == $2) ||
((prog ":") == $2)) { print $1 ; exit 0 } }' $1`
if [ "$pid" != "" ] ; then
echo "$base (pid $pid) is running..."
return 0
fi
fi

# Next try the PID files
if [ -f ${PIDDIR}/$base.$2.pid ] ; then
pid=`head -n 1 ${PIDDIR}/$base.$2.pid`
if [ "$pid" != "" ] ; then
echo "$base dead but pid file exists"
return 1
fi
fi
# See if the subsys lock exists
if [ -f ${SUBSYSDIR}/$base ] ; then
echo "$base dead but subsys locked"
return 2
fi
echo "$base is stopped"
return 3
}

success() {
return 0
}

failure() {
rc=$?
return $rc
}
PIDOF="@PIDOF@"
PGREP="@PGREP@"

OS=`uname -s`

#
# Source the generic functions.
#
. @scriptdir@/bareos-ctl-funcs.in

# if /lib/tls exists, force Bareos to use the glibc pthreads instead
if [ -d "/lib/tls" -a $OS = "Linux" -a `uname -r | cut -c1-3` = "2.4" ] ; then
export LD_ASSUME_KERNEL=2.4.19
Expand Down
162 changes: 7 additions & 155 deletions scripts/bareos-ctl-fd.in
Expand Up @@ -32,164 +32,16 @@ FD_PORT=@fd_port@
FD_USER=@fd_user@
FD_GROUP=@fd_group@
Bareos="@BAREOS@"
PIDOF=@PIDOF@

# A function to stop a program.
killproc() {
RC=0
# Test syntax.
if [ $# = 0 ]; then
echo "Usage: killproc {program} {port} [signal]"
return 1
fi

notset=0
# check for third arg to be kill level
if [ "$3" != "" ] ; then
killlevel=$3
else
notset=1
killlevel="-9"
fi

# Get base program name
base=`basename $1`

# Find pid.
pid=`pidofproc $base $2`

# Kill it.
if [ "$pid" != "" ] ; then
if [ "$notset" = "1" ] ; then
if ${PS} -p "$pid">/dev/null 2>&1; then
# TERM first, then KILL if not dead
kill -TERM $pid 2>/dev/null
sleep 1
if ${PS} -p "$pid" >/dev/null 2>&1 ; then
sleep 1
if ${PS} -p "$pid" >/dev/null 2>&1 ; then
sleep 3
if ${PS} -p "$pid" >/dev/null 2>&1 ; then
kill -KILL $pid 2>/dev/null
fi
fi
fi
fi
${PS} -p "$pid" >/dev/null 2>&1
RC=$?
[ $RC -eq 0 ] && failure "$base shutdown" || success "$base shutdown"
# RC=$((! $RC))
# use specified level only
else
if ${PS} -p "$pid" >/dev/null 2>&1; then
kill $killlevel $pid 2>/dev/null
RC=$?
[ $RC -eq 0 ] && success "$base $killlevel" || failure "$base $killlevel"
fi
fi
else
failure "$base shutdown"
fi
# Remove pid file if any.
if [ "$notset" = "1" ]; then
rm -f ${PIDDIR}/$base.$2.pid
fi
return $RC
}

# A function to find the pid of a program.
pidofproc() {
pid=""
# Test syntax.
if [ $# = 0 ] ; then
echo "Usage: pidofproc {program}"
return 1
fi

# Get base program name
base=`basename $1`

# First try PID file
if [ -f ${PIDDIR}/$base.$2.pid ] ; then
pid=`head -n 1 ${PIDDIR}/$base.$2.pid`
if [ "$pid" != "" ] ; then
echo $pid
return 0
fi
fi

# Next try "pidof"
if [ -x ${PIDOF} ] ; then
pid=`${PIDOF} $1`
fi
if [ "$pid" != "" ] ; then
echo $pid
return 0
fi

# Finally try to extract it from ps
pid=`${PSCMD} | grep $1 | ${AWK} '{ print $1 }' | tr '\n' ' '`
echo $pid
return 0
}

status() {
pid=""
# Test syntax.
if [ $# = 0 ] ; then
echo "Usage: status {program} {port}"
return 1
fi

# Get base program name
base=`basename $1`

# First try "pidof"
if [ -x ${PIDOF} ] ; then
pid=`${PIDOF} $1`
fi
if [ "$pid" != "" ] ; then
echo "$base (pid $pid) is running..."
return 0
else
pid=`${PSCMD} | ${AWK} 'BEGIN { prog=ARGV[1]; ARGC=1 }
{ if ((prog == $2) || (("(" prog ")") == $2) ||
(("[" prog "]") == $2) ||
((prog ":") == $2)) { print $1 ; exit 0 } }' $1`
if [ "$pid" != "" ] ; then
echo "$base (pid $pid) is running..."
return 0
fi
fi

# Next try the PID files
if [ -f ${PIDDIR}/$base.$2.pid ] ; then
pid=`head -n 1 ${PIDDIR}/$base.$2.pid`
if [ "$pid" != "" ] ; then
echo "$base dead but pid file exists"
return 1
fi
fi
# See if the subsys lock exists
if [ -f ${SUBSYSDIR}/$base ] ; then
echo "$base dead but subsys locked"
return 2
fi
echo "$base is stopped"
return 3
}

success() {
return 0
}

failure() {
rc=$?
return $rc
}
PIDOF="@PIDOF@"
PGREP="@PGREP@"

OS=`uname -s`

#
# Source the generic functions.
#
. @scriptdir@/bareos-ctl-funcs.in

# if /lib/tls exists, force Bareos to use the glibc pthreads instead
if [ -d "/lib/tls" -a $OS = "Linux" -a `uname -r | cut -c1-3` = "2.4" ] ; then
export LD_ASSUME_KERNEL=2.4.19
Expand Down

0 comments on commit 50877a3

Please sign in to comment.