#!/bin/sh

# NOTICE_START
# Licensed Materials - Property of IBM
# "Restricted Materials of IBM" -- 5746-SM2
# (C) Copyright IBM Corp. 2006, 2009 All Rights Reserved.
# US Government Users Restricted Rights - Use, duplication or disclosure
# restricted by GSA ADP Schedule Contract with IBM Corp.
# NOTICE_END


### BEGIN INIT INFO
# Provides: powervm-lx86
# Default-Start: 2 3 5
# Default-Stop:  0 1 4 6
# Required-Start: $local_fs $time $network
# Required-Stop: $local_fs
# Short-Description: powervm-lx86
# Description: powervm-lx86
### END INIT INFO

# *** CHKCONFIG CONFIGURATION START ***
#chkconfig: 235 95 05
#description: powervm-lx86
#processname: powervm-lx86
# *** CHKCONFIG CONFIGURATION END ***

# source functions library
if [ -f /lib/lsb/init-functions ]; then
  . /lib/lsb/init-functions
elif [ -f /etc/init.d/functions ]; then
  # RedHat compatibility
  . /etc/init.d/functions
  log_success_msg () {
    echo -n $*
    success "$*"
    echo
  }
  log_failure_msg () {
    echo -n $*
    failure "$*"
    echo
  }
  alias log_failure_msg=failure
else 
  echo "[$0][Error: 0008] Unable to source init-script functions." > /dev/stderr
  exit 1
fi


# Return values
# 0 - success
# 1 - misc error
# 2 - invalid or excess args
# 3 - unimplemented feature (e.g. reload)
# 4 - insufficient privilege
# 5 - program not installed
# 6 - program not configured

function load_or_default 
{
  local KEY="$1"
  local DEFAULT="$2"

  local VALUE="$DEFAULT"
  if [ -s /etc/opt/powervm-lx86/config ]
  then
    LINE="$(grep "^$KEY\=" /etc/opt/powervm-lx86/config)"
    if [ $? -eq 0 ]
    then
      CUT=$(echo "$LINE" | cut -d '=' -f 2 | tail -n 1)
      if [ $? -eq 0 ]
      then
        VALUE="$CUT"
      fi
    fi
  fi
  echo "$VALUE"
}


if [ -z "$POWERVM_LX86_EXEDIR" ] ;
then
  POWERVM_LX86DIR=$(load_or_default POWERVM_LX86_LOCATION /opt/powervm-lx86)"/bin"
else
  POWERVM_LX86DIR="$POWERVM_LX86_EXEDIR"
fi


if [ -z "$POWERVM_LX86_X86WORLD" ] ;
then
    POWERVM_LX86_PATH=$(load_or_default SUBJECT_WORLD_ROOT "/i386")
else
    POWERVM_LX86_PATH=$POWERVM_LX86_X86WORLD
fi

SELINUX_TRANS_ENABLED=$(tr '[:upper:]' '[:lower:]' <<< $(load_or_default ENABLE_SELINUX_TRANSITIONS n))
grep -qs "release 5" /etc/redhat-release
IS_RHEL5=$?

POWERVM_LX86_DAEMON_DIR=/var/opt/powervm-lx86/daemon
LOCKFILE=/var/lock/subsys/powervm-lx86

# If we're using SELinux, runx86 can do the context transitioning for us
RUNX86="/usr/local/bin/runx86"

# If the rest of the package has been removed, but the init script remains, exit with error 5
# (as per LSB specification 1.3)
test -f "$POWERVM_LX86DIR/powervm-lx86" ||
{
  if [ "$1" = "stop" ]; then
    exit 0;
  else
    echo "$POWERVM_LX86DIR/powervm-lx86 is not installed";
    exit 5;
  fi
}

test -f "$POWERVM_LX86DIR/powervm-lx86-daemon" ||
{
  if [ "$1" = "stop" ]; then
    exit 0;
  else
    echo "$POWERVM_LX86DIR/powervm-lx86-daemon is not installed";
    exit 5;
  fi
}

