Merge pull request #704 from CameronNemo/foreground-opt-client

Add an option in fail2ban-client to pass the foreground option to the server
pull/710/merge
Yaroslav Halchenko 2014-05-01 13:14:06 -04:00
commit 1e19bca28e
2 changed files with 23 additions and 5 deletions

View File

@ -51,6 +51,7 @@ class Fail2banClient:
self.__conf["conf"] = "/etc/fail2ban" self.__conf["conf"] = "/etc/fail2ban"
self.__conf["dump"] = False self.__conf["dump"] = False
self.__conf["force"] = False self.__conf["force"] = False
self.__conf["background"] = True
self.__conf["verbose"] = 1 self.__conf["verbose"] = 1
self.__conf["interactive"] = False self.__conf["interactive"] = False
self.__conf["socket"] = None self.__conf["socket"] = None
@ -83,6 +84,8 @@ class Fail2banClient:
print " -v increase verbosity" print " -v increase verbosity"
print " -q decrease verbosity" print " -q decrease verbosity"
print " -x force execution of the server (remove socket file)" print " -x force execution of the server (remove socket file)"
print " -b start server in background (default)"
print " -f start server in foreground (note that the client forks once itself)"
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"
print print
@ -125,6 +128,10 @@ class Fail2banClient:
self.__conf["force"] = True self.__conf["force"] = True
elif opt[0] == "-i": elif opt[0] == "-i":
self.__conf["interactive"] = True self.__conf["interactive"] = True
elif opt[0] == "-b":
self.__conf["background"] = True
elif opt[0] == "-f":
self.__conf["background"] = False
elif opt[0] in ["-h", "--help"]: elif opt[0] in ["-h", "--help"]:
self.dispUsage() self.dispUsage()
sys.exit(0) sys.exit(0)
@ -194,7 +201,8 @@ class Fail2banClient:
# Start the server # Start the server
self.__startServerAsync(self.__conf["socket"], self.__startServerAsync(self.__conf["socket"],
self.__conf["pidfile"], self.__conf["pidfile"],
self.__conf["force"]) self.__conf["force"],
self.__conf["background"])
try: try:
# Wait for the server to start # Wait for the server to start
self.__waitOnServer() self.__waitOnServer()
@ -242,14 +250,12 @@ class Fail2banClient:
# #
# Start the Fail2ban server in daemon mode. # Start the Fail2ban server in daemon mode.
def __startServerAsync(self, socket, pidfile, force = False): def __startServerAsync(self, socket, pidfile, force = False, background = True):
# Forks the current process. # Forks the current process.
pid = os.fork() pid = os.fork()
if pid == 0: if pid == 0:
args = list() args = list()
args.append(self.SERVER) args.append(self.SERVER)
# Start in background mode.
args.append("-b")
# Set the socket path. # Set the socket path.
args.append("-s") args.append("-s")
args.append(socket) args.append(socket)
@ -259,6 +265,12 @@ class Fail2banClient:
# Force the execution if needed. # Force the execution if needed.
if force: if force:
args.append("-x") args.append("-x")
# Start in foreground mode if requested.
if background:
args.append("-b")
else:
args.append("-f")
try: try:
# Use the current directory. # Use the current directory.
exe = os.path.abspath(os.path.join(sys.path[0], self.SERVER)) exe = os.path.abspath(os.path.join(sys.path[0], self.SERVER))
@ -312,7 +324,7 @@ class Fail2banClient:
# Reads the command line options. # Reads the command line options.
try: try:
cmdOpts = 'hc:s:p:xdviqV' cmdOpts = 'hc:s:p:xfbdviqV'
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:

View File

@ -34,6 +34,12 @@ decrease verbosity
\fB\-x\fR \fB\-x\fR
force execution of the server (remove socket file) force execution of the server (remove socket file)
.TP .TP
\fB\-b\fR
start the server in background mode (default)
.TP
\fB\-f\fR
start the server in foreground mode (note that the client forks once itself)
.TP
\fB\-h\fR, \fB\-\-help\fR \fB\-h\fR, \fB\-\-help\fR
display this help message display this help message
.TP .TP