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,
9 * with the OCaml linking exception described in ../COPYING.LIB.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 /* Please read libvirt/README file. */
24 Optstring_val (value strv)
26 if (strv == Val_int (0)) /* None */
28 else /* Some string */
29 return String_val (Field (strv, 0));
33 Val_opt (void *ptr, Val_ptr_t Val_ptr)
36 CAMLlocal2 (optv, ptrv);
38 if (ptr) { /* Some ptr */
39 optv = caml_alloc (1, 0);
41 Store_field (optv, 0, ptrv);
50 option_default (value option, value deflt)
52 if (option == Val_int (0)) /* "None" */
55 return Field (option, 0);
60 _raise_virterror (const char *fn)
67 errp = virGetLastError ();
70 /* Fake a _virError structure. */
71 memset (&err, 0, sizeof err);
72 err.code = VIR_ERR_INTERNAL_ERROR;
73 err.domain = VIR_FROM_NONE;
74 err.level = VIR_ERR_ERROR;
75 err.message = (char *) fn;
79 rv = Val_virterror (errp);
80 caml_raise_with_arg (*caml_named_value ("ocaml_libvirt_virterror"), rv);
83 /* Suppresses a compiler warning. */
87 /* Convert the virErrorNumber, virErrorDomain and virErrorLevel enums
88 * into values (longs because they are variants in OCaml).
90 * The enum values are part of the libvirt ABI so they cannot change,
91 * which means that we can convert these numbers directly into
92 * OCaml variants (which use the same ordering) very fast.
94 * The tricky part here is when we are linked to a newer version of
95 * libvirt than the one we were compiled against. If the newer libvirt
96 * generates an error code which we don't know about then we need
97 * to convert it into VIR_*_UNKNOWN (code).
100 #define MAX_VIR_CODE 50 /* VIR_ERR_NO_STORAGE_VOL */
101 #define MAX_VIR_DOMAIN 17 /* VIR_FROM_STORAGE */
102 #define MAX_VIR_LEVEL VIR_ERR_ERROR
105 Val_err_number (virErrorNumber code)
110 if (0 <= code && code <= MAX_VIR_CODE)
113 rv = caml_alloc (1, 0); /* VIR_ERR_UNKNOWN (code) */
114 Store_field (rv, 0, Val_int (code));
121 Val_err_domain (virErrorDomain code)
126 if (0 <= code && code <= MAX_VIR_DOMAIN)
129 rv = caml_alloc (1, 0); /* VIR_FROM_UNKNOWN (code) */
130 Store_field (rv, 0, Val_int (code));
137 Val_err_level (virErrorLevel code)
142 if (0 <= code && code <= MAX_VIR_LEVEL)
145 rv = caml_alloc (1, 0); /* VIR_ERR_UNKNOWN_LEVEL (code) */
146 Store_field (rv, 0, Val_int (code));
152 /* Convert a virterror to a value. */
154 Val_virterror (virErrorPtr err)
157 CAMLlocal3 (rv, connv, optv);
159 rv = caml_alloc (9, 0);
160 Store_field (rv, 0, Val_err_number (err->code));
161 Store_field (rv, 1, Val_err_domain (err->domain));
163 Val_opt (err->message, (Val_ptr_t) caml_copy_string));
164 Store_field (rv, 3, Val_err_level (err->level));
167 Val_opt (err->str1, (Val_ptr_t) caml_copy_string));
169 Val_opt (err->str2, (Val_ptr_t) caml_copy_string));
171 Val_opt (err->str3, (Val_ptr_t) caml_copy_string));
172 Store_field (rv, 7, caml_copy_int32 (err->int1));
173 Store_field (rv, 8, caml_copy_int32 (err->int2));
178 static void conn_finalize (value);
179 static void dom_finalize (value);
180 static void net_finalize (value);
181 static void pol_finalize (value);
182 static void vol_finalize (value);
184 static struct custom_operations conn_custom_operations = {
185 "conn_custom_operations",
187 custom_compare_default,
189 custom_serialize_default,
190 custom_deserialize_default
193 static struct custom_operations dom_custom_operations = {
194 "dom_custom_operations",
196 custom_compare_default,
198 custom_serialize_default,
199 custom_deserialize_default
203 static struct custom_operations net_custom_operations = {
204 "net_custom_operations",
206 custom_compare_default,
208 custom_serialize_default,
209 custom_deserialize_default
212 static struct custom_operations pol_custom_operations = {
213 "pol_custom_operations",
215 custom_compare_default,
217 custom_serialize_default,
218 custom_deserialize_default
221 static struct custom_operations vol_custom_operations = {
222 "vol_custom_operations",
224 custom_compare_default,
226 custom_serialize_default,
227 custom_deserialize_default
231 Val_connect (virConnectPtr conn)
235 rv = caml_alloc_custom (&conn_custom_operations,
236 sizeof (virConnectPtr), 0, 1);
237 Connect_val (rv) = conn;
242 Val_dom (virDomainPtr dom)
246 rv = caml_alloc_custom (&dom_custom_operations,
247 sizeof (virDomainPtr), 0, 1);
253 Val_net (virNetworkPtr net)
257 rv = caml_alloc_custom (&net_custom_operations,
258 sizeof (virNetworkPtr), 0, 1);
264 Val_pol (virStoragePoolPtr pol)
268 rv = caml_alloc_custom (&pol_custom_operations,
269 sizeof (virStoragePoolPtr), 0, 1);
275 Val_vol (virStorageVolPtr vol)
279 rv = caml_alloc_custom (&vol_custom_operations,
280 sizeof (virStorageVolPtr), 0, 1);
285 /* This wraps up the (dom, conn) pair (Domain.t). */
287 Val_domain (virDomainPtr dom, value connv)
292 rv = caml_alloc_tuple (2);
294 Store_field (rv, 0, v);
295 Store_field (rv, 1, connv);
299 /* This wraps up the (net, conn) pair (Network.t). */
301 Val_network (virNetworkPtr net, value connv)
306 rv = caml_alloc_tuple (2);
308 Store_field (rv, 0, v);
309 Store_field (rv, 1, connv);
313 /* This wraps up the (pol, conn) pair (Pool.t). */
315 Val_pool (virStoragePoolPtr pol, value connv)
320 rv = caml_alloc_tuple (2);
322 Store_field (rv, 0, v);
323 Store_field (rv, 1, connv);
327 /* This wraps up the (vol, conn) pair (Volume.t). */
329 Val_volume (virStorageVolPtr vol, value connv)
334 rv = caml_alloc_tuple (2);
336 Store_field (rv, 0, v);
337 Store_field (rv, 1, connv);
342 conn_finalize (value connv)
344 virConnectPtr conn = Connect_val (connv);
345 if (conn) (void) virConnectClose (conn);
349 dom_finalize (value domv)
351 virDomainPtr dom = Dom_val (domv);
352 if (dom) (void) virDomainFree (dom);
356 net_finalize (value netv)
358 virNetworkPtr net = Net_val (netv);
359 if (net) (void) virNetworkFree (net);
363 pol_finalize (value polv)
365 virStoragePoolPtr pol = Pol_val (polv);
366 if (pol) (void) virStoragePoolFree (pol);
370 vol_finalize (value volv)
372 virStorageVolPtr vol = Vol_val (volv);
373 if (vol) (void) virStorageVolFree (vol);