check_privileges()
{
  if [ ! $EUID -eq 0 ]
  then
    echo "[$0][Error: 0001] You must be root to run this script."
    exit 4
  fi
  if [ -e "/etc/opt/powervm-lx86/config" ]
  then
    if [ "`stat -c%U /etc/opt/powervm-lx86/config 2>/dev/null`" != "root" ]
    then
      echo "[$0][Error: 0002] /etc/opt/powervm-lx86/config must be owned by root."
      exit 1
    fi
    # permissions in -rwxrwxrwx format
    # is the w permission contained in the last two sets of permissions?
    stat -c%A /etc/opt/powervm-lx86/config 2>/dev/null | grep ^.....*w &>/dev/null
    if [ $? -eq "0" ]
    then
      echo "[$0][Error: 0003] /etc/opt/powervm-lx86/config must be writable only by root."
      exit 1
    fi
  fi  

  # The following check includes POWERVM_LX86_DAEMON_DIR
  for file in `find "${POWERVM_LX86_DAEMON_DIR}" -maxdepth 1`
  do
    if [ `stat -c%U "$file"` != "daemon" ]
    then
      echo "[$0][Error: 0009] ${file} must be owned by daemon"
      exit 1
    else
      # permissions in -rwxrwxrwx format
      # can the file owner (daemon) write to it? 
      stat -c%A "$file" 2>/dev/null | grep ^..w &>/dev/null
      if [ $? -ne "0" ]
      then
        echo "[$0][Error: 0010] $file must be writable by daemon."
        exit 1
      fi
    fi

    # Need to force the daemon's files to the right context.
    if [ -e "/usr/bin/setfattr" -a /usr/sbin/selinuxenabled ]
    then
      DAEMONCONTEXT="system_u:object_r:lx86_var_t"
      if [ 0 -eq $IS_RHEL5 ]
      then
        DAEMONCONTEXT="${DAEMONCONTEXT}:s0"
      fi

      /usr/bin/setfattr -n "security.selinux" -v "$DAEMONCONTEXT" "$file" > /dev/null 2> /dev/null
    fi
 
  done  

  if [ -e "/usr/bin/setfattr" -a /usr/sbin/selinuxenabled -a $SELINUX_TRANS_ENABLED = "y" ]
  then
    LOGDIR=$(load_or_default LOGFILE_PATH "/var/opt/powervm-lx86/log")
    LOGCONTEXT="system_u:object_r:lx86_var_t"
    if [ 0 -eq $IS_RHEL5 ]
    then
      LOGCONTEXT="${LOGCONTEXT}:s0"
    fi
    /usr/bin/setfattr -n "security.selinux" -v "$LOGCONTEXT" "$LOGDIR"
  fi
}

setup_binfmt_misc()
{
    # Firstly, ensure that we have binfmt_misc available in the kernel:
    if [ ! -e /proc/sys/fs/binfmt_misc/status ]
    then
       # It's not mounted - see if the fs exists.
        if ! grep -q binfmt_misc /proc/filesystems
        then
            echo -n "Attempting to modprobe binfmt_misc... "
            /sbin/modprobe binfmt_misc
            if [ $? -ne 0 ]
            then
                echo "[$0][Error: 0004] Kernel doesn't contain binfmt_misc and loading it as a module failed."
                echo "Please check your kernel configuration and ensure binfmt_misc is available."
                return 1;
            else
                echo " OK"
            fi
        fi
    
        echo "Mounting /proc/sys/fs/binfmt_misc... "
        /bin/mount -t binfmt_misc none /proc/sys/fs/binfmt_misc 2>/dev/null

        # The above might tell us it failed when really it succeeded
        # For example, sometimes loading the module causes it to automatically
        # mount the directory
        if [ ! -e /proc/sys/fs/binfmt_misc/status ]
        then
            # This really shouldn't happen if the above succeeds.  Perhaps it could if /proc isn't mounted, though!
            echo "[$0][Error: 0005] Could not mount /proc/sys/fs/binfmt_misc (from binfmt_misc)."
            return 1;
        else
            echo " OK"
        fi
    fi

    if [ -e /proc/sys/fs/binfmt_misc/i386 -o -e /proc/sys/fs/binfmt_misc/i386so ] 
    then
        reset_binfmt_misc
    fi

    # Actually register the binary now.  Note the flags, PC.  (Credentials and preserve argv0)
    echo 1 > /proc/sys/fs/binfmt_misc/status
    echo ":i386:M::\x7fELF\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x03:\xff\xff\xff\xff\xff\xfe\xfe\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfb\xff\xff:$POWERVM_LX86DIR/i386-router:POC" > /proc/sys/fs/binfmt_misc/register
    echo ":i386so:M::\x7fELF\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x03\x00\x03:\xff\xff\xff\xff\xff\xfe\xfe\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfb\xff\xff:$POWERVM_LX86DIR/i386-router:POC" > /proc/sys/fs/binfmt_misc/register

    # Check whether it actually registered
    if [ ! -f /proc/sys/fs/binfmt_misc/i386 ];
    then
      echo "[$0][Error: 0006] Failed to register i386 handler with binfmt_misc."
      return 1;
    fi

    # Check whether it actually registered
    if [ ! -f /proc/sys/fs/binfmt_misc/i386so ];
    then
      echo "[$0][Error: 0007] Failed to register i386so handler with binfmt_misc."
      return 1;
    fi

    return 0; # Success
}

