I have a linux application written in c++. The application listens to a socket on a certain port. I implemented this using ACE Acceptor. In addition the application starts postgresql database using the init script /etc/init.d/postgresql start by calling the ACE_OS::system function.
The problem I am having is: When the application exits, the port is still occupied. When I run netstat I see that the postgres is listening to that port. (This only happens if I start postgres from the application on any given port).
Is there a way to close the port? Why does postgres listen to that port?
Is there a way to close the port?
Yes. Close the socket, or set FD_CLOEXEC on the underlying file descriptor.
Or ... wrap your call to the child process (...postgresql start
) with something that will close fds higher than stderr:
ACE_OS::system("perl -MPOSIX -e 'POSIX::close($_) for 3 .. sysconf(_SC_OPEN_MAX); exec @ARGV' /etc/init.d/postgresql start");
or similar. Tuck that in a script to make it look nicer.
Why does postgres listen to that port?
Your child processes (and their children) are inheriting your open file descriptors, including the socket your c++ app opens.