Extract static method for daemonization for testing
@@ -372,6 +372,15 @@ class Daemonizer(SimplePlugin):
'Daemonizing now may cause strange failures.' %
threading.enumerate(), level=30)
+ self.daemonize(self.stdin, self.stdout, self.stderr, self.bus.log)
+
+ self.finalized = True
+ start.priority = 65
+
+ @staticmethod
+ def daemonize(
+ stdin='/dev/null', stdout='/dev/null', stderr='/dev/null',
+ logger=lambda msg: None):
# See http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC16
# (or http://www.faqs.org/faqs/unix-faq/programmer/faq/ section 1.7)
# and http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66012
@@ -388,7 +397,7 @@ class Daemonizer(SimplePlugin):
pass
else:
# This is the first parent. Exit, now that we've forked.
- self.bus.log('Forking once.')
+ logger('Forking once.')
os._exit(0)
except OSError:
# Python raises OSError rather than returning negative numbers.
@@ -402,7 +411,7 @@ class Daemonizer(SimplePlugin):
try:
pid = os.fork()
if pid > 0:
- self.bus.log('Forking twice.')
+ logger('Forking twice.')
os._exit(0) # Exit second parent
except OSError:
exc = sys.exc_info()[1]
@@ -411,9 +420,9 @@ class Daemonizer(SimplePlugin):
os.umask(0)
- si = open(self.stdin, 'r')
- so = open(self.stdout, 'a+')
- se = open(self.stderr, 'a+')
+ si = open(stdin, 'r')
+ so = open(stdout, 'a+')
+ se = open(stderr, 'a+')
# os.dup2(fd, fd2) will close fd2 if necessary,
# so we don't explicitly close stdin/out/err.
@@ -422,9 +431,7 @@ class Daemonizer(SimplePlugin):
os.dup2(so.fileno(), sys.stdout.fileno())
os.dup2(se.fileno(), sys.stderr.fileno())
- self.bus.log('Daemonized to PID: %s' % os.getpid())
- self.finalized = True
- start.priority = 65
+ logger('Daemonized to PID: %s' % os.getpid())
class PIDFile(SimplePlugin):
