1 /* OCaml bindings for libvirt.
2 * (C) Copyright 2007 Richard W.M. Jones, Red Hat Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library 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 GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 /* Please read libvirt/README file. */
23 Optstring_val (value strv)
25 if (strv == Val_int (0)) /* None */
27 else /* Some string */
28 return String_val (Field (strv, 0));
32 Val_opt (void *ptr, Val_ptr_t Val_ptr)
35 CAMLlocal2 (optv, ptrv);
37 if (ptr) { /* Some ptr */
38 optv = caml_alloc (1, 0);
40 Store_field (optv, 0, ptrv);
49 option_default (value option, value deflt)
51 if (option == Val_int (0)) /* "None" */
54 return Field (option, 0);
59 _raise_virterror (virConnectPtr conn, const char *fn)
66 errp = conn ? virConnGetLastError (conn) : virGetLastError ();
69 /* Fake a _virError structure. */
70 memset (&err, 0, sizeof err);
71 err.code = VIR_ERR_INTERNAL_ERROR;
72 err.domain = VIR_FROM_NONE;
73 err.level = VIR_ERR_ERROR;
74 err.message = (char *) fn;
78 rv = Val_virterror (errp);
79 caml_raise_with_arg (*caml_named_value ("ocaml_libvirt_virterror"), rv);
82 /* Suppresses a compiler warning. */
86 /* Raise an error if a function is not supported. */
88 not_supported (const char *fn)
93 fnv = caml_copy_string (fn);
94 caml_raise_with_arg (*caml_named_value ("ocaml_libvirt_not_supported"), fnv);
97 /* Suppresses a compiler warning. */
101 /* Convert the virErrorNumber, virErrorDomain and virErrorLevel enums
102 * into values (longs because they are variants in OCaml).
104 * The enum values are part of the libvirt ABI so they cannot change,
105 * which means that we can convert these numbers directly into
106 * OCaml variants (which use the same ordering) very fast.
108 * The tricky part here is when we are linked to a newer version of
109 * libvirt than the one we were compiled against. If the newer libvirt
110 * generates an error code which we don't know about then we need
111 * to convert it into VIR_*_UNKNOWN (code).
114 #define MAX_VIR_CODE VIR_ERR_NO_STORAGE_VOL
115 #define MAX_VIR_DOMAIN VIR_FROM_STORAGE
116 #define MAX_VIR_LEVEL VIR_ERR_ERROR
119 Val_err_number (virErrorNumber code)
124 if (0 <= code && code <= MAX_VIR_CODE)
127 rv = caml_alloc (1, 0); /* VIR_ERR_UNKNOWN (code) */
128 Store_field (rv, 0, Val_int (code));
135 Val_err_domain (virErrorDomain code)
140 if (0 <= code && code <= MAX_VIR_DOMAIN)
143 rv = caml_alloc (1, 0); /* VIR_FROM_UNKNOWN (code) */
144 Store_field (rv, 0, Val_int (code));
151 Val_err_level (virErrorLevel code)
156 if (0 <= code && code <= MAX_VIR_LEVEL)
159 rv = caml_alloc (1, 0); /* VIR_ERR_UNKNOWN_LEVEL (code) */
160 Store_field (rv, 0, Val_int (code));
166 /* Convert a virterror to a value. */
168 Val_virterror (virErrorPtr err)
171 CAMLlocal3 (rv, connv, optv);
173 rv = caml_alloc (12, 0);
174 Store_field (rv, 0, Val_err_number (err->code));
175 Store_field (rv, 1, Val_err_domain (err->domain));
177 Val_opt (err->message, (Val_ptr_t) caml_copy_string));
178 Store_field (rv, 3, Val_err_level (err->level));
180 /* conn, dom and net fields, all optional */
182 connv = Val_connect_no_finalize (err->conn);
183 optv = caml_alloc (1, 0);
184 Store_field (optv, 0, connv);
185 Store_field (rv, 4, optv); /* Some conn */
188 optv = caml_alloc (1, 0);
189 Store_field (optv, 0, Val_domain_no_finalize (err->dom, connv));
190 Store_field (rv, 5, optv); /* Some (dom, conn) */
193 Store_field (rv, 5, Val_int (0)); /* None */
195 optv = caml_alloc (1, 0);
196 Store_field (optv, 0, Val_network_no_finalize (err->net, connv));
197 Store_field (rv, 11, optv); /* Some (net, conn) */
199 Store_field (rv, 11, Val_int (0)); /* None */
201 Store_field (rv, 4, Val_int (0)); /* None */
202 Store_field (rv, 5, Val_int (0)); /* None */
203 Store_field (rv, 11, Val_int (0)); /* None */
207 Val_opt (err->str1, (Val_ptr_t) caml_copy_string));
209 Val_opt (err->str2, (Val_ptr_t) caml_copy_string));
211 Val_opt (err->str3, (Val_ptr_t) caml_copy_string));
212 Store_field (rv, 9, caml_copy_int32 (err->int1));
213 Store_field (rv, 10, caml_copy_int32 (err->int2));
218 static void conn_finalize (value);
219 static void dom_finalize (value);
220 static void net_finalize (value);
221 #ifdef HAVE_VIRSTORAGEPOOLPTR
222 static void pol_finalize (value);
224 #ifdef HAVE_VIRSTORAGEVOLPTR
225 static void vol_finalize (value);
228 static struct custom_operations conn_custom_operations = {
229 "conn_custom_operations",
231 custom_compare_default,
233 custom_serialize_default,
234 custom_deserialize_default
237 static struct custom_operations dom_custom_operations = {
238 "dom_custom_operations",
240 custom_compare_default,
242 custom_serialize_default,
243 custom_deserialize_default
247 static struct custom_operations net_custom_operations = {
248 "net_custom_operations",
250 custom_compare_default,
252 custom_serialize_default,
253 custom_deserialize_default
256 #ifdef HAVE_VIRSTORAGEPOOLPTR
257 static struct custom_operations pol_custom_operations = {
258 "pol_custom_operations",
260 custom_compare_default,
262 custom_serialize_default,
263 custom_deserialize_default
267 #ifdef HAVE_VIRSTORAGEVOLPTR
268 static struct custom_operations vol_custom_operations = {
269 "vol_custom_operations",
271 custom_compare_default,
273 custom_serialize_default,
274 custom_deserialize_default
279 Val_connect (virConnectPtr conn)
283 rv = caml_alloc_custom (&conn_custom_operations,
284 sizeof (virConnectPtr), 0, 1);
285 Connect_val (rv) = conn;
290 Val_dom (virDomainPtr dom)
294 rv = caml_alloc_custom (&dom_custom_operations,
295 sizeof (virDomainPtr), 0, 1);
301 Val_net (virNetworkPtr net)
305 rv = caml_alloc_custom (&net_custom_operations,
306 sizeof (virNetworkPtr), 0, 1);
311 #ifdef HAVE_VIRSTORAGEPOOLPTR
313 Val_pol (virStoragePoolPtr pol)
317 rv = caml_alloc_custom (&pol_custom_operations,
318 sizeof (virStoragePoolPtr), 0, 1);
324 #ifdef HAVE_VIRSTORAGEVOLPTR
326 Val_vol (virStorageVolPtr vol)
330 rv = caml_alloc_custom (&vol_custom_operations,
331 sizeof (virStorageVolPtr), 0, 1);
337 /* No-finalize versions of Val_connect, Val_dom, Val_net ONLY for use
338 * by virterror wrappers.
341 Val_connect_no_finalize (virConnectPtr conn)
345 rv = caml_alloc (1, Abstract_tag);
346 Store_field (rv, 0, (value) conn);
351 Val_dom_no_finalize (virDomainPtr dom)
355 rv = caml_alloc (1, Abstract_tag);
356 Store_field (rv, 0, (value) dom);
361 Val_net_no_finalize (virNetworkPtr net)
365 rv = caml_alloc (1, Abstract_tag);
366 Store_field (rv, 0, (value) net);
370 /* This wraps up the (dom, conn) pair (Domain.t). */
372 Val_domain (virDomainPtr dom, value connv)
377 rv = caml_alloc_tuple (2);
379 Store_field (rv, 0, v);
380 Store_field (rv, 1, connv);
384 /* This wraps up the (net, conn) pair (Network.t). */
386 Val_network (virNetworkPtr net, value connv)
391 rv = caml_alloc_tuple (2);
393 Store_field (rv, 0, v);
394 Store_field (rv, 1, connv);
398 #ifdef HAVE_VIRSTORAGEPOOLPTR
399 /* This wraps up the (pol, conn) pair (Pool.t). */
401 Val_pool (virStoragePoolPtr pol, value connv)
406 rv = caml_alloc_tuple (2);
408 Store_field (rv, 0, v);
409 Store_field (rv, 1, connv);
414 #ifdef HAVE_VIRSTORAGEVOLPTR
415 /* This wraps up the (vol, conn) pair (Volume.t). */
417 Val_volume (virStorageVolPtr vol, value connv)
422 rv = caml_alloc_tuple (2);
424 Store_field (rv, 0, v);
425 Store_field (rv, 1, connv);
430 /* No-finalize versions of Val_domain, Val_network ONLY for use by
431 * virterror wrappers.
434 Val_domain_no_finalize (virDomainPtr dom, value connv)
439 rv = caml_alloc_tuple (2);
440 v = Val_dom_no_finalize (dom);
441 Store_field (rv, 0, v);
442 Store_field (rv, 1, connv);
447 Val_network_no_finalize (virNetworkPtr net, value connv)
452 rv = caml_alloc_tuple (2);
453 v = Val_net_no_finalize (net);
454 Store_field (rv, 0, v);
455 Store_field (rv, 1, connv);
460 conn_finalize (value connv)
462 virConnectPtr conn = Connect_val (connv);
463 if (conn) (void) virConnectClose (conn);
467 dom_finalize (value domv)
469 virDomainPtr dom = Dom_val (domv);
470 if (dom) (void) virDomainFree (dom);
474 net_finalize (value netv)
476 virNetworkPtr net = Net_val (netv);
477 if (net) (void) virNetworkFree (net);
480 #ifdef HAVE_VIRSTORAGEPOOLPTR
482 pol_finalize (value polv)
484 virStoragePoolPtr pol = Pol_val (polv);
485 if (pol) (void) virStoragePoolFree (pol);
489 #ifdef HAVE_VIRSTORAGEVOLPTR
491 vol_finalize (value volv)
493 virStorageVolPtr vol = Vol_val (volv);
494 if (vol) (void) virStorageVolFree (vol);