fix permission checking on full file.
[xfstests-dev.git] / common.rc
1 ##/bin/sh
2
3 #
4 # Copyright (c) 2000 Silicon Graphics, Inc.  All Rights Reserved.
5
6 # This program is free software; you can redistribute it and/or modify it
7 # under the terms of version 2 of the GNU General Public License as
8 # published by the Free Software Foundation.
9
10 # This program is distributed in the hope that it would be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
14 # Further, this software is distributed without any warranty that it is
15 # free of the rightful claim of any third person regarding infringement
16 # or the like.  Any license provided herein, whether implied or
17 # otherwise, applies only to this software file.  Patent licenses, if
18 # any, provided herein do not apply to combinations of this program with
19 # other software, or any other product whatsoever.
20
21 # You should have received a copy of the GNU General Public License along
22 # with this program; if not, write the Free Software Foundation, Inc., 59
23 # Temple Place - Suite 330, Boston MA 02111-1307, USA.
24
25 # Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
26 # Mountain View, CA  94043, or:
27
28 # http://www.sgi.com 
29
30 # For further information regarding this notice, see: 
31
32 # http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
33 #
34
35 # we need common.config
36 if ! . ./common.config
37 then
38     echo "$iam: failed to source common.config"
39     exit 1
40 fi
41
42 # awk
43 AWK_PROG=awk
44
45 # ps
46 PS_HAVE_BSD=false
47 PS_ALL_FLAGS=-efw
48
49 # host os
50 PLATFORM=linux
51
52 # extra parameters for fsstress
53 FSSTRESS_AVOID="-f resvsp=0 -f unresvsp=0"
54
55 # env variables for mem checking
56 EF_PROTECT_FREE=1 # efence
57
58 export AWK_PROG PS_HAVE_BSD PS_ALL_FLAGS PLATFORM
59 export EF_PROTECT_FREE
60
61 # we override mount so we can specify mount options
62
63 mount()
64 {
65     case "$*"
66     in
67         *remount*)
68             /bin/mount $*
69             ;;
70         *ext2*)
71             /bin/mount $*
72             ;;
73         *xfs*)
74             /bin/mount $* $MOUNT_OPTIONS
75             ;;
76         *)
77             /bin/mount $*
78             ;;
79     esac
80 }
81
82 #
83
84 _get_pids_by_name()
85 {
86     if [ $# -ne 1 ]
87     then
88         echo "Usage: _get_pids_by_name process-name" 1>&2
89         exit 1
90     fi
91
92     # Algorithm ... all ps(1) variants have a time of the form MM:SS or
93     # HH:MM:SS before the psargs field, use this as the search anchor.
94     #
95     # Matches with $1 (process-name) occur if the first psarg is $1
96     # or ends in /$1 ... the matching uses sed's regular expressions,
97     # so passing a regex into $1 will work.
98
99     ps $PS_ALL_FLAGS \
100     | sed -n \
101         -e 's/$/ /' \
102         -e 's/[         ][      ]*/ /g' \
103         -e 's/^ //' \
104         -e 's/^[^ ]* //' \
105         -e "/[0-9]:[0-9][0-9]  *[^ ]*\/$1 /s/ .*//p" \
106         -e "/[0-9]:[0-9][0-9]  *$1 /s/ .*//p"
107 }
108
109 # fqdn for localhost
110 #
111 _get_fqdn()
112 {
113     host=`hostname`
114     nslookup $host | $AWK_PROG '{ if ($1 == "Name:") print $2 }'
115 }
116
117 # fix malloc libs output
118 #
119 _fix_malloc()
120 {
121     # filter out the Electric Fence notice
122     perl -e '
123         while (<>) {
124             if (defined $o && /^\s+Electric Fence/) {
125                 chomp($o);
126                 print "$o";
127                 undef $o;
128                 next;
129             }
130             print $o if (defined $o);
131
132             $o=$_;
133         }
134         print $o if (defined $o);
135     '
136 }
137
138 # check if run as root
139 #
140 _need_to_be_root()
141 {
142     id=`id | sed -e 's/(.*//' -e 's/.*=//'`
143     if [ "$id" -ne 0 ]
144     then
145         echo "Arrgh ... you need to be root (not uid=$id) to run this test"
146         exit 1
147     fi
148 }
149
150
151 #
152 # _df_device : get an IRIX style df line for a given device 
153 #
154 #       - returns "" if not mounted
155 #       - returns fs type in field two (ala IRIX)
156 #       - joins line together if split by fancy df formatting
157 #       - strips header etc
158 #
159
160 _df_device()
161 {
162     if [ $# -ne 1 ]
163     then
164         echo "Usage: _df_device device" 1>&2
165         exit 1
166     fi
167     
168     df -T 2>/dev/null | $AWK_PROG -v what=$1 '
169         match($1,what) && NF==1 { 
170             v=$1
171             getline
172             print v, $0
173             exit
174         }
175         match($1,what) {
176             print
177             exit
178         }
179     '
180 }
181
182 #
183 # _df_dir : get an IRIX style df line for device where a directory resides
184 #
185 #       - returns fs type in field two (ala IRIX)
186 #       - joins line together if split by fancy df formatting
187 #       - strips header etc
188 #
189
190 _df_dir()
191 {
192     if [ $# -ne 1 ]
193     then
194         echo "Usage: _df_dir device" 1>&2
195         exit 1
196     fi
197     
198     df -T $1 2>/dev/null | $AWK_PROG -v what=$1 '
199         NR == 2 && NF==1 { 
200             v=$1
201             getline 
202             print v, $0;
203             exit 0
204         }
205         NR == 2 {
206             print;
207             exit 0
208         }
209         {}
210     '
211     # otherwise, nada
212 }
213
214 # return percentage used disk space for mounted device
215
216 _used()
217 {
218     if [ $# -ne 1 ]
219     then
220         echo "Usage: _used device" 1>&2
221         exit 1
222     fi
223     
224     _df_device $1 | $AWK_PROG '{ sub("%", "") ; print $6 }'
225 }
226
227 # return the FS type of a mounted device
228 #
229 _fs_type()
230 {
231     if [ $# -ne 1 ]
232     then
233         echo "Usage: _fs_type device" 1>&2
234         exit 1
235     fi
236     
237     _df_device $1 | $AWK_PROG '{ print $2 }'
238 }
239
240 # return the FS mount options of a mounted device
241 #
242 _fs_options()
243 {
244     if [ $# -ne 1 ]
245     then
246         echo "Usage: _fs_options device" 1>&2
247         exit 1
248     fi
249     
250     $AWK_PROG -v dev=$1 '
251         match($1,dev) { print $4 }
252     ' </proc/mounts
253     
254 }
255
256 # returns device number if a file is a block device
257 #
258 _is_block_dev()
259 {
260     if [ $# -ne 1 ]
261     then
262         echo "Usage: _is_block_dev dev" 1>&2
263         exit 1
264     fi
265     
266     [ -b $1 ] && src/lstat64 $1 | $AWK_PROG '/Device type:/ { print $9 }'
267 }
268
269 # Do a command, log it to $seq.full, optionally test return status
270 # and die if command fails. If called with one argument _do executes the
271 # command, logs it, and returns its exit status. With two arguments _do
272 # first prints the message passed in the first argument, and then "done"
273 # or "fail" depending on the return status of the command passed in the
274 # second argument. If the command fails and the variable _do_die_on_error
275 # is set to "always" or the two argument form is used and _do_die_on_error
276 # is set to "message_only" _do will print an error message to
277 # $seq.out and exit.
278
279 _do()
280 {
281   if [ $# -eq 1 ]; then
282     _cmd=$1
283   elif [ $# -eq 2 ]; then
284     _note=$1
285     _cmd=$2
286     echo -n "$_note... "
287   else
288     echo "Usage: _do [note] cmd" 1>&2
289     status=1; exit
290   fi
291
292   (eval "echo '---' \"$_cmd\"") >>$seq.full
293   (eval "$_cmd") >$tmp._out 2>&1; ret=$?
294   cat $tmp._out | _fix_malloc >>$seq.full
295   if [ $# -eq 2 ]; then
296     if [ $ret -eq 0 ]; then
297       echo "done"
298     else
299       echo "fail"
300     fi
301   fi
302   if [ $ret -ne 0  ] \
303      && [ "$_do_die_on_error" = "always" \
304          -o \( $# -eq 2 -a "$_do_die_on_error" = "message_only" \) ]
305   then
306       [ $# -ne 2 ] && echo
307       eval "echo \"$_cmd\" failed \(returned $ret\): see $seq.full"
308       status=1; exit
309   fi
310
311   return $ret
312 }
313
314 # bail out, setting up .notrun file
315
316 _notrun()
317 {
318     echo "$*" >$seq.notrun
319     echo "$seq not run: $*"
320     status=0
321     exit
322 }
323
324 # just plain bail out
325
326 _fail()
327 {
328     echo "$*" | tee -a $seq.full
329     echo "(see $seq.full for details)"
330     status=1
331     exit 1
332 }
333
334 # this test needs a scratch partition - check we're ok & unmount it
335
336 _require_scratch()
337 {
338
339     if [ -z "$SCRATCH_DEV" -o "`_is_block_dev $SCRATCH_DEV`" = "" ]
340     then
341         _notrun "this test requires a valid \$SCRATCH_DEV"
342     fi
343     
344     if [ "`_is_block_dev $SCRATCH_DEV`" = "`_is_block_dev $TEST_DEV`" ]
345     then
346         _notrun "this test requires a valid \$SCRATCH_DEV"
347     fi
348     
349     # mounted?
350     if mount | grep -q $SCRATCH_DEV
351     then 
352         # if it's mounted, make sure its on $SCRATCH_MNT
353         if ! mount | grep $SCRATCH_DEV | grep -q $SCRATCH_MNT
354         then 
355             echo "\$SCRATCH_DEV is mounted but not on \$SCRATCH_MNT - aborting"
356             exit 1
357         fi
358         
359         # and then unmount it
360     
361         if ! umount $SCRATCH_DEV
362         then
363             echo "failed to unmount $SCRATCH_DEV"
364             exit 1
365         fi
366     fi
367     
368     # should be ok now
369
370 }
371
372 # this test needs a logdev 
373
374 _require_logdev()
375 {
376     if [ -z "$SCRATCH_LOGDEV" -o ! -b "$SCRATCH_LOGDEV" ]
377     then
378         _notrun "This test requires a valid \$SCRATCH_LOGDEV" 
379     fi
380 }
381
382 # this test requires loopback device support
383
384 _require_loop()
385 {
386     if grep loop /proc/devices >/dev/null 2>&1
387     then
388         :
389     else
390         _notrun "This test requires loopback device support"
391     fi
392 }
393
394
395 # check that a FS is mounted as XFS. if so, return mount point
396 #
397 _xfs_mounted()
398 {
399     if [ $# -ne 1 ]
400     then
401         echo "Usage: _xfs_mounted device" 1>&2
402         exit 1
403     fi
404     
405     device=$1
406     
407     if mount | grep $device | $AWK_PROG '
408         /type xfs/  { print $3 ; exit 0 }
409         END         { exit 1 }
410     '
411     then
412         echo "_xfs_mounted: $device is not a mounted XFS FS"
413         exit 1
414     fi
415 }
416
417 # remount a FS to a new mode (ro or rw)
418 #
419
420 _remount()
421 {
422     if [ $# -ne 2 ]
423     then
424         echo "Usage: _remount device ro/rw" 1>&2
425         exit 1
426     fi
427     device=$1
428     mode=$2
429     
430     if ! mount -o remount,$mode $device
431     then
432         echo "_remount: failed to remount filesystem on $device as $mode"
433         exit 1
434     fi
435     
436     # we might like to check here later
437     #options=`_fs_options $device`
438
439 }
440
441 # run xfs_check on a FS. 
442 #
443 # if the filesystem is mounted, it's either remounted ro before being
444 # checked or it's unmounted and then remounted
445 #
446
447 USE_REMOUNT=0
448
449 _check_fs()
450 {
451     if [ $# -ne 1 ]
452     then
453         echo "Usage: _check_fs device" 1>&2
454         exit 1
455     fi
456     
457     device=$1
458     type=`_fs_type $device`
459     ok=1
460     
461     if [ "$type" = "xfs" ]
462     then
463         # mounted... 
464         
465         if [ $USE_REMOUNT -eq 0 ]
466         then
467             mountpoint=`_xfs_mounted $device`
468             umount $device
469         else
470             _remount $device ro
471         fi
472     fi
473
474     xfs_logprint -t $device 2>&1 | tee $tmp.fs_check | grep -q "<CLEAN>"
475     if [ $? -ne 0 ]
476     then
477         echo "_check_fs: filesystem on $device has dirty log (see $seq.full)"
478         
479         echo "_check_fs: filesystem on $device has dirty log"   >>$seq.full
480         echo "*** xfs_logprint -t output ***"                   >>$seq.full
481         cat $tmp.fs_check                                       >>$seq.full
482         echo "*** end xfs_logprint output"                      >>$seq.full
483         
484         ok=0
485     fi
486         
487     
488     xfs_check $device 2>&1 | _fix_malloc >$tmp.fs_check 
489     if [ -s $tmp.fs_check ]
490     then
491         echo "_check_fs: filesystem on $device is inconsistent (c) (see $seq.full)"
492         
493         echo "_check_fs: filesystem on $device is inconsistent" >>$seq.full
494         echo "*** xfs_check output ***"                         >>$seq.full
495         cat $tmp.fs_check                                       >>$seq.full
496         echo "*** end xfs_check output"                         >>$seq.full
497         
498         ok=0
499     fi
500     
501     if ! xfs_repair -n $device >$tmp.fs_check 2>&1
502     then
503         echo "_check_fs: filesystem on $device is inconsistent (r) (see $seq.full)"
504         
505         echo "_check_fs: filesystem on $device is inconsistent" >>$seq.full
506         echo "*** xfs_repair -n output ***"                     >>$seq.full
507         cat $tmp.fs_check | _fix_malloc                         >>$seq.full
508         echo "*** end xfs_repair output"                        >>$seq.full
509         
510         ok=0
511     fi
512     rm -f $tmp.fs_check
513     
514     if [ $ok -eq 0 ]
515     then
516         echo "*** mount output ***"                             >>$seq.full
517         mount                                                   >>$seq.full
518         echo "*** end mount output"                             >>$seq.full
519     fi
520     
521     if [ "$type" = "xfs" ]
522     then
523         # mounted... 
524         if [ $USE_REMOUNT -eq 0 ]
525         then
526             if ! mount -t xfs $device $mountpoint
527             then
528                 echo "!!! failed to remount $device on $mountpoint"
529                 ok=0
530             fi
531         else
532             _remount $device rw
533         fi
534     fi
535     
536     [ $ok -eq 0 ] && exit 1
537     return 0    
538 }
539
540 ################################################################################
541
542 [ -d /usr/bsd ] && PATH=$PATH:/usr/bsd
543 [ -d /usr/freeware/bin ] && PATH=$PATH:/usr/freeware/bin
544
545 if [ "$iam" != new ]
546 then
547
548     # make some further configuration checks here
549     
550     if [ "$TEST_DEV" = ""  ]
551     then
552         echo "common.rc: Error: \$TEST_DEV is not set"
553         exit 1
554     fi
555     
556     if [ "`_fs_type $TEST_DEV`" != "xfs" ]
557     then
558         echo "common.rc: Error: \$TEST_DEV ($TEST_DEV) is not a MOUNTED XFS filesystem"
559         df -T $TEST_DEV
560         exit 1
561     fi
562
563 fi
564
565 # check for some required biunaries on our $PATH
566 #
567 for exec in mkfs.xfs xfs_logprint xfs_check xfs_repair xfs_db
568 do
569     if which $exec >/dev/null 2>&1 
570     then
571         :
572     else
573         echo "common.rc: cannot find $exec on \$PATH=$PATH"
574         exit 1
575     fi
576 done
577
578 # make sure this script returns success
579 /bin/true