Some problems of faxrunqd

Shun-Jee Liu (liu@hermes.com.tw)
Fri, 31 Jan 1997 00:42:53 +0800 (CST)


Dear gert,

   First, thank you for your wonderful package.  I plan to use mgetty+sendfax
as dial in/out server, pager server, and fax server.  I am trying to
print from Windows 95 and view fax from WWW but still working.

   Attached is my patch for faxrunqd.  Except the bug fixed, I have added
one keyword 'spooltime'.  So I also add one line in faxspool:

	$echo "spooltime `date`" >>$job.q

   Hope it helps.

Shun-Jee Liu
Hermes-Epitek
Tel: 886-3-5790022 ext 418
Fax: 886-3-5798320


*** faxrunqd	Fri Jan 31 00:25:57 1997
--- faxrunqd.orig	Mon Jan 27 01:05:22 1997
***************
*** 17,23 ****
  ##
  ## (c) 1995 Bodo Bauer - S.u.S.E. GmbH, Fuerth
  ##
- ## 1997 Modified by Shun-Jee Liu, liu@atm.cm.nctu.edu.tw
  ## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  ## History:
  ##
--- 17,22 ----
***************
*** 25,37 ****
  ##   04IX95     0.41 -> Accounting                     
  ##   05IX95     0.42 -> Use private Lockfiles LCK...$tty.fax
  ##                      Runs on galois since 5IX95 16:30
- ##   01IX97     0.5  -> Fix some bugs
  ##
  ## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  ##             Please send pizzas and bug-reports to bb@suse.de
  ## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  
! $vers = "faxrunqd IX97 0.5";
  
  ## ----------------------------------------------------------------------------
  ## Configuration
--- 24,35 ----
  ##   04IX95     0.41 -> Accounting                     
  ##   05IX95     0.42 -> Use private Lockfiles LCK...$tty.fax
  ##                      Runs on galois since 5IX95 16:30
  ##
  ## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  ##             Please send pizzas and bug-reports to bb@suse.de
  ## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  
! $vers = "faxrunqd IX95 0.42";
  
  ## ----------------------------------------------------------------------------
  ## Configuration
***************
*** 42,48 ****
  ## ---------------------
  
  ## write debug info to
! $logfile   = "/var/spool/fax/outgoing/faxrunqd.log";
  
  ## where the spooled faxes will be found
  $spooldir  = "/var/spool/fax/outgoing";
--- 40,46 ----
  ## ---------------------
  
  ## write debug info to
! $logfile   = "/var/spool/fax/faxrunqd.log";
  
  ## where the spooled faxes will be found
  $spooldir  = "/var/spool/fax/outgoing";
***************
*** 54,66 ****
  $mailer    = "/usr/lib/sendmail";
  
  ## where lock files live in
! $lockdir   = "/var/lock";
  
  ## say we are alife
  $touch     = "/var/spool/fax/outgoing/.last_run";
  
  ## writing my own pid in
! $pidfile = "/var/run/faxrunqd.pid";
  
  ## accounting file, one line per fax
  $accfile = "/var/spool/fax/outgoing/faxrunqd.acc";
--- 52,64 ----
  $mailer    = "/usr/lib/sendmail";
  
  ## where lock files live in
! $lockdir   = "/var/spool/uucp";
  
  ## say we are alife
  $touch     = "/var/spool/fax/outgoing/.last_run";
  
  ## writing my own pid in
! $pidfile = "/etc/faxrunqd.pid";
  
  ## accounting file, one line per fax
  $accfile = "/var/spool/fax/outgoing/faxrunqd.acc";
***************
*** 74,88 ****
  $waittime  = 10;
  
  ## BUSY
! $maxbusy   = 10;
! $waitbusy  = 3;
  
  ##
  ## Other important things...
  ## -------------------------
  
  ## which lines to use for faxes
! @ttys      = ( "ttyS0", "ttyS1", "ttyS2", "ttyS3" );
  
  ## after processing the queue, faxrunqd will sleep for ?? seconds
  $sleeptime = 60;
--- 72,86 ----
  $waittime  = 10;
  
  ## BUSY
! $maxbusy   = 5;
! $waitbusy  = 15;
  
  ##
  ## Other important things...
  ## -------------------------
  
  ## which lines to use for faxes
! @ttys      = ( "ttyC0", "ttyC1", "ttyC2", "ttyC3" );
  
  ## after processing the queue, faxrunqd will sleep for ?? seconds
  $sleeptime = 60;
