From 7c4f48df57bf94beba40635c9c8cdc7be18510d8 Mon Sep 17 00:00:00 2001 From: Kolan Sh Date: Tue, 20 Mar 2012 16:46:24 +0400 Subject: [PATCH 1/2] sudo removed --- etc/init.d/snail-watcher | 26 ++++++ etc/init.d/{snail => snail-xserver} | 9 ++- etc/local.d/snail.start | 3 - usr/local/sbin/snail.install => snail.install | 8 +- snail.uninstall | 34 ++++++++ usr/local/bin/nvrun | 80 +++++-------------- usr/local/bin/snail.nvoff | 4 - usr/local/bin/snail.nvon | 4 - usr/local/bin/snail.nvrun | 16 ---- usr/local/bin/snail.nvstat | 11 --- usr/local/sbin/snail.autostop | 34 -------- usr/local/sbin/snail.dmidecode | 4 - usr/local/{bin => sbin}/snail.nv_pwr_off | 2 +- usr/local/{bin => sbin}/snail.nv_pwr_on | 2 +- usr/local/sbin/snail.uninstall | 18 ----- usr/local/sbin/snail.watcher | 31 +++++++ 16 files changed, 121 insertions(+), 165 deletions(-) create mode 100755 etc/init.d/snail-watcher rename etc/init.d/{snail => snail-xserver} (80%) delete mode 100755 etc/local.d/snail.start rename usr/local/sbin/snail.install => snail.install (70%) create mode 100755 snail.uninstall delete mode 100755 usr/local/bin/snail.nvoff delete mode 100755 usr/local/bin/snail.nvon delete mode 100755 usr/local/bin/snail.nvrun delete mode 100755 usr/local/bin/snail.nvstat delete mode 100755 usr/local/sbin/snail.autostop delete mode 100755 usr/local/sbin/snail.dmidecode rename usr/local/{bin => sbin}/snail.nv_pwr_off (98%) rename usr/local/{bin => sbin}/snail.nv_pwr_on (97%) delete mode 100755 usr/local/sbin/snail.uninstall create mode 100755 usr/local/sbin/snail.watcher diff --git a/etc/init.d/snail-watcher b/etc/init.d/snail-watcher new file mode 100755 index 0000000..fa96087 --- /dev/null +++ b/etc/init.d/snail-watcher @@ -0,0 +1,26 @@ +#!/sbin/runscript + +DAEMON="/usr/local/sbin/snail.watcher" +DAEMON_ARGS="" +PIDFILE=/var/run/snail.pid +PATH=$PATH:/usr/local/bin + +depend() { + need localmount +} + +start() { + ebegin "Starting ${SVCNAME}" + start-stop-daemon -m -b --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..3ddd719 100755 --- a/usr/local/bin/nvrun +++ b/usr/local/bin/nvrun @@ -1,68 +1,30 @@ -#!/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=5 # 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 $? - -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 $? + elif [ 0 -ne $INTEL_IF_FAIL ]; then + echo "Running $@ on Intel" + "$@" + echo "Application $@ was performed on Intel" + fi +} 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 b/usr/local/sbin/snail.watcher new file mode 100755 index 0000000..6420e32 --- /dev/null +++ b/usr/local/sbin/snail.watcher @@ -0,0 +1,31 @@ +#!/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 0:0 $LOCK_FILE # create lock file +rm -f $FIFO_FILE && mkfifo --mode 640 $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 + cat <$FIFO_FILE >/dev/null + flock -u 9 # unlock to get access for clients + flock -n 9 + if [ 0 -ne $? ]; then # test is $LOCK_FILE locked by any client + snail.nv_pwr_on && $SERVICE start # starting X server + flock 9 + echo >$FIFO_FILE | cat <$FIFO_FILE >/dev/null # flush fifo + $SERVICE stop && snail.nv_pwr_off # stopping X server + fi + done +} 9 > $LOCK_FILE From 7d36cbda32d3269546d9295f5046f7263874448c Mon Sep 17 00:00:00 2001 From: Kolan Sh Date: Wed, 21 Mar 2012 00:15:32 +0400 Subject: [PATCH 2/2] fifo/lock fixes --- etc/init.d/snail-watcher | 18 +++++++++++------- usr/local/bin/nvrun | 8 +++++--- usr/local/sbin/snail.watcher | 31 ------------------------------- usr/local/sbin/snail.watcher.sh | 28 ++++++++++++++++++++++++++++ 4 files changed, 44 insertions(+), 41 deletions(-) delete mode 100755 usr/local/sbin/snail.watcher create mode 100755 usr/local/sbin/snail.watcher.sh diff --git a/etc/init.d/snail-watcher b/etc/init.d/snail-watcher index fa96087..1ccdf30 100755 --- a/etc/init.d/snail-watcher +++ b/etc/init.d/snail-watcher @@ -1,8 +1,9 @@ #!/sbin/runscript -DAEMON="/usr/local/sbin/snail.watcher" +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() { @@ -11,16 +12,19 @@ depend() { start() { ebegin "Starting ${SVCNAME}" - start-stop-daemon -m -b --start --exec $DAEMON \ - --pidfile $PIDFILE \ - -- $DAEMON_ARGS + 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 + start-stop-daemon --stop \ + --exec $DAEMON \ + --pidfile $PIDFILE eend $? } - diff --git a/usr/local/bin/nvrun b/usr/local/bin/nvrun index 3ddd719..d8bf067 100755 --- a/usr/local/bin/nvrun +++ b/usr/local/bin/nvrun @@ -2,7 +2,7 @@ PATH=/usr/local/sbin:/usr/local/bin:$PATH -TIMEOUT=5 # wait timeout for X server start +TIMEOUT=10 # wait timeout for X server start let INTEL_IF_FAIL=1 # run on Intel if nVidia fails SERVICE=/etc/init.d/snail-xserver @@ -13,7 +13,7 @@ FIFO_FILE=/tmp/.snail.fifo { - echo -n >$FIFO_FILE + echo -n > $FIFO_FILE & flock -w $TIMEOUT -s 9 if [ 0 -eq $? ]; then @@ -27,4 +27,6 @@ FIFO_FILE=/tmp/.snail.fifo echo "Application $@ was performed on Intel" fi -} 9 > $LOCK_FILE + flock -u 9 + +} 9>$LOCK_FILE diff --git a/usr/local/sbin/snail.watcher b/usr/local/sbin/snail.watcher deleted file mode 100755 index 6420e32..0000000 --- a/usr/local/sbin/snail.watcher +++ /dev/null @@ -1,31 +0,0 @@ -#!/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 0:0 $LOCK_FILE # create lock file -rm -f $FIFO_FILE && mkfifo --mode 640 $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 - cat <$FIFO_FILE >/dev/null - flock -u 9 # unlock to get access for clients - flock -n 9 - if [ 0 -ne $? ]; then # test is $LOCK_FILE locked by any client - snail.nv_pwr_on && $SERVICE start # starting X server - flock 9 - echo >$FIFO_FILE | cat <$FIFO_FILE >/dev/null # flush fifo - $SERVICE stop && snail.nv_pwr_off # stopping X server - fi - done -} 9 > $LOCK_FILE 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