This patch fixes the problem with capturing data on configured
interfaces. The problem is that we start tcpdump inside shell. When we
stop the capture we call Popen.terminate() but this terminates the
shell process, not the tcpdump process. Until it's terminated the data are
not flushed to capture file and we transfer empty file to controller.
The fix for this problem is running without shell.
Additionally I'm removing the quotation marks around pcap filter as they
are not required and cause unnecessary entry in log when it is empty.
The last change in the patch is moving redirection of stderr to the code
where Popen is called. This is a must because of using shell=False.
Resolves #150
Signed-off-by: Jan Tluka <jtluka(a)redhat.com>
---
lnst/Common/PacketCapture.py | 10 ++++++----
1 file changed, 6 insertions(+), 4 deletions(-)
diff --git a/lnst/Common/PacketCapture.py b/lnst/Common/PacketCapture.py
index f45dc99..2717200 100644
--- a/lnst/Common/PacketCapture.py
+++ b/lnst/Common/PacketCapture.py
@@ -11,6 +11,7 @@ rpazdera(a)redhat.com (Radek Pazdera)
"""
import subprocess
+import os
class PacketCapture:
""" Capture/handle traffic that goes through a specific
@@ -49,14 +50,15 @@ class PacketCapture:
output_file = self._file
pcap_filter = self._filter
- self._cmd = "tcpdump -p -i %s -w %s \"%s\" 2>/dev/null" %
\
+ self._cmd = "tcpdump -p -i %s -w %s %s" % \
(interface, output_file, pcap_filter)
def _execute_tcpdump(self):
""" Start tcpdump in the background """
- cmd = self._cmd
- self._tcpdump = subprocess.Popen(cmd, shell=True, stdout=None,
- stderr=None)
+ cmd = self._cmd.split()
+ fnull = open(os.devnull, "w")
+ self._tcpdump = subprocess.Popen(cmd, shell=False, stdout=fnull,
+ stderr=subprocess.STDOUT)
def _run(self):
self._compose_cmd()
--
2.1.0