diff mbox

[22/25] iotests: Add _timeout function

Message ID 1424887718-10800-23-git-send-email-mreitz@redhat.com
State New
Headers show

Commit Message

Max Reitz Feb. 25, 2015, 6:08 p.m. UTC
Add a function for executing a command with a timeout.

Signed-off-by: Max Reitz <mreitz@redhat.com>
---
 tests/qemu-iotests/common.rc | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
diff mbox

Patch

diff --git a/tests/qemu-iotests/common.rc b/tests/qemu-iotests/common.rc
index 22d3514..bd8453a 100644
--- a/tests/qemu-iotests/common.rc
+++ b/tests/qemu-iotests/common.rc
@@ -489,5 +489,21 @@  _die()
         exit 1
 }
 
+_timeout()
+{
+    local timeout=1
+    local pid
+
+    if [ "$1" = "-t" ]; then
+        shift
+        timeout=$1
+        shift
+    fi
+    "$@" &
+    pid=$!
+    (sleep $timeout; kill -9 $pid &> /dev/null) &
+    wait -n $pid $!
+}
+
 # make sure this script returns success
 true