From 763a059fe967b5be4db4ca7b07279ae990a07c7d Mon Sep 17 00:00:00 2001 From: Peter Wu Date: Sun, 16 Oct 2016 17:05:46 +0200 Subject: extcap_example.py: fix hang on exit MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Reviewed-by: Stig Bjørlykke Tested-by: Petri Dish Buildbot Reviewed-by: Peter Wu --- doc/extcap_example.py | 61 +++++++++++++++++---------------------------------- 1 file changed, 20 insertions(+), 41 deletions(-) (limited to 'doc/extcap_example.py') diff --git a/doc/extcap_example.py b/doc/extcap_example.py index de28da01af..0847cb5648 100755 --- a/doc/extcap_example.py +++ b/doc/extcap_example.py @@ -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(interface, fifo): - global doExit +def extcap_close_fifo(fifo): + if not os.path.exists(fifo): + print ( "Fifo does not exist!", file=sys.stderr ) + return - 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) -- cgit v1.2.3