source: trunk/debathena/debathena/firefox-wrapper/firefox.sh @ 23058

Revision 23058, 7.9 KB checked in by rbasch, 16 years ago (diff)
In firefox-wrapper: * Use firefox's -remote option to ping a running instance, as the availability and location of mozilla-xremote-client varies between distributions and versions. * Use a zenity question dialog instead of a warning, since in later zenity versions the warning does not include a Cancel button. * Redirect fs output to /dev/null. * Add dependencies on zenity, debathena-attach. * Remove the dependency on lsb-release.
RevLine 
[22492]1#!/bin/sh
2#
[22649]3# $Id: firefox.sh,v 1.8 2007-06-22 15:13:03 rbasch Exp $
[22492]4# Firefox wrapper script for Athena.
5
6moz_progname=firefox
7
8# Profile directory's parent.
9prof_parent=$HOME/.mozilla/firefox
10
11# The following lockers need to be attached to run plugins and helper
12# applications.
13lockers="infoagents acro"
14
15# testlock is used to test whether the profile directory's lock file
16# is actually locked.
[22784]17testlock=/usr/bin/testlock
[22492]18
19# Set the plugin path.  We allow the user to skip loading our
20# standard plugins via the MOZ_PLUGIN_PATH_OVERRIDE variable.
21if [ "${MOZ_PLUGIN_PATH_OVERRIDE+set}" = set ]; then
22  MOZ_PLUGIN_PATH="$MOZ_PLUGIN_PATH_OVERRIDE"
23else
24  # Append our plugin path to the user's setting (if any).
25  MOZ_PLUGIN_PATH=${MOZ_PLUGIN_PATH:+"$MOZ_PLUGIN_PATH:"}$plugin_path
26fi
27export MOZ_PLUGIN_PATH
28
29# Get the profile directory path, by parsing the profiles.ini file.
30get_profdir () {
31  inifile="$prof_parent/profiles.ini"
32  if [ ! -s "$inifile" ]; then
33    return 1
34  fi
[22784]35  awk -F= -v parent="$prof_parent" '
[22492]36    BEGIN {
37      nprofiles = 0;
38      use_default = 1;
39    }
40 
41    $1 ~ /^\[.*\]$/ {
42      section = substr($1, 2, length($1) - 2);
43      if (section ~ /^Profile[0-9]*$/) {
44        id = section;
45        nprofiles++;
46      }
47    }
48    $1 == "StartWithLastProfile" {
49      if (section == "General")
50        use_default = int($2);
51    }
52    $1 == "Name"       { a[id, "name"] = $2; }
53    $1 == "IsRelative" { a[id, "isrelative"] = $2; }
54    $1 == "Path"       { a[id, "path"] = $2; }
55    $1 == "Default"    { a[id, "default"] = $2; }
56 
57    END {
58      count = 0;
59      default = "";
60      for (i = 0; i < nprofiles; i++) {
61        id = "Profile" i;
62        if (a[id, "name"] != "" && a[id, "isrelative"] != "" &&
63            a[id, "path"] != "") {
64          count++;
65          if (int(a[id, "default"]) != 0)
66            default = id;
67        }
68      }
69      if (use_default != 0 && default != "")
70        id = default;
71      else if (nprofiles == 1 && count == 1)
72        id = "Profile0";
73      else
74        id = "";
75      if (id != "") {
76        if (int(a[id, "isrelative"]) == 0)
77          print a[id, "path"];
78        else
79          print parent "/" a[id, "path"];
80      }
81    }' $inifile
82}
83
[22527]84# Prompt the user on how to deal with an existing locked profile when
85# no running Firefox window can be found, and take action accordingly.
[22492]86# Parameter 1 is the profile directory path.
[22527]87# If the function returns, the lock file(s) will have been removed per
88# the user's choice, and the caller should continue.  Otherwise, the
[22492]89# process will exit.
90dispose_lock () {
91  lockfile="$1/.parentlock"
92  locklink="$1/lock"
93  # Extract the IP address and PID from the contents of the symlink.
[22527]94  # Also note whether firefox used fnctl() to lock .parentlock,
95  # which is indicated with a leading '+' in the PID.
[22967]96  eval `ls -l "$locklink" | awk '{
[22492]97    if (split($NF, a, ":") == 2)
[22527]98      printf("lock_ip=%s ; lock_pid=%d ; use_fcntl=%d\n",
99              a[1], int(a[2]), (substr(a[2], 1, 1) == "+")); }'`
[22492]100
101  # If we cannot recognize the link contents, just continue.
102  if [ -z "$lock_ip" ]; then
103    return 0
104  fi
105
[22527]106  local=false
107  if [ "$use_fcntl" -ne 0 ]; then
108    # An fcntl()-style lock was acquired; check it.
109    if [ -f "$lockfile" ]; then
110      # testlock tests whether there is a write lock on the file.
111      # If so, it outputs the locker's PID, and exits with status 2.
112      # If the lock is held by a process running on another host, the
113      # PID will be 0.
114      pid=`$testlock "$lockfile" 2>/dev/null`
115      if [ $? -ne 2 ]; then
116        # File is not locked, remove the symlink and continue.
117        rm -f "$locklink"
118        return 0
119      fi
120      # The file is locked.  If the lock is held by a process on
121      # this machine, the locker pid will be non-0.
122      if [ "$pid" -ne 0 ]; then
123        local=true
124      fi
125    fi
126  else
127    # Handle an old-style (symlink) lock.
128    my_host=`hostname`
[22784]129    if [ "$lock_ip" = "`host $my_host | awk '{ print $NF; }'`" ]; then
[22527]130      # Lock is held on this machine.
131      local=true
132    fi
133  fi
134
135  if [ "$local" = true ]; then
136    # The lock is held by a process on this machine; check if it is
137    # still running.
138    if kill -0 $lock_pid 2>/dev/null ; then
139      # Lock is held by a running process.
140      lock_host="this machine"
141    else
142      # Process is no longer running.  Nuke the lock and continue.
143      rm -f "$lockfile" "$locklink"
144      return 0
145    fi
146  else
147    # The lock is held by a process on another machine.  Get its
148    # host name.
149    lock_host=`host $lock_ip | \
[22492]150      sed -n -e 's/^.*domain name pointer \(.*\)$/\1/p' | \
151      sed -e 's/\.*$//' | tr '[A-Z]' '[a-z]'`
[22527]152    if [ -z "$lock_host" ]; then
153      lock_host="$lock_ip"
154    fi
[22492]155  fi
156
157  dialog_text="
158  Your Firefox profile directory is locked by process $lock_pid 
159  on $lock_host. 
160"
161
162  dialog_text="$dialog_text
163  If you select \"OK\" to continue, the profile will be forcibly 
164  unlocked; if the process holding the lock is still running, 
165  you risk corrupting your profile.
166
167  If you are not certain whether your Firefox process is still
168  running on $lock_host, select \"Cancel\" to exit 
169  without starting Firefox. 
170"
171
[23058]172  zenity --title "Firefox profile locked" --question --text "$dialog_text"
[22492]173
174  case $? in
175  0)
[22527]176    rm -f "$lockfile" "$locklink"
[22492]177    ;;
178  *)
179    exit 1
180    ;;
181  esac
182}
183
184# Give a warning if we're running on a dialup machine.
185if [ -x /etc/athena/dialuptype ]; then
186  cat << \EOF
187
188*** PLEASE DO NOT RUN FIREFOX ON THE DIALUPS! ***
189
190Firefox is a very large and resource-intensive program, and it is
191not appropriate to run it on a dialup machine.
192
193Please run Firefox on your local machine rather than on a dialup.
194
195Thank you.
196
197EOF
198fi
199
200# Attach needed lockers.
201for locker in $lockers ; do
[22784]202  /bin/attach -h -n -q $locker
[22492]203done
204
205# Configure fontconfig to use fonts for MathML.
206if [ -z "$FONTCONFIG_FILE" ]; then
207  FONTCONFIG_FILE=/mit/infoagents/share/fonts/fonts.conf
208  export FONTCONFIG_FILE
209fi
210
211# If this is the first time the user has run firefox, create
212# the top-level profile directory now, so that we can set
213# the ACL appropriately.
214if [ ! -d "$prof_parent" ]; then
215  mkdir -p "$prof_parent"
[23058]216  /usr/bin/fs setacl "$prof_parent" system:anyuser none \
217    system:authuser none >/dev/null 2>&1
[22492]218fi
219
[22539]220# We want Firefox to download files for helper applications to
221# /var/tmp instead of /tmp, so that users have a better chance
222# of retrieving them later.
223if [ -z "$TMPDIR" ]; then
224  TMPDIR=/var/tmp
225  export TMPDIR
226fi
227
[22492]228# If the user specified any option, skip the check for a running
229# Firefox, and invoke the program now.
230case "$1" in
231-*)
[22784]232  exec /usr/bin/firefox.debathena-orig "$@"
[22492]233  ;;
234esac
235
236if [ "${MOZ_NO_REMOTE+set}" = "set" ]; then
237  found_running=false
238else
[23058]239  # See if there is a running instance of Firefox, by trying to
240  # "ping" it using X properties.
[22492]241  user="${LOGNAME:+-u $LOGNAME}"
[23058]242  /usr/bin/firefox.debathena-orig $user -a "$moz_progname" -remote \
[22524]243    "ping()" >/dev/null 2>&1
[22492]244  case $? in
245  0)
[23058]246    # We successfully pinged it.
[22492]247    found_running=true
248    ;;
249  2)
250    # This is the expected status when there is no running firefox window.
251    found_running=false
252    ;;
253  *)
254    # An unexpected error occurred.
255    found_running=error
256    ;;
257  esac
258fi
259
260# If Firefox is not running, check for a (possibly stale) lock on
261# the profile directory.
262if [ $found_running != true ]; then
263  profdir=`get_profdir`
264  if [ -n "$profdir" ]; then
265    # firefox now uses fcntl()-style locking on .parentlock, but an
266    # apparent openafs bug leaves the lock set after the program exits.
[22527]267    # Fortunately, it still maintains the "lock" symlink (which may
268    # also have been left from running a pre-1.5.0 firefox on the
269    # profile), so use its presence to help detect a stale lock.
[22492]270    if [ -h "$profdir/lock" ]; then
[22527]271      # The symlink exists, so the profile is (potentially) locked.
272      dispose_lock "$profdir"
[22492]273    else
274      # The symlink is gone, so just nuke the lock file, to work around
275      # the aforementioned openafs bug.
276      rm -f "$profdir/.parentlock"
277    fi
278  fi
279fi
280
[22797]281exec /usr/bin/firefox.debathena-orig "$@"
Note: See TracBrowser for help on using the repository browser.