I have a C application that runs a shell script. The C programs uses the following logic (at a high level):
- C application listens on port 7791, call this "webClient".
- Once webClient gets data via port 7791, among other things, it calls the fork command.
- Inside child process, run execve with my shell script.
- The shell script does various activities, including starting another C daemon that should not be listening on any ports (does not use sockets at all). Call this C daemon "emaildae". Only one daemon is started, obviously in the background.
- The webClient application waits for child process to finish, reporting any problems.
- The webClient application goes back to listening on port 7791.
When I run netstat -nlp | grep 7791
, I see webClient listening, as I would expect. When I kill webClient, I now see emaildae listening on port 7791 (using netstat again). I have tried starting emaildae using nohup
and disown
, but neither nohup
by itself nor nohup
with disown
solve the problem. I tried closing the socket using close(socketFd)
in the child process, like I do in other places, but that did not work either. I know from various web searches that the socket descriptor gets passed to child processes, along with all file descriptors. What I don't know is how to prevent this from happening. Maybe I closed it wrong. If there is a way to close the socket descriptor without impacting the parent process, that might fix things. Any ideas?