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

I have a program which waits for a socket connection and for each connection forks to do work.

What I would like to do is rewrite this to have the main program act like an operator and accept connections. When it makes a new connection it should call the latest version of another program which will then do it's work via the established socket.

The idea is to be able to update the code of the program being called without interrupting service.

Is this kind of thing possible?

Replies are listed 'Best First'.
Re: Using latest code
by holli (Abbot) on Sep 01, 2007 at 19:50 UTC
    You can use system to call another program. As for the version thing, the (imho) easiest way would be to store the actual version number of each "other program" somewhere on disk and read that info upon each request. Store the programs in a "version_number/program" structure and construct the call path accordingly.


    holli, /regexed monk/
      I thought about using a system call but how can I handle passing the socket connection as-is? The operator or switchboard program has already established a connection with the user and validated them. I'd like to hand off this connection to another program to then handle the rest of the actions.
        Well, that brings a new problem into play.
        Honestly, I have no idea how to share an already opened socket via system. I think I would just put the "establishing and validating" code into a module and use that from the versioned scripts.


        holli, /regexed monk/

        Ever heard of inetd? Or NPH CGI? It is quite easy to pass an open socket to a child process.

        The way to do that with system (avoids fork amd exec and so is more portable) is simply to dup the socket to be STDIN and/or STDOUT (file descriptors 0 and/or 1, since file handles don't get inherited).

        Use the sample code in open if you want to save then restore your existing STDIN/STDOUT, then just do:

        # (save STDIN/STDOUT here) open STDIN, "<&".fileno(*SOCK) or die "Can't dup SOCK to STDIN: $!$/"; open STDOUT, ">&".fileno(*SOCK) or die "Can't dup SOCK to STDOUT: $!$/"; system( ... ) and ...; # (restore STDIN/STDOUT here)

        Perl is smart enough to ensure that STDIN remains fd 0 and STDOUT remains fd 1 for the above.

        - tye        

Re: Using latest code
by shmem (Chancellor) on Sep 02, 2007 at 08:38 UTC
    The idea is to be able to update the code of the program being called without interrupting service.
    <plug mode="shameless">

    If you are calling perl, you can do that with AutoReloader. It handles code updates for running processes. I've presented that one in a meditation some time ago.

    </plug>

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}