#!/bin/sh # Copyright © 2001, 2002, 2003, 2004, 2005 Jamie Zawinski # # Permission to use, copy, modify, distribute, and sell this software and its # documentation for any purpose is hereby granted without fee, provided that # the above copyright notice appear in all copies and that both that # copyright notice and this permission notice appear in supporting # documentation. No representations are made about the suitability of this # software for any purpose. It is provided "as is" without express or # implied warranty. # # Created: 10-Jun-2001 # # This script does a bunch of clean-up on the kiosks, to try and reset things # to a usable state without necessarily resorting to restarting X. # # - First, it checks whether the window manager and panel are running. # If they aren't, then it gives up and kills X (letting init restart it.) # # - Next, it checks to see whether xscreensaver is running. If it's not, # then it launches it, and exits. # # - Next, it checks to see whether Firefox is running and responsive. # If it's running but wedged, it is killed (hard.) # # - Next, it checks to see whether the screen saver is active (blanked.) # If the screen is not blanked, then someone is using the machine, and # we leave them alone. # # - If the screen is blanked, then we run kiosk-ps-reaper to kill off any # processes that shouldn't be running. # # - Finally, if Firefox is not running, it is launched. # progname=kiosk-nanny export DISPLAY=:0.0 export PATH=/bin:/usr/bin:/sbin:/usr/local/bin:/usr/local/sbin:/usr/bin/X11 hourly=false daily=false debug=false immunity=false usage() { echo "usage: $progname [ -hourly | -daily ] [ -debug ]" >&2 exit 1 } # process command line # while [ $# != 0 ]; do case "$1" in -hourly ) hourly=true ; shift ;; -daily ) daily=true ; shift ;; -debug ) debug=true ; shift ;; * ) usage ;; esac done if [ "$hourly" = true -a "$daily" = true ]; then usage fi # If /etc/nox exists, then we're not running X by default on this box, # so don't do anything here. # if [ -f /rw/etc/nox ]; then exit 0 fi # to avoid all the kiosks hammering the NFS server at once, this # script sleeps a custom amount of time for each. It does this # by finding the last byte of our IP address, subtracting 64, # and sleeping that number of quarter-minutes. # n=`ifconfig eth0 | sed -n 's/.* inet addr:[0-9]*\.[0-9]*\.[0-9]*\.\([0-9]*\).*$/\1/p'` if [ -z "$n" ]; then echo "$progname: can't find IP address?" >&2 exit 1 fi n=`echo "($n-64) * 15" | bc` if [ "$debug" = true ]; then echo "debug: not sleeping ($n)" >&2 else sleep $n fi # set some important environment variables . /usr/local/sbin/kiosk-env # log all stdout/stderr text to syslog log_lines() { while read line ; do if [ "$debug" = true ]; then echo "$line" else logger -t "$progname" "$line" fi done } ( ############################################################################## ## ## Check to see if the basic requirements are still running. ## ############################################################################## if [ "$daily" = true ]; then echo "daily reset: restarting X." >&2 killall X >/dev/null 2>&1 exit 1 fi xping() { ( xprop -root -len 1 _XKB_RULES_NAMES >/dev/null 2>&1 & pid=$! ( sleep 10 ; kill $pid ) & wait $pid ) 2>/dev/null } if ! xping; then if [ "$debug" = true ]; then echo "X server not responding; checking again." >&2 fi if ! xping; then # try one more time, just in case echo "X server not responding: restarting X." >&2 saver=`ls -l /proc/*/exe 2>/dev/null | grep /lib/xscreensaver/ | \ sed 's@.*/@@'` if [ "$saver" = "" ]; then saver="(none)"; fi echo "active screen saver: $saver" >&2 # perhaps provide a clue as to why X is not responding #/usr/bin/top -bn1 | head -19 | grep . w | head -1 killall X >/dev/null 2>&1 sleep 2 killall -9 X >/dev/null 2>&1 exit 1 elif [ "$debug" = true ]; then echo "debug: X server is responding (on second try)" >&2 fi elif [ "$debug" = true ]; then echo "debug: X server is responding" >&2 fi critical_app() { p=$1 if [ '' = "`pidof $p`" ]; then # uh oh. wait a bit and check again in case it's still starting up. sleep 5 if [ '' = "`pidof $p`" ]; then echo "$p is not running! killing X." >&2 killall X >/dev/null 2>&1 exit 1 elif [ "$debug" = true ]; then echo "debug: $p is alive (just started?)" >&2 fi elif [ "$debug" = true ]; then echo "debug: $p is alive" >&2 fi } critical_app metacity critical_app gnome-panel critical_app multiload-applet-2 ############################################################################## ## ## Make sure xscreensaver is running. ## ############################################################################## running=`xscreensaver-command -time 2>&-` # if it's not running, maybe we're racing with it, and it just hasn't # started yet. Wait a few seconds and check again. # if [ -z "$running" ]; then sleep 5 running=`xscreensaver-command -time 2>&-` fi # ok, it's really not running. launch it, and exit (because we know # the saver isn't active right now: it just started.) # if [ -z "$running" ]; then if [ "$debug" = true ]; then echo "xscreensaver died! re-launching..." >&2 fi su guest -s /bin/sh -c "xscreensaver >/dev/null 2>&1 &" exit 0 fi ############################################################################## ## ## If Firefox is running, but wedged, kill it. ## ############################################################################## killed_firefox=false reset_firefox_prefs() { if [ "$debug" = true ]; then echo "debug: resetting firefox prefs..." >&2 fi foxslt=`sed -n 's/^Path=\(.*\)$/\1/p' \ /home/guest-ro/.mozilla/firefox/profiles.ini` # fuck locks rm -f /home/guest/.mozilla/firefox/$foxslt/lock cp -p /home/guest-ro/.mozilla/firefox/$foxslt/*.{js,rdf} \ /home/guest/.mozilla/firefox/$foxslt/ } kill_firefox() { if [ "$debug" = true ]; then echo "debug: killing firefox..." >&2 fi killall firefox-bin >/dev/null 2>&1 sleep 3 killall -9 firefox-bin >/dev/null 2>&1 reset_firefox_prefs killed_firefox=true } if [ '' != "`pidof firefox-bin`" ]; then if ( mozilla-ping --timeout 10 ); then if [ "$debug" = true ]; then echo "debug: firefox is alive" >&2 fi else if [ "$debug" = true ]; then echo "debug: firefox is wedged..." >&2 fi kill_firefox fi fi ############################################################################## ## ## If the screen is blanked, reset things. ## ############################################################################## if ( echo "$running" | grep -q ' blanked ' ); then # the screen is blank. # turn on host-based access control: /usr/bin/X11/xhost - >/dev/null 2>&1 # allow access from server (telomere aka kiosk) /usr/bin/X11/xhost kiosk >/dev/null 2>&1 # if any hosts have been explicitly allowed, disallow them: hosts=`xhost 2>/dev/null | sed -n -e 's/^INET:/-/p' | sed 's/^-\(kiosk[^.]*\|localhost\)\(\..*\|\)$//'` if [ ! -z "$hosts" ]; then xhost $hosts >/dev/null 2>&1 fi # Make sure the X console is selected. # Usually this is VT 7, but sometimes (why?) it's VT 8. # xlog=/var/log/XFree86.0.log vt=`sed -n 's/^.* using VT number \([0-9][0-9]*\).*$/\1/p' $xlog` chvt ${vt:-7} if [ "$hourly" = true -a "$killed_firefox" = false ]; then # echo "hourly reset: restarting firefox." >&2 kill_firefox else # reset the firefox prefs constantly. reset_firefox_prefs fi if [ "$hourly" = true ]; then # once an hour, reset the background picture # use xsri because it doesn't use vroot.h # (for some reason xsri now gets an X error when xscreensaver # is running, sigh...) /usr/bin/xsri /usr/local/etc/images/kioskbg.jpg >/dev/null 2>&1 fi # now kill off any unknown processes. if [ "$immunity" = false ]; then kiosk-ps-reaper -v fi fi ############################################################################## ## ## If Firefox is not running, launch it. ## (It takes so long to start up that people get confused...) ## ############################################################################## if [ '' = "`pidof firefox-bin`" ]; then # if [ "$killed_firefox" = false ]; then # echo "firefox is dead: launching it." >&2 # fi if [ "$debug" = true ]; then echo "launching firefox..." >&2 fi su guest -c "exec firefox" >/dev/null 2>&1 & fi ) 2>&1 | log_lines exit 0