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

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