Index: asterisk-1.2/res/res_agi.c
===================================================================
--- asterisk-1.2/res/res_agi.c	(revision 10177)
+++ asterisk-1.2/res/res_agi.c	(working copy)
@@ -78,12 +78,15 @@
 
 static char *deadapp = "DeadAGI";
 
+static char *niceapp = "NiceAGI";
+
 static char *synopsis = "Executes an AGI compliant application";
 static char *esynopsis = "Executes an EAGI compliant application";
 static char *deadsynopsis = "Executes AGI on a hungup channel";
+static char *nicesynopsis = "Executes an AGI compliant application, does not SIGHUP process on hangup";
 
 static char *descrip =
-"  [E|Dead]AGI(command|args): Executes an Asterisk Gateway Interface compliant\n"
+"  [E|Dead|Nice]AGI(command|args): Executes an Asterisk Gateway Interface compliant\n"
 "program on a channel. AGI allows Asterisk to launch external programs\n"
 "written in any language to control a telephony channel, play audio,\n"
 "read DTMF digits, etc. by communicating with the AGI protocol on stdin\n"
@@ -92,6 +95,8 @@
 " hangup, or 0 on non-hangup exit. \n"
 "Using 'EAGI' provides enhanced AGI, with incoming audio available out of band"
 "on file descriptor 3\n\n"
+"Using 'NiceAGI' will allow your script to gracefully detect a channel hangup"
+" if it does not support ignoring SIGHUP\n\n"
 "Use the CLI command 'show agi' to list available agi commands\n";
 
 static int agidebug = 0;
@@ -1842,7 +1847,7 @@
 	return 0;
 }
 #define RETRY	3
-static int run_agi(struct ast_channel *chan, char *request, AGI *agi, int pid, int dead)
+static int run_agi(struct ast_channel *chan, char *request, AGI *agi, int pid, int dead, int nice)
 {
 	struct ast_channel *c;
 	int outfd;
@@ -1914,10 +1919,11 @@
 		}
 	}
 	/* Notify process */
-	if (pid > -1) {
-		if (kill(pid, SIGHUP))
-			ast_log(LOG_WARNING, "unable to send SIGHUP to AGI process %d: %s\n", pid, strerror(errno));
-	}
+	if (nice==0)
+		if (pid > -1) {
+			if (kill(pid, SIGHUP))
+				ast_log(LOG_WARNING, "unable to send SIGHUP to AGI process %d: %s\n", pid, strerror(errno));
+		}
 	fclose(readf);
 	return returnstatus;
 }
@@ -2001,7 +2007,7 @@
 	return RESULT_SUCCESS;
 }
 
-static int agi_exec_full(struct ast_channel *chan, void *data, int enhanced, int dead)
+static int agi_exec_full(struct ast_channel *chan, void *data, int enhanced, int dead, int nice)
 {
 	int res=0;
 	struct localuser *u;
@@ -2041,7 +2047,7 @@
 		agi.fd = fds[1];
 		agi.ctrl = fds[0];
 		agi.audio = efd;
-		res = run_agi(chan, argv[0], &agi, pid, dead);
+		res = run_agi(chan, argv[0], &agi, pid, dead, nice);
 		close(fds[1]);
 		if (efd > -1)
 			close(efd);
@@ -2054,7 +2060,7 @@
 {
 	if (chan->_softhangup)
 		ast_log(LOG_WARNING, "If you want to run AGI on hungup channels you should use DeadAGI!\n");
-	return agi_exec_full(chan, data, 0, 0);
+	return agi_exec_full(chan, data, 0, 0, 0);
 }
 
 static int eagi_exec(struct ast_channel *chan, void *data)
@@ -2069,7 +2075,7 @@
 		ast_log(LOG_WARNING, "Unable to set channel '%s' to linear mode\n", chan->name);
 		return -1;
 	}
-	res = agi_exec_full(chan, data, 1, 0);
+	res = agi_exec_full(chan, data, 1, 0, 0);
 	if (!res) {
 		if (ast_set_read_format(chan, readformat)) {
 			ast_log(LOG_WARNING, "Unable to restore channel '%s' to format %s\n", chan->name, ast_getformatname(readformat));
@@ -2080,9 +2086,14 @@
 
 static int deadagi_exec(struct ast_channel *chan, void *data)
 {
-	return agi_exec_full(chan, data, 0, 1);
+	return agi_exec_full(chan, data, 0, 1, 0);
 }
 
+static int niceagi_exec(struct ast_channel *chan, void *data)
+{
+	return agi_exec_full(chan, data, 0, 0, 1);
+}
+
 static char showagi_help[] =
 "Usage: show agi [topic]\n"
 "       When called with a topic as an argument, displays usage\n"
@@ -2108,6 +2119,7 @@
 	ast_cli_unregister(&cli_debug);
 	ast_cli_unregister(&cli_no_debug);
 	ast_unregister_application(eapp);
+	ast_unregister_application(niceapp);
 	ast_unregister_application(deadapp);
 	return ast_unregister_application(app);
 }
@@ -2119,6 +2131,7 @@
 	ast_cli_register(&cli_debug);
 	ast_cli_register(&cli_no_debug);
 	ast_register_application(deadapp, deadagi_exec, deadsynopsis, descrip);
+	ast_register_application(niceapp, niceagi_exec, nicesynopsis, descrip);
 	ast_register_application(eapp, eagi_exec, esynopsis, descrip);
 	return ast_register_application(app, agi_exec, synopsis, descrip);
 }
