Claudio,

I wonder if you could take a look at this new patch for the NOTYET 
feature of qmail-reply.  I've tested this using the "10 concurrent 
delivery test" and in each test all 10 senders showed up in the 
reply.db file.

I figure you're much better suited to finding the mouse traps than I 
am for obvious reasons.  This could be completely brain-dead, but 
then again if may be a suitable implementation.

This patch is against a qmail-reply.c that has already been patched 
with the latest patch you sent to the list - it only contains a 
potential fix to recent_update().


        -neil

--- qmail-reply.org	Fri Jan 24 18:14:59 2003
+++ qmail-reply.c	Fri Jan 24 18:17:12 2003
@@ -30,6 +30,14 @@
 void temp_rewind() { strerr_die2x(111, FATAL, "Unable to rewind message."); }
 void temp_fork() { strerr_die2sys(111, FATAL, "Unable to fork: "); }
 
+static void free_stralloc(stralloc* sa)
+{
+	sa->len = 0;
+	alloc_free(sa->s);
+	sa->s = 0;
+	return;
+}
+
 void usage(void)
 {
 	strerr_die1x(100,
@@ -265,9 +273,45 @@
 	substdio ss;
 	char *s, *t;
 	datetime_sec time, last;
-	unsigned long pid;
 	unsigned int slen, i, n;
 	int fd, loop;
+	int rest=0;
+
+	time = now();
+	t = fntmptph;
+	t += fmt_str(t, ".qmail-reply.tmp");
+	*t++ = 0;
+	
+
+	sig_alarmcatch(sigalrm);
+	alarm(600); /* give up after 10 min */
+
+	fd = open_excl(fntmptph);
+	if (fd == -1) {  	/*someone else is updating db*/
+		rest=(int) (getpid() % 5) + 1;  /*pause for up to 5 seconds*/
+		for (loop = 0;;++loop) {
+			if (stat(fntmptph, &st) == -1) 
+				if (errno == error_noent) break;
+			if (st.st_mtime + 300 < time) {
+				/* stale tmp file */
+				tryunlinktmp();
+				if ((fd = open_excl(fntmptph)) != -1 ) break;
+			}
+			if (loop == 2)
+				strerr_die2x(111, FATAL,
+				    "could not open tmp file.");
+			sleep(rest++);
+		}
+	}
+	/*re-read the db just in case it was updated*/ 
+	/* while we were snoozing*/
+	if (rest) {
+		free_stralloc(&rs);
+		n=control_readfile(&rs,".qmail-reply.db",1);
+		if (n == -1)
+			strerr_die2sys(111, FATAL,
+			    "read db file .qmail-reply.db: ");
+	}
 
 	s = rs.s; slen = rs.len;
 	n = slen + len + 10;
@@ -278,31 +322,9 @@
 		s += i;
 	}
 
-	pid = getpid();
-	time = now();
-	t = fntmptph;
-	t += fmt_str(t, ".qmail-reply.tmp.");
-	t += fmt_ulong(t, pid);
-	*t++ = 0;
-	
-	for (loop = 0;;++loop) {
-		if (stat(fntmptph, &st) == -1) if (errno == error_noent)
-			break;
-		/* really should never get to this point */
-		if (st.st_mtime + 900 < time) {
-			/* stale tmp file */
-			tryunlinktmp();
-		}
-		if (loop == 2)
-			strerr_die2x(111, FATAL,
-			    "could not open tmp file.");
-		sleep(2);
-	}
-
-	sig_alarmcatch(sigalrm);
-	alarm(600); /* give up after 10 min */
-	fd = open_excl(fntmptph);
-	if (fd == -1)
+	/*if fd == -1 try open again*/
+	if (fd == -1) fd = open_excl(fntmptph);
+	if (fd == -1)   /*if still -1 give up*/
 		strerr_die2sys(111, FATAL, "unable to open tmp file: ");
 
 	substdio_fdbuf(&ss, write, fd, rsoutbuf, sizeof(rsoutbuf));

Reply via email to