[evolution-patches] [mailer] when starting spamd, use unix domain socket instead of opening tcp port #53109



Attached patch changes junk plugin behavior to use unix sockets instead of tcp connection for locally runned spamd daemon. It also kills the daemon on evolution quit.
Index: camel/ChangeLog
===================================================================
RCS file: /cvs/gnome/evolution/camel/ChangeLog,v
retrieving revision 1.2268
diff -u -p -r1.2268 ChangeLog
--- camel/ChangeLog	4 Oct 2004 15:13:25 -0000	1.2268
+++ camel/ChangeLog	12 Oct 2004 14:47:09 -0000
@@ -1,3 +1,9 @@
+2004-10-12  Radek Doulik  <rodo ximian com>
+
+	* camel-junk-plugin.c: new init/quit methods implementations
+
+	* camel-junk-plugin.h: added junk plugin init/quit declarations
+
 2004-10-04  JP Rosevear  <jpr novell com>
 
 	* providers/imap4/camel-imap4-provider.c: include camel-i18n.h
Index: camel/camel-junk-plugin.c
===================================================================
RCS file: /cvs/gnome/evolution/camel/camel-junk-plugin.c,v
retrieving revision 1.2
diff -u -p -r1.2 camel-junk-plugin.c
--- camel/camel-junk-plugin.c	12 Nov 2003 21:12:48 -0000	1.2
+++ camel/camel-junk-plugin.c	12 Oct 2004 14:47:09 -0000
@@ -73,3 +73,21 @@ camel_junk_plugin_commit_reports (CamelJ
 	if (csp->commit_reports)
 		(*csp->commit_reports) ();
 }
+
+void
+camel_junk_plugin_init (CamelJunkPlugin *csp)
+{
+	d(fprintf (stderr, "camel_junk_plugin_init\n");)
+
+	if (csp->init)
+		(*csp->init) ();
+}
+
+void
+camel_junk_plugin_quit (CamelJunkPlugin *csp)
+{
+	d(fprintf (stderr, "camel_junk_plugin_quit\n");)
+
+	if (csp->quit)
+		(*csp->quit) ();
+}
Index: camel/camel-junk-plugin.h
===================================================================
RCS file: /cvs/gnome/evolution/camel/camel-junk-plugin.h,v
retrieving revision 1.2
diff -u -p -r1.2 camel-junk-plugin.h
--- camel/camel-junk-plugin.h	12 Nov 2003 21:12:48 -0000	1.2
+++ camel/camel-junk-plugin.h	12 Oct 2004 14:47:09 -0000
@@ -48,6 +48,12 @@ struct _CamelJunkPlugin
 
 	/* called after one or more junk/ham(s) reported */
 	void (*commit_reports) (void);
+
+	/* called before all other calls to junk plugin for initialization */
+	void (*init) (void);
+
+	/* called when evo is going to quit */
+	void (*quit) (void);
 };
 
 const char * camel_junk_plugin_get_name (CamelJunkPlugin *csp);
