How do I reserve a list of ports for my custom applications?
To be specific, the product I'm creating has a lot of processes and a lot of intercommunication between them.
The problem I'm having is that - every once in a while - the OS steals my ports. It's rare, but it happens.
This could be because a different application has used "::bind" with no port specified.
Or sometimes my own applications steal the port when I call "::connect" with an unbound socket. As seen from the man page:
If the socket has not already been bound to a local address, connect() shall bind it to an address which, unless the socket's address family is AF_UNIX, is an unused local address.
So my question is, can I reserve the ports that I need so the OS doesn't use them? Can this be accomplished with /etc/services? Or is there a different way?
SELinux
in Enforcing mode can meet your requirement, I'm still learning on it. So just a guess, maybe you can define your own policy forSELinux
to reserve yours ports, such asmy_server_port_t tcp 1111, 2222, 3333, 4444-4600
. If your application will run everywhere (not a server application), I'm afraid you can't control whetherSELinux
is ON or OFF. – LiuYan 刘研 Jun 24 '11 at 01:58