daemonizing after binding to port

M

mk

Hello everyone,

I'm starting a SocketServer.TCPServer in my program, but since I want to
report problems to script starting the program, I want to go daemon
*after* TCPServer has done binding to port.

Is this likely to cause problems? I mean, my client works when I do the
above, that is, it connects to the server. But I'm not sure if above is
the Right Thing(tm).

First:

self.server = SocketServer.TCPServer((host, port), handleclass,
bind_and_activate=False)
self.server.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.server.server_bind()
self.server.server_activate()

(except etc skipped for brevity)

Second:

def daemonize(self):
try:
pid = os.fork()
except OSError, e:
print 'failed to fork:', str(e)
os._exit(1)
# if pid is non-zero, we're in parent
if pid:
os._exit(0)
os.setsid()
os.chdir(globalpath)
os.umask(0)




Regards,
mk
 
N

Nobody

There is more to becoming a well-behaved daeon process than merely
detaching the process. For details, see PEP 3143 “Standard daemon
process library†<URL:http://www.python.org/dev/peps/pep-3143/>.

The OP's code does most of what the Linux/BSD daemon() call does, except
that it doesn't close or replace inherited descriptors.

Many of the recommendations in PEP 3143 are "optional extras".

The main "requirement" which cannot reasonably be encapsulated into a
daemon() procedure is to avoid subsequently acquiring a controlling
terminal (by passing O_NOCTTY to all open() calls). Even if Python was to
integrate this into open() (and similar), there's no guarantee that
libraries used via extension modules or ctypes will do likewise.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top