2006-06-26 20:05:00 +00:00
|
|
|
# This file is part of Fail2Ban.
|
|
|
|
#
|
|
|
|
# Fail2Ban 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 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Fail2Ban 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 Fail2Ban; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
|
|
|
|
# Author: Cyril Jaquier
|
|
|
|
#
|
2008-07-21 14:13:13 +00:00
|
|
|
# $Revision: 696 $
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
__author__ = "Cyril Jaquier"
|
2008-07-21 14:13:13 +00:00
|
|
|
__version__ = "$Revision: 696 $"
|
|
|
|
__date__ = "$Date: 2008-05-19 23:05:32 +0200 (Mon, 19 May 2008) $"
|
2006-06-26 20:05:00 +00:00
|
|
|
__copyright__ = "Copyright (c) 2004 Cyril Jaquier"
|
|
|
|
__license__ = "GPL"
|
|
|
|
|
2006-11-06 22:48:46 +00:00
|
|
|
from threading import Lock, RLock
|
2006-09-17 21:59:29 +00:00
|
|
|
from jails import Jails
|
2006-06-26 20:05:00 +00:00
|
|
|
from transmitter import Transmitter
|
2008-03-06 01:19:06 +00:00
|
|
|
from asyncserver import AsyncServer
|
|
|
|
from asyncserver import AsyncServerException
|
|
|
|
from common import version
|
2006-09-25 17:03:48 +00:00
|
|
|
import logging, logging.handlers, sys, os, signal
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
# Gets the instance of the logger.
|
|
|
|
logSys = logging.getLogger("fail2ban.server")
|
|
|
|
|
|
|
|
class Server:
|
2008-03-06 01:19:06 +00:00
|
|
|
|
|
|
|
PID_FILE = "/var/run/fail2ban/fail2ban.pid"
|
2006-10-24 19:40:51 +00:00
|
|
|
|
2006-08-23 19:53:09 +00:00
|
|
|
def __init__(self, daemon = False):
|
2006-11-06 22:48:46 +00:00
|
|
|
self.__loggingLock = Lock()
|
|
|
|
self.__lock = RLock()
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__jails = Jails()
|
|
|
|
self.__daemon = daemon
|
|
|
|
self.__transm = Transmitter(self)
|
2008-03-06 01:19:06 +00:00
|
|
|
self.__asyncServer = AsyncServer(self.__transm)
|
2008-07-21 14:13:13 +00:00
|
|
|
self.__logLevel = None
|
|
|
|
self.__logTarget = None
|
2006-06-26 20:05:00 +00:00
|
|
|
# Set logging level
|
2008-07-21 14:13:13 +00:00
|
|
|
self.setLogLevel(3)
|
|
|
|
self.setLogTarget("STDOUT")
|
2006-06-26 20:05:00 +00:00
|
|
|
|
2006-10-30 22:47:30 +00:00
|
|
|
def __sigTERMhandler(self, signum, frame):
|
|
|
|
logSys.debug("Caught signal %d. Exiting" % signum)
|
|
|
|
self.quit()
|
|
|
|
|
2006-10-04 22:17:53 +00:00
|
|
|
def start(self, sock, force = False):
|
2008-03-06 01:19:06 +00:00
|
|
|
logSys.info("Starting Fail2ban v" + version.version)
|
|
|
|
|
2006-10-30 22:47:30 +00:00
|
|
|
# Install signal handlers
|
|
|
|
signal.signal(signal.SIGTERM, self.__sigTERMhandler)
|
|
|
|
signal.signal(signal.SIGINT, self.__sigTERMhandler)
|
|
|
|
|
2006-09-17 21:59:29 +00:00
|
|
|
# First set the mask to only allow access to owner
|
|
|
|
os.umask(0077)
|
2006-09-19 20:38:32 +00:00
|
|
|
if self.__daemon:
|
2008-07-21 14:13:13 +00:00
|
|
|
logSys.info("Starting in daemon mode")
|
2006-09-19 20:38:32 +00:00
|
|
|
ret = self.__createDaemon()
|
2006-08-23 19:53:09 +00:00
|
|
|
if ret:
|
|
|
|
logSys.info("Daemon started")
|
|
|
|
else:
|
|
|
|
logSys.error("Could not create daemon")
|
|
|
|
raise ServerInitializationError("Could not create daemon")
|
2008-07-21 14:13:13 +00:00
|
|
|
|
|
|
|
# Creates a PID file.
|
|
|
|
try:
|
|
|
|
logSys.debug("Creating PID file %s" % Server.PID_FILE)
|
|
|
|
pidFile = open(Server.PID_FILE, 'w')
|
|
|
|
pidFile.write("%s\n" % os.getpid())
|
|
|
|
pidFile.close()
|
|
|
|
except IOError, e:
|
|
|
|
logSys.error("Unable to create PID file: %s" % e)
|
|
|
|
|
2006-06-26 20:05:00 +00:00
|
|
|
# Start the communication
|
2006-08-23 19:53:09 +00:00
|
|
|
logSys.debug("Starting communication")
|
2006-10-04 22:17:53 +00:00
|
|
|
try:
|
2008-03-06 01:19:06 +00:00
|
|
|
self.__asyncServer.start(sock, force)
|
|
|
|
except AsyncServerException, e:
|
|
|
|
logSys.error("Could not start server: %s", e)
|
|
|
|
# Removes the PID file.
|
|
|
|
try:
|
|
|
|
logSys.debug("Remove PID file %s" % Server.PID_FILE)
|
|
|
|
os.remove(Server.PID_FILE)
|
|
|
|
except OSError, e:
|
|
|
|
logSys.error("Unable to remove PID file: %s" % e)
|
2006-08-23 19:53:09 +00:00
|
|
|
logSys.info("Exiting Fail2ban")
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
def quit(self):
|
2006-08-20 21:07:28 +00:00
|
|
|
self.stopAllJail()
|
2006-10-04 22:17:53 +00:00
|
|
|
# Stop communication
|
2008-03-06 01:19:06 +00:00
|
|
|
self.__asyncServer.stop()
|
|
|
|
# Shutdowns the logging.
|
|
|
|
try:
|
|
|
|
self.__loggingLock.acquire()
|
|
|
|
logging.shutdown()
|
|
|
|
finally:
|
|
|
|
self.__loggingLock.release()
|
2006-06-26 20:05:00 +00:00
|
|
|
|
2006-10-16 19:42:50 +00:00
|
|
|
def addJail(self, name, backend):
|
|
|
|
self.__jails.add(name, backend)
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
def delJail(self, name):
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__jails.remove(name)
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
def startJail(self, name):
|
2006-11-06 22:48:46 +00:00
|
|
|
try:
|
|
|
|
self.__lock.acquire()
|
2007-03-26 21:17:31 +00:00
|
|
|
if not self.isAlive(name):
|
2006-11-06 22:48:46 +00:00
|
|
|
self.__jails.get(name).start()
|
|
|
|
finally:
|
|
|
|
self.__lock.release()
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
def stopJail(self, name):
|
2006-11-06 22:48:46 +00:00
|
|
|
try:
|
|
|
|
self.__lock.acquire()
|
2007-03-26 21:17:31 +00:00
|
|
|
if self.isAlive(name):
|
2006-11-06 22:48:46 +00:00
|
|
|
self.__jails.get(name).stop()
|
|
|
|
self.delJail(name)
|
|
|
|
finally:
|
|
|
|
self.__lock.release()
|
2006-08-20 21:07:28 +00:00
|
|
|
|
|
|
|
def stopAllJail(self):
|
2006-11-06 22:48:46 +00:00
|
|
|
try:
|
|
|
|
self.__lock.acquire()
|
|
|
|
for jail in self.__jails.getAll():
|
|
|
|
self.stopJail(jail)
|
|
|
|
finally:
|
|
|
|
self.__lock.release()
|
2006-08-28 20:22:15 +00:00
|
|
|
|
2007-03-26 21:17:31 +00:00
|
|
|
def isAlive(self, name):
|
|
|
|
return self.__jails.get(name).isAlive()
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
def setIdleJail(self, name, value):
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__jails.get(name).setIdle(value)
|
2006-09-17 21:59:29 +00:00
|
|
|
return True
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
def getIdleJail(self, name):
|
2006-09-19 20:38:32 +00:00
|
|
|
return self.__jails.get(name).getIdle()
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
# Filter
|
2006-09-17 21:59:29 +00:00
|
|
|
def addIgnoreIP(self, name, ip):
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__jails.getFilter(name).addIgnoreIP(ip)
|
2006-09-17 21:59:29 +00:00
|
|
|
|
|
|
|
def delIgnoreIP(self, name, ip):
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__jails.getFilter(name).delIgnoreIP(ip)
|
2006-09-17 21:59:29 +00:00
|
|
|
|
|
|
|
def getIgnoreIP(self, name):
|
2006-09-19 20:38:32 +00:00
|
|
|
return self.__jails.getFilter(name).getIgnoreIP()
|
2006-09-17 21:59:29 +00:00
|
|
|
|
2006-10-24 19:40:51 +00:00
|
|
|
def addLogPath(self, name, fileName):
|
|
|
|
self.__jails.getFilter(name).addLogPath(fileName)
|
2006-09-13 21:31:22 +00:00
|
|
|
|
2006-10-24 19:40:51 +00:00
|
|
|
def delLogPath(self, name, fileName):
|
|
|
|
self.__jails.getFilter(name).delLogPath(fileName)
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
def getLogPath(self, name):
|
2008-07-21 14:13:13 +00:00
|
|
|
return [m.getFileName()
|
|
|
|
for m in self.__jails.getFilter(name).getLogPath()]
|
2006-06-26 20:05:00 +00:00
|
|
|
|
2006-08-28 20:22:15 +00:00
|
|
|
def setFindTime(self, name, value):
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__jails.getFilter(name).setFindTime(value)
|
2006-08-28 20:22:15 +00:00
|
|
|
|
2006-09-25 17:03:48 +00:00
|
|
|
def getFindTime(self, name):
|
2006-09-19 20:38:32 +00:00
|
|
|
return self.__jails.getFilter(name).getFindTime()
|
2006-06-26 20:05:00 +00:00
|
|
|
|
2006-12-23 16:31:00 +00:00
|
|
|
def addFailRegex(self, name, value):
|
|
|
|
self.__jails.getFilter(name).addFailRegex(value)
|
|
|
|
|
|
|
|
def delFailRegex(self, name, index):
|
|
|
|
self.__jails.getFilter(name).delFailRegex(index)
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
def getFailRegex(self, name):
|
2006-09-19 20:38:32 +00:00
|
|
|
return self.__jails.getFilter(name).getFailRegex()
|
2006-06-26 20:05:00 +00:00
|
|
|
|
2006-12-23 16:31:00 +00:00
|
|
|
def addIgnoreRegex(self, name, value):
|
|
|
|
self.__jails.getFilter(name).addIgnoreRegex(value)
|
|
|
|
|
|
|
|
def delIgnoreRegex(self, name, index):
|
|
|
|
self.__jails.getFilter(name).delIgnoreRegex(index)
|
2006-11-12 14:52:36 +00:00
|
|
|
|
|
|
|
def getIgnoreRegex(self, name):
|
|
|
|
return self.__jails.getFilter(name).getIgnoreRegex()
|
|
|
|
|
2006-06-26 20:05:00 +00:00
|
|
|
def setMaxRetry(self, name, value):
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__jails.getFilter(name).setMaxRetry(value)
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
def getMaxRetry(self, name):
|
2006-09-19 20:38:32 +00:00
|
|
|
return self.__jails.getFilter(name).getMaxRetry()
|
2006-06-26 20:05:00 +00:00
|
|
|
|
2006-07-08 16:51:14 +00:00
|
|
|
# Action
|
|
|
|
def addAction(self, name, value):
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__jails.getAction(name).addAction(value)
|
2006-07-08 16:51:14 +00:00
|
|
|
|
|
|
|
def getLastAction(self, name):
|
2006-09-19 20:38:32 +00:00
|
|
|
return self.__jails.getAction(name).getLastAction()
|
2006-07-08 16:51:14 +00:00
|
|
|
|
|
|
|
def delAction(self, name, value):
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__jails.getAction(name).delAction(value)
|
2006-07-08 16:51:14 +00:00
|
|
|
|
2006-07-16 21:35:08 +00:00
|
|
|
def setCInfo(self, name, action, key, value):
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__jails.getAction(name).getAction(action).setCInfo(key, value)
|
2006-07-16 21:35:08 +00:00
|
|
|
|
|
|
|
def getCInfo(self, name, action, key):
|
2006-09-19 20:38:32 +00:00
|
|
|
return self.__jails.getAction(name).getAction(action).getCInfo(key)
|
2006-07-16 21:35:08 +00:00
|
|
|
|
|
|
|
def delCInfo(self, name, action, key):
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__jails.getAction(name).getAction(action).delCInfo(key)
|
2006-07-16 21:35:08 +00:00
|
|
|
|
2006-06-26 20:05:00 +00:00
|
|
|
def setBanTime(self, name, value):
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__jails.getAction(name).setBanTime(value)
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
def getBanTime(self, name):
|
2006-09-19 20:38:32 +00:00
|
|
|
return self.__jails.getAction(name).getBanTime()
|
2006-06-26 20:05:00 +00:00
|
|
|
|
2006-07-08 16:51:14 +00:00
|
|
|
def setActionStart(self, name, action, value):
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__jails.getAction(name).getAction(action).setActionStart(value)
|
2006-06-26 20:05:00 +00:00
|
|
|
|
2006-07-08 16:51:14 +00:00
|
|
|
def getActionStart(self, name, action):
|
2006-09-19 20:38:32 +00:00
|
|
|
return self.__jails.getAction(name).getAction(action).getActionStart()
|
2006-06-26 20:05:00 +00:00
|
|
|
|
2006-07-08 16:51:14 +00:00
|
|
|
def setActionStop(self, name, action, value):
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__jails.getAction(name).getAction(action).setActionStop(value)
|
2006-06-26 20:05:00 +00:00
|
|
|
|
2006-07-08 16:51:14 +00:00
|
|
|
def getActionStop(self, name, action):
|
2006-09-19 20:38:32 +00:00
|
|
|
return self.__jails.getAction(name).getAction(action).getActionStop()
|
2006-06-26 20:05:00 +00:00
|
|
|
|
2006-07-08 16:51:14 +00:00
|
|
|
def setActionCheck(self, name, action, value):
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__jails.getAction(name).getAction(action).setActionCheck(value)
|
2006-06-26 20:05:00 +00:00
|
|
|
|
2006-07-08 16:51:14 +00:00
|
|
|
def getActionCheck(self, name, action):
|
2006-09-19 20:38:32 +00:00
|
|
|
return self.__jails.getAction(name).getAction(action).getActionCheck()
|
2006-06-26 20:05:00 +00:00
|
|
|
|
2006-07-08 16:51:14 +00:00
|
|
|
def setActionBan(self, name, action, value):
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__jails.getAction(name).getAction(action).setActionBan(value)
|
2006-06-26 20:05:00 +00:00
|
|
|
|
2006-07-08 16:51:14 +00:00
|
|
|
def getActionBan(self, name, action):
|
2006-09-19 20:38:32 +00:00
|
|
|
return self.__jails.getAction(name).getAction(action).getActionBan()
|
2006-06-26 20:05:00 +00:00
|
|
|
|
2006-07-08 16:51:14 +00:00
|
|
|
def setActionUnban(self, name, action, value):
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__jails.getAction(name).getAction(action).setActionUnban(value)
|
2006-06-26 20:05:00 +00:00
|
|
|
|
2006-07-08 16:51:14 +00:00
|
|
|
def getActionUnban(self, name, action):
|
2006-09-19 20:38:32 +00:00
|
|
|
return self.__jails.getAction(name).getAction(action).getActionUnban()
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
# Status
|
|
|
|
def status(self):
|
2006-11-06 22:48:46 +00:00
|
|
|
try:
|
|
|
|
self.__lock.acquire()
|
|
|
|
jailList = ''
|
|
|
|
for jail in self.__jails.getAll():
|
|
|
|
jailList += jail + ', '
|
|
|
|
length = len(jailList)
|
|
|
|
if not length == 0:
|
|
|
|
jailList = jailList[:length-2]
|
|
|
|
ret = [("Number of jail", self.__jails.size()),
|
|
|
|
("Jail list", jailList)]
|
|
|
|
return ret
|
|
|
|
finally:
|
|
|
|
self.__lock.release()
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
def statusJail(self, name):
|
2006-09-19 20:38:32 +00:00
|
|
|
return self.__jails.get(name).getStatus()
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
# Logging
|
|
|
|
|
|
|
|
##
|
|
|
|
# Set the logging level.
|
|
|
|
#
|
|
|
|
# Incrementing the value gives more messages.
|
|
|
|
# 0 = FATAL
|
|
|
|
# 1 = ERROR
|
|
|
|
# 2 = WARNING
|
|
|
|
# 3 = INFO
|
|
|
|
# 4 = DEBUG
|
|
|
|
# @param value the level
|
|
|
|
|
|
|
|
def setLogLevel(self, value):
|
2006-11-06 22:48:46 +00:00
|
|
|
try:
|
|
|
|
self.__loggingLock.acquire()
|
|
|
|
self.__logLevel = value
|
|
|
|
logLevel = logging.DEBUG
|
|
|
|
if value == 0:
|
|
|
|
logLevel = logging.FATAL
|
|
|
|
elif value == 1:
|
|
|
|
logLevel = logging.ERROR
|
|
|
|
elif value == 2:
|
|
|
|
logLevel = logging.WARNING
|
|
|
|
elif value == 3:
|
|
|
|
logLevel = logging.INFO
|
|
|
|
logging.getLogger("fail2ban").setLevel(logLevel)
|
|
|
|
finally:
|
|
|
|
self.__loggingLock.release()
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
##
|
|
|
|
# Get the logging level.
|
|
|
|
#
|
|
|
|
# @see setLogLevel
|
|
|
|
# @return the log level
|
|
|
|
|
|
|
|
def getLogLevel(self):
|
2006-11-06 22:48:46 +00:00
|
|
|
try:
|
|
|
|
self.__loggingLock.acquire()
|
|
|
|
return self.__logLevel
|
|
|
|
finally:
|
|
|
|
self.__loggingLock.release()
|
2006-08-06 21:24:06 +00:00
|
|
|
|
2006-12-18 22:30:30 +00:00
|
|
|
##
|
|
|
|
# Sets the logging target.
|
|
|
|
#
|
|
|
|
# target can be a file, SYSLOG, STDOUT or STDERR.
|
|
|
|
# @param target the logging target
|
|
|
|
|
2006-08-06 21:24:06 +00:00
|
|
|
def setLogTarget(self, target):
|
2006-11-06 22:48:46 +00:00
|
|
|
try:
|
|
|
|
self.__loggingLock.acquire()
|
2008-03-06 01:19:06 +00:00
|
|
|
# set a format which is simpler for console use
|
|
|
|
formatter = logging.Formatter("%(asctime)s %(name)-16s: %(levelname)-6s %(message)s")
|
2006-11-06 22:48:46 +00:00
|
|
|
if target == "SYSLOG":
|
2008-03-06 01:19:06 +00:00
|
|
|
# Syslog daemons already add date to the message.
|
|
|
|
formatter = logging.Formatter("%(name)-16s: %(levelname)-6s %(message)s")
|
2006-12-13 23:02:46 +00:00
|
|
|
facility = logging.handlers.SysLogHandler.LOG_DAEMON
|
2006-12-18 22:30:30 +00:00
|
|
|
hdlr = logging.handlers.SysLogHandler("/dev/log",
|
2006-12-13 23:02:46 +00:00
|
|
|
facility = facility)
|
2006-11-06 22:48:46 +00:00
|
|
|
elif target == "STDOUT":
|
|
|
|
hdlr = logging.StreamHandler(sys.stdout)
|
|
|
|
elif target == "STDERR":
|
|
|
|
hdlr = logging.StreamHandler(sys.stderr)
|
|
|
|
else:
|
|
|
|
# Target should be a file
|
|
|
|
try:
|
2007-03-22 22:17:41 +00:00
|
|
|
open(target, "a").close()
|
2006-11-06 22:48:46 +00:00
|
|
|
hdlr = logging.FileHandler(target)
|
|
|
|
except IOError:
|
|
|
|
logSys.error("Unable to log to " + target)
|
2006-12-18 22:30:30 +00:00
|
|
|
logSys.info("Logging to previous target " + self.__logTarget)
|
2006-11-06 22:48:46 +00:00
|
|
|
return False
|
2007-03-22 22:17:41 +00:00
|
|
|
# Removes previous handlers
|
|
|
|
for handler in logging.getLogger("fail2ban").handlers:
|
|
|
|
# Closes the handler.
|
|
|
|
logging.getLogger("fail2ban").removeHandler(handler)
|
2008-03-06 01:19:06 +00:00
|
|
|
handler.close()
|
2006-11-06 22:48:46 +00:00
|
|
|
# tell the handler to use this format
|
|
|
|
hdlr.setFormatter(formatter)
|
|
|
|
logging.getLogger("fail2ban").addHandler(hdlr)
|
2008-07-21 14:13:13 +00:00
|
|
|
# Does not display this message at startup.
|
|
|
|
if not self.__logTarget == None:
|
|
|
|
logSys.info("Changed logging target to %s for Fail2ban v%s" %
|
|
|
|
(target, version.version))
|
|
|
|
# Sets the logging target.
|
|
|
|
self.__logTarget = target
|
2006-11-06 22:48:46 +00:00
|
|
|
return True
|
|
|
|
finally:
|
|
|
|
self.__loggingLock.release()
|
2006-08-06 21:24:06 +00:00
|
|
|
|
|
|
|
def getLogTarget(self):
|
2006-11-06 22:48:46 +00:00
|
|
|
try:
|
|
|
|
self.__loggingLock.acquire()
|
|
|
|
return self.__logTarget
|
|
|
|
finally:
|
|
|
|
self.__loggingLock.release()
|
2006-08-23 19:53:09 +00:00
|
|
|
|
2006-09-19 20:38:32 +00:00
|
|
|
def __createDaemon(self):
|
2006-08-23 19:53:09 +00:00
|
|
|
""" Detach a process from the controlling terminal and run it in the
|
|
|
|
background as a daemon.
|
|
|
|
|
|
|
|
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/278731
|
|
|
|
"""
|
|
|
|
|
|
|
|
try:
|
|
|
|
# Fork a child process so the parent can exit. This will return control
|
|
|
|
# to the command line or shell. This is required so that the new process
|
|
|
|
# is guaranteed not to be a process group leader. We have this guarantee
|
|
|
|
# because the process GID of the parent is inherited by the child, but
|
|
|
|
# the child gets a new PID, making it impossible for its PID to equal its
|
|
|
|
# PGID.
|
|
|
|
pid = os.fork()
|
|
|
|
except OSError, e:
|
|
|
|
return((e.errno, e.strerror)) # ERROR (return a tuple)
|
|
|
|
|
|
|
|
if pid == 0: # The first child.
|
|
|
|
|
|
|
|
# Next we call os.setsid() to become the session leader of this new
|
|
|
|
# session. The process also becomes the process group leader of the
|
|
|
|
# new process group. Since a controlling terminal is associated with a
|
|
|
|
# session, and this new session has not yet acquired a controlling
|
|
|
|
# terminal our process now has no controlling terminal. This shouldn't
|
|
|
|
# fail, since we're guaranteed that the child is not a process group
|
|
|
|
# leader.
|
|
|
|
os.setsid()
|
|
|
|
|
|
|
|
# When the first child terminates, all processes in the second child
|
|
|
|
# are sent a SIGHUP, so it's ignored.
|
|
|
|
signal.signal(signal.SIGHUP, signal.SIG_IGN)
|
|
|
|
|
|
|
|
try:
|
|
|
|
# Fork a second child to prevent zombies. Since the first child is
|
|
|
|
# a session leader without a controlling terminal, it's possible for
|
|
|
|
# it to acquire one by opening a terminal in the future. This second
|
|
|
|
# fork guarantees that the child is no longer a session leader, thus
|
|
|
|
# preventing the daemon from ever acquiring a controlling terminal.
|
|
|
|
pid = os.fork() # Fork a second child.
|
|
|
|
except OSError, e:
|
|
|
|
return((e.errno, e.strerror)) # ERROR (return a tuple)
|
|
|
|
|
|
|
|
if (pid == 0): # The second child.
|
|
|
|
# Ensure that the daemon doesn't keep any directory in use. Failure
|
|
|
|
# to do this could make a filesystem unmountable.
|
|
|
|
os.chdir("/")
|
|
|
|
else:
|
|
|
|
os._exit(0) # Exit parent (the first child) of the second child.
|
|
|
|
else:
|
|
|
|
os._exit(0) # Exit parent of the first child.
|
|
|
|
|
|
|
|
# Close all open files. Try the system configuration variable, SC_OPEN_MAX,
|
|
|
|
# for the maximum number of open files to close. If it doesn't exist, use
|
|
|
|
# the default value (configurable).
|
|
|
|
try:
|
|
|
|
maxfd = os.sysconf("SC_OPEN_MAX")
|
|
|
|
except (AttributeError, ValueError):
|
|
|
|
maxfd = 256 # default maximum
|
|
|
|
|
|
|
|
for fd in range(0, maxfd):
|
|
|
|
try:
|
|
|
|
os.close(fd)
|
|
|
|
except OSError: # ERROR (ignore)
|
|
|
|
pass
|
|
|
|
|
|
|
|
# Redirect the standard file descriptors to /dev/null.
|
2006-10-24 19:40:51 +00:00
|
|
|
os.open("/dev/null", os.O_RDONLY) # standard input (0)
|
2006-08-23 19:53:09 +00:00
|
|
|
os.open("/dev/null", os.O_RDWR) # standard output (1)
|
|
|
|
os.open("/dev/null", os.O_RDWR) # standard error (2)
|
|
|
|
return True
|
|
|
|
|
2006-06-26 20:05:00 +00:00
|
|
|
|
2006-08-23 19:53:09 +00:00
|
|
|
class ServerInitializationError(Exception):
|
2006-09-17 21:59:29 +00:00
|
|
|
pass
|