682f57
diff -up cyrus-imapd-2.4.6/lib/lock_flock.c.flock cyrus-imapd-2.4.6/lib/lock_flock.c
682f57
--- cyrus-imapd-2.4.6/lib/lock_flock.c.flock	2010-12-20 14:15:49.000000000 +0100
682f57
+++ cyrus-imapd-2.4.6/lib/lock_flock.c	2011-02-10 12:56:45.262786102 +0100
682f57
@@ -52,6 +52,10 @@
682f57
 #endif
682f57
 
682f57
 #include "cyr_lock.h"
682f57
+#include <syslog.h>
682f57
+
682f57
+/* Locking timeout parameter */
682f57
+#define MAXTIME 99
682f57
 
682f57
 const char *lock_method_desc = "flock";
682f57
 
682f57
@@ -68,6 +72,18 @@ const char *lock_method_desc = "flock";
682f57
  * 'failaction' is provided, it is filled in with a pointer to a fixed
682f57
  * string naming the action that failed.
682f57
  *
682f57
+ *  Modified by jwade 4/16/2002 to work around seen file locking problem
682f57
+ *  Added locking timeout parameter to allow processes that are  
682f57
+ *  waiting for a lock to eventually time out
682f57
+ *
682f57
+ *  Calls flock() in non-blocking fashion and then retries until a 
682f57
+ *  maximum delay is reached or the lock succeeds.
682f57
+ *  
682f57
+ *  As written, uses a quadratic backoff on retries with MAXTIME being
682f57
+ *  the longest interval delay.   Total delay time is the sum of the squares
682f57
+ *  of all integers whose square is less than MAXTIME.  In the case of 
682f57
+ *  MAXTIME = 99 this is 0+1+4+9+16+25+36+49+64+81= 285 Seconds   
682f57
+ *  This time is arbitrary and can be adjusted
682f57
  */
682f57
 int lock_reopen(fd, filename, sbuf, failaction)
682f57
 int fd;
682f57
@@ -78,17 +94,29 @@ const char **failaction;
682f57
     int r;
682f57
     struct stat sbuffile, sbufspare;
682f57
     int newfd;
682f57
+    int delay=0, i=0;
682f57
 
682f57
     if (!sbuf) sbuf = &sbufspare;
682f57
 
682f57
-    for (;;) {
682f57
-	r = flock(fd, LOCK_EX);
682f57
+    for(i=0,delay=0;;) {
682f57
+	r = flock(fd, LOCK_EX|LOCK_NB);
682f57
 	if (r == -1) {
682f57
-	    if (errno == EINTR) continue;
682f57
-	    if (failaction) *failaction = "locking";
682f57
+	    if (errno == EINTR) {
682f57
+                 continue;
682f57
+            }
682f57
+            else if ((errno == EWOULDBLOCK) && (delay < MAXTIME)) {
682f57
+                syslog(LOG_DEBUG, "lock: reopen-blocked sleeping for %d on interval %d (%d, %s)" , delay, i, fd, filename);
682f57
+                sleep(delay);
682f57
+                i++;
682f57
+                delay = i*i;
682f57
+                continue;
682f57
+            }
682f57
+	    if (failaction) {
682f57
+                if (delay >= MAXTIME) *failaction = "locking_timeout";
682f57
+                else *failaction = "locking";
682f57
+            }
682f57
 	    return -1;
682f57
 	}
682f57
-
682f57
 	fstat(fd, sbuf);
682f57
 	r = stat(filename, &sbuffile);
682f57
 	if (r == -1) {
682f57
@@ -96,9 +124,7 @@ const char **failaction;
682f57
 	    flock(fd, LOCK_UN);
682f57
 	    return -1;
682f57
 	}
682f57
-
682f57
 	if (sbuf->st_ino == sbuffile.st_ino) return 0;
682f57
-
682f57
 	newfd = open(filename, O_RDWR);
682f57
 	if (newfd == -1) {
682f57
 	    if (failaction) *failaction = "opening";