extcap_example.py: fix hang on exit

I guess that when SIGINT is intercepted, then the writes are restarted
and the doExit condition is never checked. Remove this racy check in
favor of catching the KeyboardInterrupt exception.

Test: tshark -i example1; kill tshark; check process list for python.

Bug: 11657
Change-Id: Ia8b1ee560b9dcd31dd91df27fbfb8e91237581c9
Reviewed-on: https://code.wireshark.org/review/18218
Petri-Dish: Peter Wu <peter@lekensteyn.nl>
Reviewed-by: Stig Bjørlykke <stig@bjorlykke.org>
Tested-by: Petri Dish Buildbot <buildbot-no-reply@wireshark.org>
Reviewed-by: Peter Wu <peter@lekensteyn.nl>
This commit is contained in:
Peter Wu 2016-10-16 17:05:46 +02:00
parent 2176423b4a
commit 763a059fe9
1 changed files with 20 additions and 41 deletions

View File

@ -57,7 +57,6 @@ ERROR_INTERFACE = 2
ERROR_FIFO = 3
ERROR_DELAY = 4
doExit = False
globalinterface = 0
"""
@ -87,10 +86,6 @@ class ArgumentParser(argparse.ArgumentParser):
raise exc
super(ArgumentParser, self).error(message)
def signalHandler(signal, frame):
global doExit
doExit = True
#### EXTCAP FUNCTIONALITY
"""@brief Extcap configuration
@ -212,47 +207,28 @@ def pcap_fake_package ( message, fake_ip ):
return pcap
def extcap_capture(interface, fifo, delay, verify, message, remote, fake_ip):
global doExit
signal.signal(signal.SIGINT, signalHandler)
signal.signal(signal.SIGTERM , signalHandler)
tdelay = delay if delay != 0 else 5
try:
os.stat(fifo)
except OSError:
doExit = True
print ( "Fifo does not exist, exiting!" )
if not os.path.exists(fifo):
print ( "Fifo does not exist, exiting!", file=sys.stderr )
sys.exit(1)
fh = open(fifo, 'w+b', 0 )
fh.write (pcap_fake_header())
with open(fifo, 'wb', 0 ) as fh:
fh.write (pcap_fake_header())
while doExit == False:
out = ("%s|%04X%s|%s" % ( remote.strip(), len(message), message, verify )).encode("utf8")
try:
while True:
out = ("%s|%04X%s|%s" % ( remote.strip(), len(message), message, verify )).encode("utf8")
fh.write (pcap_fake_package(out, fake_ip))
time.sleep(tdelay)
except IOError:
doExit = True
fh.close()
def extcap_close_fifo(fifo):
if not os.path.exists(fifo):
print ( "Fifo does not exist!", file=sys.stderr )
return
def extcap_close_fifo(interface, fifo):
global doExit
signal.signal(signal.SIGINT, signalHandler)
signal.signal(signal.SIGTERM , signalHandler)
tdelay = delay if delay != 0 else 5
try:
os.stat(fifo)
except OSError:
doExit = True
print ( "Fifo does not exist!" )
fh = open(fifo, 'w+b', 0 )
# This is apparently needed to workaround an issue on Windows/macOS
# where the message cannot be read. (really?)
fh = open(fifo, 'wb', 0 )
fh.close()
####
@ -301,7 +277,7 @@ if __name__ == '__main__':
elif ( fifo_found == 1 ):
fifo = arg
break
extcap_close_fifo("", fifo)
extcap_close_fifo(fifo)
sys.exit(ERROR_ARG)
if ( len(sys.argv) <= 1 ):
@ -340,10 +316,13 @@ if __name__ == '__main__':
# The following code demonstrates error management with extcap
if args.delay > 5:
print("Value for delay [%d] too high" % args.delay, file=sys.stderr)
extcap_close_fifo(interface, args.fifo)
extcap_close_fifo(args.fifo)
sys.exit(ERROR_DELAY)
extcap_capture(interface, args.fifo, args.delay, args.verify, message, args.remote, fake_ip)
try:
extcap_capture(interface, args.fifo, args.delay, args.verify, message, args.remote, fake_ip)
except KeyboardInterrupt:
pass
else:
usage()
sys.exit(ERROR_USAGE)