mirror of
https://github.com/fhem/fhem-mirror.git
synced 2025-02-07 16:59:18 +00:00
PRESENCE/presenced: fixed crash of presenced when receiving an invalid command (thanks to Sven); code makeup
git-svn-id: https://svn.fhem.de/fhem/trunk@10988 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
parent
fc6aa6b733
commit
af05573479
@ -42,6 +42,9 @@ use strict;
|
|||||||
|
|
||||||
|
|
||||||
sub Log($$);
|
sub Log($$);
|
||||||
|
sub timestamp();
|
||||||
|
sub daemonize();
|
||||||
|
sub doQuery($$);
|
||||||
|
|
||||||
my $new_client;
|
my $new_client;
|
||||||
my $server;
|
my $server;
|
||||||
@ -68,14 +71,11 @@ GetOptions(
|
|||||||
"l=s" => \$opt_l, "logfile=s" => \$opt_l,
|
"l=s" => \$opt_l, "logfile=s" => \$opt_l,
|
||||||
"p=i" => \$opt_p, "port=i" => \$opt_p,
|
"p=i" => \$opt_p, "port=i" => \$opt_p,
|
||||||
"P=s" => \$opt_P, "pid-file=s" => \$opt_P,
|
"P=s" => \$opt_P, "pid-file=s" => \$opt_P,
|
||||||
"h" => \$opt_h, "help" => \$opt_h);
|
"h" => \$opt_h, "help" => \$opt_h
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
Log 0, "=================================================" if($opt_l);
|
Log 0, "=================================================" if($opt_l);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Log 1, "started with PID $$";
|
Log 1, "started with PID $$";
|
||||||
|
|
||||||
if(-e "$opt_P")
|
if(-e "$opt_P")
|
||||||
@ -85,11 +85,8 @@ if(-e "$opt_P")
|
|||||||
exit 1;
|
exit 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sub print_usage ()
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
sub print_usage () {
|
|
||||||
print "Usage:\n";
|
print "Usage:\n";
|
||||||
print " presenced -d [-p <port>] [-P <filename>] \n";
|
print " presenced -d [-p <port>] [-P <filename>] \n";
|
||||||
print " presenced [-h | --help]\n";
|
print " presenced [-h | --help]\n";
|
||||||
@ -104,10 +101,8 @@ sub print_usage () {
|
|||||||
print " Print detailed log output\n";
|
print " Print detailed log output\n";
|
||||||
print " -h, --help\n";
|
print " -h, --help\n";
|
||||||
print " Print detailed help screen\n";
|
print " Print detailed help screen\n";
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if($opt_d)
|
if($opt_d)
|
||||||
{
|
{
|
||||||
daemonize();
|
daemonize();
|
||||||
@ -119,12 +114,10 @@ if($opt_h)
|
|||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
open(PIDFILE, ">$opt_P") or die("Could not open PID file $opt_P: $!");
|
open(PIDFILE, ">$opt_P") or die("Could not open PID file $opt_P: $!");
|
||||||
print PIDFILE $$."\n";
|
print PIDFILE $$."\n";
|
||||||
close PIDFILE;
|
close PIDFILE;
|
||||||
|
|
||||||
|
|
||||||
$server = new IO::Socket::INET (
|
$server = new IO::Socket::INET (
|
||||||
LocalPort => $opt_p,
|
LocalPort => $opt_p,
|
||||||
Proto => 'tcp',
|
Proto => 'tcp',
|
||||||
@ -140,8 +133,6 @@ Log 0, "created socket on ".$server->sockhost().":".$server->sockport();
|
|||||||
my $listener = IO::Select->new();
|
my $listener = IO::Select->new();
|
||||||
$listener->add($server);
|
$listener->add($server);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
my @new_handles;
|
my @new_handles;
|
||||||
my %child_handles;
|
my %child_handles;
|
||||||
my %child_config;
|
my %child_config;
|
||||||
@ -168,7 +159,6 @@ $server_pid = $$ unless(defined($server_pid));
|
|||||||
|
|
||||||
while(1)
|
while(1)
|
||||||
{
|
{
|
||||||
|
|
||||||
if($log_queue->pending)
|
if($log_queue->pending)
|
||||||
{
|
{
|
||||||
Log 2, $log_queue->dequeue;
|
Log 2, $log_queue->dequeue;
|
||||||
@ -184,7 +174,6 @@ while(1)
|
|||||||
|
|
||||||
$listener->add($new_client);
|
$listener->add($new_client);
|
||||||
Log 1, "new connection from ".$new_client->peerhost().":".$new_client->peerport();
|
Log 1, "new connection from ".$new_client->peerhost().":".$new_client->peerport();
|
||||||
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -193,11 +182,8 @@ while(1)
|
|||||||
|
|
||||||
if($buf)
|
if($buf)
|
||||||
{
|
{
|
||||||
|
|
||||||
$buf =~ s/(^\s*|\s*$)//g;
|
$buf =~ s/(^\s*|\s*$)//g;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if($buf =~ /^\s*([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}\s*\|\s*\d+\s*$/)
|
if($buf =~ /^\s*([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}\s*\|\s*\d+\s*$/)
|
||||||
{
|
{
|
||||||
$client->send("command accepted\n");
|
$client->send("command accepted\n");
|
||||||
@ -222,9 +208,6 @@ while(1)
|
|||||||
my $new_thread = threads->new(\&doQuery, ($write_handle, $address, $timeout));
|
my $new_thread = threads->new(\&doQuery, ($write_handle, $address, $timeout));
|
||||||
Log 2, "created thread ".$new_thread->tid()." for processing device $address within $timeout seconds for peer ".$client->peerhost().":".$client->peerport();
|
Log 2, "created thread ".$new_thread->tid()." for processing device $address within $timeout seconds for peer ".$client->peerhost().":".$client->peerport();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
$new_thread->detach();
|
$new_thread->detach();
|
||||||
|
|
||||||
$child_handles{$client} = $new_thread;
|
$child_handles{$client} = $new_thread;
|
||||||
@ -255,7 +238,6 @@ while(1)
|
|||||||
$queues{$child_handles{$client}->tid()}->enqueue("stop");
|
$queues{$child_handles{$client}->tid()}->enqueue("stop");
|
||||||
$client->send("command accepted\n");
|
$client->send("command accepted\n");
|
||||||
delete($child_handles{$client});
|
delete($child_handles{$client});
|
||||||
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -264,12 +246,9 @@ while(1)
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
||||||
$client->send("command rejected\n");
|
$client->send("command rejected\n");
|
||||||
$queues{$child_handles{$client}->tid()}->enqueue("stop");
|
|
||||||
Log 1, "received invalid command >>$buf<< from client ".$client->peerhost().":".$client->peerport();
|
Log 1, "received invalid command >>$buf<< from client ".$client->peerhost().":".$client->peerport();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -287,17 +266,10 @@ while(1)
|
|||||||
$client = undef;
|
$client = undef;
|
||||||
|
|
||||||
Log 1, "closed successfully all threads";
|
Log 1, "closed successfully all threads";
|
||||||
;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if(defined($sig_received))
|
if(defined($sig_received))
|
||||||
{
|
{
|
||||||
@ -307,10 +279,9 @@ while(1)
|
|||||||
Log 0, "exiting";
|
Log 0, "exiting";
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
sub daemonize
|
sub daemonize()
|
||||||
{
|
{
|
||||||
use POSIX;
|
use POSIX;
|
||||||
POSIX::setsid or die "setsid $!";
|
POSIX::setsid or die "setsid $!";
|
||||||
@ -346,8 +317,6 @@ my $nextrun = gettimeofday();
|
|||||||
my $cmd;
|
my $cmd;
|
||||||
my $run = 1;
|
my $run = 1;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if($address and $timeout)
|
if($address and $timeout)
|
||||||
{
|
{
|
||||||
THREADLOOP: while($run)
|
THREADLOOP: while($run)
|
||||||
@ -373,7 +342,6 @@ my $run = 1;
|
|||||||
$nextrun = gettimeofday();
|
$nextrun = gettimeofday();
|
||||||
|
|
||||||
$log_queue->enqueue(threads->tid()."|new address: $address - new timeout $timeout");
|
$log_queue->enqueue(threads->tid()."|new address: $address - new timeout $timeout");
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -383,15 +351,16 @@ my $run = 1;
|
|||||||
{
|
{
|
||||||
{
|
{
|
||||||
lock($querylocker);
|
lock($querylocker);
|
||||||
|
|
||||||
if($querylocker gt (gettimeofday() - 2))
|
if($querylocker gt (gettimeofday() - 2))
|
||||||
{
|
{
|
||||||
$log_queue->enqueue(threads->tid()."|waiting before hcitool command execution because last command was executed within the last 2 seconds");
|
$log_queue->enqueue(threads->tid()."|waiting before hcitool command execution because last command was executed within the last 2 seconds");
|
||||||
|
|
||||||
sleep rand(1) + 1;
|
sleep rand(1) + 1;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$hcitool = qx(which hcitool);
|
$hcitool = qx(which hcitool);
|
||||||
chomp $hcitool;
|
chomp $hcitool;
|
||||||
|
|
||||||
if( -x "$hcitool")
|
if( -x "$hcitool")
|
||||||
{
|
{
|
||||||
$return = qx(hcitool name $address 2>/dev/null);
|
$return = qx(hcitool name $address 2>/dev/null);
|
||||||
@ -400,9 +369,12 @@ my $run = 1;
|
|||||||
{
|
{
|
||||||
$write_handle->send("error\n") if(defined($write_handle));
|
$write_handle->send("error\n") if(defined($write_handle));
|
||||||
}
|
}
|
||||||
|
|
||||||
$querylocker = gettimeofday();
|
$querylocker = gettimeofday();
|
||||||
}
|
}
|
||||||
|
|
||||||
chomp $return;
|
chomp $return;
|
||||||
|
|
||||||
if(not $return =~ /^\s*$/)
|
if(not $return =~ /^\s*$/)
|
||||||
{
|
{
|
||||||
$write_handle->send("present;$return\n") if(defined($write_handle));
|
$write_handle->send("present;$return\n") if(defined($write_handle));
|
||||||
@ -414,19 +386,16 @@ my $run = 1;
|
|||||||
|
|
||||||
$nextrun = gettimeofday() + $timeout;
|
$nextrun = gettimeofday() + $timeout;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
sleep 1;
|
sleep 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
delete($queues{threads->tid()}) if(exists($queues{threads->tid()}));
|
delete($queues{threads->tid()}) if(exists($queues{threads->tid()}));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
sub timestamp
|
sub timestamp()
|
||||||
{
|
{
|
||||||
|
|
||||||
return POSIX::strftime("%Y-%m-%d %H:%M:%S",localtime);
|
return POSIX::strftime("%Y-%m-%d %H:%M:%S",localtime);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -440,6 +409,7 @@ sub Log($$)
|
|||||||
{
|
{
|
||||||
($thread, $message) = split("\\|", $message);
|
($thread, $message) = split("\\|", $message);
|
||||||
}
|
}
|
||||||
|
|
||||||
if($loglevel <= $opt_v)
|
if($loglevel <= $opt_v)
|
||||||
{
|
{
|
||||||
if($opt_l)
|
if($opt_l)
|
||||||
@ -455,5 +425,4 @@ sub Log($$)
|
|||||||
|
|
||||||
close(LOGFILE);
|
close(LOGFILE);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user