pndnotifyd: fix some crashes
[pandora-libraries.git] / testdata / scripts / pnd_run.sh
index c3562ea..0ea23e8 100755 (executable)
@@ -100,6 +100,12 @@ PND_WaitFor() {
        PND_EndTask
 }
 
+PND_WaitForExit() {
+       # this used to wait for $1 ($EXENAME), but that doesn't work well
+       # when multiple PNDs run as too many PNDs have run.sh as $EXENAME
+       wait
+}
+
 PND_Exec() {
        local CMD="$*"
        {
@@ -135,7 +141,7 @@ PND_Exec() {
 showHelp() {
        cat <<endHELP
 Usage:
-  pnd_run.sh -p file.pnd -e cmd [-a args] [-b pndid] [-s path] [-c speed] [-d [path]] [-x] [-m] [-u]
+  pnd_run.sh -p file.pnd -e cmd [-a args] [-b pndid] [-s path] [-c speed] [-d [path]] [-x] [-m] [-u] [-- more_args]
     -p file.pnd        : Specify the pnd file to execute
     -e cmd     : Command to run
     -a args    : Arguments to the command
@@ -146,6 +152,9 @@ Usage:
     -x         : Stop X before starting the apps
     -m         : Only mount the pnd, dont run it (-e become optional)
     -u         : Only umount the pnd, dont run it (-e become optional)
+
+  If '--' is specified, all subsequent arguments are passed through to the command
+  (useful if you want to pass quotes and weird chars to the command)
 endHELP
 }
 
@@ -314,8 +323,8 @@ mountPnd() {
                        #detect fs
                        case $PND_FSTYPE in
                        ISO)
-                               /sbin/losetup $FREELOOP "$PND" #attach the pnd to the loop device
-                               mntline="mount" #setup the mountline for later
+                               /sbin/losetup -r $FREELOOP "$PND" #attach the pnd to the loop device
+                               mntline="mount -o ro" #setup the mountline for later
                                mntdev="${FREELOOP}"
                                ;;
                        directory)
@@ -325,8 +334,8 @@ mountPnd() {
                                mntdev="${PND}"
                                ;;
                        Squashfs)
-                               /sbin/losetup $FREELOOP "$PND" #attach the pnd to the loop device
-                               mntline="mount -t squashfs"
+                               /sbin/losetup -r $FREELOOP "$PND" #attach the pnd to the loop device
+                               mntline="mount -t squashfs -o ro"
                                mntdev="${FREELOOP}"
                                ;;
                        *)
@@ -357,10 +366,10 @@ mountPnd() {
 
        # For backward compatibility
        if [[ "$MOUNT_TARGET" != "$PND_MOUNT_DIR" ]];then
-               if [ -d "$PND_MOUNT_DIR/$PND_NAME" ];then
-                       rmdir "$PND_MOUNT_DIR/$PND_NAME"
-               else
+               if [ -h "$PND_MOUNT_DIR/$PND_NAME" ];then
                        rm "$PND_MOUNT_DIR/$PND_NAME"
+               else
+                       rmdir "$PND_MOUNT_DIR/$PND_NAME"
                fi
                if [ ! -e "$PND_MOUNT_DIR/$PND_NAME" ];then
                        ln -s "$MOUNT_TARGET/$PND_NAME" "$PND_MOUNT_DIR/$PND_NAME"
@@ -522,12 +531,18 @@ runApp() {
                cd "$STARTDIR";                 # cd to folder specified by the optional arg -s
        fi
 
+       echo "$EXENAME" | grep -q "^\.\/" && DOTSLASH="true"
+
+       # now set up the env for the PND, don't run any tools after this point
+       # because PATH and LD_LIBRARY_PATH may be different and incompatible
+
        if [ -d "$UNION_MOUNT_DIR/$PND_NAME/lib" ];then
                export LD_LIBRARY_PATH="$UNION_MOUNT_DIR/$PND_NAME/lib:${LD_LIBRARY_PATH:-"/usr/lib:/lib"}"
        else
                export LD_LIBRARY_PATH="$UNION_MOUNT_DIR/$PND_NAME:${LD_LIBRARY_PATH:-"/usr/lib:/lib"}"
        fi
 
+       REAL_PATH="$PATH"
        if [ -d "$UNION_MOUNT_DIR/$PND_NAME/bin" ];then
                export PATH="$UNION_MOUNT_DIR/$PND_NAME/bin:${PATH:-"/usr/bin:/bin:/usr/local/bin"}"
        fi
@@ -536,21 +551,42 @@ runApp() {
                export XDG_DATA_DIRS="$UNION_MOUNT_DIR/$PND_NAME/share:$XDG_DATA_DIRS:/usr/share"
        fi
 
-       export XDG_CONFIG_HOME="$UNION_MOUNT_DIR/$PND_NAME"
+       export REAL_HOME="$HOME"
+       export HOME="$UNION_MOUNT_DIR/$PND_NAME"
+       export XDG_CONFIG_HOME="$HOME"
+       export XDG_DATA_HOME="$HOME"
+       export XDG_CACHE_HOME="$HOME"
 
-       if echo "$EXENAME"|grep -q ^\.\/;then
-               "$EXENAME" $ARGUMENTS
+       if [ -e "${APPDATADIR}/PND_pre_script.sh" ]; then
+               echo "Starting user configured pre-script ${APPDATADIR}/PND_pre_script.sh"
+               . ${APPDATADIR}/PND_pre_script.sh # Sourcing so it can shared vars with post-script ;)
+               echo "${APPDATADIR}/PND_pre_script.sh is done"
+       fi
+
+       if [ "$DOTSLASH" = "true" ]; then
+               "$EXENAME" $ARGUMENTS "$@"
        else
-               "./$EXENAME" $ARGUMENTS
+               "./$EXENAME" $ARGUMENTS "$@"
        fi
        RC=$?
 
        #the app could have exited now, OR it went into bg, we still need to wait in that case till it really quits!
-       PID=$(pidof -o %PPID -x \"$EXENAME\")   # get pid of app
-       while [ "$PID" ];do                     # wait till we get no pid back for tha app, again a bit ugly, but it works
-               sleep 10s
-               PID=`pidof -o %PPID -x \"$EXENAME\"`
-       done
+       LD_LIBRARY_PATH= PATH="$REAL_PATH" PND_WaitForExit "$EXENAME"
+
+       if [ -e "${APPDATADIR}/PND_post_script.sh" ]; then
+               echo "Starting user configured post-script ${APPDATADIR}/PND_post_script.sh"
+               . ${APPDATADIR}/PND_post_script.sh
+               echo "${APPDATADIR}/PND_post_script.sh is done"
+       fi
+
+       # clean up to avoid any effects of the .pnd on the remaining parts of this script
+       unset LD_LIBRARY_PATH
+       export HOME="$REAL_HOME"
+       unset REAL_HOME
+       export PATH="$REAL_PATH"
+       unset REAL_PATH
+       # ... the rest should not matter
+
        return $RC
 }
 
@@ -582,19 +618,12 @@ main() {
                        PND_EndTask
                fi
                oPWD=$(pwd)
-               if [ -e "${APPDATADIR}/PND_pre_script.sh" ]; then
-                       PND_BeginTask "Starting user configured pre-script"
-                       . ${APPDATADIR}/PND_pre_script.sh # Sourcing so it can shared vars with post-script ;)
-                       PND_EndTask
-               fi
-               PND_BeginTask "Starting the application ($EXENAME $ARGUMENTS)"
-               runApp
+               old_fb0_geometry=$(fbset -fb /dev/fb0 -s | grep geometry | awk '{print $2, $3, $4, $5, $6}')
+
+               PND_BeginTask "Starting the application ( $EXENAME $ARGUMENTS "$@")"
+               runApp "$@"
                PND_EndTask
-               if [ -e "${APPDATADIR}/PND_post_script.sh" ]; then
-                       PND_BeginTask "Starting user configured post-script"
-                       . ${APPDATADIR}/PND_post_script.sh
-                       PND_EndTask
-               fi
+
                cd $oPWD
                if [ $CLOSE_X ]; then
                        PND_BeginTask "Restarting X"
@@ -606,6 +635,26 @@ main() {
                        PND_resetCPUSpeed
                        PND_EndTask
                fi
+               PND_BeginTask "Restoring the frame buffer status"
+               ofbset -fb /dev/fb0 -pos 0 0
+               fbset -fb /dev/fb0 -g $old_fb0_geometry
+               old_res=$(echo $old_fb0_geometry | awk '{print $1, $2}')
+               ofbset -fb /dev/fb0 -size $old_res -en 1
+               if ! lsof /dev/fb1 > /dev/null; then
+                       ofbset -fb /dev/fb1 -mem 0 -size 0 0 -en 0
+               fi
+               PND_EndTask
+
+               running_pnd_count=$(ps ax | grep "pnd_run.s[h]" | wc -l)
+               # restore settings if we are the last exiting pnd
+               # compare with 2 because one is used by subshell where "ps ax | ..." runs
+               if [ "$running_pnd_count" -eq "2" ]; then
+                       PND_BeginTask "Restoring nub mode"
+                       sed -n '1p' /etc/pandora/conf/nubs.state > /proc/pandora/nub0/mode
+                       sed -n '7p' /etc/pandora/conf/nubs.state > /proc/pandora/nub1/mode
+                       PND_EndTask
+               fi
+
                PND_BeginTask "uMount the PND"
                umountUnion
                PND_EndTask
@@ -616,8 +665,18 @@ main() {
 ######################################################################################
 ####   Parsing the arguments :
 ##
+if [ "$#" -lt 1 ]; then
+       showHelp
+       exit 1
+fi
+
 ACTION=run
 while [ "$#" -gt 0 ];do
+       if [ "$1" == "--" ]; then
+               shift
+               break
+       fi
+
        if [ "$#" -gt 1 ] && ( [[ "$(echo $2|cut -c 1)" != "-" ]] || [[ "$1" = "-a" ]] );then
                case "$1" in
                 -p) PND="$2";;
@@ -629,7 +688,6 @@ while [ "$#" -gt 0 ];do
                 -d) APPDATASET=1;APPDATADIR="$2";;
                 -a) ARGUMENTS="$2";;
                 *)     echo "ERROR while parsing arguments: \"$1 $2\" is not a valid argument"; 
-                       echo "Arguments were : $PND_ARGS"
                        showHelp;
                        exit 1 ;;
                esac
@@ -641,7 +699,6 @@ while [ "$#" -gt 0 ];do
                 -x) CLOSE_X=1;;
                 -d) APPDATASET=1;;
                 *)     echo "ERROR while parsing arguments: \"$1\" is not a valid argument"; 
