~ubuntu-branches/ubuntu/dapper/autofs/dapper

« back to all changes in this revision

Viewing changes to debian/init

  • Committer: Bazaar Package Importer
  • Author(s): Steinar H. Gunderson
  • Date: 2005-10-06 14:42:32 UTC
  • mfrom: (1.1.2 upstream) (2.1.1 sarge)
  • Revision ID: james.westby@ubuntu.com-20051006144232-6h2yly3gss95xjfl
Tags: 4.1.4-8
* Added a debian/compat file instead of setting DH_COMPAT.
* When purging, check if /usr/bin/ucf exists, and don't use it if it
  doesn't. (Closes: #330556)
* Ship .md5sum files for ucf with the md5sums from the conffiles in sarge,
  so the user won't be asked by ucf on the initial upgrade from sarge to
  an ucf-enabled version. (Closes: #331688)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#! /bin/bash
2
 
#
3
 
# $Id: //depot/autofs-4.0/samples/rc.autofs.in#4 $
4
 
#
5
 
# rc file for automount using a Sun-style "master map".
6
 
# We first look for a local /etc/auto.master, then a YP
7
 
# map with that name
8
 
#
9
 
# On most distributions, this file should be called:
10
 
# /etc/rc.d/init.d/autofs or /etc/init.d/autofs
11
 
#
12
 
 
13
 
# For Redhat-ish systems
14
 
#
15
 
# chkconfig: 345 15 85
16
 
# description: Automounts filesystems on demand
17
 
 
18
 
# This is used in the Debian distribution to determine the proper
19
 
# location for the S- and K-links to this init file.
20
 
# The following value is extracted by debstd to figure out how to
21
 
# generate the postinst script. Edit the field to change the way the
22
 
# script is registered through update-rc.d (see the manpage for
23
 
# update-rc.d!)
24
 
#
25
 
FLAGS="defaults 21"
26
 
 
27
 
#
28
 
# Location of the automount daemon and the init directory
29
 
#
30
 
DAEMON=/usr/sbin/automount
31
 
initdir=/etc/init.d
32
 
 
33
 
#
34
 
# Determine which kind of configuration we're using
35
 
#
36
 
system=unknown
37
 
if [ -f /etc/debian_version ]; then
38
 
    system=debian
39
 
elif [ -f /etc/redhat-release ]; then
40
 
    system=redhat
41
 
else
42
 
    echo "$0: Unknown system, please port and contact autofs@linux.kernel.org" 1>&2
43
 
    exit 1
44
 
fi
45
 
 
46
 
if [ $system = redhat ]; then
47
 
    . $initdir/functions
48
 
fi
49
 
 
50
 
test -e $DAEMON || exit 0
51
 
thisscript="$0"
52
 
if [ ! -f "$thisscript" ]; then
53
 
    echo "$0: Cannot find myself" 1>&2
54
 
    exit 1
55
 
fi
56
 
 
57
 
PATH=/sbin:/usr/sbin:/bin:/usr/bin
58
 
export PATH
59
 
 
60
 
#
61
 
# We can add local options here
62
 
# e.g. localoptions='rsize=8192,wsize=8192'
63
 
#
64
 
localoptions=''
65
 
 
66
 
#
67
 
# Daemon options
68
 
# e.g. --timeout 60
69
 
#
70
 
daemonoptions=''
71
 
 
72
 
#
73
 
# This function will build a list of automount commands to execute in
74
 
# order to activate all the mount points. It is used to figure out
75
 
# the difference of automount points in case of a reload
76
 
#
77
 
function getmounts()
78
 
{
79
 
    if ! getmounts_file /etc/auto.master; then
80
 
        if ! getnismounts auto.master; then
81
 
            echo "no autofs mounts configured!" 1>&2
82
 
        fi
83
 
    fi
84
 
}
85
 
 
86
 
function getmounts_file()
87
 
{
88
 
    #
89
 
    # Check for local maps to be loaded
90
 
    #
91
 
    local map="$1"
92
 
    shift
93
 
    if [ -f "$map" ]
94
 
    then
95
 
        cat "$map" | sed -e '/^#/d' -e '/^$/d' | process_master_file "$@"
96
 
    else
97
 
        return 1
98
 
    fi
99
 
}
100
 
 
101
 
function process_master_file()
102
 
{
103
 
    local line_options="$@" daemon_options
104
 
    while read dir map options
105
 
    do
106
 
        options="$line_options $options"
107
 
        case "$dir" in
108
 
            +*) getnismounts "${dir/+/}" $map $options
109
 
                continue;
110
 
        esac
111
 
        if [ ! -z "$dir" -a ! -z "$map" \
112
 
                -a x`echo "$map" | cut -c1` != 'x-' ]
113
 
        then
114
 
            type=""
115
 
            if [ -x $map ]; then
116
 
                type=program
117
 
            elif [ -f $map ]; then
118
 
                type=file
119
 
            elif [ x`echo "$map" | cut -c1` == 'x/' ]; then
120
 
                # Starts with '/', thus a file, but doesn't exist
121
 
                # so leave it out, leaving 'type=""' (hro)
122
 
                type=""
123
 
            elif [ -f /etc/$map ]; then
124
 
                type=file
125
 
                map=/etc/$map
126
 
            elif [ $map == hesiod ]; then
127
 
                type=hesiod
128
 
                map=""
129
 
            elif [ $map == ldap ]; then
130
 
                type=ldap
131
 
                map=""
132
 
            else
133
 
                case "$map" in
134
 
                    !*) type=program map="${map/!/}";;
