From 893ed27f5b4f2f8770d0570f6bb5f7b9ee434de1 Mon Sep 17 00:00:00 2001
From: Mark Spencer <markster@digium.com>
Date: Sun, 21 Mar 2004 08:01:53 +0000
Subject: [PATCH] Create temporary file and then sendmail it, then delete it,
 to avoid a hanging mailcmd from hanging asterisk (bug #1262)

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@2511 65c4cc65-6c06-0410-ace0-fbb531ad65f3
---
 apps/app_voicemail.c | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)

diff --git a/apps/app_voicemail.c b/apps/app_voicemail.c
index ca43f82900..5a4196b7d7 100755
--- a/apps/app_voicemail.c
+++ b/apps/app_voicemail.c
@@ -700,13 +700,16 @@ static int base_encode(char *filename, FILE *so)
 
 static int sendmail(char *srcemail, struct ast_vm_user *vmu, int msgnum, char *mailbox, char *callerid, char *attach, char *format, long duration, int attach_user_voicemail)
 {
-	FILE *p;
+	FILE *p=NULL;
+	int pfd;
 	char date[256];
 	char host[256];
 	char who[256];
 	char bound[256];
 	char fname[256];
 	char dur[256];
+	char tmp[80] = "/tmp/astmail-XXXXXX";
+	char tmp2[256];
 	time_t t;
 	struct tm tm;
 	struct vm_zone *the_zone = NULL;
@@ -714,7 +717,16 @@ static int sendmail(char *srcemail, struct ast_vm_user *vmu, int msgnum, char *m
 	if (!strcmp(format, "wav49"))
 		format = "WAV";
 	ast_log(LOG_DEBUG, "Attaching file '%s', format '%s', uservm is '%d', global is %d\n", attach, format, attach_user_voicemail, attach_voicemail);
-	p = popen(mailcmd, "w");
+	/* Make a temporary file instead of piping directly to sendmail, in case the mail
+	   command hangs */
+	pfd = mkstemp(tmp);
+	if (pfd > -1) {
+		p = fdopen(pfd, "w");
+		if (!p) {
+			close(pfd);
+			pfd = -1;
+		}
+	}
 	if (p) {
 		gethostname(host, sizeof(host));
 		if (strchr(srcemail, '@'))
@@ -811,7 +823,9 @@ static int sendmail(char *srcemail, struct ast_vm_user *vmu, int msgnum, char *m
 			base_encode(fname, p);
 			fprintf(p, "\n\n--%s--\n.\n", bound);
 		}
-		pclose(p);
+		fclose(p);
+		snprintf(tmp2, sizeof(tmp2), "( %s < %s ; rm -f %s ) &", mailcmd, tmp, tmp);
+		system(tmp2);
 		ast_log(LOG_DEBUG, "Sent mail to %s with command '%s'\n", who, mailcmd);
 	} else {
 		ast_log(LOG_WARNING, "Unable to launch '%s'\n", mailcmd);
-- 
GitLab