From c6244a8509d4e7a7bba65c453802e5ca24dfcebd Mon Sep 17 00:00:00 2001 From: sebres Date: Fri, 22 Dec 2023 14:08:39 +0100 Subject: [PATCH] `fail2ban-regex`: don't error by output if stdout pipe gets closed (e. g. using together with `head`); amend to gh-2758 (see gh-3653) --- fail2ban/client/fail2banregex.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/fail2ban/client/fail2banregex.py b/fail2ban/client/fail2banregex.py index 22a96ebc7..5275a683c 100644 --- a/fail2ban/client/fail2banregex.py +++ b/fail2ban/client/fail2banregex.py @@ -789,7 +789,15 @@ class Fail2banRegex(object): return True +def _loc_except_hook(exctype, value, traceback): + if (exctype != BrokenPipeError and exctype != IOError or value.errno != 32): + return sys.__excepthook__(exctype, value, traceback) + # pipe seems to be closed (head / tail / etc), thus simply exit: + sys.exit(0) + def exec_command_line(*args): + sys.excepthook = _loc_except_hook; # stop on closed/broken pipe + logging.exitOnIOError = True parser = get_opt_parser() (opts, args) = parser.parse_args(*args)