[asterisk-commits] tilghman: trunk r77787 - in /trunk: include/asterisk/agi.h res/res_agi.c

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Mon Jul 30 14:11:28 CDT 2007


Author: tilghman
Date: Mon Jul 30 14:11:28 2007
New Revision: 77787

URL: http://svn.digium.com/view/asterisk?view=rev&rev=77787
Log:
Cleanup of res_agi, ensuring thread safety (closes issue #10288)

Modified:
    trunk/include/asterisk/agi.h
    trunk/res/res_agi.c

Modified: trunk/include/asterisk/agi.h
URL: http://svn.digium.com/view/asterisk/trunk/include/asterisk/agi.h?view=diff&rev=77787&r1=77786&r2=77787
==============================================================================
--- trunk/include/asterisk/agi.h (original)
+++ trunk/include/asterisk/agi.h Mon Jul 30 14:11:28 2007
@@ -30,7 +30,7 @@
 typedef struct agi_state {
 	int fd;		/* FD for general output */
 	int audio;	/* FD for audio output */
-	int ctrl;	/* FD for input control */
+	int ctrl;		/* FD for input control */
 } AGI;
 
 typedef struct agi_command {
@@ -45,12 +45,17 @@
 	char *usage;
 	/* Does this application run dead */
 	int dead;
+	/* Pointer to module that registered the agi command */
+	struct ast_module *mod;
 	/* Linked list pointer */
 	AST_LIST_ENTRY(agi_command) list;
 } agi_command;
 
-int ast_agi_register(agi_command *cmd);
-int ast_agi_unregister(agi_command *cmd);
+void ast_agi_fdprintf(int fd, char *fmt, ...);
+int ast_agi_register(struct ast_module *mod, agi_command *cmd);
+int ast_agi_unregister(struct ast_module *mod, agi_command *cmd);
+void ast_agi_register_multiple(struct ast_module *mod, agi_command *cmd, int len);
+void ast_agi_unregister_multiple(struct ast_module *mod, agi_command *cmd, int len);
 
 #if defined(__cplusplus) || defined(c_plusplus)
 }

Modified: trunk/res/res_agi.c
URL: http://svn.digium.com/view/asterisk/trunk/res/res_agi.c?view=diff&rev=77787&r1=77786&r2=77787
==============================================================================
--- trunk/res/res_agi.c (original)
+++ trunk/res/res_agi.c Mon Jul 30 14:11:28 2007
@@ -67,10 +67,6 @@
 #include "asterisk/agi.h"
 
 #define MAX_ARGS 128
-#define MAX_COMMANDS 128
-
-/* Recycle some stuff from the CLI interface */
-#define fdprintf agi_debug_cli
 
 static char *app = "AGI";
 
@@ -119,7 +115,7 @@
 
 static agi_command *find_command(char *cmds[], int exact);
 
