Skip to content

Commit

Permalink
BF: Lock server's executeCmd to prevent racing among iptables calls (…
Browse files Browse the repository at this point in the history
…Closes: #554162)

Many kudos go to Michael Saavedra for the solution and the patch.

git-svn-id: https://fail2ban.svn.sourceforge.net/svnroot/fail2ban/branches/FAIL2BAN-0_8@784 a942ae1a-1317-0410-a47c-b1dcaea8d605
  • Loading branch information
yarikoptic committed Sep 24, 2011
1 parent 3eb5e3b commit 3a58d0e
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions server/action.py
Expand Up @@ -25,11 +25,15 @@
__license__ = "GPL"

import logging, os
import threading
#from subprocess import call

# Gets the instance of the logger.
logSys = logging.getLogger("fail2ban.actions.action")

# Create a lock for running system commands
_cmd_lock = threading.Lock()

##
# Execute commands.
#
Expand Down Expand Up @@ -301,17 +305,21 @@ def __processCmd(self, cmd, aInfo = None):
#@staticmethod
def executeCmd(realCmd):
logSys.debug(realCmd)
try:
# The following line gives deadlock with multiple jails
#retcode = call(realCmd, shell=True)
retcode = os.system(realCmd)
if retcode == 0:
logSys.debug("%s returned successfully" % realCmd)
return True
else:
logSys.error("%s returned %x" % (realCmd, retcode))
except OSError, e:
logSys.error("%s failed with %s" % (realCmd, e))
_cmd_lock.acquire()
try: # Try wrapped within another try needed for python version < 2.5
try:
# The following line gives deadlock with multiple jails
#retcode = call(realCmd, shell=True)
retcode = os.system(realCmd)
if retcode == 0:
logSys.debug("%s returned successfully" % realCmd)
return True
else:
logSys.error("%s returned %x" % (realCmd, retcode))
except OSError, e:
logSys.error("%s failed with %s" % (realCmd, e))
finally:
_cmd_lock.release()
return False
executeCmd = staticmethod(executeCmd)

0 comments on commit 3a58d0e

Please sign in to comment.