SVN: wireless-net/trunk/sysconfig/interfaces/up.d/ppp/wlan-up.py

arekm arekm at pld-linux.org
Mon Nov 6 21:45:37 CET 2006


Author: arekm
Date: Mon Nov  6 21:45:34 2006
New Revision: 7942

Modified:
   wireless-net/trunk/sysconfig/interfaces/up.d/ppp/wlan-up.py
Log:
Don't use grep, kill, cat binaries.

Modified: wireless-net/trunk/sysconfig/interfaces/up.d/ppp/wlan-up.py
==============================================================================
--- wireless-net/trunk/sysconfig/interfaces/up.d/ppp/wlan-up.py	(original)
+++ wireless-net/trunk/sysconfig/interfaces/up.d/ppp/wlan-up.py	Mon Nov  6 21:45:34 2006
@@ -11,6 +11,34 @@
     except OSError, err:
         return err.errno == errno.EPERM
 
+def get_iface_pid(iface):
+    pid_file = "/var/run/%s.pid" % iface
+    if not os.path.isfile(pid_file):
+        return False
+    fp_pid = open("/var/run/%s.pid" % iface, "r")
+    pid = fp_pid.readline()
+    fp_pid.close()
+    pid = int(pid)
+    if len(pid) and pid > 0:
+        return pid
+    return False
+
+def kill_pid(pid):
+    if len(pid) and pid > 0 and pid_exists(pid):
+        try:
+            os.kill(pid, signal.SIGTERM)
+        except OSError, e:
+            pass
+        if pid_exists(pid):
+            time.sleep(1)
+        if pid_exists(pid):
+            try:
+                os.kill(pid, signal.SIGKILL)
+            except OSError, e:
+                pass
+        return not pid_exists(pid)
+    return False
+
 config = ConfigParser.ConfigParser()
 config.read('/etc/wireless-net/wlan.conf')
 r_cfg = "/etc/wireless-net/wlan-router.conf"
@@ -57,16 +85,25 @@
         fl.write("%s: \"%s\" tried to use \"%s\" account [iface=%s, ip=%s]\n"
                 % (time.asctime(time.gmtime()), reth_mac, user, ppp_iface, ip))
         fl.close()
-        os.system("/bin/kill -TERM `/bin/cat /var/run/%s.pid` > /dev/null 2>&1" % ppp_iface)
+        pid = get_iface_pid(ppp_iface)
+        kill_pid(pid)
 
     if reth_mac == None:
         reth_mac = "(unknown)"
 
     # check if user is already logged in
-    pd = os.popen("/sbin/ip addr show 2>&1 | /bin/grep -E \'peer %s/[[:digit:]]+.*ppp[[:digit:]]+\' | /bin/sed -e \'s#.* global ##g\'" % ppp_remip, "r")
-    used_ifaces = pd.readlines()
+    pd = os.popen("/sbin/ip addr show 2>&1")
+    ip_a_data = pd.readlines()
     pd.close()
-    used_ifaces = [i.strip() for i in used_ifaces]
+    re_inet = re.compile('^.* inet .* peer ' + ppp_remip + '/\d+ scope .* (ppp\d+)')
+    ifaces = {}
+    for ip_a_line in ip_a_data:
+        m = re_inet.match(ip_a_line)
+        if m:
+            ip_a_iface = m.group(1)
+            if not ifaces.has_key(ip_a_iface):
+                ifaces[ip_a_iface.strip()] = 1
+    used_ifaces = ifaces.keys()
     for iface in used_ifaces:
         if iface == ppp_iface:
             used_ifaces.remove(iface)
@@ -74,26 +111,9 @@
     if len(used_ifaces):
         fl.write("%s: \"%s\" already logged in - killing stale sessions [stale=%s, iface=%s, ip=%s, mac=%s]\n" % (time.asctime(time.gmtime()), user, used_ifaces, ppp_iface, ip, reth_mac))
     for iface in used_ifaces:
-        pid_file = "/var/run/%s.pid" % iface
-        if not os.path.isfile(pid_file):
-            fl.write("%s: \"%s\" unreadable %s file, can't kill stale session [iface=%s]\n" % (time.asctime(time.gmtime()), user, pid_file, iface))
-            continue
-        fp_pid = open("/var/run/%s.pid" % iface, "r")
-        pid = fp_pid.readline()
-        fp_pid.close()
-        pid = int(pid)
-        if len(pid) and pid > 0 and pid_exists(pid):
-            try:
-                os.kill(pid, signal.SIGTERM)
-            except OSError, e:
-                fl.write("%s: \"%s\" SIGTERM unable to kill stale session [iface=%s, pid=%d]\n" % (time.asctime(time.gmtime()), user, iface, pid)
-            if pid_exists(pid):
-                time.sleep(1)
-            if pid_exists(pid):
-                try:
-                    os.kill(pid, signal.SIGKILL)
-                except OSError, e:
-                    fl.write("%s: \"%s\" SIGKILL unable to kill stale session [iface=%s, pid=%d]\n" % (time.asctime(time.gmtime()), user, iface, pid)
+        pid = get_iface_pid(iface)
+        if not kill_pid(pid):
+            fl.write("%s: \"%s\" unable to kill stale session [iface=%s, pid=%d]\n" % (time.asctime(time.gmtime()), user, iface, pid))
     
     fl.write("%s: \"%s\" logged in [iface=%s, ip=%s, mac=%s]\n" % (time.asctime(time.gmtime()), user, ppp_iface, ip, reth_mac))
 


More information about the pld-cvs-commit mailing list