Richard W.M. Jones
2017-Jan-27 10:11 UTC
Re: [Libguestfs] [nbdkit PATCH v3 1/4] plugins: Don't use bogus errno from non-C plugins
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
Eric Blake
2017-Jan-27 14:01 UTC
Re: [Libguestfs] [nbdkit PATCH v3 1/4] plugins: Don't use bogus errno from non-C plugins
On 01/27/2017 04:11 AM, Richard W.M. Jones wrote:> > 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.Which shows my lack of familiarity with OCaml; but I'm guessing that also means that OCaml comes with easy ways to directly set errno so that it will be visible from C.> > 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);Okay, I'll respin the patch along these lines. -- Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Richard W.M. Jones
2017-Jan-27 18:08 UTC
Re: [Libguestfs] [nbdkit PATCH v3 1/4] plugins: Don't use bogus errno from non-C plugins
On Fri, Jan 27, 2017 at 08:01:17AM -0600, Eric Blake wrote:> On 01/27/2017 04:11 AM, Richard W.M. Jones wrote: > > > > 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. > > Which shows my lack of familiarity with OCaml; but I'm guessing that > also means that OCaml comes with easy ways to directly set errno so that > it will be visible from C.Actually no, that would be too easy. Failing system calls (in OCaml code) are turned into exceptions. The exception contains something which has a 1-1 mapping with the errno, but is not the errno. In libguestfs we had to do some mappings for those, so it's a pain. So in fact I'm not sure if errno is preserved properly or if we should map the Is_exception_result. Need to think about this - at the moment I'm in the middle of some customer crisis. Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com libguestfs lets you edit virtual machines. Supports shell scripting, bindings from many languages. http://libguestfs.org
Seemingly Similar Threads
- Re: [nbdkit PATCH v3 1/4] plugins: Don't use bogus errno from non-C plugins
- [PATCH nbdkit v2 1/2] ocaml: Change pread method to avoid leaking heap memory.
- [nbdkit PATCH v2 08/24] ocaml: Implement .cache script callback
- [PATCH nbdkit 1/2] ocaml: Initialize pread buffer with zeroes to avoid leaking heap memory.
- [PATCH v2 1/3] common/mlpcre: add offset flag for PCRE.matches