1 | #!/bin/sh |
---|
2 | # |
---|
3 | # $Id: firefox.sh,v 1.2 2006-07-28 21:06:20 rbasch Exp $ |
---|
4 | # Firefox wrapper script for Athena. |
---|
5 | |
---|
6 | moz_progname=firefox |
---|
7 | |
---|
8 | # Profile directory's parent. |
---|
9 | prof_parent=$HOME/.mozilla/firefox |
---|
10 | |
---|
11 | # The following lockers need to be attached to run plugins and helper |
---|
12 | # applications. |
---|
13 | lockers="infoagents acro" |
---|
14 | |
---|
15 | case `uname` in |
---|
16 | SunOS) |
---|
17 | firefox_libdir=/opt/sfw/lib/firefox |
---|
18 | LD_LIBRARY_PATH=/usr/athena/lib |
---|
19 | export LD_LIBRARY_PATH |
---|
20 | |
---|
21 | # On Solaris, use the X server's shared memory transport for better |
---|
22 | # performance. |
---|
23 | if [ -z "$XSUNTRANSPORT" ]; then |
---|
24 | XSUNTRANSPORT=shmem |
---|
25 | XSUNSMESIZE=512 |
---|
26 | export XSUNTRANSPORT XSUNSMESIZE |
---|
27 | fi |
---|
28 | |
---|
29 | java_plugin_dir=/usr/java/jre/plugin/sparc/ns7 |
---|
30 | ;; |
---|
31 | |
---|
32 | Linux) |
---|
33 | firefox_libdir=/usr/lib/firefox |
---|
34 | java_plugin_dir=/usr/java/jdk/jre/plugin/i386/ns7 |
---|
35 | ;; |
---|
36 | esac |
---|
37 | |
---|
38 | # mozilla-xremote-client sends a command to a running Mozilla |
---|
39 | # application using X properties. Its possible return codes are: |
---|
40 | # 0 success |
---|
41 | # 1 failed to connect to the X server |
---|
42 | # 2 no running window found |
---|
43 | # 3 failed to send command |
---|
44 | # 4 usage error |
---|
45 | moz_remote=$firefox_libdir/mozilla-xremote-client |
---|
46 | |
---|
47 | # testlock is used to test whether the profile directory's lock file |
---|
48 | # is actually locked. |
---|
49 | testlock=/usr/athena/bin/testlock |
---|
50 | |
---|
51 | # Set the plugin path. We allow the user to skip loading our |
---|
52 | # standard plugins via the MOZ_PLUGIN_PATH_OVERRIDE variable. |
---|
53 | if [ "${MOZ_PLUGIN_PATH_OVERRIDE+set}" = set ]; then |
---|
54 | MOZ_PLUGIN_PATH="$MOZ_PLUGIN_PATH_OVERRIDE" |
---|
55 | else |
---|
56 | # Append our plugin path to the user's setting (if any). |
---|
57 | # The Java plugin is in the locally-installed JRE package, and its |
---|
58 | # directory is set above; others (besides the default "null" plugin) |
---|
59 | # live in infoagents. |
---|
60 | plugin_path=$java_plugin_dir:/mit/infoagents/arch/@sys/lib/mozilla/plugins |
---|
61 | MOZ_PLUGIN_PATH=${MOZ_PLUGIN_PATH:+"$MOZ_PLUGIN_PATH:"}$plugin_path |
---|
62 | fi |
---|
63 | export MOZ_PLUGIN_PATH |
---|
64 | |
---|
65 | # Get the profile directory path, by parsing the profiles.ini file. |
---|
66 | get_profdir () { |
---|
67 | case `uname` in |
---|
68 | SunOS) |
---|
69 | awk=nawk |
---|
70 | ;; |
---|
71 | *) |
---|
72 | awk=awk |
---|
73 | ;; |
---|
74 | esac |
---|
75 | |
---|
76 | inifile="$prof_parent/profiles.ini" |
---|
77 | if [ ! -s "$inifile" ]; then |
---|
78 | return 1 |
---|
79 | fi |
---|
80 | $awk -F= -v parent="$prof_parent" ' |
---|
81 | BEGIN { |
---|
82 | nprofiles = 0; |
---|
83 | use_default = 1; |
---|
84 | } |
---|
85 | |
---|
86 | $1 ~ /^\[.*\]$/ { |
---|
87 | section = substr($1, 2, length($1) - 2); |
---|
88 | if (section ~ /^Profile[0-9]*$/) { |
---|
89 | id = section; |
---|
90 | nprofiles++; |
---|
91 | } |
---|
92 | } |
---|
93 | $1 == "StartWithLastProfile" { |
---|
94 | if (section == "General") |
---|
95 | use_default = int($2); |
---|
96 | } |
---|
97 | $1 == "Name" { a[id, "name"] = $2; } |
---|
98 | $1 == "IsRelative" { a[id, "isrelative"] = $2; } |
---|
99 | $1 == "Path" { a[id, "path"] = $2; } |
---|
100 | $1 == "Default" { a[id, "default"] = $2; } |
---|
101 | |
---|
102 | END { |
---|
103 | count = 0; |
---|
104 | default = ""; |
---|
105 | for (i = 0; i < nprofiles; i++) { |
---|
106 | id = "Profile" i; |
---|
107 | if (a[id, "name"] != "" && a[id, "isrelative"] != "" && |
---|
108 | a[id, "path"] != "") { |
---|
109 | count++; |
---|
110 | if (int(a[id, "default"]) != 0) |
---|
111 | default = id; |
---|
112 | } |
---|
113 | } |
---|
114 | if (use_default != 0 && default != "") |
---|
115 | id = default; |
---|
116 | else if (nprofiles == 1 && count == 1) |
---|
117 | id = "Profile0"; |
---|
118 | else |
---|
119 | id = ""; |
---|
120 | if (id != "") { |
---|
121 | if (int(a[id, "isrelative"]) == 0) |
---|
122 | print a[id, "path"]; |
---|
123 | else |
---|
124 | print parent "/" a[id, "path"]; |
---|
125 | } |
---|
126 | }' $inifile |
---|
127 | } |
---|
128 | |
---|
129 | # Prompt the user on how to deal with an existing lock file when no |
---|
130 | # running Firefox window can be found, and take action accordingly. |
---|
131 | # Parameter 1 is the profile directory path. |
---|
132 | # If the function returns, the lock file will have been removed per the |
---|
133 | # user's choice, and the caller should continue. Otherwise, the |
---|
134 | # process will exit. |
---|
135 | dispose_lock () { |
---|
136 | lockfile="$1/.parentlock" |
---|
137 | locklink="$1/lock" |
---|
138 | # Extract the IP address and PID from the contents of the symlink. |
---|
139 | eval `ls -l $locklink | awk '{ |
---|
140 | if (split($NF, a, ":") == 2) |
---|
141 | printf("lock_ip=%s ; lock_pid=%d\n", a[1], int(a[2])); }'` |
---|
142 | |
---|
143 | # If we cannot recognize the link contents, just continue. |
---|
144 | if [ -z "$lock_ip" ]; then |
---|
145 | return 0 |
---|
146 | fi |
---|
147 | |
---|
148 | # Get the host name of the lock holder. |
---|
149 | lock_host=`host $lock_ip | \ |
---|
150 | sed -n -e 's/^.*domain name pointer \(.*\)$/\1/p' | \ |
---|
151 | sed -e 's/\.*$//' | tr '[A-Z]' '[a-z]'` |
---|
152 | if [ -z "$lock_host" ]; then |
---|
153 | lock_host=$lock_ip |
---|
154 | fi |
---|
155 | |
---|
156 | dialog_text=" |
---|
157 | Your Firefox profile directory is locked by process $lock_pid |
---|
158 | on $lock_host. |
---|
159 | " |
---|
160 | |
---|
161 | dialog_text="$dialog_text |
---|
162 | If you select \"OK\" to continue, the profile will be forcibly |
---|
163 | unlocked; if the process holding the lock is still running, |
---|
164 | you risk corrupting your profile. |
---|
165 | |
---|
166 | If you are not certain whether your Firefox process is still |
---|
167 | running on $lock_host, select \"Cancel\" to exit |
---|
168 | without starting Firefox. |
---|
169 | " |
---|
170 | |
---|
171 | zenity --title "Firefox profile locked" --warning --text "$dialog_text" |
---|
172 | |
---|
173 | case $? in |
---|
174 | 0) |
---|
175 | rm -f "$lockfile" |
---|
176 | ;; |
---|
177 | *) |
---|
178 | exit 1 |
---|
179 | ;; |
---|
180 | esac |
---|
181 | } |
---|
182 | |
---|
183 | # Give a warning if we're running on a dialup machine. |
---|
184 | if [ -x /etc/athena/dialuptype ]; then |
---|
185 | cat << \EOF |
---|
186 | |
---|
187 | *** PLEASE DO NOT RUN FIREFOX ON THE DIALUPS! *** |
---|
188 | |
---|
189 | Firefox is a very large and resource-intensive program, and it is |
---|
190 | not appropriate to run it on a dialup machine. |
---|
191 | |
---|
192 | Please run Firefox on your local machine rather than on a dialup. |
---|
193 | |
---|
194 | Thank you. |
---|
195 | |
---|
196 | EOF |
---|
197 | fi |
---|
198 | |
---|
199 | # Attach needed lockers. |
---|
200 | for locker in $lockers ; do |
---|
201 | /bin/athena/attach -h -n -q $locker |
---|
202 | done |
---|
203 | |
---|
204 | # Configure fontconfig to use fonts for MathML. |
---|
205 | if [ -z "$FONTCONFIG_FILE" ]; then |
---|
206 | FONTCONFIG_FILE=/mit/infoagents/share/fonts/fonts.conf |
---|
207 | export FONTCONFIG_FILE |
---|
208 | fi |
---|
209 | |
---|
210 | # If this is the first time the user has run firefox, create |
---|
211 | # the top-level profile directory now, so that we can set |
---|
212 | # the ACL appropriately. |
---|
213 | if [ ! -d "$prof_parent" ]; then |
---|
214 | mkdir -p "$prof_parent" |
---|
215 | /bin/athena/fs setacl "$prof_parent" system:anyuser none system:authuser none |
---|
216 | fi |
---|
217 | |
---|
218 | # If the user specified any option, skip the check for a running |
---|
219 | # Firefox, and invoke the program now. |
---|
220 | case "$1" in |
---|
221 | -*) |
---|
222 | exec $firefox_libdir/firefox "$@" |
---|
223 | ;; |
---|
224 | esac |
---|
225 | |
---|
226 | if [ "${MOZ_NO_REMOTE+set}" = "set" ]; then |
---|
227 | found_running=false |
---|
228 | else |
---|
229 | # See if there is a running instance of Firefox. |
---|
230 | user="${LOGNAME:+-u $LOGNAME}" |
---|
231 | $firefox_libdir/run-mozilla.sh $moz_remote $user -a "$moz_progname" \ |
---|
232 | "ping()" >/dev/null 2>&1 |
---|
233 | case $? in |
---|
234 | 0) |
---|
235 | found_running=true |
---|
236 | ;; |
---|
237 | 2) |
---|
238 | # This is the expected status when there is no running firefox window. |
---|
239 | # Clear the error condition. |
---|
240 | remote_error= |
---|
241 | found_running=false |
---|
242 | ;; |
---|
243 | *) |
---|
244 | # An unexpected error occurred. |
---|
245 | found_running=error |
---|
246 | ;; |
---|
247 | esac |
---|
248 | fi |
---|
249 | |
---|
250 | # If Firefox is not running, check for a (possibly stale) lock on |
---|
251 | # the profile directory. |
---|
252 | if [ $found_running != true ]; then |
---|
253 | profdir=`get_profdir` |
---|
254 | if [ -n "$profdir" ]; then |
---|
255 | # firefox now uses fcntl()-style locking on .parentlock, but an |
---|
256 | # apparent openafs bug leaves the lock set after the program exits. |
---|
257 | # Fortunately, it still maintains the "lock" symlink, so use its |
---|
258 | # presence to help detect a stale lock. |
---|
259 | lockfile="$profdir/.parentlock" |
---|
260 | if [ -h "$profdir/lock" ]; then |
---|
261 | # The symlink exists. See if the profile is actually locked. |
---|
262 | lock_pid=`$testlock "$lockfile" 2>/dev/null` |
---|
263 | if [ $? -eq 2 ]; then |
---|
264 | # The file is locked. If the lock is held by a process on |
---|
265 | # this machine, the locker pid will be non-0, so check if |
---|
266 | # the process is still running. |
---|
267 | if [ "$lock_pid" -eq 0 ]; then |
---|
268 | # The profile is locked by a process running on another |
---|
269 | # machine. Ask the user how to deal with it. |
---|
270 | dispose_lock "$profdir" |
---|
271 | else |
---|
272 | if kill -0 $lock_pid 2>/dev/null ; then |
---|
273 | # Lock is held by a running process. |
---|
274 | : |
---|
275 | else |
---|
276 | # The process holding the lock is no longer running. |
---|
277 | # Assume this is the result of the openafs bug noted |
---|
278 | # above; nuke the lock and continue. |
---|
279 | rm -f "$lockfile" |
---|
280 | fi |
---|
281 | fi |
---|
282 | fi |
---|
283 | else |
---|
284 | # The symlink is gone, so just nuke the lock file, to work around |
---|
285 | # the aforementioned openafs bug. |
---|
286 | rm -f "$profdir/.parentlock" |
---|
287 | fi |
---|
288 | fi |
---|
289 | fi |
---|
290 | |
---|
291 | exec $firefox_libdir/firefox "$@" |
---|