#!/bin/bash
# chkconfig: 2345 80 80
# description: Sets irq affinity to all cpus
#
### BEGIN INIT INFO
# Provides: irqbindall
# Required-Start:
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 
# Description: Sets irq affinity to all cpus
### END INIT INFO

. /etc/rc.status

prog=irqbindall

start() {
	if [ -r /proc/irq/default_smp_affinity ]
	then
		cpumask="$(cat /proc/irq/default_smp_affinity)"
	else 
		cpumask="fffffff"
	fi

	#disable irq balancer
	echo "Stopping irq balancer"
	systemctl stop irqbalance > /dev/null

	echo -n "Binding interrupts to cpu_mask: $cpumask... "
	exec 3>&1
	for i in `ls /proc/irq/ | grep '^[0-9]'`
	do
		stderr="$((echo "$cpumask" > /proc/irq/$i/smp_affinity) 2>&1 >&3 3>&-)"
		if [ -n "$stderr" ]; then
			logger -t irqbindall "IRQ$i: $stderr"
		fi
	done
	exec 3>&-

	echo "done."
	return 0
}

case "$1" in
  start)
        start
        ;;
  stop)
	/bin/true
        ;;
  restart)
        start
    ;;
  reload)
        start
        ;;
  condrestart)
	start
        ;;
  status)
        rc_status -v
        ;;
  *)
        echo $"Usage: $prog {start|stop|status|restart|reload|condrestart}"
        exit 1
esac

exit $?