reset_binfmt_misc()
{
    if [ -e /proc/sys/fs/binfmt_misc/i386 ] 
    then
        echo -1 > /proc/sys/fs/binfmt_misc/i386
    fi
    if [ -e /proc/sys/fs/binfmt_misc/i386so ] 
    then
        echo -1 > /proc/sys/fs/binfmt_misc/i386so
    fi
}

daemon_start()
{
    # If this build has a powervm-lx86-daemon, run it:
    if [ -x "$POWERVM_LX86DIR/powervm-lx86-daemon" ] ;
    then
        echo -n "Starting powervm-lx86-daemon"
        # Starting the powervm-lx86-daemon involves fetching a licence - we wait this long (secs) for the
        # powervm-lx86-daemon lockfile to appear (signalling it has started up):
        POLLTIMEOUT=30
        
        # First get rid of any already-running powervm-lx86-daemon:
        pkill -f -x "$POWERVM_LX86DIR/powervm-lx86-daemon"
        rm -f "$POWERVM_LX86_DAEMON_DIR/powervm-lx86-daemon.lock"

        "$POWERVM_LX86DIR/powervm-lx86-daemon"
        if [ $? -ne 0 ] ; then
            log_failure_msg ""
            return 1;
        else
            # OK, wait for the lockfile to appear:
            while [ ! -f "$POWERVM_LX86_DAEMON_DIR/powervm-lx86-daemon.lock" ] 
            do
              sleep 1
              POLLTIMEOUT=$((POLLTIMEOUT - 1))
              echo -n "."
              if [ $POLLTIMEOUT -lt 1 ]
              then
                  log_failure_msg "Timeout waiting for powervm-lx86-daemon"
                  return 1;
              fi
            done
            # Sleep once more, the socket is being created:
            sleep 2

            log_success_msg ""
            return 0;
        fi
    else
        return 0; # Success!
    fi
}

daemon_stop()
{
    # If this build has a powervm-lx86-daemon, it was run so stop it:
    if [ -x "$POWERVM_LX86DIR/powervm-lx86-daemon" ] ;
    then
        pkill -f -x "$POWERVM_LX86DIR/powervm-lx86-daemon"

        echo -n "Stopping powervm-lx86-daemon"
        STOPTIMEOUT=10
        while pgrep -f -x "$POWERVM_LX86DIR/powervm-lx86-daemon" &>/dev/null
        do
          sleep 1
          STOPTIMEOUT=$((STOPTIMEOUT - 1))
          echo -n "."
          if [ $STOPTIMEOUT -lt 1 ]
          then
              log_failure_msg
              echo -n "powervm-lx86-daemon is not responding, force terminating"
              pkill -9 -f -x "$POWERVM_LX86DIR/powervm-lx86-daemon" &>/dev/null

              # Give it a chance to stop
              sleep 2
          fi
        done

        log_success_msg
    fi
    return;
}

run_rcmonitor()
{
  # only for SUSE
  if [ -e "/etc/SuSE-release" ] ;
  then
    pgrep -f -x "$POWERVM_LX86DIR/powervm-lx86-rcmonitor" &>/dev/null
    if [ $? -ne 0 ] ; then
      "$POWERVM_LX86DIR/powervm-lx86-rcmonitor" &
    fi
    log_success_msg "Starting powervm-lx86-rcmonitor"
  fi
}

stop_rcmonitor()
{
  # only for SUSE
  if [ -e "/etc/SuSE-release" ] ;
  then
    pkill -f -x "$POWERVM_LX86DIR/powervm-lx86-rcmonitor"
    if [ "$?" -eq 0 -o "$?" -eq 1 ]
    then
      log_success_msg "Stopping powervm-lx86-rcmonitor"
    else
      log_failure_msg "Stopping powervm-lx86-rcmonitor"
    fi
  fi
}