***************
*** 166,174 ****
  	##
  
  	close ( LOG );
- 	# Because system() will fork new process, and invoke SIGCHLD when
- 	# child is completed.
- 	$SIG { 'CHLD' } = 'DEFAULT';
  	$status = &faxit ( $job, $line );
  	exit $status;
  
--- 164,169 ----
***************
*** 191,226 ****
      local ( $childpid ) = 0;
  
      $childpid = wait();
-     if ($childpid == -1) {
- 	print LOG "Unknown reason: PID = $$, child PID = $childpid\n";
- 	return;
-     }
  
      $date = &getdate();
      print LOG "$child{$childpid} [$date]\n";
      print LOG "\t Child returns (PID $childpid)\n";
  
!     if ( -f "$spooldir/$child{$childpid}/JOB.$childpid" ) {
! 	open ( TMPLOG, "$spooldir/$child{$childpid}/JOB.$childpid" ); 
! 	while ( <TMPLOG> ) {
! 		print LOG $_;
! 	}
! 	close ( TMPLOG );
! 	unlink ( "$spooldir/$child{$childpid}/JOB.$childpid" );
      }
  
      $child{$childpid} = "*";
  
      ## Wait for the next child
      $SIG { 'CHLD' } = 'sigchld_handler';
  
!     # If this routine does not complete soon enough, some child may
!     # fail to invoke this handler ==>
!     #		child become zombie (perl <zombie>)
!     #		parent may hang
!     # if ( $sleep ) {
!     #	sleep ( $sleeptime );
!     # }
  }
  
  ## ----------------------------------------------------------------------------
--- 186,216 ----
      local ( $childpid ) = 0;
  
      $childpid = wait();
  
      $date = &getdate();
      print LOG "$child{$childpid} [$date]\n";
      print LOG "\t Child returns (PID $childpid)\n";
  
!     open ( TMPLOG, "$spooldir/$child{$childpid}/JOB.$childpid" ); 
!     while ( <TMPLOG> ) {
! 	print LOG $_;
      }
+     close ( TMPLOG );
+ 
+     ## flush logfile
+     close ( LOG );
+     open ( LOG, ">>$logfile" ) || die "unable to open file \"$logfile\"";
+ 
+     unlink ( "$spooldir/$child{$childpid}/JOB.$childpid" );
  
      $child{$childpid} = "*";
  
      ## Wait for the next child
      $SIG { 'CHLD' } = 'sigchld_handler';
  
!     if ( $sleep ) {
! 	sleep ( $sleeptime );
!     }
  }
  
  ## ----------------------------------------------------------------------------
***************
*** 232,241 ****
      local ( $mailto ) = $_[0];
      local ( $phone  ) = $_[1];
      local ( $job    ) = $_[2];
!     local ( $spool  ) = $_[3];
!     local ( $start  ) = $_[4];
!     local ( $end    ) = $_[5];
!     local ( $tries  ) = $_[6];
  
      open ( MAILER, "|$mailer $mailto" );
      print MAILER "To: $mailto\n";
--- 222,230 ----
      local ( $mailto ) = $_[0];
      local ( $phone  ) = $_[1];
      local ( $job    ) = $_[2];
!     local ( $start  ) = $_[3];
!     local ( $end    ) = $_[4];
!     local ( $tries  ) = $_[5];
  
      open ( MAILER, "|$mailer $mailto" );
      print MAILER "To: $mailto\n";
***************
*** 244,256 ****
      $date = &getdate();
  
      if ( $tries == 1 ) {
! 	print MAILER "Your fax has been sent successfully.\n\n";
      } else {
! 	$tries --;
! 	print MAILER "Sending succeeded after $tries unsuccessful tries.\n\n";
      }
