1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
|
/* nbdkit
* Copyright Red Hat
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of Red Hat nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <caml/alloc.h>
#include <caml/fail.h>
#include <caml/memory.h>
#include <caml/mlvalues.h>
#include <caml/threads.h>
#include <caml/unixsupport.h>
#define NBDKIT_API_VERSION 2
#include <nbdkit-plugin.h>
#include "plugin.h"
/* Bindings for miscellaneous nbdkit_* utility functions. */
/* NB: noalloc function. */
NBDKIT_DLL_PUBLIC value
ocaml_nbdkit_set_error (value nv)
{
nbdkit_set_error (code_of_unix_error (nv));
return Val_unit;
}
NBDKIT_DLL_PUBLIC value
ocaml_nbdkit_parse_size (value strv)
{
CAMLparam1 (strv);
CAMLlocal1 (rv);
int64_t r;
r = nbdkit_parse_size (String_val (strv));
if (r == -1)
caml_invalid_argument ("nbdkit_parse_size");
rv = caml_copy_int64 (r);
CAMLreturn (rv);
}
NBDKIT_DLL_PUBLIC value
ocaml_nbdkit_parse_probability (value whatv, value strv)
{
CAMLparam2 (whatv, strv);
CAMLlocal1 (dv);
int r;
double d;
r = nbdkit_parse_probability (String_val (whatv), String_val (strv), &d);
if (r == -1)
caml_invalid_argument ("nbdkit_parse_probability");
dv = caml_copy_double (d);
CAMLreturn (dv);
}
NBDKIT_DLL_PUBLIC value
ocaml_nbdkit_parse_bool (value strv)
{
CAMLparam1 (strv);
CAMLlocal1 (rv);
int r;
r = nbdkit_parse_bool (String_val (strv));
if (r == -1)
caml_invalid_argument ("nbdkit_parse_bool");
rv = Val_bool (r);
CAMLreturn (rv);
}
NBDKIT_DLL_PUBLIC value
ocaml_nbdkit_parse_delay (value whatv, value strv)
{
CAMLparam2 (whatv, strv);
CAMLlocal1 (rv);
unsigned sec, nsec;
int r;
r = nbdkit_parse_delay (String_val (whatv), String_val (strv),
&sec, &nsec);
if (r == -1)
caml_invalid_argument ("nbdkit_parse_delay");
rv = caml_alloc (2, 0);
Store_field (rv, 0, Val_int (sec));
Store_field (rv, 1, Val_int (nsec));
CAMLreturn (rv);
}
NBDKIT_DLL_PUBLIC value
ocaml_nbdkit_read_password (value strv)
{
CAMLparam1 (strv);
CAMLlocal1 (rv);
char *password;
int r;
r = nbdkit_read_password (String_val (strv), &password);
if (r == -1)
caml_invalid_argument ("nbdkit_read_password");
rv = caml_copy_string (password);
free (password);
CAMLreturn (rv);
}
NBDKIT_DLL_PUBLIC value
ocaml_nbdkit_stdio_safe (value unitv)
{
CAMLparam1 (unitv);
CAMLlocal1 (rv);
int r;
r = nbdkit_stdio_safe ();
if (r == -1)
caml_invalid_argument ("nbdkit_stdio_safe");
rv = Val_bool (r);
CAMLreturn (rv);
}
NBDKIT_DLL_PUBLIC value
ocaml_nbdkit_realpath (value strv)
{
CAMLparam1 (strv);
CAMLlocal1 (rv);
char *ret;
ret = nbdkit_realpath (String_val (strv));
if (ret == NULL)
caml_failwith ("nbdkit_realpath");
rv = caml_copy_string (ret);
free (ret);
CAMLreturn (rv);
}
NBDKIT_DLL_PUBLIC value
ocaml_nbdkit_nanosleep (value secv, value nsecv)
{
CAMLparam2 (secv, nsecv);
int r;
caml_enter_blocking_section ();
r = nbdkit_nanosleep (Int_val (secv), Int_val (nsecv));
caml_leave_blocking_section ();
if (r == -1)
caml_failwith ("nbdkit_nanosleep");
CAMLreturn (Val_unit);
}
NBDKIT_DLL_PUBLIC value
ocaml_nbdkit_export_name (value unitv)
{
CAMLparam1 (unitv);
CAMLlocal1 (rv);
const char *ret;
ret = nbdkit_export_name ();
/* Note that NULL indicates error. Default export name is [""] even
* for oldstyle.
*/
if (ret == NULL)
caml_failwith ("nbdkit_export_name");
rv = caml_copy_string (ret);
CAMLreturn (rv);
}
NBDKIT_DLL_PUBLIC value
ocaml_nbdkit_is_tls (value unitv)
{
CAMLparam1 (unitv);
CAMLlocal1 (rv);
int r;
r = nbdkit_is_tls ();
if (r == -1)
caml_invalid_argument ("nbdkit_is_tls");
rv = Val_bool (r);
CAMLreturn (rv);
}
/* NB: noalloc function. */
NBDKIT_DLL_PUBLIC value
ocaml_nbdkit_shutdown (value unitv)
{
CAMLparam1 (unitv);
nbdkit_shutdown ();
CAMLreturn (Val_unit);
}
/* NB: noalloc function. */
NBDKIT_DLL_PUBLIC value
ocaml_nbdkit_disconnect (value boolv)
{
CAMLparam1 (boolv);
nbdkit_disconnect (Bool_val (boolv));
CAMLreturn (Val_unit);
}
/* NB: noalloc function. */
NBDKIT_DLL_PUBLIC value
ocaml_nbdkit_debug (value strv)
{
nbdkit_debug ("%s", String_val (strv));
return Val_unit;
}
NBDKIT_DLL_PUBLIC value
ocaml_nbdkit_version (value unitv)
{
CAMLparam1 (unitv);
CAMLlocal1 (rv);
rv = caml_copy_string (PACKAGE_VERSION);
CAMLreturn (rv);
}
/* NB: noalloc function. */
NBDKIT_DLL_PUBLIC value
ocaml_nbdkit_api_version (value unitv)
{
return Val_int (NBDKIT_API_VERSION);
}
#ifdef HAVE_CAML_SOCKETADDR_H
#include <caml/socketaddr.h>
#ifndef HAVE_CAML_UNIX_ALLOC_SOCKADDR
#define caml_unix_alloc_sockaddr alloc_sockaddr /* OCaml <= 4.14 */
#endif
NBDKIT_DLL_PUBLIC value
ocaml_nbdkit_peer_name (value unitv)
{
CAMLparam1 (unitv);
CAMLlocal1 (rv);
struct sockaddr_storage sa;
socklen_t len = sizeof sa;
int r;
r = nbdkit_peer_name ((struct sockaddr *) &sa, &len);
if (r == -1) caml_failwith ("nbdkit_peer_name");
rv = caml_unix_alloc_sockaddr ((void *) &sa, len, -1);
CAMLreturn (rv);
}
#else /* !HAVE_CAML_SOCKETADDR_H */
NBDKIT_DLL_PUBLIC value
ocaml_nbdkit_peer_name (value unitv)
{
CAMLparam1 (unitv);
CAMLlocal1 (rv);
caml_failwith ("nbdkit_peer_name is not supported by this version of OCaml");
}
#endif /* !HAVE_CAML_SOCKETADDR_H */
NBDKIT_DLL_PUBLIC value
ocaml_nbdkit_peer_pid (value unitv)
{
CAMLparam1 (unitv);
CAMLlocal1 (rv);
int64_t id = nbdkit_peer_pid ();
if (id == -1) caml_failwith ("nbdkit_peer_pid");
rv = caml_copy_int64 (id);
CAMLreturn (rv);
}
NBDKIT_DLL_PUBLIC value
ocaml_nbdkit_peer_uid (value unitv)
{
CAMLparam1 (unitv);
CAMLlocal1 (rv);
int64_t id = nbdkit_peer_uid ();
if (id == -1) caml_failwith ("nbdkit_peer_uid");
rv = caml_copy_int64 (id);
CAMLreturn (rv);
}
NBDKIT_DLL_PUBLIC value
ocaml_nbdkit_peer_gid (value unitv)
{
CAMLparam1 (unitv);
CAMLlocal1 (rv);
int64_t id = nbdkit_peer_gid ();
if (id == -1) caml_failwith ("nbdkit_peer_gid");
rv = caml_copy_int64 (id);
CAMLreturn (rv);
}
NBDKIT_DLL_PUBLIC value
ocaml_nbdkit_peer_security_context (value unitv)
{
CAMLparam1 (unitv);
CAMLlocal1 (rv);
char *label = nbdkit_peer_security_context ();
if (label == NULL) caml_failwith ("nbdkit_peer_security_context");
rv = caml_copy_string (label);
free (label);
CAMLreturn (rv);
}
NBDKIT_DLL_PUBLIC value
ocaml_nbdkit_peer_tls_dn (value unitv)
{
CAMLparam1 (unitv);
CAMLlocal1 (rv);
char *label = nbdkit_peer_tls_dn ();
if (label == NULL) caml_failwith ("nbdkit_peer_tls_dn");
rv = caml_copy_string (label);
free (label);
CAMLreturn (rv);
}
NBDKIT_DLL_PUBLIC value
ocaml_nbdkit_peer_tls_issuer_dn (value unitv)
{
CAMLparam1 (unitv);
CAMLlocal1 (rv);
char *label = nbdkit_peer_tls_issuer_dn ();
if (label == NULL) caml_failwith ("nbdkit_peer_tls_issuer_dn");
rv = caml_copy_string (label);
free (label);
CAMLreturn (rv);
}
|