dovecot-2.0-sslstream: lib-master: When executing standalone, tr...

dovecot at dovecot.org dovecot at dovecot.org
Sat Feb 13 02:58:37 EET 2010


details:   http://hg.dovecot.org/dovecot-2.0-sslstream/rev/379b993c5ca6
changeset: 10592:379b993c5ca6
user:      Timo Sirainen <tss at iki.fi>
date:      Wed Jan 27 23:20:05 2010 +0200
description:
lib-master: When executing standalone, try first to read config from global socket.

diffstat:

4 files changed, 67 insertions(+), 28 deletions(-)
src/lib-master/Makefile.am               |    1 
src/lib-master/master-service-private.h  |    1 
src/lib-master/master-service-settings.c |   88 ++++++++++++++++++++----------
src/lib-master/master-service.c          |    5 +

diffs (175 lines):

diff -r 193fa6e7635c -r 379b993c5ca6 src/lib-master/Makefile.am
--- a/src/lib-master/Makefile.am	Wed Jan 27 22:44:13 2010 +0200
+++ b/src/lib-master/Makefile.am	Wed Jan 27 23:20:05 2010 +0200
@@ -5,6 +5,7 @@ AM_CPPFLAGS = \
 AM_CPPFLAGS = \
 	-I$(top_srcdir)/src/lib \
 	-I$(top_srcdir)/src/lib-settings \
+	-DPKG_RUNDIR=\""$(rundir)"\" \
 	-DSYSCONFDIR=\""$(pkgsysconfdir)"\" \
 	-DBINDIR=\""$(bindir)"\"
 
diff -r 193fa6e7635c -r 379b993c5ca6 src/lib-master/master-service-private.h
--- a/src/lib-master/master-service-private.h	Wed Jan 27 22:44:13 2010 +0200
+++ b/src/lib-master/master-service-private.h	Wed Jan 27 23:20:05 2010 +0200
@@ -56,6 +56,7 @@ struct master_service {
 	unsigned int die_with_master:1;
 	unsigned int call_avail_overflow:1;
 	unsigned int delay_status_updates:1;
+	unsigned int config_path_is_default:1;
 };
 
 void master_service_io_listeners_add(struct master_service *service);
diff -r 193fa6e7635c -r 379b993c5ca6 src/lib-master/master-service-settings.c
--- a/src/lib-master/master-service-settings.c	Wed Jan 27 22:44:13 2010 +0200
+++ b/src/lib-master/master-service-settings.c	Wed Jan 27 23:20:05 2010 +0200
@@ -17,6 +17,7 @@
 #include <sys/stat.h>
 
 #define DOVECOT_CONFIG_BIN_PATH BINDIR"/doveconf"
+#define DOVECOT_CONFIG_SOCKET_PATH PKG_RUNDIR"/config"
 
 #define CONFIG_READ_TIMEOUT_SECS 10
 #define CONFIG_HANDSHAKE "VERSION\tconfig\t1\t0\n"
@@ -100,32 +101,65 @@ master_service_exec_config(struct master
 }
 
 static int
