Skip to content

Commit eef08fe

Browse files
Bastian-KrauseEmantor
authored andcommitted
driver/rawnetworkinterfacedriver: make start_record() block until capture started
The expectation of RawNetworkInterfaceDriver.start_record() is that it returns once the packet capturing is running, so later code can start right away generating packets that are received on the interface. This does not work reliably currently because the labgrid-raw-interface helper (wrapping tcpdump) is started via subprocess.Popen(), but that does not mean the capture is actually running just yet. To fix this, wait until the subprocess emits "tcpdump: listening on", which is tcpdump's way of telling that it's now actually capturing. Signed-off-by: Bastian Krause <[email protected]>
1 parent 1e207b2 commit eef08fe

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

labgrid/driver/rawnetworkinterfacedriver.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,10 +216,30 @@ def start_record(self, filename, *, count=None, timeout=None):
216216
cmd.append(str(timeout))
217217
cmd = self._wrap_command(cmd)
218218
if filename is None:
219-
self._record_handle = subprocess.Popen(cmd, stdout=subprocess.PIPE)
219+
self._record_handle = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
220220
else:
221221
with open(filename, "wb") as outdata:
222222
self._record_handle = subprocess.Popen(cmd, stdout=outdata, stderr=subprocess.PIPE)
223+
224+
# wait for capture start
225+
stderr = b""
226+
while True:
227+
line = self._record_handle.stderr.readline()
228+
if not line: # process ended prematurely
229+
try:
230+
self._stop(self._record_handle)
231+
except subprocess.CalledProcessError as e:
232+
# readd consumed stderr to exception
233+
e.stderr = stderr
234+
raise
235+
236+
if line.startswith(b"tcpdump: listening on"):
237+
break
238+
239+
# collect and log other lines in stderr before capturing has started
240+
stderr += line
241+
self.logger.warning(line.decode().rstrip())
242+
223243
return self._record_handle
224244

225245
@Driver.check_active

0 commit comments

Comments
 (0)