#!/bin/bash
#
# Start/Stop the workload manager
#
# Copyright IBM Corporation. 2008
#
# Authors:     Balbir Singh <balbir@linux.vnet.ibm.com>
# This program is free software; you can redistribute it and/or modify it
# under the terms of version 2.1 of the GNU Lesser General Public License
# as published by the Free Software Foundation.
#
# This program is distributed in the hope that it would be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# cgconfig Control Groups Configuration Startup
# chkconfig: - 5 95
# description: This script runs the cgconfigparser utility to parse and setup
#              the control group filesystem. It uses /etc/cgconfig.conf
#              and parses the configuration specified in there.
# config:	/etc/cgconfig.conf
#

### BEGIN INIT INFO
# Provides:             cgconfig
# Required-Start:
# Required-Stop:
# Should-Start:         ypbind
# Should-Stop:          ypbind
# Short-Description:    Create and setup control group filesystem(s)
# Description:          Create and setup control group filesystem(s)
### END INIT INFO

WITHOUT_RC_COMPAT=1

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


CGCONFIGPARSER_BIN=cgconfigparser
SERVICENAME=cgconfig
CONFIG_FILE=/etc/cgconfig.conf

LOCKFILE=/var/lock/subsys/$SERVICENAME
RETVAL=0

# read the config
CREATE_DEFAULT=yes
SourceIfNotEmpty /etc/sysconfig/$SERVICENAME

create_default_groups() {
	defaultcgroup=

        if [ -f /etc/cgrules.conf ]; then
	    read user ctrl defaultcgroup <<< \
		    $(grep -m1 '^\*[[:space:]]\+' /etc/cgrules.conf)
            if [ -n "$defaultcgroup" -a "$defaultcgroup" = "*" ]; then
                passed "/etc/cgrules.conf incorrect"
                passed "Overriding it"
                defaultcgroup=
            fi
        fi

        if [ -z $defaultcgroup ]
        then
            defaultcgroup=sysdefault/
        fi

        #
        # Find all mounted subsystems and create comma-separated list
        # of controllers.
        #
        controllers=`lssubsys 2>/dev/null | tr '\n' ',' | sed s/.$//`

        #
        # Create the default group, ignore errors when the default group
        # already exists.
        #
        cgcreate -f 664 -d 775 -g $controllers:$defaultcgroup 2>/dev/null

        #
        # special rule for cpusets
        #
        if echo $controllers | grep -q -w cpuset; then
                cpus=`cgget -nv -r cpuset.cpus /`
                cgset -r cpuset.cpus=$cpus $defaultcgroup
                mems=`cgget -nv -r cpuset.mems /`
                cgset -r cpuset.mems=$mems $defaultcgroup
        fi

        #
        # Classify everything to default cgroup. Ignore errors, some processes
        # may exit after ps is run and before cgclassify moves them.
        #
        cgclassify -g $controllers:$defaultcgroup `ps --no-headers -eL o tid` \
                 2>/dev/null || :
}

startup_failure()
{
        msg_starting "cgconfig"
        printf "%s" "$1"
        failure "cgconfig startup"
        echo
}

check()
{
	# Check system before trying to start
	[ -d /sys/fs/cgroup ] || {
		startup_failure "kernel too old - no  /sys/fs/cgroup"
		return 1
	}
	# Prepare tmpfs for /sys/fs/cgroup
	if ! grep -q "[[:space:]]/sys/fs/cgroup[[:space:]]" /proc/mounts; then
		mount -t tmpfs -o noexec,nodev,nosuid tmpfs /sys/fs/cgroup
	fi
	echo -n "Checking cgconfig configuration: "
	if "$CGCONFIGPARSER_BIN" -l "$CONFIG_FILE"; then
		success "cgconfig check"
		RETVAL=0
	else
		failure "cgconfig check"
		RETVAL=1
	fi
	echo
	return $RETVAL
}

start() {
	check || return
	is_yes "$CREATE_DEFAULT" && create_default_groups
	touch "$LOCKFILE"
	return $RETVAL
}

stop() {
	action "Stopping cgconfig service: " cgclear
	RETVAL=$?
	[ $RETVAL -eq 0 ] && rm -f "$LOCKFILE"
	return $RETVAL
}

restart()
{
	stop
	start
}


case "$1" in
    stop)
        stop;
        ;;
    start)
        start;
        ;;
    restart)
        restart
        ;;
    reload)
        restart
        ;;
    condstop)
        if [ -e "$LOCKFILE" ]; then
                stop
        fi
        ;;
    condreload)
        if [ -e "$LOCKFILE" ]; then
                restart
        fi
        ;;
    condrestart)
        if [ -e "$LOCKFILE" ]; then
                restart
        fi
        ;;
    status)
        if [ -f "$LOCKFILE" ]; then
            echo "Running"
        else
            echo "Stopped"
        fi
        ;;
    check)
        check
        ;;
    *)
	msg_usage "${0##*/} {start|stop|restart|reload|status|condrestart|check}"
	RETVAL=1
	;;
esac

exit $RETVAL
