#!/bin/bash
#
# Copyright IBM Corporation © 2005, 2008
#
# Original Author: John Kacur <jkacur at ca.ibm.com>
#

CHKCONFIG=/sbin/chkconfig

info() {
    echo $0: $*
}

check_sysctl_setting()
{
    key=${1//\//.}
    val=$2
    if grep -q "^[[:space:]]*$key[[:space:]]*=[[:space:]]*$val[[:space:]]*\$" \
            /etc/sysctl.conf; then
        info "Setting on sysctl $key is correct ($val)"
    else
        echo $key = $val >> /etc/sysctl.conf
    fi
    /sbin/sysctl -p >/dev/null
    act=`cat /proc/sys/${key//./\/}`
    if [ "$val" != "$act" ]; then
        info "Setting of sysctl $key is incorrect in /proc/sys" 1>&2
        exit 1
    fi
}

#---------------------------------------------------------------------------#
# Create the realtime group.                                                #
#---------------------------------------------------------------------------#

# This will be the default group (the current default group is different)
DEFAULT_GROUP=realtime

# Although the groupadd can be executed even if the group already exists
# we test for its existence anyway, for better messages to the user
if grep -q ^${DEFAULT_GROUP}: /etc/group; then
       info "The group ${DEFAULT_GROUP} already exists, no need to readd it."
else
       /usr/sbin/groupadd ${DEFAULT_GROUP} > /dev/null 2>&1
       info "The group ${DEFAULT_GROUP} was successfully added."
fi

#-----------------------------------------------------------------------#
# Make the realtime group the default group.                            #
#-----------------------------------------------------------------------#

# Extract the realtime group's gid
DEFAULT_GID=$(awk -F: "/${DEFAULT_GROUP}/"'{ print $3 }' /etc/group)

if [ "${DEFAULT_GID}" = "$(grep "GROUP=" /etc/default/useradd | awk -F= '{ print $2 }')" ]; then
       info "The default group is already ${DEFAULT_GROUP}, no changes are necessary."
else

       # IF = input file
       IF=/etc/default/useradd

       # Move it out of the way
       mv ${IF} ${IF}.old

       # Generate the new one
       # The following line creates a line specifying the default group
       echo GROUP=${DEFAULT_GID} > ${IF}

       # And this line deletes the old line specifying the default group
       sed /GROUP=/d ${IF}.old >> ${IF}
       
       info "The default group has been changed to ${DEFAULT_GROUP}"
fi

#-----------------------------------------------------------------------#
# Setup realtime group limits                                           #
#-----------------------------------------------------------------------#

TIMEOUT=unlimited                       # in minutes
RT_PRIORITY=100
NICE=-20
MEMLOCK=unlimited
LIMITSCONF=/etc/security/limits.conf

if grep -q @${DEFAULT_GROUP} ${LIMITSCONF}; then
    info "The group ${DEFAULT_GROUP} is already set up for security."
else
    if ! sed -i -e "/^# End of file/d" /etc/security/limits.conf; then
        info "Modification of /etc/security/limits.conf failed."
        exit 1
    fi
    cat >>${LIMITSCONF} <<EOF
@realtime       soft    cpu             $TIMEOUT
@realtime       -       rtprio          $RT_PRIORITY
@realtime       -       nice            $NICE
@realtime       -       memlock         $MEMLOCK
# End of file
EOF
fi

#-----------------------------------------------------------------------#
# Increase the maximum precision available to users of /dev/rtc         #
#-----------------------------------------------------------------------#

# (FIXME: needed?) 
# check_sysctl_setting dev.rtc.max-user-freq 8192

#-----------------------------------------------------------------------#
# Prevent blanking and set sysrq for the current running kernel         #
#-----------------------------------------------------------------------#

setterm -blank 0 < /dev/tty1 > /dev/tty1 2>/dev/null

# Add the line kernel.sysrq = 1 to /etc/sysctl.conf
check_sysctl_setting kernel.sysrq 1

#-----------------------------------------------------------------------#
# add "kernel.numa_balancing = 0" in /etc/sysctl.conf to avoid           #
# unexpected inconsistent behaivor in future RT tests (bsc#1173678)     # 
#-----------------------------------------------------------------------#

echo "kernel.numa_balancing = 0" >> /etc/sysctl.conf

#-----------------------------------------------------------------------#
# Configure irqbindall and irqbalance services                          #
#-----------------------------------------------------------------------#

info "Turning irqbindall on and irqbalance off."
$CHKCONFIG -s irqbindall on
$CHKCONFIG -s irqbalance off

info "Turning set_kthread_prio on."
$CHKCONFIG -s set_kthread_prio on

info "All changes were successfully executed."

exit 0