135
 
                    *)  type=yp;;
136
 
                esac
137
 
            fi
138
 
            daemon_options=`munge_options daemon $daemonoptions $options`
139
 
            options=`munge_options mount $options`
140
 
 
141
 
            if [ "$type" ]; then
142
 
                echo "$DAEMON $daemon_options -- $dir $type $map $localoptions $options"
143
 
            fi
144
 
        fi
145
 
    done
146
 
}
147
 
 
148
 
function getnismounts()
149
 
{
150
 
    #
151
 
    # Check for YellowPage maps to be loaded
152
 
    #
153
 
    local map="$1"
154
 
    shift
155
 
    if [ -e /usr/bin/ypcat ] && [ `ypcat -k "$map" 2>/dev/null | wc -l` -gt 0 ]
156
 
    then
157
 
        ypcat -k "$map" | process_master_file "$@"
158
 
    else
159
 
        return 1
160
 
    fi
161
 
}
162
 
 
163
 
#
164
 
# List active mounts
165
 
#
166
 
function active()
167
 
{
168
 
    ps ax|grep "[0-9]:[0-9][0-9] $DAEMON " |
169
 
        while read pid tt stat time command
170
 
        do
171
 
            echo $command
172
 
        done
173
 
}
174
 
 
175
 
#
176
 
# Status lister.
177
 
#
178
 
function status()
179
 
{
180
 
        echo "Configured Mount Points:"
181
 
        echo "------------------------"
182
 
        getmounts | sed 's/ -- / /'
183
 
        echo ""
184
 
        echo "Active Mount Points:"
185
 
        echo "--------------------"
186
 
        active
187
 
}
188
 
 
189
 
function munge_options()
190
 
{
191
 
        local which="$1"
192
 
        shift
193
 
        echo "$@" | awk -v which="$which" '
194
 
BEGIN {
195
 
        RS="[, \n-]"
196
 
        FS="="
197
 
        daemon_opts[ "timeout" ] = "timeout"
198
 
        daemon_opts[ "t" ] = "timeout"
199
 
}
200
 
{
201
 
        if ( $0 ~ /^$/ )
202
 
                next
203
 
        if ( $1 in daemon_opts ) {
204
 
                daemon[ daemon_opts[ $1 ] ] = $2
205
 
        } else {
206
 
                mount[$0] = 1
207
 
        }
208
 
}
209
 
END {
210
 
        if ( which ~ "^daemon$" ) {
211
 
                if ( "timeout" in daemon ) {
212
 
                        printf "--timeout=%s\n", daemon["timeout"]
213
 
                }
214
 
        } else {
215
 
                for ( a in mount ) {
216
 
                        if ( length( out ) )
217
 
                                out=out "," a
218
 
                        else
219
 
                                out=a
220
 
                }
221
 
                printf "%s\n", out
222
 
        }
223
 
}
224
 
'
225
 
}
226
 
 
227
 
# return true if at least one pid is alive
228
 
function alive()
229
 
{
230
 
    if [ -z "$*" ]; then
231
 
        return 1
232
 
    fi
233
 
    for i in $*; do
234
 
        if kill -0 $i 2> /dev/null; then
235
 
            return 0
236
 
        fi
237
 
    done
238
 
 
239
 
    return 1
240
 
}
241
 
 
242
 
#
243
 
# Redhat start/stop function.
244
 
#
245
 
function redhat()
246
 
{
247
 
 
248
 
#
249
 
# See how we were called.
250
 
#
251
 
case "$1" in
252
 
  start)
253
 
        # Check if the automounter is already running?
254
 
        if [ ! -f /var/lock/subsys/autofs ]; then
255
 
            echo 'Starting automounter: '
256
 
            getmounts | sed 's/ -- / /' | sh
257
 
            touch /var/lock/subsys/autofs
258
 
        fi
259
 
        ;;
260
 
  stop)
261
 
        pids=$(/sbin/pidof $DAEMON)
262
 
        kill -TERM $pids 2> /dev/null && sleep 1
263
 
        count=1
264
 
        while alive $pids; do
265
 
            sleep 5
266
 
            count=$(expr $count + 1)
267
 
            if [ $count -gt 5 ]; then
268
 
                echo "Giving up on automounter"
269
 
                break;
270
 
            fi
271
 
            echo "Automounter not stopped yet: retrying... (attempt $count)"
272
 
        done
273
 
        if [ $count -gt 1 -a $count -le 5 ]; then
274
 
            echo "Automounter stopped"
275
 
        fi
276
 
        rm -f /var/lock/subsys/autofs
277
 
        ;;
278
 
  reload|restart)
