Skip to contentSkip to conversation

tools/ocaml/libs/xs: Create FDs with the correct CLOEXEC status

PATCH In review Andrew Cooper 2 participants 3 messages Oct 12 – Oct 26

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tools/ocaml/libs/xs: Create FDs with the correct CLOEXEC status

... rather than editing them after the fact.

This capability exists from Ocaml 4.05 which is also our minimum supported version.

---
CC: Andrii Sultanov <[email protected]>
CC: Guillaume Thouvenin <[email protected]>

Found while reviewing the other FD handling patch.

---
 tools/ocaml/libs/xs/xs.ml | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
tools/ocaml/libs/xs/xs.ml +2 -4
index addccc80099b..a088d67a8cb6 100644
@@ -155,9 +155,8 @@ exception Failed_to_connect
 let daemon_open () =
   try
     let sockaddr = Unix.ADDR_UNIX(daemon_socket) in
-    let sock = Unix.socket Unix.PF_UNIX Unix.SOCK_STREAM 0 in
+    let sock = Unix.socket ~cloexec:true Unix.PF_UNIX Unix.SOCK_STREAM 0 in
     Unix.connect sock sockaddr;
-    Unix.set_close_on_exec sock;
     make sock
   with _ -> raise Failed_to_connect
 
@@ -169,8 +168,7 @@ let domain_open () =
     with Unix.Unix_error(_, _, _) ->
       "/proc/xen/xenbus" in
 
-  let fd = Unix.openfile path [ Unix.O_RDWR ] 0o550 in
-  Unix.set_close_on_exec fd;
+  let fd = Unix.openfile path [ Unix.O_RDWR; Unix.O_CLOEXEC ] 0o550 in
   make fd
 
 let close xsh = Xsraw.close xsh.con

base-commit: 0d3bb3c98bc99ec1c84ebb3df71b6e18daebff4f

Signature · 1 line
2.39.5
Acked-by on v1 1/1
#1 lore raw
Quoted 41 lines · Andrew Cooper

... rather than editing them after the fact.

This capability exists from Ocaml 4.05 which is also our minimum supported version.

---
CC: Andrii Sultanov <[email protected]>
CC: Guillaume Thouvenin <[email protected]>

Found while reviewing the other FD handling patch.

---
  tools/ocaml/libs/xs/xs.ml | 6 ++----
  1 file changed, 2 insertions(+), 4 deletions(-)
tools/ocaml/libs/xs/xs.ml +2 -4
index addccc80099b..a088d67a8cb6 100644
@@ -155,9 +155,8 @@ exception Failed_to_connect
  let daemon_open () =
    try
      let sockaddr = Unix.ADDR_UNIX(daemon_socket) in
-    let sock = Unix.socket Unix.PF_UNIX Unix.SOCK_STREAM 0 in
+    let sock = Unix.socket ~cloexec:true Unix.PF_UNIX Unix.SOCK_STREAM 0 in
      Unix.connect sock sockaddr;
-    Unix.set_close_on_exec sock;
      make sock
    with _ -> raise Failed_to_connect
 
@@ -169,8 +168,7 @@ let domain_open () =
      with Unix.Unix_error(_, _, _) ->
        "/proc/xen/xenbus" in
 
-  let fd = Unix.openfile path [ Unix.O_RDWR ] 0o550 in
-  Unix.set_close_on_exec fd;
+  let fd = Unix.openfile path [ Unix.O_RDWR; Unix.O_CLOEXEC ] 0o550 in
    make fd
 
  let close xsh = Xsraw.close xsh.con
Quoted 1 line

base-commit: 0d3bb3c98bc99ec1c84ebb3df71b6e18daebff4f

The change on its own is fine - there's no functional impact. But as far as I can tell, these functions are not used anywhere (they haven't been used in xapi in a long time), so there's limited utility in this cleanup.

Regarding your question from the original thread, if this patch was intended as an answer to it:

Quoted 1 line

Where do these sockets come from?  What flags where they opened with?

The sockets that are dumped come from xenstored.ml:process_special_fds:

      let (cfd, _addr) = Unix.accept fd in

The default is keep-on-exec, so there shouldn't be any need to call Unix.clear_close_on_exec in the patches suggested by Andrew Mbugua.

#2 lore raw
Quoted 42 lines · Andrii Sultanov
Quoted 40 lines · Andrew Cooper

... rather than editing them after the fact.

This capability exists from Ocaml 4.05 which is also our minimum supported
version.

---
CC: Andrii Sultanov <[email protected]>
CC: Guillaume Thouvenin <[email protected]>

Found while reviewing the other FD handling patch.
---
  tools/ocaml/libs/xs/xs.ml | 6 ++----

  1 file changed, 2 insertions(+), 4 deletions(-)
tools/ocaml/libs/xs/xs.ml +0 -0
index addccc80099b..a088d67a8cb6 100644
@@ -155,9 +155,8 @@ exception Failed_to_connect

  let daemon_open () =
    try
      let sockaddr = Unix.ADDR_UNIX(daemon_socket) in
-    let sock = Unix.socket Unix.PF_UNIX Unix.SOCK_STREAM 0 in +    let sock = Unix.socket ~cloexec:true Unix.PF_UNIX Unix.SOCK_STREAM 0 in
      Unix.connect sock sockaddr;
-    Unix.set_close_on_exec sock;
      make sock
    with _ -> raise Failed_to_connect
  @@ -169,8 +168,7 @@ let domain_open () =
      with Unix.Unix_error(_, _, _) ->
        "/proc/xen/xenbus" in
  -  let fd = Unix.openfile path [ Unix.O_RDWR ] 0o550 in
-  Unix.set_close_on_exec fd;
+  let fd = Unix.openfile path [ Unix.O_RDWR; Unix.O_CLOEXEC ] 0o550 in make fd
    let close xsh = Xsraw.close xsh.con

Quoted 6 lines
Quoted 1 line

base-commit: 0d3bb3c98bc99ec1c84ebb3df71b6e18daebff4f

The change on its own is fine - there's no functional impact. But as far as I
can tell, these functions are not used anywhere (they haven't been used in xapi
in a long time), so there's limited utility in this cleanup.

Agreed, but it's still better to have code be correct, in case anyone else is using it.

Independently, we should see about pruning the libraries which aren't used.  Sadly, they're all mutually entangled.

Quoted 7 lines

Regarding your question from the original thread, if this patch was intended
as an answer to it:

Quoted 1 line

Where do these sockets come from?  What flags where they opened with?

Quoted 9 lines

The sockets that are dumped come from xenstored.ml:process_special_fds:

      let (cfd, _addr) = Unix.accept fd in

The default is keep-on-exec, so there shouldn't be any need to call Unix.clear_close_on_exec in the patches suggested by Andrew Mbugua.

Thanks.

~Andrew