@@ -16,7 +16,11 @@
type handle
-external init: unit -> handle = "stub_eventchn_init"
+external init_cloexec: bool -> handle = "stub_eventchn_init"
+
+let init ?(cloexec=true) () = init_cloexec cloexec
+
+external fdopen: Unix.file_descr -> handle = "stub_eventchn_fdopen"
external fd: handle -> Unix.file_descr = "stub_eventchn_fd"
type t = int
@@ -43,7 +43,14 @@ val to_int: t -> int
val of_int: int -> t
-val init: unit -> handle
+val init: ?cloexec:bool -> unit -> handle
+(** [init ?cloexec ()]
+ Return an initialised event channel interface.
+ The default is to close the underlying file descriptor
+ on [execve], which can be overriden with [~cloexec:false].
+ On error it will throw a Failure exception. *)
+
+val fdopen: Unix.file_descr -> handle
(** Return an initialised event channel interface. On error it
will throw a Failure exception. *)
@@ -58,14 +58,36 @@ static struct custom_operations xenevtchn_ops = {
custom_compare_ext_default /* raises Failure */
};
-CAMLprim value stub_eventchn_init(void)
+CAMLprim value stub_eventchn_init(value cloexec)
{
- CAMLparam0();
+ CAMLparam1(cloexec);
CAMLlocal1(result);
xenevtchn_handle *xce;
caml_enter_blocking_section();
- xce = xenevtchn_open(NULL, 0);
+ xce = xenevtchn_open(NULL, Bool_val(cloexec) ? 0 : XENEVTCHN_NO_CLOEXEC);
+ caml_leave_blocking_section();
+
+ if ( xce == NULL )
+ caml_failwith("open failed");
+
+ /* contains file descriptors, trigger full GC at least every 128
+ * allocations
+ */
+ result = caml_alloc_custom(&xenevtchn_ops, sizeof(xce), 0, 1);
+ _H(result) = xce;
+ CAMLreturn(result);
+}
+
+CAMLprim value stub_eventchn_fdopen(value fdval)
+{
+ CAMLparam1(fdval);
+ CAMLlocal1(result);
+ xenevtchn_handle *xce;
+
+ caml_enter_blocking_section();
+ /* having any flags here would raise EINVAL */
+ xce = xenevtchn_fdopen(NULL, Int_val(fdval), 0);
caml_leave_blocking_section();
if (xce == NULL)
Signed-off-by: Edwin Török <edvin.torok@citrix.com> --- Reason for inclusion in 4.17: - needed for a bugfix in a followup commit Changes since v2: - new in v3 --- tools/ocaml/libs/eventchn/xeneventchn.ml | 6 +++- tools/ocaml/libs/eventchn/xeneventchn.mli | 9 +++++- tools/ocaml/libs/eventchn/xeneventchn_stubs.c | 28 +++++++++++++++++-- 3 files changed, 38 insertions(+), 5 deletions(-)