#!/bin/bash
#
# ksmtuned     Kernel Samepage Merging (KSM) Tuning Daemon
#
# Author:      Dan Kenigsberg <danken@redhat.com>
#
# Copyright 2009 Red Hat, Inc. and/or its affiliates.
# Released under the GPL
#
# chkconfig: - 85 15
# description: The KSM tuning daemon controls whether (and with what vigor) \
#              ksm should ksm search duplicated pages.
# processname: ksmtuned
# config: /etc/ksmtuned.conf
# pidfile: /var/run/ksmtuned.pid
#
### BEGIN INIT INFO
# Provides: ksmtuned
# Required-Start: $remote_fs
# Required-Stop: $remote_fs
# Should-Start:
# Default-Start: 3 4 5
# Default-Stop: 0 1 2 6
# Short-Description: tune the speed of ksm
# Description: The Kernel Samepage Merging control Daemon is a simple script
#   that controls whether (and with what vigor) should ksm search duplicated
#   memory pages.
#   needs testing and ironing. contact danken@redhat.com if something breaks.
### END INIT INFO
WITHOUT_RC_COMPAT=1

# Source function library.
. /etc/init.d/functions

PIDFILE=/var/run/ksmtuned.pid
LOCKFILE=/var/lock/subsys/ksmtuned
RETVAL=0

start() {
    [ -d /sys/kernel/mm/ksm/ ] || exit 0;
    start_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE"  --expect-user root -- ksmtuned
    RETVAL=$?
    return $RETVAL
}

stop() {
    stop_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" --displayname ksmtuned bash 
    RETVAL=$?
    return $RETVAL
    rm -f $PIDFILE
}


restart() {
    stop
    start
}

reload() {
    msg_reloading acpid
    stop_daemon --pidfile "$PIDFILE" --expect-user root -SIGUSR1 --displayname ksmtuned bash
    RETVAL=$?
    return $RETVAL
}

case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  status)
	status --pidfile "$PIDFILE" --expect-user root --displayname ksmtuned bash
	RETVAL=$?
	;;
  restart)
	restart
	;;
  reload)
	restart
	;;
  condstop)
	if [ -e "$LOCKFILE" ]; then
	    stop
	fi
	;;
  condrestart)
	if [ -e "$LOCKFILE" ]; then
	    restart
	fi
	;;
  condreload)
	if [ -e "$LOCKFILE" ]; then
	    reload
	fi
	;;
  *)
	msg_usage "${0##*/} {start|stop|reload|restart|condstop|condrestart|condreload|status|}"
	RETVAL=1
esac

exit $RETVAL