-                       echo "Arguments were : $PND_ARGS"
                        showHelp;
                        exit 1 ;;
                esac
@@ -650,6 +707,12 @@ while [ "$#" -gt 0 ];do
        fi
 done
 
+if test -z "$PND"; then
+       echo "ERROR: pnd file provided (-p)"
+       showHelp
+       exit 1
+fi
+
 # getting the real full path to the file
 PND="$(readlink -f $PND)"
 
@@ -670,7 +733,7 @@ if [ ! -e "$PND" ]; then #check if theres a pnd suplied, need to clean that up a
 fi
 
 if [ ! "$EXENAME" ] && [[ "$ACTION" = "run" ]]; then
-       echo "ERROR: no executable name provided!"
+       echo "ERROR: no executable name provided! (-e)"
        showHelp
        exit 1
 fi
@@ -684,7 +747,7 @@ elif [ ! -d "$MOUNTPOINT" ]; then
 fi
 [ ! -z $APPDATASET ] || [ -z ${MOUNTPOINT} ] && APPDATADIR=${APPDATADIR:-$(dirname $PND)/$PND_NAME}
 APPDATADIR=${APPDATADIR:-${MOUNTPOINT}/pandora/appdata/${PND_NAME}}
-APPDD_FSTYPE=$(mount|awk '$3=="'${MOUNTPOINT}'"{print $5}')
+APPDD_FSTYPE=$(cat /proc/mounts | awk '$2=="'${MOUNTPOINT}'"{print $3}' | head -1)
 CPUSPEED_FILE=${MOUNTPOINT}/pandora/appdata/${PND_NAME}/cpuspeed
 if [ -f "$CPUSPEED_FILE" ]; then
        PND_CPUSPEED=$(cat "$CPUSPEED_FILE")
@@ -696,10 +759,10 @@ if [[ "$ACTION" == "run" ]];then
        PND_Start
        {
        if [ $CLOSE_X ]; then
-               main 2>&1 & 
+               main "$@" 2>&1 & 
                disown
        else
-               main 2>&1
+               main "$@" 2>&1
        fi
        }>>"$PND_LOG"
        PND_Stop