Added ability to specify PID file

pull/124/merge
Steven Hiscocks 2013-02-17 22:14:01 +00:00
parent 8cf006827e
commit ce3ab34dd8
5 changed files with 36 additions and 14 deletions

View File

@ -42,7 +42,8 @@ class Fail2banReader(ConfigReader):
ConfigReader.read(self, "fail2ban") ConfigReader.read(self, "fail2ban")
def getEarlyOptions(self): def getEarlyOptions(self):
opts = [["string", "socket", "/tmp/fail2ban.sock"]] opts = [["string", "socket", "/tmp/fail2ban.sock"],
["string", "pidfile", "/var/run/fail2ban/fail2ban.pid"]]
return ConfigReader.getOptions(self, "Definition", opts) return ConfigReader.getOptions(self, "Definition", opts)
def getOptions(self): def getOptions(self):

View File

@ -40,3 +40,10 @@ logtarget = /var/log/fail2ban.log
# #
socket = /var/run/fail2ban/fail2ban.sock socket = /var/run/fail2ban/fail2ban.sock
# Option: pidfile
# Notes.: Set the PID file. This is used to store the process ID of the
# fail2ban server.
# Values: FILE Default: /var/run/fail2ban/fail2ban.sock
#
pidfile = /var/run/fail2ban/fail2ban.pid

View File

@ -62,6 +62,7 @@ class Fail2banClient:
self.__conf["verbose"] = 1 self.__conf["verbose"] = 1
self.__conf["interactive"] = False self.__conf["interactive"] = False
self.__conf["socket"] = None self.__conf["socket"] = None
self.__conf["pidfile"] = None
def dispVersion(self): def dispVersion(self):
print "Fail2Ban v" + version print "Fail2Ban v" + version
@ -84,6 +85,7 @@ class Fail2banClient:
print "Options:" print "Options:"
print " -c <DIR> configuration directory" print " -c <DIR> configuration directory"
print " -s <FILE> socket path" print " -s <FILE> socket path"
print " -p <FILE> pidfile path"
print " -d dump configuration. For debugging" print " -d dump configuration. For debugging"
print " -i interactive mode" print " -i interactive mode"
print " -v increase verbosity" print " -v increase verbosity"
@ -119,6 +121,8 @@ class Fail2banClient:
self.__conf["conf"] = opt[1] self.__conf["conf"] = opt[1]
elif opt[0] == "-s": elif opt[0] == "-s":
self.__conf["socket"] = opt[1] self.__conf["socket"] = opt[1]
elif opt[0] == "-p":
self.__conf["pidfile"] = opt[1]
elif opt[0] == "-d": elif opt[0] == "-d":
self.__conf["dump"] = True self.__conf["dump"] = True
elif opt[0] == "-v": elif opt[0] == "-v":
@ -183,6 +187,7 @@ class Fail2banClient:
return False return False
# Start the server # Start the server
self.__startServerAsync(self.__conf["socket"], self.__startServerAsync(self.__conf["socket"],
self.__conf["pidfile"],
self.__conf["force"]) self.__conf["force"])
try: try:
# Wait for the server to start # Wait for the server to start
@ -231,7 +236,7 @@ class Fail2banClient:
# #
# Start the Fail2ban server in daemon mode. # Start the Fail2ban server in daemon mode.
def __startServerAsync(self, socket, force = False): def __startServerAsync(self, socket, pidfile, force = False):
# Forks the current process. # Forks the current process.
pid = os.fork() pid = os.fork()
if pid == 0: if pid == 0:
@ -242,6 +247,9 @@ class Fail2banClient:
# Set the socket path. # Set the socket path.
args.append("-s") args.append("-s")
args.append(socket) args.append(socket)
# Set the pidfile
args.append("-p")
args.append(pidfile)
# Force the execution if needed. # Force the execution if needed.
if force: if force:
args.append("-x") args.append("-x")
@ -297,7 +305,7 @@ class Fail2banClient:
# Reads the command line options. # Reads the command line options.
try: try:
cmdOpts = 'hc:s:xdviqV' cmdOpts = 'hc:s:p:xdviqV'
cmdLongOpts = ['help', 'version'] cmdLongOpts = ['help', 'version']
optList, args = getopt.getopt(self.__argv[1:], cmdOpts, cmdLongOpts) optList, args = getopt.getopt(self.__argv[1:], cmdOpts, cmdLongOpts)
except getopt.GetoptError: except getopt.GetoptError:
@ -328,9 +336,11 @@ class Fail2banClient:
# Set socket path # Set socket path
self.__configurator.readEarly() self.__configurator.readEarly()
socket = self.__configurator.getEarlyOptions() conf = self.__configurator.getEarlyOptions()
if self.__conf["socket"] == None: if self.__conf["socket"] == None:
self.__conf["socket"] = socket["socket"] self.__conf["socket"] = conf["socket"]
if self.__conf["pidfile"] == None:
self.__conf["pidfile"] = conf["pidfile"]
logSys.info("Using socket file " + self.__conf["socket"]) logSys.info("Using socket file " + self.__conf["socket"])
if self.__conf["dump"]: if self.__conf["dump"]:

