Prevent bandwidth script from non-existing iface
authorNeutron Soutmun <neo.neutron@gmail.com>
Mon, 20 Jul 2009 08:01:36 +0000 (15:01 +0700)
committerNeutron Soutmun <neo.neutron@gmail.com>
Mon, 20 Jul 2009 08:01:36 +0000 (15:01 +0700)
  * Prevent the bandwidth script from non-existing interface name.
  * State the return message "NOT COMPLETED" from bandwidth script is error,
    which need to do something such as in the add and remove bandwidth slot to
    retry for 3 times when this error occured before give up.

src/rh-task-bandwidth.c
tools/rahunas-bandwidth.in

index e147c27..f07876b 100644 (file)
@@ -126,6 +126,9 @@ int bandwidth_exec(struct vserver *vs, char *const args[])
     } 
   }
 
+  if ((buffer != NULL) && (strncmp (buffer, "NOT COMPLETED", 13) == 0))
+    ret = -2;  // Not complete need to retry
+
   close(exec_pipe[0]);
   close(exec_pipe[1]);
   return ret;
@@ -292,8 +295,10 @@ static int startsess (struct vserver *vs, struct task_req *req)
     snprintf(bw_req.slot_id, sizeof (bw_req.slot_id), "%d", slot_id);
     if (bandwidth_add(vs, &bw_req) == 0)
       break;
-    else
+    else {
       max_try--;
+      sleep (1); 
+    }
   }
 
 
index 6904886..a97fb39 100755 (executable)
@@ -30,7 +30,7 @@ test -f $RAHUNAS_CONFIG || exit 1
 SHAPING_DOWN_INF=""
 SHAPING_UP_INF=""
 
-set -e
+MSG_NOT_COMPLETED="NOT COMPLETED"
 
 get_config_value () {
   section=$1
@@ -190,48 +190,76 @@ bw_del() {
   $TC class del dev $SHAPING_UP_INF parent 2:${INTERFACE_ID} classid 2:${ID}
 }
 
+chk_interface () {
+  SHAPING_DOWN_INF=$1
+  SHAPING_UP_INF=$2
+
+  iface_list=`$IFCONFIG -a | grep "Link encap" | awk '{print $1}'`
+
+  echo $iface_list | grep -w "$SHAPING_DOWN_INF" > /dev/null 2>&1
+  if [ $? -ne 0 ]; then
+    return 1
+  fi
+
+  echo $iface_list | grep -w "$SHAPING_UP_INF" > /dev/null 2>&1
+  if [ $? -ne 0 ]; then
+    return 1
+  fi
+
+  return 0
+}
+
 N=@sysconfdir@/rahunas/bandwidth.sh
 
 case "$1" in
   start)
     if [ -z "$2" ] || [ -z "$3" ]; then
-      MESSAGE="NOT COMPLETED"  
+      MESSAGE="$MSG_NOT_COMPLETED"  
     else
-      SHAPING_DOWN_INF=$2
-      SHAPING_UP_INF=$3
-      RUN=${RUN}-${SHAPING_DOWN_INF}-${SHAPING_UP_INF}
-      start || true 
+      chk_interface $2 $3
+      if [ $? -eq 0 ]; then
+        RUN=${RUN}-${SHAPING_DOWN_INF}-${SHAPING_UP_INF}
+        start 
+      else
+        MESSAGE="$MSG_NOT_COMPLETED"
+      fi
     fi
 
-    test -n "$MESSAGE" || MESSAGE="NOT COMPLETED"
+    test -n "$MESSAGE" || MESSAGE="$MSG_NOT_COMPLETED"
     echo $MESSAGE
     ;;
   stop)
     if [ -z "$2" ] || [ -z "$3" ]; then
-      MESSAGE="NOT COMPLETED"  
+      MESSAGE="$MSG_NOT_COMPLETED"  
     else
-      SHAPING_DOWN_INF=$2
-      SHAPING_UP_INF=$3
-      RUN=${RUN}-${SHAPING_DOWN_INF}-${SHAPING_UP_INF}
-      stop || true
+      chk_interface $2 $3
+      if [ $? -eq 0 ]; then
+        RUN=${RUN}-${SHAPING_DOWN_INF}-${SHAPING_UP_INF}
+        stop 
+      else
+        MESSAGE="$MSG_NOT_COMPLETED"
+      fi
     fi
 
-    test -n "$MESSAGE" || MESSAGE="NOT COMPLETED"
+    test -n "$MESSAGE" || MESSAGE="$MSG_NOT_COMPLETED"
     echo $MESSAGE
 
     ;;
   restart)
     if [ -z "$2" ] || [ -z "$3" ]; then
-      MESSAGE="NOT COMPLETED"  
+      MESSAGE="$MSG_NOT_COMPLETED"  
     else
-      SHAPING_DOWN_INF=$2
-      SHAPING_UP_INF=$3
-      RUN=${RUN}-${SHAPING_DOWN_INF}-${SHAPING_UP_INF}
-      stop || true
-      start || true
+      chk_interface $2 $3
+      if [ $? -eq 0 ]; then
+        RUN=${RUN}-${SHAPING_DOWN_INF}-${SHAPING_UP_INF}
+        stop 
+        start 
+      else
+        MESSAGE="$MSG_NOT_COMPLETED"
+      fi
     fi
 
-    test -n "$MESSAGE" || MESSAGE="NOT COMPLETED"
+    test -n "$MESSAGE" || MESSAGE="$MSG_NOT_COMPLETED"
     echo $MESSAGE
 
     ;;
@@ -248,12 +276,15 @@ case "$1" in
     fi
 
     if [ -z "$6" ] || [ -z "$7" ]; then
-      MESSAGE="NOT COMPLETED"  
+      MESSAGE="$MSG_NOT_COMPLETED"  
     else
-      SHAPING_DOWN_INF=$6
-      SHAPING_UP_INF=$7
-      RUN=${RUN}-${SHAPING_DOWN_INF}-${SHAPING_UP_INF}
-      bw_add $2 $3 $4 $5 || true
+      chk_interface $6 $7
+      if [ $? -eq 0 ]; then
+        RUN=${RUN}-${SHAPING_DOWN_INF}-${SHAPING_UP_INF}
+        bw_add $2 $3 $4 $5 
+      else
+        MESSAGE="$MSG_NOT_COMPLETED"
+      fi
     fi
     echo $MESSAGE
     ;;
@@ -269,12 +300,15 @@ case "$1" in
       exit 1
     fi
     if [ -z "$3" ] || [ -z "$4" ]; then
-      MESSAGE="NOT COMPLETED"  
+      MESSAGE="$MSG_NOT_COMPLETED"  
     else
-      SHAPING_DOWN_INF=$3
-      SHAPING_UP_INF=$4
-      RUN=${RUN}-${SHAPING_DOWN_INF}-${SHAPING_UP_INF}
-      bw_del $2 || true
+      chk_interface $3 $4
+      if [ $? -eq 0 ]; then
+        RUN=${RUN}-${SHAPING_DOWN_INF}-${SHAPING_UP_INF}
+        bw_del $2 
+      else
+        MESSAGE="$MSG_NOT_COMPLETED"
+      fi
     fi
     echo $MESSAGE
     ;;