changeset 19713:aad2bfc8cbd3

stats: Split stats-connection.[ch] to lib-stats/ and plugin's mail-specific parts.
author Timo Sirainen <timo.sirainen@dovecot.fi>
date Fri, 05 Feb 2016 15:07:00 +0200
parents b2cde9a21ff8
children e3bc345e00ea
files src/lib-stats/Makefile.am src/lib-stats/stats-connection.c src/lib-stats/stats-connection.h src/plugins/stats/Makefile.am src/plugins/stats/mail-stats-connection.c src/plugins/stats/mail-stats-connection.h src/plugins/stats/stats-connection.c src/plugins/stats/stats-connection.h src/plugins/stats/stats-plugin.c
diffstat 9 files changed, 216 insertions(+), 197 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib-stats/Makefile.am	Fri Feb 05 14:55:56 2016 +0200
+++ b/src/lib-stats/Makefile.am	Fri Feb 05 15:07:00 2016 +0200
@@ -1,14 +1,17 @@
 noinst_LTLIBRARIES = libstats.la
 
 AM_CPPFLAGS = \
-	-I$(top_srcdir)/src/lib
+	-I$(top_srcdir)/src/lib \
+	-I$(top_srcdir)/src/lib-master
 
 libstats_la_SOURCES = \
 	stats.c \
+	stats-connection.c \
 	stats-parser.c
 
 headers = \
 	stats.h \
+	stats-connection.h \
 	stats-parser.h
 
 pkginc_libdir = $(pkgincludedir)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/lib-stats/stats-connection.c	Fri Feb 05 15:07:00 2016 +0200
