caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Unix subdaemon, channels, filedescriptors....
@ 2002-07-17 14:06 Basile STARYNKEVITCH
  2002-07-17 16:05 ` Bruno.Verlyck
  2002-07-19 13:08 ` Xavier Leroy
  0 siblings, 2 replies; 3+ messages in thread
From: Basile STARYNKEVITCH @ 2002-07-17 14:06 UTC (permalink / raw)
  To: caml-list


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
http://www.poesia-filter.org/) 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/pervasives.ml is not
published in pervasives.mli, and there is no caml_in_channels_list
primitive

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 () = 
      try 
          Printf.printf "%s\n" (input_line lsch); flush stdout;
	  loop ()
      with End_of_file -> ()
  in
  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?

Regards.

-- 

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 caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] Unix subdaemon, channels, filedescriptors....
  2002-07-17 14:06 [Caml-list] Unix subdaemon, channels, filedescriptors Basile STARYNKEVITCH
@ 2002-07-17 16:05 ` Bruno.Verlyck
  2002-07-19 13:08 ` Xavier Leroy
  1 sibling, 0 replies; 3+ messages in thread
From: Bruno.Verlyck @ 2002-07-17 16:05 UTC (permalink / raw)
  To: basile.starynkevitch; +Cc: caml-list

   Date: Wed, 17 Jul 2002 16:06:31 +0200
   From: Basile STARYNKEVITCH <basile.starynkevitch@cea.fr>

   I would like to close all the filedescriptors after the fork.
This is what set_close_on_exec is for, except it's after the exec.
Is it enough ?  (You wrote `it fork and execs processes').

   Unfortunately, the caml_out_channels_list primitive (coded in C in
   ocaml/byterun/io.c), which is used in stdlib/pervasives.ml is not
   published in pervasives.mli, and there is no caml_in_channels_list
   primitive

   May I suggest publishing this primitive in the future ocaml v3.05?
Do you mean: these two primitives ?

BTW, all this stuff was added to support Cash (now you know what is my
next suggestion :-).  Since flush_all was considered of general
interest, caml_out_channels_list was integrated to the main sources to
implement it.  Had flush_all not been added to Pervasives, it would
have stayed in the C part of Cash.  So to get caml_out_channels_list
right now, an external declaration is enough.  For
caml_in_channels_list, 20 lines of C will do (1).

   Also, Unix.open_process_in and related seems to forget closing the file
   descriptors in the child process, 
[..]
   How do I close (in Ocaml) every channel except stdin, stdout, stderr?
In the next version (and in the CVS sources), every channel opened by
Pervasives is marked by set_close_on_exec, so this problem should go
away.

Bruno.

(1) At first sight, copy caml_out_channels_list, and replace
    if (channel->max == NULL) {
by
    if (channel->max != NULL && channel->fd >= 0) {
(Untested).
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] Unix subdaemon, channels, filedescriptors....
  2002-07-17 14:06 [Caml-list] Unix subdaemon, channels, filedescriptors Basile STARYNKEVITCH
  2002-07-17 16:05 ` Bruno.Verlyck
@ 2002-07-19 13:08 ` Xavier Leroy
  1 sibling, 0 replies; 3+ messages in thread
From: Xavier Leroy @ 2002-07-19 13:08 UTC (permalink / raw)
  To: Basile STARYNKEVITCH; +Cc: caml-list

> I am coding in Ocaml a daemon program (the Poesia monitor - see
> http://www.poesia-filter.org/) 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).

This can easily be written in terms of Unix.fork and Unix.exec; you
can use the sources for the Unix.process_* functions as a starting point.

> 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:

This is standard Unix behavior: file descriptors are preserved across
"exec", unless they were set to "close on exec" mode before the exec
(see Unix.set_close_on_exec).  

- Xavier Leroy
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2002-07-19 13:08 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-07-17 14:06 [Caml-list] Unix subdaemon, channels, filedescriptors Basile STARYNKEVITCH
2002-07-17 16:05 ` Bruno.Verlyck
2002-07-19 13:08 ` Xavier Leroy

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).