diff --git a/utils/pidlock.py b/utils/pidlock.py new file mode 100644 index 00000000..6eeb0dde --- /dev/null +++ b/utils/pidlock.py @@ -0,0 +1,98 @@ +# 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 +# +# $Revision$ + +__author__ = "Cyril Jaquier" +__version__ = "$Revision$" +__date__ = "$Date$" +__copyright__ = "Copyright (c) 2004 Cyril Jaquier" +__license__ = "GPL" + +import os, logging + +# Gets the instance of the logger. +logSys = logging.getLogger("fail2ban") + +class PIDLock: + """ Manages the PID lock file. + + The following class shows how to implement the singleton pattern[1] in + Python. A singleton is a class that makes sure only one instance of it + is ever created. Typically such classes are used to manage resources + that by their very nature can only exist once. + + http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52558 + """ + + class __impl: + """ Implementation of the singleton interface """ + + def setPath(self, path): + """ Set PID lock file path. + """ + self.path = path + + def create(self): + """ Create PID lock. + """ + fileHandler = open(self.path, mode='w') + pid = os.getpid() + fileHandler.write(`pid` + '\n') + fileHandler.close() + logSys.debug("Created PID lock (" + `pid` + ") in " + self.path) + + def remove(self): + """ Remove PID lock. + """ + os.remove(self.path) + logSys.debug("Removed PID lock " + self.path) + + def exists(self): + """ Returns the current PID if Fail2Ban is running or False + if no instance found. + """ + try: + fileHandler = open(self.path) + pid = fileHandler.readline() + fileHandler.close() + return pid + except IOError: + return False + + # storage for the instance reference + __instance = None + + def __init__(self): + """ Create singleton instance """ + # Check whether we already have an instance + if PIDLock.__instance is None: + # Create and remember instance + PIDLock.__instance = PIDLock.__impl() + + # Store instance reference as the only member in the handle + self.__dict__['_PIDLock__instance'] = PIDLock.__instance + + def __getattr__(self, attr): + """ Delegate access to implementation """ + return getattr(self.__instance, attr) + + def __setattr__(self, attr, value): + """ Delegate access to implementation """ + return setattr(self.__instance, attr, value) + \ No newline at end of file diff --git a/utils/process.py b/utils/process.py index 0b5a0983..03650212 100644 --- a/utils/process.py +++ b/utils/process.py @@ -30,10 +30,10 @@ import os, logging, signal logSys = logging.getLogger("fail2ban") def createDaemon(): - """Detach a process from the controlling terminal and run it in the - background as a daemon. + """ 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 + http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/278731 """ try: @@ -101,35 +101,6 @@ def createDaemon(): os.open("/dev/null", os.O_RDWR) # standard error (2) return True - -def checkForPID(lockfile): - """ Checks for running Fail2Ban. - - Returns the current PID if Fail2Ban is running or False - if no instance found. - """ - try: - fileHandler = open(lockfile) - pid = fileHandler.readline() - fileHandler.close() - return pid - except IOError: - return False - -def createPID(lockfile): - """ Creates a PID lock file with the current PID. - """ - fileHandler = open(lockfile, mode='w') - pid = os.getpid() - fileHandler.write(`pid`+'\n') - fileHandler.close() - logSys.debug("Created PID lock ("+`pid`+") in "+lockfile) - -def removePID(lockfile): - """ Remove PID lock. - """ - os.remove(lockfile) - logSys.debug("Removed PID lock "+lockfile) def killPID(pid): """ Kills the process with the given PID using the