@@ -0,0 +1,104 @@
+/* Copyright (c) 2011-2016 Dovecot authors, see the included COPYING file */
+
+#include "lib.h"
+#include "str.h"
+#include "master-service.h"
+#include "stats-connection.h"
+
+#include <unistd.h>
+#include <fcntl.h>
+
+struct stats_connection {
+	int refcount;
+
+	int fd;
+	char *path;
+
+	bool open_failed;
+};
+
+static bool stats_connection_open(struct stats_connection *conn)
+{
+	if (conn->open_failed)
+		return FALSE;
+
+	conn->fd = open(conn->path, O_WRONLY | O_NONBLOCK);
+	if (conn->fd == -1) {
+		i_error("stats: open(%s) failed: %m", conn->path);
+		conn->open_failed = TRUE;
+		return FALSE;
+	}
+	return TRUE;
+}
+
+struct stats_connection *
+stats_connection_create(const char *path)
+{
+	struct stats_connection *conn;
+
+	conn = i_new(struct stats_connection, 1);
+	conn->refcount = 1;
+	conn->path = i_strdup(path);
+	(void)stats_connection_open(conn);
+	return conn;
+}
+
+void stats_connection_ref(struct stats_connection *conn)
+{
+	conn->refcount++;
+}
+
+void stats_connection_unref(struct stats_connection **_conn)
+{
+	struct stats_connection *conn = *_conn;
+
+	i_assert(conn->refcount > 0);
+	if (--conn->refcount > 0)
+		return;
+
+	*_conn = NULL;
+	if (conn->fd != -1) {
+		if (close(conn->fd) < 0)
+			i_error("close(%s) failed: %m", conn->path);
+	}
+	i_free(conn->path);
+	i_free(conn);
+}
+
+void stats_connection_send(struct stats_connection *conn, const string_t *str)
+{
+	static bool pipe_warned = FALSE;
+	ssize_t ret;
+
+	/* if master process has been stopped (and restarted), don't even try
+	   to notify the stats process anymore. even if one exists, it doesn't
+	   know about us. */
+	if (master_service_is_master_stopped(master_service))
+		return;
+
+	if (conn->fd == -1) {
+		if (!stats_connection_open(conn))
+			return;
+	}
+
+	if (str_len(str) > PIPE_BUF && !pipe_warned) {
+		i_warning("stats update sent more bytes that PIPE_BUF "
+			  "(%"PRIuSIZE_T" > %u), this may break statistics",
+			  str_len(str), (unsigned int)PIPE_BUF);
+		pipe_warned = TRUE;
+	}
+
+	ret = write(conn->fd, str_data(str), str_len(str));
+	if (ret != (ssize_t)str_len(str)) {
+		if (ret < 0) {
+			/* don't log EPIPE errors. they can happen when
+			   Dovecot is stopped. */
+			if (errno != EPIPE)
+				i_error("write(%s) failed: %m", conn->path);
+		} else if ((size_t)ret != str_len(str))
+			i_error("write(%s) wrote partial update", conn->path);
+		if (close(conn->fd) < 0)
+			i_error("close(%s) failed: %m", conn->path);
+		conn->fd = -1;
+	}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/lib-stats/stats-connection.h	Fri Feb 05 15:07:00 2016 +0200
@@ -0,0 +1,10 @@
+#ifndef STATS_CONNECTION_H
+#define STATS_CONNECTION_H
+
+struct stats_connection *stats_connection_create(const char *path);
+void stats_connection_ref(struct stats_connection *conn);
+void stats_connection_unref(struct stats_connection **conn);
+
+void stats_connection_send(struct stats_connection *conn, const string_t *str);
+
+#endif
--- a/src/plugins/stats/Makefile.am	Fri Feb 05 14:55:56 2016 +0200
+++ b/src/plugins/stats/Makefile.am	Fri Feb 05 15:07:00 2016 +0200
@@ -16,12 +16,12 @@
 lib90_stats_plugin_la_SOURCES = \
 	mail-stats.c \
 	mail-stats-fill.c \
-	stats-connection.c \
+	mail-stats-connection.c \
 	stats-plugin.c
 
 noinst_HEADERS = \
 	mail-stats.h \
-	stats-connection.h \
+	mail-stats-connection.h \
 	stats-plugin.h
 
 stats_moduledir = $(moduledir)/stats
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/plugins/stats/mail-stats-connection.c	Fri Feb 05 15:07:00 2016 +0200
@@ -0,0 +1,72 @@
+/* Copyright (c) 2011-2016 Dovecot authors, see the included COPYING file */
+
+#include "lib.h"
+#include "base64.h"
+#include "hostpid.h"
+#include "net.h"
+#include "str.h"
+#include "strescape.h"
+#include "mail-storage.h"
+#include "stats.h"
+#include "stats-plugin.h"
+#include "mail-stats-connection.h"
+
+void mail_stats_connection_connect(struct stats_connection *conn,
+				   struct mail_user *user)
+{
+	struct stats_user *suser = STATS_USER_CONTEXT(user);
+	string_t *str = t_str_new(128);
+
+	str_append(str, "CONNECT\t");
+	/* required fields */
+	str_append(str, suser->stats_session_id);
+	str_append_c(str, '\t');
+	str_append_tabescaped(str, user->username);
+	str_append_c(str, '\t');
+	str_append_tabescaped(str, user->service);
+	str_printfa(str, "\t%s", my_pid);
+
+	/* optional fields */
+	if (user->local_ip != NULL) {
+		str_append(str, "\tlip=");
+		str_append(str, net_ip2addr(user->local_ip));
+	}
+	if (user->remote_ip != NULL) {
+		str_append(str, "\trip=");
+		str_append(str, net_ip2addr(user->remote_ip));
+	}
+	str_append_c(str, '\n');
+	stats_connection_send(conn, str);
+}
+
+void mail_stats_connection_disconnect(struct stats_connection *conn,
+				      struct mail_user *user)
+{
+	struct stats_user *suser = STATS_USER_CONTEXT(user);
+	string_t *str = t_str_new(128);
+
+	str_append(str, "DISCONNECT\t");
+	str_append(str, suser->stats_session_id);
+	str_append_c(str, '\n');
+	stats_connection_send(conn, str);
+}
+
+void mail_stats_connection_send_session(struct stats_connection *conn,
+					struct mail_user *user,
+					const struct stats *stats)
+{
+	struct stats_user *suser = STATS_USER_CONTEXT(user);
+	string_t *str = t_str_new(256);
+	buffer_t *buf;
+
+	buf = buffer_create_dynamic(pool_datastack_create(), 128);
+	stats_export(buf, stats);
+
+	str_append(str, "UPDATE-SESSION\t");
+	str_append(str, suser->stats_session_id);
+	str_append_c(str, '\t');
+	base64_encode(buf->data, buf->used, str);
+
+	str_append_c(str, '\n');
+	stats_connection_send(conn, str);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/plugins/stats/mail-stats-connection.h	Fri Feb 05 15:07:00 2016 +0200
@@ -0,0 +1,19 @@
+#ifndef MAIL_STATS_CONNECTION_H
+#define MAIL_STATS_CONNECTION_H
+
+#include "stats-connection.h"
+
+struct mail_stats;
+struct mail_user;
+
+void mail_stats_connection_connect(struct stats_connection *conn,
+				   struct mail_user *user);
+void mail_stats_connection_disconnect(struct stats_connection *conn,
+				      struct mail_user *user);
+
+void mail_stats_connection_send_session(struct stats_connection *conn,
+					struct mail_user *user,
+					const struct stats *stats);
+void mail_stats_connection_send(struct stats_connection *conn, const string_t *str);
+
+#endif
--- a/src/plugins/stats/stats-connection.c	Fri Feb 05 14:55:56 2016 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,168 +0,0 @@
-/* Copyright (c) 2011-2016 Dovecot authors, see the included COPYING file */
-
-#include "lib.h"
-#include "base64.h"
-#include "hostpid.h"
-#include "net.h"
-#include "str.h"
-#include "strescape.h"
-#include "master-service.h"
-#include "mail-storage.h"
-#include "stats.h"
-#include "stats-plugin.h"
-#include "stats-connection.h"
-
-struct stats_connection {
-	int refcount;
-
-	int fd;
-	char *path;
-
-	bool open_failed;
-};
-
-static bool stats_connection_open(struct stats_connection *conn)
-{
-	if (conn->open_failed)
-		return FALSE;
-
-	conn->fd = open(conn->path, O_WRONLY | O_NONBLOCK);
-	if (conn->fd == -1) {
-		i_error("stats: open(%s) failed: %m", conn->path);
-		conn->open_failed = TRUE;
-		return FALSE;
-	}
-	return TRUE;
-}
-
-struct stats_connection *
-stats_connection_create(const char *path)
-{
-	struct stats_connection *conn;
-
-	conn = i_new(struct stats_connection, 1);
-	conn->refcount = 1;
-	conn->path = i_strdup(path);
-	(void)stats_connection_open(conn);
-	return conn;
-}
-
-void stats_connection_ref(struct stats_connection *conn)
-{
-	conn->refcount++;
-}
-
-void stats_connection_unref(struct stats_connection **_conn)
-{
-	struct stats_connection *conn = *_conn;
-
-	i_assert(conn->refcount > 0);
-	if (--conn->refcount > 0)
-		return;
-
-	*_conn = NULL;
-	if (conn->fd != -1) {
-		if (close(conn->fd) < 0)
-			i_error("close(%s) failed: %m", conn->path);
-	}
-	i_free(conn->path);
-	i_free(conn);
-}
-
-void stats_connection_send(struct stats_connection *conn, const string_t *str)
-{
-	static bool pipe_warned = FALSE;
-	ssize_t ret;
-
-	/* if master process has been stopped (and restarted), don't even try
-	   to notify the stats process anymore. even if one exists, it doesn't
-	   know about us. */
-	if (master_service_is_master_stopped(master_service))
-		return;
-
-	if (conn->fd == -1) {
-		if (!stats_connection_open(conn))
-			return;
-	}
-
-	if (str_len(str) > PIPE_BUF && !pipe_warned) {
-		i_warning("stats update sent more bytes that PIPE_BUF "
-			  "(%"PRIuSIZE_T" > %u), this may break statistics",
-			  str_len(str), (unsigned int)PIPE_BUF);
-		pipe_warned = TRUE;
-	}
-
-	ret = write(conn->fd, str_data(str), str_len(str));
-	if (ret != (ssize_t)str_len(str)) {
-		if (ret < 0) {
-			/* don't log EPIPE errors. they can happen when
-			   Dovecot is stopped. */
-			if (errno != EPIPE)
-				i_error("write(%s) failed: %m", conn->path);
-		} else if ((size_t)ret != str_len(str))
-			i_error("write(%s) wrote partial update", conn->path);
-		if (close(conn->fd) < 0)
-			i_error("close(%s) failed: %m", conn->path);
-		conn->fd = -1;
-	}
-}
-
-void stats_connection_connect(struct stats_connection *conn,
-			      struct mail_user *user)
-{
-	struct stats_user *suser = STATS_USER_CONTEXT(user);
-	string_t *str = t_str_new(128);
-
-	str_append(str, "CONNECT\t");
-	/* required fields */
-	str_append(str, suser->stats_session_id);
-	str_append_c(str, '\t');
-	str_append_tabescaped(str, user->username);
-	str_append_c(str, '\t');
-	str_append_tabescaped(str, user->service);
-	str_printfa(str, "\t%s", my_pid);
-
-	/* optional fields */
-	if (user->local_ip != NULL) {
-		str_append(str, "\tlip=");
-		str_append(str, net_ip2addr(user->local_ip));
-	}
-	if (user->remote_ip != NULL) {
-		str_append(str, "\trip=");
-		str_append(str, net_ip2addr(user->remote_ip));
-	}
-	str_append_c(str, '\n');
-	stats_connection_send(conn, str);
-}
-
-void stats_connection_disconnect(struct stats_connection *conn,
-				 struct mail_user *user)
-{
-	struct stats_user *suser = STATS_USER_CONTEXT(user);
-	string_t *str = t_str_new(128);
-
-	str_append(str, "DISCONNECT\t");
-	str_append(str, suser->stats_session_id);
-	str_append_c(str, '\n');
-	stats_connection_send(conn, str);
-}
-
-void stats_connection_send_session(struct stats_connection *conn,
-				   struct mail_user *user,
-				   const struct stats *stats)
-{
-	struct stats_user *suser = STATS_USER_CONTEXT(user);
-	string_t *str = t_str_new(256);
-	buffer_t *buf;
-
-	buf = buffer_create_dynamic(pool_datastack_create(), 128);
-	stats_export(buf, stats);
-
-	str_append(str, "UPDATE-SESSION\t");
-	str_append(str, suser->stats_session_id);
-	str_append_c(str, '\t');
-	base64_encode(buf->data, buf->used, str);
-
-	str_append_c(str, '\n');
-	stats_connection_send(conn, str);
-}
--- a/src/plugins/stats/stats-connection.h	Fri Feb 05 14:55:56 2016 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,21 +0,0 @@
-#ifndef STATS_CONNECTION_H
-#define STATS_CONNECTION_H
-
-struct mail_stats;
-struct mail_user;
-
-struct stats_connection *stats_connection_create(const char *path);
-void stats_connection_ref(struct stats_connection *conn);
-void stats_connection_unref(struct stats_connection **conn);
-
-void stats_connection_connect(struct stats_connection *conn,
-			      struct mail_user *user);
-void stats_connection_disconnect(struct stats_connection *conn,
-				 struct mail_user *user);
-
-void stats_connection_send_session(struct stats_connection *conn,
-				   struct mail_user *user,
-				   const struct stats *stats);
-void stats_connection_send(struct stats_connection *conn, const string_t *str);
-
-#endif
--- a/src/plugins/stats/stats-plugin.c	Fri Feb 05 14:55:56 2016 +0200
+++ b/src/plugins/stats/stats-plugin.c	Fri Feb 05 15:07:00 2016 +0200
@@ -8,7 +8,7 @@
 #include "settings-parser.h"
 #include "mail-stats.h"
 #include "stats.h"
-#include "stats-connection.h"
+#include "mail-stats-connection.h"
 #include "stats-plugin.h"
 
 #define STATS_CONTEXT(obj) \
@@ -129,8 +129,8 @@
 		suser->session_sent_duplicate = !changed;
 		suser->last_session_update = now;
 		stats_copy(suser->last_sent_session_stats, suser->session_stats);
-		stats_connection_send_session(suser->stats_conn, user,
-					      suser->session_stats);
+		mail_stats_connection_send_session(suser->stats_conn, user,
+						   suser->session_stats);
 	}
 
 	if (suser->to_stats_timeout != NULL)
@@ -333,7 +333,7 @@
 					 stats_io_deactivate, user);
 	/* send final stats before disconnection */
 	session_stats_refresh(user);
-	stats_connection_disconnect(stats_conn, user);
+	mail_stats_connection_disconnect(stats_conn, user);
 
 	if (suser->to_stats_timeout != NULL)
 		timeout_remove(&suser->to_stats_timeout);
@@ -434,7 +434,7 @@
 	suser->last_sent_session_stats = stats_alloc(user->pool);
 
 	MODULE_CONTEXT_SET(user, stats_user_module, suser);
-	stats_connection_connect(suser->stats_conn, user);
+	mail_stats_connection_connect(suser->stats_conn, user);
 	suser->to_stats_timeout =
 		timeout_add(suser->refresh_secs*1000,
 			    session_stats_refresh_timeout, user);