1#! /bin/sh
2
3TMP_UMOUNT_TABLE=/tmp/umounting_table_fgd456
4TMP_DEVICE_NAME=/tmp/mounting_divice_name
5TMP_DATA_XYZ=/tmp/proc_partition_dev
6DISK_FIND_TABLE=/tmp/find_disk_abcxzy
7UPDATE_NAME=/tmp/update_name
8
9dev_inproc(){
10	rm -f $TMP_DATA_XYZ
11	cat /proc/partitions | awk '{print $4}' | grep sd >$TMP_DATA_XYZ
12	while read LINE
13	do
14		dev=`echo "$1" | cut -c 10-`
15		if [ "$LINE" = "$dev" ];then
16			return 0
17			break
18		fi
19	done<$TMP_DATA_XYZ
20	return 1
21}
22
23# $1--->umounting dir
24isdafake(){
25	
26	mount | awk '{print $1}' | grep ^/dev/sd | cut -c 6->$TMP_DEVICE_NAME
27	divnam=`echo "$1" | awk -F/ '{print $NF}'`
28	sleep 1
29	while read LINE
30	do
31		if [ "$LINE" = "$divnam" ];then
32			return 1
33			break
34		fi
35	done<$TMP_DEVICE_NAME
36	return 0
37}
38
39# $---> sda1
40print_log()
41{
42        if [ `length $1` != '4' ];then
43                return
44        fi
45
46	sata_disk_name=`/bin/config get sata_diskname`
47
48        device_name=`cat $DISK_FIND_TABLE | grep $1 | awk -F* '{print $2}'`
49	volume_name=`cat $DISK_FIND_TABLE | grep $1 | awk -F* '{{{i=NF-3; a="*";while(i!=0) {printf $(NF-i); if(i!=1) printf a; i--}}}}'`
50
51	if [ "x$sata_disk_name" = "x$1" ]; then
52		logger "[External disk detached] The External disk $deivce_name($volume_name) is removed from the router"
53		return
54	else
55		logger "[USB device detached] The USB storage device $device_name($volume_name) is removed from the router"
56		return
57	fi
58
59	volume_name=`/usr/sbin/vol_id -L /dev/$1`
60	if [ ! -n "$volume_name" ]; then
61	       volume_name=$1
62	fi
63	logger "[USB device detached] The USB storage device $1($volume_name) is removed from the router"
64	return
65}
66
67
68# $1--->upper dir (/tmp/mnt or /tmp/ftpgust/shares)
69umount_all()
70{
71	umount_flag=0
72	RESULT=/tmp/umount_result
73
74	sync
75	sync
76	
77	rm -f $RESULT
78
79	upper_dir="$1"
80	
81	cd $upper_dir
82	if [ $? -ne 0 ]; then
83		return
84	fi
85	
86	for i in * ; do
87		if [ "$i" = "*" ];then
88			# if there are no sub folders in the cur folder,"i" var would be '*',
89			# and ignore it and jump over
90			continue
91		fi
92
93		c=0
94		while [ $c -le 3 ]; do
95			/usr/sbin/detach_afp_shares
96			sync
97			umount $upper_dir/"$i"
98			if [ $? -eq 0 ]; then
99				[ $upper_dir = "/tmp/mnt" ] && print_log $1
100				break;
101			fi
102			let c=$c+1
103		done
104		umount_path="$upper_dir/$i"
105		if [ $c -eq 4 ]; then
106			if dev_inproc "$umount_path"
107			then
108				umount_flag=1
109				#echo 1 > $RESULT
110				#exit 1
111				continue
112			else
113				umount "$upper_dir" -f
114				if [ $? -eq 0 ]; then
115					[ $upper_dir = "/tmp/mnt" ] && print_log $1
116				fi
117			fi
118		fi
119		rmdir "$i"
120	done	
121	# when there are NOT any disks, we should set 0 to /tmp/umount_result
122	if [ "x$(/bin/df | grep sd)" != "x"  -a $umount_flag -eq 1 ]; then
123		echo 1 > $RESULT
124	else
125		echo 0 > $RESULT
126		ledcontrol -n sata -c green -s off
127		ledcontrol -n usb1 -c green -s off
128		ledcontrol -n usb3 -c green -s off
129	fi
130}
131
132umount_part()
133{
134 # call from hotplug2
135	RESULT=/tmp/umount_result
136	
137	sync
138	sync
139
140	rm -f $RESULT
141	
142	umount_dir="$1"
143
144	if isdafake "$umount_dir"
145	then
146		return
147	fi
148
149	c=0
150	while [ $c -le 3 ]; do
151		sync
152		/usr/sbin/detach_afp_shares "$umount_dir"
153		sync
154		umount "$umount_dir"
155		if [ $? -eq 0 ];then
156			break
157		fi
158		let c=$c+1
159	done
160	if [ $c -eq 4 ]; then
161		# only when disk is also in /proc/partition file,
162		# in other words,the disk is not unpluged ,exit directly
163		if dev_inproc "$umount_dir"
164		then
165			echo 1 > $RESULT
166			exit 1
167		else
168			umount "$umount_dir" -f
169		fi
170	fi
171	rmdir "$umount_dir"
172	if [ $? -ne 0 ];then
173			# kill some possible tasks who is accessing usb disk to block umount command
174			#/etc/init.d/samba stop; FIXME: commented by ASL
175			kill -9 `pidof minidlna`
176			sleep 1
177			/usr/sbin/detach_afp_shares "$umount_dir"
178			sync
179			umount "$umount_dir"
180			rmdir "$umount_dir"
181	fi
182
183}
184
185check_umount_disk_led()
186{
187	# USB disk
188	disk_name=`echo $1 | sed 's/[0-9]//g'`
189	ls -l /sys/block | grep sd | awk '{print $9}' > /tmp/disk_rm
190	while read LINE
191	do
192		#cd /sys/block/sda; ls -l
193		#../../devices/platform/ipq-dwc3.0/dwc3.0/xhci-hcd.0/usb1/1-1/1-1:1.0/host4/target4:0:0/4:0:0:0
194		#../../devices/platform/ipq-dwc3.1/dwc3.1/xhci-hcd.1/usb3/4-1/4-1:1.0/host3/target3:0:0/3:0:0:0
195		# To get usb1 or usb3
196		#usb_port=$(ls -l /sys/block/$LINE | grep usb | awk '{print $11}' | awk -F/ '{print $8}')
197		#usb_name=`echo $usb_port | sed 's/[0-9]//g'`
198		#usb_num=`echo $usb_port | cut -c 4-`
199		if [ "x$LINE" = "x$disk_name" ]; then
200			usb_interface=$(ls -l /sys/block/$LINE | grep usb | awk '{print $11}' | awk -F/ '{print $6}')
201			if [ "x$usb_interface" != "x" ]; then
202				if [ "$usb_interface" = "dwc3.0" ]; then
203					ledcontrol -n usb1 -c green -s off
204				elif [ "$usb_interface" = "dwc3.1" ]; then
205					ledcontrol -n usb3 -c green -s off
206				fi
207			fi
208		fi
209	done</tmp/disk_rm
210	rm /tmp/disk_rm
211
212	#eSATA disk.
213	sata_disk_name=`/bin/config get sata_diskname`
214	if [ "x$sata_disk_name" = "x$disk_name" ]; then
215		ledcontrol -n sata -c green -s off
216	fi
217
218}
219
220# $1--->device name(sda1 or sdb3 or sdc3)
221# $2--->upper dir for ftp(ftpadmin or ftpguest)
222umount_part_ftp()
223{
224	devnam=$1
225	up_dir=$2
226	
227	mount | grep '^/dev/$devnam\>' | awk '{print $3}' | grep ^/tmp/$up_dir | sed -e 's/\\040/ /g'>$TMP_UMOUNT_TABLE
228	while read LINE
229	
230	do
231		umount_part "$LINE"
232	done<$TMP_UMOUNT_TABLE
233
234}
235
236ftp_down_upload()
237{
238       RESULT=/tmp/umount_result
239       LOAD=/tmp/ftp_load
240       rm $LOAD
241       ftp_load=0
242       /usr/sbin/ftpload
243       if [ -f $LOAD ]; then
244		ftp_load=`cat $LOAD`
245		if [ $ftp_load -gt 0 ]; then
246			echo 1 >$RESULT
247			exit 1
248		fi	
249       fi
250}
251
252service_stop()
253{
254	killall minidlna
255	killall -9 smbd
256}
257
258service_stop
259
260if [ $# -eq 0 ]; then
261	echo "Stop greendownload..." > /dev/console
262	green_download.sh stop
263	ftp_down_upload
264	umount_all "/tmp/ftpadmin/shares"
265#	umount_all "/tmp/ftpguest/shares"
266	umount_all "/tmp/mnt"
267#	logger "[USB device detached] ALL USB storage device is removed from the router"
268else
269	echo "Detect if need to stop greendownload..." > /dev/console
270	green_download.sh stop $1
271	ftp_down_upload
272	umount_part_ftp "$1" "ftpadmin"
273#	umount_part_ftp "$1" "ftpguest"
274	umount_part "/tmp/mnt/$1"
275	sleep 2
276	check_umount_disk_led $1
277	print_log $1
278fi
279
280echo `date` stop smbd at hotplug2.umount >> /tmp/debug_smb
281/usr/sbin/update_smb
282/usr/sbin/update_afp
283/sbin/cmdftp start
284/sbin/cmddlna start
285# in order to run this after run update_smb, moved it here
286test -f $RESULT || echo 0 > $RESULT
287
288df |grep sd |awk '{print $1}' |cut -c 6- >$UPDATE_NAME
289while read LINE
290do
291	update_smb $LINE
292	sleep 1
293done<$UPDATE_NAME
294