Merge branch 'vuplus-1.6' of 192.168.102.66:/var/ikseong/repo/openembedded into vuplu...
[vuplus_openembedded] / recipes / autofs / autofs-4.1.4 / autofs
1 #!/bin/sh
2 #
3 # $Id: rc.autofs.in,v 1.58 2005/04/11 11:30:54 raven Exp $
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 28 72
16 # processname: /usr/sbin/automount
17 # config: /etc/auto.master
18 # description: Automounts filesystems on demand
19
20 # This is used in the Debian distribution to determine the proper
21 # location for the S- and K-links to this init file.
22 # The following value is extracted by debstd to figure out how to
23 # generate the postinst script. Edit the field to change the way the
24 # script is registered through update-rc.d (see the manpage for
25 # update-rc.d!)
26 #
27 FLAGS="defaults 21"
28
29 #
30 # Location of the automount daemon and the init directory
31 #
32 DAEMON=/usr/sbin/automount
33 prog=`basename $DAEMON`
34 initdir=/etc/init.d
35
36 test -e $DAEMON || exit 0
37
38 #
39 # Determine which kind of configuration we're using
40 #
41 system=debian
42
43
44 PATH=/sbin:/usr/sbin:/bin:/usr/bin
45 export PATH
46
47 #
48 # We can add local options here
49 # e.g. localoptions='rsize=8192,wsize=8192'
50 #
51 localoptions=''
52
53 daemonoptions='--ghost '
54
55 #
56 # load custom settings
57 #
58 TIMEOUT=5
59 DISABLE_DIRECT=1
60 DAEMON_EXIT_WAIT=20
61
62 daemonoptions="$daemonoptions --timeout=$TIMEOUT"
63
64
65 #
66 # Check for all maps that are to be loaded
67 #
68 getschemes()
69 {
70         SOURCES=`grep ^automount: /etc/nsswitch.conf | \
71                  sed -e 's/^.*://' -e 's/\[.*\]/ /g'`
72
73         if [ `echo $SOURCES | awk '{print NF}'` -gt 0 ]
74         then
75                 echo ${SOURCES}
76         else
77                 echo files
78         fi
79 }
80
81 catnismap()
82 {
83     if [ -z "$1" ] ; then
84         map="auto_master"
85     else
86         map="$1"
87     fi
88     /usr/bin/ypcat -k "$map" 2> /dev/null | sed -e '/^#/d' -e '/^$/d'
89 }
90
91 getfilemounts()
92 {
93     if [ -f /etc/auto.master ] ; then
94         cat /etc/auto.master | awk '{print $0}' | sed -e '/^#/d' -e '/^$/d' | (
95         while read auto_master_in
96         do
97             if [ "`echo $auto_master_in | grep '^+'`" = "" ]; then
98                 echo $auto_master_in
99             else
100                 for nismap in `cat /etc/auto.master | grep '^\+' |
101                         sed -e '/^#/d' -e '/^$/d'`; do
102                     catnismap `echo "$nismap" | sed -e 's/^\+//'`
103                 done
104             fi
105         done
106         )
107     fi
108 }
109
110 getnismounts()
111 {
112     YPMAP=`catnismap auto.master`
113     if [ -z "$YPMAP" ]; then
114        catnismap
115     else
116        catnismap auto.master
117     fi
118 }
119
120 getldapmounts()
121 {
122     if [ -x /usr/lib/autofs/autofs-ldap-auto-master ]; then
123         /usr/lib/autofs/autofs-ldap-auto-master 2> /dev/null
124     fi
125 }
126
127 getrawmounts()
128 {
129     for scheme in `getschemes` ; do
130         case "$scheme" in
131             files)
132                 if [ -z "$filescheme" ] ; then
133                     getfilemounts
134                     filescheme=1
135                     export filescheme
136                 fi
137                 ;;
138             nis)
139                 if [ -z "$nisscheme" ] ; then
140                     getnismounts
141                     nisscheme=1
142                     export nisscheme
143                 fi
144                 ;;
145             ldap*)
146                 if [ -z "$ldapscheme" ] ; then
147                     getldapmounts
148                     ldapscheme=1
149                     export ldapscheme
150                 fi
151                 ;;
152         esac
153     done
154 }
155
156
157 #
158 # This function will build a list of automount commands to execute in
159 # order to activate all the mount points. It is used to figure out
160 # the difference of automount points in case of a reload
161 #
162 getmounts()
163 {
164         local LC_ALL=C
165         export LC_ALL
166         knownmaps=" "
167         getrawmounts | (
168         while read dir map options
169         do
170             # These checks screen out duplicates and skip over directories
171             # where the map is '-'.
172             # We can't do empty or direct host maps, so don't bother trying.
173
174             # Strip trailing slashes from the dir line if it exists to aid
175             # in checking for duplicate maps
176             dir=`echo "$dir" | sed -e "s/\/*$//"`
177
178             if [ ! -z "$map" -a "$map" = "-hosts" ] ; then
179                 continue
180             fi
181
182             if [ $DISABLE_DIRECT -eq 1 \
183                         -a x`echo $dir | grep -E "^/-"` != 'x' ]
184             then
185                 continue
186             fi
187
188             # Do not include a map if it is a duplicate, maps on top of
189             # another map or another map, maps on top of it.
190             for knownmap in $knownmaps
191             do  
192                 if [ "`echo $dir/ | grep ^$knownmap`" != "" \
193                         -o "`echo $knownmap | grep ^$dir/`" != "" \]
194                 then 
195                     continue 2
196                 fi      
197             done
198
199             if [ ! -z "$dir" -a ! -z "$map" \
200                         -a x`echo "$map" | cut -c1` != 'x-' ]
201             then
202                 # If the options include a -t or --timeout, a -g or --ghost,
203                 # a -v or --verbose or a -d or --debug paramter, then pull
204                 # those particular options out.
205                 : echo DAEMONOPTIONS OPTIONS $daemonoptions $options
206                 startupoptions=
207                 if echo "$options" | grep -qE -- '\B-(t\b|-timeout\b=)' ;
208                 then
209                     startupoptions="--timeout=$(echo $options |\
210                         sed 's/.*-\(t[^0-9]*\|-timeout\)[ \t=]*\([0-9][0-9]*\).*$/\2/g')"
211                 elif echo "$daemonoptions" | grep -q -- '-t' ;
212                 then
213                     # It's okay to be sloppy with DAEMONOPTIONS as there is no
214                     # possibility of conflicting with mount or map options.
215                     startupoptions="--timeout=$(echo $daemonoptions | \
216                       sed 's/.*--*t\(imeout\)*[ \t=]*\([0-9][0-9]*\).*$/\2/g')"
217                 fi
218
219                 # Check for the ghost option
220                 if echo "$daemonoptions $options" | grep -qE -- '\B-(g\b|-ghost\b)' ;
221                 then
222                     startupoptions="$startupoptions --ghost"
223                 fi
224                 # Dont even deal with conflicts between --ghost and [no]browse
225                 # Its just insane to configure things like that.
226                 if echo "$options" | grep -qE -- '\B-browse\b' ;
227                 then
228                     startupoptions="$startupoptions --ghost"
229                 fi
230                 # Check for verbose
231                 if echo "$daemonoptions $options" | \
232                                         grep -qE -- '\B-(v\b|-verbose\b)' ;
233                 then
234                     startupoptions="$startupoptions --verbose"
235                 fi
236
237                 # Check for debug
238                 if echo "$daemonoptions $options" | \
239                                         grep -qE -- '\B-(d\b|-debug\b)' ;
240                 then
241                     startupoptions="$startupoptions --debug"
242                 fi
243
244                 # Other option flags are intended for maps.
245                 mapoptions="$(echo "$daemonoptions $options" |\
246                       sed   's/-\(t[^0-9]*\|-timeout\)[ \t=]*\([0-9][0-9]*\)//g' |
247                       sed   's/-\(g\b\|-ghost\b\)//g' |
248                       sed   's/-\(v\b\|-verbose\b\)//g' |
249                       sed   's/-\(d\b\|-debug\b\)//g' |
250                       sed   's/-\b\(no\)\?browse\b//g')"
251
252                 # Break up the maptype and map, if the map type is specified
253                 maptype=`echo $map | cut -f1 -d:`
254                 # Handle degenerate map specifiers
255                 if [ "$maptype" = "$map" ] ; then
256                     if [ "$map" = "hesiod" -o "$map" = "userhome" ] ; then
257                         maptype=$map
258                         map=
259                     elif [ "$map" = "multi" ] ; then
260                         maptype=$map
261                         map=
262 #                   elif echo "$map" | grep -q '^!'; then
263 #                       map=`echo "$map"| sed -e 's/^!//g'`
264                     elif `echo $map | grep -q "^/"` && [ -x "$map" ]; then
265                         maptype=file
266                     elif [ -x "/etc/$map" ]; then
267                         maptype=program
268                         map=`echo /etc/$map | sed 's^//^/^g'`
269                     elif `echo $map | grep -q "^/"` && [ -f "$map" ]; then
270                         maptype=file
271                     elif [ -f "/etc/$map" ]; then
272                         maptype=file
273                         map=`echo /etc/$map | sed 's^//^/^g'`
274                     else
275                         maptype=yp
276                         if [ "$UNDERSCORETODOT" = "1" ] ; then
277                             map=`basename $map | sed -e s/^auto_home/auto.home/ -e s/^auto_mnt/auto.mnt/`
278                         else
279                             map=`basename $map | sed 's^//^/^g'`
280                         fi
281                     fi
282                 fi
283                 map=`echo $map | cut -f2- -d:`
284
285                 : echo STARTUPOPTIONS $startupoptions
286                 : echo DIR $dir
287                 : echo MAPTYPE $maptype
288                 : echo MAP $map
289                 : echo MAPOPTIONS $mapoptions
290                 : echo LOCALOPTIONS $localoptions
291
292                 NEWOPTIONS=""
293                 for m in $mapoptions
294                 do
295                         if [ x"$m" = x"--" ]; then
296                                 NEWOPTIONS="$NEWOPTIONS $localoptions --"
297                         else
298                                 NEWOPTIONS="$NEWOPTIONS $m"
299                         fi
300                 done
301                 mapoptions=$NEWOPTIONS
302
303                 echo "$DAEMON $startupoptions $dir $maptype $map $mapoptions $localoptions" | sed -e 's/        / /g' -e 's/  */ /g'
304
305                 : echo ------------------------
306                 knownmaps=" $dir/ $knownmaps"
307             fi
308         done
309     )
310 }
311
312 #
313 # Status lister.
314 #
315 status()
316 {
317         echo -e $"Configured Mount Points:\n------------------------"
318         getmounts
319         echo ""
320         echo -e $"Active Mount Points:\n--------------------"
321         ps axwww|grep "[0-9]:[0-9][0-9] $DAEMON " | (
322                 while read pid tt stat time command; do echo $command; done
323         )
324 }
325
326 get_command_from_pid()
327 {
328         ps ax | grep "[0-9]:[0-9][0-9] $DAEMON " | (
329                 while read pid tt stat time command; do
330                         if [ "$pid" = "$1" ] ; then
331                                 echo `echo "$command" | \
332                                         sed 's/--pid-file.*\.pid/ /'`
333                                 return 0
334                         fi
335                 done
336         )
337         return 0
338 }
339
340 # return true if at least one pid is alive
341 alive()
342 {
343     if [ -z "$*" ]; then
344         return 1
345     fi
346     for i in $*; do
347         if kill -0 $i 2> /dev/null; then
348             return 0
349         fi
350     done
351
352     return 1
353 }
354
355 #
356 # Find pids of process group leaders
357 #
358 get_pgrp_pids()
359 {
360         ps -eo pid,pgrp,cmd|grep $DAEMON|\
361         awk '{print $1,$2}'|\
362         while read pid pgrp
363         do
364                 if [ $pid -eq $pgrp ]
365                 then
366                         echo $pid
367                 fi
368         done
369 }
370
371 #
372 # Signal each automount process group leader
373 #
374 signal_automounts()
375 {
376         RETVAL=0
377         pgrp_pids=`get_pgrp_pids`
378         for pid in $pgrp_pids
379         do
380                 kill -USR2 $pid 2 > /dev/null
381                 count=0
382                 while ps ax|grep -v grep|grep $pid >/dev/null
383                 do
384                         sleep 1 
385                         count=$(expr $count + 1)
386                         if [ $count -gt $DAEMON_EXIT_WAIT ]; then
387                                 break;
388                         fi
389                 done
390         done
391
392         if [ -n "`/sbin/pidof $DAEMON`" ] ; then
393                 RETVAL=1
394         fi
395
396         return $RETVAL
397 }
398
399 umount_loopback()
400 {
401     loops=`LC_ALL=C awk '!/^#/ && $1 ~ /^\/dev\/loop/ && $2 != "/" {print $2}' /proc/mounts`
402     automounts=`LC_ALL=C awk '!/^#/ && $1 ~ /^automount/ {print $2}' /proc/mounts`
403
404     for l in $loops
405     do
406         # If a loop device is mounted into a subdir of the automount point,
407         # umount it here.
408         for a in $automounts
409         do
410             match=`echo $l | grep -E "^$a[\$|/]" || true`
411             if [ -n "$match" ]; then
412                 echo ""
413                 echo -n $"Unmounting loopback filesystem $match:  "
414                 loopdev=`LC_ALL=C grep $l /proc/mounts | awk '{print $1}'`
415                 umount -d $match
416                 if [ $? -eq 0 ]; then
417                     echo "done"
418                 else
419                     echo "failed ($?)"
420                 fi
421             fi
422         done
423     done
424
425     # If the loop device file exists under the automount point, umount
426     # it here.
427     loopmnts=`LC_ALL=C awk '!/^#/ && $1 ~ /^\/dev\/loop/ && $2 != "/" {print $1}' /proc/mounts`
428     for l in $loopmnts
429     do
430         loopmnt=`losetup $l | awk -F\( '{print $2}' | awk -F\) '{print $1}'`
431         for a in $automounts
432         do
433             match=`echo $loopmnt | grep -E "^$a[\$|/]" || true`
434             if [ -n "$match" ]; then
435                 echo ""
436                 echo -n $"Unmounting loopback filesystem $match:  "
437                 umount -d $match
438                 if [ $? -eq 0 ]; then
439                     echo "done"
440                 else
441                     echo "failed ($?)"
442                 fi
443             fi
444         done
445     done
446 }
447
448 stop() {
449         echo -n $"Stopping $prog: "
450         count=0
451         while [ -n "`pidof $DAEMON`" -a $count -lt 15 ] ; do
452                 killall -TERM $prog >& /dev/null
453                 RETVAL=$?
454                 [ $RETVAL = 0 -a -z "`pidof $DAEMON`" ] || sleep 3
455                 count=`expr $count + 1`
456         done
457         if [ -n "`pidof $DAEMON`" ] ; then
458                 echo "done."
459         else
460                 echo "failed."
461         fi
462         return $RETVAL
463 }
464
465
466
467 case "$1" in
468 start)
469         echo -n 'Starting automounter:'
470
471         # make sure autofs4 is loaded
472         if ! grep -q autofs /proc/filesystems
473         then
474                 # autofs filesystem support not loaded
475                 echo -n ' loading autofs4 kernel module,'
476                 modprobe autofs4 || true
477         elif ([ -f /proc/modules ] && lsmod) | grep -q autofs[^4]; then
478                 # wrong autofs filesystem module loaded
479                 echo
480                 echo "WARNING: autofs kernel module is loaded, autofs4 needed"
481                 echo " for correct behaviour. You might experience mount failures."
482         fi
483
484         # Check that maps exist
485         if [ -z "$(getrawmounts)" ]
486         then
487                 echo " no automount maps defined."
488                 exit 0
489         fi
490
491         # ensure pid file directory exists
492         if [ ! -e /var/run/autofs ]
493         then
494                 mkdir /var/run/autofs
495         fi
496
497         getmounts | while read cmd rest
498         do
499                 mnt=`echo $rest | sed 's/^.* \(\/[^ ]*\) [A-Za-z].*$/\1/'`
500                 pidfile=/var/run/autofs/`echo $mnt | sed 's,/,_,g'`.pid
501
502                 start-stop-daemon --start --pidfile $pidfile --quiet \
503                         --exec $DAEMON -- --pid-file=$pidfile $rest
504
505                 ret=$?
506
507                 if [ $ret -ne 0 ]
508                 then
509                         echo
510                         echo " failed to start automount point $mnt"
511                 fi
512         done
513
514         echo " done."
515         ;;
516 stop)
517         echo -n 'Stopping automounter:'
518
519         umount_loopback
520         
521         any=0
522         for file in `ls /var/run/autofs/*.pid 2>/dev/null`
523         do
524                 if [ -e "$file" ]
525                 then
526                         any=1
527                         pid=`head -n 1 $file`
528                         mnt=`ps -wwo 'cmd=' $pid | sed -e \
529                           's,.* --pid-file=/var/run/autofs/\([^ ]*\)\.pid.*,\1,; s,_,/,g'`
530                         dname=`basename $DAEMON`
531
532                         start-stop-daemon --stop --quiet --pidfile $file --name $dname
533
534                         ret=$?
535                         case $ret in
536                         0)
537 #                               echo -n " $mnt"
538                                 rm -f $file
539                                 ;;
540                         1)
541                                 echo -n -e "\n No process for automount $mnt"
542                                 rm -f $file
543                                 ;;
544                         2)
545                                 echo -n -e "\n  Couldn't stop automount for $mnt"
546                                 ;;
547                         *)
548                                 echo -n -e \
549                                 "\n Strange start-stop-daemon exit status: $ret"
550                                 ;;
551                         esac
552                 fi
553         done
554
555         echo " done."
556         ;;
557 reload)
558         echo "Reloading automounter: checking for changes ... "
559
560         TMP1=`mktemp autofs.XXXXXX`
561         TMP2=`mktemp autofs.XXXXXX`
562
563         getmounts >$TMP1
564
565         for i in `ls /var/run/autofs/*.pid 2>/dev/null`
566         do
567                 pid=`head -n 1 $i 2>/dev/null`
568                 [ "$pid" = "" ] && continue
569                 
570                 cmd=`get_command_from_pid $pid`
571                 echo $cmd >>$TMP2
572
573                 mnt=`ps -wwo 'cmd=' $pid | sed -e \
574                  's,.* --pid-file=/var/run/autofs/\([^ ]*\)\.pid.*,\1,; s,_,/,g'`
575
576                 if ! grep -q "$cmd" $TMP1; then
577                         echo "Stopping automounter for: $mnt"
578                         kill -USR2 $pid 2> /dev/null
579                 else
580                         echo "Reloading automounter map for: $mnt"
581                         kill -HUP $pid 2> /dev/null
582                 fi
583         done
584
585         cat $TMP1 | (while read x; do
586                 if ! grep -q "^$x" $TMP2; then
587                         echo $x | while read cmd rest; do
588
589                         mnt=`echo $rest | sed 's/^.* \(\/[^ ]*\) [A-Za-z].*$/\1/'`
590                         pidfile=/var/run/autofs/`echo $mnt | sed 's,/,_,g'`.pid
591
592                         start-stop-daemon --start --pidfile $pidfile --quiet \
593                                 --exec $DAEMON -- --pid-file=$pidfile $rest
594
595                         ret=$?
596
597                         if [ $ret -ne 0 ]
598                         then
599                                 echo
600                                 echo " failed to start automount point $mnt"
601                         else
602                                 echo "Started automounter: $mnt"
603                         fi
604                         done
605                 fi
606         done)
607
608         rm -f $TMP1 $TMP2
609         ;;
610 force-reload|restart)
611         stop
612         start
613         ;;
614 status)
615         status
616         ;;
617 getmounts)
618         getmounts
619         ;;
620 active)
621         alive
622         ;;
623 *)
624         echo "Usage: $initdir/autofs {start|stop|restart|reload|status|getmounts|active}" >&2
625         exit 1
626         ;;
627 esac
628
629 exit 0
630