diff --git a/etc/init.d/snail-watcher b/etc/init.d/snail-watcher new file mode 100755 index 0000000..1ccdf30 --- /dev/null +++ b/etc/init.d/snail-watcher @@ -0,0 +1,30 @@ +#!/sbin/runscript + +DAEMON="/usr/local/sbin/snail.watcher.sh" +DAEMON_ARGS="" +PIDFILE=/var/run/snail.pid +#PATH=/usr/local/sbin:/usr/local/bin:$PATH +PATH=$PATH:/usr/local/bin + +depend() { + need localmount +} + +start() { + ebegin "Starting ${SVCNAME}" + start-stop-daemon --make-pidfile \ + --background \ + --start \ + --exec $DAEMON \ + --pidfile $PIDFILE \ + -- $DAEMON_ARGS + eend $? +} + +stop() { + ebegin "Stopping ${SVCNAME}" + start-stop-daemon --stop \ + --exec $DAEMON \ + --pidfile $PIDFILE + eend $? +} diff --git a/etc/init.d/snail b/etc/init.d/snail-xserver similarity index 80% rename from etc/init.d/snail rename to etc/init.d/snail-xserver index 56983b4..d258dd1 100755 --- a/etc/init.d/snail +++ b/etc/init.d/snail-xserver @@ -15,9 +15,13 @@ start() { export LD_LIBRARY_PATH=/usr/lib/opengl/nvidia/lib:/usr/lib/opengl/nvidia/lib start-stop-daemon --start --quiet --background --pidfile $PIDFILE --exec $DAEMON -- $DAEMON_ARGS status=$? + # FIXME: remove --background flag and next cycle let i=300 - while [ 0 -lt $i ]; do snail.nvstat >/dev/null && break; sleep 0.01; let i--; done - (nohup snail.autostop &>/dev/null)& + while [ 0 -lt $i ]; do + [ -f $PIDFILE ] && pgrep -s `cat $PIDFILE` &>/dev/null && break + sleep 0.01 + let i-- + done eend $status } @@ -27,4 +31,3 @@ stop() { snail.nv_pwr_off || eend -1 eend $result } - diff --git a/etc/local.d/snail.start b/etc/local.d/snail.start deleted file mode 100755 index 99c819c..0000000 --- a/etc/local.d/snail.start +++ /dev/null @@ -1,3 +0,0 @@ -# /etc/conf.d/local.start - -snail.nv_pwr_off diff --git a/usr/local/sbin/snail.install b/snail.install similarity index 70% rename from usr/local/sbin/snail.install rename to snail.install index 2f6511f..7c3d665 100755 --- a/usr/local/sbin/snail.install +++ b/snail.install @@ -4,7 +4,7 @@ PREFIX=/usr/local # Tell the system to use xorg-x11 OpenGL libraries and nVidia GLX extensions eselect opengl set xorg-x11 -for arch in 32bit 64bit; do for chip in i915 i965 r300 r600 sw; do sudo eselect mesa set $arch $chip classic &>/dev/null; done; done +for arch in 32bit 64bit; do for chip in i915 i965 r300 r600 sw; do eselect mesa set $arch $chip classic &>/dev/null; done; done # Create backups of original xorg.conf.* files and replacing with snail's versions [[ -f /etc/X11/xorg.conf && ! -f /etc/X11/xorg.conf.snail-bkp ]] && mv /etc/X11/xorg.conf /etc/X11/xorg.conf.snail-bkp @@ -19,9 +19,3 @@ BusIDIntel="PCI:`lspci | grep VGA | grep Intel | cut -d" " -f1 | sed 's~\.~\:~g BusIDNvidia="PCI:`lspci | grep VGA | grep nVidia | cut -d" " -f1 | sed 's~\.~\:~g ; s~0\([0-9]\)~\1~g'`" sed -i "s~BusIDIntel~$BusIDIntel~" /etc/X11/xorg.conf.intel sed -i "s~BusIDNvidia~$BusIDNvidia~" /etc/X11/xorg.conf.nvidia - -# Allow in sudo for %video group to start/stop second X-server with nVidia card and run snail.dmidecode -sed -i '/snail/d' /etc/sudoers -sudoers_lines=`wc -l /etc/sudoers | cut -d" " -f1` -sed -i "${sudoers_lines}a#snail:\n#snail: Allow %video group to start/stop second X-server with nVidia card\n#snail:\n%video ALL= NOPASSWD: /etc/init.d/snail start\n%video ALL= NOPASSWD: /etc/init.d/snail stop\n%video ALL= NOPASSWD: /usr/local/sbin/snail.dmidecode" /etc/sudoers - diff --git a/snail.uninstall b/snail.uninstall new file mode 100755 index 0000000..b3bcd02 --- /dev/null +++ b/snail.uninstall @@ -0,0 +1,34 @@ +#!/bin/sh + +# Tell the system to use both xorg-x11 OpenGL and GLX libraries +eselect opengl set xorg-x11 + +# Restore xorg.conf.* files from backups +[ -f /etc/X11/xorg.conf.snail-bkp ] && rm -f /etc/X11/xorg.conf && mv /etc/X11/xorg.conf.snail-bkp /etc/X11/xorg.conf +[ -f /etc/X11/xorg.conf.intel.snail-bkp ] && rm -f /etc/X11/xorg.conf.intel && mv /etc/X11/xorg.conf.intel.snail-bkp /etc/X11/xorg.conf.intel +[ -f /etc/X11/xorg.conf.nvidia.snail-bkp ] && rm -f /etc/X11/xorg.conf.nvidia && mv /etc/X11/xorg.conf.nvidia.snail-bkp /etc/X11/xorg.conf.nvidia + +# Remove backup files +for f in "" .intel .nvidia ; do + rm -f /etc/X11/xorg.conf${f}.snail-bkp +done + +# Remove all Snail files +rm -f /etc/X11/xorg.conf.intel.snail +rm -f /etc/X11/xorg.conf.nvidia +rm -f /etc/init.d/snail-watcher +rm -f /etc/init.d/snail-xserver +rm -f /usr/local/bin/nvrun +rm -f /usr/local/bin/snail.vglrun +rm -f /usr/local/lib64/libdlfaker.so +rm -f /usr/local/lib64/libgefaker.so +rm -f /usr/local/lib64/librrfaker.so +rm -f /usr/local/lib64/snail/extensions +rm -f /usr/local/lib64/snail/modules/drivers +rm -f /usr/local/lib64/snail/modules/extensions +rm -f /usr/local/lib64/snail/modules/libfb.so +rm -f /usr/local/lib64/snail/modules/libwfb.so +rmdir /usr/local/lib64/snail/modules 2>/dev/null +rm -f /usr/local/sbin/snail.nv_pwr_off +rm -f /usr/local/sbin/snail.nv_pwr_on +rm -f /usr/local/sbin/snail.watcher diff --git a/usr/local/bin/nvrun b/usr/local/bin/nvrun index 8ecb30e..d8bf067 100755 --- a/usr/local/bin/nvrun +++ b/usr/local/bin/nvrun @@ -1,68 +1,32 @@ -#!/bin/sh +#!/bin/bash -LOCK_FILE=/tmp/.snail.nvrun.lock -NVRUN=snail.nvrun +PATH=/usr/local/sbin:/usr/local/bin:$PATH -( - let i=16 - while [ 0 -lt $i ]; do - flock -n 9 - status=$? +TIMEOUT=10 # wait timeout for X server start +let INTEL_IF_FAIL=1 # run on Intel if nVidia fails - if [[ 0 -ne $status && 5 -eq $i ]]; then - echo "Cann't lock $COUNT_FILE. Exiting..." - exit -1 +SERVICE=/etc/init.d/snail-xserver +PROC=nvrun - elif [ 0 -eq $status ]; then - break - fi +LOCK_FILE=/tmp/.snail.lock +FIFO_FILE=/tmp/.snail.fifo - sleep 0.01 - let i-- - done - snail.nvstat || snail.nvon +{ + echo -n > $FIFO_FILE & + flock -w $TIMEOUT -s 9 - if [ 0 -ne $? ]; then - echo "Cann't switch on nVidia graphics. Exiting..." - exit -1 - fi + if [ 0 -eq $? ]; then + echo "Running $@ on nVidia" + LD_PRELOAD="/usr/lib/opengl/nvidia/lib/libGL.so:/usr/lib/opengl/nvidia/lib/libnvidia-tls.so" snail.vglrun -d :1 "$@" + echo "Application $@ was performed on nVidia" -) 9>$LOCK_FILE || exit $? + elif [ 0 -ne $INTEL_IF_FAIL ]; then + echo "Running $@ on Intel" + "$@" + echo "Application $@ was performed on Intel" + fi -snail.nvrun "$@" - -( - let i=16 - while [ 0 -lt $i ]; do - flock -n 9 - status=$? - - if [[ 0 -ne $status && 5 -eq $i ]]; then - echo "Cann't lock $COUNT_FILE. Exiting..." - exit -1 - - elif [ 0 -eq $status ]; then - break - fi - - sleep 0.01 - let i-- - done - - pgrep $NVRUN &>/dev/null - - if [ 0 -eq $? ]; then - echo "Other applications still use nVidia chip. So we aren't switching off it, just exiting..." - exit 0 - else - snail.nvoff - [ 0 -ne $? ] && echo "Error: Cann't switch off nVidia graphics. Exiting..." && exit -1 - echo "nVidia chip powered off." - exit 0 - fi - -) 9>$LOCK_FILE - -exit $? + flock -u 9 +} 9>$LOCK_FILE diff --git a/usr/local/bin/snail.nvoff b/usr/local/bin/snail.nvoff deleted file mode 100755 index d979229..0000000 --- a/usr/local/bin/snail.nvoff +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/sh - -sudo /etc/init.d/snail stop - diff --git a/usr/local/bin/snail.nvon b/usr/local/bin/snail.nvon deleted file mode 100755 index c6e3214..0000000 --- a/usr/local/bin/snail.nvon +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/sh - -sudo /etc/init.d/snail start - diff --git a/usr/local/bin/snail.nvrun b/usr/local/bin/snail.nvrun deleted file mode 100755 index 3625894..0000000 --- a/usr/local/bin/snail.nvrun +++ /dev/null @@ -1,16 +0,0 @@ -#!/bin/sh - -snail.nvstat - -if [ "$?" == 0 ]; then - echo "Running $@ on nVidia Optimus" - LD_PRELOAD="/usr/lib/opengl/nvidia/lib/libGL.so:\ -/usr/lib/opengl/nvidia/lib/libnvidia-tls.so" snail.vglrun -d :1 "$@" - echo "Application $@ was performed on nVidia Optimus" - -else - echo "Running $@ on Intel GPU" - "$@" - echo "Application $@ was performed on Intel GPU" -fi - diff --git a/usr/local/bin/snail.nvstat b/usr/local/bin/snail.nvstat deleted file mode 100755 index e22ed95..0000000 --- a/usr/local/bin/snail.nvstat +++ /dev/null @@ -1,11 +0,0 @@ -#!/bin/sh - -[ -f /tmp/.X1-lock ] && pgrep -s `cat /tmp/.X1-lock` &>/dev/null - -let status=$? - -[ 0 -eq $status ] && echo "nVidia graphics is ACTIVE" \ - || echo "nVidia graphics is NOT ACTIVE" - -exit $status - diff --git a/usr/local/sbin/snail.autostop b/usr/local/sbin/snail.autostop deleted file mode 100755 index daeba88..0000000 --- a/usr/local/sbin/snail.autostop +++ /dev/null @@ -1,34 +0,0 @@ -#!/bin/sh - -LOCK_FILE=/tmp/.snail.nvrun.lock -NVRUN=nvrun - -while [ 1 ]; do - if [ ! `pgrep $NVRUN` ]; then - - ( - for i in 0 1 2 3 4 5; do - flock -n 9 - status=$? - - if [[ 0 -ne $status && 5 -eq $i ]]; then - echo "Cann't lock $COUNT_FILE. Exiting..." - exit -1 - - elif [ 0 -eq $status ]; then - break - fi - - sleep 0.01 - done - - snail.nvoff - [ 0 -ne $? ] && echo "Error: Cann't switch off nVidia graphics. Exiting..." && exit -1 - echo "nVidia chip powered off." - exit 0 - ) 9>$LOCK_FILE ; exit $? - fi - - sleep 0.1 -done - diff --git a/usr/local/sbin/snail.dmidecode b/usr/local/sbin/snail.dmidecode deleted file mode 100755 index fd8af70..0000000 --- a/usr/local/sbin/snail.dmidecode +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/sh - -dmidecode $@ -s system-product-name - diff --git a/usr/local/bin/snail.nv_pwr_off b/usr/local/sbin/snail.nv_pwr_off similarity index 98% rename from usr/local/bin/snail.nv_pwr_off rename to usr/local/sbin/snail.nv_pwr_off index 016aea4..e8bf408 100755 --- a/usr/local/bin/snail.nv_pwr_off +++ b/usr/local/sbin/snail.nv_pwr_off @@ -1,6 +1,6 @@ #!/bin/sh -model=`sudo snail.dmidecode` +model=`dmidecode $@ -s system-product-name` let retry=8 while [ 0 -lt $retry ]; do diff --git a/usr/local/bin/snail.nv_pwr_on b/usr/local/sbin/snail.nv_pwr_on similarity index 97% rename from usr/local/bin/snail.nv_pwr_on rename to usr/local/sbin/snail.nv_pwr_on index f35ec3e..2ede6e2 100755 --- a/usr/local/bin/snail.nv_pwr_on +++ b/usr/local/sbin/snail.nv_pwr_on @@ -1,6 +1,6 @@ #!/bin/sh -model=`sudo snail.dmidecode` +model=`dmidecode $@ -s system-product-name` let retry=8 while [ 0 -lt $retry ]; do diff --git a/usr/local/sbin/snail.uninstall b/usr/local/sbin/snail.uninstall deleted file mode 100755 index 43bdcbd..0000000 --- a/usr/local/sbin/snail.uninstall +++ /dev/null @@ -1,18 +0,0 @@ -#!/bin/sh - -# Tell the system to use both xorg-x11 OpenGL and GLX libraries -eselect opengl set xorg-x11 - -# Restore xorg.conf.* files from backups -[ -f /etc/X11/xorg.conf.snail-bkp ] && rm -f /etc/X11/xorg.conf && mv /etc/X11/xorg.conf.snail-bkp /etc/X11/xorg.conf -[ -f /etc/X11/xorg.conf.intel.snail-bkp ] && rm -f /etc/X11/xorg.conf.intel && mv /etc/X11/xorg.conf.intel.snail-bkp /etc/X11/xorg.conf.intel -[ -f /etc/X11/xorg.conf.nvidia.snail-bkp ] && rm -f /etc/X11/xorg.conf.nvidia && mv /etc/X11/xorg.conf.nvidia.snail-bkp /etc/X11/xorg.conf.nvidia - -# Remove backup files -for f in "" .intel .nvidia ; do - rm -f /etc/X11/xorg.conf${f}.snail-bkp -done - -# Remove records in sudoers -sed -i '/snail/d' /etc/sudoers - diff --git a/usr/local/sbin/snail.watcher.sh b/usr/local/sbin/snail.watcher.sh new file mode 100755 index 0000000..16384ad --- /dev/null +++ b/usr/local/sbin/snail.watcher.sh @@ -0,0 +1,28 @@ +#!/bin/bash + +PATH=/usr/local/sbin:/usr/local/bin:$PATH + +SERVICE=/etc/init.d/snail-xserver +PROC=nvrun + +LOCK_FILE=/tmp/.snail.lock +FIFO_FILE=/tmp/.snail.fifo + +$SERVICE stop && snail.nv_pwr_off # stop X service + +touch $LOCK_FILE && chown root:video $LOCK_FILE && chmod 660 $LOCK_FILE # create lock file +rm -f $FIFO_FILE && mkfifo --mode 660 $FIFO_FILE && chgrp video $FIFO_FILE # create fifo + +{ + echo >$FIFO_FILE | cat $FIFO_FILE >/dev/null # flush fifo + flock -n 9 # make exclusive lock + + while [ 1 ]; do + read <$FIFO_FILE + snail.nv_pwr_on && $SERVICE start # starting X server + flock -u 9 + flock 9 + echo >$FIFO_FILE | cat $FIFO_FILE >/dev/null # flush fifo + $SERVICE stop && snail.nv_pwr_off # stopping X server + done +} 9>$LOCK_FILE