5c37348
#!/bin/sh
5c37348
#
5c37348
# qemu	Allow users to run non-native Linux programs by just clicking on them
5c37348
#	(or typing ./file.exe)
5c37348
#
5c37348
# chkconfig: 2345 35 98
5c37348
# description: Allow users to run non-native Linux programs by just clicking \
5c37348
#	       on them (or typing ./file.exe)
5c37348
5c37348
. /etc/rc.d/init.d/functions
5c37348
RETVAL=0
5c37348
QEMU=/usr/bin
5c37348
5c37348
start() {
5c37348
	cpu=`uname -m`
5c37348
        case "$cpu" in
5c37348
	    i386|i486|i586|i686|i86pc|BePC)
5c37348
		cpu="i386"
5c37348
		;;
5c37348
	    "Power Macintosh"|ppc|ppc64)
5c37348
		cpu="ppc"
5c37348
		;;
5c37348
	    armv4l|armv5l)
5c37348
		cpu="arm"
5c37348
		;;
d0fefc1
	    sh4)
d0fefc1
		cpu="sh4"
d0fefc1
		;;
5c37348
	esac
5c37348
5c37348
	echo -n $"Registering binary handler for qemu applications"
5c37348
	/sbin/modprobe binfmt_misc &>/dev/null
5c37348
	if [ "$cpu" != i386 -a -x $QEMU/qemu-i386 -a -d /usr/qemu-i386 ] ; then
4c8582a
	    echo ":qemu-i386:M::\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x03\x00:\xff\xff\xff\xff\xff\xfe\xfe\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfa\xff\xff\xff:$QEMU/qemu-i386:" > /proc/sys/fs/binfmt_misc/register
4c8582a
	    echo ":qemu-i486:M::\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x06\x00:\xff\xff\xff\xff\xff\xfe\xfe\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfa\xff\xff\xff:$QEMU/qemu-i386:" > /proc/sys/fs/binfmt_misc/register
5c37348
	fi
5c37348
	if [ "$cpu" != arm -a -x $QEMU/qemu-arm -a -d /usr/qemu-arm ] ; then
4c8582a
	    echo   ":qemu-arm:M::\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x28\x00:\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfa\xff\xff\xff:$QEMU/qemu-arm:" > /proc/sys/fs/binfmt_misc/register
5c37348
	fi
5c37348
	if [ "$cpu" != ppc -a -x $QEMU/qemu-ppc -a -d /usr/qemu-ppc ] ; then
4c8582a
	    echo   ":ppc:M::\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x14:\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfa\xff\xff\xff:$QEMU/qemu-ppc:" > /proc/sys/fs/binfmt_misc/register
5c37348
	echo do ppc
5c37348
	fi
5c37348
	if [ "$cpu" != sparc -a -x $QEMU/qemu-sparc -a -d /usr/qemu-sparc ] ; then
4c8582a
	    echo   ":qemu-sparc:M::\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x02:\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfa\xff\xff\xff:$QEMU/qemu-sparc:" > /proc/sys/fs/binfmt_misc/register
5c37348
	fi
d0fefc1
	if [ "$cpu" != sh4 -a -x $QEMU/qemu-sh4 -a -d /usr/qemu-sh4 ] ; then
4c8582a
	    echo   ":qemu-sh4:M::\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x2a\x00:\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfa\xff\xff\xff:$QEMU/qemu-sh4:" > /proc/sys/fs/binfmt_misc/register
d0fefc1
	fi
5c37348
	echo
5c37348
}
5c37348
5c37348
stop() {
5c37348
	echo -n $"Unregistering binary handler for qemu applications"
d0fefc1
	for a in i386 i486 ppc arm sparc sh4 ] ; do 
5c37348
	    [ -r /proc/sys/fs/binfmt_misc/qemu-$a ] && echo "-1" >/proc/sys/fs/binfmt_misc/qemu-$a
5c37348
	done
5c37348
	echo
5c37348
}
5c37348
5c37348
reload() {
5c37348
	stop
5c37348
	start
5c37348
}
5c37348
5c37348
qemu_status() {
5c37348
	if ls /proc/sys/fs/binfmt_misc/qemu-* &>/dev/null; then 
5c37348
		echo $"qemu binary format handlers are registered."
5c37348
		return 0
5c37348
	else
5c37348
		echo $"qemu binary format handlers are not registered."
5c37348
		return 3
5c37348
	fi
5c37348
}
5c37348
5c37348
case "$1" in
5c37348
	start)
5c37348
		start
5c37348
		;;
5c37348
	stop)
5c37348
		stop
5c37348
		;;
5c37348
	status)
5c37348
		qemu_status
5c37348
		RETVAL=$?
5c37348
		;;
5c37348
	restart)
5c37348
		stop
5c37348
		start
5c37348
		;;
5c37348
	condrestart)
5c37348
		if qemu_status &>/dev/null; then
5c37348
			stop
5c37348
			start
5c37348
		fi
5c37348
		;;
5c37348
	*)
5c37348
		echo $"Usage: $prog {start|stop|status|restart|condrestart}"
5c37348
		exit 1
5c37348
esac
5c37348
exit $RETVAL
5c37348