Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

passing file/socket descriptors between processes 1

Status
Not open for further replies.

sedj

Programmer
Aug 6, 2002
5,610
Hi,

My googling skills appear to be poor today.

Has anyone any idea how (or what commands/methods I should be searching for) to pass a socket descriptor from a parent process to a forked new process ?

For example :

Code:
int iSockFd = // my socket from an accept() method

if (!fork()) {
  execlp("/a/new/executable", "new_process", 0);
  // and pass the iSockFd to this executable
} else {
  wait(NULL);
}

I have an inkling I need to use pipe() and dup(), but I'm not entirely sure how to use them in the parent or the child exe, and I'm darned if I can find any tutorials/examples out there !

Cheers
 
execve manual page said:
execve() does not return on success, and the text, data, bss, and stack
of the calling process are overwritten by that of the program loaded.
The program invoked inherits the calling process’s PID, and any open
file descriptors that are not set to close on exec. Signals pending on
the calling process are cleared. Any signals set to be caught by the
calling process are reset to their default behaviour. The SIGCHLD sig?
nal (when set to SIG_IGN) may or may not be reset to SIG_DFL.

The quick way - after the fork(), close stdin and dup your sockfd to be the new stdin, then exec your process. It should then be reading the socket as if it were stdin.

The long way is to do some of the following
1. use fcntl() to reset the close-on-exec bit of the sockfd, so that it remains open in the new process. The relevant manual page snip is this I believe
Code:
       F_SETFD
              Set  the  close?on?exec  flag  to  the  value  specified  by the
              FD_CLOEXEC bit of arg.
2. Communicate the actual value of sockfd to the new process, using a command line argument. Say
Code:
if (!fork()) {
  char fdstring[100];
  sprintf( fdstring, "-sock=%d", iSockFd );  // format to taste
  execlp("/a/new/executable", "new_process", fdstring, 0);
  // and pass the iSockFd to this executable
} else {
  wait(NULL);
}
The new executable would need to parse it's argc/argv to then recover the actual value of the sockfd to be used.

--
 
Thankyou Salem, I'll give those methods a try.

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
The correct and portable way in *nix to pass a file
descriptor involves some sys specific gymnastics.
Please refer to Stevens: "Advanced Programming in the Unix
Environment" pg. 479-513


 
In the end I went for fcntl() and passed the sockfd as an int on the programme args.
Thanks for the help !

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top