View File

@ -54,6 +54,7 @@ class Fail2banServer:
self.__conf["background"] = True self.__conf["background"] = True
self.__conf["force"] = False self.__conf["force"] = False
self.__conf["socket"] = "/var/run/fail2ban/fail2ban.sock" self.__conf["socket"] = "/var/run/fail2ban/fail2ban.sock"
self.__conf["pidfile"] = "/var/run/fail2ban/fail2ban.pid"
def dispVersion(self): def dispVersion(self):
print "Fail2Ban v" + version print "Fail2Ban v" + version
@ -81,6 +82,7 @@ class Fail2banServer:
print " -b start in background" print " -b start in background"
print " -f start in foreground" print " -f start in foreground"
print " -s <FILE> socket path" print " -s <FILE> socket path"
print " -p <FILE> pidfile path"
print " -x force execution of the server (remove socket file)" print " -x force execution of the server (remove socket file)"
print " -h, --help display this help message" print " -h, --help display this help message"
print " -V, --version print the version" print " -V, --version print the version"
@ -97,6 +99,8 @@ class Fail2banServer:
self.__conf["background"] = False self.__conf["background"] = False
if opt[0] == "-s": if opt[0] == "-s":
self.__conf["socket"] = opt[1] self.__conf["socket"] = opt[1]
if opt[0] == "-p":
self.__conf["pidfile"] = opt[1]
if opt[0] == "-x": if opt[0] == "-x":
self.__conf["force"] = True self.__conf["force"] = True
if opt[0] in ["-h", "--help"]: if opt[0] in ["-h", "--help"]:
@ -112,7 +116,7 @@ class Fail2banServer:
# Reads the command line options. # Reads the command line options.
try: try:
cmdOpts = 'bfs:xhV' cmdOpts = 'bfs:p:xhV'
cmdLongOpts = ['help', 'version'] cmdLongOpts = ['help', 'version']
optList, args = getopt.getopt(self.__argv[1:], cmdOpts, cmdLongOpts) optList, args = getopt.getopt(self.__argv[1:], cmdOpts, cmdLongOpts)
except getopt.GetoptError: except getopt.GetoptError:
@ -123,7 +127,9 @@ class Fail2banServer:
try: try:
self.__server = Server(self.__conf["background"]) self.__server = Server(self.__conf["background"])
self.__server.start(self.__conf["socket"], self.__conf["force"]) self.__server.start(self.__conf["socket"],
self.__conf["pidfile"],
self.__conf["force"])
return True return True
except Exception, e: except Exception, e:
logSys.exception(e) logSys.exception(e)

View File

@ -40,8 +40,6 @@ logSys = logging.getLogger("fail2ban.server")
class Server: class Server:
PID_FILE = "/var/run/fail2ban/fail2ban.pid"
def __init__(self, daemon = False): def __init__(self, daemon = False):
self.__loggingLock = Lock() self.__loggingLock = Lock()
self.__lock = RLock() self.__lock = RLock()
@ -59,7 +57,7 @@ class Server:
logSys.debug("Caught signal %d. Exiting" % signum) logSys.debug("Caught signal %d. Exiting" % signum)
self.quit() self.quit()
def start(self, sock, force = False): def start(self, sock, pidfile, force = False):
logSys.info("Starting Fail2ban v" + version.version) logSys.info("Starting Fail2ban v" + version.version)
# Install signal handlers # Install signal handlers
@ -79,8 +77,8 @@ class Server:
# Creates a PID file. # Creates a PID file.
try: try:
logSys.debug("Creating PID file %s" % Server.PID_FILE) logSys.debug("Creating PID file %s" % pidfile)
pidFile = open(Server.PID_FILE, 'w') pidFile = open(pidfile, 'w')
pidFile.write("%s\n" % os.getpid()) pidFile.write("%s\n" % os.getpid())
pidFile.close() pidFile.close()
except IOError, e: except IOError, e:
@ -94,8 +92,8 @@ class Server:
logSys.error("Could not start server: %s", e) logSys.error("Could not start server: %s", e)
# Removes the PID file. # Removes the PID file.
try: try:
logSys.debug("Remove PID file %s" % Server.PID_FILE) logSys.debug("Remove PID file %s" % pidfile)
os.remove(Server.PID_FILE) os.remove(pidfile)
except OSError, e: except OSError, e:
logSys.error("Unable to remove PID file: %s" % e) logSys.error("Unable to remove PID file: %s" % e)
logSys.info("Exiting Fail2ban") logSys.info("Exiting Fail2ban")