Re: execve(): unexpected socket behavior



Simple Simon wrote:
Linux 2.6.11 i686
gcc 3.3.5
clib 2.3.4

I have a process which opens and writes to a TCP socket, which at regular intervals I replace with another instance of the process via execve().

Technically it remains the same process all the way through. What you have is another instance of the *program* running in the same *process*.

If I do a 'netstat -t', I see a growing list of ESTABLISHED connections, one for each execve()'ed instance, with only the current instance (most recent connection) actually used.

So: Each instance of the process is correctly inheriting the previous set of open descriptors. I have been unable to figure out how to reference them, however, so I can determine (in successive instances) when I already have an open socket and so don't have to open another one.

This reduces to detecting when an instance is the first instance of the process, and when it is an execve()'ed instance.

The socket descriptor is a file scope variable, so is automatically initialized to 0 when the process loads. I had thought that this variable would be non-zero upon successive instances, but that isn't the case (so testing it for 0 as a condition of calling socket() doesn't work).

I'm scratching my head trying to figure out how to reference the inherited descriptor.

Any comment or pointer to a illuminating reference appreciated.

Set an environment variable to the value of the file descriptor. Then, if the EV exists (and assuming correct use of the CLOEXEC flag), you already have an open descriptor and the value of the EV tells you what it is. Otherwise you need to make one (and export it).

HT
.



Relevant Pages

  • execve(): unexpected socket behavior
    ... I have a process which opens and writes to a TCP socket, ... The socket descriptor is a file scope variable, ... I'm scratching my head trying to figure out how to reference the ...
    (comp.unix.programmer)
  • Re: File mapping
    ... pipes with pipeand so forth. ... open function for all sorts of system specific devices. ... You can read and write from a socket using the file ... "r+") opens a TCP connection to Google (but it's up to you to ...
    (comp.unix.programmer)
  • Re: Socket access to low numbered ports?
    ... I'm now using sudo to launch the application which just opens the ... > listening sockets and then calls os.setuid to set the uid back to an ... AF_UNIX socket to pass the low ports back to your application through ...
    (comp.lang.python)
  • Re: j2me: reading long streams of data
    ... > deal with such conditions in j2me. ... > downfall to any program that opens a socket on a handheld device and ...
    (comp.lang.java.programmer)
  • Re: Track the users on line?
    ... opens a socket to the webserver ... closes the socket. ... not to keep socket open between requests. ... If he try again to log from a new browser, the check for the flag doesn't ...
    (microsoft.public.dotnet.framework.aspnet)