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