Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[Caml-list] Unix subdaemon, channels, filedescriptors....
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Basile STARYNKEVITCH <basile.starynkevitch@c...>
Subject: [Caml-list] Unix subdaemon, channels, filedescriptors....

Dear All,

Sorry to coming back on channels and unix filedescriptors....

My problem:

I am coding in Ocaml a daemon program (the Poesia monitor - see which happens to launch daemon
subprocesses, i.e. it fork and execs processes which do not use their
stdin stdout and stderr. Basically I would need something like a
Unix.process_none which forks a command without any stdin stdout and
stderr (which are technically redirected to /dev/null).

In practice I expect my poesia monitor to have lots (=hundreds) of
opened file descriptors, and I would like the child process to have no
file descriptor opened except stdin stdout stderr

I would like to close all the filedescriptors after the
fork. Unfortunately, the caml_out_channels_list primitive (coded in C
in ocaml/byterun/io.c), which is used in stdlib/ is not
published in pervasives.mli, and there is no caml_in_channels_list

May I suggest publishing this primitive in the future ocaml v3.05?

Also, Unix.open_process_in and related seems to forget closing the
file descriptors in the child process, as this example (running only
on Linux) demonstrates:

  #load "unix.cma";;
  (* opening bogus stuff *)
  let fooch = open_out "/tmp/foo" and barch = open_out "/tmp/bar" ;;

  (* demonstrating that the opened process has fooch and barch still opened *)
  let lsch = Unix.open_process_in "ls -l /proc/self/fd" in
  let rec loop () = 
          Printf.printf "%s\n" (input_line lsch); flush stdout;
	  loop ()
      with End_of_file -> ()
  loop ();
  Unix.close_process_in lsch

When running this stuff, I am getting

total 0
lr-x------    1 basile   basile         64 Jul 17 15:57 0 -> pipe:[4700690]
l-wx------    1 basile   basile         64 Jul 17 15:57 1 -> pipe:[4700695]
l-wx------    1 basile   basile         64 Jul 17 15:57 2 -> pipe:[4700689]
l-wx------    1 basile   basile         64 Jul 17 15:57 3 -> /tmp/foo
l-wx------    1 basile   basile         64 Jul 17 15:57 4 -> /tmp/bar
lr-x------    1 basile   basile         64 Jul 17 15:57 5 -> /proc/18709/fd

I think that the child process (running the command) should not have
other opened filedescriptors than the documented stdin, stdout,
stderr, or that this behavior should be documented

How do I close (in Ocaml) every channel except stdin, stdout, stderr?



N.B. Any opinions expressed here are only mine, and not of my organization.
N.B. Les opinions exprimees ici me sont personnelles et n engagent pas le CEA.

Basile STARYNKEVITCH   ----  Commissariat à l Energie Atomique * France
DRT/LIST/DTSI/SLA * CEA/Saclay b.528 (p111f) * 91191 GIF/YVETTE CEDEX 
phone:+33 1,6908.6055; fax: 1,6908.8395 home: 1,4665.4553; mobile: 6,8501.2359
work email: Basile point Starynkevitch at cea point fr 
home email: Basile at Starynkevitch point net

To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: