1#!/bin/sh
2# $1 = `USB_approved_device*`, where `*` is the number which one should be checked.
3
4test_usb_busy()
5{
6	sync
7
8	fuser -m "$1" 1> /dev/dull
9	if [ $? -eq 0 ]; then
10		# one access is found at least
11		return 1
12	else
13		# no process is accessing this device
14		return 0
15	fi
16}
17
18str="`/bin/config get $1`"
19volume="`echo "$str" | cut -d'*' -f1`"
20device="`echo "$str" | cut -d'*' -f2`"
21# take off all space characters at tail of `device`
22device="`echo $device`"
23dir="/tmp/mnt/"
24RESULT="/tmp/testbusy_result"
25
26[ -f $RESULT ] && /bin/rm $RESULT
27
28if [ $# -eq 0 ] ; then
29	test_usb_busy $dir
30	[ $? -eq 0 ] && echo 0 > $RESULT || echo 1 > $RESULT
31	exit
32fi
33
34/bin/config show | grep shared_usb_folder | while read tmp
35do
36	if [ "X$tmp" = "X" ]; then
37		test_usb_busy $dir
38		[ $? -eq 0 ] && echo 0 > $RESULT || echo 1 > $RESULT
39		exit
40	fi
41
42	vol="`echo $tmp | cut -d'*' -f5`"
43	dev="`echo $tmp | cut -d'*' -f6`"
44	if [ "X$vol" != "X$volume" -o "X$dev" != "X$device" ]; then
45		continue
46	fi
47
48	num=`echo $tmp | cut -c18`
49	count=0
50	for temp in `ls $dir`
51	do
52		if [ "$count" = "$num" ]; then
53			# pre is the prefix of device name, such as 'sda', 'sdb', etc.
54			pre="`echo $temp | cut -c1-3`"
55			break
56		fi
57		let count=$count+1
58	done
59
60	sig=0
61	for temp in `ls $dir`
62	do
63		if [ "X`echo $temp | cut -c1-3`" = "X$pre" ]; then
64			test_usb_busy "$dir$temp"
65			if [ $? -ne 0 ] ; then
66				sig=1
67				break
68			fi
69		fi
70	done
71	[ $sig -eq 0 ] && echo 0 > $RESULT || echo 1 > $RESULT
72	exit
73done
74