!     print MAILER "Spool   Time: $spool\n" if $spool;
!     print MAILER "Deliver Time: $start -> $end\n";
      
      if ( -f "$spooldir/$job/JOB.log" ) {
  	print MAILER "\nFaxlog:\n\n";
--- 233,243 ----
      $date = &getdate();
  
      if ( $tries == 1 ) {
! 	print MAILER "Your fax has been sent successfully.\n";
      } else {
! 	print MAILER "Sending succeeded after $tries unsuccessful tries.\n";
      }
!     print MAILER "Time: $start -> $end\n";
      
      if ( -f "$spooldir/$job/JOB.log" ) {
  	print MAILER "\nFaxlog:\n\n";
***************
*** 272,284 ****
      local ( $mailto ) = $_[0];
      local ( $phone  ) = $_[1];
      local ( $job    ) = $_[2];
!     local ( $spool  ) = $_[3];
!     local ( $start  ) = $_[4];
!     local ( $end    ) = $_[5];
!     local ( $tries  ) = $_[6];
  
!     open ( MAILER, "|$mailer $mailto faxadmin" );
!     print MAILER "To: $mailto, faxadmin\n";
      print MAILER "Subject: Error in transmitting fax to $phone\n";
      print MAILER "From: root (Fax Subsystem)\n\n";
      print MAILER "Inputfile: $input\n";
--- 259,270 ----
      local ( $mailto ) = $_[0];
      local ( $phone  ) = $_[1];
      local ( $job    ) = $_[2];
!     local ( $start  ) = $_[3];
!     local ( $end    ) = $_[4];
!     local ( $tries  ) = $_[5];
  
!     open ( MAILER, "|$mailer $mailto" );
!     print MAILER "To: $mailto\n";
      print MAILER "Subject: Error in transmitting fax to $phone\n";
      print MAILER "From: root (Fax Subsystem)\n\n";
      print MAILER "Inputfile: $input\n";
***************
*** 289,298 ****
  	print MAILER ") $_";;
      }
      close ( JOBLOG );
! #    print MAILER "\nThe fax job is suspended, you can requeue it with the commands:\n\n";
! #    print MAILER "\tcd $spooldir/$job\n";
! #    print MAILER "\trm JOB.log\n";
! #    print MAILER "\tmv JOB.suspended JOB\n";
      close ( MAILER );
  }
  
--- 275,284 ----
  	print MAILER ") $_";;
      }
      close ( JOBLOG );
!     print MAILER "\nThe fax job is suspended, you can requeue it with the commands:\n\n";
!     print MAILER "\tcd $spooldir/$job\n";
!     print MAILER "\trm JOB.log\n";
!     print MAILER "\tmv JOB.suspended JOB\n";
      close ( MAILER );
  }
  
***************
*** 316,322 ****
      local ( $tries  ) = 0;
      local ( $status ) = -1;
  
-     local ( $spooldate );
      local ( $startdate );
      local ( $enddate   );
      local ( $mailto    );
--- 302,307 ----
***************
*** 338,345 ****
  	    ( $dummy, $mail ) = split ( /\s/, $_, 2 );
  	} elsif ( /^phone/ ) {
  	    ( $dummy, $phone ) = split ( /\s/, $_, 2 );
- 	} elsif ( /^spooltime/ ) {
- 	    ( $dummy, $spooldate ) = split ( /\s/, $_, 2 );
  	} elsif ( /^time/ ) {
  	    ( $dummy, $time ) = split ( /\s/, $_, 2 );
  	} elsif ( /^verbose_to/ ) {
--- 323,328 ----
***************
*** 393,398 ****
--- 376,383 ----
  				 
      print TMPLOG "\t [$tries] Exit Status: $status ($sendfax_ret[$status])\n";
      
+     open ( JOB, ">> $spooldir/$job/JOB.locked" );
+ 				 
      if ( $status == 0 ) {
  	##
  	## transmission successful
***************
*** 400,412 ****
  	print TMPLOG "\t transmission successful\n";
  	print TMPLOG "\t time:$startdate -> $enddate\n";
  	
- 	## notify user
- 	&mail_success( $mailto, $phone, $job, $spooldate, $startdate, $enddate, $tries );
- 
  	## remove job
  	system ( "rm -f $spooldir/$job/*" );
  	system ( "rmdir $spooldir/$job" );
  	
  	## write accounting info
  	open ( ACC, ">>$accfile" ) || die "can not open file: $accfile";
  	print ACC "SUCCESS: JOB $job BY $mailto TO $phone TRIES $tries TIME $startdate -> $enddate\n";
--- 385,397 ----
  	print TMPLOG "\t transmission successful\n";
  	print TMPLOG "\t time:$startdate -> $enddate\n";
  	
  	## remove job
  	system ( "rm -f $spooldir/$job/*" );
  	system ( "rmdir $spooldir/$job" );
  	
+ 	## notify user
+ 	&mail_success( $mailto, $phone, $job, $startdate, $enddate, $tries );
+ 
  	## write accounting info
  	open ( ACC, ">>$accfile" ) || die "can not open file: $accfile";
  	print ACC "SUCCESS: JOB $job BY $mailto TO $phone TRIES $tries TIME $startdate -> $enddate\n";
***************
*** 465,474 ****
  	    }
  	    $newtime = $hour*100 + $min;
  	    print TMPLOG "\t next try after $newtime\n";
- 
- 	    open ( JOB, ">>$spooldir/$job/JOB.locked" );
- 	    print JOB "time $newtime\n";
- 	    close ( JOB );
  	    
  	}
  	
