Thanx. I finally got it working with the following code.
use POSIX qw(:sys_wait_h);
use IO::Socket;
use DBI;
$port=7272;
$pid = fork;
exit if $pid;
die "Couldn't fork: $!" unless defined($pid);
POSIX::setsid() or die "Can't start a new session: $!";
$time_to_die=0;
sub signal_handler{
$time_to_die=1;
}
sub REAPER{
1 until (-1 == waitpid(-1, WNOHANG));
}
$SIG{INT} = $SIG{TERM} = $SIG{HUP} = \&signal_handler;
$SIG{CHLD} = \&REAPER;
$file="syncd.log";
open(LOG, ">>$file");
print LOG "Program Started : ".localtime()."\n";
until ($time_to_die){
### Debug statement (uncomment to debug to log file)
#print LOG "Until Loop\n";
$sock = IO::Socket::INET->new(LocalPort => $port,
Type => SOCK_STREAM,
Reuse => 1,
Listen => 25,
Timeout => 120) or die "Couldn't be
+a tcp server on port $port : $@\n";
while($new_sock = $sock->accept()) {
### Debug statements (uncomment to debug to log file)
#print LOG "While Loop\n";
#print LOG "Inside While new_sock Status: ".$new_sock."\n";
next if $child = fork;
die "child fork: $!" unless defined $child;
$sock->close;
login($new_sock);
exit 0;
}
close($sock);
### Debug statement (uncomment to debug to log file)
#print LOG "Outside While new_sock Status: ".$new_sock."\n";
if($new_sock!=$sock->accept()){
report_crash();
respawn_syncd();
print LOG "Socket is Undefined Program Crashed to prevent infi
+nite loop!\n";
$time_to_die=1;
}
}
close(LOG);
Again thanx for the help. If you think I should have done it some other way let me know and I re-evaluate my code. I would hate to have a badly coded program because they only cause headaches later. |