@@ -55,5 +61,7 @@ int camel_junk_plugin_check_junk (CamelJ
 void camel_junk_plugin_report_junk (CamelJunkPlugin *csp, struct _CamelMimeMessage *message);
 void camel_junk_plugin_report_notjunk (CamelJunkPlugin *csp, struct _CamelMimeMessage *message);
 void camel_junk_plugin_commit_reports (CamelJunkPlugin *csp);
+void camel_junk_plugin_init (CamelJunkPlugin *csp);
+void camel_junk_plugin_quit (CamelJunkPlugin *csp);
 
 #endif
Index: mail/ChangeLog
===================================================================
RCS file: /cvs/gnome/evolution/mail/ChangeLog,v
retrieving revision 1.3466
diff -u -p -r1.3466 ChangeLog
--- mail/ChangeLog	30 Sep 2004 01:47:23 -0000	1.3466
+++ mail/ChangeLog	12 Oct 2004 14:47:13 -0000
@@ -1,3 +1,14 @@
+2004-10-12  Radek Doulik  <rodo ximian com>
+
+	* em-junk-filter.c: when starting new spamd, call it with
+	--socketpath parameter to use unix sockets instead of opening TCP
+	port. kill such started daemon in quit callback
+
+	* mail-component.c (impl_quit): call mail_session_quit
+
+	* mail-session.c (mail_session_init): call junk plugin init
+	(mail_session_quit): new method, called on evo exit
+
 2004-09-30  David Malcolm  <dmalcolm redhat com>
 
 	* em-migrate.c (upgrade_passwords_1_2): fix uninitialised pointer.
Index: mail/em-junk-filter.c
===================================================================
RCS file: /cvs/gnome/evolution/mail/em-junk-filter.c,v
retrieving revision 1.24
diff -u -p -r1.24 em-junk-filter.c
--- mail/em-junk-filter.c	20 Sep 2004 05:59:54 -0000	1.24
+++ mail/em-junk-filter.c	12 Oct 2004 14:47:13 -0000
@@ -33,11 +33,13 @@
 #include <signal.h>
 #include <string.h>
 #include <pthread.h>
+#include <signal.h>
 
 #include <camel/camel-file-utils.h>
 #include <camel/camel-data-wrapper.h>
 #include <camel/camel-stream-fs.h>
 #include <camel/camel-i18n.h>
+#include <e-util/e-mktemp.h>
 
 #include "mail-session.h"
 #include "em-junk-filter.h"
@@ -48,12 +50,15 @@
 
 static pthread_mutex_t em_junk_sa_init_lock = PTHREAD_MUTEX_INITIALIZER;
 static pthread_mutex_t em_junk_sa_report_lock = PTHREAD_MUTEX_INITIALIZER;
+static pthread_mutex_t em_junk_sa_preferred_socket_path_lock = PTHREAD_MUTEX_INITIALIZER;
 
 static const char *em_junk_sa_get_name (void);
 static gboolean em_junk_sa_check_junk (CamelMimeMessage *msg);
 static void em_junk_sa_report_junk (CamelMimeMessage *msg);
 static void em_junk_sa_report_notjunk (CamelMimeMessage *msg);
 static void em_junk_sa_commit_reports (void);
+static void em_junk_sa_init (void);
+static void em_junk_sa_quit (void);
 
 static EMJunkPlugin spam_assassin_plugin = {
 	{
@@ -63,6 +68,8 @@ static EMJunkPlugin spam_assassin_plugin
 		em_junk_sa_report_junk,
 		em_junk_sa_report_notjunk,
 		em_junk_sa_commit_reports,
+		em_junk_sa_init,
+		em_junk_sa_quit,
 	},
 	NULL,
 	NULL
@@ -72,14 +79,17 @@ static gboolean em_junk_sa_tested = FALS
 static gboolean em_junk_sa_spamd_tested = FALSE;
 static gboolean em_junk_sa_use_spamc = FALSE;
 static gboolean em_junk_sa_available = FALSE;
-static int em_junk_sa_spamd_port = -1;
+static gboolean em_junk_sa_system_spamd_available = FALSE;
+static gboolean em_junk_sa_new_daemon_started = FALSE;
+static char *em_junk_sa_socket_path = NULL;
+static char *em_junk_sa_spamd_pidfile = NULL;
 static char *em_junk_sa_spamc_binary = NULL;
 static GConfClient *em_junk_sa_gconf = NULL;
 
 /* volatile so not cached between threads */
 static volatile gboolean em_junk_sa_local_only;
 static volatile gboolean em_junk_sa_use_daemon;
-static volatile int em_junk_sa_daemon_port;
+static char * em_junk_sa_preferred_socket_path;
 
 static const char *
 em_junk_sa_get_name (void)
@@ -162,6 +172,7 @@ pipe_to_sa_with_error (CamelMimeMessage 
 		close (fds[1]);
 	}
 	
+	d(printf ("wait for child %d termination\n", pid));
 	result = waitpid (pid, &status, 0);
 	
 	if (result == -1 && errno == EINTR) {
@@ -189,26 +200,41 @@ pipe_to_sa (CamelMimeMessage *msg, const
 	return pipe_to_sa_with_error (msg, in, argv, -1);
 }
 
+static char *
+em_junk_sa_get_socket_path ()
+{
+	if (em_junk_sa_preferred_socket_path)
+		return em_junk_sa_preferred_socket_path;
+	else
+		return em_junk_sa_socket_path;
+}
+
 static gboolean
-em_junk_sa_test_spamd_running (char *binary, int port)
+em_junk_sa_test_spamd_running (char *binary, gboolean system)
 {
-	char port_buf[12], *argv[5];
+	char *argv[5];
 	int i = 0;
-	
-	d(fprintf (stderr, "test if spamd is running (port %d) using %s\n", port, binary));
+	gboolean rv;
+
+	pthread_mutex_lock (&em_junk_sa_preferred_socket_path_lock);
+
+	d(fprintf (stderr, "test if spamd is running (system %d) or using socket path %s\n", system, em_junk_sa_get_socket_path ()));
 	
 	argv[i++] = binary;
 	argv[i++] = "-x";
 	
-	if (port > 0) {
-		sprintf (port_buf, "%d", port);
-		argv[i++] = "-p";
-		argv[i++] = port_buf;
+	if (!system) {
+		argv[i++] = "-U";
+		argv[i++] = em_junk_sa_get_socket_path ();
 	}
 	
 	argv[i] = NULL;
 	
-	return pipe_to_sa (NULL, "From test 127 0 0 1", argv) == 0;
+	rv = pipe_to_sa (NULL, "From test 127 0 0 1", argv) == 0;
+
+	pthread_mutex_unlock (&em_junk_sa_preferred_socket_path_lock);
+
+	return rv;
 }
 
 static void
@@ -231,37 +257,40 @@ em_junk_sa_test_spamassassin (void)
 #define MAX_SPAMD_PORTS 1
 
 static gboolean
-em_junk_sa_run_spamd (char *binary, int *port)
+em_junk_sa_run_spamd (char *binary)
 {
-	char *argv[6];
-	char port_buf[12];
-	int i, p = em_junk_sa_daemon_port;
+	char *argv[8];
+	int i;
+	gboolean rv = FALSE;
+
+	pthread_mutex_lock (&em_junk_sa_preferred_socket_path_lock);
 
 	d(fprintf (stderr, "looks like spamd is not running\n"));
 
 	i = 0;
 	argv[i++] = binary;
-	argv[i++] = "--port";
-	argv[i++] = port_buf;
+	argv[i++] = "--socketpath";
+	argv[i++] = em_junk_sa_get_socket_path ();
 		
 	if (em_junk_sa_local_only)
 		argv[i++] = "--local";
 		
 	argv[i++] = "--daemonize";
+	argv[i++] = "--pidfile";
+	argv[i++] = em_junk_sa_spamd_pidfile;
 	argv[i] = NULL;
-		
-	for (i = 0; i < MAX_SPAMD_PORTS; i++, p++) {
-		d(fprintf (stderr, "trying to run %s at port %d\n", binary, p));
+
+	d(fprintf (stderr, "trying to run %s with socket path %s\n", binary, em_junk_sa_get_socket_path ()));
 			
-		snprintf (port_buf, 11, "%d", p);
-		if (!pipe_to_sa (NULL, NULL, argv)) {
-			d(fprintf (stderr, "success at port %d\n", p));
-			*port = p;
-			return TRUE;
-		}
+	if (!pipe_to_sa (NULL, NULL, argv)) {
+		d(fprintf (stderr, "success\n"));
+
+		rv = TRUE;
 	}
 
-	return FALSE;
+	pthread_mutex_unlock (&em_junk_sa_preferred_socket_path_lock);
+
+	return rv;
 }
 
 static void
@@ -270,7 +299,6 @@ em_junk_sa_test_spamd (void)
 	char *argv[4];
 	int i, b;
 	gboolean try_system_spamd = TRUE;
-	gboolean new_daemon_started = FALSE;
 	char *spamc_binaries [3] = {"spamc", "/usr/sbin/spamc", NULL};
 	char *spamd_binaries [3] = {"spamd", "/usr/sbin/spamd", NULL};
 
@@ -309,53 +337,52 @@ em_junk_sa_test_spamd (void)
 	if (try_system_spamd) {
 		for (b = 0; spamc_binaries [b]; b ++) {
 			em_junk_sa_spamc_binary = spamc_binaries [b];
-			if (em_junk_sa_test_spamd_running (em_junk_sa_spamc_binary, -1)) {
+			if (em_junk_sa_test_spamd_running (em_junk_sa_spamc_binary, TRUE)) {
 				em_junk_sa_use_spamc = TRUE;
-				em_junk_sa_spamd_port = -1;
+				em_junk_sa_system_spamd_available = TRUE;
 				break;
 			}
 		}
 	}
 
 	/* if there's no system spamd running, try to use user one on evo spamd port */
-	if (!em_junk_sa_use_spamc) {
-		int port = em_junk_sa_daemon_port;
-
-		for (i = 0; i < MAX_SPAMD_PORTS; i ++, port ++) {
-			for (b = 0; spamc_binaries [b]; b ++) {
-				em_junk_sa_spamc_binary = spamc_binaries [b];
-				if (em_junk_sa_test_spamd_running (em_junk_sa_spamc_binary, port)) {
-					em_junk_sa_use_spamc = TRUE;
-					em_junk_sa_spamd_port = port;
-					break;
-				}
+	if (!em_junk_sa_use_spamc && em_junk_sa_preferred_socket_path) {
+		for (b = 0; spamc_binaries [b]; b ++) {
+			em_junk_sa_spamc_binary = spamc_binaries [b];
+			if (em_junk_sa_test_spamd_running (em_junk_sa_spamc_binary, FALSE)) {
+				em_junk_sa_use_spamc = TRUE;
+				em_junk_sa_system_spamd_available = FALSE;
+				break;
 			}
 		}
 	}
 
 	/* unsuccessful? try to run one ourselfs */
-	if (!em_junk_sa_use_spamc)
+	if (!em_junk_sa_use_spamc) {
+		em_junk_sa_socket_path = e_mktemp ("spamd-socket-path-XXXXXX");
+		em_junk_sa_spamd_pidfile = e_mktemp ("spamd-pid-file-XXXXXX");
 		for (b = 0; spamd_binaries [b]; b ++) {
-			em_junk_sa_use_spamc = em_junk_sa_run_spamd (spamd_binaries [b], &em_junk_sa_spamd_port);
+			em_junk_sa_use_spamc = em_junk_sa_run_spamd (spamd_binaries [b]);
 			if (em_junk_sa_use_spamc) {
-				new_daemon_started = TRUE;
+				em_junk_sa_new_daemon_started = TRUE;
 				break;
 			}
 		}
-	
+	}
+
 	/* new daemon started => let find spamc binary */
-	if (em_junk_sa_use_spamc && new_daemon_started) {
+	if (em_junk_sa_use_spamc && em_junk_sa_new_daemon_started) {
 		em_junk_sa_use_spamc = FALSE;
 		for (b = 0; spamc_binaries [b]; b ++) {
 			em_junk_sa_spamc_binary = spamc_binaries [b];
-			if (em_junk_sa_test_spamd_running (em_junk_sa_spamc_binary, em_junk_sa_spamd_port)) {
+			if (em_junk_sa_test_spamd_running (em_junk_sa_spamc_binary, FALSE)) {
 				em_junk_sa_use_spamc = TRUE;
 				break;
 			}
 		}
 	}
 
-	d(fprintf (stderr, "use spamd %d at port %d with %s\n", em_junk_sa_use_spamc, em_junk_sa_spamd_port, em_junk_sa_spamc_binary));
+	d(fprintf (stderr, "use spamd: %s\n", em_junk_sa_use_spamc ? "yes" : "no"));
 	
 	em_junk_sa_spamd_tested = TRUE;
 }
@@ -379,21 +406,24 @@ em_junk_sa_is_available (void)
 static gboolean
 em_junk_sa_check_junk (CamelMimeMessage *msg)
 {
-	char *argv[5], buf[12];
+	char *argv[5];
 	int i = 0;
-	
+	gboolean rv;
+
+
 	d(fprintf (stderr, "em_junk_sa_check_junk\n"));
 	
 	if (!em_junk_sa_is_available ())
 		return FALSE;
-	
+
+	pthread_mutex_lock (&em_junk_sa_preferred_socket_path_lock);
+
 	if (em_junk_sa_use_spamc && em_junk_sa_use_daemon) {
 		argv[i++] = em_junk_sa_spamc_binary;
 		argv[i++] = "-c";
-		if (em_junk_sa_spamd_port != -1) {
-			sprintf (buf, "%d", em_junk_sa_spamd_port);
-			argv[i++] = "-p";
-			argv[i++] = buf;
+		if (!em_junk_sa_system_spamd_available) {
+			argv[i++] = "-U";
+			argv[i++] = em_junk_sa_get_socket_path ();
 		}
 	} else {
 		argv [i++] = "spamassassin";
@@ -404,7 +434,11 @@ em_junk_sa_check_junk (CamelMimeMessage 
 	
 	argv[i] = NULL;
 
-	return pipe_to_sa_with_error (msg, NULL, argv, 0) != 0;
+	rv = pipe_to_sa_with_error (msg, NULL, argv, 0) != 0;
+
+	pthread_mutex_unlock (&em_junk_sa_preferred_socket_path_lock);
+
+	return rv;
 }
 
 static void
@@ -495,25 +529,70 @@ em_junk_sa_setting_notify(GConfClient *g
 		em_junk_sa_local_only = gconf_value_get_bool(value);
 	else if (!strcmp(tkey, "use_daemon"))
 		em_junk_sa_use_daemon = gconf_value_get_bool(value);
-	else if (!strcmp(tkey, "daemon_port"))
-		em_junk_sa_daemon_port = gconf_value_get_int(value);
+	else if (!strcmp(tkey, "socket_path")) {
+		pthread_mutex_lock (&em_junk_sa_preferred_socket_path_lock);
+		g_free (em_junk_sa_preferred_socket_path);
+		em_junk_sa_preferred_socket_path = g_strdup (gconf_value_get_string(value));
+		pthread_mutex_unlock (&em_junk_sa_preferred_socket_path_lock);
+	}
 }
 
 const EMJunkPlugin *
 em_junk_filter_get_plugin (void)
 {
+	return &spam_assassin_plugin;
+}
+
+static void
+em_junk_sa_init (void)
+{
 	if (!em_junk_sa_gconf) {
 		em_junk_sa_gconf = gconf_client_get_default();
 		gconf_client_add_dir (em_junk_sa_gconf, "/apps/evolution/mail/junk/sa", GCONF_CLIENT_PRELOAD_ONELEVEL, NULL);
 
 		em_junk_sa_local_only = gconf_client_get_bool (em_junk_sa_gconf, "/apps/evolution/mail/junk/sa/local_only", NULL);
 		em_junk_sa_use_daemon = gconf_client_get_bool (em_junk_sa_gconf, "/apps/evolution/mail/junk/sa/use_daemon", NULL);
-		em_junk_sa_daemon_port = gconf_client_get_int (em_junk_sa_gconf, "/apps/evolution/mail/junk/sa/daemon_port", NULL);
+
+		pthread_mutex_lock (&em_junk_sa_preferred_socket_path_lock);
+		g_free (em_junk_sa_preferred_socket_path);
+		em_junk_sa_preferred_socket_path = g_strdup (gconf_client_get_string (em_junk_sa_gconf, "/apps/evolution/mail/junk/sa/socket_path", NULL));
+		pthread_mutex_unlock (&em_junk_sa_preferred_socket_path_lock);
 
 		gconf_client_notify_add(em_junk_sa_gconf, "/apps/evolution/mail/junk/sa",
 					(GConfClientNotifyFunc)em_junk_sa_setting_notify,
 					NULL, NULL, NULL);
 	}
+}
 
-	return &spam_assassin_plugin;
+static void
+em_junk_sa_quit (void)
+{
+	g_free (em_junk_sa_preferred_socket_path);
+	em_junk_sa_preferred_socket_path = NULL;
+
+	d(fprintf (stderr, "em_junk_sa_quit\n"));
+
+	if (em_junk_sa_new_daemon_started) {
+		int fd = open (em_junk_sa_spamd_pidfile, O_RDONLY);
+
+		if (fd != -1) {
+			char pid_str [16];
+			int bytes;
+
+			bytes = read (fd, pid_str, 15);
+			if (bytes > 0) {
+				int pid;
+
+				pid_str [bytes] = 0;
+				pid = atoi (pid_str);
+
+				if (pid > 0) {
+					kill (pid, SIGTERM);
+					d(fprintf (stderr, "em_junk_sa_quit send SIGTERM to daemon with pid %d\n", pid));
+				}
+			}
+
+			close (fd);
+		}
+	}
 }
Index: mail/mail-component.c
===================================================================
RCS file: /cvs/gnome/evolution/mail/mail-component.c,v
retrieving revision 1.98
diff -u -p -r1.98 mail-component.c
--- mail/mail-component.c	24 Sep 2004 04:23:29 -0000	1.98
+++ mail/mail-component.c	12 Oct 2004 14:47:14 -0000
@@ -658,6 +658,8 @@ impl_quit(PortableServer_Servant servant
 		if (mc->priv->quit_expunge)
 			gconf_client_set_int(gconf, "/apps/evolution/mail/trash/empty_date", now, NULL);
 
+		mail_session_quit ();
+
 		mc->priv->quit_state = MC_QUIT_SYNC;
 	}
 		/* Falls through */
Index: mail/mail-session.c
===================================================================
RCS file: /cvs/gnome/evolution/mail/mail-session.c,v
retrieving revision 1.98
diff -u -p -r1.98 mail-session.c
--- mail/mail-session.c	20 Sep 2004 05:59:55 -0000	1.98
+++ mail/mail-session.c	12 Oct 2004 14:47:14 -0000
@@ -641,11 +641,21 @@ mail_session_init (const char *base_dire
 								(GConfClientNotifyFunc) mail_session_check_junk_notify,
 								session, NULL, NULL);
 	session->junk_plugin = CAMEL_JUNK_PLUGIN (em_junk_filter_get_plugin ());
+	if (session->junk_plugin)
+		camel_junk_plugin_init (session->junk_plugin);
 
 	/* The shell will tell us to go online. */
 	camel_session_set_online ((CamelSession *) session, FALSE);
 	
 	g_free (camel_dir);
+}
+
+void
+mail_session_quit (const char *base_directory)
+{
+	if (session->junk_plugin)
+		camel_junk_plugin_quit (session->junk_plugin);
+	session->junk_plugin = NULL;
 }
 
 gboolean
Index: mail/mail-session.h
===================================================================
RCS file: /cvs/gnome/evolution/mail/mail-session.h,v
retrieving revision 1.17
diff -u -p -r1.17 mail-session.h
--- mail/mail-session.h	3 Feb 2004 13:11:41 -0000	1.17
+++ mail/mail-session.h	12 Oct 2004 14:47:14 -0000
@@ -33,6 +33,7 @@ extern "C" {
 #endif /* __cplusplus */
 
 void mail_session_init (const char *base_directory);
+void mail_session_quit ();
 gboolean mail_session_get_interactive (void);
 void mail_session_set_interactive (gboolean interactive);
 char *mail_session_request_dialog (const char *prompt, gboolean secret,


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]