Skip to contentSkip to conversation

tools/ocaml: Dup2 /dev/null to stdin in daemonize()

PATCH In review Christian Lindig 4 participants 4 messages Feb 27, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tools/ocaml: Dup2 /dev/null to stdin in daemonize()

Don't close stdin in daemonize() but dup2 /dev/null instead. This avoids fd 0 being reused and potentially written to.

---
 tools/ocaml/xenstored/stdext.ml | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/ocaml/xenstored/stdext.ml +2 -2
index 879565c515..ffb516a0d4 100644
@@ -100,9 +100,9 @@ let daemonize () =
 
 		begin match Unix.fork () with
 		| 0 ->
-			let nullfd = Unix.openfile "/dev/null" [ Unix.O_WRONLY ] 0 in
+			let nullfd = Unix.openfile "/dev/null" [ Unix.O_RDWR] 0 in
 			begin try
-				Unix.close Unix.stdin;
+				Unix.dup2 nullfd Unix.stdin;
 				Unix.dup2 nullfd Unix.stdout;
 				Unix.dup2 nullfd Unix.stderr;
 			with exn -> Unix.close nullfd; raise exn
Signature · 7 lines
2.20.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Reviewed-by on v1 1/1
#1 lore raw
Quoted 4 lines · Christian Lindig

Don't close stdin in daemonize() but dup2 /dev/null instead. This avoids fd 0 being reused and potentially written to.

Possibly worth noting that this fixes a bug whereby /dev/xen/evtchn reliably gets opened on fd 0.

I can fix the wording up on commit if there are no other concerns.

Juergen for 4.12

Quoted 10 lines
---
 tools/ocaml/xenstored/stdext.ml | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/ocaml/xenstored/stdext.ml +0 -0
index 879565c515..ffb516a0d4 100644
@@ -100,9 +100,9 @@ let daemonize () =
 

tools/ocaml/xenstored/stdext.ml let daemonize () = · line 108

Quoted 10 lines
tools/ocaml/xenstored/stdext.ml +2 -2
 		begin match Unix.fork () with
 		| 0 ->
-			let nullfd = Unix.openfile "/dev/null" [ Unix.O_WRONLY ] 0 in
+			let nullfd = Unix.openfile "/dev/null" [ Unix.O_RDWR] 0 in
 			begin try
-				Unix.close Unix.stdin;
+				Unix.dup2 nullfd Unix.stdin;
 				Unix.dup2 nullfd Unix.stdout;
 				Unix.dup2 nullfd Unix.stderr;
 			with exn -> Unix.close nullfd; raise exn

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#2 lore raw
Quoted 6 lines · Andrew Cooper
Quoted 4 lines · Christian Lindig

Don't close stdin in daemonize() but dup2 /dev/null instead. This avoids fd 0 being reused and potentially written to.

Quoted 7 lines

Possibly worth noting that this fixes a bug whereby /dev/xen/evtchn reliably gets opened on fd 0.

I can fix the wording up on commit if there are no other concerns.

Juergen for 4.12

Juergen

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel