diff --git a/python/daemonize.py b/python/daemonize.py new file mode 100644 index 0000000..f3cc348 --- /dev/null +++ b/python/daemonize.py @@ -0,0 +1,55 @@ +#!/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 = '/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: + 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() 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)