1 /* 'top'-like tool for libvirt domains.
2 (C) Copyright 2007-2021 Richard W.M. Jones, Red Hat Inc.
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.
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.
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.
26 #include <caml/alloc.h>
27 #include <caml/fail.h>
28 #include <caml/memory.h>
29 #include <caml/mlvalues.h>
31 #include <libxml/parser.h>
32 #include <libxml/xpath.h>
33 #include <libxml/xpathInternals.h>
35 /* xpathobj contains a list of dev attributes, return the list
36 * as an OCaml array of strings.
39 get_devs (xmlDocPtr doc, xmlXPathObjectPtr xpathobj)
42 CAMLlocal2 (rv, nodev);
43 const xmlNodeSetPtr nodes = xpathobj->nodesetval;
49 if (nodes == NULL || nodes->nodeNr == 0)
50 rv = caml_alloc (0, 0);
52 /* Count the nodes that contain data. */
54 for (i = 0; i < nodes->nodeNr; ++i) {
55 node = nodes->nodeTab[i];
56 if (node->type != XML_ATTRIBUTE_NODE)
61 rv = caml_alloc (nr_nodes, 0);
63 for (i = 0; i < nodes->nodeNr; ++i) {
64 node = nodes->nodeTab[i];
65 if (node->type != XML_ATTRIBUTE_NODE)
67 attr = (xmlAttrPtr) node;
68 str = (char *) xmlNodeListGetString (doc, attr->children, 1);
69 nodev = caml_copy_string (str);
71 Store_field (rv, nr_nodes, nodev);
79 /* external get_blk_net_devs : string -> string array * string array */
81 get_blk_net_devs (value xmlv)
84 CAMLlocal3 (rv, blkdevs, netifs);
86 xmlXPathContextPtr xpathctx;
87 xmlXPathObjectPtr xpathobj;
90 /* For security reasons, call xmlReadMemory (not xmlParseMemory) and
91 * pass XML_PARSE_NONET.
93 doc = xmlReadMemory (String_val (xmlv), caml_string_length (xmlv),
94 NULL, NULL, XML_PARSE_NONET);
96 caml_invalid_argument ("xmlReadMemory: unable to parse XML");
98 xpathctx = xmlXPathNewContext (doc);
100 caml_invalid_argument ("xmlXPathNewContext: unable to create new context");
102 expr = "//devices/disk/target/@dev";
103 xpathobj = xmlXPathEvalExpression (BAD_CAST expr, xpathctx);
104 if (xpathobj == NULL)
105 caml_invalid_argument (expr);
107 blkdevs = get_devs (doc, xpathobj);
108 xmlXPathFreeObject (xpathobj);
110 expr = "//devices/interface/target/@dev";
111 xpathobj = xmlXPathEvalExpression (BAD_CAST expr, xpathctx);
112 if (xpathobj == NULL)
113 caml_invalid_argument (expr);
115 netifs = get_devs (doc, xpathobj);
116 xmlXPathFreeObject (xpathobj);
118 xmlXPathFreeContext (xpathctx);
121 rv = caml_alloc (2, 0);
122 Store_field (rv, 0, blkdevs);
123 Store_field (rv, 1, netifs);