Link to home
Start Free TrialLog in
Avatar of idadan
idadan

asked on

setting a timeout

Here is a copy of a admin at my hosting copany "I am thinking because it involves uploading the program is just not
executing but yet hanging around and keeping httpd childs open until we kill
them off.  Could you set a timeout or somethiong on this script?"  How would I do this?  Here is the script.  Thanks for any suggestions/help.
#!/usr/local/bin/perl
    use CGI;
      require '/home/dank/public_html/cgi-local/chat/locksubs.pl';

    $cgi = new CGI;

print "Content-type: text/html\n\n";


    if ($cgi->param('file') eq '') {
      &no_pic;
}
      $username = $cgi->param('USERNAME');
      $pics_dir = '/home/dank/public_html/cgi-local/pics';
    $filename = "/home/dank/public_html/cgi-local/pics/$username.gif";
    open(OUT,"> $filename");
    while($bytesread = read($cgi->param('file'), $buffer, 1024)) {
        print OUT $buffer;
    }
    close(OUT);
      &lock("waiting-count.db","$pics_dir",'3');
      open(COUNT,"<$pics_dir/waiting-count.db");
      $count = <COUNT>;
      close(COUNT);
      $count++;
      open(COUNT,">$pics_dir/waiting-count.db");
      print COUNT "$count";
      close(COUNT);
      &unlock("waiting-count.db","$pics_dir");      
      &lock("waiting.db","$pics_dir",'3');
      open(WAIT,">>$pics_dir/waiting.db");
      print WAIT "$username--$count\n";
      close(WAIT);
      &unlock("waiting.db","$pics_dir");

print "done. username = $username\n";

sub no_pic {
      print "sorry please give a pic to process.\n";
      exit;
}
Avatar of ozo
ozo
Flag of United States of America image

What is &lock
Avatar of idadan
idadan

ASKER

A file locking routine, from the cgi-perl cookbook (matt wright)...
ASKER CERTIFIED SOLUTION
Avatar of arhnold
arhnold

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of idadan

ASKER

Ok... in this line
    $SIG{'ALRM'} = 'Wait_For_Timeout'; # A subroutine
is that legal? It looks funny to me.. and also shouldn't you define $go_on as something before calling it or won't it just read that as 0?  I need this code to work as is, if there is anything wrong, could you just look it over and check it?  thanks.
Yes this is legal.  If you have the Programming Perl v2 book look at p340-1 for an example.  The only difference is that they use
$SIG{'x'} = \&foo, but its the same thing in the end.  

      The $go_on variable was defined a couple hundred lines previous in the script.  It was declared as a 'my' and was visible to all code in this file.

      You could keep your code pretty much the same.  Write a new subroutine to cleanup the locks on a timeout and print an error message.  Then wrap the area of the code you wish to guard with the timer stuff. e.g

$SIG{'ALRM'} = \&My_Timeout;

alarm(60);
&lock("waiting.db","$pics_dir",'3');
   open(WAIT,">>$pics_dir/waiting.db");
   print WAIT "$username--$count\n";
   close(WAIT);
   &unlock("waiting.db","$pics_dir");
## We completed this part in under 60 seconds, cancel the timer
alarm(0);

$SIG{'ALRM'} = 'DEFAULT'; # Return to normal response
Avatar of idadan

ASKER

Thanks for the help.