From 84847a4ebc8b89b9dac2efc0e133b43cb762a381 Mon Sep 17 00:00:00 2001 From: kaliko Date: Wed, 25 May 2011 19:50:42 +0200 Subject: [PATCH] Writting pid before decoupling ain't working. Setting umask behaves better. --- src/daemon.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/daemon.py b/src/daemon.py index 7489233..c3ec539 100644 --- a/src/daemon.py +++ b/src/daemon.py @@ -28,6 +28,7 @@ class Daemon(object): self.stdout = stdout self.stderr = stderr self.pidfile = pidfile + self.umask = 0 def daemonize(self): """ @@ -47,7 +48,7 @@ class Daemon(object): # Decouple from parent environment os.chdir("/") os.setsid() - os.umask(0) + self.umask = os.umask(0) # Do second fork try: @@ -59,6 +60,7 @@ class Daemon(object): sys.stderr.write("fork #2 failed: %d (%s)\n" % (e.errno, e.strerror)) sys.exit(1) + self.write_pid() # redirect standard file descriptors sys.stdout.flush() sys.stderr.flush() @@ -78,12 +80,14 @@ class Daemon(object): return pid = str(os.getpid()) try: + os.umask(self.umask) file(self.pidfile, 'w').write('%s\n' % pid) #except IOError, wpid_err: except Exception, wpid_err: sys.stderr.write(u'Error trying to write pid file to %s: %s\n' % (unicode(self.pidfile, 'utf-8'), wpid_err)) sys.exit(1) + os.umask(0) atexit.register(self.delpid) def signal_management(self): @@ -94,7 +98,7 @@ class Daemon(object): sys.exit(1) def delpid(self): - os.remove(self.pidfile) + os.unlink(self.pidfile) def start(self): """ @@ -113,7 +117,6 @@ class Daemon(object): sys.stderr.write(message % self.pidfile) sys.exit(1) - self.write_pid() # Start the daemon self.daemonize() self.run() -- 2.39.5