Add tct/resource_locking.sh
authorAleksander Mistewicz <a.mistewicz@samsung.com>
Fri, 18 Mar 2016 15:07:03 +0000 (16:07 +0100)
committerAleksander Mistewicz <a.mistewicz@samsung.com>
Fri, 13 May 2016 12:09:43 +0000 (14:09 +0200)
Change-Id: I9526afee6ca0776fe32960dba9fa2ee522742909
Signed-off-by: Aleksander Mistewicz <a.mistewicz@samsung.com>
tct/resource_locking.sh [new file with mode: 0755]

diff --git a/tct/resource_locking.sh b/tct/resource_locking.sh
new file mode 100755 (executable)
index 0000000..7e6478d
--- /dev/null
@@ -0,0 +1,40 @@
+#!/bin/sh
+
+# Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Script used to lock a file used for storing UUID.
+# Prints on stdout name of the locked SDMUX.
+#
+# Syntax is: resource_locking.sh ${SDMUX%_*}
+#
+# Author: Aleksander Mistewicz <a.mistewicz@samsung.com>
+
+SLEEP=100
+while [ $SLEEP -ge 10 ]
+do
+    for i in $(ls "/var/tmp/uuid-${1}"* | grep -v "lock");
+    do
+        lockfile-create --use-pid --retry 0 $i
+        if [ $? -eq 0 ]; then
+            echo "Locked: $i" >&2
+            echo "${i##*-}"
+            exit
+        fi
+        echo "Skipped: $i" >&2
+    done
+    SLEEP=$((SLEEP-2))
+    RAND=$(od -An -N1 -t u1 /dev/urandom)
+    sleep $(($RAND % $SLEEP + 5))
+done