Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
Bug 347477 - Fail2ban not removing stale socket file on start
Summary: Fail2ban not removing stale socket file on start
Status: RESOLVED FIXED
Alias: None
Product: Gentoo Linux
Classification: Unclassified
Component: Current packages (show other bugs)
Hardware: All Linux
: High normal (vote)
Assignee: Gentoo Netmon project
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-12-01 20:37 UTC by Michael Lorant
Modified: 2013-01-03 12:39 UTC (History)
1 user (show)

See Also:
Package list:
Runtime testing required: ---


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Michael Lorant 2010-12-01 20:37:38 UTC
Fail2ban doesn't clean up a stale socket file after a system crash. If fail2ban doesn't shutdown cleanly, this file is left behind and the init.d startup scripts do not remove it. This caues fail2ban to be unable to restart on next boot.

Reproducible: Always

Steps to Reproduce:
1. Power failure
2. Attempt to restart fail2ban manually or via rc scripts.


Actual Results:  
There is no error message on a normal start:
 * Starting fail2ban ...
 * Failed to start fail2ban                                               [ !! ]

Starting it directly with the following command:
/usr/bin/fail2ban-client start

Gives the following results:
2010-12-02 07:21:31,114 fail2ban.server : INFO   Starting Fail2ban v0.8.4
2010-12-02 07:21:31,115 fail2ban.server : INFO   Starting in daemon mode
ERROR  Could not start server. Maybe an old socket file is still present. Try to remove /var/run/fail2ban/fail2ban.sock. If you used fail2ban-client to start the server, adding the -x option will do it


Expected Results:  
There should be a warning that the lock file exists without fail2ban running and it should be removed automatically.

If the fail2ban directory in /var/run doesn't exist, it also fails to start. This directory needs to recreated if it doesn't exist.
Comment 1 Markos Chandras (RETIRED) gentoo-dev 2010-12-02 12:23:09 UTC
Which version? How about -r1?
Comment 2 Michael Lorant 2010-12-02 20:47:18 UTC
This was tested with 0.8.4. The -r1 makes no difference since the init.d script is part of the official package (it has a gentoo init.d).

Start code for gentoo:
start() {
	ebegin "Starting fail2ban"
	${FAIL2BAN} start &> /dev/null
	eend $? "Failed to start fail2ban"
}

Start code for redhat:
start() {
    echo -n $"Starting fail2ban: "
    getpid
    if [ -z "$pid" ]; then
	rm -rf /var/run/fail2ban/fail2ban.sock # in case of unclean shutdown
        $FAIL2BAN start > /dev/null
        RETVAL=$?
    fi
    if [ $RETVAL -eq 0 ]; then
        touch /var/lock/subsys/fail2ban
        echo_success
    else
        echo_failure
    fi
    echo
    return $RETVAL
}

I can quickly come up with a simple fix just by turning it into this:
start() {
	ebegin "Starting fail2ban"
	[[ ! -d /var/run/fail2ban ]] && mkdir var/run/fail2ban
	[[ -z /var/run/fail2ban/fail2ban.sock ]] && rm -f /var/run/fail2ban/fail2ban.sock
	${FAIL2BAN} start &> /dev/null
	eend $? "Failed to start fail2ban"
}
Comment 3 Michael Lorant 2010-12-02 20:48:44 UTC
Fixed a missing slash:

start() {
        ebegin "Starting fail2ban"
        [[ ! -d /var/run/fail2ban ]] && mkdir /var/run/fail2ban
        [[ -z /var/run/fail2ban/fail2ban.sock ]] && rm -f
/var/run/fail2ban/fail2ban.sock
        ${FAIL2BAN} start &> /dev/null
        eend $? "Failed to start fail2ban"
}

Should also have some variables for the PID and PID directory to make it cleaner.
Comment 4 Markos Chandras (RETIRED) gentoo-dev 2010-12-02 21:26:37 UTC
(In reply to comment #3)
> Fixed a missing slash:
> 
> start() {
>         ebegin "Starting fail2ban"
>         [[ ! -d /var/run/fail2ban ]] && mkdir /var/run/fail2ban
>         [[ -z /var/run/fail2ban/fail2ban.sock ]] && rm -f
> /var/run/fail2ban/fail2ban.sock
>         ${FAIL2BAN} start &> /dev/null
>         eend $? "Failed to start fail2ban"
> }
> 
> Should also have some variables for the PID and PID directory to make it
> cleaner.
> 

I'd say that 

 -z /var/run/fail2ban/fail2ban.sock is not correct. It should be 

 -e /var/run/fail2ban/fail2ban.sock
Comment 5 Michael Lorant 2010-12-02 21:33:07 UTC
Oops, I stand corrected. Haven't done any shell scripting for a while. -z is for strings, -e makes a lot more sense :)
Comment 6 Markos Chandras (RETIRED) gentoo-dev 2010-12-02 21:51:29 UTC
Fixed in -r2

Please test and reopen this bug if needed


+*fail2ban-0.8.4-r2 (02 Dec 2010)
+
+  02 Dec 2010; Markos Chandras <hwoarang@gentoo.org> +fail2ban-0.8.4-r2.ebuild,
+  +files/gentoo-initd_create_run_dir.patch, metadata.xml:
+  Create /var/run/fail2ban on runtime and remove stalled sock file in case of
+  system crash. Thanks to Michael Lorant <mikel@mlvision.com.au>. Bug #347477.
+  Taking over maintainership
+