[dovecot-cvs] dovecot/src/master main.c, 1.80.2.21, 1.80.2.22 ssl-init-main.c, 1.2, 1.2.2.1 ssl-init.c, 1.25.2.2, 1.25.2.3 ssl-init.h, 1.5, 1.5.2.1

tss at dovecot.org tss at dovecot.org
Sun May 13 16:45:48 EEST 2007


Update of /var/lib/cvs/dovecot/src/master
In directory talvi:/tmp/cvs-serv13618

Modified Files:
      Tag: branch_1_0
	main.c ssl-init-main.c ssl-init.c ssl-init.h 
Log Message:
When running multiple Dovecot instances, only one of them needs to
regenerate ssl-parameters.dat.



Index: main.c
===================================================================
RCS file: /var/lib/cvs/dovecot/src/master/main.c,v
retrieving revision 1.80.2.21
retrieving revision 1.80.2.22
diff -u -d -r1.80.2.21 -r1.80.2.22
--- main.c	11 May 2007 13:28:52 -0000	1.80.2.21
+++ main.c	13 May 2007 13:45:46 -0000	1.80.2.22
@@ -218,6 +218,9 @@
 					i_error("unknown child %s exited "
 						"successfully", dec2str(pid));
 				}
+			} else if (status == 1 &&
+				   process_type == PROCESS_TYPE_SSL_PARAM) {
+				/* kludgy. hide this failure. */
 			} else {
 				msg = get_exit_status_message(status);
 				msg = msg == NULL ? "" :
@@ -241,7 +244,7 @@
 			mail_process_destroyed(pid);
 			break;
 		case PROCESS_TYPE_SSL_PARAM:
-			ssl_parameter_process_destroyed(pid);
+			ssl_parameter_process_destroyed(abnormal_exit);
 			break;
 		case PROCESS_TYPE_DICT:
 			dict_process_restart();

Index: ssl-init-main.c
===================================================================
RCS file: /var/lib/cvs/dovecot/src/master/ssl-init-main.c,v
retrieving revision 1.2
retrieving revision 1.2.2.1
diff -u -d -r1.2 -r1.2.2.1
--- ssl-init-main.c	7 Feb 2006 12:38:04 -0000	1.2
+++ ssl-init-main.c	13 May 2007 13:45:46 -0000	1.2.2.1
@@ -2,6 +2,7 @@
 
 #include "lib.h"
 #include "lib-signals.h"
+#include "file-lock.h"
 #include "randgen.h"
 #include "ssl-init.h"
 
@@ -11,17 +12,16 @@
 #include <sys/stat.h>
 
 #ifdef HAVE_SSL
-static void generate_parameters_file(const char *fname)
+static int generate_parameters_file(const char *fname)
 {
 	const char *temp_fname;
 	mode_t old_mask;
-	int fd;
+	int fd, ret;
 
 	temp_fname = t_strconcat(fname, ".tmp", NULL);
-	(void)unlink(temp_fname);
 
 	old_mask = umask(0);
-	fd = open(temp_fname, O_WRONLY | O_CREAT | O_EXCL, 0644);
+	fd = open(temp_fname, O_WRONLY | O_CREAT, 0644);
 	umask(old_mask);
 
 	if (fd == -1) {
@@ -29,25 +29,40 @@
 			temp_fname);
 	}
 
-	_ssl_generate_parameters(fd, temp_fname);
+	/* If multiple dovecot instances are running, only one of them needs
+	   to regenerate this file. */
+	ret = file_try_lock(fd, F_WRLCK);
+	if (ret < 0)
+		i_fatal("file_try_lock(%s) failed: %m", temp_fname);
+	if (ret == 0) {
+		/* someone else is writing this */
+		return -1;
+	}
+	if (ftruncate(fd, 0) < 0)
+		i_fatal("ftruncate(%s) failed: %m", temp_fname);
 
-	if (close(fd) < 0)
-		i_fatal("close(%s) failed: %m", temp_fname);
+	_ssl_generate_parameters(fd, temp_fname);
 
 	if (rename(temp_fname, fname) < 0)
 		i_fatal("rename(%s, %s) failed: %m", temp_fname, fname);
+	if (close(fd) < 0)
+		i_fatal("close(%s) failed: %m", temp_fname);
 
 	i_info("SSL parameters regeneration completed");
+	return 0;
 }
 #else
-static void generate_parameters_file(const char *fname __attr_unused__)
+static int generate_parameters_file(const char *fname __attr_unused__)
 {
 	i_fatal("Dovecot built without SSL support");
+	return -1;
 }
 #endif
 
 int main(int argc, char *argv[])
 {
+	int ret;
+
 	lib_init();
 	i_set_failure_internal();
 
@@ -55,9 +70,10 @@
 		i_fatal("Usage: ssl-build-param <path>");
 
 	random_init();
-	generate_parameters_file(argv[1]);
+	if (generate_parameters_file(argv[1]) < 0)
+		ret = 1;
 
 	random_deinit();
 	lib_deinit();
-	return 0;
+	return ret;
 }

Index: ssl-init.c
===================================================================
RCS file: /var/lib/cvs/dovecot/src/master/ssl-init.c,v
retrieving revision 1.25.2.2
retrieving revision 1.25.2.3
diff -u -d -r1.25.2.2 -r1.25.2.3
--- ssl-init.c	26 Jun 2006 08:56:26 -0000	1.25.2.2
+++ ssl-init.c	13 May 2007 13:45:46 -0000	1.25.2.3
@@ -60,11 +60,14 @@
 	i_fatal_status(FATAL_EXEC, "execv(%s) failed: %m", binpath);
 }
 
-void ssl_parameter_process_destroyed(pid_t pid __attr_unused__)
+void ssl_parameter_process_destroyed(bool abnormal_exit)
 {
-	if (file_copy(SSL_PARAMETERS_PERM_PATH, generating_path, TRUE) <= 0) {
-		i_error("file_copy(%s, %s) failed: %m",
-			SSL_PARAMETERS_PERM_PATH, generating_path);
+	if (!abnormal_exit) {
+		if (file_copy(SSL_PARAMETERS_PERM_PATH,
+			      generating_path, TRUE) <= 0) {
+			i_error("file_copy(%s, %s) failed: %m",
+				SSL_PARAMETERS_PERM_PATH, generating_path);
+		}
 	}
 	i_free_and_null(generating_path);
 }

Index: ssl-init.h
===================================================================
RCS file: /var/lib/cvs/dovecot/src/master/ssl-init.h,v
retrieving revision 1.5
retrieving revision 1.5.2.1
diff -u -d -r1.5 -r1.5.2.1
--- ssl-init.h	5 Feb 2006 18:00:15 -0000	1.5
+++ ssl-init.h	13 May 2007 13:45:46 -0000	1.5.2.1
@@ -3,7 +3,7 @@
 
 #define SSL_PARAMETERS_FILENAME "ssl-parameters.dat"
 
-void ssl_parameter_process_destroyed(pid_t pid);
+void ssl_parameter_process_destroyed(bool abnormal_exit);
 
 void ssl_check_parameters_file(void);
 void _ssl_generate_parameters(int fd, const char *fname);



More information about the dovecot-cvs mailing list