xenserver: Add new helper function to interface-reconfigure.
authorBen Pfaff <blp@nicira.com>
Wed, 5 Aug 2009 21:53:48 +0000 (14:53 -0700)
committerBen Pfaff <blp@nicira.com>
Thu, 6 Aug 2009 23:57:06 +0000 (16:57 -0700)
This will be useful in the followin commit.

xenserver/opt_xensource_libexec_interface-reconfigure

index 8f33e102d30331429684531a36704ca734f5567b..6de62b38740188e70d911a50bc075fcb1772ad82 100755 (executable)
@@ -543,6 +543,15 @@ def run_command(command):
         return False
     return True
 
+def read_first_line_of_file(name):
+    file = None
+    try:
+        file = open(name, 'r')
+        return file.readline().rstrip('\n')
+    finally:
+        if file != None:
+            file.close()
+
 def down_netdev(interface, deconfigure=True):
     if not interface_exists(interface):
         log("down_netdev: interface %s does not exist, ignoring" % interface)
@@ -550,14 +559,10 @@ def down_netdev(interface, deconfigure=True):
     if deconfigure:
         # Kill dhclient.
         pidfile_name = '/var/run/dhclient-%s.pid' % interface
-        pidfile = None
         try:
-            pidfile = open(pidfile_name, 'r')
-            os.kill(int(pidfile.readline()), signal.SIGTERM)
+            os.kill(int(read_first_line_of_file(pidfile_name)), signal.SIGTERM)
         except:
             pass
-        if pidfile != None:
-            pidfile.close()
 
         # Remove dhclient pidfile.
         try: