Add .gitignore file for git.
[virt-mem.git] / lib / virt_mem_utsname.ml
1 (* Memory info command 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_types
25 open Virt_mem_mmap
26
27 let parse_utsname bits =
28   (* Expect the first (sysname) field to always be "Linux", which is
29    * also a good way to tell if we're synchronized to the right bit of
30    * memory.
31    *)
32   bitmatch bits with
33   | { "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;
34       nodename : 65*8 : string;
35       release : 65*8 : string;
36       version : 65*8 : string;
37       machine : 65*8 : string;
38       domainname : 65*8 : string } ->
39       Some {
40         uts_kernel_name = "Linux";
41         uts_nodename = truncate_c_string nodename;
42         uts_kernel_release = truncate_c_string release;
43         uts_kernel_version = truncate_c_string version;
44         uts_machine = truncate_c_string machine;
45         uts_domainname = truncate_c_string domainname
46       }
47   | { _ } ->
48       None
49
50 let find_utsname debug ({ domname = name; mem = mem; ksyms = ksyms }
51                           as kimage) =
52   let utsname =
53     (* In Linux 2.6.25, the symbol is init_uts_ns.
54      * http://lxr.linux.no/linux/init/version.c
55      *)
56     try
57       let addr = Ksymmap.find "init_uts_ns" ksyms in
58
59       let bs = Bitstring.bitstring_of_string (get_bytes mem addr (65*6+4)) in
60       (bitmatch bs with
61        | { _ : 32 : int;              (* the kref, atomic_t, always 32 bits *)
62            new_utsname : -1 : bitstring } ->
63            parse_utsname new_utsname
64        | { _ } ->
65            if debug then
66              eprintf (f_"%s: unexpected init_uts_ns in kernel image\n") name;
67            None
68       )
69     with
70       Not_found ->
71         (* In Linux 2.6.9, the symbol is system_utsname.
72          * http://lxr.linux.no/linux-bk+v2.6.9/include/linux/utsname.h#L24
73          *)
74         try
75           let addr = Ksymmap.find "system_utsname" ksyms in
76
77           let bits =
78             Bitstring.bitstring_of_string (get_bytes mem addr (65*6)) in
79           parse_utsname bits
80         with
81           Not_found ->
82             eprintf (f_"%s: could not find utsname in kernel image\n")
83               name;
84             None
85   in
86   { kimage with utsname = utsname }