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 50 /* VIR_ERR_NO_STORAGE_VOL */
115 #define MAX_VIR_DOMAIN 17 /* 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 (9, 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));
181 Val_opt (err->str1, (Val_ptr_t) caml_copy_string));
183 Val_opt (err->str2, (Val_ptr_t) caml_copy_string));
185 Val_opt (err->str3, (Val_ptr_t) caml_copy_string));
186 Store_field (rv, 7, caml_copy_int32 (err->int1));
187 Store_field (rv, 8, caml_copy_int32 (err->int2));
192 static void conn_finalize (value);
193 static void dom_finalize (value);
194 static void net_finalize (value);
195 #ifdef HAVE_VIRSTORAGEPOOLPTR
196 static void pol_finalize (value);
198 #ifdef HAVE_VIRSTORAGEVOLPTR
199 static void vol_finalize (value);
201 #ifdef HAVE_VIRJOBPTR
202 static void jb_finalize (value);
205 static struct custom_operations conn_custom_operations = {
206 "conn_custom_operations",
208 custom_compare_default,
210 custom_serialize_default,
211 custom_deserialize_default
214 static struct custom_operations dom_custom_operations = {
215 "dom_custom_operations",
217 custom_compare_default,
219 custom_serialize_default,
220 custom_deserialize_default
224 static struct custom_operations net_custom_operations = {
225 "net_custom_operations",
227 custom_compare_default,
229 custom_serialize_default,
230 custom_deserialize_default
233 #ifdef HAVE_VIRSTORAGEPOOLPTR
234 static struct custom_operations pol_custom_operations = {
235 "pol_custom_operations",
237 custom_compare_default,
239 custom_serialize_default,
240 custom_deserialize_default
244 #ifdef HAVE_VIRSTORAGEVOLPTR
245 static struct custom_operations vol_custom_operations = {
246 "vol_custom_operations",
248 custom_compare_default,
250 custom_serialize_default,
251 custom_deserialize_default
255 #ifdef HAVE_VIRJOBPTR
256 static struct custom_operations jb_custom_operations = {
257 "jb_custom_operations",
259 custom_compare_default,
261 custom_serialize_default,
262 custom_deserialize_default
267 Val_connect (virConnectPtr conn)
271 rv = caml_alloc_custom (&conn_custom_operations,
272 sizeof (virConnectPtr), 0, 1);
273 Connect_val (rv) = conn;
278 Val_dom (virDomainPtr dom)
282 rv = caml_alloc_custom (&dom_custom_operations,
283 sizeof (virDomainPtr), 0, 1);
289 Val_net (virNetworkPtr net)
293 rv = caml_alloc_custom (&net_custom_operations,
294 sizeof (virNetworkPtr), 0, 1);
299 #ifdef HAVE_VIRSTORAGEPOOLPTR
301 Val_pol (virStoragePoolPtr pol)
305 rv = caml_alloc_custom (&pol_custom_operations,
306 sizeof (virStoragePoolPtr), 0, 1);
312 #ifdef HAVE_VIRSTORAGEVOLPTR
314 Val_vol (virStorageVolPtr vol)
318 rv = caml_alloc_custom (&vol_custom_operations,
319 sizeof (virStorageVolPtr), 0, 1);
325 #ifdef HAVE_VIRJOBPTR
327 Val_jb (virJobPtr jb)
331 rv = caml_alloc_custom (&jb_custom_operations,
332 sizeof (virJobPtr), 0, 1);
338 /* This wraps up the (dom, conn) pair (Domain.t). */
340 Val_domain (virDomainPtr dom, value connv)
345 rv = caml_alloc_tuple (2);
347 Store_field (rv, 0, v);
348 Store_field (rv, 1, connv);
352 /* This wraps up the (net, conn) pair (Network.t). */
354 Val_network (virNetworkPtr net, value connv)
359 rv = caml_alloc_tuple (2);
361 Store_field (rv, 0, v);
362 Store_field (rv, 1, connv);
366 #ifdef HAVE_VIRSTORAGEPOOLPTR
367 /* This wraps up the (pol, conn) pair (Pool.t). */
369 Val_pool (virStoragePoolPtr pol, value connv)
374 rv = caml_alloc_tuple (2);
376 Store_field (rv, 0, v);
377 Store_field (rv, 1, connv);
382 #ifdef HAVE_VIRSTORAGEVOLPTR
383 /* This wraps up the (vol, conn) pair (Volume.t). */
385 Val_volume (virStorageVolPtr vol, value connv)
390 rv = caml_alloc_tuple (2);
392 Store_field (rv, 0, v);
393 Store_field (rv, 1, connv);
398 #ifdef HAVE_VIRJOBPTR
399 /* This wraps up the (jb, conn) pair (Job.t). */
401 Val_job (virJobPtr jb, value connv)
406 rv = caml_alloc_tuple (2);
408 Store_field (rv, 0, v);
409 Store_field (rv, 1, connv);
415 conn_finalize (value connv)
417 virConnectPtr conn = Connect_val (connv);
418 if (conn) (void) virConnectClose (conn);
422 dom_finalize (value domv)
424 virDomainPtr dom = Dom_val (domv);
425 if (dom) (void) virDomainFree (dom);
429 net_finalize (value netv)
431 virNetworkPtr net = Net_val (netv);
432 if (net) (void) virNetworkFree (net);
435 #ifdef HAVE_VIRSTORAGEPOOLPTR
437 pol_finalize (value polv)
439 virStoragePoolPtr pol = Pol_val (polv);
440 if (pol) (void) virStoragePoolFree (pol);
444 #ifdef HAVE_VIRSTORAGEVOLPTR
446 vol_finalize (value volv)
448 virStorageVolPtr vol = Vol_val (volv);
449 if (vol) (void) virStorageVolFree (vol);
453 #ifdef HAVE_VIRJOBPTR
455 jb_finalize (value jbv)
457 virJobPtr jb = Jb_val (jbv);
458 if (jb) (void) virJobFree (jb);