--- 450,455 ----
***************
*** 477,483 ****
  	    ## suspend job and notify user
  	    ##
  	    rename ( "$spooldir/$job/JOB.locked", "$spooldir/$job/JOB.suspended" );
! 	    &mail_error( $mailto, $phone, $job, $spooldate, $startdate, $enddate, $tries );
  	    print TMPLOG "\t Suspending job\n";
  
  	    ## write accounting info
--- 458,464 ----
  	    ## suspend job and notify user
  	    ##
  	    rename ( "$spooldir/$job/JOB.locked", "$spooldir/$job/JOB.suspended" );
! 	    &mail_error( $mailto, $phone, $job, $startdate, $enddate, $tries );
  	    print TMPLOG "\t Suspending job\n";
  
  	    ## write accounting info
***************
*** 508,514 ****
  $date = &getdate();
  
  open ( LOG, ">>$logfile" ) || die "unable to open file \"$logfile\"";
- select((select(LOG), $| = 1)[0]);
  
  print LOG "\n**** $vers\n**** [$date] PID $$\n\n";
  
--- 489,494 ----
***************
*** 525,540 ****
  $SIG { 'CHLD' } = 'sigchld_handler';
  
  ##
- ## remove stale locks
- ##
- system ( "rm -f $lockdir/LCK..*.fax" );
- 
- ##
  ## go to fax spool directory, process all JOB files
  ##
  if ( -d $spooldir ) {
  
      while ( 1 ) {
  	## we are alife!
  	open ( TOUCH, ">$touch" ) || die "unable to open file \"$touch\"";
  	print TOUCH "$date\n";
--- 505,516 ----
  $SIG { 'CHLD' } = 'sigchld_handler';
  
  ##
  ## go to fax spool directory, process all JOB files
  ##
  if ( -d $spooldir ) {
  
      while ( 1 ) {
+ 
  	## we are alife!
  	open ( TOUCH, ">$touch" ) || die "unable to open file \"$touch\"";
  	print TOUCH "$date\n";
***************
*** 553,559 ****
  		print LOG "$job [$date]\n";
  		
  		## lock the job
! 		next unless rename ( "$spooldir/$job/JOB", "$spooldir/$job/JOB.locked" );
  
  		## is it time to do the job?
  		$now = &gettime();
--- 529,535 ----
  		print LOG "$job [$date]\n";
  		
  		## lock the job
! 		rename ( "$spooldir/$job/JOB", "$spooldir/$job/JOB.locked" );
  
  		## is it time to do the job?
  		$now = &gettime();
***************
*** 573,579 ****
  		} else {
  		    
  		    ## which line to use?
! 		    $line = "***";
  		    foreach $tty ( @ttys ) {
  			if ( ! -f "$lockdir/LCK..$tty" && ! -f "$lockdir/LCK..$tty.fax" ) {
  			    $line = $tty;
--- 549,555 ----
  		} else {
  		    
  		    ## which line to use?
! 		    $tty = "***";
  		    foreach $tty ( @ttys ) {
  			if ( ! -f "$lockdir/LCK..$tty" && ! -f "$lockdir/LCK..$tty.fax" ) {
  			    $line = $tty;
***************
*** 595,606 ****
  			## fax it
  			&spawn_fax ( $job, $line );
  			## give a little time to lock the line
! 			# Not necessary, because LCK..$line.fax is created
! 			# sleep ( 10 );
  		    }
  		}
  	    }
  	}
  	## wait some time...
  	$sleep = 1;
  	sleep ( $sleeptime );
--- 571,585 ----
  			## fax it
  			&spawn_fax ( $job, $line );
  			## give a little time to lock the line
! 			sleep ( 10 );
  		    }
  		}
  	    }
+ 	    ## flush logfile
+ 	    close ( LOG );
+ 	    open ( LOG, ">>$logfile" ) || die "unable to open file \"$logfile\"";
  	}
+ 
  	## wait some time...
  	$sleep = 1;
  	sleep ( $sleeptime );
***************
*** 611,613 ****
--- 590,601 ----
      print LOG "there is no spooldir: \"$spooldir\"\n<--\n";
      die "there is no spooldir: \"$spooldir\"";
  }
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+