LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-27-2010, 04:29 AM   #1
Ashregan
LQ Newbie
 
Registered: Aug 2010
Location: Switzerland
Distribution: CentOS
Posts: 4

Rep: Reputation: 0
Question Server socket assigned by children processes ?


Hello Forum

I fond so many good tips here that I hope someone will be able to help me with my problem also.

I am currently programming a suite of daemons in c++ on CentOS 64.
More or less all those daemons are SOAP servers, with the SOAP part handled by gSOAP.

One of the daemons is a monitor sitting on a certain server and more or less just checking if the other daemons are up and running - but also offering a SOAP interface to get the 'current situation' on the machine.

Whenever the detected situation does not correspond to the defined (i.E. a process which should be is not running) it is started up.

This part, I implemented via a system() call that starts the other process.

I was a little confused when lately often this monitoring process would not start because it could not bind its server port - the one the SOAP server is listening on.

And confusion got bigger when netstat told me that the port is assigned to one of the processes that were before started by the monitor..?

Even better: if I then kill the process of which netstat tells me it holds the port, the next netstat shows the next monitor-started process as the one listening on the port..

Only if I kill all the processes that were started by the monitor, the port is released and I am able to launch the monitor again.

Now I wonder: why does this port get 'reassigned' to the children - and most of all: how can I prevent this from happening?

Any help greatly appreciated
 
Old 08-29-2010, 08:14 AM   #2
estabroo
Senior Member
 
Registered: Jun 2008
Distribution: debian, ubuntu, sidux
Posts: 1,126
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
It's not some much that they get reassigned to the children, it's that the children have it open as well. I'm guessing you opened the socket then forked the children, just close the socket in the children before doing anything else with the child process
 
Old 08-30-2010, 03:16 AM   #3
Ashregan
LQ Newbie
 
Registered: Aug 2010
Location: Switzerland
Distribution: CentOS
Posts: 4

Original Poster
Rep: Reputation: 0
Thanks fo replying.

There is no fork in the monitor - I just call the system() function with the command that starts the child, plus a "&" to have it detached from the shell.

Is there a way to get the children really started 'from scratch' - meaning without them 'having the port open as well' ?

Just like if they are started from command line (which I thought would be the case when using the system() command..)
 
Old 08-30-2010, 07:43 AM   #4
estabroo
Senior Member
 
Registered: Jun 2008
Distribution: debian, ubuntu, sidux
Posts: 1,126
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
you could use a fork/exec instead of that system call, then you'd be able to set the behavior you'd like, otherwise I'd guess you could set the close on exec flag on the socket (using fcntl), that'd probably work as well
 
Old 08-30-2010, 01:00 PM   #5
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 116Reputation: 116
Calling system() IS a fork. You are just choosing to use a specific set of parameters when you do it that way.
 
Old 08-31-2010, 02:41 AM   #6
Ashregan
LQ Newbie
 
Registered: Aug 2010
Location: Switzerland
Distribution: CentOS
Posts: 4

Original Poster
Rep: Reputation: 0
jiml8 - you are perfectly right, of course: system() IS also a fork.

Well, I now tried a fork / execv combination, anyway, but I'm still as far as I was before.

What I did was to first fork, then close the server in the child process, then replace the process image by execv.

Unfortunately this didn't work out, as the server terminated in the parent as well (it is implemented as a singleton, guess that's the reason).
I probably really have to somehow close this socket, either in the child process after the fork, or in the startup of the other programs.

The first seems preferrable to me - the second way would require every program that gets started by the monitor to have these closing machanisms included.
But there I still have a problem understanding how this works. The fork will 'duplicate' the process, apparently including this listening socket.
To close this socket, I need to have its file descriptor - which has also been duplicated and therefore is not the original one anymore.
So the question would be: how do I, after the fork, find out which file descriptor I have to close in the child process line ?

Or is there some 'close all' command that would help here?
 
Old 08-31-2010, 03:38 AM   #7
Ashregan
LQ Newbie
 
Registered: Aug 2010
Location: Switzerland
Distribution: CentOS
Posts: 4

Original Poster
Rep: Reputation: 0
Finally I think I got it: fcntl(sock, F_SETFD, FD_CLOEXEC) has to be called on the server socket.

Now I just have to figure out how to get to that fd, which is hidden by the gSoap framework.

Thanks everyone for helping!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to share socket between processes Aga Programming 16 06-24-2020 05:32 PM
[SOLVED] Child processes of a Parent Process occupying socket memory gaurav.rai1984@gmail.com Linux - Newbie 2 08-03-2010 03:27 AM
Share socket within different processes servel Programming 8 05-26-2009 08:12 PM
broadcast (udp) socket - multi processes emarri Programming 1 09-25-2006 06:06 AM
cannot read data at server socket, though client socket sends it jacques83 Linux - Networking 0 11-15-2005 01:58 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:27 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration