aboutsummaryrefslogtreecommitdiffstats
path: root/doc/extcap_example.py
diff options
context:
space:
mode:
authorRoland Knall <roland.knall@br-automation.com>2016-01-04 18:32:39 +0100
committerAnders Broman <a.broman58@gmail.com>2016-07-30 21:15:39 +0000
commitc611eded2272ac79997fb3ce11f2339dc32b53cb (patch)
tree48d73f9ed21c55448ae77fae24020cf4763da2c7 /doc/extcap_example.py
parentc2ac9c5c0316a27f54830fae0b70cf7825c6d99c (diff)
extcap: Use stderr to print error message
This patch reads out the stderr messages from an extcap utility and displays it to an user. It was tested on Qt but not on GTK, but should work their as well. On Mac OS/X and Windows the child_watch does not behave as it was intended. Therefore in extcap_cleanup, the callbacks are called manually, if and only if, they have not been called already. The reason why it displays two error messages is, that by the time the first one is being displayed, glib has not returned from the spawned process on Linux yet. So there is no way to add the stderr correctly, and putting a handler to stderr into interface_opts will lead to memory errors, cause then the code tries to access memory outside of its protection. Bug: 11892 Change-Id: I2db60dd480fed3e01428b91a705057e4f088bd15 Reviewed-on: https://code.wireshark.org/review/12954 Reviewed-by: Roland Knall <rknall@gmail.com> Petri-Dish: Roland Knall <rknall@gmail.com> Tested-by: Petri Dish Buildbot <buildbot-no-reply@wireshark.org> Reviewed-by: Dario Lombardo <lomato@gmail.com> Reviewed-by: Anders Broman <a.broman58@gmail.com>
Diffstat (limited to 'doc/extcap_example.py')
-rwxr-xr-xdoc/extcap_example.py74
1 files changed, 70 insertions, 4 deletions
diff --git a/doc/extcap_example.py b/doc/extcap_example.py
index ab9967c..9007e26 100755
--- a/doc/extcap_example.py
+++ b/doc/extcap_example.py
@@ -40,6 +40,7 @@ other script-based formates beside VBScript
"""
+from __future__ import print_function
import os
import sys
import signal
@@ -52,12 +53,40 @@ from threading import Thread
ERROR_USAGE = 0
ERROR_ARG = 1
-ERROR_INTERFACE = 2
+ERROR_INTERFACE = 2
ERROR_FIFO = 3
+ERROR_DELAY = 4
doExit = False
globalinterface = 0
+"""
+This code has been taken from http://stackoverflow.com/questions/5943249/python-argparse-and-controlling-overriding-the-exit-status-code - originally developed by Rob Cowie http://stackoverflow.com/users/46690/rob-cowie
+"""
+class ArgumentParser(argparse.ArgumentParser):
+ def _get_action_from_name(self, name):
+ """Given a name, get the Action instance registered with this parser.
+ If only it were made available in the ArgumentError object. It is
+ passed as it's first arg...
+ """
+ container = self._actions
+ if name is None:
+ return None
+ for action in container:
+ if '/'.join(action.option_strings) == name:
+ return action
+ elif action.metavar == name:
+ return action
+ elif action.dest == name:
+ return action
+
+ def error(self, message):
+ exc = sys.exc_info()[1]
+ if exc:
+ exc.argument = self._get_action_from_name(exc.argument_name)
+ raise exc
+ super(ArgumentParser, self).error(message)
+
def signalHandler(signal, frame):
global doExit
doExit = True
@@ -214,6 +243,23 @@ def extcap_capture(interface, fifo, delay, verify, message, remote, fake_ip):
fh.close()
+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 )
+ fh.close()
+
####
def usage():
@@ -227,7 +273,7 @@ if __name__ == '__main__':
message = ""
fake_ip = ""
- parser = argparse.ArgumentParser(
+ parser = ArgumentParser(
prog="Extcap Example",
description="Extcap example program for python"
)
@@ -243,12 +289,26 @@ if __name__ == '__main__':
# Interface Arguments
parser.add_argument("--verify", help="Demonstrates a verification bool flag", action="store_true" )
- parser.add_argument("--delay", help="Demonstrates an integer variable", type=int, default=0, choices=[0, 1, 2, 3, 4, 5] )
+ parser.add_argument("--delay", help="Demonstrates an integer variable", type=int, default=0, choices=[0, 1, 2, 3, 4, 5, 6] )
parser.add_argument("--remote", help="Demonstrates a selector choice", default="if1", choices=["if1", "if2"] )
parser.add_argument("--message", help="Demonstrates string variable", nargs='?', default="" )
parser.add_argument("--fake_ip", help="Add a fake sender IP adress", nargs='?', default="127.0.0.1" )
- args, unknown = parser.parse_known_args()
+ try:
+ args, unknown = parser.parse_known_args()
+ except argparse.ArgumentError, exc:
+ print( "%s: %s" % ( exc.argument.dest, exc.message ), file=sys.stderr)
+ fifo_found = 0
+ fifo = ""
+ for arg in sys.argv:
+ if (arg == "--fifo" or arg == "--extcap-fifo") :
+ fifo_found = 1
+ elif ( fifo_found == 1 ):
+ fifo = arg
+ break
+ extcap_close_fifo("", fifo)
+ sys.exit(ERROR_ARG)
+
if ( len(sys.argv) <= 1 ):
parser.exit("No arguments given!")
@@ -282,6 +342,12 @@ if __name__ == '__main__':
elif args.capture:
if args.fifo is None:
sys.exit(ERROR_FIFO)
+ # 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)
+ sys.exit(ERROR_DELAY)
+
extcap_capture(interface, args.fifo, args.delay, args.verify, message, args.remote, fake_ip)
else:
usage()