-static void agi_debug_cli(int fd, char *fmt, ...)
+void ast_agi_fdprintf(int fd, char *fmt, ...)
 {
 	char *stuff;
 	int res = 0;
@@ -221,7 +217,7 @@
 	/* If we have a script parameter, relay it to the fastagi server */
 	/* Script parameters take the form of: AGI(agi://my.example.com/?extension=${EXTEN}) */
 	if (!ast_strlen_zero(script))
-		fdprintf(s, "agi_network_script: %s\n", script);
+		ast_agi_fdprintf(s, "agi_network_script: %s\n", script);
 
 	ast_debug(4, "Wow, connected!\n");
 	fds[0] = s;
@@ -368,38 +364,38 @@
 
 	/* Print initial environment, with agi_request always being the first
 	   thing */
-	fdprintf(fd, "agi_request: %s\n", request);
-	fdprintf(fd, "agi_channel: %s\n", chan->name);
-	fdprintf(fd, "agi_language: %s\n", chan->language);
-	fdprintf(fd, "agi_type: %s\n", chan->tech->type);
-	fdprintf(fd, "agi_uniqueid: %s\n", chan->uniqueid);
+	ast_agi_fdprintf(fd, "agi_request: %s\n", request);
+	ast_agi_fdprintf(fd, "agi_channel: %s\n", chan->name);
+	ast_agi_fdprintf(fd, "agi_language: %s\n", chan->language);
+	ast_agi_fdprintf(fd, "agi_type: %s\n", chan->tech->type);
+	ast_agi_fdprintf(fd, "agi_uniqueid: %s\n", chan->uniqueid);
 
 	/* ANI/DNIS */
-	fdprintf(fd, "agi_callerid: %s\n", S_OR(chan->cid.cid_num, "unknown"));
-	fdprintf(fd, "agi_calleridname: %s\n", S_OR(chan->cid.cid_name, "unknown"));
-	fdprintf(fd, "agi_callingpres: %d\n", chan->cid.cid_pres);
-	fdprintf(fd, "agi_callingani2: %d\n", chan->cid.cid_ani2);
-	fdprintf(fd, "agi_callington: %d\n", chan->cid.cid_ton);
-	fdprintf(fd, "agi_callingtns: %d\n", chan->cid.cid_tns);
-	fdprintf(fd, "agi_dnid: %s\n", S_OR(chan->cid.cid_dnid, "unknown"));
-	fdprintf(fd, "agi_rdnis: %s\n", S_OR(chan->cid.cid_rdnis, "unknown"));
+	ast_agi_fdprintf(fd, "agi_callerid: %s\n", S_OR(chan->cid.cid_num, "unknown"));
+	ast_agi_fdprintf(fd, "agi_calleridname: %s\n", S_OR(chan->cid.cid_name, "unknown"));
+	ast_agi_fdprintf(fd, "agi_callingpres: %d\n", chan->cid.cid_pres);
+	ast_agi_fdprintf(fd, "agi_callingani2: %d\n", chan->cid.cid_ani2);
+	ast_agi_fdprintf(fd, "agi_callington: %d\n", chan->cid.cid_ton);
+	ast_agi_fdprintf(fd, "agi_callingtns: %d\n", chan->cid.cid_tns);
+	ast_agi_fdprintf(fd, "agi_dnid: %s\n", S_OR(chan->cid.cid_dnid, "unknown"));
+	ast_agi_fdprintf(fd, "agi_rdnis: %s\n", S_OR(chan->cid.cid_rdnis, "unknown"));
 
 	/* Context information */
-	fdprintf(fd, "agi_context: %s\n", chan->context);
-	fdprintf(fd, "agi_extension: %s\n", chan->exten);
-	fdprintf(fd, "agi_priority: %d\n", chan->priority);
-	fdprintf(fd, "agi_enhanced: %s\n", enhanced ? "1.0" : "0.0");
+	ast_agi_fdprintf(fd, "agi_context: %s\n", chan->context);
+	ast_agi_fdprintf(fd, "agi_extension: %s\n", chan->exten);
+	ast_agi_fdprintf(fd, "agi_priority: %d\n", chan->priority);
+	ast_agi_fdprintf(fd, "agi_enhanced: %s\n", enhanced ? "1.0" : "0.0");
 
 	/* User information */
-	fdprintf(fd, "agi_accountcode: %s\n", chan->accountcode ? chan->accountcode : "");
+	ast_agi_fdprintf(fd, "agi_accountcode: %s\n", chan->accountcode ? chan->accountcode : "");
 
 	/* Send any parameters to the fastagi server that have been passed via the agi application */
 	/* Agi application paramaters take the form of: AGI(/path/to/example/script|${EXTEN}) */
 	for(count = 1; count < argc; count++)
-		fdprintf(fd, "agi_arg_%d: %s\n", count, argv[count]);
+		ast_agi_fdprintf(fd, "agi_arg_%d: %s\n", count, argv[count]);
 
 	/* End with empty return */
-	fdprintf(fd, "\n");
+	ast_agi_fdprintf(fd, "\n");
 }
 
 static int handle_answer(struct ast_channel *chan, AGI *agi, int argc, char *argv[])
@@ -410,7 +406,7 @@
 	if (chan->_state != AST_STATE_UP)
 		res = ast_answer(chan);
 
-	fdprintf(agi->fd, "200 result=%d\n", res);
+	ast_agi_fdprintf(agi->fd, "200 result=%d\n", res);
 	return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE;
 }
 
@@ -423,7 +419,7 @@
 	if (sscanf(argv[3], "%d", &to) != 1)
 		return RESULT_SHOWUSAGE;
 	res = ast_waitfordigit_full(chan, to, agi->audio, agi->ctrl);
-	fdprintf(agi->fd, "200 result=%d\n", res);
+	ast_agi_fdprintf(agi->fd, "200 result=%d\n", res);
 	return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE;
 }
 
@@ -442,7 +438,7 @@
 	   parsing, then here, add a newline at the end of the string
 	   before sending it to ast_sendtext --DUDE */
 	res = ast_sendtext(chan, argv[2]);
-	fdprintf(agi->fd, "200 result=%d\n", res);
+	ast_agi_fdprintf(agi->fd, "200 result=%d\n", res);
 	return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE;
 }
 
@@ -455,15 +451,15 @@
 
 	res = ast_recvchar(chan,atoi(argv[2]));
 	if (res == 0) {
-		fdprintf(agi->fd, "200 result=%d (timeout)\n", res);
+		ast_agi_fdprintf(agi->fd, "200 result=%d (timeout)\n", res);
 		return RESULT_SUCCESS;
 	}
 	if (res > 0) {
-		fdprintf(agi->fd, "200 result=%d\n", res);
+		ast_agi_fdprintf(agi->fd, "200 result=%d\n", res);
 		return RESULT_SUCCESS;
 	}
 	else {
-		fdprintf(agi->fd, "200 result=%d (hangup)\n", res);
+		ast_agi_fdprintf(agi->fd, "200 result=%d (hangup)\n", res);
 		return RESULT_FAILURE;
 	}
 }
@@ -477,10 +473,10 @@
 
 	buf = ast_recvtext(chan,atoi(argv[2]));
 	if (buf) {
-		fdprintf(agi->fd, "200 result=1 (%s)\n", buf);
+		ast_agi_fdprintf(agi->fd, "200 result=1 (%s)\n", buf);
 		ast_free(buf);
 	} else {	
-		fdprintf(agi->fd, "200 result=-1\n");
+		ast_agi_fdprintf(agi->fd, "200 result=-1\n");
 	}
 	return RESULT_SUCCESS;
 }
@@ -502,9 +498,9 @@
 		x = 1;
 	res = ast_channel_setoption(chan, AST_OPTION_TDD, &x, sizeof(char), 0);
 	if (res != RESULT_SUCCESS)
-		fdprintf(agi->fd, "200 result=0\n");
+		ast_agi_fdprintf(agi->fd, "200 result=0\n");
 	else
-		fdprintf(agi->fd, "200 result=1\n");
+		ast_agi_fdprintf(agi->fd, "200 result=1\n");
 	return RESULT_SUCCESS;
 }
 
@@ -518,7 +514,7 @@
 	res = ast_send_image(chan, argv[2]);
 	if (!ast_check_hangup(chan))
 		res = 0;
-	fdprintf(agi->fd, "200 result=%d\n", res);
+	ast_agi_fdprintf(agi->fd, "200 result=%d\n", res);
 	return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE;
 }
 
@@ -555,7 +551,7 @@
 	
 	res = ast_control_streamfile(chan, argv[3], fwd, rev, stop, pause, NULL, skipms, NULL);
 	
-	fdprintf(agi->fd, "200 result=%d\n", res);
+	ast_agi_fdprintf(agi->fd, "200 result=%d\n", res);
 
 	return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE;
 }
@@ -577,7 +573,7 @@
 		return RESULT_SHOWUSAGE;
 
 	if (!(fs = ast_openstream(chan, argv[2], chan->language))) {
-		fdprintf(agi->fd, "200 result=%d endpos=%ld\n", 0, sample_offset);
+		ast_agi_fdprintf(agi->fd, "200 result=%d endpos=%ld\n", 0, sample_offset);
 		return RESULT_SUCCESS;
 	}	
 	vfs = ast_openvstream(chan, argv[2], chan->language);
@@ -604,7 +600,7 @@
 		/* Stop this command, don't print a result line, as there is a new command */
 		return RESULT_SUCCESS;
 	}
-	fdprintf(agi->fd, "200 result=%d endpos=%ld\n", res, sample_offset);
+	ast_agi_fdprintf(agi->fd, "200 result=%d endpos=%ld\n", res, sample_offset);
 	return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE;
 }
 
@@ -631,7 +627,7 @@
 	}
 
 	if (!(fs = ast_openstream(chan, argv[2], chan->language))) {
-		fdprintf(agi->fd, "200 result=%d endpos=%ld\n", 0, sample_offset);
+		ast_agi_fdprintf(agi->fd, "200 result=%d endpos=%ld\n", 0, sample_offset);
 		ast_log(LOG_WARNING, "Unable to open %s\n", argv[2]);
 		return RESULT_SUCCESS;
 	}
@@ -668,7 +664,7 @@
 			res=0;
 	}
 
