Skip to content

Commit 70f6150

Browse files
Merge pull request #1745 from Bastian-Krause/bst/rawnetworkinterface-fixes
RawNetworkInterfaceDriver/labgrid-raw-interface Fixes
2 parents e0eb75e + eef08fe commit 70f6150

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

helpers/labgrid-raw-interface

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,7 @@ def main(program, options):
7373
args.append(str(options.count))
7474

7575
if options.timeout:
76-
# The timeout is implemented by specifying the number of seconds before rotating the
77-
# dump file, but limiting the number of files to 1
78-
args.append("-G")
79-
args.append(str(options.timeout))
80-
args.append("-W")
81-
args.append("1")
76+
args = ["timeout", "--signal=INT", "--preserve-status", str(options.timeout)] + args
8277

8378
elif program == "ip":
8479
args.append("link")
@@ -131,7 +126,7 @@ if __name__ == "__main__":
131126
# tcpdump
132127
tcpdump_parser = subparsers.add_parser("tcpdump")
133128
tcpdump_parser.add_argument("ifname", type=str, help="interface name")
134-
tcpdump_parser.add_argument("count", type=int, default=None, help="amount of frames to capture while recording")
129+
tcpdump_parser.add_argument("count", type=int, nargs="?", default=None, help="amount of frames to capture while recording")
135130
tcpdump_parser.add_argument(
136131
"--timeout", type=int, default=None, help="Amount of time to capture while recording. 0 means capture forever"
137132
)

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)