changeset 257:90abe1af4c72 HEAD

fixed some locking issues
author Timo Sirainen <tss@iki.fi>
date Mon, 16 Sep 2002 11:41:35 +0300
parents 73a5f8a34893
children 0a45c4744f51
files src/lib-index/mail-modifylog.c
diffstat 1 files changed, 19 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib-index/mail-modifylog.c	Mon Sep 16 10:39:26 2002 +0300
+++ b/src/lib-index/mail-modifylog.c	Mon Sep 16 11:41:35 2002 +0300
@@ -73,16 +73,6 @@
 	return FALSE;
 }
 
-static int mail_modifylog_wait_lock(MailModifyLog *log)
-{
-	i_assert(!log->anon_mmap);
-
-	if (file_wait_lock(log->fd, F_RDLCK) < 1)
-                modifylog_set_syscall_error(log, "file_wait_lock()");
-
-	return TRUE;
-}
-
 /* returns 1 = yes, 0 = no, -1 = error */
 static int mail_modifylog_have_other_users(MailModifyLog *log)
 {
@@ -276,14 +266,24 @@
 		return index_file_set_syscall_error(log->index, path, "open()");
 	}
 
-	ret = file_wait_lock(fd, F_WRLCK);
+	/* if we can't get the lock, we fail. it shouldn't happen. */
+	ret = file_try_lock(fd, F_WRLCK);
 	if (ret < 0) {
 		index_file_set_syscall_error(log->index, path,
 					     "file_wait_lock()");
+	} else if (ret == 0) {
+		index_set_error(log->index, "Couldn't get exclusive lock for "
+				"created modify log %s", path);
 	}
 
-	if (ret == 1 && mail_modifylog_init_fd(log, fd, path)) {
-		if (log->fd == -1 || modifylog_mark_full(log)) {
+	if (ret > 0 && mail_modifylog_init_fd(log, fd, path)) {
+		/* drop back to read lock */
+		if (file_wait_lock(fd, F_RDLCK) < 0) {
+			modifylog_set_syscall_error(log, "file_wait_lock()");
+			ret = -1;
+		}
+
+		if (ret > 0 && (log->fd == -1 || modifylog_mark_full(log))) {
 			mail_modifylog_close(log);
 
 			log->fd = fd;
@@ -320,7 +320,6 @@
 		path = t_strconcat(log->index->filepath, ".log", NULL);
 
 		if (!modifylog_open_and_init_file(log, path) ||
-		    !mail_modifylog_wait_lock(log) ||
 		    !mmap_update(log, TRUE)) {
 			/* fatal failure */
 			mail_modifylog_free(log);
@@ -349,6 +348,12 @@
 		return -1;
 	}
 
+	if (file_wait_lock(fd, F_RDLCK) < 0) {
+		modifylog_set_syscall_error(log, "file_wait_lock()");
+		(void)close(fd);
+		return -1;
+	}
+
 	ret = read(fd, &hdr, sizeof(hdr));
 	if (ret < 0)
 		index_file_set_syscall_error(log->index, path, "read()");
@@ -428,7 +433,6 @@
 	log = mail_modifylog_new(index);
 
 	if (!mail_modifylog_find_or_create(log) ||
-	    !mail_modifylog_wait_lock(log) ||
 	    !mmap_update(log, TRUE)) {
 		/* fatal failure */
 		mail_modifylog_free(log);