-	fdprintf(agi->fd, "200 result=%d endpos=%ld\n", res, sample_offset);
+	ast_agi_fdprintf(agi->fd, "200 result=%d endpos=%ld\n", res, sample_offset);
 	return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE;
 }
 
@@ -688,7 +684,7 @@
 	res = ast_say_number_full(chan, num, argv[3], chan->language, argc > 4 ? argv[4] : NULL, agi->audio, agi->ctrl);
 	if (res == 1)
 		return RESULT_SUCCESS;
-	fdprintf(agi->fd, "200 result=%d\n", res);
+	ast_agi_fdprintf(agi->fd, "200 result=%d\n", res);
 	return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE;
 }
 
@@ -704,7 +700,7 @@
 	res = ast_say_digit_str_full(chan, argv[2], argv[3], chan->language, agi->audio, agi->ctrl);
 	if (res == 1) /* New command */
 		return RESULT_SUCCESS;
-	fdprintf(agi->fd, "200 result=%d\n", res);
+	ast_agi_fdprintf(agi->fd, "200 result=%d\n", res);
 	return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE;
 }
 
@@ -718,7 +714,7 @@
 	res = ast_say_character_str_full(chan, argv[2], argv[3], chan->language, agi->audio, agi->ctrl);
 	if (res == 1) /* New command */
 		return RESULT_SUCCESS;
-	fdprintf(agi->fd, "200 result=%d\n", res);
+	ast_agi_fdprintf(agi->fd, "200 result=%d\n", res);
 	return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE;
 }
 
@@ -733,7 +729,7 @@
 	res = ast_say_date(chan, num, argv[3], chan->language);
 	if (res == 1)
 		return RESULT_SUCCESS;
-	fdprintf(agi->fd, "200 result=%d\n", res);
+	ast_agi_fdprintf(agi->fd, "200 result=%d\n", res);
 	return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE;
 }
 
@@ -748,7 +744,7 @@
 	res = ast_say_time(chan, num, argv[3], chan->language);
 	if (res == 1)
 		return RESULT_SUCCESS;
-	fdprintf(agi->fd, "200 result=%d\n", res);
+	ast_agi_fdprintf(agi->fd, "200 result=%d\n", res);
 	return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE;
 }
 
@@ -782,7 +778,7 @@
 	if (res == 1)
 		return RESULT_SUCCESS;
 
-	fdprintf(agi->fd, "200 result=%d\n", res);
+	ast_agi_fdprintf(agi->fd, "200 result=%d\n", res);
 	return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE;
 }
 
@@ -796,7 +792,7 @@
 	res = ast_say_phonetic_str_full(chan, argv[2], argv[3], chan->language, agi->audio, agi->ctrl);
 	if (res == 1) /* New command */
 		return RESULT_SUCCESS;
-	fdprintf(agi->fd, "200 result=%d\n", res);
+	ast_agi_fdprintf(agi->fd, "200 result=%d\n", res);
 	return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE;
 }
 
@@ -819,11 +815,11 @@
 	if (res == 2)			/* New command */
 		return RESULT_SUCCESS;
 	else if (res == 1)
-		fdprintf(agi->fd, "200 result=%s (timeout)\n", data);
+		ast_agi_fdprintf(agi->fd, "200 result=%s (timeout)\n", data);
 	else if (res < 0 )
-		fdprintf(agi->fd, "200 result=-1\n");
+		ast_agi_fdprintf(agi->fd, "200 result=-1\n");
 	else
-		fdprintf(agi->fd, "200 result=%s\n", data);
+		ast_agi_fdprintf(agi->fd, "200 result=%s\n", data);
 	return RESULT_SUCCESS;
 }
 
@@ -833,7 +829,7 @@
 	if (argc != 3)
 		return RESULT_SHOWUSAGE;
 	ast_copy_string(chan->context, argv[2], sizeof(chan->context));
-	fdprintf(agi->fd, "200 result=0\n");
+	ast_agi_fdprintf(agi->fd, "200 result=0\n");
 	return RESULT_SUCCESS;
 }
 	
@@ -842,7 +838,7 @@
 	if (argc != 3)
 		return RESULT_SHOWUSAGE;
 	ast_copy_string(chan->exten, argv[2], sizeof(chan->exten));
-	fdprintf(agi->fd, "200 result=0\n");
+	ast_agi_fdprintf(agi->fd, "200 result=0\n");
 	return RESULT_SUCCESS;
 }
 
@@ -859,7 +855,7 @@
 	}
 
 	ast_explicit_goto(chan, NULL, NULL, pri);
