[Asterisk-Users] AGI Rocks!! (A happy camper)

Steven Critchfield critch at basesys.com
Tue Nov 25 14:42:40 MST 2003


On Tue, 2003-11-25 at 14:02, costas wrote:
> I was just looking at AGI with PHP myself. I just have a real dumb question. How does Linux know to send $stdout(or echo) to *? What if there are other apps open as well waiting for input. WOn't they get the output?
> 
> Also, how does the AGI know to read from $stdin is * input?

I understand that sometimes C can be daunting to a non programmer, but
this is a fairly easy look at the source question. I'll help you out on
this one.

In /usr/src/asterisk/apps/app_agi.c you will find a function
load_module. In fact, you will find this with all builtin applications.
Near the end of this function you will see ...
    return ast_register_application(app, agi_exec, synopsis, descrip);
The action here should be fairly easy to understand. It registers an
application with each of those arguments. Of interest is the agi_exec
function as it is what asterisk will call when launching the AGI
application. 

At this point it may seem odd that all that agi_exec does is call
agi_exec_full, but it is due to the eagi application being intertwined
into agi. In agi_exec_full, you scan through it, you will find
launch_script. This is where your application gets launched and
connected to asterisk.

In launch_script you will find a section like this.
    pid = fork();
    if (pid < 0) {
        ast_log(LOG_WARNING, "Failed to fork(): %s\n", strerror(errno));
        return -1;
    }
    if (!pid) {
        /* Redirect stdin and out, provide enhanced audio channel if desired */
        dup2(fromast[0], STDIN_FILENO);
        dup2(toast[1], STDOUT_FILENO);


So this is how it reconnects the stdin and stdout to asterisk.

> ---------- Original Message ----------------------------------
> From: WipeOut <wipe_out at users.sourceforge.net>
> Reply-To: asterisk-users at lists.digium.com
> Date:  Tue, 25 Nov 2003 19:14:48 +0000
> 
> >A note to all those who are avoiding writing up an AGI becasue it looks 
> >two complicated..
> >
> >I have just written up my first and its awesome.. It makes Asterisk open 
> >to all sorts of possibilities.. let your imagination run wild..
> >
> >I put off writing an AGI script because a) I could not find any docs b) 
> >it looked like the only way to do it was perl and I know nothing about 
> >perl and c) I am not a coder, more a simple sysadmin..
> >
> >Eventually I decided to give it a try, and seeing as the language I know 
> >the best is PHP I decided to do my script in PHP.. The truth of the 
> >matter is that once you have worked out how to get your variables from 
> >Asterisk into a usable array or whatever you are happy using the rest is 
> >a piece of cake..
> >
> >It goes without saying that what I have done is stupidly simple but for 
> >a non coder its still a giant achivement..
> >
> >So all I can say is thanks to the Mark and the team for dreaming up 
> >AGI... To those who are putting off giving it a go, don't!!.. Dive in 
> >and give it a try..
> >
> >PS. I won't be much help to anyone on AGI related questions, the list is 
> >still you best bet, this was just to hopefully inspire others to give it 
> >a try..
> >
> >_______________________________________________
> >Asterisk-Users mailing list
> >Asterisk-Users at lists.digium.com
> >http://lists.digium.com/mailman/listinfo/asterisk-users
> >
> 
> --
> Costas Menico
> Meezon Software Corp
> 201-224-8111
> costas at meezon.com
> 
> --
> _______________________________________________
> Asterisk-Users mailing list
> Asterisk-Users at lists.digium.com
> http://lists.digium.com/mailman/listinfo/asterisk-users
-- 
Steven Critchfield  <critch at basesys.com>




More information about the asterisk-users mailing list