Blame SOURCES/cyrus-imapd-2.1.3-flock.patch

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