traveler has asked for the wisdom of the Perl Monks concerning the following question:

The Camel book says that "perl always sets the close-on-exec flag for file descriptors above 2". When I open sockets with IO::Socket::INET->new() and I fork then exec a child, the sockets are still open in the child. Is the Camel book wrong or does something in IO::Socket::INET->new() behave differently than the Camel book indicates. A grep for fcntl and the CLOEXEC flag didn't turn up anything, nor did a search on perlmonks. If there is an fcntl or something changing the behavior, can someone give me a hint as to why, please? (I'm on Linux 2.2.17; perl 5.005_03).
IO::Socket::INET->new(Listen => 5, LocalAddr => 'localhost', LocalPort => 2000, Proto => 'udp'); #... exec "otherprog" if fork == 0;
Thanks, --traveler

Replies are listed 'Best First'.
Re: sockets close-on-exec
by LD2 (Curate) on May 30, 2001 at 06:26 UTC
    traveler,

    This doesn't really answer your question, but is more of a comment - you may want to use system rather than exec in your code.

    Update: You may want to invest and purchase a copy of the Perl Cookbook. From pg. 621
    Closing a Socket After Forking
    - use $socket->shutdown(0);
    There is more detail you may want to read up on as well.
      Unfortunately "system" does not allow me other flexibility I need. Update: I do have the perl cookbook. It is quite useful. I know about shutdown, too. The problems are that 1) "system" does not let me launch multiple children and track them by waiting for the DIE signals and 2) some of the sockets are not created by IO::Socket but by modules written by others.

      The query was why the close-on-exec does not seem to work. I know that I could keep track of every socket I create and close it, but I'd like to know if there is some accidental way I am foiling the close-on-exec.