Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 27 May 2007 21:46:37 GMT
From:      Ivan Voras <ivoras@FreeBSD.org>
To:        Perforce Change Reviews <perforce@FreeBSD.org>
Subject:   PERFORCE change 120477 for review
Message-ID:  <200705272146.l4RLkbnK089902@repoman.freebsd.org>

next in thread | raw e-mail | index | archive | help
http://perforce.freebsd.org/chv.cgi?CH=120477

Change 120477 by ivoras@ivoras_finstall on 2007/05/27 21:45:37

	- Fix sysctl retrieval, wrt \0 characters
	- Implement GEOM confxml tree retrieval
	- Implement GetDrives() XML-RPC function

Affected files ...

.. //depot/projects/soc2007/ivoras_finstall/pybackend/freebsd.py#3 edit
.. //depot/projects/soc2007/ivoras_finstall/pybackend/systoold.py#4 edit
.. //depot/projects/soc2007/ivoras_finstall/pybackend/systoolengine.py#4 edit
.. //depot/projects/soc2007/ivoras_finstall/pybackend/testtool/st.py#2 edit
.. //depot/projects/soc2007/ivoras_finstall/pybackend/xmldict.py#3 edit

Differences ...

==== //depot/projects/soc2007/ivoras_finstall/pybackend/freebsd.py#3 (text+ko) ====

@@ -22,15 +22,35 @@
 # Interface to (most) FreeBSD's low-level utilities
 
 import os, sys
+import xmldict
 
 cmd_sysctl = "/sbin/sysctl"
 cmd_geom = "/sbin/geom"
 cmd_mount = "/sbin/mount"
 
+file_dmesg = "/var/run/dmesg.boot"
+
 def get_sysctl(name):
 	global cmd_sysctl
-	return os.popen("%s -b %s" % (cmd_sysctl, name))
+	str = os.popen("%s -b %s" % (cmd_sysctl, name)).read().strip()
+	while str[-1] == "\0":
+		str = str[:-1]
+	return str
 
 def get_cmd_output(name):
-	return os.popen(name).read()
+	return os.popen(name).read().strip()
+
+def get_dmesg():
+	global file_dmesg
+	return [x.strip() for x in file(file_dmesg, "r").readlines()]
+
+def get_geom_xml():
+	return xmldict.buildxmldict(get_sysctl("kern.geom.confxml"))
+
+if __name__ == "__main__":
+	xml = get_geom_xml()
+	for cls in xml["mesh"]["class"]:
+		if cls["name"].data == "DISK":
+			for geom in cls["geom"]:
+				print geom["name"].data, geom["provider"]["mediasize"].data
 

==== //depot/projects/soc2007/ivoras_finstall/pybackend/systoold.py#4 (text+ko) ====

@@ -28,7 +28,7 @@
 import os,sys
 from getopt import getopt, GetoptError
 from select import select
-from SimpleXMLRPCServer import SimpleXMLRPCServer
+from SimpleXMLRPCServer import SimpleXMLRPCServer, SimpleXMLRPCRequestHandler
 
 import globals
 from systoolengine import SysToolEngine
@@ -65,19 +65,25 @@
 		sys.exit(1)
 
 
+def ToUpper(str):
+	return str.upper()
+
 engine = SysToolEngine()
 
-server = SimpleXMLRPCServer((bind_host, bind_port), None, globals.debug_level > 0)
+server = SimpleXMLRPCServer((bind_host, bind_port), SimpleXMLRPCRequestHandler, globals.debug_level > 0)
 server.register_introspection_functions()
+server.register_function(ToUpper)
 server.register_instance(engine)
 
+while not globals.exit_systoold:
+	try:
+		r,w,e = select([server.fileno()], [], [], 100)
+		if r:
+			# Only handle requests if there's a connection pending
+			# This is done to enable (somewhat) graceful exiting of this
+			# TCP connection handler loop
+			server.handle_request()
+	except KeyboardInterrupt:
+		break
 
-while not globals.exit_systoold:
-	r,w,e = select([server.socket], [], [], 100)
-	print r
-	if r:
-		# Only handle requests if there's a connection pending
-		# This is done to enable (somewhat) graceful exiting of this
-		# TCP connection handler loop
-		server.handle_request()
-	
+server.server_close()

==== //depot/projects/soc2007/ivoras_finstall/pybackend/systoolengine.py#4 (text+ko) ====

@@ -78,7 +78,33 @@
 		Examines "kern.drives" sysctl. This is NOT the list of
 		valid GEOM leaves which can be mounted, but a list of
 		found hardware."""
-		return freebsd.get_sysctl("kern.disks").split(" ")
+		drive_list = freebsd.get_sysctl("kern.disks").split(" ")
+		dmesg = freebsd.get_dmesg()
+		drive_dict = {}
+		for drive in drive_list:
+			found = False
+			# try finding a line containing a "<" in hope that it's a description
+			for line in dmesg:
+				if line.startswith("%s:" % drive) and line.find("<") != -1:
+					drive_dict[drive] = {"name" : line[len(drive)+2:]}
+					found = True
+			if not found:
+				# try finding the longest line
+				drive_dict[drive] = {"name": ""}
+				for line in dmesg:
+					if line.startswith("%s:" % drive) and len(drive_dict[drive]["name"]) < len(line)-len(drive)-2:
+						drive_dict[drive] = {"name": line[len(drive)+2:]}
+						found = True
+			if not found:
+				drive_dict[drive]["name"] = "(unknown)"
+		geomxml = freebsd.get_geom_xml()
+		for cls in geomxml["mesh"]["class"]:
+			if cls["name"].data == "DISK":
+				for geom in cls["geom"]:
+					dev_name = geom["name"].data
+					if dev_name in drive_dict:
+						drive_dict[dev_name]["mediasize"] = int(geom["provider"]["mediasize"].data) / (1024*1024) # in MB, since XML-RPC doesn't have int64
+		return drive_dict
 
 
 	def GetMountPoints(self):

==== //depot/projects/soc2007/ivoras_finstall/pybackend/testtool/st.py#2 (text+ko) ====

@@ -23,4 +23,12 @@
 # The purpose if this utility is mainly to test the XML-RPC calls in the
 # SysToolD
 
+import sys
+from xmlrpclib import ServerProxy
+
+server = ServerProxy("http://localhost:1025")
+if not "systoold" in server.GetCaps():
+	print "This is a bogus server"
+	sys.exit(1)
 
+print server.GetDrives()

==== //depot/projects/soc2007/ivoras_finstall/pybackend/xmldict.py#3 (text+ko) ====

@@ -181,11 +181,15 @@
     def builddict(self):
         """Builds a nested-dictionary-like structure from the xml. This method
         picks up tags on the main level and calls processTag() for nested tags."""
+	global DEBUG
         d = Tag('<root>', '')
         while True:
             tag, attrs, data = self.getnexttag()
-            if data != '': # data is actually between the last tag and this one
+            if data != '' and data != "\0": # data is actually between the last tag and this one
                 sys.stderr.write("Warning: inline data between tags?!\n")
+                if DEBUG:
+                    sys.stderr.write(" ^^^ |"+data+"|\n")
+		    sys.stderr.write(" len=%d" % len(data))
             if not tag:
                 break
             if tag[-1] == '/': # an 'empty' tag (e.g. <empty/>)



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200705272146.l4RLkbnK089902>