[Libguestfs] [nbdkit PATCH v3 1/4] plugins: Don't use bogus errno from non-C plugins

Richard W.M. Jones rjones at redhat.com
Fri Jan 27 10:11:56 UTC 2017


On Thu, Jan 26, 2017 at 08:58:34PM -0600, Eric Blake wrote:
> diff --git a/plugins/ocaml/ocaml.c b/plugins/ocaml/ocaml.c
> index e2b433e..5d7aeeb 100644
> --- a/plugins/ocaml/ocaml.c
> +++ b/plugins/ocaml/ocaml.c
> @@ -499,6 +499,17 @@ SET(pwrite)
>  SET(flush)
>  SET(trim)
> 
> +/* We can't guarantee that errno is stable across language binding
> + * glue code, so this callback is implemented in C only, and not
> + * exposed in OCaml.
> + */
> +static int
> +plugin_ocaml_errno_is_reliable (void *handle)
> +{
> +  return 0;
> +}

Actually OCaml is a real compiled language, and the call from C to
OCaml code (via caml_callback_exn) is a short piece of asm which
preserves errno.

However you'll need to save errno around caml_enter_blocking_section
since that unblocks and processes signals.

IOW:

  static int
  pread_wrapper (void *h, void *buf, uint32_t count, uint64_t offset)
  {
    CAMLparam0 ();
    CAMLlocal3 (rv, strv, offsetv);
+   int saved_errno;
...
    rv = caml_callback3_exn (pread_fn, *(value *) h, strv, offsetv);
+   saved_errno = errno;
    if (Is_exception_result (rv)) {
      nbdkit_error ("%s", caml_format_exception (Extract_exception (rv)));
      caml_enter_blocking_section ();
+     errno = saved_errno;
      CAMLreturnT (int, -1);
    }

Rich.

-- 
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
Read my programming and virtualization blog: http://rwmj.wordpress.com
virt-p2v converts physical machines to virtual machines.  Boot with a
live CD or over the network (PXE) and turn machines into KVM guests.
http://libguestfs.org/virt-v2v




More information about the Libguestfs mailing list