From 0d3ccc0c91b944f318a6d0648d96fd7f99405f22 Mon Sep 17 00:00:00 2001 From: Jim Pryor Date: Tue, 11 Aug 2009 08:04:53 -0400 Subject: Make python script use i/o hooks too Signed-off-by: Jim Pryor --- src-wireless/wireless-dbus | 66 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 49 insertions(+), 17 deletions(-) (limited to 'src-wireless/wireless-dbus') diff --git a/src-wireless/wireless-dbus b/src-wireless/wireless-dbus index 6f53a6c..30b77d8 100644 --- a/src-wireless/wireless-dbus +++ b/src-wireless/wireless-dbus @@ -4,7 +4,7 @@ import dbus import shlex import subprocess from signal import SIGTERM -from os import kill +import os from time import sleep # dbus constants. @@ -29,8 +29,7 @@ def read_config(config): def wep_hex2dec(key): if len(key) not in [10, 26]: - print "Bad key" - raise SyntaxError + fail("Bad key", report_type="err") x=0 new_key=[] @@ -40,10 +39,20 @@ def wep_hex2dec(key): return new_key -def fail(msg=None): +# JP: connect to our hookable reporting functions in /usr/lib/network/globals + +def report(report_type, *args): + report_handler.stdin.write('report_%s %s\n' % (report_type, ' '.join(map(repr,args)))) + +def fail(msg=None, report_type="fail"): if msg: - print " -", msg - kill(int(open("/var/run/wpa_supplicant.pid").read()),SIGTERM) + report(report_type, msg) + try: + pid = open("/var/run/wpa_supplicant.pid").read() + except IOError: + pass + else: + os.kill(int(pid),SIGTERM) sys.exit(1) @@ -74,16 +83,18 @@ def start(profile): except KeyError: args.append("-c/etc/wpa_supplicant.conf") elif not profile['SECURITY'] in ['wpa', 'wep', 'none']: - fail("Invalid security chosen") + fail("Invalid security chosen", report_type="err") # Start wpa_supplicant + report('debug', 'wireless_dbus', 'starting wpa_supplicant') supplicant = subprocess.Popen(args,stderr=subprocess.STDOUT,stdout=subprocess.PIPE) output = supplicant.communicate()[0] if supplicant.returncode not in [255,0]: - print output + print >>sys.stderr, output # JP: print to stderr fail("Could not start wpa_supplicant") # Connect to wpa_supplicant + report('debug', 'wireless_dbus', 'connecting to wpa_supplicant') bus = dbus.SystemBus() wpas_obj = bus.get_object(WPAS_DBUS_SERVICE, WPAS_DBUS_OPATH) wpas = dbus.Interface(wpas_obj, WPAS_DBUS_INTERFACE) @@ -105,6 +116,7 @@ def start(profile): # Add and select the network. Networks already specified for wpa-config if profile['SECURITY'] in ['wpa','wep','none']: + report('debug', 'wireless_dbus', 'add and select network') path = iface.addNetwork() net_obj = bus.get_object(WPAS_DBUS_SERVICE, path) @@ -116,6 +128,7 @@ def start(profile): opts = dbus.Dictionary({"ssid": dbus.ByteArray(profile['ESSID']), "psk": dbus.String(profile['KEY'])}, signature="sv") + report('debug', 'wireless_dbus', 'connect to network with security=wpa') rnet.set(opts) elif profile['SECURITY'] == "wep": key=profile['KEY'] @@ -132,10 +145,12 @@ def start(profile): "wep_tx_keyidx": dbus.Int32(1), "wep_key0": dbus.ByteArray(keydbus)}, signature="sv") + report('debug', 'wireless_dbus', 'connect to network with security=wep') rnet.set(opts) elif profile['SECURITY'] == "none": opts = dbus.Dictionary({"ssid": dbus.ByteArray(profile['ESSID'])}, signature="sv") + report('debug', 'wireless_dbus', 'connect to network with security=none') rnet.set(opts) # Determine timeout @@ -158,20 +173,37 @@ def start(profile): # Run ethernet and get an ip. try: - subprocess.check_call(["/usr/lib/network/connections/ethernet-iproute", "up", sys.argv[2]]) + subprocess.check_call([ETHERNET_IPROUTE, "up", sys.argv[2]]) except subprocess.CalledProcessError: fail() sys.exit(0) def stop(profile): - subprocess.call(["/usr/lib/network/connections/ethernet", "down", sys.argv[2]]) - kill(int(open("/var/run/wpa_supplicant.pid").read()),SIGTERM) + ret = subprocess.call([ETHERNET_IPROUTE, "down", sys.argv[2]]) + os.kill(int(open("/var/run/wpa_supplicant.pid").read()),SIGTERM) sys.exit(0) if __name__ == "__main__": - profile = read_config("/etc/network.d/"+sys.argv[2]) - - if sys.argv[1] == "up": - start(profile) - elif sys.argv[1] == "down": - stop(profile) + + CONN_DIR = os.path.abspath(os.path.dirname(sys.argv[0])) + ETHERNET_IPROUTE = os.path.join(CONN_DIR,"ethernet-iproute") + WIRELESS = os.path.join(CONN_DIR,"wireless") + + # setup bash report_* handler + report_handler_script = 'source /etc/rc.conf; source /etc/rc.d/functions; source %s/../globals; while read cmd args; do eval $cmd "$args"; done' % (CONN_DIR,) + report_handler = subprocess.Popen(report_handler_script, executable='/bin/bash', stdin=subprocess.PIPE, shell=True) + + try: + profile_name = sys.argv[2] + profile = read_config("/etc/network.d/"+profile_name) + + if sys.argv[1] == "up": + start(profile) + elif sys.argv[1] == "down": + stop(profile) + + finally: + report_handler.stdin.close() + report_handler.wait() + +# vim: et ts=4 -- cgit v1.2.3-24-g4f1b