Solved

setting a timeout

Posted on 1998-09-09
6
220 Views
Last Modified: 2010-03-05
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;
}
0
Comment
Question by:idadan
  • 3
  • 2
6 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 1204782
What is &lock
0
 

Author Comment

by:idadan
ID: 1204783
A file locking routine, from the cgi-perl cookbook (matt wright)...
0
 

Accepted Solution

by:
arhnold earned 100 total points
ID: 1204784
Hello,
      One method I use when I want to have a timeout on some part of a script is to use SIGALRM, with the signal handler set to a subroutine to handle the case.  Here is an example hacked from a program of mine...

..yada yada yada...
$timeout = 60;
$SIG{'ALRM'} = 'Wait_For_Timeout'; # A subroutine
alarm($timeout)  # Starts the alarm timer ticking down
   # Main reading loop.
   while ($go_on) {
      &Go_Do_Something(); # This has $timeout seconds to return
                          # else the ALRM will pop  
      # We returned in time, restart the timer.              
      alarm($timeout);
      ... yada yada yada...


## In this case here is what the signal handler would do...
sub Wait_For_Timeout {
   #####################################
   # Signal Handler for Wait_For (ALRM).
   #####################################
   &dprint("\n\t** Script Timeout.  Moving on...\n");
   $go_on=0;  # Breaks the while() loop above
}

      Hope this helps.
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:idadan
ID: 1204785
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.
0
 

Expert Comment

by:arhnold
ID: 1204786
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
0
 

Author Comment

by:idadan
ID: 1204787
Thanks for the help.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now