-master_service_read_config(struct master_service *service, const char *path,
+master_service_open_config(struct master_service *service,
 			   const struct master_service_settings_input *input,
-			   const char **error_r)
-{
+			   const char **path_r, const char **error_r)
+{
+	const char *path;
 	struct stat st;
-	int fd, ret;
-
-	if (service->config_fd != -1) {
+	int fd;
+
+	*path_r = path = input->config_path != NULL ? input->config_path :
+		master_service_get_config_path(service);
+
+	if (service->config_fd != -1 && input->config_path == NULL) {
+		/* use the already opened config socket */
 		fd = service->config_fd;
 		service->config_fd = -1;
-	} else {
-		fd = net_connect_unix_with_retries(path, 1000);
-		if (fd < 0) {
-			*error_r = t_strdup_printf(
-				"net_connect_unix(%s) failed: %m", path);
-
-			if (stat(path, &st) == 0 && 
-			    !S_ISSOCK(st.st_mode) && !S_ISFIFO(st.st_mode)) {
-				/* it's a file, not a socket/pipe */
-				master_service_exec_config(service,
-							   input->preserve_home);
-			}
-			return -1;
-		}
-		net_set_nonblock(fd, FALSE);
-	}
+		return fd;
+	}
+
+	if (service->config_path_is_default && input->config_path == NULL) {
+		/* first try to connect to the default config socket.
+		   configuration may contain secrets, so in default config
+		   this fails because the socket is 0600. it's useful for
+		   developers though. :) */
+		fd = net_connect_unix(DOVECOT_CONFIG_SOCKET_PATH);
+		if (fd >= 0) {
+			*path_r = DOVECOT_CONFIG_SOCKET_PATH;
+			net_set_nonblock(fd, FALSE);
+			return fd;
+		}
+		/* fallback to executing doveconf */
+	}
+
+	fd = net_connect_unix_with_retries(path, 1000);
+	if (fd < 0) {
+		*error_r = t_strdup_printf("net_connect_unix(%s) failed: %m",
+					   path);
+
+		if (stat(path, &st) == 0 &&
+		    !S_ISSOCK(st.st_mode) && !S_ISFIFO(st.st_mode)) {
+			/* it's a file, not a socket/pipe */
+			master_service_exec_config(service,
+						   input->preserve_home);
+		}
+		return -1;
+	}
+	net_set_nonblock(fd, FALSE);
+	return fd;
+}
+
+static int
+master_service_read_config(struct master_service *service,
+			   const struct master_service_settings_input *input,
+			   const char **path_r, const char **error_r)
+{
+	int fd, ret;
+
+	fd = master_service_open_config(service, input, path_r, error_r);
+	if (fd == -1)
+		return -1;
 
 	T_BEGIN {
 		string_t *str;
@@ -154,7 +188,8 @@ master_service_read_config(struct master
 		ret = write_full(fd, str_data(str), str_len(str));
 	} T_END;
 	if (ret < 0) {
-		*error_r = t_strdup_printf("write_full(%s) failed: %m", path);
+		*error_r = t_strdup_printf("write_full(%s) failed: %m",
+					   *path_r);
 		return -1;
 	}
 	return fd;
@@ -198,9 +233,7 @@ int master_service_settings_read(struct 
 
 	if (getenv("DOVECONF_ENV") == NULL &&
 	    (service->flags & MASTER_SERVICE_FLAG_NO_CONFIG_SETTINGS) == 0) {
-		path = input->config_path != NULL ? input->config_path :
-			master_service_get_config_path(service);
-		fd = master_service_read_config(service, path, input, error_r);
+		fd = master_service_read_config(service, input, &path, error_r);
 		if (fd == -1)
 			return -1;
 	}
@@ -259,7 +292,8 @@ int master_service_settings_read(struct 
 		}
 	}
 
-	if ((service->flags & MASTER_SERVICE_FLAG_KEEP_CONFIG_OPEN) != 0)
+	if ((service->flags & MASTER_SERVICE_FLAG_KEEP_CONFIG_OPEN) != 0 &&
+	    service->config_fd == -1 && input->config_path == NULL)
 		service->config_fd = fd;
 	else if (fd != -1)
 		(void)close(fd);
diff -r 193fa6e7635c -r 379b993c5ca6 src/lib-master/master-service.c
--- a/src/lib-master/master-service.c	Wed Jan 27 22:44:13 2010 +0200
+++ b/src/lib-master/master-service.c	Wed Jan 27 23:20:05 2010 +0200
@@ -134,8 +134,10 @@ master_service_init(const char *name, en
 	service->config_fd = -1;
 
 	service->config_path = getenv(MASTER_CONFIG_FILE_ENV);
-	if (service->config_path == NULL)
+	if (service->config_path == NULL) {
 		service->config_path = DEFAULT_CONFIG_FILE_PATH;
+		service->config_path_is_default = TRUE;
+	}
 
 	if ((flags & MASTER_SERVICE_FLAG_STANDALONE) == 0) {
 		service->version_string = getenv(MASTER_DOVECOT_VERSION_ENV);
@@ -245,6 +247,7 @@ bool master_service_parse_option(struct 
 	switch (opt) {
 	case 'c':
 		service->config_path = arg;
+		service->config_path_is_default = FALSE;
 		break;
 	case 'k':
 		service->keep_environment = TRUE;


More information about the dovecot-cvs mailing list