715255664688c08d660972d800801f8126410ca5
[pandora-libraries.git] / testdata / scripts / pnd_run.sh
1 #!/bin/bash
2  
3 #Usage: pnd_run.sh -p your.pnd -e executeable [-a "(arguments)"] [ -s "cd to folder inside pnd"] [-u (skip union)] [-b override BASENAME (name of mountpoint/pandora/appdata)] [-x close x before launching(script needs to be started with nohup for this to work]
4 # -n to skip union mount, should probably be removed before release
5 # -s startdir
6 # arguments can be inside -e, -a is optional
7  
8 #/etc/sudoers needs to be adjusted if you touch any of the sudo lines in the wrong place.
9  
10 # look at the comments in the nox part, adjust 
11  
12 #use "lsof /usr/lib/libX11.so.6 | awk '{print $1}'| sort | uniq > whitelist" with nothing running to generate the whitelist
13  
14 #launch the script with nohup for -x to work!
15  
16 #todo
17 #make sure to only use free loop devices!
18 #cleanup
19  
20 # parse arguments
21 TEMP=`getopt -o p:e:a:b:s:m::u::n::x:: -- "$@"`
22  
23 if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
24  
25 # Note the quotes around `$TEMP': they are essential!
26 eval set -- "$TEMP"
27  
28 while true ; do
29         case "$1" in
30                 -p) echo "pnd set to \`$2'" ;PND=$2;shift 2;;
31                 -e) echo "exec set to \`$2'" ;EXENAME=$2;shift 2 ;;
32                 -n) echo "n set, no union pls!";NOUNION=1;shift 2;;
33                 -b) echo "BASENAME set to $2";BASENAME=$2;shift 2;;
34                 -s) echo "startdir set to $2";STARTDIR=$2;shift 2;;
35                 -m) echo "mount";mount=1;shift 2;;
36                 -u) echo "umount";umount=1;shift 2;;
37                 -x) echo "no x";nox=1;shift 2;;
38                 -a) 
39                         case "$2" in
40                                 "") echo "no arguments"; shift 2 ;;
41                                 *)  echo "args set to \`$2'" ;ARGUMENTS=$2;shift 2 ;;
42                         esac ;;
43                 --) shift ; break ;;
44                 *) echo "Error while parsing arguments!" ; exit 1 ;;
45         esac
46 done
47  
48 if [ ! $PND ]; then
49         echo "Usage: pnd_run.sh -p your.pnd -e executeable [-a \"(arguments)\"] [ -s \"cd to folder inside pnd\"] [-u (skip union)] [-b override BASENAME (name of mountpoint/pandora/appdata)] [-x close x before launching(script needs to be started with nohup for this to work]"
50         exit 1
51 fi
52 if [ $nox ]; then
53         applist=$(lsof /usr/lib/libX11.so.6 | awk '{print $1}'| sort | uniq)
54         whitelist=$(cat ~/pndtest/whitelist) #adjust this to a fixed whitelist, maybe in the config dir
55         filteredlist=$(echo -e "$applist\n\n$whitelist\n\n$whitelist" | sort | uniq -u) #whitelist appended two times so those items are always removed
56         if [ ${#filteredlist} -ge 1 ]; then
57                 message=$(echo -e "The following applications are still running, are you sure you want to close x? \n$filteredlist")
58                 echo -e “ae[34me[30m”
59                 xmessage -center "$message", -buttons yes,no
60                 if [ $? = 102 ]; then
61                 exit 1
62                 fi
63                 sudo /etc/init.d/gdm stop
64                 sleep 5s
65         else
66                 echo -e “ae[34me[30m”
67                 xmessage -center "killing x, nothing of value will be lost", -buttons ok,cancel
68                 if [ $? = 102 ]; then
69                 exit 1
70                 fi
71                 # close x now, do we want to use gdm stop or just kill x?
72                 sudo /etc/init.d/gdm stop
73                 sleep 5s
74         fi
75 fi
76  
77 #vars
78 DFS=$(file -b $PND | awk '{ print $1 }') #is -p a zip/iso or folder?
79 MOUNTPOINT=$(df $PND | sed -ne 's/.*\% \(\S*\)/\1/p' | tail -n1)
80 echo "mountpoint: $MOUNTPOINT"
81 #MOUNTPOINT=$(df -h $PND | grep -E '[1-9]%' | awk '{ print $6  }') #find out which mountpoint the pnd/folder is on, there probably is a better way to do this
82  
83  
84 #BASENAME really should be something sensible and somewhat unique
85 #if -b is set use that as basename, else generate it from PND
86 #get basename (strip extension if file) for union mountpoints etc, maybe  this should be changed to something specified inside the xml
87 #this should probably be changed to .... something more sensible
88 if [ ! $BASENAME ]; then BASENAME=$(basename "$PND" | cut -d'.' -f1) ; fi
89  
90  
91  
92 oCWD=$(pwd)
93  
94 #detect fs
95 if [ $DFS = ISO ]; then
96  
97         usedminor=$( ls -l /dev/loop* | awk '{print $6}')
98         freeminor=$( echo -e "$(seq 0 64)\n$usedminor" | sort -rn | uniq -u | tail -n1)
99         sudo mknod -m777 /dev/loop$freeminor b 7 $freeminor
100         sudo losetup /dev/loop$freeminor $PND
101  
102         mntline="sudo mount /dev/loop$freeminor /mnt/pnd/$BASENAME/"
103 #       mntline="sudo mount -o loop,mode=777 $PND /mnt/pnd/$BASENAME"
104         echo "Filetype is $DFS"
105 elif [ $DFS = Zip ]; then
106         mntline="fuse-zip $PND /mnt/pnd/$BASENAME -o ro,fmask=000" #TOTALLY untested right now
107         echo "Filetype is $DFS"
108 elif [ $DFS = directory ]; then
109         mntline="sudo mount --bind -o ro $PND /mnt/pnd/$BASENAME"
110 #we bind the folder, now it can be treated in a unified way ATENTION: -o ro doesnt work for --bind at least on 25, on 26 its possible using remount, may have changed on 27
111         echo "Filetype is $DFS"
112 else
113         echo "error"
114         exit 1;
115 fi
116  
117 #create mountpoints, check if they exist already first to avoid annoying error messages
118 if [ ! -d /mnt/pnd/$BASENAME ]; then sudo mkdir -p /mnt/pnd/$BASENAME ; fi
119 if [ ! -d $MOUNTPOINT/pandora/appdata/$BASENAME ]; then sudo mkdir -p $MOUNTPOINT/pandora/appdata/$BASENAME; fi
120 if [ ! -d /mnt/utmp/$BASENAME ]; then sudo mkdir -p /mnt/utmp/$BASENAME; fi 
121  
122 #mount
123  
124 if [ ! $NOUNION ] && [ ! $umount ]; then
125         #is the union already mounted? if not mount evrything, else launch the stuff
126         mount | grep "on /mnt/utmp/$BASENAME type" # > /dev/null
127         if [ ! $? -eq 0 ]; then 
128                 echo "$mntline"
129                 $mntline #mount the pnd/folder
130                 echo "mounting union!"
131                 sudo mount -t aufs -o exec,dirs\=$MOUNTPOINT/pandora/appdata/$BASENAME=rw+nolwh:/mnt/pnd/$BASENAME=rr none /mnt/utmp/$BASENAME # put union on top
132  
133         else
134                 echo "Union already mounted"
135         fi
136  
137         if [ $mount ]; then echo "mounted /mnt/utmp/$BASENAME"; exit 1; fi;
138  
139         #start app
140         cd /mnt/utmp/$BASENAME
141         if [ $STARTDIR ]; then cd $STARTDIR; fi #cd to folder specified by the optional arg -s
142         #echo "/lib/ld-linux.so.2 --library-path /mnt/utmp/$BASENAME/ $EXENAME $ARGUMENTS"
143         #/lib/ld-linux.so.2 --library-path /mnt/utmp/$BASENAME/ $EXENAME $ARGUMENTS
144         LD_LIBRARY_PATH=/mnt/utmp/$BASENAME ./$EXENAME $ARGUMENTS
145         #the app could have exited now, OR it went into bg, we still need to wait in that case till it really quits!
146         PID=`pidof -o %PPID -x $EXENAME`
147         while [ "$PID" ]
148         do
149         sleep 10s
150         PID=`pidof -o %PPID -x $EXENAME`
151         done
152         echo end
153  
154         #app exited
155         cd $oCWD #cd out of the mountpoint so we can umount, doesnt really matter to where...
156  
157 elif [ ! $umount ]; then
158         $mntline
159         if [ $mount ]; then echo "mounted /mnt/pnd/$BASENAME"; exit 1; fi;
160         cd /mnt/pnd/$BASENAME
161         if [ $STARTDIR ]; then cd $STARTDIR; fi
162         echo $(pwd)
163         #/lib/ld-linux.so.2 --library-path /mnt/pnd/$BASENAME/ $EXENAME $ARGUMENTS      
164         ./$EXENAME $ARGUMENTS 
165         LD_LIBRARY_PATH=/mnt/pnd/$BASENAME ./$EXENAME $ARGUMENTS
166         #the app could have exited now, OR it went into bg, we still need to wait in that case till it really quits!
167         PID=`pidof -o %PPID -x $EXENAME`
168         while [ $PID ]
169         do
170         sleep 10s
171         PID=`pidof -o %PPID -x $EXENAME`
172         done
173         echo end
174  
175         cd $oCWD
176 else
177 echo "-u set, nothing to do here"
178 fi
179  
180  
181 #clean up
182 if [ ! $NOUNION ] ; then sudo umount /mnt/utmp/$BASENAME; fi #umount union if -u wasnt set
183 if [ $NOUNION ] ; then sudo umount /mnt/pnd/$BASENAME; fi #umount iso if -u WAS set
184 if [ $? -eq 0 ]; then # check if the umount was successfull, if it wasnt it would mean that theres still something running so we skip this stuff, this WILL lead to clutter if it happens, so we should make damn sure it never happens
185         if [ ! $NOUNION ] ; then
186                 sudo umount /mnt/pnd/$BASENAME
187                 sudo rmdir $MOUNTPOINT/pandora/appdata/$BASENAME/.wh..wh.plnk
188                 sudo rmdir $MOUNTPOINT/pandora/appdata/$BASENAME/.wh..wh..tmp 
189                 sudo rmdir $MOUNTPOINT/pandora/appdata/$BASENAME/
190                 sudo rmdir /mnt/utmp/$BASENAME;
191         fi
192         if [ $DFS = ISO ]; then
193                 sudo losetup -d /dev/loop$freeminor
194                 sudo rm /dev/loop$freeminor
195         fi
196         sudo rmdir /mnt/pnd/$BASENAME 
197 fi
198 if [ $nox ]; then
199 echo "starting x in 5s"
200 sleep 5
201 sudo /etc/init.d/gdm start
202 fi