stop_x86_services()
{
  # first stopping the services using rc
  pgrep -f -x "$POWERVM_LX86DIR/powervm-lx86-daemon" &>/dev/null
  if [ "$?" -eq 0 -a -e "$POWERVM_LX86_PATH/etc/.sw_installed_successfully" -a -x "$RUNX86" ] ; then
    cd "$POWERVM_LX86_PATH"
    # we will have two blogd instances using the same /dev/blog
    # creating boot.msg in the x86 world will prevent confusion
    if [ ! -e "$POWERVM_LX86_PATH/var/log/boot.msg" ] ; then
      touch "$POWERVM_LX86_PATH/var/log/boot.msg" &>/dev/null
    fi
    echo -n "Stopping x86 services "
    if [ -e "/etc/SuSE-release" ] ;
    then
      # SuSE rc wants the runlevels in environment variables
      set `/sbin/runlevel`
      export PREVLEVEL=$2
      export RUNLEVEL=0
      if [ -z "$PREVLEVEL" ] ;
      then
        log_failure_msg ""
        return 
      else
        # no failure so we need a new line for rc messages to print correctly
        echo ""
      fi
      $RUNX86 /etc/init.d/rc
    else
      # need a new line when rc starts printing its messages
      echo ""
      # RedHat rc wants the new runlevel as argument
      $RUNX86 /etc/rc.d/rc 0
    fi
  fi
  
  # looking for powervm-lx86 processes
  # We have to use -f here, sometimes due to script execs the process name isn't what we expect.
  # Avoid using -x, the code following the pgrep does more accurate checking.
  for THEPID in `pgrep -f powervm-lx86 2>/dev/null`
  do
    # making sure the pid corresponds to a powervm-lx86 process
    # Notice the extra space  
    THEFILENAME="`readlink /proc/$THEPID/exe` "
    # using grep instead of direct comparison to cover cases
    # when the binary has been deleted (deleted appended to /proc/$THEPID/exe)
    # and possibly other cases as well
    # Notice the extra space  
    echo "$THEFILENAME" | grep "$POWERVM_LX86DIR/powervm-lx86 " &>/dev/null
    if [ $? -eq "0" ]
    then
      POWERVM_LX86_PROCS="$POWERVM_LX86_PROCS $THEPID"
    fi
  done
  
  if [ ! -z "$POWERVM_LX86_PROCS" ] ;
  then
    # pause to let them complete normally
    sleep 4
    
    # sending TERM signal to any processes left
    echo "Sending TERM signal to rest of powervm-lx86 processes"
    kill -TERM $POWERVM_LX86_PROCS 2>/dev/null
    
    # sleep for a few secs to give processes time to finish
    sleep 4
    
    # sending KILL signal to processes that could not finish 
    echo "Sending KILL signal to rest of powervm-lx86 processes"
    kill -KILL $POWERVM_LX86_PROCS 2>/dev/null
  fi
}

