[PATCH 3/9] tests: remotehost add execute_stop()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Before we have to kill application we start
in the thread - in most cases using
killall and sometimes kill other applicantions
eg. tcpdump, iper, iperf3, tshark

With this patch we are able to stop/kill
single application/thread instead, base
on pid file.

Signed-off-by: Janusz Dziedzic <janusz.dziedzic@xxxxxxxxx>
---
 tests/hwsim/remotehost.py | 58 ++++++++++++++++++++++++++++++++++++---
 1 file changed, 54 insertions(+), 4 deletions(-)

diff --git a/tests/hwsim/remotehost.py b/tests/hwsim/remotehost.py
index b87b58936..a1ef1f34b 100644
--- a/tests/hwsim/remotehost.py
+++ b/tests/hwsim/remotehost.py
@@ -8,6 +8,7 @@ import logging
 import subprocess
 import threading
 import tempfile
+import os
 
 logger = logging.getLogger()
 
@@ -34,6 +35,17 @@ def execute_thread(command, reply):
     reply.append(status)
     reply.append(buf)
 
+def gen_reaper_file(conf):
+    fd, filename = tempfile.mkstemp(dir='/tmp', prefix=conf + '-')
+    f = os.fdopen(fd, 'w')
+
+    f.write("#!/bin/sh\n")
+    f.write("name=\"$(basename $0)\"\n")
+    f.write("echo $$ > /tmp/$name.pid\n")
+    f.write("exec \"$@\"\n");
+
+    return filename;
+
 class Host():
     def __init__(self, host=None, ifname=None, port=None, name="", user="root"):
         self.host = host
@@ -86,17 +98,55 @@ class Host():
         return status, buf.decode()
 
     # async execute
-    def execute_run(self, command, res):
+    def execute_run(self, command, res, use_reaper=True):
+        if use_reaper:
+            filename = gen_reaper_file("reaper")
+            self.send_file(filename, filename)
+            self.execute(["chmod", "755", filename])
+            _command = [filename] + command
+        else:
+            filename = ""
+            _command = command
+
         if self.host is None:
-            cmd = command
+            cmd = _command
         else:
-            cmd = ["ssh", self.user + "@" + self.host, ' '.join(command)]
+            cmd = ["ssh", self.user + "@" + self.host, ' '.join(_command)]
         _cmd = self.name + " execute_run: " + ' '.join(cmd)
         logger.debug(_cmd)
-        t = threading.Thread(target=execute_thread, args=(cmd, res))
+        t = threading.Thread(target=execute_thread, name=filename, args=(cmd, res))
         t.start()
         return t
 
+    def execute_stop(self, t):
+        if t.name.find("reaper") == -1:
+            raise Exception("use_reaper required")
+
+        pid_file = t.name + ".pid"
+
+        if t.isAlive():
+            cmd = ["kill `cat " + pid_file + "`"]
+            self.execute(cmd)
+
+        # try again
+        self.wait_execute_complete(t, 5)
+        if t.isAlive():
+            cmd = ["kill `cat " + pid_file + "`"]
+            self.execute(cmd)
+
+        # try with -9
+        self.wait_execute_complete(t, 5)
+        if t.isAlive():
+            cmd = ["kill -9 `cat " + pid_file + "`"]
+            self.execute(cmd)
+
+        self.wait_execute_complete(t, 5)
+        if t.isAlive():
+            raise Exception("thread still alive")
+
+        self.execute(["rm", pid_file])
+        self.execute(["rm", t.name])
+
     def wait_execute_complete(self, t, wait=None):
         if wait == None:
             wait_str = "infinite"
-- 
2.17.1


_______________________________________________
Hostap mailing list
Hostap@xxxxxxxxxxxxxxxxxxx
http://lists.infradead.org/mailman/listinfo/hostap



[Index of Archives]     [Linux Wireless]     [Linux Kernel]     [ATH6KL]     [Linux Bluetooth]     [Linux Netdev]     [Kernel Newbies]     [IDE]     [Security]     [Git]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux ATA RAID]     [Samba]     [Device Mapper]

  Powered by Linux