1#!/bin/sh 2 3# Usage: 4# 5# In case of 'Safely Remove' - 6# 1. /usr/sbin/detach_afp_shares 7# 2. /usr/sbin/detach_afp_shares <mount_point> 8# 9# In case of 'Forcefully Remove' - 10# 3. /usr/sbin/detach_afp_shares "hotplug2" 11# 4. /usr/sbin/detach_afp_shares <mount_point> "hotplug2" 12 13if [ $# -eq 0 ]; then 14 echo "No Arg" 15elif [ $# -eq 1 ]; then 16 echo "Arg 1 : $1" 17elif [ $# -eq 2 ]; then 18 echo "Arg 1 : $1" 19 echo "Arg 2 : $2" 20fi 21 22TMP_AFP_SHARE_LOCK="/tmp/tmp_afp_share_lock" 23 24# Functions 25check_afp_share_locked() 26{ 27 if [ -f ${TMP_AFP_SHARE_LOCK} ]; then 28 return 1 29 else 30 return 0 31 fi 32} 33 34hold_lock() 35{ 36 # Check lock file 37 currenttime=`date +%s` 38 check_afp_share_locked 39 while [ $? -ne 0 ]; do 40 newtime=`date +%s` 41 # The longest waiting time is 30s, avoid endless waiting 42 if [ `expr ${newtime} - ${currenttime}` -gt 30 ]; then 43 exit 1 44 fi 45 sleep 1 46 check_afp_share_locked 47 done 48 49 # Create lock file 50 touch ${TMP_AFP_SHARE_LOCK} 51} 52 53release_lock() 54{ 55 rm -f ${TMP_AFP_SHARE_LOCK} 56} 57 58# Either Safely remove all devices from Web UI or unplug all devices forcefully 59if [ $# -eq 0 ] || [ $# -eq 1 -a "$1" = "hotplug2" ]; then 60 echo "===> Removing all" 61 62 # Reload master afpd and all smbd processes, as volumes are changed 63 hold_lock 64 # Clear all mount entry from AppleVolumes.default 65 echo > /etc/netatalk/AppleVolumes.default 66 67 # Send HUP signal to master afpd processes, as volumes are changed 68 /bin/kill -HUP `cat /var/run/afpd.pid` 69 sleep 1 70 release_lock 71 72 # Kill all the afpd processes 73 echo "====> Killing all afpd." 74 75 # In case of 'Safely Remove' 76 if [ $# -eq 0 ]; then 77 # Kill all the afpd processes 78 killall afpd 79 80 # Wait until all afpd processes are died 81 while [ 1 ] 82 do 83 status=`ps | grep afpd | grep -v grep` 84 if [ $? -ne 0 ]; then 85 echo "======> no afpd running" 86 break 87 else 88 echo "======> afpd running" 89 sleep 1 90 fi 91 done 92 93 # In case of 'Forcefully Remove' 94 else 95 # Kill all the afpd processes 96 killall -KILL afpd 97 fi 98 99 # Kill all the cnid_dbd processes 100 echo "====> Killing all cnid_dbd." 101 102 # In case of 'Safely Remove' 103 if [ $# -eq 0 ]; then 104 # Kill all the cnid_dbd processes 105 killall cnid_dbd 106 107 # Wait until all cnid_dbd processes are died 108 while [ 1 ] 109 do 110 status=`ps | grep cnid_dbd | grep -v grep` 111 if [ $? -ne 0 ]; then 112 echo "======> no cnid_dbd running" 113 break 114 else 115 echo "======> cnid_dbd running" 116 sleep 1 117 fi 118 done 119 120 # In case of 'Forcefully Remove' 121 else 122 # Kill all the cnid_dbd processes 123 killall -KILL cnid_dbd 124 fi 125 126# Either Safely remove one of the devices from Web UI or unplug one of the devices forcefully 127elif [ $# -eq 1 -a "$1" != "hotplug2" ] || [ $# -eq 2 -a "$2" = "hotplug2" ]; then 128 echo "===> Removing $1" 129 mount_point=$1 130 131 # Reload master afpd and all smbd processes, as volumes are changed 132 hold_lock 133 # Clear specific mount entry from AppleVolumes.default 134 sed '/^\/mnt\/'${mount_point}'\/.*$/d' /etc/netatalk/AppleVolumes.default > /tmp/AppleVolumes.default.tmp 135 cat /tmp/AppleVolumes.default.tmp > /etc/netatalk/AppleVolumes.default 136 rm -f /tmp/AppleVolumes.default.tmp 137 138 # Send HUP signal to master afpd, as volumes are changed 139 /bin/kill -HUP `cat /var/run/afpd.pid` 140 sleep 1 141 release_lock 142 143 # Kill all the specific afpd processes 144 afppids=`ps | grep afpd | grep -v grep | awk '{print $1}'` 145 for afppid in $afppids 146 do 147 status=`ls -l /proc/$afppid/cwd | awk '{print $11}' | grep -o "$mount_point[ ]*$\|$mount_point/"` 148 if [ $? -eq 0 ]; then 149 echo "======> Killing afpd with pid: $afppid" 150 151 # In case of 'Safely Remove' 152 if [ $# -eq 1 ]; then 153 # Send 'TERM' signal, as the afpd is not required 154 /bin/kill -TERM $afppid 155 156 # Wait until the specific afpd process is died 157 while [ 1 ] 158 do 159 status=`ls -l /proc/$afppid/cwd | awk '{print $11}' | grep -o "$mount_point[ ]*$\|$mount_point/"` 160 if [ $? -ne 0 ]; then 161 echo "======> no afpd running" 162 break 163 else 164 echo "======> afpd running with pid: $afppid" 165 sleep 1 166 fi 167 done 168 169 # In case of 'Forcefully Remove' 170 else 171 # Send 'KILL' signal, as the afpd is not required 172 /bin/kill -KILL $afppid 173 fi 174 fi 175 done 176 177 # Kill all the specific cnid_dbd processes 178 cnidpids=`ps | grep cnid_dbd | grep "$mount_point/" | grep -v grep | awk '{print $1}'` 179 for cnidpid in $cnidpids 180 do 181 echo "=====> Killing cnid_dbd with pid: $cnidpid" 182 183 # In case of 'Safely Remove' 184 if [ $# -eq 1 ]; then 185 # Send 'TERM' signal, as the cnid_dbd is not required 186 /bin/kill -TERM $cnidpid 187 188 # Wait until the specific cnid_dbd process is died 189 while [ 1 ] 190 do 191 status=`ps | grep cnid_dbd | grep "$mount_point/" | grep -v grep` 192 if [ $? -ne 0 ]; then 193 echo "======> no cnid_dbd running" 194 break 195 else 196 echo "======> cnid_dbd running with pid: $cnidpid" 197 sleep 1 198 fi 199 done 200 201 # In case of 'Forcefully Remove' 202 else 203 # Send 'KILL' signal, as the cnid_dbd is not required 204 /bin/kill -KILL $cnidpid 205 fi 206 done 207else 208 echo "======> nothing to be done" 209fi 210