Blame SOURCES/iptables-do_not_lock_again_and_again.patch

26b15f
commit 24f8174646123c2833bc87967b366796231b04e0
26b15f
Author: Liping Zhang <zlpnobody@gmail.com>
26b15f
Date:   Sun Feb 5 21:57:34 2017 +0800
26b15f
26b15f
    xshared: do not lock again and again if "-w" option is not specified
26b15f
    
26b15f
    After running the following commands, some confusing messages was printed
26b15f
    out:
26b15f
      # while : ; do
26b15f
      iptables -A INPUT &
26b15f
      iptables -D INPUT &
26b15f
      done
26b15f
      [...]
26b15f
      Another app is currently holding the xtables lock; still -9s 0us time
26b15f
      ahead to have a chance to grab the lock...
26b15f
      Another app is currently holding the xtables lock; still -29s 0us time
26b15f
      ahead to have a chance to grab the lock...
26b15f
    
26b15f
    If "-w" option is not specified, the "wait" will be zero, so we should
26b15f
    check whether the timer_left is less than wait_interval before we call
26b15f
    select to sleep.
26b15f
    
26b15f
    Also remove unused "BASE_MICROSECONDS" and "struct timeval waited_time"
26b15f
    introduced by commit e8f857a5a151 ("xtables: Add an interval option for
26b15f
    xtables lock wait").
26b15f
    
26b15f
    Fixes: e8f857a5a151 ("xtables: Add an interval option for xtables lock wait")
26b15f
    Signed-off-by: Liping Zhang <zlpnobody@gmail.com>
26b15f
    Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
26b15f
26b15f
diff --git a/iptables/xshared.c b/iptables/xshared.c
26b15f
index cccb8ae..055acf2 100644
26b15f
--- a/iptables/xshared.c
26b15f
+++ b/iptables/xshared.c
26b15f
@@ -17,7 +17,6 @@
26b15f
 #include "xshared.h"
26b15f
 
26b15f
 #define XT_LOCK_NAME	"/run/xtables.lock"
26b15f
-#define BASE_MICROSECONDS	100000
26b15f
 
26b15f
 /*
26b15f
  * Print out any special helps. A user might like to be able to add a --help
26b15f
@@ -249,13 +248,11 @@ void xs_init_match(struct xtables_match *match)
26b15f
 
26b15f
 bool xtables_lock(int wait, struct timeval *wait_interval)
26b15f
 {
26b15f
-	struct timeval time_left, wait_time, waited_time;
26b15f
+	struct timeval time_left, wait_time;
26b15f
 	int fd, i = 0;
26b15f
 
26b15f
 	time_left.tv_sec = wait;
26b15f
 	time_left.tv_usec = 0;
26b15f
-	waited_time.tv_sec = 0;
26b15f
-	waited_time.tv_usec = 0;
26b15f
 
26b15f
 	fd = open(XT_LOCK_NAME, O_CREAT, 0600);
26b15f
 	if (fd < 0)
26b15f
@@ -264,6 +261,9 @@ bool xtables_lock(int wait, struct timeval *wait_interval)
26b15f
 	while (1) {
26b15f
 		if (flock(fd, LOCK_EX | LOCK_NB) == 0)
26b15f
 			return true;
26b15f
+		else if (wait >= 0 && timercmp(&time_left, wait_interval, <))
26b15f
+			return false;
26b15f
+
26b15f
 		if (++i % 10 == 0) {
26b15f
 			if (wait != -1)
26b15f
 				fprintf(stderr, "Another app is currently holding the xtables lock; "
26b15f
@@ -279,10 +279,7 @@ bool xtables_lock(int wait, struct timeval *wait_interval)
26b15f
 		if (wait == -1)
26b15f
 			continue;
26b15f
 
26b15f
-		timeradd(&waited_time, wait_interval, &waited_time);
26b15f
 		timersub(&time_left, wait_interval, &time_left);
26b15f
-		if (!timerisset(&time_left))
26b15f
-			return false;
26b15f
 	}
26b15f
 }
26b15f