-	fdprintf(agi->fd, "200 result=0\n");
+	ast_agi_fdprintf(agi->fd, "200 result=0\n");
 	return RESULT_SUCCESS;
 }
 		
@@ -935,12 +931,12 @@
 	if (!res)
 		res = ast_waitstream(chan, argv[4]);
 	if (res) {
-		fdprintf(agi->fd, "200 result=%d (randomerror) endpos=%ld\n", res, sample_offset);
+		ast_agi_fdprintf(agi->fd, "200 result=%d (randomerror) endpos=%ld\n", res, sample_offset);
 	} else {
 		fs = ast_writefile(argv[2], argv[3], NULL, O_CREAT | O_WRONLY | (sample_offset ? O_APPEND : 0), 0, AST_FILE_MODE);
 		if (!fs) {
 			res = -1;
-			fdprintf(agi->fd, "200 result=%d (writefile)\n", res);
+			ast_agi_fdprintf(agi->fd, "200 result=%d (writefile)\n", res);
 			if (sildet)
 				ast_dsp_free(sildet);
 			return RESULT_FAILURE;
@@ -960,14 +956,14 @@
 			res = ast_waitfor(chan, -1);
 			if (res < 0) {
 				ast_closestream(fs);
-				fdprintf(agi->fd, "200 result=%d (waitfor) endpos=%ld\n", res,sample_offset);
+				ast_agi_fdprintf(agi->fd, "200 result=%d (waitfor) endpos=%ld\n", res,sample_offset);
 				if (sildet)
 					ast_dsp_free(sildet);
 				return RESULT_FAILURE;
 			}
 			f = ast_read(chan);
 			if (!f) {
-				fdprintf(agi->fd, "200 result=%d (hangup) endpos=%ld\n", 0, sample_offset);
+				ast_agi_fdprintf(agi->fd, "200 result=%d (hangup) endpos=%ld\n", 0, sample_offset);
 				ast_closestream(fs);
 				if (sildet)
 					ast_dsp_free(sildet);
@@ -982,7 +978,7 @@
 					ast_stream_rewind(fs, 200);
 					ast_truncstream(fs);
 					sample_offset = ast_tellstream(fs);
-					fdprintf(agi->fd, "200 result=%d (dtmf) endpos=%ld\n", f->subclass, sample_offset);
+					ast_agi_fdprintf(agi->fd, "200 result=%d (dtmf) endpos=%ld\n", f->subclass, sample_offset);
 					ast_closestream(fs);
 					ast_frfree(f);
 					if (sildet)
@@ -1022,12 +1018,12 @@
 				break;
 		}
 
-              	if (gotsilence) {
-                     	ast_stream_rewind(fs, silence-1000);
-                	ast_truncstream(fs);
-			sample_offset = ast_tellstream(fs);
+			if (gotsilence) {
+				ast_stream_rewind(fs, silence-1000);
+				ast_truncstream(fs);
+				sample_offset = ast_tellstream(fs);
 		}		
-		fdprintf(agi->fd, "200 result=%d (timeout) endpos=%ld\n", res, sample_offset);
+		ast_agi_fdprintf(agi->fd, "200 result=%d (timeout) endpos=%ld\n", res, sample_offset);
 		ast_closestream(fs);
 	}
 
@@ -1054,7 +1050,7 @@
 		chan->whentohangup = time(NULL) + timeout;
 	else
 		chan->whentohangup = 0;
-	fdprintf(agi->fd, "200 result=0\n");
+	ast_agi_fdprintf(agi->fd, "200 result=0\n");
 	return RESULT_SUCCESS;
 }
 
@@ -1065,7 +1061,7 @@
 	if (argc == 1) {
 		/* no argument: hangup the current channel */
 		ast_softhangup(chan,AST_SOFTHANGUP_EXPLICIT);
-		fdprintf(agi->fd, "200 result=1\n");
+		ast_agi_fdprintf(agi->fd, "200 result=1\n");
 		return RESULT_SUCCESS;
 	} else if (argc == 2) {
 		/* one argument: look for info on the specified channel */
@@ -1073,12 +1069,12 @@
 		if (c) {
 			/* we have a matching channel */
 			ast_softhangup(c,AST_SOFTHANGUP_EXPLICIT);
-			fdprintf(agi->fd, "200 result=1\n");
+			ast_agi_fdprintf(agi->fd, "200 result=1\n");
 			ast_channel_unlock(c);
 			return RESULT_SUCCESS;
 		}
 		/* if we get this far no channel name matched the argument given */
-		fdprintf(agi->fd, "200 result=-1\n");
+		ast_agi_fdprintf(agi->fd, "200 result=-1\n");
 		return RESULT_SUCCESS;
 	} else {
 		return RESULT_SHOWUSAGE;
@@ -1101,7 +1097,7 @@
 		ast_log(LOG_WARNING, "Could not find application (%s)\n", argv[1]);
 		res = -2;
 	}
-	fdprintf(agi->fd, "200 result=%d\n", res);
+	ast_agi_fdprintf(agi->fd, "200 result=%d\n", res);
 
 	/* Even though this is wrong, users are depending upon this result. */
 	return res;
@@ -1124,7 +1120,7 @@
 		ast_set_callerid(chan, l, n, NULL);
 	}
 
