From 892547c4def6f8915a3ab891ec65a473a06aa913 Mon Sep 17 00:00:00 2001 From: Fabien Boucher Date: Mon, 11 Oct 2010 18:22:37 +0200 Subject: [PATCH 1/2] First version of daemonizer. --- python/daemonize.py | 54 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 python/daemonize.py diff --git a/python/daemonize.py b/python/daemonize.py new file mode 100644 index 0000000..889dd00 --- /dev/null +++ b/python/daemonize.py @@ -0,0 +1,54 @@ +#!/usr/bin/env python +# +#Copyright 2010, Fabien Boucher +# +#This program is free software: you can redistribute it and/or modify +#it under the terms of the GNU General Public License as published by +#the Free Software Foundation, either version 3 of the License, or +#(at your option) any later version. +# +#This program is distributed in the hope that it will be useful, +#but WITHOUT ANY WARRANTY; without even the implied warranty of +#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the- +#GNU General Public License for more details. +# +#You should have received a copy of the GNU General Public License +#along with this program. If not, see . + +import sys + +from lockfile import LockFile +from daemon import DaemonContext + +class OcspDaemon(DaemonContext): + def __init__(self, redirect_output = '/tmp/ocsp_responder.log', + chroot = None, + detach = True, + pidfile = '/var/run/ocsp_responder.pid'): + + self.redirect_output = file(redirect_output, 'a') + self.chroot = chroot + self.detach = detach + self.pidfile = pidfile + if self.pidfile: + self.pidfile = LockFile(pidfile) + if not self.detach: + self.redirect_output = sys.stdout + + DaemonContext.__init__(self, + stdout = self.redirect_output, + stderr = self.redirect_output, + detach_process = self.detach, + pidfile = self.pidfile) + +# Simple test +if __name__ == '__main__': + from time import sleep + + def main(): + print 'begin to work' + sleep(30) + print 'stop to work' + + with OcspDaemon(detach = False, pidfile = None): + main() From c7ddc52aea9c8fa41f6af441cd4ec348948eeb35 Mon Sep 17 00:00:00 2001 From: fabien Date: Mon, 11 Oct 2010 20:59:59 +0200 Subject: [PATCH 2/2] Branch option deamon in ocspd.py --- python/daemonize.py | 7 ++++--- python/ocspd.py | 11 ++++++++--- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/python/daemonize.py b/python/daemonize.py index 889dd00..f3cc348 100644 --- a/python/daemonize.py +++ b/python/daemonize.py @@ -20,16 +20,17 @@ from lockfile import LockFile from daemon import DaemonContext -class OcspDaemon(DaemonContext): +class OCSPDaemon(DaemonContext): def __init__(self, redirect_output = '/tmp/ocsp_responder.log', chroot = None, detach = True, - pidfile = '/var/run/ocsp_responder.pid'): + pidfile = '/tmp/ocsp_responder.pid'): self.redirect_output = file(redirect_output, 'a') self.chroot = chroot self.detach = detach self.pidfile = pidfile + if self.pidfile: self.pidfile = LockFile(pidfile) if not self.detach: @@ -50,5 +51,5 @@ def main(): sleep(30) print 'stop to work' - with OcspDaemon(detach = False, pidfile = None): + with OCSPDaemon(detach = False, pidfile = None): main() diff --git a/python/ocspd.py b/python/ocspd.py index a4197d5..0c71d27 100644 --- a/python/ocspd.py +++ b/python/ocspd.py @@ -17,6 +17,7 @@ from optparse import OptionParser from ocspserver import OCSPServer +from daemonize import OCSPDaemon from config import ConfigObject version = 0.1 @@ -44,8 +45,8 @@ def _keep_running(): def _init_parser(): parser = OptionParser(usage=usage) - #parser.add_option("-d", "--daemon", - # help="Daemon, detach from current console") + parser.add_option("-d", "--daemon", + help="Daemon, detach from current console") #parser.add_option("-r", "--chroot", # help="Directory where to jail the process") parser.add_option("-p", "--port", default=2560, @@ -75,4 +76,8 @@ def main(): else: cfg = _parse_config(options.config) - _main_loop(cfg) + if options.daemon: + with OCSPDaemon(): + __main_loop(cfg) + else: + _main_loop(cfg)