diff options
author | Jeroen van Meeuwen (Kolab Systems) <vanmeeuwen@kolabsys.com> | 2011-11-09 23:00:30 +0000 |
---|---|---|
committer | Jeroen van Meeuwen (Kolab Systems) <vanmeeuwen@kolabsys.com> | 2011-11-09 23:00:30 +0000 |
commit | a5b0049c34e74c5725f31c615254464fe8d61de8 (patch) | |
tree | 7cac92ef20f3674bb21a0c128bfe4f18848e2573 | |
parent | 44169b3a26e74ef4e2dcebd35cdbe938d1f11b90 (diff) | |
download | pykolab-a5b0049c34e74c5725f31c615254464fe8d61de8.tar.gz |
Add signal handlers to kolabd and saslauthd
Add -p / --pid-file option to these daemons
-rw-r--r-- | kolabd/__init__.py | 34 | ||||
-rw-r--r-- | saslauthd/__init__.py | 33 |
2 files changed, 62 insertions, 5 deletions
diff --git a/kolabd/__init__.py b/kolabd/__init__.py index 56bfe9b..c749ae6 100644 --- a/kolabd/__init__.py +++ b/kolabd/__init__.py @@ -34,8 +34,6 @@ import traceback import pykolab from pykolab.auth import Auth -from pykolab.conf import Conf -from pykolab.imap import IMAP from pykolab.constants import * from pykolab.translate import _ @@ -59,6 +57,12 @@ class KolabDaemon(object): default = False, help = _("Fork to the background.")) + daemon_group.add_option( "-p", "--pid-file", + dest = "pidfile", + action = "store", + default = "/var/run/kolabd/kolabd.pid", + help = _("Path to the PID file to use.")) + conf.finalize_conf() self.thread_count = 0 @@ -77,8 +81,11 @@ class KolabDaemon(object): if pid == 0: log.remove_stdout_handler() - - self.do_sync() + self.write_pid() + self.set_signal_handlers() + self.do_sync() + elif not conf.fork_mode: + self.do_sync() except SystemExit, e: exitcode = e @@ -134,3 +141,22 @@ class KolabDaemon(object): %(primary_domain, (end_time-start_time)) ) domain_auth[primary_domain].synchronize(primary_domain, secondary_domains) + + def reload_config(self): + pass + + def remove_pid(self): + if os.access(conf.pidfile, os.R_OK): + os.remove(conf.pidfile) + raise SystemExit + + def set_signal_handlers(self): + import signal + signal.signal(signal.SIGHUP, self.reload_config) + signal.signal(signal.SIGTERM, self.remove_pid) + + def write_pid(self): + pid = os.getpid() + fp = open(conf.pidfile,'w') + fp.write("%d\n" %(pid)) + fp.close() diff --git a/saslauthd/__init__.py b/saslauthd/__init__.py index c74b39c..9756c54 100644 --- a/saslauthd/__init__.py +++ b/saslauthd/__init__.py @@ -52,8 +52,16 @@ class SASLAuthDaemon(object): default = False, help = _("Fork to the background.")) + daemon_group.add_option( "-p", "--pid-file", + dest = "pidfile", + action = "store", + default = "/var/run/kolabd/kolabd.pid", + help = _("Path to the PID file to use.")) + conf.finalize_conf() + self.thread_count = 0 + def run(self): """ Run the SASL authentication daemon. @@ -67,7 +75,10 @@ class SASLAuthDaemon(object): pid = os.fork() if pid == 0: + self.thread_count += 1 log.remove_stdout_handler() + self.set_signal_handlers() + self.write_pid() self.do_saslauthd() elif not conf.fork_mode: self.do_saslauthd() @@ -89,6 +100,7 @@ class SASLAuthDaemon(object): exitcode = 2 traceback.print_exc() print >> sys.stderr, _("Traceback occurred, please report a bug at http://bugzilla.kolabsys.com") + sys.exit(exitcode) def do_saslauthd(self): @@ -108,7 +120,7 @@ class SASLAuthDaemon(object): try: os.remove('/var/run/saslauthd/mux') except: - # TODO: Do the "could not remove, could not start dance" + # TODO: Do the "could not remove, could not start" dance pass s.bind('/var/run/saslauthd/mux') @@ -141,3 +153,22 @@ class SASLAuthDaemon(object): clientsocket.send(struct.pack("!H2s", 2, "NO")) clientsocket.close() + + def reload_config(self): + pass + + def remove_pid(self): + if os.access(conf.pidfile, os.R_OK): + os.remove(conf.pidfile) + raise SystemExit + + def set_signal_handlers(self): + import signal + signal.signal(signal.SIGHUP, self.reload_config) + signal.signal(signal.SIGTERM, self.remove_pid) + + def write_pid(self): + pid = os.getpid() + fp = open(conf.pidfile,'w') + fp.write("%d\n" %(pid)) + fp.close() |