Combined binary.
[virt-mem.git] / uname / virt_uname.ml
1 (* Memory info for virtual domains.
2    (C) Copyright 2008 Richard W.M. Jones, Red Hat Inc.
3    http://libvirt.org/
4
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2 of the License, or
8    (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  *)
19
20 open Printf
21
22 open Virt_mem_gettext.Gettext
23 open Virt_mem_utils
24 open Virt_mem_mmap
25
26 let run debug images =
27   (* Print new_utsname structure from bitstring. *)
28   let print_new_utsname name bs =
29     (* Truncate an OCaml string at the first ASCII NUL character, ie. as
30      * if it were a C string.
31      *)
32     let truncate str =
33       try
34         let i = String.index str '\000' in
35         String.sub str 0 i
36       with
37         Not_found -> str
38     in
39     (* Expect the first (sysname) field to always be "Linux", which is
40      * also a good way to tell if we're synchronized to the right bit of
41      * memory.
42      *)
43     bitmatch bs with
44     | { "Linux\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000" : 65*8 : string;
45         nodename : 65*8 : string;
46         release : 65*8 : string;
47         version : 65*8 : string;
48         machine : 65*8 : string;
49         domainname : 65*8 : string } ->
50         printf "%s: Linux %s %s %s %s %s\n"
51           name
52           (truncate nodename) (truncate release)
53           (truncate version) (truncate machine) (truncate domainname)
54     | { _ } ->
55         eprintf (f_"%s: unexpected system_utsname in kernel image\n")
56           name
57   in
58
59   List.iter (
60     fun (name, arch, mem, lookup_ksym) ->
61       (* In Linux 2.6.25, the symbol is init_uts_ns.
62        * http://lxr.linux.no/linux/init/version.c
63        *)
64       try
65         let addr = lookup_ksym "init_uts_ns" in
66
67         let bs = Bitmatch.bitstring_of_string (get_bytes mem addr (65*6+4)) in
68         (bitmatch bs with
69          | { _ : 32 : int;              (* the kref, atomic_t, always 32 bits *)
70              new_utsname : -1 : bitstring } ->
71              print_new_utsname name new_utsname
72          | { _ } ->
73              eprintf (f_"%s: unexpected init_uts_ns in kernel image\n")
74                name)
75       with
76         Not_found ->
77           (* In Linux 2.6.9, the symbol is system_utsname.
78            * http://lxr.linux.no/linux-bk+v2.6.9/include/linux/utsname.h#L24
79            *)
80           try
81             let addr = lookup_ksym "system_utsname" in
82
83             let bs =
84               Bitmatch.bitstring_of_string (get_bytes mem addr (65*6)) in
85             print_new_utsname name bs
86           with
87             Not_found ->
88               eprintf (f_"%s: could not find utsname in kernel image\n") name
89   ) images
90
91 let summary = s_"uname command for virtual machines"
92 let description = s_"\
93 virt-uname prints the uname information such as OS version,
94 architecture and node name for virtual machines running under
95 libvirt."
96
97 let () = Virt_mem.register "uname" summary description true run