]> kaliko git repositories - python-daemon.git/commitdiff
Writting pid before decoupling ain't working. Setting umask behaves better.
authorkaliko <efrim@azylum.org>
Wed, 25 May 2011 17:50:42 +0000 (19:50 +0200)
committerkaliko <efrim@azylum.org>
Wed, 25 May 2011 17:50:42 +0000 (19:50 +0200)
src/daemon.py

index 7489233bddbb8e84a297cde3b5288cffc480f983..c3ec53919d29a5439a27f6ea2549a5c9bc17dbb5 100644 (file)
@@ -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()