From 91d5f5dedf46f607388e0a1cd77f540e24b0b0ba Mon Sep 17 00:00:00 2001 From: "Richard W.M. Jones" Date: Thu, 1 Jan 1970 00:00:00 +0000 Subject: [PATCH] new_utsname parsing, uname finished. --- uname/.depend | 8 +++++-- uname/virt_uname.ml | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 2 deletions(-) diff --git a/uname/.depend b/uname/.depend index eafb6d7..0adc4a7 100644 --- a/uname/.depend +++ b/uname/.depend @@ -1,2 +1,6 @@ -virt_uname.cmo: ../lib/virt_mem_gettext.cmo ../lib/virt_mem.cmi -virt_uname.cmx: ../lib/virt_mem_gettext.cmx ../lib/virt_mem.cmx +virt_uname.cmo: ../lib/virt_mem_utils.cmo ../lib/virt_mem_mmap.cmi \ + ../lib/virt_mem_gettext.cmo ../lib/virt_mem.cmi \ + /usr/lib64/ocaml/bitmatch/bitmatch.cmi +virt_uname.cmx: ../lib/virt_mem_utils.cmx ../lib/virt_mem_mmap.cmx \ + ../lib/virt_mem_gettext.cmx ../lib/virt_mem.cmx \ + /usr/lib64/ocaml/bitmatch/bitmatch.cmi diff --git a/uname/virt_uname.ml b/uname/virt_uname.ml index 54c0148..dcdf5c8 100644 --- a/uname/virt_uname.ml +++ b/uname/virt_uname.ml @@ -17,7 +17,11 @@ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *) +open Printf + open Virt_mem_gettext.Gettext +open Virt_mem_utils +open Virt_mem_mmap let usage = s_"NAME virt-uname - uname command for virtual machines @@ -31,3 +35,66 @@ DESCRIPTION libvirt." let verbose, images = Virt_mem.start usage + +let () = + (* Print new_utsname structure from bitstring. *) + let print_new_utsname name bs = + (* Truncate an OCaml string at the first ASCII NUL character, ie. as + * if it were a C string. + *) + let truncate str = + try + let i = String.index str '\000' in + String.sub str 0 i + with + Not_found -> str + in + bitmatch bs with + | { sysname : 65*8 : string; + nodename : 65*8 : string; + release : 65*8 : string; + version : 65*8 : string; + machine : 65*8 : string; + domainname : 65*8 : string } -> + printf "%s: %s %s %s %s %s %s\n" + name + (truncate sysname) (truncate nodename) (truncate release) + (truncate version) (truncate machine) (truncate domainname) + | { _ } -> + eprintf (f_"%s: unexpected system_utsname in kernel image\n") + name + in + + List.iter ( + fun (name, arch, mem, lookup_ksym) -> + (* In Linux 2.6.25, the symbol is init_uts_ns. + * http://lxr.linux.no/linux/init/version.c + *) + try + let addr = lookup_ksym "init_uts_ns" in + if verbose then printf "init_uts_ns at %Lx\n" addr; + + let bs = Bitmatch.bitstring_of_string (get_bytes mem addr (65*6+4)) in + (bitmatch bs with + | { _ : 32 : int; (* the kref, atomic_t, always 32 bits *) + new_utsname : -1 : bitstring } -> + print_new_utsname name new_utsname + | { _ } -> + eprintf (f_"%s: unexpected init_uts_ns in kernel image\n") + name) + with + Not_found -> + (* In Linux 2.6.9, the symbol is system_utsname. + * http://lxr.linux.no/linux-bk+v2.6.9/include/linux/utsname.h#L24 + *) + try + let addr = lookup_ksym "system_utsname" in + if verbose then printf "system_utsname at %Lx\n" addr; + + let bs = + Bitmatch.bitstring_of_string (get_bytes mem addr (65*6)) in + print_new_utsname name bs + with + Not_found -> + eprintf (f_"%s: could not find utsname in kernel image\n") name + ) images -- 1.8.3.1