http://qs1969.pair.com?node_id=171378


in reply to Passing a socket to a parent

On a (sufficiently) UNIX system when you create a child process by forking, the child process inherits open file descriptors from the parent (server parents most often use this to share the "listen" socket with a host of children). What you want to do is take a file descriptor created and obtained by the "child" to be handed back to the parent. I don't think that is possible (again, in UNIX).

You can certainly keep the child around and have it use a preexisting connection with the parent to keep using the connection it created. So everytime you create a child, have a file descriptor created between the parent and the child that is used by the child to "forward" traffic back to the parent. Then when the child starts, it can attempt a connection with the server and if succeeds, it tells the parent: which kills all the other children, and then deals with the child as the forwarder to the socket.

Another way to do this could be to have siblings that go at it, and the first successful connection has the parent kill all the other children. Then the successful child continues as the main application, while the parent hangs around to handle the signals etc. Coordination is still required, but the child does not have to be coded as a special gateway.

You can also use pipes and redirects to use STDIN/OUT for these purposes, but one thing I am pretty sure of is that the socket will be killed (shutdown) if your child that created it exits.

-- termix

Replies are listed 'Best First'.
Re: Re: Passing a socket to a parent
by belg4mit (Prior) on Jun 04, 2002 at 02:37 UTC
    This is in fact possible with UN*X. It's just not easy. It is exactly what lingerd does

    --
    perl -pew "s/\b;([mnst])/'$1/g"

Re: Re: Passing a socket to a parent
by suaveant (Parson) on Jun 04, 2002 at 02:02 UTC
    Doh! Yeah.. I'm using Linux...

    I thought you could pass a socket, similar to the way you can pass an open filehandle, but I certainly could be wrong...

                    - Ant
                    - Some of my best work - (1 2 3)