diff mbox series

[for-4.17,v3,08/15] tools/ocaml/libs/evtchn: add xenevtchn_fdopen bindings

Message ID 3e7f15778ffb5d5d7adde8567c4d50197607f7b2.1667920496.git.edvin.torok@citrix.com (mailing list archive)
State New, archived
Headers show
Series OCaml fixes for Xen 4.17 | expand

Commit Message

Edwin Török Nov. 8, 2022, 3:34 p.m. UTC
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(-)

Comments

Christian Lindig Nov. 9, 2022, 1:49 p.m. UTC | #1
On 8 Nov 2022, at 15:34, Edwin Török <edvin.torok@citrix.com<mailto:edvin.torok@citrix.com>> wrote:

Signed-off-by: Edwin Török <edvin.torok@citrix.com<mailto: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(-)


Acked-by: Christian Lindig <christian.lindig@citrix.com<mailto:christian.lindig@citrix.com>>
diff mbox series

Patch

diff --git a/tools/ocaml/libs/eventchn/xeneventchn.ml b/tools/ocaml/libs/eventchn/xeneventchn.ml
index dd00a1f0ea..34f7391f5e 100644
--- a/tools/ocaml/libs/eventchn/xeneventchn.ml
+++ b/tools/ocaml/libs/eventchn/xeneventchn.ml
@@ -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
diff --git a/tools/ocaml/libs/eventchn/xeneventchn.mli b/tools/ocaml/libs/eventchn/xeneventchn.mli
index 08c7337643..3965b29621 100644
--- a/tools/ocaml/libs/eventchn/xeneventchn.mli
+++ b/tools/ocaml/libs/eventchn/xeneventchn.mli
@@ -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. *)
 
diff --git a/tools/ocaml/libs/eventchn/xeneventchn_stubs.c b/tools/ocaml/libs/eventchn/xeneventchn_stubs.c
index 2263c4caa1..92092ca31e 100644
--- a/tools/ocaml/libs/eventchn/xeneventchn_stubs.c
+++ b/tools/ocaml/libs/eventchn/xeneventchn_stubs.c
@@ -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)