279
 
        if [ ! -f /var/lock/subsys/autofs ]; then
280
 
                echo "Automounter not running"
281
 
                exit 1
282
 
        fi
283
 
        echo "Checking for changes to /etc/auto.master ...."
284
 
        TMP1=`mktemp /tmp/autofs.XXXXXX` || { echo "could not make temp file" >& 2; exit 1; }
285
 
        TMP2=`mktemp /tmp/autofs.XXXXXX` || { echo "could not make temp file" >& 2; exit 1; }
286
 
        getmounts | sed 's/ -- / /' >$TMP1
287
 
        ps ax|grep "[0-9]:[0-9][0-9] $DAEMON " | (
288
 
            while read pid tt stat time command; do
289
 
                echo "$command" >>$TMP2
290
 
                if ! grep -q "^$command" $TMP2; then
291
 
                        while kill -USR2 $pid; do
292
 
                                sleep 3
293
 
                        done
294
 
                        echo "Stop $command"
295
 
                fi
296
 
            done
297
 
        )
298
 
        ( while read x; do
299
 
                if ! grep -q "^$x" $TMP2; then
300
 
                        $x
301
 
                        echo "Start $x"
302
 
                fi
303
 
        done ) < $TMP1
304
 
        rm -f $TMP1 $TMP2
305
 
        ;;
306
 
  status)
307
 
        status
308
 
        ;;
309
 
  getmounts)
310
 
        getmounts
311
 
        ;;
312
 
  active)
313
 
        active
314
 
        ;;
315
 
 
316
 
  *)
317
 
        echo "Usage: $initdir/autofs {start|stop|restart|reload|status|getmounts|active}"
318
 
        exit 1
319
 
esac
320
 
}
321
 
 
322
 
#
323
 
# Debian start/stop functions.
324
 
#
325
 
function debian()
326
 
{
327
 
#
328
 
# See how we were called.
329
 
#
330
 
case "$1" in
331
 
    start)
332
 
        echo -n 'Starting automounter: '
333
 
        getmounts | while read cmd args
334
 
        do
335
 
                opt=${args%%-- *}
336
 
                rest=${args#*-- }
337
 
                mnt=${rest%% *}
338
 
                rest=${rest#* }
339
 
                echo -n " $mnt"
340
 
                if [ ! -d /var/run/autofs ]; then
341
 
                        mkdir /var/run/autofs
342
 
                fi
343
 
                pidfile=/var/run/autofs/`echo $mnt | sed 's,_,__,g;s,/,_:,g'`.pid
344
 
                start-stop-daemon --start --pidfile $pidfile --quiet \
345
 
                        --exec $DAEMON -- $opt $mnt $rest
346
 
        done
347
 
        echo "done."
348
 
        ;;
349
 
    stop)
350
 
        echo -n 'Stopping automounter: '
351
 
        start-stop-daemon --stop --quiet --signal USR2 --exec $DAEMON
352
 
        for file in /var/run/autofs/*.pid
353
 
        do
354
 
            if [ -e "$file" ]
355
 
            then
356
 
                pid=`head -n 1 $file`
357
 
                command=`tail -n 1 $file`
358
 
                if [ -z "`ps --no-heading $pid`" ]
359
 
                then
360
 
                    echo -n "Stopped $pid($command) "
361
 
                    rm -f $file
362
 
                else
363
 
                    echo -n "Couldn't stop $pid($command) "
364
 
                fi
365
 
            fi
366
 
        done
367
 
        echo "done."
368
 
        ;;
369
 
    reload)
370
 
        echo -n "Reloading automounter: checking for changes ... "
371
 
        TMP=/var/run/autofs.tmp
372
 
        getmounts | sed 's/ -- / /' >$TMP
373
 
        for i in /var/run/autofs/*.pid
374
 
        do
375
 
                pid=`head -n 1 $i 2>/dev/null`
376
 
                [ "$pid" = "" ] && continue
377
 
                command=`tail +2 $i`
378
 
                if ! grep -q "^$command" $TMP
379
 
                then
380
 
                        echo -n "Stopping automounter: $command"
381
 
                        kill -USR2 $pid
382
 
                        rm -f $i
383
 
                fi
384
 
        done
385
 
        echo "done."
386
 
        rm -f $TMP
387
 
        $thisscript start
388
 
        ;;
389
 
    force-reload|restart)
390
 
        $0 stop
391
 
        $0 start
392
 
        ;;
393
 
    status)
394
 
        status
395
 
        ;;
396
 
    getmounts)
397
 
        getmounts | sed 's/ -- / /'
398
 
        ;;
399
 
    active)
400
 
        active
401
 
        ;;
402
 
    *)
403
 
        echo "Usage: $initdir/autofs {start|stop|restart|reload|force-reload|status|getmounts|active}" >&2
404
 
        exit 1
405
 
        ;;
406
 
esac
407
 
}
408
 
 
409
 
if [ $system = debian ]; then
410
 
        debian "$@"
411
 
elif [ $system = redhat ]; then
412
 
        redhat "$@"
413
 
fi
414
 
 
415
 
exit 0