[lfs-support] /lib/lsb/init-functions - statusproc, etc

Qrux qrux.qed at gmail.com
Sat Feb 18 13:58:16 PST 2012


All kidding aside, I couldn't stop laughing.  Not sure if I should have been crying, though.  :)  Now, my script-fu is not Jack-Black-panda strong, but AFAICT it's horribly broken.  I'd be happy to be educated otherwise, though.

Here's the current statusproc() function:

################################################################################
# statusproc()                                                                 #
# Usage: statusproc [-p pidfile] pathname                                      #
#                                                                              #
# Purpose: This function prints the status of a particular daemon to stdout    #
#                                                                              #
# Inputs: -p pidfile, use the specified pidfile instead of pidof               #
#         pathname, path to the specified program                              #
#                                                                              #
# Note: Output to stdout.  Not logged.                                         #
#                                                                              #
# Return values:                                                               #
#       0 - Status printed                                                     #
#       1 - Input error. The daemon to check was not specified.                #
################################################################################
statusproc()
{
   if [ "${#}" = "0" ]; then
      echo "Usage: statusproc {program}"
      exit 1
   fi

    local pidfile
    local pidlist

    # Process arguments
    while true; do
      case "${1}" in

          -p)
              pidfile="${2}"
              shift 2
              ;;
      esac
   done

   if [ -z "${pidfile}" ]; then
      pidlist=`pidofproc -p "${pidfile}" $@`
   else
      pidlist=`pidofproc $@`
   fi

   # Trim trailing blanks
   pidlist=`echo "${pidlist}" | sed -r 's/ +$//'`

   base="${1##*/}"

   if [ -n "${pidlist}" ]; then
      echo -e "${INFO}${base} is running with Process" \
         "ID(s) ${pidlist}.${NORMAL}"
   else
      if [ -n "${base}" -a -e "/var/run/${base}.pid" ]; then
         echo -e "${WARNING}${1} is not running but" \
            "/var/run/${base}.pid exists.${NORMAL}"
      else
         if [ -z "${pidlist}" -a -n "${pidfile}" ]; then
            echo -e "${WARNING}${1} is not running" \
               "but ${pidfile} exists.${NORMAL}"
         else
            echo -e "${INFO}${1} is not running.${NORMAL}"
         fi
      fi
   fi
}
################################################################################

This is sort of an epic travesty. :D  First, it starts with a nice infinite-loop 'while true' to process the arguments, with no breaks that I can see.  For me, this just hangs.

Then, there's an inverted check...If the ${pidfile} is optional, why is it being used in the [ -z ] check?  Finally, aside from the infinite-loop, there's no way to capture the <path-to-proc> argument, whether or not -p is used.  You can tell someone wanted to shift and use $1, but how does that work in the event -p comes after?  Sure, it's not *allowed* usage, but...bad usage probably shouldn't hang the script.

I assume...Because vanilla-LFS doesn't include any daemons, this code was never exercised.  But, BLFS comes with plenty, many of which call this function their "status" invocations.  Which, at least for me, just hangs.

Here's a patch that works.  And, I've included--for a surprisingly low price of $19.99 if you call in the next 15 minutes!--a handy function called is_dir_empty(), which...when called, returns 0 (zero) for an empty directory.  It's a nice thing to have for scripts that have conditional elements based on whether or not files are present in a directory; I didn't see an analog in the current script.

	Q




--- lfs-bootscripts-20111017/lfs/lib/services/init-functions-ORIG	2011-10-21 20:30:16.000000000 -0700
+++ lfs-bootscripts-20111017/lfs/lib/services/init-functions	2012-02-18 13:46:12.093976105 -0800
@@ -487,26 +487,31 @@
     local pidlist
 
     # Process arguments
-    while true; do
+    local args
+    while [ $# -ne 0 ] ; do
       case "${1}" in
-
           -p)
+              args="${args} ${1}"
               pidfile="${2}"
               shift 2
               ;;
+           *)
+              args="${args} ${1}"
+              shift 1
+              ;;
       esac
    done
 
    if [ -z "${pidfile}" ]; then
-      pidlist=`pidofproc -p "${pidfile}" $@`
+      pidlist=`pidofproc $args`
    else
-      pidlist=`pidofproc $@`
+      pidlist=`pidofproc -p "${pidfile}" $@`
    fi
 
    # Trim trailing blanks
    pidlist=`echo "${pidlist}" | sed -r 's/ +$//'`
 
-   base="${1##*/}"
+   base="${args##*/}"
 
    if [ -n "${pidlist}" ]; then
       echo -e "${INFO}${base} is running with Process" \
@@ -742,4 +747,26 @@
    return 0
 }
 
+################################################################################
+# is_dir_empty()                                                               #
+#                                                                              #
+# Purpose: Checks if a directory has no entries (files, subdirs, etc)          #
+#                                                                              #
+# Inputs: Accepts a single string which represents a path                      #
+#                                                                              #
+# Return values:                                                               #
+#       0 - No entries                                                         #
+#       1 - Enries present                                                     #
+#     254 - Invalid directory path given                                       #
+#     255 - No argument given                                                  #
+#                                                                              #
+################################################################################
+is_dir_empty()
+{
+   local dir=$1
+   if [ -z $dir ] ; then echo "$dir unspecified ; aborting" ; return 255 ; fi
+   if [ ! -d $dir ] ; then echo "$dir is not a directory; aborting" ; return 254 ; fi
+   if [ 0 -eq $(/bin/ls -1 $dir/* 2> /dev/null | wc -l) ] ; then return 0 ; else return 1 ; fi
+}
+
 # End /lib/lsb/init-functions




More information about the lfs-support mailing list