tosh has asked for the wisdom of the Perl Monks concerning the following question:

Hello folks, I have a script which makes a call to an external program:
#!/usr/bin/perl print "Content-Type: text/html\n\n"; `/usr/local/realproducer/realproducer ARGS...`; print "Done\n";
When I run it from the shell it all works fine. But when I run it from CGI it doesn't run at all. There are no CGI errors, so it's not that.

Possibly there's an environment variables issue, and so I printed them from the script when run from the shell:
'HOME' = '/home/me'; 'SSH_CLIENT' = 'IP ADDRESS 1027 22'; 'SSH_CONNECTION' = 'IP ADDRESS 1027 IP ADDRESS 22'; 'BLOCKSIZE' = 'K'; 'EDITOR' = 'vi'; 'MAIL' = '/var/mail/me'; 'PWD' = '/tmp'; 'USER' = 'me'; 'LOGNAME' = 'me'; 'SHLVL' = '1'; 'OLDPWD' = '/home/me'; 'ENV' = '/home/me/.shrc'; '_' = '/web/admin/media.pl'; 'PATH' = '/sbin:/bin:/usr/sbin:/usr/bin:/usr/games:/usr/local/sbin:/us +r/local/bi n:/usr/X11R6/bin:/home/me/bin'; 'SHELL' = '/usr/local/bin/bash'; 'TERM' = 'vt100'; 'PAGER' = 'more'; 'SSH_TTY' = '/dev/ttyp0';
But nothing looks like it would cause problems, except for maybe the 'TERM' variable.

Another twist in the problem is that the "realproducer" program I am trying to run is a LINUX binary which I am running on FreeBSD in LINUX binary compatibility mode, perhaps it can't find the /compat/linux libraries? If this is the case does anyone know how I would tell PERL to look there?

Is it possible to have the CGI script create a user shell environment that the program could run in? What would that look like?

Thanks for any insights or directions anyone has, this is making me crazy...

Tosh

UPDATE: I've looked at all the easy things like permissions and if anything is actually running. It returns an ERROR 11 which seems to be "Resource deadlock avoided" whatever that means within the context. I've decided to cop-out and just have the progran run from CRON, I hope that will work... :(

Replies are listed 'Best First'.
Re: CGI script creating a user shell environment
by ambrus (Abbot) on Feb 21, 2004 at 20:40 UTC

    Some random thoughts.

    1. Does the cgi run in tainted mode? If so, have you untainted $ENV{PATH} and the command's arguments?
    2. Are you sure the program does not run? Maybe you just don't get its output.
    3. If you put an error in the cgi, does it get reported to you? Don't you just get an error but dont see it from the browser?
Re: CGI script creating a user shell environment
by Jaap (Curate) on Feb 21, 2004 at 20:54 UTC
    Does the user that executes the script in CGI mode have rx permission on the realproducer thing? Try also to print $! and $@ and such after the command. Try also to system() it and see what system returns.
Re: CGI script creating a user shell environment
by iburrell (Chaplain) on Feb 22, 2004 at 20:49 UTC
    First, you aren't checking the response code of the program. You don't know if it failed to run. Also, there should be some error ouptut if it fails. Where is that going? Things to check are different permissions and environment variables between the apache and shell user.

    Also, I don't like using the backtick operators in void context. If you aren't capturing the output in a variable, it is better to use the system function. One advantage is that the output isn't stored in a possibly huge variable; it is sent straight back to the web server. Another is that the return value can be checked. Finally, the array form of system can be used to bypass the shell.

      You are right about not using qx in void context.

      However the return status of the command you run with qx can be checked by $?>>8. Also qx bypasses the shell if it is given only one word with no special characters.