d0d307
--- cyrus-imapd-2.1.3/lib/lock_flock.c	Tue Oct  2 16:08:13 2001
d0d307
+++ cyrus-imapd-2.1.3-patched/lib/lock_flock.c	Tue Apr 16 09:44:58 2002
d0d307
@@ -51,6 +51,10 @@
d0d307
 #endif
d0d307
 
d0d307
 #include "lock.h"
d0d307
+#include <syslog.h>
d0d307
+
d0d307
+/* Locking timeout parameter */
d0d307
+#define MAXTIME 99
d0d307
 
d0d307
 const char *lock_method_desc = "flock";
d0d307
 
d0d307
@@ -69,6 +73,18 @@
d0d307
  * 'failaction' is provided, it is filled in with a pointer to a fixed
d0d307
  * string naming the action that failed.
d0d307
  *
d0d307
+ *  Modified by jwade 4/16/2002 to work around seen file locking problem
d0d307
+ *  Added locking timeout parameter to allow processes that are  
d0d307
+ *  waiting for a lock to eventually time out
d0d307
+ *
d0d307
+ *  Calls flock() in non-blocking fashion and then retries until a 
d0d307
+ *  maximum delay is reached or the lock succeeds.
d0d307
+ *  
d0d307
+ *  As written, uses a quadratic backoff on retries with MAXTIME being
d0d307
+ *  the longest interval delay.   Total delay time is the sum of the squares
d0d307
+ *  of all integers whose square is less than MAXTIME.  In the case of 
d0d307
+ *  MAXTIME = 99 this is 0+1+4+9+16+25+36+49+64+81= 285 Seconds   
d0d307
+ *  This time is arbitrary and can be adjusted
d0d307
  */
d0d307
 int lock_reopen(fd, filename, sbuf, failaction)
d0d307
 int fd;
d0d307
@@ -79,17 +95,29 @@
d0d307
     int r;
d0d307
     struct stat sbuffile, sbufspare;
d0d307
     int newfd;
d0d307
+    int delay=0, i=0;
d0d307
 
d0d307
     if (!sbuf) sbuf = &sbufspare;
d0d307
 
d0d307
-    for (;;) {
d0d307
-	r = flock(fd, LOCK_EX);
d0d307
+    for(i=0,delay=0;;) {
d0d307
+	r = flock(fd, LOCK_EX|LOCK_NB);
d0d307
 	if (r == -1) {
d0d307
-	    if (errno == EINTR) continue;
d0d307
-	    if (failaction) *failaction = "locking";
d0d307
+	    if (errno == EINTR) {
d0d307
+                 continue;
d0d307
+            }
d0d307
+            else if ((errno == EWOULDBLOCK) && (delay < MAXTIME)) {
d0d307
+                syslog(LOG_DEBUG, "lock: reopen-blocked sleeping for %d on interval %d (%d, %s)" , delay, i, fd, filename);
d0d307
+                sleep(delay);
d0d307
+                i++;
d0d307
+                delay = i*i;
d0d307
+                continue;
d0d307
+            }
d0d307
+	    if (failaction) {
d0d307
+                if (delay >= MAXTIME) *failaction = "locking_timeout";
d0d307
+                else *failaction = "locking";
d0d307
+            }
d0d307
 	    return -1;
d0d307
 	}
d0d307
-
d0d307
 	fstat(fd, sbuf);
d0d307
 	r = stat(filename, &sbuffile);
d0d307
 	if (r == -1) {
d0d307
@@ -97,9 +125,7 @@
d0d307
 	    flock(fd, LOCK_UN);
d0d307
 	    return -1;
d0d307
 	}
d0d307
-
d0d307
 	if (sbuf->st_ino == sbuffile.st_ino) return 0;
d0d307
-
d0d307
 	newfd = open(filename, O_RDWR);
d0d307
 	if (newfd == -1) {
d0d307
 	    if (failaction) *failaction = "opening";