-	fdprintf(agi->fd, "200 result=1\n");
+	ast_agi_fdprintf(agi->fd, "200 result=1\n");
 	return RESULT_SUCCESS;
 }
 
@@ -1133,18 +1129,18 @@
 	struct ast_channel *c;
 	if (argc == 2) {
 		/* no argument: supply info on the current channel */
-		fdprintf(agi->fd, "200 result=%d\n", chan->_state);
+		ast_agi_fdprintf(agi->fd, "200 result=%d\n", chan->_state);
 		return RESULT_SUCCESS;
 	} else if (argc == 3) {
 		/* one argument: look for info on the specified channel */
 		c = ast_get_channel_by_name_locked(argv[2]);
 		if (c) {
-			fdprintf(agi->fd, "200 result=%d\n", c->_state);
+			ast_agi_fdprintf(agi->fd, "200 result=%d\n", c->_state);
 			ast_channel_unlock(c);
 			return RESULT_SUCCESS;
 		}
 		/* if we get this far no channel name matched the argument given */
-		fdprintf(agi->fd, "200 result=-1\n");
+		ast_agi_fdprintf(agi->fd, "200 result=-1\n");
 		return RESULT_SUCCESS;
 	} else {
 		return RESULT_SHOWUSAGE;
@@ -1156,7 +1152,7 @@
 	if (argv[3])
 		pbx_builtin_setvar_helper(chan, argv[2], argv[3]);
 
-	fdprintf(agi->fd, "200 result=1\n");
+	ast_agi_fdprintf(agi->fd, "200 result=1\n");
 	return RESULT_SUCCESS;
 }
 
@@ -1176,9 +1172,9 @@
 	}
 
 	if (ret)
-		fdprintf(agi->fd, "200 result=1 (%s)\n", ret);
+		ast_agi_fdprintf(agi->fd, "200 result=1 (%s)\n", ret);
 	else
-		fdprintf(agi->fd, "200 result=0\n");
+		ast_agi_fdprintf(agi->fd, "200 result=0\n");
 
 	return RESULT_SUCCESS;
 }
@@ -1197,9 +1193,9 @@
 	}
 	if (chan) { /* XXX isn't this chan2 ? */
 		pbx_substitute_variables_helper(chan2, argv[3], tmp, sizeof(tmp) - 1);
-		fdprintf(agi->fd, "200 result=1 (%s)\n", tmp);
+		ast_agi_fdprintf(agi->fd, "200 result=1 (%s)\n", tmp);
 	} else {
-		fdprintf(agi->fd, "200 result=0\n");
+		ast_agi_fdprintf(agi->fd, "200 result=0\n");
 	}
 	if (chan2 && (chan2 != chan))
 		ast_channel_unlock(chan2);
@@ -1236,7 +1232,7 @@
 	if (level <= option_verbose)
 		ast_verbose("%s %s: %s\n", prefix, chan->data, argv[1]);
 	
-	fdprintf(agi->fd, "200 result=1\n");
+	ast_agi_fdprintf(agi->fd, "200 result=1\n");
 	
 	return RESULT_SUCCESS;
 }
@@ -1250,9 +1246,9 @@
 		return RESULT_SHOWUSAGE;
 	res = ast_db_get(argv[2], argv[3], tmp, sizeof(tmp));
 	if (res) 
-		fdprintf(agi->fd, "200 result=0\n");
+		ast_agi_fdprintf(agi->fd, "200 result=0\n");
 	else
-		fdprintf(agi->fd, "200 result=1 (%s)\n", tmp);
+		ast_agi_fdprintf(agi->fd, "200 result=1 (%s)\n", tmp);
 
 	return RESULT_SUCCESS;
 }
