changeset 489:e0922d603706

Install SIGTERM handler
author mmj
date Tue, 03 May 2005 04:56:38 +1000
parents 1a2f00c5b2e9
children 6250c7961451
files ChangeLog VERSION src/mlmmj-send.c
diffstat 3 files changed, 23 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Tue May 03 04:20:15 2005 +1000
+++ b/ChangeLog	Tue May 03 04:56:38 2005 +1000
@@ -1,3 +1,4 @@
+ o Install SIGTERM handler in mlmmj-send to allow it to shut down gracefully
  o Also remove mails when it's a bounce from a person not subbed
  o Introduce read() wrapper (Stevens)
  o Bouncing confirmation mails of sub or unsub should be cleaned from
--- a/VERSION	Tue May 03 04:20:15 2005 +1000
+++ b/VERSION	Tue May 03 04:56:38 2005 +1000
@@ -1,1 +1,1 @@
-1.2.5
+1.2.6pre2
--- a/src/mlmmj-send.c	Tue May 03 04:20:15 2005 +1000
+++ b/src/mlmmj-send.c	Tue May 03 04:56:38 2005 +1000
@@ -41,6 +41,7 @@
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
+#include <signal.h>
 
 #include "mlmmj.h"
 #include "mlmmj-send.h"
@@ -64,6 +65,14 @@
 static int addtohdr = 0;
 static int prepmailinmem = 0;
 static int maxverprecips = MAXVERPRECIPS;
+static int gotsigterm = 0;
+
+void catch_sig_term(int sig)
+{
+	/* We should only get SIGTERM here, but better safe than sorry */
+	if(sig == SIGTERM)
+		gotsigterm = 1;
+}
 
 char *bounce_from_adr(const char *recipient, const char *listadr,
 		      const char *mailfilename)
@@ -374,6 +383,11 @@
 		return MLMMJ_FROM;
 	}
 	for(i = 0; i < addrs->count; i++) {
+		if(gotsigterm) {
+			log_error(LOG_ARGS, "TERM signal recieved, "
+					"shutting down.");
+			return -1;
+		}
 		if(strchr(addrs->strs[i], '@') == NULL) {
 			errno = 0;
 			log_error(LOG_ARGS, "No @ in address, ignoring %s",
@@ -573,8 +587,9 @@
 				  hdrs, hdrslen, body, bodylen);
 			myfree(bounceaddr);
 		}
-		if(res && listaddr && archivefilename) {
-			/* we failed, so save the addresses and bail */
+		if((res || gotsigterm) && listaddr && archivefilename) {
+			/* we failed or got a SIGTERM, so save the addresses
+			 * and bail */
 			index = mybasename(archivefilename);
 			return requeuemail(listdir, index, addrs, i);
 		}
@@ -640,6 +655,10 @@
 	mlmmjbounce = concatstr(2, bindir, "/mlmmj-bounce");
 	myfree(bindir);
 	
+	/* install signal handler for SIGTERM */
+	if(signal(SIGTERM, catch_sig_term) == SIG_ERR)
+		log_error(LOG_ARGS, "Could not install SIGTERM handler!");
+
 	while ((opt = getopt(argc, argv, "aVDhm:l:L:R:F:T:r:s:")) != -1){
 		switch(opt) {
 		case 'a':