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

Hello All,
On some purpose I wrote two small programs start.cgi and server.cgi:

start.cgi
#!/usr/bin/perl -w open(PID, "< pid") || die "Can't open PID file: $!"; $pid = <PID>; close PID; if(defined $pid){ if($pid==0){ # print "Location: http://test/test.html \n\n"; print "Content-type: text/html\n\n"; print "<html>hello<html>"; exec '/usr/sites/test/vcgi/server.cgi'; } }
server.cgi
#!/usr/bin/perl -w # use IO::Socket; open(PID, "> pid") || die "Can't open PID file: $!"; print PID $$ ; close PID;
Both programs and pid file are located at '/usr/sites/test/vcgi' which is set as 'cgi-bin' dir in Apache config file. Start.cgi is executed from a browser. It checks if server.cgi is on (reading pid file) runs it if it's not and returns 'hello'. At the beginning pid file contains '0'. test.html page contains nothing but html and body tags

The problem.
Programs work fine until I remove comments from 'use..' statement in server.cgi OR replace two print statements with 'Location...' one in start.cgi.
If I do any of those server.cgi writes nothing into pid and I'm not sure if it's executed at all.
If I run start.cgi from shell everything works fine with 'use..' and 'Location...'.
I don't undertand this at all and will be appreciated if anyone tell me where the source of this problem might be.
Thank you.

Replies are listed 'Best First'.
Re: CGI problems - please help
by bobn (Chaplain) on Sep 11, 2003 at 13:01 UTC

    You will want to look in your webserver's error logs for clues. For Apache, this is typically a file named error_log. On Linux, at least, you can find it by typing

    locate error_log
    

    --Bob Niederman, http://bob-n.com

    All code given here is UNTESTED unless otherwise stated.

      Yes, I know. It is empty.
Re: CGI problems - please help
by zby (Vicar) on Sep 11, 2003 at 12:58 UTC
    I can't spot anything that could couse this problem (assuming the file permissions are right and your testing procedure was sound). I think you should just play with the code and try to write the program in some other way.

    One way I would propose is to get rid of the exec call and instead pull the whole code from the server.cgi into the block where the exec is being called.

    By the way there are race conditions in this code. Two (or more) instances of the program can read simultaneusly the "0" value from the pid file and think that they are the first. You should lock the file (see perldoc -f flock).

      Unfortunately, I can't get rid of exec call because the whole idea is that start.cgi is a client programm which must check if server is running and start it if it's not. I can't get rid of location also because start.cgi must then redirect client to a page which will connect to the server via sockets.
        Then you should not use exec to run the server but rather system. With exec the server will keep the socket to the client open. And probably die with SIGPIPE when the client closes it.

        Update: Or you can close the pipe explicitely in the server code.

        I believe you should fork the server before using the exec function.