@@ -1264,7 +1260,7 @@
 	if (argc != 5)
 		return RESULT_SHOWUSAGE;
 	res = ast_db_put(argv[2], argv[3], argv[4]);
-	fdprintf(agi->fd, "200 result=%c\n", res ? '0' : '1');
+	ast_agi_fdprintf(agi->fd, "200 result=%c\n", res ? '0' : '1');
 	return RESULT_SUCCESS;
 }
 
@@ -1275,7 +1271,7 @@
 	if (argc != 4)
 		return RESULT_SHOWUSAGE;
 	res = ast_db_del(argv[2], argv[3]);
-	fdprintf(agi->fd, "200 result=%c\n", res ? '0' : '1');
+	ast_agi_fdprintf(agi->fd, "200 result=%c\n", res ? '0' : '1');
 	return RESULT_SUCCESS;
 }
 
@@ -1290,7 +1286,7 @@
 	else
 		res = ast_db_deltree(argv[2], NULL);
 
-	fdprintf(agi->fd, "200 result=%c\n", res ? '0' : '1');
+	ast_agi_fdprintf(agi->fd, "200 result=%c\n", res ? '0' : '1');
 	return RESULT_SUCCESS;
 }
 
@@ -1322,7 +1318,7 @@
 
 static int handle_noop(struct ast_channel *chan, AGI *agi, int arg, char *argv[])
 {
-	fdprintf(agi->fd, "200 result=0\n");
+	ast_agi_fdprintf(agi->fd, "200 result=0\n");
 	return RESULT_SUCCESS;
 }
 
@@ -1332,7 +1328,7 @@
 		ast_moh_start(chan, argc > 3 ? argv[3] : NULL, NULL);
 	else if (!strncasecmp(argv[2], "off", 3))
 		ast_moh_stop(chan);
-	fdprintf(agi->fd, "200 result=0\n");
+	ast_agi_fdprintf(agi->fd, "200 result=0\n");
 	return RESULT_SUCCESS;
 }
 
@@ -1586,7 +1582,7 @@
 /*!
  * \brief AGI commands list
  */