x86_pre_boot()
{
  if [ ! -f "$POWERVM_LX86_PATH"/etc/sysconfig/cron ] ; then
    > "$POWERVM_LX86_PATH"/etc/sysconfig/cron
  fi

  # Generic cleanup stuff to perform at boot.
  if [ -f "$POWERVM_LX86_PATH/var/run/utmpx" -o -f "$POWERVM_LX86_PATH/var/log/wtmpx" ]; then
    > "$POWERVM_LX86_PATH/var/run/utmpx"
    touch "$POWERVM_LX86_PATH/var/log/wtmpx"
    chgrp utmp "$POWERVM_LX86_PATH/var/run/utmpx" "$POWERVM_LX86_PATH/var/log/wtmpx"
    chmod 0664 "$POWERVM_LX86_PATH/var/run/utmpx" "$POWERVM_LX86_PATH/var/log/wtmpx"
  fi
  
  find "$POWERVM_LX86_PATH"/var/run "$POWERVM_LX86_PATH"/var/lock/ -type f ! -name wtmpx ! -name utmpx ! -name utmp -exec rm -f {} \; &>/dev/null
  rm -f "$POWERVM_LX86_PATH"/var/lib/rpm/__db* &> /dev/null

  rm -f "$POWERVM_LX86_PATH"/tmp/.X*-lock "$POWERVM_LX86_PATH"/tmp/.lock.* "$POWERVM_LX86_PATH"/tmp/.gdm_socket "$POWERVM_LX86_PATH"/tmp/.s.PGSQL.*
  rm -rf "$POWERVM_LX86_PATH"/tmp/.X*-unix "$POWERVM_LX86_PATH"/tmp/.ICE-unix "$POWERVM_LX86_PATH"/tmp/.font-unix "$POWERVM_LX86_PATH"/tmp/hsperfdata_* \
         "$POWERVM_LX86_PATH"/tmp/kde-* "$POWERVM_LX86_PATH"/tmp/ksocket-* "$POWERVM_LX86_PATH"/tmp/mc-* "$POWERVM_LX86_PATH"/tmp/mcop-* "$POWERVM_LX86_PATH"/tmp/orbit-*  \
         "$POWERVM_LX86_PATH"/tmp/scrollkeeper-*  "$POWERVM_LX86_PATH"/tmp/ssh-* "$POWERVM_LX86_PATH"/tmp/screens "$POWERVM_LX86_PATH"/tmp/uscreens \
         "$POWERVM_LX86_PATH"/var/run/screens "$POWERVM_LX86_PATH"/var/run/uscreens "$POWERVM_LX86_PATH"/tmp/ssh-*/*agent* "$POWERVM_LX86_PATH"/var/spool/uucp/LCK* \
         "$POWERVM_LX86_PATH"/fsck_corrected_errors
         
  rm -f "$POWERVM_LX86_PATH"/fastboot "$POWERVM_LX86_PATH"/fsckoptions "$POWERVM_LX86_PATH"/forcefsck "$POWERVM_LX86_PATH"/.autofsck "$POWERVM_LX86_PATH"/halt "$POWERVM_LX86_PATH"/poweroff &> /dev/null

  mkdir -m 1777 -p "$POWERVM_LX86_PATH"/tmp/.ICE-unix >/dev/null 2>&1
  chown root:root "$POWERVM_LX86_PATH"/tmp/.ICE-unix    
  if [ -x "$POWERVM_LX86_PATH"/sbin/pam_console_apply ]
  then
    $RUNX86 /sbin/pam_console_apply -r
  fi

  ls "$POWERVM_LX86_PATH"/etc/resolv.conf.saved.by.* &>/dev/null 
  if [ "$?" -eq 0 -a -e "$POWERVM_LX86_PATH/sbin/modify_resolvconf" ]
  then
    # This is called from start_x86_services, powervm-lx86-daemon should be ready at this point
    $RUNX86 /sbin/modify_resolvconf cleanup
  fi  
}

start_x86_services()
{
  pgrep -x -f "$POWERVM_LX86DIR/powervm-lx86-daemon" &>/dev/null
  if [ "$?" -eq 0 -a -e "$POWERVM_LX86_PATH/etc/.sw_installed_successfully" -a -x "$RUNX86" ] ; then
    cd "$POWERVM_LX86_PATH"
    # we will have two blogd instances using the same /dev/blog
    # creating boot.msg in the x86 world will prevent confusion
    if [ ! -e "$POWERVM_LX86_PATH/var/log/boot.msg" ] ; then
      touch "$POWERVM_LX86_PATH/var/log/boot.msg" &>/dev/null
    fi
    set `/sbin/runlevel`
    export PREVLEVEL=N
    export RUNLEVEL=$2
    if [ -z "$RUNLEVEL" ] ;
    then
      log_failure_msg "Starting x86 services"
      return
    fi
    echo "Starting x86 services"
    if [ -e "/etc/SuSE-release" ] ;
    then
      # SuSE rc wants the runlevels in environment variables
      STARTCOMMAND="$RUNX86 /etc/init.d/rc"
    else
      # RedHat rc wants the new runlevel as argument
      STARTCOMMAND="$RUNX86 /etc/rc.d/rc $RUNLEVEL"
    fi

    x86_pre_boot 

    RC_TIMEOUT=25
    ${STARTCOMMAND} &
    RC_PID=$!

    RC_COUNTER=0
    while true ; do
      if [ ${RC_COUNTER} -lt ${RC_TIMEOUT} ] ; then
        if ps ${RC_PID} &>/dev/null ; then
          # rc is still running, lets wait 1 more sec
          sleep 1
          ((RC_COUNTER=RC_COUNTER+1))
        else
          # rc's finished
          break
        fi
      else
        # rc is taking too long to run, RC_TIMEOUT secs have passed
        # lets continue and let rc run in the background
        echo " continuing in the background"
        break
      fi  
    done
  fi
}

# Returns 1 if powervm-lx86 is already running, 0 otherwise
is_lx86_running()
{
  pgrep -x -f "$POWERVM_LX86DIR/powervm-lx86-daemon" 1>/dev/null 2>/dev/null
  if [ $? -eq 0 ];
  then
    # If we have /bin/true in our x86 World then as an extra
    # check we can also try running it. This checks
    # that the daemon is actually healthy since powervm-lx86 will
    # shake hands with the server.
    if [ -x "$POWERVM_LX86_PATH/bin/true" ];
    then
      cd "$POWERVM_LX86_PATH"
      "$POWERVM_LX86DIR/powervm-lx86" /bin/true >/dev/null 2>/dev/null
      if [ $? -eq 0 ];
      then
        return 1;
      else
        return 0;
      fi
    fi
    return 1;
  else
    return 0;
  fi
}

setup_selinux()
{
  if [ $SELINUX_TRANS_ENABLED = "y" -a -e /etc/opt/powervm-lx86/exec_types ];
  then

    SELINUX_DIR=$(load_or_default SELINUX_EXEC_PATH /var/opt/powervm-lx86/selinux)

    if [ ! -d $SELINUX_DIR ];
    then
      mkdir -p $SELINUX_DIR;
    fi

    for type in `cat /etc/opt/powervm-lx86/exec_types`;
    do
      SELINUX_SCRIPT=$SELINUX_DIR/$type;
      echo "#!$POWERVM_LX86DIR/powervm-lx86 -s" > $SELINUX_SCRIPT;
      chmod a+x $SELINUX_SCRIPT;
      SCRIPTCONTEXT="system_u:object_r:lx86_$type"
      if [ 0 -eq $IS_RHEL5 ]
      then
        SCRIPTCONTEXT="${SCRIPTCONTEXT}:s0"
      fi

      # This can fail, if the policy doesn't contain the appropriate type, that shouldn't 
      # be a problem.
      setfattr -n security.selinux -v "$SCRIPTCONTEXT" $SELINUX_SCRIPT >/dev/null 2>/dev/null;
    done
  fi
}

case "$1" in
start)
    # Things we should do on startup:
    # Try to start the server first; if this doesn't fail (e.g. licence problem) 
    # then continue. 
    check_privileges

    [ -e "/etc/redhat-release" ] && setup_selinux

    # Check whether powervm-lx86 is already running
    is_lx86_running
    if [ $? -eq 1 ];
    then
      log_success_msg "powervm-lx86 is already running"
      exit 0
    fi

    daemon_start
    if [ $? -eq 0 ] ;
    then
        [ -e "/etc/redhat-release" ] && touch $LOCKFILE
        # Then, enable binfmt_misc:
        setup_binfmt_misc
        if [ $? -eq 0 ] ;
        then
            echo -n
        else
            # Binfmt_misc failed, stop the daemon
            log_failure_msg "Failed to install binfmt misc handler"
            daemon_stop
            exit 1
        fi
        run_rcmonitor
        start_x86_services
        exit 0
    else
        exit 1
    fi
;;

stop)
  # Note stopping an already stopped service is considered success, as per LSB
	check_privileges

        # Check whether powervm-lx86 is already stopped
        is_lx86_running
        if  [ $? -eq 0 ];
        then
          log_success_msg "powervm-lx86 is already stopped"
          exit 0
        fi

        stop_x86_services
	reset_binfmt_misc
	stop_rcmonitor
	daemon_stop
        [ -e "/etc/redhat-release" ] && rm -f $LOCKFILE
	exit 0
;;

status)
  # First test if the powervm-lx86-daemon is running
  is_lx86_running
  if [ $? -eq 1 ];
  then
    log_success_msg "powervm-lx86 is running"
    exit 0
  else
    log_success_msg "powervm-lx86 is stopped"
    exit 1
  fi
;;

# As status, but don't write anything.  log_success_msg writes to syslog on some systems.
qstatus)
  # First test if the powervm-lx86-daemon is running
  is_lx86_running
  if [ $? -eq 1 ];
  then
    exit 0
  else
    exit 1
  fi
;;

force-reload|restart)
  $0 stop
  $0 start  
;;

*)
  echo "Usage: $0 {start|stop|force-reload|restart|status}";
  exit 3
;;

esac
exit 0
# end of powervm-lx86