-static struct agi_command commands[MAX_COMMANDS] = {
+static struct agi_command commands[] = {
 	{ { "answer", NULL }, handle_answer, "Answer channel", usage_answer , 0 },
 	{ { "channel", "status", NULL }, handle_channelstatus, "Returns status of the connected channel", usage_channelstatus , 0 },
 	{ { "database", "del", NULL }, handle_dbdel, "Removes database key/value", usage_dbdel , 1 },
@@ -1653,12 +1649,19 @@
 	return 0;
 }
 
-int ast_agi_register(agi_command *agi)
-{
-	if (!find_command(agi->cmda,1)) {
+int ast_agi_register(struct ast_module *mod, agi_command *cmd)
+{
+	char fullcmd[80];
+
+	ast_join(fullcmd, sizeof(fullcmd), cmd->cmda);
+
+ 	if (!find_command(cmd->cmda,1)) {
 		AST_RWLIST_WRLOCK(&agi_commands);
-		AST_LIST_INSERT_TAIL(&agi_commands, agi, list);
+		AST_LIST_INSERT_TAIL(&agi_commands, cmd, list);
 		AST_RWLIST_UNLOCK(&agi_commands);
+		if (mod != ast_module_info->self)
+			ast_module_ref(ast_module_info->self);
+		ast_verb(2, "AGI Command '%s' registered\n",fullcmd);
 		return 1;
 	} else {
 		ast_log(LOG_WARNING, "Command already registered!\n");
@@ -1666,24 +1669,48 @@
 	}
 }
 
-int ast_agi_unregister(agi_command *cmd)
+int ast_agi_unregister(struct ast_module *mod, agi_command *cmd)
 {
 	struct agi_command *e;
 	int unregistered = 0;
+	char fullcmd[80];
+	
+	ast_join(fullcmd, sizeof(fullcmd), cmd->cmda);
 
 	AST_RWLIST_WRLOCK(&agi_commands);
 	AST_RWLIST_TRAVERSE_SAFE_BEGIN(&agi_commands, e, list) {
 		if (cmd == e) {
 			AST_RWLIST_REMOVE_CURRENT(&agi_commands, list);
+			if (mod != ast_module_info->self)
+				ast_module_unref(ast_module_info->self);
 			unregistered=1;
 			break;
 		}
 	}
 	AST_RWLIST_TRAVERSE_SAFE_END
 	AST_RWLIST_UNLOCK(&agi_commands);
-	if (!unregistered)
-		ast_log(LOG_WARNING, "Unable to unregister command!\n");
+	if (unregistered)
+		ast_verb(2, "AGI Command '%s' unregistered\n",fullcmd);
+	else
+		ast_log(LOG_WARNING, "Unable to unregister command: '%s'!\n",fullcmd);
 	return unregistered;
+}
+
+void ast_agi_register_multiple(struct ast_module *mod, agi_command *cmd, int len)
+{
+	int i;
+
+	for (i = 0; i < len; i++)
+		ast_agi_register(mod, cmd + i);
+
+}
+
+void ast_agi_unregister_multiple(struct ast_module *mod, agi_command *cmd, int len)
+{
+	int i;
+
+	for (i = 0; i < len; i++)
+		ast_agi_unregister(mod, cmd + i);
 }
 
 static agi_command *find_command(char *cmds[], int exact)
@@ -1791,12 +1818,18 @@
 
 	parse_args(buf, &argc, argv);
 	if ((c = find_command(argv, 0)) && (!dead || (dead && c->dead))) {
+		/* if this command wasnt registered by res_agi, be sure to usecount
+		the module we are using */
+		if (c->mod != ast_module_info->self)
+			ast_module_ref(c->mod);
 		res = c->handler(chan, agi, argc, argv);
+		if (c->mod != ast_module_info->self)
+			ast_module_unref(c->mod);
 		switch(res) {
 		case RESULT_SHOWUSAGE:
-			fdprintf(agi->fd, "520-Invalid command syntax.  Proper usage follows:\n");
-			fdprintf(agi->fd, c->usage);
-			fdprintf(agi->fd, "520 End of proper usage.\n");
+			ast_agi_fdprintf(agi->fd, "520-Invalid command syntax.  Proper usage follows:\n");
+			ast_agi_fdprintf(agi->fd, c->usage);
+			ast_agi_fdprintf(agi->fd, "520 End of proper usage.\n");
 			break;
 		case AST_PBX_KEEPALIVE:
 			/* We've been asked to keep alive, so do so */
@@ -1808,9 +1841,9 @@
 			return -1;
 		}
 	} else if ((c = find_command(argv, 0))) {
-		fdprintf(agi->fd, "511 Command Not Permitted on a dead channel\n");
+		ast_agi_fdprintf(agi->fd, "511 Command Not Permitted on a dead channel\n");
 	} else {
-		fdprintf(agi->fd, "510 Invalid or unknown command\n");
+		ast_agi_fdprintf(agi->fd, "510 Invalid or unknown command\n");
 	}
 	return 0;
 }
@@ -2156,15 +2189,9 @@
 
 static int unload_module(void)
 {
-	struct agi_command *e;
 
 	ast_cli_unregister_multiple(cli_agi, sizeof(cli_agi) / sizeof(struct ast_cli_entry));
-	AST_RWLIST_WRLOCK(&agi_commands);
-	AST_RWLIST_TRAVERSE_SAFE_BEGIN(&agi_commands, e, list) {
-		AST_RWLIST_REMOVE_CURRENT(&agi_commands, list);
-	}
-	AST_RWLIST_TRAVERSE_SAFE_END
-	AST_RWLIST_UNLOCK(&agi_commands);
+	ast_agi_unregister_multiple(ast_module_info->self, commands, sizeof(commands) / sizeof(struct agi_command));
 	ast_unregister_application(eapp);
 	ast_unregister_application(deadapp);
 	return ast_unregister_application(app);
@@ -2172,12 +2199,8 @@
 
 static int load_module(void)
 {
-	int i;
-
 	ast_cli_register_multiple(cli_agi, sizeof(cli_agi) / sizeof(struct ast_cli_entry));
-	for (i=0; i < (sizeof(commands) / sizeof(struct agi_command)); i++) {
-		ast_agi_register(&commands[i]);
-	}
+	ast_agi_register_multiple(ast_module_info->self, commands, sizeof(commands) / sizeof(struct agi_command));
 	ast_register_application(deadapp, deadagi_exec, deadsynopsis, descrip);
 	ast_register_application(eapp, eagi_exec, esynopsis, descrip);
 	return ast_register_application(app, agi_exec, synopsis, descrip);




More information about the asterisk-commits mailing list